aboutsummaryrefslogtreecommitdiff
path: root/gn3/computations
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-29 06:34:19 +0300
committerBonfaceKilz2021-11-04 12:45:57 +0300
commit847a5e0656ed686a0541e47958a845a0d3725daf (patch)
treef2ec4c5a1907f8a190be40d08976effe54ef5b80 /gn3/computations
parent28b0ced4ec13451c5c7323ed5135d126f296836a (diff)
downloadgenenetwork3-847a5e0656ed686a0541e47958a845a0d3725daf.tar.gz
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.
Diffstat (limited to 'gn3/computations')
-rw-r--r--gn3/computations/partial_correlations.py27
1 files changed, 27 insertions, 0 deletions
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]: