From 903d8902f943c91bef5158c0e2c044d14b41cfa4 Mon Sep 17 00:00:00 2001 From: "Abolfazl (Abe)" Date: Tue, 19 Dec 2023 16:24:01 -0800 Subject: [PATCH] add `getReplicateScore` close #23 --- screenpro/phenoScore.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/screenpro/phenoScore.py b/screenpro/phenoScore.py index 90cf4d9..c10a60a 100644 --- a/screenpro/phenoScore.py +++ b/screenpro/phenoScore.py @@ -188,6 +188,46 @@ def getZScorePhenotypeScore(x, y, x_ctrl, y_ctrl, growth_rate, math, ave): # return ((delta - ctrl_median) / growth_rate) / ctrl_std +def getReplicateScore(screen, x_label, y_label, growth_rate=1, ctrl_label='negCtrl'): + """ + Calculate phenotype score for each pair of replicates. + Args: + screen: ScreenPro object + x_label: name of the first condition in column `condition` of `screen.adata.obs` + y_label: name of the second condition in column `condition` of `screen.adata.obs` + growth_rate: growth rate term to use for normalizing phenotype score + ctrl_label: string to identify labels of negative control oligos + + Returns: + pd.DataFrame: dataframe of phenotype scores + """ + adata_ctrl = screen.adata[:, screen.adata.var.targetType.eq(ctrl_label)].copy() + + results = {} + + for replicate in screen.adata.obs.replicate.unique(): + res = getPhenotypeScore( + x=screen.adata[screen.adata.obs.query(f'condition == "{x_label}" & replicate == {replicate}').index].X, + y=screen.adata[screen.adata.obs.query(f'condition == "{y_label}" & replicate == {replicate}').index].X, + + x_ctrl=adata_ctrl[adata_ctrl.obs.query(f'condition == "{x_label}" & replicate == {replicate}').index].X, + y_ctrl=adata_ctrl[adata_ctrl.obs.query(f'condition == "{y_label}" & replicate == {replicate}').index].X, + + growth_rate=growth_rate, + math=screen.math, + ave='row' # there is only one column so `row` option here is equivalent to the value before averaging. + ) + + results.update({f'replicate_{replicate}': res}) + + out = pd.DataFrame( + results, + index=screen.adata.var.index + ) + + return out + + def generatePseudoGeneLabels(adata, num_pseudogenes=None, ctrl_label='negCtrl'): """ Generate new labels per `num_pseudogenes` randomly selected non targeting oligo in `adata.var`.