aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--r_qtl/r_qtl2.py31
-rw-r--r--tests/r_qtl/test_files/test_covar.zipbin490 -> 562 bytes
-rw-r--r--tests/r_qtl/test_files/test_covar_transposed.zipbin541 -> 594 bytes
-rw-r--r--tests/r_qtl/test_r_qtl2_covar.py30
4 files changed, 48 insertions, 13 deletions
diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py
index 26e8c76..682542b 100644
--- a/r_qtl/r_qtl2.py
+++ b/r_qtl/r_qtl2.py
@@ -134,6 +134,37 @@ def make_process_data_geno(cdata) -> tuple[
for items in zip(ids, vals[1:]))
return (__non_transposed__, __transposed__)
+def make_process_data_covar(cdata) -> tuple[
+ Callable[[dict], dict],
+ Callable[[str, tuple[str, ...], tuple[str, ...]],
+ tuple[dict, ...]]]:
+ """Build functions to process sex and cross information in covar files."""
+ def replace_sex_code(val):
+ sex_info = cdata.get("sex", False)
+ if bool(sex_info):
+ return sex_info.get(val, val)
+ return val
+ def replace_cross_info(val):
+ cross_info = cdata.get("cross_info", False)
+ if bool(cross_info):
+ return cross_info.get(val, val)
+ return val
+ def non_transposed(row: dict) -> dict:
+ return {
+ key: thread_op(value, replace_sex_code, replace_cross_info)
+ for key,value in row.items()
+ }
+ def transposed(id_key: str,
+ ids: tuple[str, ...],
+ vals: tuple[str, ...]) -> tuple[dict, ...]:
+ return tuple(
+ dict(zip(
+ [id_key, vals[0]],
+ (thread_op(item, replace_sex_code, replace_cross_info)
+ for item in items)))
+ for items in zip(ids, vals[1:]))
+ return (non_transposed, transposed)
+
def __default_process_value_transposed__(
id_key: str,
ids: tuple[str, ...],
diff --git a/tests/r_qtl/test_files/test_covar.zip b/tests/r_qtl/test_files/test_covar.zip
index 6fbb5e9..61961a0 100644
--- a/tests/r_qtl/test_files/test_covar.zip
+++ b/tests/r_qtl/test_files/test_covar.zip
Binary files differ
diff --git a/tests/r_qtl/test_files/test_covar_transposed.zip b/tests/r_qtl/test_files/test_covar_transposed.zip
index 5e933cc..c0758a3 100644
--- a/tests/r_qtl/test_files/test_covar_transposed.zip
+++ b/tests/r_qtl/test_files/test_covar_transposed.zip
Binary files differ
diff --git a/tests/r_qtl/test_r_qtl2_covar.py b/tests/r_qtl/test_r_qtl2_covar.py
index c18ee74..eef7a33 100644
--- a/tests/r_qtl/test_r_qtl2_covar.py
+++ b/tests/r_qtl/test_r_qtl2_covar.py
@@ -10,19 +10,21 @@ from r_qtl import r_qtl2 as rqtl2
@pytest.mark.parametrize(
"filepath,expected",
(("tests/r_qtl/test_files/test_covar.zip",
- ({"id": "1", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "2", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "3", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "146", "sex": "f", "cross_direction": "(BxS)x(BxS)"},
- {"id": "147", "sex": "f", "cross_direction": "(BxS)x(BxS)"},
- {"id": "148", "sex": "f", "cross_direction": "(BxS)x(BxS)"})),
+ ({"id": "1", "sex": "male", "cross_direction": 1},
+ {"id": "2", "sex": "male", "cross_direction": 1},
+ {"id": "3", "sex": "male", "cross_direction": 1},
+ {"id": "71", "sex": "male", "cross_direction": 0},
+ {"id": "72", "sex": "male", "cross_direction": 0},
+ {"id": "146", "sex": "female", "cross_direction": 1},
+ {"id": "147", "sex": "female", "cross_direction": 1},
+ {"id": "148", "sex": "female", "cross_direction": 1})),
("tests/r_qtl/test_files/test_covar_transposed.zip",
- ({"id": "1", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "2", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "3", "sex": "m", "cross_direction": "(BxS)x(BxS)"},
- {"id": "146", "sex": "f", "cross_direction": "(BxS)x(BxS)"},
- {"id": "147", "sex": "f", "cross_direction": "(BxS)x(BxS)"},
- {"id": "148", "sex": "f", "cross_direction": "(BxS)x(BxS)"})),
+ ({"id": "1", "sex": "male", "cross_direction": 1},
+ {"id": "2", "sex": "male", "cross_direction": 1},
+ {"id": "3", "sex": "male", "cross_direction": 1},
+ {"id": "146", "sex": "female", "cross_direction": 1},
+ {"id": "147", "sex": "female", "cross_direction": 1},
+ {"id": "148", "sex": "female", "cross_direction": 1})),
))
def test_parse_covar_files(filepath, expected):
"""Test parsing of 'covar' files from the R/qtl2 bundle.
@@ -33,4 +35,6 @@ def test_parse_covar_files(filepath, expected):
"""
with ZipFile(Path(filepath).absolute(), "r") as zfile:
cdata = rqtl2.control_data(zfile)
- assert tuple(rqtl2.file_data(zfile, "covar", cdata)) == expected
+ process_fns = rqtl2.make_process_data_covar(cdata)
+ assert tuple(
+ rqtl2.file_data(zfile, "covar", cdata, *process_fns)) == expected