2727from scipy .signal import find_peaks
2828from sklearn .neighbors import KernelDensity
2929
30- from como .custom_types import RNAPrepMethod
3130from como .migrations import gene_info_migrations
32- from como .project import Config
31+ from como .types import RNAPrepMethod
3332from como .utils import convert_gene_data
3433
3534
@@ -525,20 +524,17 @@ def calculate_z_score(metrics: NamedMetrics) -> NamedMetrics:
525524
526525def cpm_filter (
527526 * ,
528- context_name : str ,
529527 metrics : NamedMetrics ,
530528 filtering_options : _FilteringOptions ,
531- prep : RNAPrepMethod ,
529+ output_csv_filepath : Path ,
532530) -> NamedMetrics :
533531 """Apply Counts Per Million (CPM) filtering to the count matrix for a given sample."""
534- config = Config ()
535532 n_exp = filtering_options .replicate_ratio
536533 n_top = filtering_options .high_replicate_ratio
537534 cut_off = filtering_options .cut_off
538535
539- sample : str
540536 metric : _StudyMetrics
541- for sample , metric in metrics .items ():
537+ for metric in metrics .values ():
542538 counts : pd .DataFrame = metric .count_matrix
543539 entrez_ids : list [str ] = metric .entrez_gene_ids
544540 library_size : pd .DataFrame = counts .sum (axis = 1 )
@@ -548,12 +544,11 @@ def cpm_filter(
548544 # thus, (0 / 1) * 1_000_000 = 0
549545 library_size [library_size == 0 ] = 1
550546
551- output_filepath = config .result_dir / context_name / prep .value / f"CPM_Matrix_{ prep .value } _{ sample } .csv"
552- output_filepath .parent .mkdir (parents = True , exist_ok = True )
547+ output_csv_filepath .parent .mkdir (parents = True , exist_ok = True )
553548 counts_per_million : pd .DataFrame = (counts / library_size ) * 1_000_000
554549 counts_per_million .insert (0 , "entrez_gene_ids" , pd .Series (entrez_ids ))
555- logger .debug (f"Writing CPM matrix to { output_filepath } " )
556- counts_per_million .to_csv (output_filepath , index = False )
550+ logger .debug (f"Writing CPM matrix to { output_csv_filepath } " )
551+ counts_per_million .to_csv (output_csv_filepath , index = False )
557552
558553 # TODO: Counts per million is adding ~61,500 columns (equal to the number of genes) for some reason.
559554 # Most likely due to multiplying by 1_000_000, not exactly sure why
@@ -656,24 +651,31 @@ def zfpkm_filter(*, metrics: NamedMetrics, filtering_options: _FilteringOptions,
656651
657652def filter_counts (
658653 * ,
659- context_name : str ,
660654 metrics : NamedMetrics ,
661655 technique : FilteringTechnique ,
662656 filtering_options : _FilteringOptions ,
663- prep : RNAPrepMethod ,
657+ cpm_output_filepath : Path | None = None ,
664658) -> NamedMetrics :
665659 """Filter the count matrix based on the specified technique."""
666660 match technique :
667661 case FilteringTechnique .cpm :
662+ if cpm_output_filepath is None :
663+ raise ValueError ("CPM output filepath must be provided" )
668664 return cpm_filter (
669- context_name = context_name , metrics = metrics , filtering_options = filtering_options , prep = prep
665+ metrics = metrics ,
666+ filtering_options = filtering_options ,
667+ output_csv_filepath = cpm_output_filepath ,
670668 )
669+
671670 case FilteringTechnique .tpm :
672671 return tpm_quantile_filter (metrics = metrics , filtering_options = filtering_options )
672+
673673 case FilteringTechnique .zfpkm :
674674 return zfpkm_filter (metrics = metrics , filtering_options = filtering_options , calcualte_fpkm = True )
675+
675676 case FilteringTechnique .umi :
676677 return zfpkm_filter (metrics = metrics , filtering_options = filtering_options , calcualte_fpkm = False )
678+
677679 case _:
678680 raise ValueError (f"Technique must be one of { FilteringTechnique } " )
679681
@@ -721,11 +723,9 @@ async def save_rnaseq_tests(
721723 entrez_gene_ids = read_counts_results .entrez_gene_ids
722724
723725 metrics = filter_counts (
724- context_name = context_name ,
725726 metrics = metrics ,
726727 technique = technique ,
727728 filtering_options = filtering_options ,
728- prep = prep ,
729729 )
730730
731731 expressed_genes : list [str ] = []
@@ -758,6 +758,7 @@ async def save_rnaseq_tests(
758758
759759 boolean_matrix .to_csv (output_filepath , index = False )
760760 logger .info (
761- f"{ context_name } - Found { expressed_count } expressed and { high_confidence_count } confidently expressed genes"
761+ f"{ context_name } - Found { expressed_count } expressed genes, "
762+ f"{ high_confidence_count } of which are confidently expressed"
762763 )
763764 logger .success (f"Wrote boolean matrix to { output_filepath } " )
0 commit comments