-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
434 lines (364 loc) · 14.9 KB
/
test.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import os
import io
import sys
import unittest
from unittest import TestCase
from unittest.mock import patch
import webservices as ws
import lrgparser as lrgp
import ui as ui
import bedgen as bg
import functions
import xml.etree.ElementTree as ET
class WebServicesTests(TestCase):
"""Tests designed to test the functions contained within the
webservices.py file.
"""
def setUp(self):
self.this_directory_path = os.path.dirname(__file__)
self.xml_path_relative = "testfiles/LRG_384.xml"
self.xml_path_full = self.this_directory_path + self.xml_path_relative
def test_hgnc_search(self):
"""Checks that the LRG ID returned by the LRG website is correct """
self.assertEqual(ws.search_by_hgnc("MYH7"), "LRG_384")
def test_invalid_hgnc_search(self):
"""Checks that a SystemExit is raised when invalid input is provided"""
with self.assertRaises(SystemExit) as se:
ws.search_by_hgnc("invalid_hgnc")
def test_invalid_search_by_lrg(self):
"""Checks that a SystemExit is raised when invalid input is provided"""
with self.assertRaises(SystemExit) as se:
ws.search_by_lrg("invalid_lrg")
def test_lrg_xml_file(self):
"""Checks that the LRG file returned by the LRG website has the
correct ID
"""
test_xml = open(self.xml_path_full)
test_xml_contents = test_xml.read()
test_xml.close()
self.assertIn("LRG_384", test_xml_contents)
class LRGParserTests(TestCase):
"""Tests designed to test the functions contained within the
lrgparser.py file.
"""
def setUp(self):
self.this_directory_path = os.path.dirname(__file__)
self.xml_path_relative = "testfiles/LRG_384.xml"
self.xml_path_full = self.this_directory_path + self.xml_path_relative
def test_get_tree_and_root_file(self):
"""Tests that a file passed to the get_tree_and_root_file
function returns a root object with a single root tag - "lrg"
"""
root = lrgp.get_tree_and_root_file(str(self.xml_path_full))
self.assertEqual(root.tag, "lrg")
def test_invalid_get_tree_and_root_file(self):
"""Tests that a file passed to the get_tree_and_root_file
function returns a root object with a single root tag - "lrg"
"""
with self.assertRaises(SystemExit) as se:
lrgp.get_tree_and_root_file(str("fakefilepath"))
def test_get_tree_and_root_string(self):
"""Tests that a string passed to the get_tree_and_root_string
function returns a root object with a single root tag - "lrg"
"""
try:
lrg_xml = ws.search_by_lrg("LRG_384")
root = lrgp.get_tree_and_root_string(lrg_xml)
self.assertEqual(root.tag, "lrg")
except Exception as e:
self.fail("Error querying the LRG site:", e)
def test_get_genome_builds(self):
"""Tests that the correct genome build numbers are extracted from
an LRG xml file
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
genomebuilds = lrgp.get_genome_builds(root)
expected_genomebuilds = ['GRCh37.p13', 'GRCh38.p12']
self.assertEqual(genomebuilds, expected_genomebuilds)
def test_get_transcript_ids(self):
"""Tests that the correct transcript IDs are extracted from
an LRG xml file
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
transcripts = lrgp.get_transcript_ids(root)
expected_transcripts = ['NM_000257.2',
'NM_000257.3',
'ENST00000355349.3']
self.assertEqual(transcripts, expected_transcripts)
def test_lrg_object_creator(self):
"""Tests that an 'lrg_object' is created properly when using the
lrg_object_creator function and LRG_Object class
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
genome_choice = 'GRCh37.p13'
transcript_choice = 'NM_000257.2'
flank = 0
lrg_object = lrgp.lrg_object_creator(root,
genome_choice,
transcript_choice,
flank)
self.assertEqual(lrg_object.lrg_id, 'LRG_384')
self.assertEqual(lrg_object.hgnc_id, '7577')
self.assertEqual(lrg_object.seq_source, 'NG_007884.1')
self.assertEqual(lrg_object.mol_type, 'dna')
self.assertEqual(lrg_object.chromosome, '14')
self.assertEqual(lrg_object.mapped_flanked_exon_coords.get(1),
[23904870, 23904828])
self.assertEqual(lrg_object.mapped_flanked_exon_coords.get(40),
[23882080, 23881946])
def test_arg_collection(self):
"""Tests that arguments passed to the argument parsing function are
correctly captured
"""
arguments = lrgp.arg_collection(['-l LRG_384'])
self.assertEqual(arguments.get("lrgid"), " LRG_384")
@patch('bedgen.write_bed_file', return_value=True)
def test_automated_main(self, write_bed_file):
"""Tests the lrgparser main() function using sufficient arguments
for automated BED file generation. The writing of the physical file is
prevented using the @patch decorator to avoid unwanted file generation
"""
arguments_full = lrgp.arg_collection(["-f", "testfiles/LRG_384.xml",
"-r", "GRCh37.p13",
"-t", "NM_000257.2"])
arguments_full_introns = lrgp.arg_collection(["-f", "testfiles/LRG_384.xml",
"-r", "GRCh37.p13",
"-t", "NM_000257.2",
"-i"])
arguments_full_flank = lrgp.arg_collection(["-f", "testfiles/LRG_384.xml",
"-r", "GRCh37.p13",
"-t", "NM_000257.2",
"-fl", "200"])
self.assertEqual(lrgp.main(arguments_full), True)
self.assertEqual(lrgp.main(arguments_full_introns), True)
self.assertEqual(lrgp.main(arguments_full_flank), True)
@patch('ui.input', side_effect=["MYH7", "1", "1", "0", "y"])
@patch('bedgen.write_bed_file', return_value=True)
@patch('os.system', return_value="")
def test_nonautomated_main(self, input, write_bed_file, system):
"""Tests the lrgparser main() function using insufficient arguments
for automated BED file generation. ui.input is patched to simulate
user input for gene name, genome selection, transcript selection,
flank size and intron inclusion. The writing of the physical file is
prevented using the @patch decorator to avoid unwanted file generation
"""
arguments = lrgp.arg_collection([])
self.assertEqual(lrgp.main(arguments), True)
class BedgenTests(TestCase):
"""Tests to designed to test the functions contained within the
bedgen.py file.
"""
def setUp(self):
self.this_directory_path = os.path.dirname(__file__)
self.xml_path_relative = "testfiles/LRG_384.xml"
self.xml_path_full = self.this_directory_path + self.xml_path_relative
def test_create_bed_contents(self):
"""Tests that the contents of the BED file are correctly generated
with accurate values
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
genome_choice = 'GRCh37.p13'
transcript_choice = 'NM_000257.2'
flank = 0
lrg_object = lrgp.lrg_object_creator(root,
genome_choice,
transcript_choice,
flank)
bedcontents_nointrons = bg.create_bed_contents(lrg_object, False)
bedcontents_introns = bg.create_bed_contents(lrg_object, True)
self.assertEqual(len(bedcontents_nointrons), 40)
self.assertEqual(len(bedcontents_introns), 79)
self.assertEqual(bedcontents_nointrons[0],
['chr14', 23904828, 23904870, "Exon_1"])
self.assertEqual(bedcontents_nointrons[39],
['chr14', 23881946, 23882080, "Exon_40"])
self.assertEqual(bedcontents_introns[40],
['chr14', 23903459, 23904827, 'Intron_1'])
self.assertEqual(bedcontents_introns[78],
['chr14', 23882081, 23882966, 'Intron_39'])
def test_write_bed_file(self):
"""Tests that the BED file can be written to the local disk"""
filepath = "testfilename"
bedheader = ["header_item_1", "header_item_2"]
bedcontents = [['chr14', 23881946, 23882080, "Exon_1"],
['chr14', 23881946, 23882080, "Exon_40"]]
success = bg.write_bed_file(filepath, bedheader, bedcontents)
self.assertEqual(success, True)
os.remove(filepath)
with self.assertRaises(SystemExit) as se:
bg.write_bed_file(None, bedheader, bedcontents)
class FunctionsTests(TestCase):
""" Tests that the functions that handle exon and intron coordinates
create dictionaries containing the correct values.
"""
def setUp(self):
self.this_directory_path = os.path.dirname(__file__)
self.xml_path_relative = "testfiles/LRG_384.xml"
self.xml_path_full = self.this_directory_path + self.xml_path_relative
self.xml_path_relative_pos = "testfiles/LRG_155.xml"
self.xml_path_full_pos = self.this_directory_path + self.xml_path_relative_pos
def test_get_exon_coords(self):
""" Tests that assess the creation of the exon_coords dictionary
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
test_xml_pos = open(self.xml_path_full_pos)
root_pos = lrgp.get_tree_and_root_file(test_xml_pos)
test_xml_pos.close()
genome_choice = 'GRCh37.p13'
transcript_choice = 'NM_000257.2'
pos_transcript_choice = 'NM_002389.4'
exon_coordinates = functions.get_exon_coords(root,
genome_choice,
transcript_choice)
pos_exon_coordinates = functions.get_exon_coords(root_pos,
genome_choice,
pos_transcript_choice)
self.assertEqual(type(exon_coordinates),dict)
self.assertEqual(len(exon_coordinates), 40)
self.assertEqual(exon_coordinates[1],[23904870,23904828])
self.assertEqual(exon_coordinates[40],[23882080,23881946])
self.assertEqual(type(pos_exon_coordinates),dict)
self.assertEqual(len(pos_exon_coordinates), 14)
def test_get_intron_coords(self):
""" Tests that assess the creation of the intron_coords dictionary
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
test_xml_pos = open(self.xml_path_full_pos)
root_pos = lrgp.get_tree_and_root_file(test_xml_pos)
test_xml_pos.close()
genome_choice = 'GRCh37.p13'
transcript_choice = 'NM_000257.2'
pos_transcript_choice = 'NM_002389.4'
exon_coordinates = functions.get_exon_coords(root,
genome_choice,
transcript_choice)
intron_coordinates = functions.get_intron_coords(exon_coordinates)
self.assertEqual(type(intron_coordinates),dict)
self.assertEqual(len(intron_coordinates), 39)
self.assertEqual(intron_coordinates[1],[23904827,23903459])
self.assertEqual(intron_coordinates[39],[23882966,23882081])
pos_exon_coordinates = functions.get_exon_coords(root_pos,
genome_choice,
pos_transcript_choice)
pos_intron_coordinates = functions.get_intron_coords(pos_exon_coordinates)
self.assertEqual(type(pos_intron_coordinates),dict)
self.assertEqual(len(pos_intron_coordinates), 13)
def test_get_flanked_coords(self):
""" Tests that asses the creation of the exon_coords dictionary when
flanking regions are used.
"""
test_xml = open(self.xml_path_full)
root = lrgp.get_tree_and_root_file(test_xml)
test_xml.close()
test_xml_pos = open(self.xml_path_full_pos)
root_pos = lrgp.get_tree_and_root_file(test_xml_pos)
test_xml_pos.close()
genome_choice = 'GRCh37.p13'
transcript_choice = 'NM_000257.2'
pos_transcript_choice = 'NM_002389.4'
exon_coordinates = functions.get_exon_coords(root,
genome_choice,
transcript_choice)
flanked_coordinates = functions.get_flanked_coords(exon_coordinates,
100)
self.assertEqual(type(flanked_coordinates),dict)
self.assertEqual(len(flanked_coordinates), 40)
pos_exon_coordinates = functions.get_exon_coords(root_pos,
genome_choice,
pos_transcript_choice)
pos_flanked_coordinates = functions.get_flanked_coords(pos_exon_coordinates,
100)
self.assertEqual(type(pos_flanked_coordinates),dict)
self.assertEqual(len(pos_flanked_coordinates), 14)
self.assertEqual(pos_flanked_coordinates[1],[207925282,207925754])
self.assertEqual(pos_flanked_coordinates[14],[207966763,207968961])
class UITests(TestCase):
"""Tests designed to test the functions contained within the
ui.py file. User input with input() is simulated using the @patch
decorator from unittest.mock
"""
def setUp(self):
"""Create some dictionaries to be used as arguments in the tests.
"""
self.args_none = {'file': None, 'geneid': None, 'lrgid': None}
self.args = {'file': "test", 'geneid': "7577", 'lrgid': "LRG_384"}
def test_determine_if_show_ui_true(self):
"""Check that the UI will be shown if no file, geneid or lrgid is
provided
"""
self.assertEqual(ui.determine_if_show_ui(self.args_none), True)
def test_determine_if_show_ui_false(self):
"""Check that the UI will not be shown if a file, geneid or lrgid is
provided
"""
self.assertEqual(ui.determine_if_show_ui(self.args), False)
@patch('ui.os.system', return_value=None)
def test_splashscreen(self, systemreplace):
"""Checks that the splashscreen correctly returns. Functions without
a return statement will return None if completed successfully.
"""
self.assertEqual(ui.splashscreen(), None)
@patch('ui.input', return_value='MYH7')
def test_ask_what_gene(self, input):
"""Check that the ask_what_gene() function correctly returns the
string that the user enters.
"""
self.assertEqual(ui.ask_what_gene(), "MYH7")
@patch('ui.input', side_effect=["badinput", "10", "1"])
def test_ask_which_genome_build(self, input):
"""Check that the ask_which_genome() function correctly returns the
string that the user enters.
"""
availablebuilds = ['GRCh37.p13', 'GRCh38.p12']
self.assertEqual(ui.ask_which_genome_build(availablebuilds),
"GRCh37.p13")
@patch('ui.input', side_effect=["badinput", "10", "1"])
def test_ask_which_transcript(self, input):
"""Check that the ask_which_transcript() function correctly handles
invalid input (string then number choice that is too large)
followed by a valid transcript.
"""
availabletranscripts = ["NM_000257.2",
"NM_000257.4",
"ENST00000355349.3"]
self.assertEqual(ui.ask_which_transcript(availabletranscripts),
"NM_000257.2")
@patch('ui.input', return_value="y")
def test_ask_include_introns_yes(self, input):
"""Check that the ask_include_introns() function correctly returns
True when chosen
"""
self.assertEqual(ui.ask_include_introns(), True)
@patch('ui.input', side_effect=["badinput", "y"])
def test_ask_include_introns_invalid(self, input):
"""Check that the ask_include_introns() function correctly handles
invalid input followed by valid input
"""
self.assertEqual(ui.ask_include_introns(), True)
@patch('ui.input', return_value="n")
def test_ask_include_introns_no(self, input):
"""Check that the ask_include_introns() function correctly returns
False when chosen
"""
self.assertEqual(ui.ask_include_introns(), False)
@patch('ui.input', side_effect=["badinput", "-10", "150"])
def test_ask_flank_size(self, input):
"""Check that the ask_flank_size() function correctly handles
invalid input (string and then an int over the max allowed)
followed by returning a valid input value when provided (150)
"""
self.assertEqual(ui.ask_flank_size(), "150")
if __name__ == '__main__':
unittest.main(buffer=True)