Debian Codegraph @

pyfastaq/tests/tasks_test.py

raw
1 #!/usr/bin/env python3
2
3 import sys
4 import filecmp
5 import os
6 import unittest
7 from pyfastaq import tasks, sequences
8
9 modules_dir = os.path.dirname(os.path.abspath(sequences.__file__))
10 data_dir = os.path.join(modules_dir, 'tests', 'data')
11
12 class Error (Exception): pass
13
14
15 class TestACGTN_only(unittest.TestCase):
16 def test_acgtn_only(self):
17 '''Test acgtn_only'''
18 tmpfile = 'tmp.test_acgtn_only.fa'
19 infile = os.path.join(data_dir, 'test_acgtn_only.in.fa')
20 expected = os.path.join(data_dir, 'test_acgtn_only.expected.fa')
21 tasks.acgtn_only(infile, tmpfile)
22 self.assertTrue(filecmp.cmp(expected, tmpfile, shallow=False))
23 os.unlink(tmpfile)
24
25
26 class TestCafToFastq(unittest.TestCase):
27 def test_caf_to_fastq_default(self):
28 '''Test caf_to_fastq with no filtering'''
29 tmpfile = 'tmp.fq'
30 tasks.caf_to_fastq(os.path.join(data_dir, 'caf_test.caf'), tmpfile)
31 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'caf_test.to_fastq.no_trim.min_length_0.fq'), tmpfile, shallow=False))
32 os.unlink(tmpfile)
33
34 def test_caf_to_fastq_trim_and_min_length(self):
35 '''Test caf_to_fastq with trimming and min_length'''
36 tmpfile = 'tmp.fq'
37 tasks.caf_to_fastq(os.path.join(data_dir, 'caf_test.caf'), tmpfile, trim=True, min_length=6)
38 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'caf_test.to_fastq.trim.min_length_6.fq'), tmpfile, shallow=False))
39 os.unlink(tmpfile)
40
41
42 class TestCapillaryToPairs(unittest.TestCase):
43 def test_capillary_to_pairs(self):
44 '''Check that capillary reads file converted to paired and unpaired'''
45 tmp_prefix = 'tmp.cap_to_pairs'
46 tasks.capillary_to_pairs(os.path.join(data_dir, 'sequences_test_cap_to_read_pairs.fa'), tmp_prefix)
47 # sequences have been hashed, so could be in any order in
48 # output files. So need to check contents of files are OK
49 d_correct_paired = {}
50 d_correct_unpaired = {}
51 tasks.file_to_dict(os.path.join(data_dir, 'sequences_test_cap_to_read_pairs.fa.paired.gz'), d_correct_paired)
52 tasks.file_to_dict(os.path.join(data_dir, 'sequences_test_cap_to_read_pairs.fa.unpaired.gz'), d_correct_unpaired)
53 d_test_paired = {}
54 d_test_unpaired = {}
55 tasks.file_to_dict(tmp_prefix + '.paired.gz', d_test_paired)
56 tasks.file_to_dict(tmp_prefix + '.unpaired.gz', d_test_unpaired)
57 self.assertDictEqual(d_test_paired, d_correct_paired)
58 self.assertDictEqual(d_test_unpaired, d_correct_unpaired)
59 os.unlink(tmp_prefix + '.paired.gz')
60 os.unlink(tmp_prefix + '.unpaired.gz')
61
62
63 class TestDeinterleave(unittest.TestCase):
64 def test_deinterleave(self):
65 '''deinterleave should deal with an interleaved file correctly'''
66 tmp_1 = 'tmp.deinterleaved_1.fa'
67 tmp_2 = 'tmp.deinterleaved_2.fa'
68 tasks.deinterleave(os.path.join(data_dir, 'sequences_test_interleaved.fa'), tmp_1, tmp_2)
69 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_deinterleaved_1.fa'), tmp_1))
70 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_deinterleaved_2.fa'), tmp_2))
71
72 tasks.deinterleave(os.path.join(data_dir, 'sequences_test_interleaved.fq'), tmp_1, tmp_2, fasta_out=True)
73 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_deinterleaved_1.fa'), tmp_1))
74 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_deinterleaved_2.fa'), tmp_2))
75
76 with self.assertRaises(tasks.Error):
77 tasks.deinterleave(os.path.join(data_dir, 'sequences_test_interleaved_bad.fa'), tmp_1, tmp_2)
78 os.unlink(tmp_1)
79 os.unlink(tmp_2)
80
81
82 class TestEnumerateNames(unittest.TestCase):
83 def test_enumerate_names(self):
84 '''Test enomereate_names works with all options'''
85 outfile = 'tmp.enumerate_seqs.fa'
86 rename_out = outfile + '.rename'
87 tasks.enumerate_names(os.path.join(data_dir, 'sequences_test_enumerate_names.fa'), outfile)
88 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.start.1'), outfile))
89 tasks.enumerate_names(os.path.join(data_dir, 'sequences_test_enumerate_names.fa'), outfile, rename_file=rename_out)
90 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.start.1'), outfile))
91 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.start.1.rename_file'), rename_out))
92 tasks.enumerate_names(os.path.join(data_dir, 'sequences_test_enumerate_names.fa'), outfile, start_index=2)
93 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.start.2'), outfile))
94 tasks.enumerate_names(os.path.join(data_dir, 'sequences_test_enumerate_names.fa'), outfile, keep_illumina_suffix=True)
95 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.keep_suffix'), outfile))
96
97 tasks.enumerate_names(os.path.join(data_dir, 'sequences_test_enumerate_names.fa'), outfile, suffix='.SUFFIX')
98 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_enumerate_names.fa.out.add_suffix'), outfile, shallow=False))
99 os.unlink(outfile)
100 os.unlink(rename_out)
101
102
103 class TestExpandNucleotides(unittest.TestCase):
104 def test_expand_nucleoties(self):
105 '''Test expand_nucleotides'''
106 tmp = 'tmp.expanded'
107 fq_in = os.path.join(data_dir, 'tasks_test_expend_nucleotides.in.fq')
108 fa_in = os.path.join(data_dir, 'tasks_test_expend_nucleotides.in.fa')
109 fq_expected = os.path.join(data_dir, 'tasks_test_expend_nucleotides.out.fq')
110 fa_expected = os.path.join(data_dir, 'tasks_test_expend_nucleotides.out.fa')
111 tasks.expand_nucleotides(fq_in, tmp)
112 self.assertTrue(filecmp.cmp(fq_expected, tmp, shallow=False))
113 os.unlink(tmp)
114 tasks.expand_nucleotides(fa_in, tmp)
115 self.assertTrue(filecmp.cmp(fa_expected, tmp, shallow=False))
116 os.unlink(tmp)
117
118
119 class TestExtendGaps(unittest.TestCase):
120 def test_trim_contigs(self):
121 '''Test that gap extension works'''
122 outfile = 'tmp.gap_extend.fa'
123 tasks.trim_contigs(os.path.join(data_dir, 'sequences_test_trim_contigs.fa'), outfile, trim=2)
124 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_trim_contigs.fa.out'), outfile))
125 os.unlink(outfile)
126
127
128 class TestFastqToMiraXml(unittest.TestCase):
129 def test_fastaq_to_mira_xml(self):
130 '''check that fastaq_to_mira_xml makes the correct xml file from a fastq file'''
131 tmp = 'tmp.mira.xml'
132 tasks.fastaq_to_mira_xml(os.path.join(data_dir, 'sequences_test_good_file.fq'), tmp)
133 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_good_file_mira.xml'), tmp))
134 os.unlink(tmp)
135
136
137 class TestFastaqToOrfsGFF(unittest.TestCase):
138 def test_fastaq_to_orfs_gff(self):
139 '''Test fastaq_to_orfs_gff'''
140 outfile = 'tmp.orfs.gff'
141 tasks.fastaq_to_orfs_gff(os.path.join(data_dir, 'sequences_test_orfs.fa'), outfile, min_length=120)
142 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_orfs.gff'), outfile, shallow=False))
143 os.unlink(outfile)
144
145
146 class TestFilter(unittest.TestCase):
147 def test_length_filter(self):
148 '''Check that filtering by length works as expected'''
149 infile = os.path.join(data_dir, 'sequences_test_length_filter.fa')
150 correct_files = [os.path.join(data_dir, 'sequences_test_length_filter.min-0.max-1.fa'),
151 os.path.join(data_dir, 'sequences_test_length_filter.min-0.max-inf.fa'),
152 os.path.join(data_dir, 'sequences_test_length_filter.min-4.max-4.fa')]
153 cutoffs = [(0, 1), (0, float('inf')), (4, 4)]
154
155 for i in range(len(cutoffs)):
156 outfile = 'tmp.length_filter.fa'
157 tasks.filter(infile, outfile, minlength=cutoffs[i][0], maxlength=cutoffs[i][1])
158 self.assertTrue(filecmp.cmp(correct_files[i], outfile))
159 os.unlink(outfile)
160
161 def test_regex_filter(self):
162 '''Check that filtering by name regex works as expected'''
163 infile = os.path.join(data_dir, 'sequences_test_filter_by_regex.fa')
164 correct_files = [os.path.join(data_dir, 'sequences_test_filter_by_regex.numeric.fa'),
165 os.path.join(data_dir, 'sequences_test_filter_by_regex.first-of-pair.fa'),
166 os.path.join(data_dir, 'sequences_test_filter_by_regex.first-char-a.fa')]
167 regexes = ['^[0-9]+$', '/1$', '^a']
168
169 for i in range(len(regexes)):
170 outfile = 'tmp.regex_filter.fa'
171 tasks.filter(infile, outfile, regex=regexes[i])
172 self.assertTrue(filecmp.cmp(correct_files[i], outfile))
173 os.unlink(outfile)
174
175 def test_ids_from_file_filter(self):
176 '''Test that can extract reads from a file of read names'''
177 infile = os.path.join(data_dir, 'sequences_test_filter_by_ids_file.fa')
178 outfile = 'tmp.ids_file_filter.fa'
179 tasks.filter(infile, outfile, ids_file=infile + '.ids')
180 self.assertTrue(filecmp.cmp(infile + '.filtered', outfile))
181 os.unlink(outfile)
182
183 def test_ids_with_comments_from_file_filter(self):
184 '''Test that can extract reads from a file of read names where the read names have extra data after space'''
185 infile = os.path.join(data_dir, 'readnames_with_comments.fastq')
186 outfile = 'tmp.ids_file_filter.fastq'
187 tasks.filter(infile, outfile, ids_file=infile + '.ids')
188 self.assertTrue(filecmp.cmp(infile + '.filtered', outfile))
189 os.unlink(outfile)
190
191 def test_invert_filter(self):
192 '''Test that inverting filtering works'''
193 infile = os.path.join(data_dir, 'sequences_test_filter_by_ids_file.fa')
194 outfile = 'tmp.ids_file_filter.fa'
195 tasks.filter(infile, outfile, ids_file=infile + '.ids', invert=True)
196 self.assertTrue(filecmp.cmp(infile + '.filtered.invert', outfile))
197 os.unlink(outfile)
198
199
200 def test_paired_both_pass(self):
201 '''Test filter with paired file both pass'''
202 infile1 = os.path.join(data_dir, 'tasks_test_filter_paired_both_pass.in_1.fa')
203 infile2 = os.path.join(data_dir, 'tasks_test_filter_paired_both_pass.in_2.fa')
204 outfile1 = 'tmp.filter_both_pass_1.fa'
205 outfile2 = 'tmp.filter_both_pass_2.fa'
206 expected1 = os.path.join(data_dir, 'tasks_test_filter_paired_both_pass.out_1.fa')
207 expected2 = os.path.join(data_dir, 'tasks_test_filter_paired_both_pass.out_2.fa')
208 tasks.filter(infile1, outfile1, mate_in=infile2, mate_out=outfile2, minlength=3)
209 self.assertTrue(filecmp.cmp(outfile1, expected1, shallow=False))
210 self.assertTrue(filecmp.cmp(outfile2, expected2, shallow=False))
211 os.unlink(outfile1)
212 os.unlink(outfile2)
213
214
215 def test_paired_one_pass(self):
216 '''Test filter with paired file one pass'''
217 infile1 = os.path.join(data_dir, 'tasks_test_filter_paired_one_pass.in_1.fa')
218 infile2 = os.path.join(data_dir, 'tasks_test_filter_paired_one_pass.in_2.fa')
219 outfile1 = 'tmp.filter_one_pass_1.fa'
220 outfile2 = 'tmp.filter_one_pass_2.fa'
221 expected1 = os.path.join(data_dir, 'tasks_test_filter_paired_one_pass.out_1.fa')
222 expected2 = os.path.join(data_dir, 'tasks_test_filter_paired_one_pass.out_2.fa')
223 tasks.filter(infile1, outfile1, mate_in=infile2, mate_out=outfile2, both_mates_pass=False, minlength=3)
224 self.assertTrue(filecmp.cmp(outfile1, expected1, shallow=False))
225 self.assertTrue(filecmp.cmp(outfile2, expected2, shallow=False))
226 os.unlink(outfile1)
227 os.unlink(outfile2)
228
229
230 class TestGetSeqsFlankingGaps(unittest.TestCase):
231 def test_get_seqs_flanking_gaps(self):
232 outfile = 'tmp.seqs_flanking_gaps'
233 tasks.get_seqs_flanking_gaps(os.path.join(data_dir, 'sequences_test_get_seqs_flanking_gaps.fa'), outfile, 3, 3)
234 self.assertTrue(filecmp.cmp(outfile, os.path.join(data_dir, 'sequences_test_get_seqs_flanking_gaps.fa.out')))
235 os.unlink(outfile)
236
237
238 class TestInterleave(unittest.TestCase):
239 def test_interleave(self):
240 '''Check that interleave works as expected'''
241 tmp = 'tmp.interleaved.fa'
242 tasks.interleave(os.path.join(data_dir, 'sequences_test_deinterleaved_1.fa'),
243 os.path.join(data_dir, 'sequences_test_deinterleaved_2.fa'),
244 tmp)
245 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_interleaved.fa'), tmp))
246
247 tasks.interleave(os.path.join(data_dir, 'sequences_test_deinterleaved_no_suffixes_1.fa'),
248 os.path.join(data_dir, 'sequences_test_deinterleaved_no_suffixes_2.fa'),
249 tmp, suffix1='/1', suffix2='/2')
250 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_interleaved_with_suffixes.fa'), tmp))
251
252 with self.assertRaises(tasks.Error):
253 tasks.interleave(os.path.join(data_dir, 'sequences_test_deinterleaved_bad_1.fa'),
254 os.path.join(data_dir, 'sequences_test_deinterleaved_bad_2.fa'),
255 tmp)
256
257 with self.assertRaises(tasks.Error):
258 tasks.interleave(os.path.join(data_dir, 'sequences_test_deinterleaved_bad2_1.fa'),
259 os.path.join(data_dir, 'sequences_test_deinterleaved_bad2_2.fa'),
260 tmp)
261 os.unlink(tmp)
262
263
264 class TestMakeRandomContigs(unittest.TestCase):
265 def test_make_random_contigs(self):
266 '''Test make_random_contigs()'''
267 # Can't guarantee same results from random (even using same seed), so
268 # just check sequence names and lengths
269 def files_are_equal(file1, file2):
270 seqs1 = {}
271 seqs2 = {}
272 tasks.file_to_dict(file1, seqs1)
273 tasks.file_to_dict(file2, seqs2)
274 if len(seqs1) != len(seqs2):
275 return False
276
277 for name in seqs1:
278 seq1 = seqs1[name]
279 seq2 = seqs2[name]
280 if seq1.id != seq2.id:
281 return False
282 if len(seq1) != len(seq2):
283 return False
284
285 return True
286
287 tmp = 'tmp.random_contigs.fa'
288 tasks.make_random_contigs(2, 3, tmp)
289 self.assertTrue(files_are_equal(os.path.join(data_dir, 'sequences_test_make_random_contigs.default.fa'), tmp))
290 tasks.make_random_contigs(2, 3, tmp, prefix='p')
291 self.assertTrue(files_are_equal(os.path.join(data_dir, 'sequences_test_make_random_contigs.prefix-p.fa'), tmp))
292 tasks.make_random_contigs(2, 3, tmp, first_number=42)
293 self.assertTrue(files_are_equal(os.path.join(data_dir, 'sequences_test_make_random_contigs.first-42.fa'), tmp))
294 tasks.make_random_contigs(28, 3, tmp, name_by_letters=True)
295 self.assertTrue(files_are_equal(os.path.join(data_dir, 'sequences_test_make_random_contigs.name-by-letters.fa'), tmp))
296 os.unlink(tmp)
297
298
299 class TestMeanLength(unittest.TestCase):
300 def test_mean_length(self):
301 '''Test mean_length'''
302 expected = [3, 2, 3, 4, 4]
303 limits = [1, 2, 3, 4, None]
304 assert len(expected) == len(limits)
305 for i in range(len(expected)):
306 mean = tasks.mean_length(os.path.join(data_dir, 'tasks_test_mean_length.fa'), limit=limits[i])
307 self.assertEqual(expected[i], mean)
308
309
310 class TestMergeToOneSeq(unittest.TestCase):
311 def test_merge_to_one_seq_fa(self):
312 '''Test merge_to_one_seq with fasta'''
313 tmp = 'tmp.merged.fa'
314 tasks.merge_to_one_seq(os.path.join(data_dir, 'sequences_test_merge_to_one_seq.fa'), tmp)
315 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_merge_to_one_seq.merged.fa'), tmp, shallow=False))
316 os.unlink(tmp)
317
318 def test_merge_to_one_seq_fq(self):
319 '''Test merge_to_one_seq with fastq'''
320 tmp = 'tmp.merged.fq'
321 tasks.merge_to_one_seq(os.path.join(data_dir, 'sequences_test_merge_to_one_seq.fq'), tmp)
322 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_merge_to_one_seq.merged.fq'), tmp, shallow=False))
323 os.unlink(tmp)
324
325 class TestReverseComplement(unittest.TestCase):
326 def test_reverse_complement(self):
327 '''reverse_complement should correctly reverse complement each seq in a file'''
328 tmp = 'tmp.revcomp.fa'
329 tasks.reverse_complement(os.path.join(data_dir, 'sequences_test.fa'), tmp)
330 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_revcomp.fa'), tmp))
331 os.unlink(tmp)
332
333
334 class TestScaffoldsToContigs(unittest.TestCase):
335 def test_scaffolds_to_contigs(self):
336 '''Test scaffolds_to_contigs'''
337 tmp = 'tmp.contigs.fa'
338 tasks.scaffolds_to_contigs(os.path.join(data_dir, 'utils_test_scaffolds.fa'), tmp)
339 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'utils_test_scaffolds.fa.to_contigs.fa'), tmp))
340 os.unlink(tmp)
341
342 def test_scaffolds_to_contigs_number_contigs(self):
343 '''Test scaffolds_to_contigs with contig numbering'''
344 tmp = 'tmp.contigs.fa'
345 tasks.scaffolds_to_contigs(os.path.join(data_dir, 'utils_test_scaffolds.fa'), tmp, number_contigs=True)
346 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'utils_test_scaffolds.fa.to_contigs.number_contigs.fa'), tmp))
347 os.unlink(tmp)
348
349
350 class TestSearchForSeq(unittest.TestCase):
351 def test_search_for_seq(self):
352 '''Test that sequence search finds all hits'''
353 tmp = 'tmp.search.fa'
354 tasks.search_for_seq(os.path.join(data_dir, 'sequences_test_search_string.fa'), tmp, 'AGA')
355 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_search_string.fa.hits'), tmp))
356 os.unlink(tmp)
357
358
359 class TestSequenceTrim(unittest.TestCase):
360 def test_sequence_trim(self):
361 '''Test sequence_trim'''
362 tmp1 = 'tmp.trimmed_1.fa'
363 tmp2 = 'tmp.trimmed_2.fa'
364 in1 = os.path.join(data_dir, 'tasks_test_sequence_trim_1.fa')
365 in2 = os.path.join(data_dir, 'tasks_test_sequence_trim_2.fa')
366 to_trim = os.path.join(data_dir, 'tasks_test_sequences_to_trim.fa')
367 expected1 = os.path.join(data_dir, 'tasks_test_sequence_trim_1.trimmed.fa')
368 expected2 = os.path.join(data_dir, 'tasks_test_sequence_trim_2.trimmed.fa')
369 tasks.sequence_trim(in1, in2, tmp1, tmp2, to_trim, min_length=10, check_revcomp=True)
370 self.assertTrue(filecmp.cmp(expected1, tmp1))
371 self.assertTrue(filecmp.cmp(expected2, tmp2))
372 os.unlink(tmp1)
373 os.unlink(tmp2)
374
375
376 class ToFastg(unittest.TestCase):
377 def test_to_fastg_ids_set(self):
378 '''Test to_fastg when ids are a set'''
379 infile = os.path.join(data_dir, 'tasks_test_to_fastg.fasta')
380 tmpfile = 'tmp.to_fastg.fastg'
381 expected = os.path.join(data_dir, 'tasks_test_to_fastg.fastg')
382 ids = {'seq2'}
383 tasks.to_fastg(infile, tmpfile, circular=ids)
384 self.assertTrue(filecmp.cmp(expected, tmpfile, shallow=False))
385 os.unlink(tmpfile)
386
387
388 def test_to_fastg_ids_file(self):
389 '''Test to_fastg when ids in a file'''
390 infile = os.path.join(data_dir, 'tasks_test_to_fastg.fasta')
391 tmpfile = 'tmp.to_fastg.fastg'
392 expected = os.path.join(data_dir, 'tasks_test_to_fastg.fastg')
393 ids_file = os.path.join(data_dir, 'tasks_test_to_fastg.ids_to_circularise')
394 tasks.to_fastg(infile, tmpfile, circular=ids_file)
395 self.assertTrue(filecmp.cmp(expected, tmpfile, shallow=False))
396 os.unlink(tmpfile)
397
398
399 class TestTranslate(unittest.TestCase):
400 def test_translate(self):
401 '''Test translate works in each frame'''
402 tmp = 'tmp.translated.fa'
403 for i in range(3):
404 tasks.translate(os.path.join(data_dir, 'sequences_test_translate.fa'), tmp, frame=i)
405 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_translate.fa.frame' + str(i)), tmp))
406
407 os.unlink(tmp)
408
409
410 class TestTrim(unittest.TestCase):
411 def test_trim(self):
412 '''trim should correctly trim each seq in a file'''
413 tmp = 'tmp.trim.fq'
414 tasks.trim(os.path.join(data_dir, 'sequences_test_untrimmed.fq'), tmp, 2, 1)
415 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_trimmed.fq'), tmp))
416 os.unlink(tmp)
417
418
419 def test_trim_Ns_at_end(self):
420 '''Test Ns at ends of sequences trimmed OK'''
421 tmp = 'tmp.trim.fa'
422 tasks.trim_Ns_at_end(os.path.join(data_dir, 'sequences_test_trim_Ns_at_end.fa'), tmp)
423 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_trim_Ns_at_end.fa.trimmed'), tmp))
424 os.unlink(tmp)
425
426
427 class TestFileToDict(unittest.TestCase):
428 def test_file_to_dict(self):
429 '''check file_to_dict fills dictionary correctly'''
430 d_test = {}
431 d = {}
432 tasks.file_to_dict(os.path.join(data_dir, 'sequences_test.fa'), d_test)
433 for i in range(1,5):
434 d[str(i)] = sequences.Fasta(str(i),'ACGTA')
435
436 self.assertSequenceEqual(d_test.keys(),d.keys())
437 for i in range(1,5):
438 key = str(i)
439 self.assertEqual(d_test[key].id, d[key].id)
440 self.assertEqual(d_test[key].seq, d[key].seq)
441
442
443 class TestLengthsFromFai(unittest.TestCase):
444 def test_lengths_from_fai(self):
445 '''Check lengths_from_fai gets the length of each seq OK'''
446 d = {}
447 lengths = {str(x):x for x in range(1,5)}
448 tasks.lengths_from_fai(os.path.join(data_dir, 'sequences_test_fai_test.fa.fai'), d)
449 self.assertSequenceEqual(d.keys(), lengths.keys())
450 for i in d:
451 self.assertEqual(int(i), d[i])
452
453
454 class TestLengthOffsetsFromFai(unittest.TestCase):
455 def test_length_offsets_from_fai(self):
456 '''Test length_offsets_from_fai'''
457 got = tasks.length_offsets_from_fai(os.path.join(data_dir, 'tasks_test_length_offsets_from_fai.fa.fai'))
458 expected = {'seq1': 0, 'seq2': 42, 'seq3': 43}
459 self.assertEqual(expected, got)
460
461
462 class TestSplit(unittest.TestCase):
463 def test_split_by_base_count(self):
464 '''Check that fasta/q files get split by base count correctly'''
465 infile = os.path.join(data_dir, 'sequences_test_split_test.fa')
466 outprefix = 'tmp.sequences_test_split_test.fa.test'
467 length2files = {2: ['1','2','3','4'],
468 3: ['1','2','3'],
469 4: ['1', '2', '3'],
470 6: ['1', '2']}
471 for l in length2files:
472 tasks.split_by_base_count(infile, outprefix, l)
473 for x in range(len(length2files[l])):
474 file_index = str(length2files[l][x])
475 fname = outprefix + '.' + file_index
476 self.assertTrue(filecmp.cmp(fname, infile + '.' + str(l) + '.' + file_index))
477 os.unlink(fname)
478
479 # check that limiting the number of files works
480 tasks.split_by_base_count(infile, outprefix, 6, 2)
481 for i in range(1,4):
482 test_file = outprefix + '.' + str(i)
483 self.assertTrue(filecmp.cmp(test_file, os.path.join(data_dir, 'sequences_test_split_test.fa.6.limit2.') + str(i)))
484 os.unlink(test_file)
485
486 # check big sequence not broken
487 tasks.split_by_base_count(os.path.join(data_dir, 'sequences_test_split_test.long.fa'), outprefix, 2)
488 self.assertTrue(filecmp.cmp(outprefix + '.1', os.path.join(data_dir, 'sequences_test_split_test.long.fa.2.1')))
489 self.assertTrue(filecmp.cmp(outprefix + '.2', os.path.join(data_dir, 'sequences_test_split_test.long.fa.2.2')))
490 os.unlink(outprefix + '.1')
491 os.unlink(outprefix + '.2')
492
493 def test_split_by_fixed_size(self):
494 '''Test fasta/q file split by fixed size'''
495 infile = os.path.join(data_dir, 'sequences_test_split_fixed_size.fa')
496 outprefix = 'tmp.sequences_test_split'
497 tasks.split_by_fixed_size(infile, outprefix, 4, 1)
498
499 for i in range(1,7,1):
500 correct = os.path.join(data_dir, 'sequences_test_split_fixed_size.fa.split.' + str(i))
501 test = outprefix + '.' + str(i)
502 self.assertTrue(filecmp.cmp(test, correct))
503 os.unlink(test)
504
505 test_coords = outprefix + '.coords'
506 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_split_fixed_size.fa.split.coords'), test_coords))
507 os.unlink(test_coords)
508
509 def test_split_by_fixed_size_exclude_Ns(self):
510 infile = os.path.join(data_dir, 'sequences_test_split_fixed_size.fa')
511 outprefix = 'tmp.sequences_test_split'
512 tasks.split_by_fixed_size(infile, outprefix, 4, 1, skip_if_all_Ns=True)
513
514 for i in range(1,5,1):
515 correct = os.path.join(data_dir, 'sequences_test_split_fixed_size.fa.split.skip_if_all_Ns.' + str(i))
516 test = outprefix + '.' + str(i)
517 self.assertTrue(filecmp.cmp(test, correct))
518 os.unlink(test)
519
520 test_coords = outprefix + '.coords'
521 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_split_fixed_size.fa.split.skip_if_all_Ns.coords'), test_coords))
522 os.unlink(test_coords)
523
524 def test_split_by_fixed_size_onefile(self):
525 infile = os.path.join(data_dir, 'sequences_test_split_fixed_size_onefile.fa')
526 tmp_out = 'tmp.sequences_test_split_fixed_size_onefile.fa'
527 expected = os.path.join(data_dir, 'sequences_test_split_fixed_size_onefile.out.fa')
528 tasks.split_by_fixed_size_onefile(infile, tmp_out, chunk_size=3, tolerance=1)
529 self.assertTrue(filecmp.cmp(expected, tmp_out))
530 os.unlink(tmp_out)
531
532 def test_split_by_fixed_size_onefile_exclude_Ns(self):
533 infile = os.path.join(data_dir, 'sequences_test_split_fixed_size_onefile.fa')
534 tmp_out = 'tmp.sequences_test_split_fixed_size_onefile.skip_Ns.fa'
535 expected = os.path.join(data_dir, 'sequences_test_split_fixed_size_onefile.skip_Ns.out.fa')
536 tasks.split_by_fixed_size_onefile(infile, tmp_out, chunk_size=3, tolerance=1, skip_if_all_Ns=True)
537 self.assertTrue(filecmp.cmp(expected, tmp_out))
538 os.unlink(tmp_out)
539
540 class TestCountSequences(unittest.TestCase):
541 def test_count_sequences(self):
542 '''Check that count_sequences does as expected'''
543 self.assertEqual(2, tasks.count_sequences(os.path.join(data_dir, 'sequences_test_good_file.fq')))
544 self.assertEqual(4, tasks.count_sequences(os.path.join(data_dir, 'sequences_test.fa')))
545 self.assertEqual(0, tasks.count_sequences(os.path.join(data_dir, 'sequences_test_empty_file')))
546
547 class TestGetIds(unittest.TestCase):
548 def test_get_ids(self):
549 '''Check that IDs extracted correctly from fasta/q file'''
550 tmpfile = 'tmp.ids'
551 tasks.get_ids(os.path.join(data_dir, 'sequences_test.fa'), tmpfile)
552 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test.fa.ids'), tmpfile))
553 os.unlink(tmpfile)
554
555
556 class TestFastaToFakeQual(unittest.TestCase):
557 def test_fasta_to_fake_qual(self):
558 '''Test fasta_to_fake_qual'''
559 tmpfile = 'tmp.qual'
560 infile = os.path.join(data_dir, 'tasks_test_fasta_to_fake_qual.in.fa')
561 tasks.fastaq_to_fake_qual(infile, tmpfile)
562 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_fasta_to_fake_qual.out.default.qual'), tmpfile, shallow=False))
563 os.unlink(tmpfile)
564 tasks.fastaq_to_fake_qual(infile, tmpfile, q=42)
565 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_fasta_to_fake_qual.out.q42.qual'), tmpfile, shallow=False))
566 os.unlink(tmpfile)
567
568
569 class TestFastaToFastq(unittest.TestCase):
570 def test_fasta_to_fastq(self):
571 '''Check fasta_to_fastq converts files as expected'''
572 tasks.fasta_to_fastq(os.path.join(data_dir, 'sequences_test.fa'),
573 os.path.join(data_dir, 'sequences_test.fa.qual'),
574 'tmp.fq')
575 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test.fasta_to_fastq.fq'), 'tmp.fq'))
576
577 with self.assertRaises(tasks.Error):
578 tasks.fasta_to_fastq(os.path.join(data_dir, 'sequences_test.fa'),
579 os.path.join(data_dir, 'sequences_test.fa.qual.bad'),
580 'tmp.fq')
581
582 os.unlink('tmp.fq')
583
584
585 class TestReplaceBases(unittest.TestCase):
586 def test_sequences_replace_bases(self):
587 '''Check that fasta file gets all bases replaced OK'''
588 tmpfile = 'tmp.replace_bases.fa'
589 tasks.replace_bases(os.path.join(data_dir, 'sequences_test_fastaq_replace_bases.fa'), tmpfile, 'T', 'X')
590 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_fastaq_replace_bases.expected.fa'), tmpfile))
591 os.unlink(tmpfile)
592
593
594
595 class TestSortBySize(unittest.TestCase):
596 def test_sort_by_size(self):
597 '''Test sort_by_size'''
598 infile = os.path.join(data_dir, 'tasks_test_sort_by_size.in.fa')
599 tmpfile = 'tmp.sorted.fa'
600 tasks.sort_by_size(infile, tmpfile)
601 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_sort_by_size.out.fa'), tmpfile, shallow=False))
602 tasks.sort_by_size(infile, tmpfile, smallest_first=True)
603 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_sort_by_size.out.rev.fa'), tmpfile, shallow=False))
604 os.unlink(tmpfile)
605
606 class TestSortByName(unittest.TestCase):
607 def test_sort_by_name(self):
608 '''Test sort_by_name'''
609 infile = os.path.join(data_dir, 'tasks_test_sort_by_name.in.fa')
610 tmpfile = 'tmp.sort_by_name.fa'
611 tasks.sort_by_name(infile, tmpfile)
612 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_sort_by_name.out.fa'), tmpfile, shallow=False))
613 os.unlink(tmpfile)
614
615
616 class TestStripIlluminaSuffix(unittest.TestCase):
617 def test_strip_illumina_suffix(self):
618 '''Check illumina suffixes stripped correctly off read names'''
619 tmpfile = 'tmp.stripped.fa'
620 tasks.strip_illumina_suffix(os.path.join(data_dir, 'sequences_test_strip_illumina_suffix.fq'), tmpfile)
621 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_strip_illumina_suffix.fq.stripped'), tmpfile))
622 os.unlink(tmpfile)
623
624
625 class TestStatsFromFai(unittest.TestCase):
626 def test_stats_from_fai_nonempty(self):
627 '''Test task stats_from_fai non-empty file'''
628 infile = os.path.join(data_dir, 'tasks_test_stats_from_fai.in.fai')
629 got = tasks.stats_from_fai(infile)
630 expected = {
631 'longest': 10,
632 'shortest': 1,
633 'N50': 4,
634 'mean': 4.2,
635 'number': 5,
636 'total_length': 21
637 }
638 self.assertEqual(expected, got)
639
640
641 def test_stats_from_fai_empty(self):
642 '''Test task stats_from_fai empty file'''
643 infile = os.path.join(data_dir, 'tasks_test_stats_from_fai.in.empty.fai')
644 got = tasks.stats_from_fai(infile)
645 expected = {
646 'longest': 0,
647 'shortest': 0,
648 'N50': 0,
649 'mean': 0,
650 'number': 0,
651 'total_length': 0
652 }
653 self.assertEqual(expected, got)
654
655
656 class TestToBoulderio(unittest.TestCase):
657 def test_to_boulderio(self):
658 '''Test task to_boulderio'''
659 tmpfile = 'tmp.boulder'
660 tasks.to_boulderio(os.path.join(data_dir, 'tasks_test_to_boulderio.in.fa'), tmpfile)
661 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'tasks_test_to_boulderio.out.boulder'), tmpfile, shallow=False))
662 os.unlink(tmpfile)
663
664
665 class TestToFasta(unittest.TestCase):
666 def test_to_fasta(self):
667 '''Test to_fasta'''
668 tmpfile = 'tmp.to_fasta'
669 infiles = [
670 'sequences_test_good_file.fq',
671 'sequences_test_gffv3.gff',
672 'sequences_test_gffv3.no_FASTA_line.gff',
673 'sequences_test.embl',
674 'sequences_test.gbk',
675 'sequences_test_phylip.interleaved',
676 'sequences_test_phylip.interleaved2',
677 'sequences_test_phylip.sequential'
678 ]
679 infiles = [os.path.join(data_dir, x) for x in infiles]
680 expected_outfiles = [x + '.to_fasta' for x in infiles]
681
682 for i in range(len(infiles)):
683 tasks.to_fasta(infiles[i], tmpfile)
684 self.assertTrue(filecmp.cmp(expected_outfiles[i], tmpfile))
685
686 tasks.to_fasta(os.path.join(data_dir, 'sequences_test.fa'), tmpfile, line_length=3)
687 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test.line_length3.fa'), tmpfile))
688 tasks.to_fasta(os.path.join(data_dir, 'sequences_test_strip_after_whitespace.fa'), tmpfile, strip_after_first_whitespace=True)
689 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_strip_after_whitespace.fa.to_fasta'), tmpfile))
690 os.unlink(tmpfile)
691
692 def test_to_fasta_strip_after_whitespace_non_unique(self):
693 '''Test strip_after_whitespace with non-unique names'''
694 tmpfile = 'tmp.strip_after_whitespace.fa'
695 infile = os.path.join(data_dir, 'sequences_test.to_fasta.strip_after_whitespace_non_unique.in.fa')
696 expected = os.path.join(data_dir, 'sequences_test.to_fasta.strip_after_whitespace_non_unique.out.fa')
697
698 with self.assertRaises(tasks.Error):
699 tasks.to_fasta(infile, tmpfile, strip_after_first_whitespace=True, check_unique=True)
700
701 tasks.to_fasta(infile, tmpfile, strip_after_first_whitespace=True, check_unique=False)
702 self.assertTrue(filecmp.cmp(tmpfile, expected, shallow=False))
703 os.unlink(tmpfile)
704
705 def test_to_fasta_strip_after_whitespace_unique(self):
706 '''Test strip_after_whitespace with unique names'''
707 tmpfile = 'tmp.strip_after_whitespace.fa'
708 infile = os.path.join(data_dir, 'sequences_test.to_fasta.strip_after_whitespace_unique.in.fa')
709 expected = os.path.join(data_dir, 'sequences_test.to_fasta.strip_after_whitespace_unique.out.fa')
710 tasks.to_fasta(infile, tmpfile, strip_after_first_whitespace=True, check_unique=True)
711 self.assertTrue(filecmp.cmp(tmpfile, expected, shallow=False))
712 os.unlink(tmpfile)
713
714 class TestToUniqueByID(unittest.TestCase):
715 def test_to_unique_by_id(self):
716 '''Test to_unique_by_id()'''
717 tmpfile = 'tmp.unique_by_id.fa'
718 tasks.to_unique_by_id(os.path.join(data_dir, 'sequences_test_to_unique_by_id.fa'), tmpfile)
719 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_to_unique_by_id.fa.out'), tmpfile))
720 os.unlink(tmpfile)
721
722
723 class TestToFastaUnion(unittest.TestCase):
724 def test_to_fasta_union(self):
725 '''Test to_fasta_union'''
726 tmpfile = 'tmp.to_fasta_union'
727 tasks.to_fasta_union(os.path.join(data_dir, 'sequences_test_to_fasta_union.in.fa'), tmpfile, seqname='testname')
728 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'sequences_test_to_fasta_union.out.fa'), tmpfile, shallow=False))
729 os.unlink(tmpfile)
730
731
732 if __name__ == '__main__':
733 unittest.main()
734
735