@@ -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
515516def 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 ] = []
0 commit comments