about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-01-03 10:10:12 +0300
committerFrederick Muriuki Muriithi2024-01-03 10:10:12 +0300
commit4bf51a31c251f3d788325c58c8a78f534409f90f (patch)
tree30e5c1657c7c6c4afac3e489700fa679739da912
parent3edc6f67bb50552d0ce0e3ec372b06efb736cdb8 (diff)
downloadgn-uploader-4bf51a31c251f3d788325c58c8a78f534409f90f.tar.gz
Parse the phenotype data from the R/qtl2 bundle.
-rw-r--r--r_qtl/r_qtl2.py13
-rw-r--r--tests/r_qtl/test_files/test_pheno.zipbin0 -> 485 bytes
-rw-r--r--tests/r_qtl/test_files/test_pheno_transposed.zipbin0 -> 536 bytes
-rw-r--r--tests/r_qtl/test_r_qtl2_pheno.py33
4 files changed, 46 insertions, 0 deletions
diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py
index bc3a86f..2256609 100644
--- a/r_qtl/r_qtl2.py
+++ b/r_qtl/r_qtl2.py
@@ -163,3 +163,16 @@ def map_data(zfile: ZipFile, map_type: str, cdata: dict) -> Iterator[dict]:
 
     for row in with_transposed(zfile, map_file_key, cdata, __merge__):
         yield row
+
+def phenotype_data(zfile: ZipFile, cdata: dict) -> Iterator[dict]:
+    """Load phenotype file data."""
+    if not cdata.get("pheno_transposed", False):
+        for row in with_non_transposed(zfile, "pheno", cdata, lambda val: val):
+            yield row
+        return
+
+    def __merge__(id_key, ids, vals):
+        return tuple(dict(zip([id_key, vals[0]], items))
+                     for items in zip(ids, vals[1:]))
+    for row in with_transposed(zfile, "pheno", cdata, __merge__):
+        yield row
diff --git a/tests/r_qtl/test_files/test_pheno.zip b/tests/r_qtl/test_files/test_pheno.zip
new file mode 100644
index 0000000..5c709e7
--- /dev/null
+++ b/tests/r_qtl/test_files/test_pheno.zip
Binary files differdiff --git a/tests/r_qtl/test_files/test_pheno_transposed.zip b/tests/r_qtl/test_files/test_pheno_transposed.zip
new file mode 100644
index 0000000..9bff030
--- /dev/null
+++ b/tests/r_qtl/test_files/test_pheno_transposed.zip
Binary files differdiff --git a/tests/r_qtl/test_r_qtl2_pheno.py b/tests/r_qtl/test_r_qtl2_pheno.py
new file mode 100644
index 0000000..554d9c8
--- /dev/null
+++ b/tests/r_qtl/test_r_qtl2_pheno.py
@@ -0,0 +1,33 @@
+"""Test parsing of pheno files in a R/qtl2 bundle."""
+from pathlib import Path
+
+import pytest
+from zipfile import ZipFile
+
+from r_qtl import r_qtl2 as rqtl2
+
+@pytest.mark.unit_test
+@pytest.mark.parametrize(
+    "filepath,expected",
+    (("tests/r_qtl/test_files/test_pheno.zip",
+      ({"id": "1", "liver": "61.92", "spleen": "153.16"},
+       {"id": "2", "liver": "88.33", "spleen": "178.58"},
+       {"id": "3", "liver": "58", "spleen": "131.91"},
+       {"id": "4", "liver": "78.06", "spleen": "126.13"},
+       {"id": "5", "liver": "65.31", "spleen": "181.05"})),
+     ("tests/r_qtl/test_files/test_pheno_transposed.zip",
+      ({"id": "1", "liver": "61.92", "spleen": "153.16"},
+       {"id": "2", "liver": "88.33", "spleen": "178.58"},
+       {"id": "3", "liver": "58", "spleen": "131.91"},
+       {"id": "4", "liver": "78.06", "spleen": "126.13"},
+       {"id": "5", "liver": "65.31", "spleen": "181.05"}))))
+def test_parse_pheno_files(filepath, expected):
+    """Test parsing of 'pheno' files from the R/qtl2 bundle.
+
+    GIVEN: path to a R/qtl2 bundle
+    WHEN: the 'pheno' file is parsed
+    THEN: verify the parsed data is as expected
+    """
+    with ZipFile(Path(filepath).absolute(), "r") as zfile:
+        assert tuple(
+            rqtl2.phenotype_data(zfile, rqtl2.control_data(zfile))) == expected