2323)
2424
2525
26- async def _combine_z_distribution_for_batch (
26+ def _combine_z_distribution_for_batch (
2727 context_name : str ,
2828 batch : _BatchEntry ,
2929 matrix : pd .DataFrame ,
@@ -33,6 +33,21 @@ async def _combine_z_distribution_for_batch(
3333 weighted_z_floor : int ,
3434 weighted_z_ceiling : int ,
3535) -> pd .DataFrame :
36+ """Combine z-score distributions across samples for a single batch.
37+
38+ Args:
39+ context_name: Name of the context (e.g., tissue or condition).
40+ batch: Batch entry containing batch number and sample names.
41+ matrix: DataFrame with 'ensembl_gene_id' and sample columns.
42+ source: Source type (e.g., trna, mrna, scrna, proteomics).
43+ output_combined_matrix_filepath: Path to save the combined z-score matrix.
44+ output_figure_dirpath: Path to save the z-score distribution figure.
45+ weighted_z_floor: Minimum z-score value after combining.
46+ weighted_z_ceiling: Maximum z-score value after combining.
47+
48+ Returns:
49+ A pandas dataframe of the weighted z-distributions
50+ """
3651 output_combined_matrix_filepath .parent .mkdir (parents = True , exist_ok = True )
3752 output_figure_dirpath .mkdir (parents = True , exist_ok = True )
3853
@@ -80,15 +95,29 @@ async def _combine_z_distribution_for_batch(
8095 return weighted_matrix
8196
8297
83- async def _combine_z_distribution_for_source (
98+ def _combine_z_distribution_for_source (
8499 merged_source_data : pd .DataFrame ,
85100 context_name : str ,
86101 num_replicates : int ,
87102 output_combined_matrix_filepath : Path ,
88103 output_figure_filepath : Path ,
89104 weighted_z_floor : int = - 6 ,
90105 weighted_z_ceiling : int = 6 ,
91- ):
106+ ) -> pd .DataFrame :
107+ """Combine z-score distributions across batches for a single source.
108+
109+ Args:
110+ merged_source_data: DataFrame with 'ensembl_gene_id' and batch columns.
111+ context_name: Name of the context (e.g., tissue or condition).
112+ num_replicates: Number of replicates (samples) for weighting.
113+ output_combined_matrix_filepath: Path to save the combined z-score matrix.
114+ output_figure_filepath: Path to save the z-score distribution figure.
115+ weighted_z_floor: Minimum z-score value after combining.
116+ weighted_z_ceiling: Maximum z-score value after combining.
117+
118+ Returns:
119+ A pandas dataframe of the weighted z-distributions
120+ """
92121 if _num_columns (merged_source_data ) <= 2 :
93122 logger .warning ("A single source exists, returning matrix as-is because no additional combining can be done" )
94123 merged_source_data .columns = ["ensembl_gene_id" , "combine_z" ]
@@ -144,14 +173,10 @@ def _combine_z_distribution_for_context(
144173 return pd .DataFrame ({"ensembl_gene_id" : [], "combine_z" : []})
145174
146175 z_matrices = [
147- result .z_score_matrix .set_index ("ensembl_gene_id" ).rename (columns = dict .fromkeys (result .z_score_matrix .columns [1 :], result .type .value ))
148- for result in zscore_results
176+ res .z_score_matrix .set_index ("ensembl_gene_id" ).rename (columns = dict .fromkeys (res .z_score_matrix .columns [1 :], res .type .value ))
177+ for res in zscore_results
149178 ]
150- z_matrix = pd .DataFrame ()
151- for matrix in z_matrices :
152- z_matrix = z_matrix .merge (right = matrix , left_index = True , right_index = True , how = "outer" ) if not z_matrix .empty else matrix
153- z_matrix = z_matrix .reset_index (drop = False )
154- # z_matrix = pd.concat(z_matrices, axis=1, join="outer").reset_index()
179+ z_matrix = pd .concat (z_matrices , axis = 1 , join = "outer" ).reset_index ()
155180 if _num_columns (z_matrix ) <= 1 :
156181 logger .trace (f"Only 1 source exists for '{ context } ', returning dataframe as-is becuase no data exists to combine" )
157182 z_matrix .columns = ["ensembl_gene_id" , "combine_z" ]
@@ -229,7 +254,7 @@ async def _begin_combining_distributions(
229254 matrix = matrix [[GeneIdentifier .ENSEMBL_GENE_ID .value , * batch .sample_names ]],
230255 source = source ,
231256 output_combined_matrix_filepath = (
232- output_filepaths [source .value ].parent / f"{ context_name } _{ source .value } _batch{ batch .batch_num } _combined_z_distribution_ .csv"
257+ output_filepaths [source .value ].parent / f"{ context_name } _{ source .value } _batch{ batch .batch_num } _combined_z_distribution .csv"
233258 ),
234259 output_figure_dirpath = output_figure_dirpath ,
235260 weighted_z_floor = weighted_z_floor ,
@@ -243,7 +268,7 @@ async def _begin_combining_distributions(
243268 for df in batch_results :
244269 merged_batch_results = df if merged_batch_results .empty else merged_batch_results .merge (df , on = "ensembl_gene_id" , how = "outer" )
245270
246- merged_source_results : pd .DataFrame = await _combine_z_distribution_for_source (
271+ merged_source_results : pd .DataFrame = _combine_z_distribution_for_source (
247272 merged_source_data = merged_batch_results ,
248273 context_name = context_name ,
249274 num_replicates = sum (batch .num_samples for batch in batch_names [source .value ]),
0 commit comments