/
test_manifest_protocol.py
211 lines (150 loc) · 6.15 KB
/
test_manifest_protocol.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
"""
Tests for the 'CollectionManifest' class and protocol. All subclasses
of BaseCollectionManifest should support this functionality.
"""
import pytest
import sourmash_tst_utils as utils
import sourmash
from sourmash.manifest import BaseCollectionManifest, CollectionManifest
from sourmash.index.sqlite_index import SqliteCollectionManifest
def build_simple_manifest(runtmp):
# load and return the manifest from prot/all.zip
filename = utils.get_test_data('prot/all.zip')
idx = sourmash.load_file_as_index(filename)
mf = idx.manifest
assert len(mf) == 8
return mf
def build_sqlite_manifest(runtmp):
# return the manifest from prot/all.zip
filename = utils.get_test_data('prot/all.zip')
idx = sourmash.load_file_as_index(filename)
mf = idx.manifest
# build sqlite manifest from this 'un
mfdb = runtmp.output('test.sqlmf')
return SqliteCollectionManifest.load_from_manifest(mf, dbfile=mfdb)
def save_load_manifest(runtmp):
# save/load the manifest from a CSV.
mf = build_simple_manifest(runtmp)
mf_csv = runtmp.output('mf.csv')
mf.write_to_filename(mf_csv)
load_mf = CollectionManifest.load_from_filename(mf_csv)
return load_mf
@pytest.fixture(params=[build_simple_manifest,
save_load_manifest,
build_sqlite_manifest])
def manifest_obj(request, runtmp):
build_fn = request.param
return build_fn(runtmp)
###
### generic CollectionManifeset tests go here
###
def test_manifest_len(manifest_obj):
# check that 'len' works
assert len(manifest_obj) == 8
def test_manifest_rows(manifest_obj):
# check that '.rows' property works
rows = list(manifest_obj.rows)
assert len(rows) == 8
required_keys = set(BaseCollectionManifest.required_keys)
for row in rows:
kk = set(row.keys())
assert required_keys.issubset(kk)
def test_manifest_bool(manifest_obj):
# check that 'bool' works
assert bool(manifest_obj)
def test_make_manifest_row(manifest_obj):
# build a manifest row from a signature
sig47 = utils.get_test_data('47.fa.sig')
ss = sourmash.load_one_signature(sig47)
row = manifest_obj.make_manifest_row(ss, 'foo', include_signature=False)
assert not 'signature' in row
assert row['internal_location'] == 'foo'
assert row['md5'] == ss.md5sum()
assert row['md5short'] == ss.md5sum()[:8]
assert row['ksize'] == 31
assert row['moltype'] == 'DNA'
assert row['num'] == 0
assert row['scaled'] == 1000
assert row['n_hashes'] == len(ss.minhash)
assert not row['with_abundance']
assert row['name'] == ss.name
assert row['filename'] == ss.filename
def test_manifest_create_manifest(manifest_obj):
# test the 'create_manifest' method
sig47 = utils.get_test_data('47.fa.sig')
ss = sourmash.load_one_signature(sig47)
def yield_sigs():
yield ss, 'fiz'
new_mf = manifest_obj.create_manifest(yield_sigs(),
include_signature=False)
assert len(new_mf) == 1
new_row = list(new_mf.rows)[0]
row = manifest_obj.make_manifest_row(ss, 'fiz', include_signature=False)
required_keys = BaseCollectionManifest.required_keys
for k in required_keys:
assert new_row[k] == row[k], k
def test_manifest_select_to_manifest(manifest_obj):
# do some light testing of 'select_to_manifest'
new_mf = manifest_obj.select_to_manifest(moltype='DNA')
assert len(new_mf) == 2
def test_manifest_locations(manifest_obj):
# check the 'locations' method
locs = set(['dayhoff/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig',
'dayhoff/GCA_001593935.1_ASM159393v1_protein.faa.gz.sig',
'hp/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig',
'hp/GCA_001593935.1_ASM159393v1_protein.faa.gz.sig',
'protein/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig',
'protein/GCA_001593935.1_ASM159393v1_protein.faa.gz.sig',
'dna-sig.noext',
'dna-sig.sig.gz']
)
assert set(manifest_obj.locations()) == locs
def test_manifest___contains__(manifest_obj):
# check the 'in' operator
sigfile = utils.get_test_data('prot/dayhoff/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig')
ss = sourmash.load_one_signature(sigfile)
assert ss in manifest_obj
sigfile2 = utils.get_test_data('2.fa.sig')
ss2 = sourmash.load_one_signature(sigfile2, ksize=31)
assert ss2 not in manifest_obj
def test_manifest_to_picklist(manifest_obj):
# test 'to_picklist'
picklist = manifest_obj.to_picklist()
mf = manifest_obj.select_to_manifest(picklist=picklist)
assert mf == manifest_obj
def test_manifest_filter_rows(manifest_obj):
# test filter_rows
filter_fn = lambda x: 'OS223' in x['name']
mf = manifest_obj.filter_rows(filter_fn)
assert len(mf) == 1
row = list(mf.rows)[0]
assert row['name'] == 'NC_011663.1 Shewanella baltica OS223, complete genome'
def test_manifest_filter_cols(manifest_obj):
# test filter_rows
col_filter_fn = lambda x: 'OS223' in x[0]
mf = manifest_obj.filter_on_columns(col_filter_fn, ['name'])
assert len(mf) == 1
row = list(mf.rows)[0]
assert row['name'] == 'NC_011663.1 Shewanella baltica OS223, complete genome'
def test_manifest_iadd(manifest_obj):
# test the 'create_manifest' method
sig47 = utils.get_test_data('47.fa.sig')
ss = sourmash.load_one_signature(sig47)
def yield_sigs():
yield ss, 'fiz'
new_mf = manifest_obj.create_manifest(yield_sigs(),
include_signature=False)
assert len(new_mf) == 1
new_mf += manifest_obj
assert len(new_mf) == len(manifest_obj) + 1
def test_manifest_add(manifest_obj):
# test the 'create_manifest' method
sig47 = utils.get_test_data('47.fa.sig')
ss = sourmash.load_one_signature(sig47)
def yield_sigs():
yield ss, 'fiz'
new_mf = manifest_obj.create_manifest(yield_sigs(),
include_signature=False)
assert len(new_mf) == 1
new_mf2 = new_mf + manifest_obj
assert len(new_mf2) == len(manifest_obj) + len(new_mf)