aboutsummaryrefslogtreecommitdiff
path: root/gn3/computations
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-29 06:34:19 +0300
committerFrederick Muriuki Muriithi2021-10-29 06:34:19 +0300
commit0bb51bd78479c05839d7b7f9f878db4b5616cfda (patch)
treedb8f19d81f33cb15c75dfe328475eff58aaf6f40 /gn3/computations
parent4c73d70d6d844bf2fa3358c71b9e28daff51e69c (diff)
downloadgenenetwork3-0bb51bd78479c05839d7b7f9f878db4b5616cfda.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]: