/
test_cmd_signature_collect.py
404 lines (286 loc) · 14.1 KB
/
test_cmd_signature_collect.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Tests for 'sourmash sig collect'
"""
import pytest
import shutil
import os.path
import gzip
import sourmash
from sourmash.manifest import BaseCollectionManifest
import sourmash_tst_utils as utils
from sourmash_tst_utils import SourmashCommandFailed
def test_sig_collect_0_nothing(runtmp, manifest_db_format):
# run with just output
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
if manifest_db_format != 'sql': return
runtmp.sourmash('sig', 'collect', '-o', f'mf.{ext}',
'-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 0
def test_sig_collect_1_zipfile(runtmp, manifest_db_format):
# collect a manifest from a .zip file
protzip = utils.get_test_data('prot/protein.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', protzip, '-o', f'mf.{ext}',
'-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
def test_sig_collect_1_zipfile_csv_gz(runtmp):
# collect a manifest from a .zip file, save to csv.gz
protzip = utils.get_test_data('prot/protein.zip')
runtmp.sourmash('sig', 'collect', protzip, '-o', 'mf.csv.gz',
'-F', 'csv')
manifest_fn = runtmp.output('mf.csv.gz')
# gzip, yes?
print('XXX', manifest_fn)
with gzip.open(manifest_fn, 'rt', newline='') as fp:
fp.read()
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
def test_sig_collect_1_zipfile_csv_gz_roundtrip(runtmp):
# collect a manifest from a .zip file, save to csv.gz; then load again
protzip = utils.get_test_data('prot/protein.zip')
runtmp.sourmash('sig', 'collect', protzip, '-o', 'mf.csv.gz',
'-F', 'csv')
manifest_fn = runtmp.output('mf.csv.gz')
# gzip, yes?
print('XXX', manifest_fn)
with gzip.open(manifest_fn, 'rt', newline='') as fp:
fp.read()
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
# can we read a csv.gz?
runtmp.sourmash('sig', 'collect', 'mf.csv.gz', '-o', 'mf2.csv',
'-F', 'csv')
manifest_fn2 = runtmp.output('mf2.csv')
manifest2 = BaseCollectionManifest.load_from_filename(manifest_fn2)
assert len(manifest2) == 2
md5_list = [ row['md5'] for row in manifest2.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
def test_sig_collect_2_exists_fail(runtmp, manifest_db_format):
# collect a manifest from two .zip files
protzip = utils.get_test_data('prot/protein.zip')
allzip = utils.get_test_data('prot/protein.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', protzip, '-o', f'mf.{ext}',
'-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
# now run with same filename - should fail
with pytest.raises(SourmashCommandFailed):
runtmp.sourmash('sig', 'collect', allzip, '-o', manifest_fn,
'-F', manifest_db_format)
def test_sig_collect_2_exists_merge(runtmp, manifest_db_format):
# collect a manifest from two .zip files
protzip = utils.get_test_data('prot/protein.zip')
allzip = utils.get_test_data('prot/all.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', protzip, '-o', f'mf.{ext}',
'-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
# now run with same filename - should merge
runtmp.sourmash('sig', 'collect', allzip, '-o', manifest_fn,
'-F', manifest_db_format, '--merge')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 10
def test_sig_collect_2_exists_sql_merge_csv(runtmp, manifest_db_format):
# try to merge csv into sql
protzip = utils.get_test_data('prot/protein.zip')
allzip = utils.get_test_data('prot/all.zip')
ext = 'sqlmf'
# save as sql...
runtmp.sourmash('sig', 'collect', protzip, '-o', f'mf.{ext}',
'-F', 'sql')
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
with pytest.raises(SourmashCommandFailed):
runtmp.sourmash('sig', 'collect', allzip, '-o', manifest_fn,
'-F', 'csv', '--merge')
assert "ERROR loading" in runtmp.last_result.err
def test_sig_collect_2_exists_csv_merge_sql(runtmp):
# try to merge sql into csv
protzip = utils.get_test_data('prot/protein.zip')
allzip = utils.get_test_data('prot/all.zip')
ext = 'csv'
# save as csv...
runtmp.sourmash('sig', 'collect', protzip, '-o', f'mf.{ext}',
'-F', 'csv')
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
with pytest.raises(SourmashCommandFailed):
runtmp.sourmash('sig', 'collect', allzip, '-o', manifest_fn,
'-F', 'sql', '--merge')
assert "ERROR loading" in runtmp.last_result.err
def test_sig_collect_2_no_exists_merge(runtmp, manifest_db_format):
# test 'merge' when args.output doesn't already exist => warning
protzip = utils.get_test_data('prot/protein.zip')
allzip = utils.get_test_data('prot/all.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
manifest_fn = runtmp.output(f'mf.{ext}')
# run with --merge but no previous:
runtmp.sourmash('sig', 'collect', allzip, '-o', manifest_fn,
'-F', manifest_db_format, '--merge')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 8
err = runtmp.last_result.err
print(err)
assert "WARNING: --merge-previous specified, but output file" in err
def test_sig_collect_3_multiple(runtmp, manifest_db_format):
# collect a manifest from two .zip files
protzip = utils.get_test_data('prot/protein.zip')
hpzip = utils.get_test_data('prot/hp.zip')
dayzip = utils.get_test_data('prot/dayhoff.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', protzip, hpzip, dayzip,
'-o', f'mf.{ext}', '-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 6
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
assert 'ea2a1ad233c2908529d124a330bcb672' in md5_list
assert 'bb0e6d90df01b7bd5d0956a5f9e3ed12' in md5_list
assert 'fbca5e5211e4d58427997fd5c8343e9a' in md5_list
assert '1cbd888bf910f83ad8f1715509183223' in md5_list
locations = set([ row['internal_location'] for row in manifest.rows ])
assert protzip in locations
assert hpzip in locations
assert dayzip in locations
assert len(locations) == 3, locations
def test_sig_collect_3_multiple_use_fromfile(runtmp, manifest_db_format):
# collect a manifest from two .zip files using --from-file
protzip = utils.get_test_data('prot/protein.zip')
hpzip = utils.get_test_data('prot/hp.zip')
dayzip = utils.get_test_data('prot/dayhoff.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
fromfile = runtmp.output('fromfile.txt')
with open(fromfile, 'wt') as fp:
print(protzip, file=fp)
print(hpzip, file=fp)
print(dayzip, file=fp)
runtmp.sourmash('sig', 'collect', '--from-file', 'fromfile.txt',
'-o', f'mf.{ext}', '-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 6
md5_list = [ row['md5'] for row in manifest.rows ]
assert '16869d2c8a1d29d1c8e56f5c561e585e' in md5_list
assert '120d311cc785cc9d0df9dc0646b2b857' in md5_list
assert 'ea2a1ad233c2908529d124a330bcb672' in md5_list
assert 'bb0e6d90df01b7bd5d0956a5f9e3ed12' in md5_list
assert 'fbca5e5211e4d58427997fd5c8343e9a' in md5_list
assert '1cbd888bf910f83ad8f1715509183223' in md5_list
locations = set([ row['internal_location'] for row in manifest.rows ])
assert protzip in locations
assert hpzip in locations
assert dayzip in locations
assert len(locations) == 3, locations
def test_sig_collect_4_multiple_from_sig(runtmp, manifest_db_format):
# collect a manifest from sig files
sig43 = utils.get_test_data('47.fa.sig')
sig63 = utils.get_test_data('63.fa.sig')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', sig43, sig63,
'-o', f'mf.{ext}', '-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '09a08691ce52952152f0e866a59f6261' in md5_list
assert '38729c6374925585db28916b82a6f513' in md5_list
locations = set([ row['internal_location'] for row in manifest.rows ])
assert sig43 in locations
assert sig63 in locations
assert len(locations) == 2, locations
def test_sig_collect_4_multiple_from_sig_abspath(runtmp, manifest_db_format):
# collect a manifest from sig files, forcing abspath
sig43 = utils.get_test_data('47.fa.sig')
sig63 = utils.get_test_data('63.fa.sig')
shutil.copyfile(sig43, runtmp.output('47.fa.sig'))
shutil.copyfile(sig63, runtmp.output('63.fa.sig'))
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', '47.fa.sig', '63.fa.sig', '--abspath',
'-o', f'mf.{ext}', '-F', manifest_db_format)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '09a08691ce52952152f0e866a59f6261' in md5_list
assert '38729c6374925585db28916b82a6f513' in md5_list
locations = set([ row['internal_location'] for row in manifest.rows ])
print(locations)
assert len(locations) == 2, locations
for xx in locations:
assert xx.startswith('/')
def test_sig_collect_4_multiple_no_abspath(runtmp, manifest_db_format):
# collect a manifest from sig files, no abspath
sig43 = utils.get_test_data('47.fa.sig')
sig63 = utils.get_test_data('63.fa.sig')
# copy files to tmp, where they will not have full paths
shutil.copyfile(sig43, runtmp.output('47.fa.sig'))
shutil.copyfile(sig63, runtmp.output('63.fa.sig'))
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', '47.fa.sig', '63.fa.sig',
'-o', f'mf.{ext}', '-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 2
md5_list = [ row['md5'] for row in manifest.rows ]
assert '09a08691ce52952152f0e866a59f6261' in md5_list
assert '38729c6374925585db28916b82a6f513' in md5_list
locations = set([ row['internal_location'] for row in manifest.rows ])
print(locations)
assert len(locations) == 2, locations
assert '47.fa.sig' in locations
assert '63.fa.sig' in locations
def test_sig_collect_5_no_manifest_sbt_fail(runtmp, manifest_db_format):
# collect a manifest from files that don't have one
sbt_zip = utils.get_test_data('v6.sbt.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
with pytest.raises(SourmashCommandFailed):
runtmp.sourmash('sig', 'collect', sbt_zip,
'-o', f'mf.{ext}', '-F', manifest_db_format)
def test_sig_collect_5_no_manifest_sbt_succeed(runtmp, manifest_db_format):
# generate a manifest from files that don't have one when --no-require
sbt_zip = utils.get_test_data('v6.sbt.zip')
ext = 'sqlmf' if manifest_db_format == 'sql' else 'csv'
runtmp.sourmash('sig', 'collect', sbt_zip, '--no-require-manifest',
'-o', f'mf.{ext}', '-F', manifest_db_format)
manifest_fn = runtmp.output(f'mf.{ext}')
manifest = BaseCollectionManifest.load_from_filename(manifest_fn)
assert len(manifest) == 7
locations = set([ row['internal_location'] for row in manifest.rows ])
assert len(locations) == 1, locations
assert sbt_zip in locations