This repository has been archived by the owner on Aug 5, 2019. It is now read-only.
/
test.py
executable file
·427 lines (279 loc) · 13.8 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
#!/usr/bin/env python
"""Contains unit tests for Grapple."""
import os.path
import unittest
from subprocess import CalledProcessError
from unittest import TestCase
import grapple
class TestError(TestCase):
"""Tests involving error()"""
def test_exit(self):
"""Should raise an exception to exit the script when called with any message"""
with self.assertRaises(SystemExit):
grapple.error('')
class TestBamToFq(TestCase):
"""Tests involving bam_to_fq()"""
def setUp(self):
"""Setup code for test cases"""
# Available test file
self._test_file = os.path.join('test_files', 'lambda_iontorrent.bam')
def test_valid_file(self):
"""Should not raise an exception when a valid bam file is used"""
try:
grapple.bam_to_fq(self._test_file)
except Exception as e:
self.fail(e)
def test_invalid_file(self):
"""Should raise an exception when the wrong type of file is used"""
with self.assertRaises(ValueError):
grapple.bam_to_fq(os.path.join('test_files', 'lambda_ref.fa'))
def test_absent_file(self):
"""Should raise an exception when the file does not exist"""
with self.assertRaises(CalledProcessError):
grapple.bam_to_fq('this_file_does_not_exist.bam')
def test_no_file(self):
"""Should raise an exception when no file is given"""
with self.assertRaises(ValueError):
grapple.bam_to_fq('')
def test_none_file(self):
"""Should raise an exception when None is given"""
with self.assertRaises(AttributeError):
grapple.bam_to_fq(None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is used as the prefix"""
with self.assertRaises(TypeError):
grapple.bam_to_fq(self._test_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.bam_to_fq(self._test_file, prefix_id=None)
class TestReadCorrection(TestCase):
"""Tests involving read_correction()"""
def setUp(self):
"""Setup code before tests"""
# Available test file
self._test_file = os.path.join('test_files', 'lambda_reads.fq')
def test_valid_file(self):
"""Should not raise an exception when a valid FASTQ file is used"""
try:
grapple.read_correction(self._test_file)
except Exception as e:
self.fail(e)
def test_invalid_file(self):
"""Should raise an exception when a file with the wrong format is used"""
with self.assertRaises(ValueError):
grapple.read_correction(os.path.join('test_files', 'lambda_ref.fa'))
def test_absent_file(self):
"""Should raise an exception when the file is not found"""
with self.assertRaises(ValueError):
grapple.read_correction('this_file_does_not_exit.fq')
def test_none_file(self):
"""Should raise an exception when None is passed as for the file"""
with self.assertRaises(AttributeError):
grapple.read_correction(None)
def test_invalid_cell_type(self):
"""Should raise an exception when an invalid cell type is given"""
with self.assertRaises(ValueError):
grapple.read_correction(self._test_file, cell_type='not_a_cell_type')
def test_none_cell_type(self):
"""Should raise an exception when None is passed for the cell type"""
with self.assertRaises(TypeError):
grapple.read_correction(self._test_file, cell_type=None)
def test_invalid_match_type(self):
"""Should raise an exception when an invalid match type is given"""
with self.assertRaises(ValueError):
grapple.read_correction(self._test_file, match_type='not_a_match_type')
def test_none_match_type(self):
"""Should raise an exception when None is given as a match type"""
with self.assertRaises(TypeError):
grapple.read_correction(self._test_file, match_type=None)
class TestAlignment(TestCase):
"""Unit tests for read_alignment()"""
def setUp(self):
"""Setup code for tests"""
# Available test file
self._test_file = os.path.join('test_files', 'lambda_reads.fq')
# Available reference file
self._ref_file = os.path.join('test_files', 'lambda_ref.fa')
def test_valid_files(self):
"""Should not raise an exception when valid files are given as input"""
try:
grapple.read_alignment(self._test_file, self._ref_file)
except Exception as e:
self.fail(e)
def test_invalid_read_file(self):
"""Should raise an exception when the read file is formatted wrong"""
with self.assertRaises(ValueError):
grapple.read_alignment(self._ref_file, self._ref_file)
def test_absent_read_file(self):
"""Should raise an exception when the read file is absent"""
with self.assertRaises(CalledProcessError):
grapple.read_alignment('this_file_does_not_exist.fq', self._ref_file)
def test_none_read_file(self):
"""Should raise an exception when None is passed as the read_file"""
with self.assertRaises(AttributeError):
grapple.read_alignment(None, self._ref_file)
def test_invalid_ref_file(self):
"""Should raise an exception when the reference file is in the wrong format"""
with self.assertRaises(ValueError):
grapple.read_alignment(self._test_file, self._test_file)
def test_absent_ref_file(self):
"""Should raise an exception when the reference file doesn't exist"""
with self.assertRaises(CalledProcessError):
grapple.read_alignment(self._test_file, 'this_file_does_not_exist.fa')
def test_none_ref_file(self):
"""Should raise an exception when None is given as the reference file"""
with self.assertRaises(AttributeError):
grapple.read_alignment(self._test_file, None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is given for the prefix"""
with self.assertRaises(TypeError):
grapple.read_alignment(self._test_file, self._ref_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.read_alignment(self._test_file, self._ref_file, prefix_id=None)
class TestSamToBam(TestCase):
"""Test cases for sam_to_bam() conversion"""
def setUp(self):
"""Setup code for test cases"""
# Available test files
self._test_file = os.path.join('test_files', 'aligned_lambda.sam')
def test_valid_file(self):
"""Should not raise an exception when a valid file is to be converted"""
try:
grapple.sam_to_bam(self._test_file)
except Exception as e:
self.fail(e)
def test_invalid_file(self):
"""Should raise an exception when a file with the wrong format is converted"""
with self.assertRaises(ValueError):
grapple.sam_to_bam(os.path.join('test_files', 'lambda_ref.fa'))
def test_absent_file(self):
"""Should raise an exception when the read file does not exist"""
with self.assertRaises(CalledProcessError):
grapple.sam_to_bam('this_file_does_not_exist.sam')
def test_none_file(self):
"""Should raise an exception when None is passed as the read file"""
with self.assertRaises(AttributeError):
grapple.sam_to_bam(None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is given for the prefix"""
with self.assertRaises(TypeError):
grapple.sam_to_bam(self._test_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.sam_to_bam(self._test_file, prefix_id=None)
class TestSortAndIndex(TestCase):
"""Test cases for sort_and_index()"""
def setUp(self):
"""Setup code for test cases"""
# Available test files
self._test_file = os.path.join('test_files', 'aligned_lambda.bam')
def test_valid_file(self):
"""Should not raise an exception when a valid file is provided"""
try:
grapple.sort_and_index(self._test_file)
except Exception as e:
self.fail(e)
def test_invalid_file(self):
"""Should raise an exception when a file with the wrong format is used"""
with self.assertRaises(ValueError):
grapple.sort_and_index(os.path.join('test_files', 'lambda_ref.fa'))
def test_absent_file(self):
"""Should raise an exception when a read file does not exist"""
with self.assertRaises(CalledProcessError):
grapple.sort_and_index('this_file_does_not_exist.bam')
def test_none_file(self):
"""Should raise an exception when None is passed as the read file"""
with self.assertRaises(AttributeError):
grapple.sort_and_index(None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is given for the prefix"""
with self.assertRaises(TypeError):
grapple.sort_and_index(self._test_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.sort_and_index(self._test_file, prefix_id=None)
class TestCallVariants(TestCase):
"""Test cases for call_variants()"""
def setUp(self):
"""Setup code for test cases"""
# Available test file
self._test_file = os.path.join('test_files', 'sorted_lambda.bam')
# Available reference file
self._ref_file = os.path.join('test_files', 'lambda_ref.fa')
def test_valid_files(self):
"""Should not raise an exception when supplying valid files"""
try:
grapple.call_variants(self._test_file, self._ref_file)
except Exception as e:
self.fail(e)
def test_invalid_read_file(self):
"""Should raise an exception when supplying a read file with the wrong format"""
with self.assertRaises(ValueError):
grapple.call_variants(self._ref_file, self._ref_file)
def test_absent_read_file(self):
"""Should raise an exception when supplying a read file that doesn't exist"""
with self.assertRaises(CalledProcessError):
grapple.call_variants('this_file_does_not_exist.bam', self._ref_file)
def test_none_read_file(self):
"""Should raise an exception when None is used as the read file"""
with self.assertRaises(AttributeError):
grapple.call_variants(None, self._ref_file)
def test_invalid_ref_file(self):
"""Should raise an exception when the reference file is not in the right format"""
with self.assertRaises(ValueError):
grapple.call_variants(self._test_file, os.path.join('test_files', 'lambda_reads.fq'))
def test_absent_ref_file(self):
"""Should raise an exception when the reference file doesn't exist"""
with self.assertRaises(CalledProcessError):
grapple.call_variants(self._test_file, 'this_file_does_not_exist.fa')
def test_none_ref_file(self):
"""Should raise an exception when None is passed as the reference file"""
with self.assertRaises(AttributeError):
grapple.call_variants(self._test_file, None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is given for the prefix"""
with self.assertRaises(TypeError):
grapple.call_variants(self._test_file, self._ref_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.call_variants(self._test_file, self._ref_file, prefix_id=None)
class TestFormatConsensus(TestCase):
"""Test cases for format_consensus()"""
def setUp(self):
"""Setup for test cases"""
# Available test file
self._test_file = os.path.join('test_files', 'lambda_consensus.fa')
def test_valid_file(self):
"""Should not raise an exception when supplying valid files"""
try:
grapple.format_consensus(self._test_file)
except Exception as e:
self.fail(e)
def test_invalid_file(self):
"""Should raise an exception when the file is not in FASTA format"""
with self.assertRaises(ValueError):
grapple.format_consensus(os.path.join('test_files', 'lambda_reads.fq'))
def test_absent_file(self):
"""Should raise an exception when the file does not exist"""
with self.assertRaises(IOError):
grapple.format_consensus('this_file_does_not_exist.fa')
def test_none_file(self):
"""Should raise an exception when None is passed as a file"""
with self.assertRaises(AttributeError):
grapple.format_consensus(None)
def test_bad_prefix(self):
"""Should raise an exception when a type that cannot be converted to a string is given for the prefix"""
with self.assertRaises(TypeError):
grapple.format_consensus(self._test_file, prefix_id=0)
def test_none_prefix(self):
"""Should raise an exception when None is used as the prefix"""
with self.assertRaises(TypeError):
grapple.format_consensus(self._test_file, prefix_id=None)
if __name__ == '__main__':
unittest.main()