From 0bb51bd78479c05839d7b7f9f878db4b5616cfda Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 29 Oct 2021 06:34:19 +0300 Subject: Implement `tissue_correlation` function Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi * gn3/computations/partial_correlations.py: New function (tissue_correlation) * tests/unit/test_partial_correlations.py -> tests/unit/computations/test_partial_correlations.py: Move module. Implement tests for new function Migrate the `cal_tissue_corr` function embedded in the `web.webqtl.correlation.correlationFunction.batchCalTissueCorr` function in GN1 and implement tests to ensure it works correctly. --- gn3/computations/partial_correlations.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'gn3') diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py index b3de31c..e73edfd 100644 --- a/gn3/computations/partial_correlations.py +++ b/gn3/computations/partial_correlations.py @@ -7,6 +7,7 @@ GeneNetwork1. from functools import reduce from typing import Any, Tuple, Sequence +from scipy.stats import pearsonr, spearmanr def control_samples(controls: Sequence[dict], sampleslist: Sequence[str]): """ @@ -123,6 +124,32 @@ def find_identical_traits( if len(item[1]) > 1), tuple())) +def tissue_correlation( + primary_trait_values: Tuple[float, ...], + target_trait_values: Tuple[float, ...], + method: str) -> Tuple[float, float]: + """ + Compute the correlation between the primary trait values, and the values of + a single target value. + + This migrates the `cal_tissue_corr` function embedded in the larger + `web.webqtl.correlation.correlationFunction.batchCalTissueCorr` function in + GeneNetwork1. + """ + def spearman_corr(*args): + result = spearmanr(*args) + return (result.correlation, result.pvalue) + + method_fns = {"pearson": pearsonr, "spearman": spearman_corr} + + assert len(primary_trait_values) == len(target_trait_values), ( + "The lengths of the `primary_trait_values` and `target_trait_values` " + "must be equal") + assert method in method_fns.keys(), ( + "Method must be one of: {}".format(",".join(method_fns.keys()))) + + return method_fns[method](primary_trait_values, target_trait_values) + def batch_computed_tissue_correlation( trait_value: str, symbol_value_dict: dict, method: str = "pearson") -> Tuple[dict, dict]: -- cgit v1.2.3