-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_cna.py
149 lines (124 loc) · 4.61 KB
/
test_cna.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from unittest import mock
from unittest.mock import patch
import pytest
import pandas as pd
import synapseclient
from genie_registry.cna import cna
def createMockTable(dataframe):
table = mock.create_autospec(synapseclient.table.CsvFileTable)
table.asDataFrame.return_value = dataframe
return table
def table_query_results(*args):
return table_query_results_map[args]
database_mapping = pd.DataFrame(dict(Database=["bed"], Id=["syn8457748"]))
symbols = pd.DataFrame(
dict(
Hugo_Symbol=["AAK1", "AAED1", "AAAS", "AAED1"],
ID=["AAK1", "AAED", "AAAS", "AAD"],
)
)
# This is the gene positions that all bed dataframe will be processed against
table_query_results_map = {
("select Hugo_Symbol, ID from syn11600834 where CENTER = 'SAGE'",): createMockTable(
symbols
),
}
ENTITY = synapseclient.Project("testing", annotations={"dbMapping": ["syn10967259"]})
@pytest.fixture
def cna_class(syn, genie_config):
syn.tableQuery.side_effect = table_query_results
return cna(syn, "SAGE", genie_config)
def test_processing(cna_class):
order = ["Hugo_Symbol", "Entrez_gene_id", "GENIE-SAGE-Id1-1", "GENIE-SAGE-Id2-1"]
expected_cnadf = pd.DataFrame(
{
"Hugo_Symbol": ["AAED1", "AAK1", "AAAS"],
"GENIE-SAGE-Id1-1": [-0.5, 2.0, 0.5],
"GENIE-SAGE-Id2-1": [1.0, 1.5, -1.5],
}
)
cnadf = pd.DataFrame(
{
"Hugo_Symbol": ["AAED", "AAK1", "AAAS"],
"Entrez_gene_id": [0, 0, 0],
"GENIE-SAGE-Id1-1": [-0.5, 2, 0.5],
"GENIE-SAGE-Id2-1": [1, 1.5, -1.5],
}
)
cnadf = cnadf[order]
new_cnadf = cna_class._process(cnadf)
assert expected_cnadf.equals(new_cnadf[expected_cnadf.columns])
order = ["Hugo_Symbol", "Entrez_gene_id", "GENIE-SAGE-Id1-1", "GENIE-SAGE-Id2-1"]
expectedCnaDf = pd.DataFrame(
{
"Hugo_Symbol": ["AAAS", "AAED1"],
"GENIE-SAGE-Id1-1": [float("nan"), 1.0],
"GENIE-SAGE-Id2-1": [float("nan"), 2.0],
}
)
cnaDf = pd.DataFrame(
{
"Hugo_Symbol": ["AAED", "AAED1", "foo", "AAAS", "AAD"],
"Entrez_gene_id": [0, 0, 0, 0, 0],
"GENIE-SAGE-Id1-1": [1, 1, 0, float("nan"), 1],
"GENIE-SAGE-Id2-1": [2, 0, -1, float("nan"), float("nan")],
}
)
cnaDf = cnaDf[order]
newCnaDf = cna_class._process(cnaDf)
newCnaDf.reset_index(inplace=True, drop=True)
pd.testing.assert_frame_equal(expectedCnaDf, newCnaDf[expectedCnaDf.columns])
def test_validation(syn, cna_class):
with pytest.raises(AssertionError):
cna_class.validateFilename(["foo"])
assert cna_class.validateFilename(["data_CNA_SAGE.txt"]) == "cna"
order = ["Hugo_Symbol", "Entrez_gene_id", "GENIE-SAGE-ID1-1", "GENIE-SAGE-ID2-1"]
cnaDf = pd.DataFrame(
{
"Hugo_Symbol": ["AAED", "AAK1", "AAAS"],
"Entrez_gene_id": [0, 0, 0],
"GENIE-SAGE-ID1-1": [-2, 0, 1],
"GENIE-SAGE-ID2-1": [0.5, 1.5, -1.5],
"GENIE-SAGE-ID3-1": [float("nan"), 2, "NA"],
}
)
cnaDf = cnaDf[order]
with patch.object(syn, "get", return_value=ENTITY):
error, warning = cna_class._validate(cnaDf, False)
assert error == ""
assert warning == ""
cnaDf = pd.DataFrame(
{
"Hugo_Symbol": ["foo", "AAED", "AAED1", "AAD"],
"GENIE-SAGE-ID1-1": [1, 2, float("nan"), 1],
"GENIE-SAGE-ID2-1": [2, 1, -1, 2],
}
)
cnaDf.sort_values("Hugo_Symbol", inplace=True)
cnaDf = cnaDf[["GENIE-SAGE-ID1-1", "Hugo_Symbol", "GENIE-SAGE-ID2-1"]]
with patch.object(syn, "get", return_value=ENTITY):
error, warning = cna_class._validate(cnaDf, False)
expectedErrors = (
"Your cnv file's first column must be Hugo_Symbol\n"
"Your CNA file has duplicated Hugo_Symbols (After "
"remapping of genes): AAD,AAED,AAED1 -> AAED1,AAED1,AAED1.\n"
)
assert error == expectedErrors
assert warning == ""
order = ["Hugo_Symbol", "GENIE-SAGE-ID1-1", "GENIE-SAGE-ID2-1"]
cnaDf = pd.DataFrame(
{
"Hugo_Symbol": ["AAK1", "AAAS", "AAED1"],
"GENIE-SAGE-ID1-1": [1, 2, 1],
"GENIE-SAGE-ID2-1": [2, 1, 3],
}
)
cnaDf = cnaDf[order]
with patch.object(syn, "get", return_value=ENTITY):
error, warning = cna_class._validate(cnaDf, False)
expectedErrors = (
"All values must be NA/blank, -2, -1.5, -1, -0.5, 0, "
"0.5, 1, 1.5, or 2.\n"
)
assert error == expectedErrors
assert warning == ""