Skip to content
Newer
Older
100644 179 lines (133 sloc) 8.21 KB
1b681e5 @bow Add SearchIO.write tests
bow authored
1 # Copyright 2012 by Wibowo Arindrarto. All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license. Please see the LICENSE file that should have been included
4 # as part of this package.
5
6 """Tests for SearchIO writing."""
7
8 import os
9 import unittest
10
4e65064 @peterjc Silence experimental warning in SearchIO tests with context manager
peterjc authored
11 from Bio import BiopythonExperimentalWarning
12
13 import warnings
65d5d63 @carlosp420 PEP8 fixes, various test files
carlosp420 authored
14
15
4e65064 @peterjc Silence experimental warning in SearchIO tests with context manager
peterjc authored
16 with warnings.catch_warnings():
65d5d63 @carlosp420 PEP8 fixes, various test files
carlosp420 authored
17 warnings.simplefilter('ignore', BiopythonExperimentalWarning)
18 from Bio import SearchIO
1b681e5 @bow Add SearchIO.write tests
bow authored
19
eb73d75 @bow Simplify search object comparison method
bow authored
20 from search_tests_common import compare_search_obj
1b681e5 @bow Add SearchIO.write tests
bow authored
21
22
23 class WriteCases(unittest.TestCase):
24
25 def tearDown(self):
26 if os.path.exists(self.out):
27 os.remove(self.out)
28
9dfec35 @cbrueffer Zap redundant backslashes between brackets (PEP8 E502).
cbrueffer authored
29 def parse_write_and_compare(self, source_file, source_format, out_file,
0ebee95 @bow Update blast-tab with new kwarg for commented files
bow authored
30 out_format, **kwargs):
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
31 """Compares parsed QueryResults after they have been written to a file."""
9dfec35 @cbrueffer Zap redundant backslashes between brackets (PEP8 E502).
cbrueffer authored
32 source_qresults = list(SearchIO.parse(source_file, source_format,
0ebee95 @bow Update blast-tab with new kwarg for commented files
bow authored
33 **kwargs))
34 SearchIO.write(source_qresults, out_file, out_format, **kwargs)
35 out_qresults = list(SearchIO.parse(out_file, out_format, **kwargs))
1b681e5 @bow Add SearchIO.write tests
bow authored
36 for source, out in zip(source_qresults, out_qresults):
eb73d75 @bow Simplify search object comparison method
bow authored
37 self.assertTrue(compare_search_obj(source, out))
1b681e5 @bow Add SearchIO.write tests
bow authored
38
9dfec35 @cbrueffer Zap redundant backslashes between brackets (PEP8 E502).
cbrueffer authored
39 def read_write_and_compare(self, source_file, source_format, out_file,
0ebee95 @bow Update blast-tab with new kwarg for commented files
bow authored
40 out_format, **kwargs):
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
41 """Compares read QueryResults after it has been written to a file."""
0ebee95 @bow Update blast-tab with new kwarg for commented files
bow authored
42 source_qresult = SearchIO.read(source_file, source_format, **kwargs)
43 SearchIO.write(source_qresult, out_file, out_format, **kwargs)
44 out_qresult = SearchIO.read(out_file, out_format, **kwargs)
eb73d75 @bow Simplify search object comparison method
bow authored
45 self.assertTrue(compare_search_obj(source_qresult, out_qresult))
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
46
47
1b681e5 @bow Add SearchIO.write tests
bow authored
48 class BlastXmlWriteCases(WriteCases):
49
50 fmt = 'blast-xml'
51 out = os.path.join('Blast', 'test_write.xml')
52
53 def test_write_single_from_blastxml(self):
54 """Test blast-xml writing from blast-xml, BLAST 2.2.26+, single query (xml_2226_blastp_004.xml)"""
55 source = os.path.join('Blast', 'xml_2226_blastp_004.xml')
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
56 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
57 self.read_write_and_compare(source, self.fmt, self.out, self.fmt)
1b681e5 @bow Add SearchIO.write tests
bow authored
58
59 def test_write_multiple_from_blastxml(self):
60 """Test blast-xml writing from blast-xml, BLAST 2.2.26+, multiple queries (xml_2226_blastp_001.xml)"""
61 source = os.path.join('Blast', 'xml_2226_blastp_001.xml')
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
62 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
1b681e5 @bow Add SearchIO.write tests
bow authored
63
64
edd90df @bow Fix BlastTabWrite test typo
bow authored
65 class BlastTabWriteCases(WriteCases):
1b681e5 @bow Add SearchIO.write tests
bow authored
66
67 fmt = 'blast-tab'
68 out = os.path.join('Blast', 'test_write.txt')
69
70 def test_write_single_from_blasttab(self):
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
71 """Test blast-tab writing from blast-tab, BLAST 2.2.26+, single query (tab_2226_tblastn_004.txt)"""
1b681e5 @bow Add SearchIO.write tests
bow authored
72 source = os.path.join('Blast', 'tab_2226_tblastn_004.txt')
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
73 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
74 self.read_write_and_compare(source, self.fmt, self.out, self.fmt)
1b681e5 @bow Add SearchIO.write tests
bow authored
75
76 def test_write_multiple_from_blasttab(self):
edd90df @bow Fix BlastTabWrite test typo
bow authored
77 """Test blast-tab writing from blast-tab, BLAST 2.2.26+, multiple queries (tab_2226_tblastn_001.txt)"""
78 source = os.path.join('Blast', 'tab_2226_tblastn_001.txt')
79 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
80
6f6f18a @bow Add new format keyword: blast-tabc; Refactored writer for blast-tabc
bow authored
81 def test_write_single_from_blasttabc(self):
82 """Test blast-tabc writing from blast-tabc, BLAST 2.2.26+, single query (tab_2226_tblastn_008.txt)"""
83 source = os.path.join('Blast', 'tab_2226_tblastn_008.txt')
6d3d8a9 @bow Rename blast-tab arg 'has_comments' to 'comments'
bow authored
84 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt, comments=True)
85 self.read_write_and_compare(source, self.fmt, self.out, self.fmt, comments=True)
6f6f18a @bow Add new format keyword: blast-tabc; Refactored writer for blast-tabc
bow authored
86
87 def test_write_multiple_from_blasttabc(self):
88 """Test blast-tabc writing from blast-tabc, BLAST 2.2.26+, multiple queries (tab_2226_tblastn_005.txt)"""
89 source = os.path.join('Blast', 'tab_2226_tblastn_005.txt')
6d3d8a9 @bow Rename blast-tab arg 'has_comments' to 'comments'
bow authored
90 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt, comments=True)
6f6f18a @bow Add new format keyword: blast-tabc; Refactored writer for blast-tabc
bow authored
91
d3e11e8 @bow Add new test for BLAST commented tab writing
bow authored
92 def test_write_multiple_from_blasttabc_allfields(self):
93 """Test blast-tabc writing from blast-tabc, BLAST 2.2.28+, multiple queries (tab_2228_tblastx_001.txt)"""
94 source = os.path.join('Blast', 'tab_2228_tblastx_001.txt')
95 fields = ['qseqid', 'qgi', 'qacc', 'qaccver', 'qlen', 'sseqid',
96 'sallseqid', 'sgi', 'sallgi', 'sacc', 'saccver', 'sallacc',
97 'slen', 'qstart', 'qend', 'sstart', 'send', 'qseq', 'sseq',
98 'evalue', 'bitscore', 'score', 'length', 'pident', 'nident',
99 'mismatch', 'positive', 'gapopen', 'gaps', 'ppos', 'frames',
100 'qframe', 'sframe', 'btop', 'staxids', 'sscinames', 'scomnames',
101 'sblastnames', 'sskingdoms', 'stitle', 'salltitles', 'sstrand',
102 'qcovs', 'qcovhsp']
103 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt, comments=True, fields=fields)
104
6f6f18a @bow Add new format keyword: blast-tabc; Refactored writer for blast-tabc
bow authored
105
0c76c42 @bow Add hmmer-tab writing and its tests
bow authored
106 class HmmerTabWriteCases(WriteCases):
107
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
108 fmt = 'hmmer3-tab'
0c76c42 @bow Add hmmer-tab writing and its tests
bow authored
109 out = os.path.join('Hmmer', 'test_write.txt')
110
111 def test_write_single_from_hmmertab(self):
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
112 """Test hmmer3-tab writing from hmmer3-tab, HMMER 3.0, single query (tab_30_hmmscan_004.out)"""
0c76c42 @bow Add hmmer-tab writing and its tests
bow authored
113 source = os.path.join('Hmmer', 'tab_30_hmmscan_004.out')
114 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
115 self.read_write_and_compare(source, self.fmt, self.out, self.fmt)
116
117 def test_write_multiple_from_hmmertab(self):
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
118 """Test hmmer3-tab writing from hmmer3-tab, HMMER 3.0, multiple queries (tab_30_hmmscan_001.out)"""
0c76c42 @bow Add hmmer-tab writing and its tests
bow authored
119 source = os.path.join('Hmmer', 'tab_30_hmmscan_001.out')
a1ce03e @bow Update SearchIO.write tests with SearchIO.read tests
bow authored
120 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
1b681e5 @bow Add SearchIO.write tests
bow authored
121
122
fbb83ed @bow Add hmmer-domtab writing and its tests
bow authored
123 class HmmerDomtabWriteCases(WriteCases):
124
125 out = os.path.join('Hmmer', 'test_write.txt')
126
127 def test_write_single_from_hmmscandomtab(self):
128 """Test hmmscan-domtab writing from hmmscan-domtab, HMMER 3.0, single query (tab_30_hmmscan_004.out)"""
129 source = os.path.join('Hmmer', 'domtab_30_hmmscan_004.out')
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
130 fmt = 'hmmscan3-domtab'
fbb83ed @bow Add hmmer-domtab writing and its tests
bow authored
131 self.parse_write_and_compare(source, fmt, self.out, fmt)
132 self.read_write_and_compare(source, fmt, self.out, fmt)
133
134 def test_write_multiple_from_hmmscandomtab(self):
135 """Test hmmscan-domtab writing from hmmscan-domtab, HMMER 3.0, multiple queries (tab_30_hmmscan_001.out)"""
136 source = os.path.join('Hmmer', 'domtab_30_hmmscan_001.out')
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
137 fmt = 'hmmscan3-domtab'
fbb83ed @bow Add hmmer-domtab writing and its tests
bow authored
138 self.parse_write_and_compare(source, fmt, self.out, fmt)
139
140 def test_write_single_from_hmmsearchdomtab(self):
141 """Test hmmsearch-domtab writing from hmmsearch-domtab, HMMER 3.0, single query (tab_30_hmmscan_004.out)"""
142 source = os.path.join('Hmmer', 'domtab_30_hmmsearch_001.out')
d390255 @bow Rename hmmer-* format names to hmmer3-*
bow authored
143 fmt = 'hmmsearch3-domtab'
fbb83ed @bow Add hmmer-domtab writing and its tests
bow authored
144 self.parse_write_and_compare(source, fmt, self.out, fmt)
145 self.read_write_and_compare(source, fmt, self.out, fmt)
146
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
147
148 class BlatPslWriteCases(WriteCases):
149
820da77 @bow Merged 'blat-pslx' into 'blat-psl' format
bow authored
150 fmt = 'blat-psl'
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
151 out = os.path.join('Blat', 'test_write.txt')
152
153 def test_write_single_from_blatpsl(self):
154 """Test blat-psl writing from blat-psl, single query (psl_34_004.psl)"""
155 source = os.path.join('Blat', 'psl_34_004.psl')
820da77 @bow Merged 'blat-pslx' into 'blat-psl' format
bow authored
156 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
157 self.read_write_and_compare(source, self.fmt, self.out, self.fmt)
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
158
159 def test_write_multiple_from_blatpsl(self):
160 """Test blat-psl writing from blat-psl, multiple queries (psl_34_001.psl)"""
161 source = os.path.join('Blat', 'psl_34_001.psl')
820da77 @bow Merged 'blat-pslx' into 'blat-psl' format
bow authored
162 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt)
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
163
164 def test_write_single_from_blatpslx(self):
165 """Test blat-pslx writing from blat-pslx, single query (pslx_34_004.pslx)"""
166 source = os.path.join('Blat', 'pslx_34_004.pslx')
820da77 @bow Merged 'blat-pslx' into 'blat-psl' format
bow authored
167 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt, pslx=True)
168 self.read_write_and_compare(source, self.fmt, self.out, self.fmt, pslx=True)
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
169
170 def test_write_multiple_from_blatpslx(self):
171 """Test blat-pslx writing from blat-pslx, multiple queries (pslx_34_001.pslx)"""
172 source = os.path.join('Blat', 'pslx_34_001.pslx')
820da77 @bow Merged 'blat-pslx' into 'blat-psl' format
bow authored
173 self.parse_write_and_compare(source, self.fmt, self.out, self.fmt, pslx=True)
54199dd @bow Add tests for writing and indexing blat-psl{x}
bow authored
174
175
1b681e5 @bow Add SearchIO.write tests
bow authored
176 if __name__ == "__main__":
b67cebf @peterjc Fix common PEP8 violation in unittest verbosity argument
peterjc authored
177 runner = unittest.TextTestRunner(verbosity=2)
1b681e5 @bow Add SearchIO.write tests
bow authored
178 unittest.main(testRunner=runner)
Something went wrong with that request. Please try again.