-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·412 lines (334 loc) · 13.5 KB
/
Copy pathtest.py
File metadata and controls
executable file
·412 lines (334 loc) · 13.5 KB
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
#!/anaconda3/envs/python2/bin/python
"""
test.py
Unit tests for the vcf_parse.py program.
Author: Erik Waskiewicz
Created: 22 Oct 2018
Version: 1.0.0
Updated: 23 Oct 2018
"""
import unittest
import os
import csv
from scripts.vcf_report import vcf_report
from scripts.preferred_transcripts import preferred_transcripts
from scripts.bed_object import bed_object
from scripts.known_variants import known_variants
class TestVCF(unittest.TestCase):
def setUp(self):
"""load in common files"""
self.report = vcf_report()
self.report.load_data(
os.path.abspath('test/test.vcf'), os.path.abspath('test/')
)
self.report.make_report(False)
def tearDown(self):
"""remove output files after test has run"""
os.remove(self.report.report_path)
self.report = None
self.pt = None
# list of files to remove
filenames = ['test/SAMPLE1_bed1_VariantReport.txt',
'test/test/SAMPLE1_bed1_VariantReport.txt',
'test/test/SAMPLE1_bed2_VariantReport.txt',
'test/test/SAMPLE1_bed3bed_VariantReport.txt',
'test/test/SAMPLE1_edge_VariantReport.txt',]
for filename in filenames:
if os.path.isfile(filename):
os.remove(os.path.abspath(filename))
def test_vcf_parser_number_variants(self):
"""
Check that number of variants loaded from VCF is correct,
should be 96
"""
self.assertEqual(len(self.report.data), 96,
'Number of variants incorrect'
)
def test_vcf_parser_sample_name(self):
"""
Check that sample name loaded from VCF is correct,
should be SAMPLE1
"""
self.assertEqual(self.report.sample, 'SAMPLE1',
'Sample name incorrect'
)
def test_vcf_parser_info_fields(self):
"""
Check that info fields loaded from VCF are correct,
should be the same as the expected list below
"""
expected_info_fields = ['AC', 'AF', 'AN', 'BaseQRankSum',
'DP', 'EXON', 'FC', 'GI', 'LCRLen', 'MQ', 'MQ0',
'MQRankSum', 'TI', 'CSQ']
report_info_fields = []
for field in self.report.info_fields:
report_info_fields.append(field)
self.assertEqual(report_info_fields, expected_info_fields,
'info fields incorrect'
)
def test_vcf_parser_format_fields(self):
"""
Check that format fields loaded from VCF are correct,
should be the same as the expected list below
"""
expected_format_fields = ['AD', 'GQ', 'GT', 'NL', 'SB', 'VF']
report_format_fields = []
for field in self.report.format_fields:
report_format_fields.append(field)
self.assertEqual(report_format_fields, expected_format_fields,
'format fields incorrect'
)
def test_vcf_parser_vep_fields(self):
"""
Check that vep fields loaded from VCF are correct,
should be the same as the expected list below
"""
expected_vep_fields = ['Allele', 'Consequence', 'IMPACT', 'SYMBOL',
'Gene', 'Feature_type', 'Feature', 'BIOTYPE', 'EXON', 'INTRON',
'HGVSc', 'HGVSp', 'cDNA_position', 'CDS_position', 'Protein_position',
'Amino_acids', 'Codons', 'Existing_variation', 'ALLELE_NUM',
'DISTANCE', 'STRAND', 'FLAGS', 'VARIANT_CLASS', 'SYMBOL_SOURCE',
'HGNC_ID', 'CANONICAL', 'TSL', 'APPRIS', 'CCDS', 'ENSP', 'SWISSPROT',
'TREMBL', 'UNIPARC', 'REFSEQ_MATCH', 'GENE_PHENO', 'SIFT', 'PolyPhen',
'DOMAINS', 'HGVS_OFFSET', 'GMAF', 'AFR_MAF', 'AMR_MAF', 'EAS_MAF',
'EUR_MAF', 'SAS_MAF', 'AA_MAF', 'EA_MAF', 'ExAC_MAF', 'ExAC_Adj_MAF',
'ExAC_AFR_MAF', 'ExAC_AMR_MAF', 'ExAC_EAS_MAF', 'ExAC_FIN_MAF',
'ExAC_NFE_MAF', 'ExAC_OTH_MAF', 'ExAC_SAS_MAF', 'CLIN_SIG', 'SOMATIC',
'PHENO', 'PUBMED', 'MOTIF_NAME', 'MOTIF_POS', 'HIGH_INF_POS',
'MOTIF_SCORE_CHANGE']
self.assertEqual(self.report.vep_fields, expected_vep_fields)
def test_variant_report_number_variants_filter(self):
"""
Check that number of rows in the variant report is correct,
should be 477
"""
self.report.make_report(True)
report_sum = sum(1 for line in open(os.path.abspath(
'test/SAMPLE1_VariantReport.txt'
)))
self.assertEqual(report_sum, 229,
'Number of variants incorrect'
)
def test_variant_report_number_variants_no_filter(self):
"""
Check that number of rows in the variant report is correct,
should be 477
"""
report_sum = sum(1 for line in open(os.path.abspath(
'test/SAMPLE1_VariantReport.txt'
)))
self.assertEqual(report_sum, 271,
'Number of variants incorrect'
)
def test_load_config(self):
"""
Check that settings file is loaded correctly, the headers loaded in
should be the same as the expected list below
"""
expected_settings = ['SampleID', 'Variant', 'Filter', 'Genotype', 'AD',
'Depth', 'Transcript', 'Pref', 'CANONICAL', 'Allele', 'Consequence',
'IMPACT', 'Gene', 'Feature_type']
# load config
self.report.load_config(os.path.abspath('test/config.txt'))
self.report.make_report(False)
# compare headers in report to expected list
with open(self.report.report_path) as f:
reader = csv.reader(f, delimiter='\t')
self.assertEqual(reader.next(), expected_settings)
def test_preferred_transcripts_high_strictness_true(self):
"""
Check that preferred transcripts are labelled correctly.
NM_001007553 should be true with high and low strictness
"""
# apply preferred transcripts
self.pt = preferred_transcripts()
self.pt.load(os.path.abspath('test/PreferredTranscripts.txt'))
self.pt.apply(self.report, 'high')
# check in report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '1:115256669G>A':
if 'NM_001007553' in line[29]:
self.assertEqual(line[3], 'True')
def test_preferred_transcripts_high_strictness_false(self):
"""
Check that preferred transcripts are labelled correctly.
XM_005245221 should be false with high strictness and true with
low strictness.
"""
# apply preferred transcripts
self.pt = preferred_transcripts()
self.pt.load(os.path.abspath('test/PreferredTranscripts.txt'))
self.pt.apply(self.report, 'high')
# check in report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '1:162681151T>G':
if 'XM_005245221' in line[29]:
self.assertEqual(line[3], 'False')
def test_preferred_transcripts_low_strictness_true_1(self):
"""
Check that preferred transcripts are labelled correctly.
NM_001007553 should be true with high and low strictness
"""
# apply preferred transcripts
self.pt = preferred_transcripts()
self.pt.load(os.path.abspath('test/PreferredTranscripts.txt'))
self.pt.apply(self.report, 'low')
# check in report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '1:115256669G>A':
if 'NM_001007553' in line[29]:
self.assertEqual(line[3], 'True')
def test_preferred_transcripts_low_strictness_true_2(self):
"""
Check that preferred transcripts are labelled correctly.
XM_005245221 should be false with high strictness and true with
low strictness.
"""
# apply preferred transcripts
self.pt = preferred_transcripts()
self.pt.load(os.path.abspath('test/PreferredTranscripts.txt'))
self.pt.apply(self.report, 'low')
# check in report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '1:162681151T>G':
if 'XM_005245221' in line[29]:
self.assertEqual(line[3], 'True')
def test_preferred_transcripts_false(self):
"""
Check that preferred transcripts are labelled correctly.
Not in preferred transcripts - should be false.
"""
# apply preferred transcripts
self.pt = preferred_transcripts()
self.pt.load(os.path.abspath('test/PreferredTranscripts.txt'))
self.pt.apply(self.report, 'low')
# check in report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '3:41265953CT>C':
self.assertEqual(line[2], 'False')
def test_bed_files(self):
"""
Check that correct number of variants are filtered out with BED file
Should be 6
"""
# apply bed file
self.bed = bed_object()
self.bed.apply_single(
os.path.abspath('test/test_bed_files/bed1.bed'), self.report
)
# check number of variants in output
n = sum(1 for line in open(os.path.abspath(
'test/SAMPLE1_bed1_VariantReport.txt'
)))
self.assertEqual(n , 2)
def test_bed_files_multiple(self):
"""
Check that correct number of variants are filtered out with
multiple BED files
Should be 6 and 10
"""
# apply bed files
self.bed = bed_object()
self.bed.apply_multiple(
os.path.abspath('test/test_bed_files'), self.report
)
# check number of variants in outputs
bed1_sum = sum(1 for line in open(os.path.abspath(
'test/test/SAMPLE1_bed1_VariantReport.txt'
)))
bed2_sum = sum(1 for line in open(os.path.abspath(
'test/test/SAMPLE1_bed2_VariantReport.txt'
)))
bed3_sum = sum(1 for line in open(os.path.abspath(
'test/test/SAMPLE1_bed3bed_VariantReport.txt'
)))
self.assertEqual(bed1_sum, 2)
self.assertEqual(bed2_sum, 5)
self.assertEqual(bed3_sum, 5)
def test_known_variants(self):
"""
Check that known variant is correctly labelled as '1'
"""
# apply known variants
known = known_variants()
known.load_known_variants(os.path.abspath(
'test/KnownVariants.vcf'
))
known.apply_known_variants(self.report)
# check output report
with open(self.report.report_path) as report:
reader = csv.reader(report, delimiter='\t')
for line in reader:
if line[1] == '1:162748588C>A':
self.assertEqual(line[3], '1')
class TestEdgeVariants(unittest.TestCase):
def setUp(self):
"""load in common files"""
self.report = vcf_report()
self.report.load_data(
os.path.abspath('test/edge_variants.vcf'), os.path.abspath('test/')
)
self.report.make_report(False)
def tearDown(self):
"""remove output files after test has run"""
os.remove(self.report.report_path)
self.report = None
self.pt = None
# list of files to remove
filenames = ['test/SAMPLE1_edge_VariantReport.txt',
'test/test/SAMPLE1_edge_VariantReport.txt',]
for filename in filenames:
if os.path.isfile(filename):
os.remove(filename)
def test_edge_variants(self):
"""
Test that we get indels which overlap 5' boundary of bed file.
"""
self.bed = bed_object()
self.bed.apply_single(
os.path.abspath('test/test_bed_files/edge.bed'), self.report
)
# check number of variants in output
n = sum(1 for line in open(os.path.abspath(
'test/SAMPLE1_edge_VariantReport.txt'
)))
self.assertEqual(n , 15)
class TestEmptyVcf(unittest.TestCase):
def setUp(self):
"""load in common files"""
self.report = vcf_report()
self.report.load_data(
os.path.abspath('test/empty_vcf.vcf'), os.path.abspath('test/')
)
self.report.make_report(False)
def tearDown(self):
"""remove output files after test has run"""
os.remove(self.report.report_path)
self.report = None
self.pt = None
def test_empty_vcf(self):
"""
Test that there is an output from and empty VCF and that it is empty.
"""
# check that a file has been made
file_made = os.path.isfile('test/SAMPLE1_VariantReport.txt')
self.assertEqual(file_made, True)
# check that there are no variants in output
n = sum(1 for line in open(os.path.abspath(
'test/SAMPLE1_VariantReport.txt'
)))
self.assertEqual(n , 1) #should be one because there is a header only
# Runs all tests when the script is run
# command: python -m unittest -v test
if __name__ == '__main__':
unittest.main()