forked from biopython/biopython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_tests_common.py
165 lines (138 loc) · 6.51 KB
/
search_tests_common.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
# Copyright 2012 by Wibowo Arindrarto. All rights reserved.
# Revisions Copyright 2012 by Peter Cock. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
import os
import gzip
import unittest
try:
import sqlite3
except ImportError:
sqlite3 = None
from Bio import SearchIO
from Bio._py3k import _as_bytes
from Bio.SeqRecord import SeqRecord
class CheckRaw(unittest.TestCase):
"""Base class for testing index's get_raw method."""
def check_raw(self, filename, id, raw, **kwargs):
"""Index filename using **kwargs, check get_raw(id)==raw."""
idx = SearchIO.index(filename, self.fmt, **kwargs)
raw = _as_bytes(raw)
self.assertEqual(raw, idx.get_raw(id))
idx.close()
#Now again, but using SQLite backend
if sqlite3:
idx = SearchIO.index_db(":memory:", filename, self.fmt, **kwargs)
self.assertEqual(raw, idx.get_raw(id))
idx.close()
if os.path.isfile(filename + ".bgz"):
#Do the tests again with the BGZF compressed file
print "[BONUS %s.bgz]" % filename
self.check_raw(filename + ".bgz", id, raw, **kwargs)
class CheckIndex(unittest.TestCase):
"""Base class for testing indexing."""
def check_index(self, filename, format, **kwargs):
# check if Python3 installation has sqlite3
try:
import sqlite3
except ImportError:
sqlite3 = None
if filename.endswith(".bgz"):
handle = gzip.open(filename)
parsed = list(SearchIO.parse(handle, format, **kwargs))
handle.close()
else:
parsed = list(SearchIO.parse(filename, format, **kwargs))
# compare values by index
indexed = SearchIO.index(filename, format, **kwargs)
self.assertEqual(len(parsed), len(indexed),
"Should be %i records in %s, index says %i" \
% (len(parsed), filename, len(indexed)))
# compare values by index_db, only if sqlite3 is present
if sqlite3 is not None:
db_indexed = SearchIO.index_db(':memory:', [filename], format, **kwargs)
self.assertEqual(len(parsed), len(db_indexed),
"Should be %i records in %s, index_db says %i" \
% (len(parsed), filename, len(db_indexed)))
for qres in parsed:
idx_qres = indexed[qres.id]
# parsed and indexed qresult are different objects!
self.assertNotEqual(id(qres), id(idx_qres))
# but they should have the same attribute values
self.assertTrue(compare_search_obj(qres, idx_qres))
# sqlite3 comparison, only if it's present
if sqlite3 is not None:
dbidx_qres = db_indexed[qres.id]
self.assertNotEqual(id(qres), id(dbidx_qres))
self.assertTrue(compare_search_obj(qres, dbidx_qres))
indexed.close()
if sqlite3 is not None:
db_indexed.close()
db_indexed._con.close()
if os.path.isfile(filename + ".bgz"):
#Do the tests again with the BGZF compressed file
print "[BONUS %s.bgz]" % filename
self.check_index(filename + ".bgz", format, **kwargs)
def _num_difference(obj_a, obj_b):
"""Returns the number of instance attributes presence only in one object."""
attrs_a = obj_a.__dict__.keys()
attrs_b = obj_b.__dict__.keys()
diff = set(attrs_a).symmetric_difference(set(attrs_b))
privates = len([x for x in diff if x.startswith('_')])
return len(diff) - privates
def compare_search_obj(obj_a, obj_b):
"""Compares attribute values of two QueryResult objects."""
# check that both qresults contain the same instance attributes
assert _num_difference(obj_a, obj_b) == 0
# compare qresult attributes
# if the above assertion pass, doesn't matter if we use a or be here
compare_attrs(obj_a, obj_b, obj_a.__dict__.keys())
# compare objects recursively if it's not an HSPFragment
if not isinstance(obj_a, SearchIO.HSPFragment):
# check the number of hits contained
assert len(obj_a) == len(obj_b), "length: %r vs %r" % (len(obj_a),
len(obj_b), obj_a, obj_b)
for item_a, item_b in zip(obj_a, obj_b):
assert compare_search_obj(item_a, item_b)
return True
def compare_attrs(obj_a, obj_b, attrs):
"""Compares attribute values of two objects."""
for attr in attrs:
# don't check for contained items, they are handled separately
if attr.startswith('_items'):
continue
# get attribute values from each objects
val_a = getattr(obj_a, attr)
val_b = getattr(obj_b, attr)
# special case for HSP and HSPFragment {hit,query}
# since they are seqrecords, we compare the strings only
# comparing using compare_record is too slow
if attr in ('_hit', '_query') and (val_a is not None and val_b is
not None):
# compare seq directly if it's a contiguous hsp
if isinstance(val_a, SeqRecord) and isinstance(val_b, SeqRecord):
assert str(val_a.seq) == str(val_b.seq), \
"%s: %r vs %r" % (attr, val_a, val_b)
elif isinstance(val_a, list) and isinstance(val_b, list):
for seq_a, seq_b in zip(val_a, val_b):
assert str(seq_a.seq) == str(seq_b.seq), \
"%s: %r vs %r" % (attr, seq_a, seq_b)
# if it's a dictionary, compare values and keys
elif isinstance(val_a, dict):
assert isinstance(val_b, dict)
keys_a, values_a = val_a.keys(), val_a.values()
keys_b, values_b = val_b.keys(), val_b.values()
# sort all values and keys
[x.sort() for x in (keys_a, values_a, keys_b, values_b)]
assert keys_a == keys_b, "%s: %r vs %r" % (attr, keys_a, keys_b)
assert values_a == values_b, "%s: %r vs %r" % (attr, values_a,
values_b)
# if it's an alphabet, check the class names as alphabets are instances
elif attr == '_alphabet':
alph_a = val_a.__class__.__name__
alph_b = val_b.__class__.__name__
assert alph_a == alph_b, "%s: %r vs %r" % (attr, alph_a, alph_b)
else:
assert val_a == val_b, "%s: %r vs %r" % (attr, val_a, val_b)
return True