aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-18 12:27:32 +0300
committerBonfaceKilz2021-10-19 16:33:32 +0300
commit42c56d330fdb51820c0fdcbb0b4376ff568ea009 (patch)
treee529977fd89cd4e1daaea92cd0d4e4c3d22a7922
parent679c3edd08453d2f1ef09b3461fd8d0b038b3adf (diff)
downloadgenenetwork3-42c56d330fdb51820c0fdcbb0b4376ff568ea009.tar.gz
Move `export_informative` function to `gn3.db.traits` module
Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi * gn3/db/traits.py: Move `export_informative` function here * gn3/partial_correlations.py: Remove `export_informative` function * tests/unit/db/test_traits.py: Move `export_informative` function tests here * tests/unit/test_partial_correlations.py: Remove `export_informative` function tests The `export_informative` function relates more to the traits than to the partial correlations, and could find use in more than just the partial correlations stuff. This commit moves the function to the more traits-specific `gn3.db.traits` module.
-rw-r--r--gn3/db/traits.py24
-rw-r--r--gn3/partial_correlations.py24
-rw-r--r--tests/unit/db/test_traits.py86
-rw-r--r--tests/unit/test_partial_correlations.py87
4 files changed, 111 insertions, 110 deletions
diff --git a/gn3/db/traits.py b/gn3/db/traits.py
index 1e29aff..1c6aaa7 100644
--- a/gn3/db/traits.py
+++ b/gn3/db/traits.py
@@ -743,3 +743,27 @@ def generate_traits_filename(base_path: str = TMPDIR):
"""Generate a unique filename for use with generated traits files."""
return "{}/traits_test_file_{}.txt".format(
os.path.abspath(base_path), random_string(10))
+
+def export_informative(trait_data: dict, inc_var: bool = False) -> tuple:
+ """
+ Export informative strain
+
+ This is a migration of the `exportInformative` function in
+ web/webqtl/base/webqtlTrait.py module in GeneNetwork1.
+
+ There is a chance that the original implementation has a bug, especially
+ dealing with the `inc_var` value. It the `inc_var` value is meant to control
+ the inclusion of the `variance` value, then the current implementation, and
+ that one in GN1 have a bug.
+ """
+ def __exporter__(acc, data_item):
+ if not inc_var or data_item["variance"] is not None:
+ return (
+ acc[0] + (data_item["sample_name"],),
+ acc[1] + (data_item["value"],),
+ acc[2] + (data_item["variance"],))
+ return acc
+ return reduce(
+ __exporter__,
+ filter(lambda td: td["value"] is not None, trait_data["data"].values()),
+ (tuple(), tuple(), tuple()))
diff --git a/gn3/partial_correlations.py b/gn3/partial_correlations.py
index 8c37886..df390ed 100644
--- a/gn3/partial_correlations.py
+++ b/gn3/partial_correlations.py
@@ -6,27 +6,3 @@ GeneNetwork1.
"""
from functools import reduce
-
-def export_informative(trait_data: dict, inc_var: bool = False) -> tuple:
- """
- Export informative strain
-
- This is a migration of the `exportInformative` function in
- web/webqtl/base/webqtlTrait.py module in GeneNetwork1.
-
- There is a chance that the original implementation has a bug, especially
- dealing with the `inc_var` value. It the `inc_var` value is meant to control
- the inclusion of the `variance` value, then the current implementation, and
- that one in GN1 have a bug.
- """
- def __exporter__(acc, data_item):
- if not inc_var or data_item["variance"] is not None:
- return (
- acc[0] + (data_item["sample_name"],),
- acc[1] + (data_item["value"],),
- acc[2] + (data_item["variance"],))
- return acc
- return reduce(
- __exporter__,
- filter(lambda td: td["value"] is not None, trait_data["data"].values()),
- (tuple(), tuple(), tuple()))
diff --git a/tests/unit/db/test_traits.py b/tests/unit/db/test_traits.py
index 0c4ef78..67f0c6f 100644
--- a/tests/unit/db/test_traits.py
+++ b/tests/unit/db/test_traits.py
@@ -3,6 +3,7 @@ from unittest import mock, TestCase
from gn3.db.traits import (
build_trait_name,
export_trait_data,
+ export_informative,
set_haveinfo_field,
update_sample_data,
retrieve_trait_info,
@@ -315,3 +316,88 @@ class TestTraitsDBFunctions(TestCase):
trait_data, samplelist, dtype=dtype, var_exists=vflag,
n_exists=nflag),
expected)
+
+ def test_export_informative(self):
+ """Test that the function exports appropriate data."""
+ for trait_data, inc_var, expected in [
+ [{"data": {
+ "sample1": {
+ "sample_name": "sample1", "value": 9, "variance": None,
+ "ndata": 13
+ },
+ "sample2": {
+ "sample_name": "sample2", "value": 8, "variance": None,
+ "ndata": 13
+ },
+ "sample3": {
+ "sample_name": "sample3", "value": 7, "variance": None,
+ "ndata": 13
+ },
+ "sample4": {
+ "sample_name": "sample4", "value": 6, "variance": None,
+ "ndata": 13
+ },
+ }}, 0, (
+ ("sample1", "sample2", "sample3", "sample4"), (9, 8, 7, 6),
+ (None, None, None, None))],
+ [{"data": {
+ "sample1": {
+ "sample_name": "sample1", "value": 9, "variance": None,
+ "ndata": 13
+ },
+ "sample2": {
+ "sample_name": "sample2", "value": 8, "variance": None,
+ "ndata": 13
+ },
+ "sample3": {
+ "sample_name": "sample3", "value": None, "variance": None,
+ "ndata": 13
+ },
+ "sample4": {
+ "sample_name": "sample4", "value": 6, "variance": None,
+ "ndata": 13
+ },
+ }}, 0, (
+ ("sample1", "sample2", "sample4"), (9, 8, 6),
+ (None, None, None))],
+ [{"data": {
+ "sample1": {
+ "sample_name": "sample1", "value": 9, "variance": None,
+ "ndata": 13
+ },
+ "sample2": {
+ "sample_name": "sample2", "value": 8, "variance": None,
+ "ndata": 13
+ },
+ "sample3": {
+ "sample_name": "sample3", "value": 7, "variance": None,
+ "ndata": 13
+ },
+ "sample4": {
+ "sample_name": "sample4", "value": 6, "variance": None,
+ "ndata": 13
+ },
+ }}, True, (tuple(), tuple(), tuple())],
+ [{"data": {
+ "sample1": {
+ "sample_name": "sample1", "value": 9, "variance": None,
+ "ndata": 13
+ },
+ "sample2": {
+ "sample_name": "sample2", "value": 8, "variance": 0.657,
+ "ndata": 13
+ },
+ "sample3": {
+ "sample_name": "sample3", "value": 7, "variance": None,
+ "ndata": 13
+ },
+ "sample4": {
+ "sample_name": "sample4", "value": 6, "variance": None,
+ "ndata": 13
+ },
+ }}, 0, (
+ ("sample1", "sample2", "sample3", "sample4"), (9, 8, 7, 6),
+ (None, 0.657, None, None))]]:
+ with self.subTest(trait_data=trait_data):
+ self.assertEqual(
+ export_informative(trait_data, inc_var), expected)
diff --git a/tests/unit/test_partial_correlations.py b/tests/unit/test_partial_correlations.py
index 6eea078..f204d4f 100644
--- a/tests/unit/test_partial_correlations.py
+++ b/tests/unit/test_partial_correlations.py
@@ -1,92 +1,7 @@
"""Module contains tests for gn3.partial_correlations"""
from unittest import TestCase
-from gn3.partial_correlations import export_informative
+
class TestPartialCorrelations(TestCase):
"""Class for testing partial correlations computation functions"""
-
- def test_export_informative(self):
- """Test that the function exports appropriate data."""
- for trait_data, inc_var, expected in [
- [{"data": {
- "sample1": {
- "sample_name": "sample1", "value": 9, "variance": None,
- "ndata": 13
- },
- "sample2": {
- "sample_name": "sample2", "value": 8, "variance": None,
- "ndata": 13
- },
- "sample3": {
- "sample_name": "sample3", "value": 7, "variance": None,
- "ndata": 13
- },
- "sample4": {
- "sample_name": "sample4", "value": 6, "variance": None,
- "ndata": 13
- },
- }}, 0, (
- ("sample1", "sample2", "sample3", "sample4"), (9, 8, 7, 6),
- (None, None, None, None))],
- [{"data": {
- "sample1": {
- "sample_name": "sample1", "value": 9, "variance": None,
- "ndata": 13
- },
- "sample2": {
- "sample_name": "sample2", "value": 8, "variance": None,
- "ndata": 13
- },
- "sample3": {
- "sample_name": "sample3", "value": None, "variance": None,
- "ndata": 13
- },
- "sample4": {
- "sample_name": "sample4", "value": 6, "variance": None,
- "ndata": 13
- },
- }}, 0, (
- ("sample1", "sample2", "sample4"), (9, 8, 6),
- (None, None, None))],
- [{"data": {
- "sample1": {
- "sample_name": "sample1", "value": 9, "variance": None,
- "ndata": 13
- },
- "sample2": {
- "sample_name": "sample2", "value": 8, "variance": None,
- "ndata": 13
- },
- "sample3": {
- "sample_name": "sample3", "value": 7, "variance": None,
- "ndata": 13
- },
- "sample4": {
- "sample_name": "sample4", "value": 6, "variance": None,
- "ndata": 13
- },
- }}, True, (tuple(), tuple(), tuple())],
- [{"data": {
- "sample1": {
- "sample_name": "sample1", "value": 9, "variance": None,
- "ndata": 13
- },
- "sample2": {
- "sample_name": "sample2", "value": 8, "variance": 0.657,
- "ndata": 13
- },
- "sample3": {
- "sample_name": "sample3", "value": 7, "variance": None,
- "ndata": 13
- },
- "sample4": {
- "sample_name": "sample4", "value": 6, "variance": None,
- "ndata": 13
- },
- }}, 0, (
- ("sample1", "sample2", "sample3", "sample4"), (9, 8, 7, 6),
- (None, 0.657, None, None))]]:
- with self.subTest(trait_data=trait_data):
- self.assertEqual(
- export_informative(trait_data, inc_var), expected)