aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-21 09:00:16 +0300
committerFrederick Muriuki Muriithi2021-10-21 09:35:52 +0300
commitcad4649d19001f62ef592dedf09f3ac53744962a (patch)
tree6a94f1d57d91807699281aaa7d3e6320b5620960 /gn3
parent6818670686de86c86b6c1aa372135ab6c22af156 (diff)
downloadgenenetwork3-cad4649d19001f62ef592dedf09f3ac53744962a.tar.gz
Implement `find_identical_traits` function
Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi * gn3/partial_correlations.py: implement function `find_identical_traits` * tests/unit/test_partial_correlations.py: implement tests for function `find_identical_traits` Migrate `web.webqtl.correlation.correlationFunction.findIdenticalTraits` function in GN1 to here, adding in tests to ensure the migration works in a bug-compatible version with the original.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/partial_correlations.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/gn3/partial_correlations.py b/gn3/partial_correlations.py
index c556d10..1fb0ccc 100644
--- a/gn3/partial_correlations.py
+++ b/gn3/partial_correlations.py
@@ -6,7 +6,7 @@ GeneNetwork1.
"""
from functools import reduce
-from typing import Any, Sequence
+from typing import Any, Tuple, Sequence
def control_samples(controls: Sequence[dict], sampleslist: Sequence[str]):
"""
@@ -86,3 +86,39 @@ def fix_samples(primary_trait: dict, control_traits: Sequence[dict]) -> Sequence
control_vals_vars[0],
tuple(primary_trait[sample]["variance"] for sample in primary_samples),
control_vals_vars[1])
+
+def find_identical_traits(
+ primary_name: str, primary_value: float, control_names: Tuple[str, ...],
+ control_values: Tuple[float, ...]) -> Tuple[str, ...]:
+ """
+ Find traits that have the same value when the values are considered to
+ 3 decimal places.
+
+ This is a migration of the
+ `web.webqtl.correlation.correlationFunction.findIdenticalTraits` function in
+ GN1.
+ """
+ def __merge_identicals__(
+ acc: Tuple[str, ...],
+ ident: Tuple[str, Tuple[str, ...]]) -> Tuple[str, ...]:
+ return acc + ident[1]
+
+ def __dictify_controls__(acc, control_item):
+ ckey = "{:.3f}".format(control_item[0])
+ return {**acc, ckey: acc.get(ckey, tuple()) + (control_item[1],)}
+
+ return (reduce(## for identical control traits
+ __merge_identicals__,
+ (item for item in reduce(# type: ignore[var-annotated]
+ __dictify_controls__, zip(control_values, control_names),
+ {}).items() if len(item[1]) > 1),
+ tuple())
+ or
+ reduce(## If no identical control traits, try primary and controls
+ __merge_identicals__,
+ (item for item in reduce(# type: ignore[var-annotated]
+ __dictify_controls__,
+ zip((primary_value,) + control_values,
+ (primary_name,) + control_names), {}).items()
+ if len(item[1]) > 1),
+ tuple()))