-
Notifications
You must be signed in to change notification settings - Fork 77
/
command_sketch.py
600 lines (479 loc) · 21.7 KB
/
command_sketch.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
"""
Functions implementing the 'sketch' subcommands and related functions.
"""
import sys
import os
from collections import defaultdict, Counter
import csv
import shlex
import screed
import sourmash
from .signature import SourmashSignature
from .logging import notify, error, set_quiet, print_results
from .command_compute import (_compute_individual, _compute_merged,
ComputeParameters, add_seq, set_sig_name,
DEFAULT_MMHASH_SEED)
from sourmash import sourmash_args
from sourmash.sourmash_args import check_scaled_bounds, check_num_bounds
from sourmash.sig.__main__ import _summarize_manifest, _SketchInfo
from sourmash.manifest import CollectionManifest
DEFAULTS = dict(
dna='k=31,scaled=1000,noabund',
protein='k=10,scaled=200,noabund',
dayhoff='k=16,scaled=200,noabund',
hp='k=42,scaled=200,noabund'
)
def _parse_params_str(params_str):
"Parse a parameter string of the form 'k=ks,num=num,scaled=scaled,abund'."
moltype = None
params = {}
params['ksize'] = []
items = params_str.split(',')
for item in items:
if item == 'abund':
params['track_abundance'] = True
elif item == 'noabund':
params['track_abundance'] = False
elif item.startswith('k'):
if len(item) < 3 or item[1] != '=':
raise ValueError("k takes a parameter, e.g. 'k=31'")
params['ksize'].append(int(item[2:]))
elif item.startswith('num'):
if len(item) < 5 or item[3] != '=':
raise ValueError("num takes a parameter, e.g. 'num=500'")
if params.get('scaled'):
raise ValueError("cannot set both num and scaled in a single minhash")
try:
num = item[4:]
num = int(num)
except ValueError:
raise ValueError(f"cannot parse num='{num}' as a number")
num = check_num_bounds(num)
params['num'] = int(item[4:])
params['scaled'] = 0
elif item.startswith('scaled'):
if len(item) < 8 or item[6] != '=':
raise ValueError("scaled takes a parameter, e.g. 'scaled=1000'")
if params.get('num'):
raise ValueError("cannot set both num and scaled in a single minhash")
try:
scaled = item[7:]
scaled = int(scaled)
except ValueError:
raise ValueError(f"cannot parse scaled='{scaled}' as an integer")
scaled = check_scaled_bounds(scaled)
params['scaled'] = scaled
params['num'] = 0
elif item.startswith('seed'):
if len(item) < 6 or item[4] != '=':
raise ValueError("seed takes a parameter, e.g. 'seed=42'")
params['seed'] = int(item[5:])
elif item in ('protein', 'dayhoff', 'hp', 'dna'):
moltype = item
else:
raise ValueError(f"unknown component '{item}' in params string")
return moltype, params
class _signatures_for_sketch_factory:
"Build sigs on demand, based on args input to 'sketch'."
def __init__(self, params_str_list, default_moltype):
# first, set up defaults per-moltype
defaults = {}
for moltype, pstr in DEFAULTS.items():
mt, d = _parse_params_str(pstr)
assert mt is None # defaults cannot have moltype set!
defaults[moltype] = d
self.defaults = defaults
# next, fill out params_list
self.params_list = []
self.mult_ksize_by_3 = True
if params_str_list:
# parse each params_str passed in, using default_moltype if none
# provided.
for params_str in params_str_list:
moltype, params = _parse_params_str(params_str)
if moltype and moltype != 'dna' and default_moltype == 'dna':
raise ValueError(f"Incompatible sketch type ({default_moltype}) and parameter override ({moltype}) in '{params_str}'; maybe use 'sketch translate'?")
elif moltype == 'dna' and default_moltype and default_moltype != 'dna':
raise ValueError(f"Incompatible sketch type ({default_moltype}) and parameter override ({moltype}) in '{params_str}'")
elif moltype is None:
if default_moltype is None:
raise ValueError(f"No default moltype and none specified in param string")
moltype = default_moltype
self.params_list.append((moltype, params))
else:
if default_moltype is None:
raise ValueError(f"No default moltype and none specified in param string")
# no params str? default to a single sig, using default_moltype.
self.params_list.append((default_moltype, {}))
def get_compute_params(self, *, split_ksizes=False):
for moltype, params_d in self.params_list:
# get defaults for this moltype from self.defaults:
default_params = self.defaults[moltype]
def_seed = default_params.get('seed', DEFAULT_MMHASH_SEED)
def_num = default_params.get('num', 0)
def_abund = default_params['track_abundance']
def_scaled = default_params.get('scaled', 0)
def_dna = default_params.get('is_dna', moltype == 'dna')
def_protein = default_params.get('is_protein',
moltype == 'protein')
def_dayhoff = default_params.get('is_dayhoff',
moltype == 'dayhoff')
def_hp = default_params.get('is_hp', moltype == 'hp')
# handle ksize specially, for now - multiply by three?
def_ksizes = default_params['ksize']
ksizes = params_d.get('ksize')
if not ksizes:
ksizes = def_ksizes
# 'command sketch' adjusts k-mer sizes by 3 if non-DNA sketch.
if self.mult_ksize_by_3 and not def_dna:
ksizes = [ k*3 for k in ksizes ]
make_param = lambda ksizes: ComputeParameters(
ksizes=ksizes,
seed=params_d.get('seed', def_seed),
protein=def_protein,
dayhoff=def_dayhoff,
hp=def_hp,
dna=def_dna,
num_hashes=params_d.get('num', def_num),
track_abundance=params_d.get('track_abundance',
def_abund),
scaled=params_d.get('scaled', def_scaled))
if split_ksizes:
for ksize in ksizes:
params_obj = make_param([ksize])
yield params_obj
else:
params_obj = make_param(ksizes)
yield params_obj
def __call__(self, *, split_ksizes=False):
"Produce a new set of signatures built to match the param strings."
sigs = []
for params in self.get_compute_params(split_ksizes=split_ksizes):
sig = SourmashSignature.from_params(params)
sigs.append(sig)
return sigs
def _add_from_file_to_filenames(args):
"Add filenames from --from-file to args.filenames"
from .sourmash_args import load_pathlist_from_file
if args.from_file:
file_list = load_pathlist_from_file(args.from_file)
args.filenames.extend(file_list)
def _execute_sketch(args, signatures_factory):
"Once configured, run 'sketch' the same way underneath."
set_quiet(args.quiet)
if not args.filenames:
error('error: no input filenames provided! nothing to do - exiting.')
sys.exit(-1)
if args.license != 'CC0':
error('error: sourmash only supports CC0-licensed signatures. sorry!')
sys.exit(-1)
notify(f'computing signatures for files: {", ".join(args.filenames)}')
if args.merge and not args.output:
error("ERROR: must specify -o with --merge")
sys.exit(-1)
if args.output and args.output_dir:
error("ERROR: --output-dir doesn't make sense with -o/--output")
sys.exit(-1)
# get number of output sigs:
num_sigs = len(signatures_factory.params_list)
notify(f'Computing a total of {num_sigs} signature(s) for each input.')
if num_sigs == 0:
error('...nothing to calculate!? Exiting!')
sys.exit(-1)
if args.merge: # single name specified - combine all
_compute_merged(args, signatures_factory)
else: # compute individual signatures
_compute_individual(args, signatures_factory)
def dna(args):
"""Compute a DNA signature for one or more files.
CTB: make usable via Python?
"""
# for dna:
args.input_is_protein = False
try:
signatures_factory = _signatures_for_sketch_factory(args.param_string,
'dna')
except ValueError as e:
error(f"Error creating signatures: {str(e)}")
sys.exit(-1)
_add_from_file_to_filenames(args)
_execute_sketch(args, signatures_factory)
def protein(args):
"""Compute a protein signature for one or more files.
CTB: make usable via Python?
"""
# for protein:
args.input_is_protein = True
args.check_sequence = False
# provide good defaults for dayhoff/hp/protein!
if args.dayhoff and args.hp:
raise ValueError("cannot set both --dayhoff and --hp")
if args.dayhoff:
moltype = 'dayhoff'
elif args.hp:
moltype = 'hp'
else:
moltype = 'protein'
try:
signatures_factory = _signatures_for_sketch_factory(args.param_string,
moltype)
except ValueError as e:
error(f"Error creating signatures: {str(e)}")
sys.exit(-1)
_add_from_file_to_filenames(args)
_execute_sketch(args, signatures_factory)
def translate(args):
"""Compute protein signatures from DNA/RNA, for one or more files.
CTB: make usable via Python?
"""
# for translate:
args.input_is_protein = False
# provide good defaults for dayhoff/hp/protein!
if args.dayhoff and args.hp:
raise ValueError("cannot set both --dayhoff and --hp")
if args.dayhoff:
moltype = 'dayhoff'
elif args.hp:
moltype = 'hp'
else:
moltype = 'protein'
try:
signatures_factory = _signatures_for_sketch_factory(args.param_string,
moltype)
except ValueError as e:
error(f"Error creating signatures: {str(e)}")
sys.exit(-1)
_add_from_file_to_filenames(args)
_execute_sketch(args, signatures_factory)
def _compute_sigs(to_build, output, *, check_sequence=False):
"actually build the signatures in 'to_build' and output them to 'output'"
save_sigs = sourmash_args.SaveSignaturesToLocation(output)
save_sigs.open()
for (name, filename), param_objs in to_build.items():
assert param_objs
# now, set up to iterate over sequences.
with screed.open(filename) as screed_iter:
if not screed_iter:
error(f"ERROR: no sequences found in '{filename}'?!")
sys.exit(-1)
# build the set of empty sigs
sigs = []
is_dna = param_objs[0].dna
for p in param_objs:
if p.dna: assert is_dna
sig = SourmashSignature.from_params(p)
sigs.append(sig)
input_is_protein = not is_dna
# read sequence records & sketch
notify(f'... reading sequences from {filename}')
for n, record in enumerate(screed_iter):
if n % 10000 == 0:
if n:
notify('\r...{} {}', filename, n, end='')
try:
add_seq(sigs, record.sequence, input_is_protein,
check_sequence)
except ValueError as exc:
error(f"ERROR when reading from '{filename}' - ")
error(str(exc))
sys.exit(-1)
notify('...{} {} sequences', filename, n, end='')
set_sig_name(sigs, filename, name)
for sig in sigs:
save_sigs.add(sig)
notify(f'calculated {len(sigs)} signatures for {n+1} sequences in {filename}')
save_sigs.close()
notify(f"saved {len(save_sigs)} signature(s) to '{save_sigs.location}'. Note: signature license is CC0.")
def _output_csv_info(filename, sigs_to_build):
"output information about what signatures to build, in CSV format"
output_n = 0
with sourmash_args.FileOutputCSV(filename) as csv_fp:
w = csv.DictWriter(csv_fp, fieldnames=['filename', 'sketchtype',
'output_index', 'name',
'param_strs'])
w.writeheader()
output_n = 0
for (name, filename), param_objs in sigs_to_build.items():
param_strs = []
# should all be the same!
if param_objs[0].dna:
assert all( ( p.dna for p in param_objs ) )
sketchtype = "dna"
else:
assert not any( ( p.dna for p in param_objs ) )
sketchtype = "protein"
for p in param_objs:
param_strs.append(p.to_param_str())
row = dict(filename=filename, sketchtype=sketchtype,
param_strs="-p " + " -p ".join(param_strs),
name=name, output_index=output_n)
w.writerow(row)
output_n += 1
def fromfile(args):
if args.license != 'CC0':
error('error: sourmash only supports CC0-licensed signatures. sorry!')
sys.exit(-1)
if args.output_signatures and os.path.exists(args.output_signatures):
if not args.force_output_already_exists:
error(f"** ERROR: output location '{args.output_signatures}' already exists!")
error(f"** Not overwriting/appending.")
error(f"** Use --force-output-already-exists if you want to overwrite/append.")
sys.exit(-1)
# now, create the set of desired sketch specs.
try:
# omit a default moltype - must be provided in param string.
sig_factory = _signatures_for_sketch_factory(args.param_string, None)
except ValueError as e:
error(f"Error creating signatures: {str(e)}")
sys.exit(-1)
# take the signatures factory => convert into a bunch of ComputeParameters
# objects.
build_params = list(sig_factory.get_compute_params(split_ksizes=True))
# confirm that they do not adjust seed, which is not supported in
# 'fromfile' b/c we don't store that info in manifests. (see #1849)
for p in build_params:
if p.seed != DEFAULT_MMHASH_SEED:
error("** ERROR: cannot set 'seed' in 'sketch fromfile'")
sys.exit(-1)
# cross-product all of the names in the input CSV file
# with the sketch spec(s) provided on the command line.
to_build = defaultdict(list)
all_names = {}
total_rows = 0
skipped_sigs = 0
n_missing_name = 0
n_duplicate_name = 0
duplicate_names = set()
for csvfile in args.csvs:
with sourmash_args.FileInputCSV(csvfile) as r:
for row in r:
name = row['name']
if not name:
n_missing_name += 1
continue
genome = row['genome_filename']
proteome = row['protein_filename']
total_rows += 1
if name in all_names:
n_duplicate_name += 1
duplicate_names.add(name)
else:
all_names[name] = (genome, proteome)
fail_exit = False
if n_duplicate_name:
if args.report_duplicated:
notify("duplicated:\n" + '\n'.join(sorted(duplicate_names)))
error(f"** ERROR: {n_duplicate_name} entries have duplicate 'name' records. Exiting!")
fail_exit = True
if n_missing_name:
error(f"** ERROR: {n_missing_name} entries have blank 'name's? Exiting!")
fail_exit = True
if fail_exit:
sys.exit(-1)
# load manifests from '--already-done' databases => turn into
# ComputeParameters objects, indexed by name.
already_done = defaultdict(list)
already_done_rows = []
for filename in args.already_done:
idx = sourmash.load_file_as_index(filename)
manifest = idx.manifest
assert manifest
# for each manifest row,
for row in manifest.rows:
name = row['name']
if name:
# build a ComputeParameters object for later comparison
p = ComputeParameters.from_manifest_row(row)
# add to list for this name
already_done[name].append(p)
# matching name? check if we already have sig. if so, store!
if name in all_names:
if p in build_params:
already_done_rows.append(row)
already_done_manifest = CollectionManifest(already_done_rows)
if args.already_done:
notify(f"Loaded {len(already_done)} pre-existing names from manifest(s)")
notify(f"collected {len(already_done_rows)} rows for already-done signatures.")
## now check which are already done and track only those that are
## need to be done.
total_sigs = 0
missing = defaultdict(list)
missing_count = 0
for name, (genome, proteome) in all_names.items():
plist = already_done.get(name, [])
# check list of already done against build parameters
for p in build_params:
total_sigs += 1
# does this signature already exist?
if p not in plist:
# nope - figure out genome/proteome needed
filename = genome if p.dna else proteome
filetype = 'genome' if p.dna else 'proteome'
if filename:
# add to build list
to_build[(name, filename)].append(p)
else:
notify(f"WARNING: fromfile entry '{name}' is missing a {filetype}")
missing[name].append(p)
missing_count += 1
else:
skipped_sigs += 1
## we now have 'to_build' which contains the things we can build,
## and 'missing', which contains anything we cannot build. Report!
notify(f"Read {total_rows} rows, requesting that {total_sigs} signatures be built.")
if already_done_manifest:
info_d = _summarize_manifest(already_done_manifest)
print_results('---')
print_results("summary of already-done sketches:")
for ski in info_d['sketch_info']:
mh_type = f"num={ski['num']}" if ski['num'] else f"scaled={ski['scaled']}"
mh_abund = ", abund" if ski['abund'] else ""
sketch_str = f"{ski['count']} sketches with {ski['moltype']}, k={ski['ksize']}, {mh_type}{mh_abund}"
print_results(f" {sketch_str: <50} {ski['n_hashes']} total hashes")
print_results('---')
if args.output_manifest_matching:
already_done_manifest.write_to_filename(args.output_manifest_matching)
notify(f"output {len(already_done_manifest)} already-done signatures to '{args.output_manifest_matching}' in manifest format.")
if missing:
error("** ERROR: we cannot build some of the requested signatures.")
error(f"** {missing_count} total signatures (for {len(missing)} names) cannot be built.")
if args.ignore_missing:
error("** (continuing past this error because --ignore-missing was set)")
else:
sys.exit(-1)
notify(f"** {total_sigs - skipped_sigs} new signatures to build from {len(to_build)} files;")
if not to_build:
notify(f"** Nothing to build. Exiting!")
sys.exit(0)
if skipped_sigs:
notify(f"** {skipped_sigs} already exist, so skipping those.")
else:
notify(f"** we found no pre-existing signatures that match.")
## first, print out a summary of to_build:
print_results('---')
print_results("summary of sketches to build:")
counter = Counter()
build_info_d = {}
for filename, param_objs in to_build.items():
for p in param_objs:
moltype = p.moltype
assert len(p.ksizes) == 1
ksize = p.ksizes[0]
if not p.dna: ksize //= 3
ski = _SketchInfo(ksize=ksize, moltype=p.moltype,
scaled=p.scaled, num=p.num_hashes,
abund=p.track_abundance)
counter[ski] += 1
for ski, count in counter.items():
mh_type = f"num={ski.num}" if ski.num else f"scaled={ski.scaled}"
mh_abund = ", abund" if ski.abund else ""
sketch_str = f"{count} sketches with {ski.moltype}, k={ski.ksize}, {mh_type}{mh_abund}"
print_results(f" {sketch_str: <50}")
print_results('---')
## now, onward ho - do we build anything, or output stuff, or just exit?
if args.output_signatures: # actually compute
_compute_sigs(to_build, args.output_signatures,
check_sequence=args.check_sequence)
if args.output_csv_info: # output info necessary to construct
_output_csv_info(args.output_csv_info, to_build)
notify(f"** {total_sigs} total requested; output {total_sigs - skipped_sigs}, skipped {skipped_sigs}")