aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gn3/partial_correlations.py16
-rw-r--r--tests/unit/test_partial_correlations.py42
2 files changed, 57 insertions, 1 deletions
diff --git a/gn3/partial_correlations.py b/gn3/partial_correlations.py
index 99521c6..4db4807 100644
--- a/gn3/partial_correlations.py
+++ b/gn3/partial_correlations.py
@@ -44,3 +44,19 @@ def control_samples(controls: Sequence[dict], sampleslist: Sequence[str]):
),
[__process_control__(trait_data) for trait_data in controls],
(tuple(), tuple(), tuple(), tuple()))
+
+def dictify_by_samples(samples_vals_vars: Sequence[Sequence]) -> dict:
+ """
+ Build a sequence of dictionaries from a sequence of separate sequences of
+ samples, values and variances.
+
+ This is a partial migration of
+ `web.webqtl.correlation.correlationFunction.fixStrains` function in GN1.
+ This implementation extracts code that will find common use, and that will
+ find use in more than one place.
+ """
+ return tuple(
+ {
+ sample: {"sample_name": sample, "value": val, "variance": var}
+ for sample, val, var in zip(*trait_line)
+ } for trait_line in zip(*(samples_vals_vars[0:3])))
diff --git a/tests/unit/test_partial_correlations.py b/tests/unit/test_partial_correlations.py
index 0083ef7..6302f74 100644
--- a/tests/unit/test_partial_correlations.py
+++ b/tests/unit/test_partial_correlations.py
@@ -1,7 +1,7 @@
"""Module contains tests for gn3.partial_correlations"""
from unittest import TestCase
-from gn3.partial_correlations import control_samples
+from gn3.partial_correlations import control_samples, dictify_by_samples
sampleslist = ["B6cC3-1", "BXD1", "BXD12", "BXD16", "BXD19", "BXD2"]
control_traits = (
@@ -85,3 +85,43 @@ class TestPartialCorrelations(TestCase):
((None, None, None, None, None, None), (None, None, None, None),
(None, None, None)),
(6, 4, 3)))
+
+ def test_dictify_by_samples(self):
+ """
+ Given:
+ a sequence of sequences with sample names, values and variances, as
+ in the output of `gn3.partial_correlations.control_samples` or
+ the output of `gn3.db.traits.export_informative`
+ When:
+ the sequence is passed as an argument into the
+ `gn3.partial_correlations.dictify_by_sample`
+ Then:
+ return a sequence of dicts with keys being the values of the sample
+ names, and each of who's values being sub-dicts with the keys
+ 'sample_name', 'value' and 'variance' whose values correspond to the
+ values passed in.
+ """
+ self.assertEqual(
+ dictify_by_samples(
+ ((("B6cC3-1", "BXD1", "BXD12", "BXD16", "BXD19", "BXD2"),
+ ("BXD12", "BXD16", "BXD19", "BXD2"),
+ ("B6cC3-1", "BXD1", "BXD2")),
+ ((7.51879, 7.77141, 8.39265, 8.17443, 8.30401, 7.80944),
+ (8.39265, 8.17443, 8.30401, 7.80944),
+ (7.51879, 7.77141, 7.80944)),
+ ((None, None, None, None, None, None), (None, None, None, None),
+ (None, None, None)),
+ (6, 4, 3))),
+ ({"B6cC3-1": {"sample_name": "B6cC3-1", "value": 7.51879, "variance": None},
+ "BXD1": {"sample_name": "BXD1", "value": 7.77141, "variance": None},
+ "BXD12": {"sample_name": "BXD12", "value": 8.39265, "variance": None},
+ "BXD16": {"sample_name": "BXD16", "value": 8.17443, "variance": None},
+ "BXD19": {"sample_name": "BXD19", "value": 8.30401, "variance": None},
+ "BXD2": {"sample_name": "BXD2", "value": 7.80944, "variance": None}},
+ {"BXD12": {"sample_name": "BXD12", "value": 8.39265, "variance": None},
+ "BXD16": {"sample_name": "BXD16", "value": 8.17443, "variance": None},
+ "BXD19": {"sample_name": "BXD19", "value": 8.30401, "variance": None},
+ "BXD2": {"sample_name": "BXD2", "value": 7.80944, "variance": None}},
+ {"B6cC3-1": {"sample_name": "B6cC3-1", "value": 7.51879, "variance": None},
+ "BXD1": {"sample_name": "BXD1", "value": 7.77141, "variance": None},
+ "BXD2": {"sample_name": "BXD2", "value": 7.80944, "variance": None}}))