Skip to content

Commit 90f6155

Browse files
authored
Merge pull request #197 from HelikarLab/hotfix
Write zFPKM Graphs
2 parents fcd6857 + 64dab9c commit 90f6155

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

main/como/rnaseq.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,11 @@ def zfpkm_transform(
460460
return results, zfpkm_df
461461

462462

463-
def zfpkm_plot(results, *, plot_xfloor: int = -4, subplot_titles: bool = True):
463+
def zfpkm_plot(results, *, write_png_filepath: Path, plot_xfloor: int = -4, subplot_titles: bool = True):
464464
"""Plot the log2(FPKM) density and fitted Gaussian for each sample.
465465
466466
:param results: A dictionary of intermediate results from zfpkm_transform.
467+
:param write_png_filepath: The path to write the plot to
467468
:param: subplot_titles: Whether to display facet titles (sample names).
468469
:param plot_xfloor: Lower limit for the x-axis.
469470
:param subplot_titles: Whether to display facet titles (sample names).
@@ -509,7 +510,7 @@ def zfpkm_plot(results, *, plot_xfloor: int = -4, subplot_titles: bool = True):
509510
fig.update_layout(legend_tracegroupgap=0)
510511

511512
fig.update_layout(height=600 * len(results), width=1000, title_text="zFPKM Plots", showlegend=True)
512-
fig.write_image("zfpkm_plot.png")
513+
fig.write_image(write_png_filepath)
513514

514515

515516
def calculate_z_score(metrics: NamedMetrics) -> NamedMetrics:
@@ -619,7 +620,13 @@ def tpm_quantile_filter(*, metrics: NamedMetrics, filtering_options: _FilteringO
619620
return metrics
620621

621622

622-
def zfpkm_filter(*, metrics: NamedMetrics, filtering_options: _FilteringOptions, calcualte_fpkm: bool) -> NamedMetrics:
623+
def zfpkm_filter(
624+
*,
625+
metrics: NamedMetrics,
626+
filtering_options: _FilteringOptions,
627+
write_png_filepath: Path,
628+
calcualte_fpkm: bool,
629+
) -> NamedMetrics:
623630
"""Apply zFPKM filtering to the FPKM matrix for a given sample."""
624631
min_sample_expression = filtering_options.replicate_ratio
625632
high_confidence_sample_expression = filtering_options.high_replicate_ratio
@@ -637,7 +644,7 @@ def zfpkm_filter(*, metrics: NamedMetrics, filtering_options: _FilteringOptions,
637644
minimums = matrix == 0
638645
results, zfpkm_df = zfpkm_transform(matrix)
639646
zfpkm_df[minimums] = -4
640-
zfpkm_plot(results)
647+
zfpkm_plot(results, write_png_filepath=write_png_filepath)
641648

642649
# determine which genes are expressed
643650
min_samples = round(min_sample_expression * len(zfpkm_df.columns))
@@ -661,6 +668,7 @@ def filter_counts(
661668
technique: FilteringTechnique,
662669
filtering_options: _FilteringOptions,
663670
prep: RNASeqPreparationMethod,
671+
write_zfpkm_png_filepath: Path,
664672
) -> NamedMetrics:
665673
"""Filter the count matrix based on the specified technique."""
666674
match technique:
@@ -671,9 +679,19 @@ def filter_counts(
671679
case FilteringTechnique.tpm:
672680
return tpm_quantile_filter(metrics=metrics, filtering_options=filtering_options)
673681
case FilteringTechnique.zfpkm:
674-
return zfpkm_filter(metrics=metrics, filtering_options=filtering_options, calcualte_fpkm=True)
682+
return zfpkm_filter(
683+
metrics=metrics,
684+
filtering_options=filtering_options,
685+
write_png_filepath=write_zfpkm_png_filepath,
686+
calcualte_fpkm=True,
687+
)
675688
case FilteringTechnique.umi:
676-
return zfpkm_filter(metrics=metrics, filtering_options=filtering_options, calcualte_fpkm=False)
689+
return zfpkm_filter(
690+
metrics=metrics,
691+
filtering_options=filtering_options,
692+
write_png_filepath=write_zfpkm_png_filepath,
693+
calcualte_fpkm=False,
694+
)
677695
case _:
678696
raise ValueError(f"Technique must be one of {FilteringTechnique}")
679697

@@ -692,6 +710,7 @@ async def save_rnaseq_tests(
692710
high_batch_ratio: float,
693711
technique: FilteringTechnique,
694712
cut_off: int | float,
713+
write_zfpkm_png_filepath: Path,
695714
):
696715
"""Save the results of the RNA-Seq tests to a CSV file."""
697716
filtering_options = _FilteringOptions(
@@ -726,6 +745,7 @@ async def save_rnaseq_tests(
726745
technique=technique,
727746
filtering_options=filtering_options,
728747
prep=prep,
748+
write_zfpkm_png_filepath=write_zfpkm_png_filepath,
729749
)
730750

731751
expressed_genes: list[str] = []

main/como/rnaseq_gen.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import asyncio
55
from dataclasses import dataclass
6+
from pathlib import Path
67

78
import pandas as pd
89
from fast_bioservices import Taxon
@@ -24,6 +25,7 @@ class _Arguments:
2425
minimum_cutoff: int | str
2526
library_prep: RNASeqPreparationMethod
2627
taxon: Taxon
28+
write_zfpkm_png_filepath: Path
2729

2830
def __post_init__(self):
2931
self.library_prep = RNASeqPreparationMethod.from_string(str(self.library_prep))
@@ -48,6 +50,7 @@ async def _handle_context_batch(
4850
cut_off: int | float | str,
4951
prep: RNASeqPreparationMethod,
5052
taxon: Taxon,
53+
write_zfpkm_png_filepath: Path,
5154
) -> None:
5255
"""Iterate through each context type and create rnaseq expression file.
5356
@@ -110,6 +113,7 @@ async def _handle_context_batch(
110113
technique=technique,
111114
cut_off=cut_off,
112115
taxon_id=taxon,
116+
write_zfpkm_png_filepath=write_zfpkm_png_filepath,
113117
)
114118
logger.success(f"Results saved at '{rnaseq_output_filepath}'")
115119

@@ -119,6 +123,7 @@ async def rnaseq_gen(
119123
config_filename: str,
120124
prep: RNASeqPreparationMethod,
121125
taxon_id: int | str | Taxon,
126+
write_zfpkm_png_filepath: Path,
122127
replicate_ratio: float = 0.5,
123128
high_replicate_ratio: float = 1.0,
124129
batch_ratio: float = 0.5,
@@ -180,6 +185,7 @@ async def rnaseq_gen(
180185
cut_off=cut_off,
181186
prep=prep,
182187
taxon=taxon_id,
188+
write_zfpkm_png_filepath=write_zfpkm_png_filepath,
183189
)
184190

185191

@@ -287,6 +293,12 @@ def _parse_args() -> _Arguments:
287293
"Will separate samples into groups to only compare similarly prepared libraries. "
288294
"For example, mRNA, total-rna, scRNA, etc",
289295
)
296+
parser.add_argument(
297+
"--write-zfpkm-png-filepath",
298+
required=False,
299+
type=Path,
300+
help="If using zFPKM, the location to write graphs",
301+
)
290302
args = parser.parse_args()
291303
args.filtering_technique = args.filtering_technique.lower()
292304
args.taxon = Taxon.from_int(int(args.taxon)) if str(args.taxon).isdigit() else Taxon.from_string(str(args.taxon)) # type: ignore
@@ -306,5 +318,6 @@ def _parse_args() -> _Arguments:
306318
cut_off=args.minimum_cutoff,
307319
prep=args.library_prep,
308320
taxon_id=args.taxon,
321+
write_zfpkm_png_filepath=args.write_zfpkm_png_filepath,
309322
)
310323
)

0 commit comments

Comments
 (0)