Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 360 lines (309 sloc) 15.36 kb
1697081 @peterjc Unit tests and a fix for an issue with loading a DBSeqRecord into BioSQL...
peterjc authored
1 # This code is part of the Biopython distribution and governed by its
2 # license. Please see the LICENSE file that should have been included
3 # as part of this package.
57bb2c0 @peterjc Use basestring from Bio._py3k
peterjc authored
4
5be07dd @peterjc Tweaks so we can drop the 2to3 xrange fixer
peterjc authored
5 from Bio._py3k import range
57bb2c0 @peterjc Use basestring from Bio._py3k
peterjc authored
6 from Bio._py3k import basestring
5be07dd @peterjc Tweaks so we can drop the 2to3 xrange fixer
peterjc authored
7
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
8 from Bio.Seq import UnknownSeq
9 from Bio.SeqUtils.CheckSum import seguid
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
10 from Bio.SeqFeature import ExactPosition, UnknownPosition
11 from Bio.SeqFeature import FeatureLocation, CompoundLocation, SeqFeature
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
12 from Bio.SeqRecord import SeqRecord
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
13
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
14
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
15 def checksum_summary(record):
16 if isinstance(record.seq, UnknownSeq):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
17 return repr(record.seq)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
18 if len(record.seq) < 25:
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
19 short = str(record.seq)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
20 else:
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
21 short = str(record.seq)[:19] \
22 + "..." + str(record.seq)[-3:]
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
23 return "%s [%s] len %i" \
24 % (short, seguid(record.seq), len(record.seq))
25
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
26
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
27 def compare_reference(old_r, new_r):
1697081 @peterjc Unit tests and a fix for an issue with loading a DBSeqRecord into BioSQL...
peterjc authored
28 """Compare two Reference objects
29
30 Note new_r is assumed to be a BioSQL DBSeqRecord, due to limitations
31 of the BioSQL table structure.
32 """
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
33 assert old_r.title == new_r.title, \
34 "%s vs %s" % (old_r.title, new_r.title)
35 assert old_r.authors == new_r.authors, \
36 "%s vs %s" % (old_r.authors, new_r.authors)
37 assert old_r.journal == new_r.journal, \
38 "%s vs %s" % (old_r.journal, new_r.journal)
39 assert old_r.medline_id == new_r.medline_id, \
40 "%s vs %s" % (old_r.medline_id, new_r.medline_id)
41
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
42 if old_r.pubmed_id and new_r.pubmed_id:
1697081 @peterjc Unit tests and a fix for an issue with loading a DBSeqRecord into BioSQL...
peterjc authored
43 assert old_r.pubmed_id == new_r.pubmed_id
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
44 # Looking at BioSQL/BioSeq.py function _retrieve_reference
45 # it seems that it will get either the MEDLINE or PUBMED,
46 # but not both. I *think* the current schema does not allow
47 # us to store both... must confirm this.
48
49 # TODO - assert old_r.comment == new_r.comment
50 # Looking at the tables, I *think* the current schema does not
51 # allow us to store a reference comment. Must confirm this.
fd218d8 @peterjc Adjust Tests/seq_tests_common.py for comparing like with like
peterjc authored
52 assert old_r.comment == new_r.comment or new_r.comment == "", \
53 "%r vs %r" % (old_r.comment, new_r.comment)
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
54
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
55 # TODO - assert old_r.consrtm == new_r.consrtm
56 # Looking at the tables, I *think* the current schema does not
57 # allow us to store a consortium.
f797cb9 @peterjc Adding get_raw method to Bio.SeqIO.index() dictionary class (see Bug 300...
peterjc authored
58 assert old_r.consrtm == new_r.consrtm or new_r.consrtm == ""
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
59
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
60 if len(old_r.location) == 0:
1697081 @peterjc Unit tests and a fix for an issue with loading a DBSeqRecord into BioSQL...
peterjc authored
61 assert len(new_r.location) == 0
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
62 else:
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
63 # BioSQL can only store ONE location!
64 # TODO - Check BioPerl with a GenBank file with multiple ref locations
1697081 @peterjc Unit tests and a fix for an issue with loading a DBSeqRecord into BioSQL...
peterjc authored
65 assert isinstance(old_r.location[0], FeatureLocation)
66 assert isinstance(new_r.location[0], FeatureLocation)
67 assert old_r.location[0].start == new_r.location[0].start and \
68 old_r.location[0].end == new_r.location[0].end
69
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
70 return True
71
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
72
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
73 def compare_feature(old_f, new_f):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
74 """Compare two SeqFeature objects"""
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
75 assert isinstance(old_f, SeqFeature)
76 assert isinstance(new_f, SeqFeature)
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
77
78 assert old_f.type == new_f.type, \
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
79 "%s -> %s" % (old_f.type, new_f.type)
80
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
81 assert old_f.strand == new_f.strand, \
82 "%s -> %s" % (old_f.strand, new_f.strand)
83
84 assert old_f.ref == new_f.ref, \
85 "%s -> %s" % (old_f.ref, new_f.ref)
86
87 assert old_f.ref_db == new_f.ref_db, \
88 "%s -> %s" % (old_f.ref_db, new_f.ref_db)
89
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
90 # TODO - BioSQL does not store/retrieve feature's id (Bug 2526)
fd218d8 @peterjc Adjust Tests/seq_tests_common.py for comparing like with like
peterjc authored
91 assert old_f.id == new_f.id or new_f.id == "<unknown id>"
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
92
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
93 # TODO - Work out how the location_qualifier_value table should
94 # be used, given BioPerl seems to ignore it (Bug 2766)
95 # assert old_f.location_operator == new_f.location_operator, \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
96 # "%s -> %s" % (old_f.location_operator, new_f.location_operator)
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
97
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
98 # We dont store fuzzy locations:
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
99 assert old_f.location.start == new_f.location.start \
a71f7fe @cbrueffer Fix PEP8 E502 (the backslash is redundant between brackets).
cbrueffer authored
100 or (isinstance(old_f.location.start, UnknownPosition) and
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
101 isinstance(new_f.location.start, UnknownPosition)), \
a71f7fe @cbrueffer Fix PEP8 E502 (the backslash is redundant between brackets).
cbrueffer authored
102 "%s -> %s" % (old_f.location.start,
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
103 new_f.location.start)
104 assert old_f.location.end == new_f.location.end \
a71f7fe @cbrueffer Fix PEP8 E502 (the backslash is redundant between brackets).
cbrueffer authored
105 or (isinstance(old_f.location.end, UnknownPosition) and
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
106 isinstance(new_f.location.end, UnknownPosition)), \
a71f7fe @cbrueffer Fix PEP8 E502 (the backslash is redundant between brackets).
cbrueffer authored
107 "%s -> %s" % (old_f.location.end,
b03595f @peterjc CompoundLocation half done, created as well as sub_features
peterjc authored
108 new_f.location.end)
109
110 assert isinstance(old_f.location, CompoundLocation) == \
111 isinstance(new_f.location, CompoundLocation)
112 if isinstance(old_f.location, CompoundLocation):
db6bc30 @carlosp420 PEP8 fixes rest of test files
carlosp420 authored
113 assert len(old_f.location.parts) == len(new_f.location.parts)
114 for old_l, new_l in zip(old_f.location.parts, new_f.location.parts):
115 assert old_l.start == new_l.start
116 assert old_l.end == new_l.end
117 assert old_l.strand == new_l.strand
118 assert old_l.ref == new_l.ref
119 assert old_l.ref_db == new_l.ref_db
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
120
f8c6646 @peterjc More tests; cope with UnknownPosition in no-fuzzy start/end
peterjc authored
121 assert len(old_f.location.parts) == len(new_f.location.parts)
122 for old_sub, new_sub in zip(old_f.location.parts, new_f.location.parts):
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
123 # These are FeatureLocation objects
f8c6646 @peterjc More tests; cope with UnknownPosition in no-fuzzy start/end
peterjc authored
124 assert old_sub.nofuzzy_start == new_sub.nofuzzy_start
125 assert old_sub.nofuzzy_end == new_sub.nofuzzy_end
126 assert old_sub.strand == new_sub.strand
127
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
128 # Using private variable to avoid deprecation warnings
4fc92e1 @peterjc Avoid sub_feature deprecation warnings in tests
peterjc authored
129 assert len(old_f._sub_features) == len(new_f._sub_features), \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
130 "number of sub_features: %s -> %s" % \
4fc92e1 @peterjc Avoid sub_feature deprecation warnings in tests
peterjc authored
131 (len(old_f._sub_features), len(new_f._sub_features))
27cb9c7 @cbrueffer Fix PEP8 W293 (blank line contains whitespace).
cbrueffer authored
132
4fc92e1 @peterjc Avoid sub_feature deprecation warnings in tests
peterjc authored
133 for old_sub, new_sub in zip(old_f._sub_features, new_f._sub_features):
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
134 # These are SeqFeature objects
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
135 assert old_sub.type == new_sub.type, \
136 "%s -> %s" % (old_sub.type, new_sub.type)
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
137
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
138 assert old_sub.strand == new_sub.strand, \
139 "%s -> %s" % (old_sub.strand, new_sub.strand)
140
141 assert old_sub.ref == new_sub.ref, \
142 "%s -> %s" % (old_sub.ref, new_sub.ref)
143
144 assert old_sub.ref_db == new_sub.ref_db, \
145 "%s -> %s" % (old_sub.ref_db, new_sub.ref_db)
146
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
147 # TODO - Work out how the location_qualifier_value table should
148 # be used, given BioPerl seems to ignore it (Bug 2766)
149 # assert old_sub.location_operator == new_sub.location_operator, \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
150 # "%s -> %s" % (old_sub.location_operator, new_sub.location_operator)
151
152 # Compare sub-feature Locations:
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
153 #
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
154 # BioSQL currently does not store fuzzy locations, but instead stores
155 # them as FeatureLocation.nofuzzy_start FeatureLocation.nofuzzy_end.
156 # The vast majority of cases will be comparisons of ExactPosition
157 # class locations, so we'll try that first and catch the exceptions.
158
159 try:
160 assert str(old_sub.location) == str(new_sub.location), \
161 "%s -> %s" % (str(old_sub.location), str(new_sub.location))
1d42f4d @chrismit Changed exceptions to be py3k compatible
chrismit authored
162 except AssertionError as e:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
163 if isinstance(old_sub.location.start, ExactPosition) and \
d9500da @cbrueffer Distinguish continuation lines from the next logical line (PEP8 E125).
cbrueffer authored
164 isinstance(old_sub.location.end, ExactPosition):
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
165 # Its not a problem with fuzzy locations, re-raise
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
166 raise e
167 else:
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
168 # At least one of the locations is fuzzy
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
169 assert old_sub.location.nofuzzy_start == \
170 new_sub.location.nofuzzy_start, \
9dfec35 @cbrueffer Zap redundant backslashes between brackets (PEP8 E502).
cbrueffer authored
171 "%s -> %s" % (old_sub.location.nofuzzy_start,
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
172 new_sub.location.nofuzzy_start)
173 assert old_sub.location.nofuzzy_end == \
174 new_sub.location.nofuzzy_end, \
9dfec35 @cbrueffer Zap redundant backslashes between brackets (PEP8 E502).
cbrueffer authored
175 "%s -> %s" % (old_sub.location.nofuzzy_end,
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
176 new_sub.location.nofuzzy_end)
177
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
178 assert len(old_f.qualifiers) == len(new_f.qualifiers)
6924b7d @peterjc Don't need to use dict's keys() method for iteration etc (misc modules/t...
peterjc authored
179 assert set(old_f.qualifiers) == set(new_f.qualifiers)
180 for key in old_f.qualifiers:
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
181 if isinstance(old_f.qualifiers[key], str):
182 if isinstance(new_f.qualifiers[key], str):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
183 assert old_f.qualifiers[key] == new_f.qualifiers[key]
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
184 elif isinstance(new_f.qualifiers[key], list):
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
185 # Maybe a string turning into a list of strings?
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
186 assert [old_f.qualifiers[key]] == new_f.qualifiers[key], \
187 "%s -> %s" \
188 % (repr(old_f.qualifiers[key]),
189 repr(new_f.qualifiers[key]))
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
190 else:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
191 assert False, "Problem with feature's '%s' qualifier" & key
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
192 else:
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
193 # Should both be lists of strings...
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
194 assert old_f.qualifiers[key] == new_f.qualifiers[key], \
195 "%s -> %s" % (old_f.qualifiers[key], new_f.qualifiers[key])
196 return True
197
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
198
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
199 def compare_sequence(old, new):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
200 """Compare two Seq or DBSeq objects"""
b87e8a8 @peterjc SFF get_raw support for Bio.SeqIO.index
peterjc authored
201 assert len(old) == len(new), "%i vs %i" % (len(old), len(new))
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
202 assert str(old) == str(new)
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
203
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
204 if isinstance(old, UnknownSeq):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
205 assert isinstance(new, UnknownSeq)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
206 else:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
207 assert not isinstance(new, UnknownSeq)
208
ac8a8fa @peterjc Use the integer division operator
peterjc authored
209 ln = len(old)
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
210 s = str(old)
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
211 assert isinstance(s, str)
212
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
213 # Don't check every single element; for long sequences
214 # this takes far far far too long to run!
215 # Test both positive and negative indices
ac8a8fa @peterjc Use the integer division operator
peterjc authored
216 if ln < 50:
5be07dd @peterjc Tweaks so we can drop the 2to3 xrange fixer
peterjc authored
217 indices = list(range(-ln, ln))
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
218 else:
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
219 # A selection of end cases, and the mid point
a87df70 @peterjc PEP8 white space in Tests
peterjc authored
220 indices = [-ln, -ln + 1, -(ln // 2), -1, 0, 1, ln // 2, ln - 2, ln - 1]
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
221
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
222 # Test element access,
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
223 for i in indices:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
224 expected = s[i]
225 assert expected == old[i]
226 assert expected == new[i]
227
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
228 # Test slices
10a6320 @cbrueffer Fix spacing around inline comments (E261, E262).
cbrueffer authored
229 indices.append(ln) # check copes with overflows
230 indices.append(ln + 1000) # check copes with overflows
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
231 for i in indices:
232 for j in indices:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
233 expected = s[i:j]
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
234 assert expected == str(old[i:j]), \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
235 "Slice %s vs %s" % (repr(expected), repr(old[i:j]))
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
236 assert expected == str(new[i:j]), \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
237 "Slice %s vs %s" % (repr(expected), repr(new[i:j]))
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
238 # Slicing with step of 1 should make no difference.
239 # Slicing with step 3 might be useful for codons.
14977ce @peterjc Apply 2to3 ws_comma fixer (white space changes)
peterjc authored
240 for step in [1, 3]:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
241 expected = s[i:j:step]
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
242 assert expected == str(old[i:j:step])
243 assert expected == str(new[i:j:step])
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
244
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
245 # Check automatic end points
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
246 expected = s[i:]
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
247 assert expected == str(old[i:])
248 assert expected == str(new[i:])
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
249
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
250 expected = s[:i]
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
251 assert expected == str(old[:i])
252 assert expected == str(new[:i])
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
253
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
254 # Check "copy" splice
79842f4 @bow Updates all unchanged Bio.Seq.tostring() to str(Bio.Seq)
bow authored
255 assert s == str(old[:])
256 assert s == str(new[:])
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
257 return True
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
258
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
259
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
260 def compare_features(old_list, new_list):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
261 assert isinstance(old_list, list)
262 assert isinstance(new_list, list)
263 assert len(old_list) == len(new_list)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
264 for old_f, new_f in zip(old_list, new_list):
265 if not compare_feature(old_f, new_f):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
266 return False
267 return True
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
268
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
269
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
270 def compare_record(old, new):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
271 """Compare two SeqRecord or DBSeqRecord objects"""
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
272 assert isinstance(old, SeqRecord)
273 assert isinstance(new, SeqRecord)
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
274 # Sequence:
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
275 compare_sequence(old.seq, new.seq)
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
276 # Basics:
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
277 assert old.id == new.id
278 assert old.name == new.name
279 assert old.description == new.description
280 assert old.dbxrefs == new.dbxrefs, \
281 "dbxrefs mismatch\nOld: %s\nNew: %s" \
282 % (old.dbxrefs, new.dbxrefs)
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
283 # Features:
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
284 if not compare_features(old.features, new.features):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
285 return False
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
286
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
287 # Annotation:
288 # We are expecting to see some "extra" annotations appearing,
289 # such as 'cross_references', 'dates', 'data_file_division',
db6bc30 @carlosp420 PEP8 fixes rest of test files
carlosp420 authored
290 # 'ncbi_taxon' and 'gi'.
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
291 # TODO - address these, see Bug 2681?
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
292 new_keys = set(new.annotations).difference(old.annotations)
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
293 new_keys = new_keys.difference(['cross_references', 'date',
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
294 'data_file_division', 'ncbi_taxid', 'gi'])
295 assert not new_keys, "Unexpected new annotation keys: %s" \
296 % ", ".join(new_keys)
297 missing_keys = set(old.annotations).difference(new.annotations)
d18aab8 @peterjc autopep8 E261 - Fix spacing after comment hash
peterjc authored
298 missing_keys = missing_keys.difference(['ncbi_taxid', # Can't store chimeras
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
299 ])
300 assert not missing_keys, "Unexpectedly missing annotation keys: %s" \
301 % ", ".join(missing_keys)
b1c8597 @cbrueffer Trim EOL whitespace (PEP8 W291, W293), batch 3.
cbrueffer authored
302
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
303 # In the short term, just compare any shared keys:
6924b7d @peterjc Don't need to use dict's keys() method for iteration etc (misc modules/t...
peterjc authored
304 for key in set(old.annotations).intersection(new.annotations):
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
305 if key == "references":
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
306 assert len(old.annotations[key]) == len(new.annotations[key])
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
307 for old_r, new_r in zip(old.annotations[key], new.annotations[key]):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
308 compare_reference(old_r, new_r)
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
309 elif key == "comment":
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
310 # Turn them both into containing strings for comparison - due to
311 # line wrapping in GenBank etc we don't really expect the white
312 # space to be 100% the same.
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
313 if isinstance(old.annotations[key], list):
c638926 @peterjc Adjust comment checking to tollerate white space variation (now that we ...
peterjc authored
314 old_comment = " ".join(old.annotations[key])
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
315 else:
c638926 @peterjc Adjust comment checking to tollerate white space variation (now that we ...
peterjc authored
316 old_comment = old.annotations[key]
317 if isinstance(new.annotations[key], list):
318 new_comment = " ".join(new.annotations[key])
319 else:
320 new_comment = new.annotations[key]
14977ce @peterjc Apply 2to3 ws_comma fixer (white space changes)
peterjc authored
321 old_comment = old_comment.replace("\n", " ").replace(" ", " ")
322 new_comment = new_comment.replace("\n", " ").replace(" ", " ")
c638926 @peterjc Adjust comment checking to tollerate white space variation (now that we ...
peterjc authored
323 assert old_comment == new_comment, \
324 "Comment annotation changed by load/retrieve\n" \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
325 "Was:%s\nNow:%s" \
c638926 @peterjc Adjust comment checking to tollerate white space variation (now that we ...
peterjc authored
326 % (repr(old_comment), repr(new_comment))
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
327 elif key in ["taxonomy", "organism", "source"]:
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
328 # If there is a taxon id recorded, these fields get overwritten
329 # by data from the taxon/taxon_name tables. There is no
330 # guarantee that they will be identical after a load/retrieve.
a9a100c @chapmanb SQLite support for BioSQL database. See discussion on Bug 2866 for addit...
chapmanb authored
331 assert isinstance(new.annotations[key], basestring) \
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
332 or isinstance(new.annotations[key], list)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
333 elif type(old.annotations[key]) == type(new.annotations[key]):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
334 assert old.annotations[key] == new.annotations[key], \
335 "Annotation '%s' changed by load/retrieve\nWas:%s\nNow:%s" \
336 % (key, old.annotations[key], new.annotations[key])
337 elif isinstance(old.annotations[key], str) \
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
338 and isinstance(new.annotations[key], list):
82272b1 @peterjc autopep8 E265 - Format block comments.
peterjc authored
339 # Any annotation which is a single string gets turned into
340 # a list containing one string by BioSQL at the moment.
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
341 assert [old.annotations[key]] == new.annotations[key], \
342 "Annotation '%s' changed by load/retrieve\nWas:%s\nNow:%s" \
343 % (key, old.annotations[key], new.annotations[key])
344 elif isinstance(old.annotations[key], list) \
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
345 and isinstance(new.annotations[key], str):
3e676fb @peterjc Moving some BioSQL test code to a common file
peterjc authored
346 assert old.annotations[key] == [new.annotations[key]], \
347 "Annotation '%s' changed by load/retrieve\nWas:%s\nNow:%s" \
348 % (key, old.annotations[key], new.annotations[key])
349 return True
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
350
fd82a0a @cbrueffer Add blank lines where needed (PEP8 E302).
cbrueffer authored
351
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
352 def compare_records(old_list, new_list):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
353 assert isinstance(old_list, list)
354 assert isinstance(new_list, list)
355 assert len(old_list) == len(new_list)
9252f1c @peterjc No code changes. Removing white space before ':' character to match PEP8...
peterjc authored
356 for old_r, new_r in zip(old_list, new_list):
357 if not compare_record(old_r, new_r):
6187bc9 @peterjc Renaming my unit test SeqRecord comparison functions for better consiste...
peterjc authored
358 return False
359 return True
Something went wrong with that request. Please try again.