Skip to content

Commit b386858

Browse files
Merge pull request #293 from MontgomeryLab/issue-285
tiny-plot: sort classes in class_charts by natural order
2 parents 38b4050 + dc08e39 commit b386858

8 files changed

Lines changed: 36 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ At the core of tinyRNA is tiny-count, a highly flexible counting utility that al
161161
A wrapper R script for DESeq2 facilitates DGE analysis of counted sample files.
162162

163163
### `tiny-plot`
164-
The results of feature counting and DGE analysis are visualized with high resolution plot PDFs. User-defined plot styles are also supported via a Matplotlib stylesheet.
164+
The results of feature counting and DGE analysis are visualized with high resolution plot PDFs. User-defined plot styles are also supported via a Matplotlib style sheet.
165165

166166
[Full documentation for tiny-plot can be found here.](doc/tiny-plot.md)
167167

doc/Configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ To make it simple to specify your fastq files and their locations, along with as
3232

3333
Small RNAs can often be classified by sequence characteristics, such as length, strandedness, and 5' nucleotide. We provide a Features Sheet (`features.csv`) in which you can define selection rules to more accurately capture counts for the small RNAs of interest. [More info](#features-sheet-details).
3434

35-
#### Plot Stylesheet
35+
#### Plot Style Sheet
3636

37-
Plot styles can be optionally overridden using a matplotlibrc stylesheet. [More info](#plot-stylesheet-details).
37+
Plot styles can be optionally overridden using a matplotlibrc style sheet. [More info](#plot-style-sheet-details).
3838

3939
## Editing YAML Files
4040
The Run Config and Paths File are YAML formatted files that can be edited with a text editor. Changing values in these files is pretty straight forward, but it is useful to know a little about YAML syntax.
@@ -174,5 +174,5 @@ Examples:
174174
### Case Sensitivity
175175
All selectors are case-insensitive.
176176

177-
## Plot Stylesheet Details
178-
Matplotlib uses key-value "rc parameters" to allow for customization of its properties and styles, and one way these parameters can be specified is with a [matplotlibrc file](https://matplotlib.org/3.4.3/tutorials/introductory/customizing.html#a-sample-matplotlibrc-file), which we simply refer to as the Plot Stylesheet. You can obtain a copy of the default stylesheet used by tiny-plot with the command `tiny get-templates`. Please keep in mind that tiny-plot overrides these defaults for a few specific elements of certain plots. Feel free to reach out if there is a plot style you wish to override but find you are unable to.
177+
## Plot Style Sheet Details
178+
Matplotlib uses key-value "rc parameters" to allow for customization of its properties and styles, and one way these parameters can be specified is with a [matplotlibrc file](https://matplotlib.org/3.5.2/tutorials/introductory/customizing.html#a-sample-matplotlibrc-file), which we simply refer to as the Plot Style Sheet. You can obtain a copy of the default style sheet used by tiny-plot with the command `tiny get-templates`, and your modified style sheet can be passed using the [plot_style_sheet parameter](Parameters.md#style-sheet). Please keep in mind that tiny-plot overrides these defaults for a few specific elements of certain plots. Feel free to reach out if there is a plot style you wish to override but find you are unable to.

doc/Parameters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Feature expression levels are considered significant if their P value is less th
217217
|----------------|-------------------|--------------------------|
218218
| | plot_style_sheet: | `--style-sheet MPLSTYLE` |
219219

220-
The plot style sheet can be used to override the default Matplotlib styles used by tiny-plot. Unlike the other parameters, this option is found in the Paths File. See the [Plot Stylesheet documentation](Configuration.md#plot-stylesheet-details) for more information.
220+
The plot style sheet can be used to override the default Matplotlib styles used by tiny-plot. Unlike the other parameters, this option is found in the Paths File. The expected value for this parameter is the path to your modified style sheet. See the [Plot Style Sheet documentation](Configuration.md#plot-style-sheet-details) for more information.
221221

222222
### Vector Scatter
223223
| Run Config Key | Commandline Argument |
@@ -241,7 +241,7 @@ The min and/or max bounds for plotted lengths can be set with this option. See [
241241
| plot_dge_scatter_min: | `--dge-min VALUE` |
242242
| plot_dge_scatter_max: | `--dge-max VALUE` |
243243

244-
The min and/or max bounds for DGE scatter plots can be set with this option. The value you provide should be a log2 count value and can be whole or fractional, e.g. `--dge-min 1.9` would produce a plot whose first tick mark is labeled 2 and would include points for feature counts as low as 3.74. Unspecified bounds are automatically calculated to fit the data, and will include the margin specified by the `axes.[x/y]margin` key in the [Plot Stylesheet](Configuration.md#plot-stylesheet-details).
244+
The min and/or max bounds for DGE scatter plots can be set with this option. The value you provide should be a log2 count value and can be whole or fractional, e.g. `--dge-min 1.9` would produce a plot whose first tick mark is labeled 2 and would include points for feature counts as low as 3.74. Unspecified bounds are automatically calculated to fit the data, and will include the margin specified by the `axes.[x/y]margin` key in the [Plot Style Sheet](Configuration.md#plot-style-sheet-details).
245245

246246
### Labels for Class-related Plots
247247
| Run Config Key | Commandline Argument |

doc/tiny-plot.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Rules are referred to by their row number in the Features Sheet and the first no
5959
- Alignments which do not overlap with any features
6060

6161
#### Rule Chart Styles
62-
Percentage label darkness and bar colors reflect the magnitude of the rule's contribution. Magnitude is always considered on a 0-100% scale, rather than scaling down to the chart's view limits. These styles cannot be changed using a plot stylesheet.
62+
Percentage label darkness and bar colors reflect the magnitude of the rule's contribution. Magnitude is always considered on a 0-100% scale, rather than scaling down to the chart's view limits. These styles cannot be changed using a plot style sheet.
6363

6464

6565

@@ -111,13 +111,25 @@ The control condition is plotted on the x-axis, but it must be specified in your
111111
#### View Limits
112112
Both the lower and upper bound of the plot's axes [can be set manually](Parameters.md#bounds-for-lendist-charts). Unspecified bounds are automatically calculated to fit the data.
113113

114+
#### Zero-Count Features
115+
Due to the plot's log scale, points are not plotted for features that have 0 reads in one of the compared conditions. Zero-count features will be supported in a future release.
116+
114117

115118

116119
## sample_avg_scatter_by_dge_class
117-
The previous plot type can be extended to group and color differentially expressed features by class. Classes are sorted by abundance before plotting to maximize representation. You can also filter the classes displayed using [plot_class_scatter_filter](Parameters.md#filtering-classes-in-dge-class-scatter-plots)
120+
The previous plot type can be extended to group and color differentially expressed features by class.
118121

119122
<p float="left" align="center">
120123
<img src="../images/plots/scatter_dge_class.jpg" width="100%" alt="sample_avg_scatter_by_dge_class"/>
121124
</p>
122125

123-
>**Tip**: if you find that two groups of interest share proximity and are too similar in color, you can change the group's color with a modified Plot Stylesheet. The groups will be colored in the same order they are listed in the legend (not including P value outgroup), e.g. changing the color of the ERGO group means changing the 5th color in the `axes.prop_cycle` color cycler. See the [config file documentation](Configuration.md#plot-stylesheet-details) for more info about the Plot Stylesheet.
126+
#### Filtering Classes
127+
You can filter which classes are displayed using [plot_class_scatter_filter](Parameters.md#filtering-classes-in-dge-class-scatter-plots).
128+
129+
#### Zero-Count Classes
130+
If all features have 0 reads for a given class in one of the compared conditions, then that class is omitted from the plot and legend due to the plot's log scale. Zero-count classes will be supported in a future release.
131+
132+
#### Customizing Group Colors
133+
If you find that two groups of interest share proximity and are too similar in color, you can change the group's color with a modified Plot Style Sheet. Group colors are assigned from the `axes.prop_cycle` color cycler when there are fewer groups than colors, or from the [tab20](https://matplotlib.org/3.5.2/tutorials/colors/colormaps.html#qualitative) colormap when groups outnumber colors. First, the total list of unique classes is gathered from the counts table and sorted, and the resulting list of classes is assigned colors in the order produced by the cycler.
134+
135+
For example, changing the color of the miRNA group in the above plot means changing the 6th color in the `axes.prop_cycle` list (assuming all classes are represented in the plot). The P value outgroup is always the same color and doesn't affect the assignment process. See the [config file documentation](Configuration.md#plot-style-sheet-details) for more info about the Plot Style Sheet.

images/plots/class_chart.jpg

-1.23 KB
Loading

images/plots/scatter_dge_class.jpg

-16.1 KB
Loading

tiny/rna/plotter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
from pkg_resources import resource_filename
1919

2020
from tiny.rna.plotterlib import plotterlib
21-
from tiny.rna.util import report_execution_time, make_filename, SmartFormatter, timestamp_format, add_transparent_help
21+
from tiny.rna.util import (
22+
report_execution_time,
23+
add_transparent_help,
24+
timestamp_format,
25+
SmartFormatter,
26+
sorted_natural,
27+
make_filename
28+
)
29+
2230

2331
aqplt: plotterlib = None
2432
RASTER: bool = True
@@ -185,7 +193,8 @@ def class_charts(raw_class_counts: pd.DataFrame, mapped_reads: pd.Series, out_pr
185193
kwargs: Additional keyword arguments to pass to pandas.DataFrame.plot()
186194
"""
187195

188-
class_props = get_proportions_df(raw_class_counts, mapped_reads, class_na, scale).sort_index()
196+
class_props = get_proportions_df(raw_class_counts, mapped_reads, class_na, scale)
197+
class_props = class_props.reindex(sorted_natural(class_props.index))
189198
max_prop = class_props.max().max()
190199

191200
for library in raw_class_counts:

tiny/rna/plotterlib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def assign_class_colors(classes):
336336
else:
337337
colors = iter(plt.get_cmap("tab20"))
338338

339-
return {cls: next(colors) for cls in classes}
339+
return {cls: next(colors) for cls in sorted_natural(classes)}
340340

341341
def set_dge_class_legend_style(self):
342342
"""Widens the "scatter" figure and moves plot to the left to accommodate legend"""
@@ -383,13 +383,13 @@ def get_scatter_view_lims(counts_df: pd.DataFrame, vmin: int = None, vmax: int =
383383
x0, x1 = (minpos if x0 <= 0 else x0,
384384
minpos if x1 <= 0 else x1)
385385

386-
# Get axes margin preferences from stylesheet
386+
# Get axes margin preferences from style sheet
387387
rc_mar = {mpl.rcParams.get(f"axes.{m}", 0)
388388
for m in ('xmargin', 'ymargin')}
389389

390390
margin = max(rc_mar)
391391
if len(rc_mar) != 1:
392-
print("Stylesheet values for axes.xmargin and axes.ymargin differ. "
392+
print("Style sheet values for axes.xmargin and axes.ymargin differ. "
393393
"The larger value will be chosen for the scatter plot margin.",
394394
file=sys.stderr)
395395

0 commit comments

Comments
 (0)