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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
import unittest
import datetime
from unittest import mock
import gn2.wqflask.marker_regression.run_mapping as run_mapping
from gn2.wqflask.marker_regression.run_mapping import get_genofile_samplelist
from gn2.wqflask.marker_regression.run_mapping import geno_db_exists
from gn2.wqflask.marker_regression.run_mapping import write_input_for_browser
from gn2.wqflask.marker_regression.run_mapping import export_mapping_results
from gn2.wqflask.marker_regression.run_mapping import trim_markers_for_figure
from gn2.wqflask.marker_regression.run_mapping import get_perm_strata
from gn2.wqflask.marker_regression.run_mapping import get_chr_lengths
class AttributeSetter:
def __init__(self, obj):
for k, v in obj.items():
setattr(self, k, v)
class MockGroup(AttributeSetter):
def get_genofiles(self):
return [{"location": "~/genofiles/g1_file", "sample_list": ["S1", "S2", "S3", "S4"]}]
class TestRunMapping(unittest.TestCase):
def setUp(self):
self.group = MockGroup(
{"genofile": "~/genofiles/g1_file", "name": "GP1_", "species": "Human"})
chromosomes = {
"3": AttributeSetter({
"name": "C1",
"length": "0.04"
}),
"4": AttributeSetter({
"name": "C2",
"length": "0.03"
}),
"5": AttributeSetter({
"name": "C4",
"length": "0.01"
})
}
self.dataset = AttributeSetter(
{"fullname": "dataset_1", "group": self.group, "type": "ProbeSet"})
self.chromosomes = AttributeSetter({"chromosomes": lambda cur: chromosomes})
self.trait = AttributeSetter(
{"symbol": "IGFI", "chr": "X1", "mb": 123313, "display_name": "Test Name"})
def tearDown(self):
self.dataset = AttributeSetter(
{"group": {"location": "~/genofiles/g1_file"}})
def test_get_genofile_samplelist(self):
results_1 = get_genofile_samplelist(self.dataset)
self.assertEqual(results_1, ["S1", "S2", "S3", "S4"])
self.group.genofile = "~/genofiles/g2_file"
result_2 = get_genofile_samplelist(self.dataset)
self.assertEqual(result_2, [])
@mock.patch("gn2.wqflask.marker_regression.run_mapping.data_set")
def test_if_geno_db_exists(self, mock_data_set):
mock_data_set.create_dataset.side_effect = [
AttributeSetter({}), Exception()]
run_mapping.create_trait = mock.Mock()
results_no_error = geno_db_exists(self.dataset, "dataset_1")
results_with_error = geno_db_exists(self.dataset, "dataset_1")
self.assertEqual(mock_data_set.create_dataset.call_count, 2)
self.assertEqual(results_with_error, "False")
self.assertEqual(results_no_error, "True")
def test_trim_markers_for_figure(self):
markers = [{
"name": "MK1",
"chr": "C1",
"cM": "1",
"Mb": "12000",
"genotypes": [],
"dominance":"TT",
"additive":"VA",
"lod_score":0.5
},
{
"name": "MK2",
"chr": "C2",
"cM": "15",
"Mb": "10000",
"genotypes": [],
"lod_score":0.7
},
{
"name": "MK1",
"chr": "C3",
"cM": "45",
"Mb": "1",
"genotypes": [],
"dominance":"Tt",
"additive":"VE",
"lod_score":1
}]
marker_2 = [{
"name": "MK1",
"chr": "C1",
"cM": "1",
"Mb": "12000",
"genotypes": [],
"dominance":"TT",
"additive":"VA",
"p_wald":4.6
}]
results = trim_markers_for_figure(markers)
result_2 = trim_markers_for_figure(marker_2)
expected = [
{
"name": "MK1",
"chr": "C1",
"cM": "1",
"Mb": "12000",
"genotypes": [],
"dominance":"TT",
"additive":"VA",
"lod_score":0.5
},
{
"name": "MK1",
"chr": "C3",
"cM": "45",
"Mb": "1",
"genotypes": [],
"dominance":"Tt",
"additive":"VE",
"lod_score":1
}
]
self.assertEqual(results, expected)
self.assertEqual(result_2, marker_2)
def test_export_mapping_results(self):
"""test for exporting mapping results"""
datetime_mock = mock.Mock(wraps=datetime.datetime)
datetime_mock.now.return_value = datetime.datetime(
2019, 9, 1, 10, 12, 12)
markers = [{
"name": "MK1",
"chr": "C1",
"cM": "1",
"Mb": "12000",
"genotypes": [],
"dominance":"TT",
"additive":"VA",
"lod_score":3
},
{
"name": "MK2",
"chr": "C2",
"cM": "15",
"Mb": "10000",
"genotypes": [],
"lod_score":7
},
{
"name": "MK1",
"chr": "C3",
"cM": "45",
"Mb": "1",
"genotypes": [],
"dominance":"Tt",
"additive":"VE",
"lod_score":7
}]
with mock.patch("builtins.open", mock.mock_open()) as mock_open:
with mock.patch("gn2.wqflask.marker_regression.run_mapping.datetime.datetime", new=datetime_mock):
export_mapping_results(dataset=self.dataset, trait=self.trait, markers=markers,
results_path="~/results", mapping_method="gemma", mapping_scale="physic",
score_type="-logP", transform="qnorm",
covariates="Dataset1:Trait1,Dataset2:Trait2",
n_samples="100", vals_hash="")
write_calls = [
mock.call('Time/Date: 09/01/19 / 10:12:12\n'),
mock.call('Population: Human GP1_\n'), mock.call(
'Data Set: dataset_1\n'),
mock.call('Trait: Test Name\n'),
mock.call('Trait Hash: \n'),
mock.call('N Samples: 100\n'),
mock.call('Mapping Tool: gemma\n'),
mock.call('Transform - Quantile Normalized\n'),
mock.call('Gene Symbol: IGFI\n'), mock.call(
'Location: X1 @ 123313 Mb\n'),
mock.call('Cofactors (dataset - trait):\n'),
mock.call('Trait1 - Dataset1\n'),
mock.call('Trait2 - Dataset2\n'),
mock.call('\n'), mock.call('Name,Chr,'),
mock.call('Mb,-logP'),
mock.call(',Additive'), mock.call(',Dominance'),
mock.call('\n'), mock.call('MK1,C1,'),
mock.call('12000,'), mock.call('3'),
mock.call(',VA'), mock.call(',TT'),
mock.call('\n'), mock.call('MK2,C2,'),
mock.call('10000,'), mock.call('7'),
mock.call('\n'), mock.call('MK1,C3,'),
mock.call('1,'), mock.call('7'),
mock.call(',VE'), mock.call(',Tt')
]
mock_open.assert_called_once_with("~/results", "w+")
filehandler = mock_open()
filehandler.write.assert_has_calls(write_calls)
@mock.patch("gn2.wqflask.marker_regression.run_mapping.random.choice")
def test_write_input_for_browser(self, mock_choice):
"""test for writing input for browser"""
mock_choice.side_effect = ["F", "i", "l", "e", "s", "x"]
with mock.patch("builtins.open", mock.mock_open()) as mock_open:
expected = ['GP1__Filesx_GWAS', 'GP1__Filesx_ANNOT']
results = write_input_for_browser(
this_dataset=self.dataset, gwas_results={}, annotations={})
self.assertEqual(results, expected)
def test_get_perm_strata(self):
categorical_vars = ["C1", "C2", "W1"]
used_samples = ["S1", "S2"]
sample_list = AttributeSetter({"sample_attribute_values": {
"S1": {
"c1": "c1_value",
"c2": "c2_value",
"w1": "w1_value"
},
"S2": {
"w1": "w2_value",
"w2": "w2_value"
},
"S3": {
"c1": "c1_value",
"c2": "c2_value"
},
}})
results = get_perm_strata(this_trait={}, sample_list=sample_list,
categorical_vars=categorical_vars, used_samples=used_samples)
self.assertEqual(results, [1, 1])
def test_get_chr_length(self):
"""test for getting chromosome length"""
cursor = mock.MagicMock()
chromosomes = AttributeSetter({"chromosomes": self.chromosomes})
dataset = AttributeSetter({"species": chromosomes})
results = get_chr_lengths(
mapping_scale="physic", mapping_method="reaper", dataset=dataset, qtl_results=[])
chr_lengths = []
for key, chromo in self.chromosomes.chromosomes(cursor).items():
chr_lengths.append({"chr": chromo.name, "size": chromo.length})
self.assertEqual(chr_lengths, results)
qtl_results = [{
"chr": "16",
"cM": "0.2"
},
{
"chr": "12",
"cM": "0.5"
},
{
"chr": "18",
"cM": "0.1"
},
{
"chr": "22",
"cM": "0.4"
},
]
result_with_other_mapping_scale = get_chr_lengths(
mapping_scale="other", mapping_method="reaper", dataset=dataset, qtl_results=qtl_results)
expected_value = [{'chr': '1', 'size': '0'}, {
'chr': '16', 'size': '500000.0'}, {'chr': '18', 'size': '400000.0'}]
self.assertEqual(result_with_other_mapping_scale, expected_value)
|