-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_vcf_expression_annotator.py
357 lines (329 loc) · 16.7 KB
/
test_vcf_expression_annotator.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
import unittest
import sys
import os
import py_compile
from vatools import vcf_expression_annotator
import tempfile
from filecmp import cmp
import logging
from testfixtures import LogCapture, StringComparison as S
class VcfExpressionEncoderTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
base_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
cls.executable = os.path.join(base_dir, 'vatools', 'vcf_expression_annotator.py')
cls.test_data_dir = os.path.join(base_dir, 'tests', 'test_data')
def test_source_compiles(self):
self.assertTrue(py_compile.compile(self.executable))
def test_error_custom_format_id_column_not_set(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'custom',
'gene',
'-e', 'FPKM',
]
vcf_expression_annotator.main(command)
self.assertTrue('--id-column is not set. This is required when using the `custom` format.' == str(context.exception))
def test_error_custom_format_expression_column_not_set(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'custom',
'gene',
'-i', 'tracking_id',
]
vcf_expression_annotator.main(command)
self.assertTrue('--expression-column is not set. This is required when using the `custom` format.' == str(context.exception))
def test_error_more_than_one_sample_without_sample_name(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'multiple_samples.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue('contains more than one sample. Please use the -s option to specify which sample to annotate.' in str(context.exception))
def test_error_more_than_one_sample_with_wrong_sample_name(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'multiple_samples.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
'-s', 'nonexistent_sample',
]
vcf_expression_annotator.main(command)
self.assertTrue('does not contain a sample column for sample nonexistent_sample.' in str(context.exception))
def test_error_no_csq(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'no_csq.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue('is not VEP-annotated. Please annotate the VCF with VEP before running this tool.' in str(context.exception))
def test_error_already_TX_annotated(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.tx.vcf'),
os.path.join(self.test_data_dir, 'isoforms.fpkm_tracking'),
'cufflinks',
'transcript',
]
vcf_expression_annotator.main(command)
self.assertTrue('is already transcript expression annotated. TX format header already exists.' in str(context.exception))
def test_error_already_GX_annotated(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.gx.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue('is already gene expression annotated. GX format header already exists.' in str(context.exception))
def test_error_id_column_nonexistent_in_file(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'custom',
'gene',
'-i', 'nonexistent_column',
'-e', 'FPKM',
]
vcf_expression_annotator.main(command)
self.assertTrue('ERROR: id_column header nonexistent_column does not exist in expression_file' in str(context.exception))
def test_error_expression_column_nonexistent_in_file(self):
with self.assertRaises(Exception) as context:
command = [
os.path.join(self.test_data_dir, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'custom',
'gene',
'-i', 'tracking_id',
'-e', 'nonexistent_column',
]
vcf_expression_annotator.main(command)
self.assertTrue('ERROR: expression_column header nonexistent_column does not exist in expression_file' in str(context.exception))
def test_warning_no_csq_for_variants(self):
logging.disable(logging.NOTSET)
with LogCapture() as l:
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.no_csq.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
temp_path.cleanup()
l.check_present(('root', 'WARNING', S("Variant is missing VEP annotation. INFO column doesn't contain CSQ field for variant")))
def test_skip_variant_without_gene_in_csq(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.ensr_transcript.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.ensr_transcript.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_warning_mutation_without_matching_expression_value(self):
logging.disable(logging.NOTSET)
with LogCapture() as l:
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'no_matching_expression.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
temp_path.cleanup()
l.check_present(('root', 'WARNING', "1 of 1 genes did not have an expression entry for their gene id."))
def test_multi_sample_vcf(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'multiple_samples.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
'-s', 'H_NJ-HCC1395-HCC1395',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'multiple_samples.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_multiple_transcripts(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'multiple_transcripts.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'multiple_transcripts.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_multiple_transcripts_with_one_missing_gene(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'multiple_transcripts_one_missing_gene.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'multiple_transcripts_one_missing_gene.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_cufflinks_format_gene_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.fpkm_tracking'),
'cufflinks',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.cufflinks.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_cufflinks_format_transcript_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'isoforms.fpkm_tracking'),
'cufflinks',
'transcript',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.cufflinks.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()
def test_kallisto_format_gene_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.genes'),
'kallisto',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.kallisto.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_kallisto_format_transcript_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcripts'),
'kallisto',
'transcript',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.kallisto.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()
def test_stringtie_format_gene_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'genes.tsv'),
'stringtie',
'gene',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.stringtie.gx.vcf'), os.path.join(temp_path.name, 'input.gx.vcf')))
temp_path.cleanup()
def test_stringtie_format_transcript_mode(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'transcripts.gtf'),
'stringtie',
'transcript',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.stringtie.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()
def test_kallisto_with_transcript_version_in_vcf(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.transcript_version.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcripts'),
'kallisto',
'transcript',
"--ignore-ensembl-id-version",
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.kallisto.with_version.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()
def test_kallisto_with_transcript_version_in_expression_file(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcript_version.transcripts'),
'kallisto',
'transcript',
"--ignore-ensembl-id-version",
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.kallisto.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()
def test_warning_kallisto_with_transcript_version_in_vcf(self):
logging.disable(logging.NOTSET)
with LogCapture() as l:
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.transcript_version.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcripts'),
'kallisto',
'transcript',
]
vcf_expression_annotator.main(command)
temp_path.cleanup()
l.check_present(('root', 'WARNING', "1 of 1 transcripts did not have an expression entry for their transcript id."))
def test_warning_kallisto_with_transcript_version_in_expression_file(self):
logging.disable(logging.NOTSET)
with LogCapture() as l:
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcript_version.transcripts'),
'kallisto',
'transcript',
]
vcf_expression_annotator.main(command)
temp_path.cleanup()
l.check_present(('root', 'WARNING', "1 of 1 transcripts did not have an expression entry for their transcript id."))
def test_skip_ENSR_transcript(self):
temp_path = tempfile.TemporaryDirectory()
os.symlink(os.path.join(self.test_data_dir, 'input.ensr_transcript.vcf'), os.path.join(temp_path.name, 'input.vcf'))
command = [
os.path.join(temp_path.name, 'input.vcf'),
os.path.join(self.test_data_dir, 'kallisto.transcripts'),
'kallisto',
'transcript',
]
vcf_expression_annotator.main(command)
self.assertTrue(cmp(os.path.join(self.test_data_dir, 'input.ensr_transcript.tx.vcf'), os.path.join(temp_path.name, 'input.tx.vcf')))
temp_path.cleanup()