aboutsummaryrefslogtreecommitdiff
path: root/tests/r_qtl/test_r_qtl2_pheno.py
blob: 54244fc111e409def4c33010a1d723e02fa21d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""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": None},
       {"id": "5", "liver": None, "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": None},
       {"id": "5", "liver": None, "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:
        cdata = rqtl2.control_data(zfile)
        assert tuple(rqtl2.file_data(zfile, "pheno", cdata)) == expected

@pytest.mark.unit_test
@pytest.mark.parametrize(
    "filepath,expected",
    (("tests/r_qtl/test_files/test_phenocovar.zip",
      ({"id": "T0", "time (hrs)": "0"},
       {"id": "T2", "time (hrs)": "0.0333333333333333"},
       {"id": "T4", "time (hrs)": "0.0666666666666667"},
       {"id": "T6", "time (hrs)": "0.1"},
       {"id": "T8", "time (hrs)": "0.133333333333333"})),
     ("tests/r_qtl/test_files/test_phenocovar_transposed.zip",
      ({"id": "T0", "time (hrs)": "0"},
       {"id": "T2", "time (hrs)": "0.0333333333333333"},
       {"id": "T4", "time (hrs)": "0.0666666666666667"},
       {"id": "T6", "time (hrs)": "0.1"},
       {"id": "T8", "time (hrs)": "0.133333333333333"}))))
def test_parse_phenocovar_files(filepath, expected):
    """Test parsing of 'phenocovar' files from the R/qtl2 bundle.

    GIVEN: path to a R/qtl2 bundle
    WHEN: the 'phenocovar' file is parsed
    THEN: verify the parsed data is as expected
    """
    with ZipFile(Path(filepath).absolute(), "r") as zfile:
        cdata = rqtl2.control_data(zfile)
        assert tuple(rqtl2.file_data(zfile, "phenocovar", cdata)) == expected


@pytest.mark.unit_test
@pytest.mark.parametrize(
    "filepath,member,transposed,expected",
    (("tests/r_qtl/test_files/test_pheno.zip",
      "test_pheno.csv",
      False,
      ("1", "2", "3", "4", "5")),
     ("tests/r_qtl/test_files/test_pheno_transposed.zip",
      "test_pheno_transposed.csv",
      True,
      ("1", "2", "3", "4", "5"))))
def test_load_geno_samples(filepath, member, transposed, expected):
    assert sorted(rqtl2.load_samples(
        filepath, member, transposed)) == sorted(expected)