/
command_compute.py
585 lines (479 loc) · 17.9 KB
/
command_compute.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
"""
Functions implementing the 'compute' command and related functions.
"""
import os
import os.path
import sys
import random
import screed
from . import sourmash_args
from .signature import SourmashSignature
from .logging import notify, error, set_quiet
from .utils import RustObject
from ._lowlevel import ffi, lib
from .command_sketch import (
_compute_individual,
_compute_merged,
ComputeParameters,
add_seq,
set_sig_name,
DEFAULT_MMHASH_SEED,
)
def compute(args):
"""Compute the signature for one or more files.
Use cases:
sourmash compute multiseq.fa => multiseq.fa.sig, etc.
sourmash compute genome.fa --singleton => genome.fa.sig
sourmash compute file1.fa file2.fa -o file.sig
=> creates one output file file.sig, with one signature for each
input file.
sourmash compute file1.fa file2.fa --merge merged -o file.sig
=> creates one output file file.sig, with all sequences from
file1.fa and file2.fa combined into one signature.
"""
set_quiet(args.quiet)
if args.license != "CC0":
error("error: sourmash only supports CC0-licensed signatures. sorry!")
sys.exit(-1)
if args.input_is_protein and args.dna:
notify("WARNING: input is protein, turning off nucleotide hashing")
args.dna = False
args.protein = True
if args.scaled:
if args.scaled < 1:
error("ERROR: --scaled value must be >= 1")
sys.exit(-1)
if args.scaled != round(args.scaled, 0):
error("ERROR: --scaled value must be integer value")
sys.exit(-1)
if args.scaled >= 1e9:
notify("WARNING: scaled value is nonsensical!? Continuing anyway.")
if args.num_hashes != 0:
notify("setting num_hashes to 0 because --scaled is set")
args.num_hashes = 0
notify("computing signatures for files: {}", ", ".join(args.filenames))
if args.randomize:
notify("randomizing file list because of --randomize")
random.shuffle(args.filenames)
# get list of k-mer sizes for which to compute sketches
ksizes = args.ksizes
notify("Computing signature for ksizes: {}", str(ksizes))
num_sigs = 0
if args.dna and args.protein:
notify("Computing both nucleotide and protein signatures.")
num_sigs = 2 * len(ksizes)
elif args.dna and args.dayhoff:
notify("Computing both nucleotide and Dayhoff-encoded protein " "signatures.")
num_sigs = 2 * len(ksizes)
elif args.dna and args.hp:
notify("Computing both nucleotide and hp-encoded protein " "signatures.")
num_sigs = 2 * len(ksizes)
elif args.dna:
notify("Computing only nucleotide (and not protein) signatures.")
num_sigs = len(ksizes)
elif args.protein:
notify("Computing only protein (and not nucleotide) signatures.")
num_sigs = len(ksizes)
elif args.dayhoff:
notify(
"Computing only Dayhoff-encoded protein (and not nucleotide) " "signatures."
)
num_sigs = len(ksizes)
elif args.hp:
notify("Computing only hp-encoded protein (and not nucleotide) " "signatures.")
num_sigs = len(ksizes)
if args.protein or args.dayhoff or args.hp:
notify("")
notify(
"WARNING: you are using 'compute' to make a protein/dayhoff/hp signature,"
)
notify("WARNING: but the meaning of ksize has changed in 4.0. Please see the")
notify("WARNING: migration guide to sourmash v4.0 at http://sourmash.rtfd.io/")
notify("")
bad_ksizes = [str(k) for k in ksizes if k % 3 != 0]
if bad_ksizes:
error("protein ksizes must be divisible by 3, sorry!")
error("bad ksizes: {}", ", ".join(bad_ksizes))
sys.exit(-1)
notify("Computing a total of {} signature(s) for each input.", num_sigs)
if num_sigs == 0:
error("...nothing to calculate!? Exiting!")
sys.exit(-1)
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)
if args.track_abundance:
notify("Tracking abundance of input k-mers.")
signatures_factory = _signatures_for_compute_factory(args)
if args.merge: # single name specified - combine all
_compute_merged(args, signatures_factory)
else: # compute individual signatures
_compute_individual(args, signatures_factory)
class _signatures_for_compute_factory:
"Build signatures on demand, based on args input to 'compute'."
def __init__(self, args):
self.args = args
def __call__(self):
args = self.args
params = ComputeParameters(
ksizes=args.ksizes,
seed=args.seed,
protein=args.protein,
dayhoff=args.dayhoff,
hp=args.hp,
dna=args.dna,
num_hashes=args.num_hashes,
track_abundance=args.track_abundance,
scaled=args.scaled,
)
sig = SourmashSignature.from_params(params)
return [sig]
def _compute_individual(args, signatures_factory):
# this is where output signatures will go.
save_sigs = None
# track: is this the first file? in cases where we have empty inputs,
# we don't want to open any outputs.
first_file_for_output = True
# if args.output is set, we are aggregating all output to a single file.
# do not open a new output file for each input.
open_output_each_time = True
if args.output:
open_output_each_time = False
for filename in args.filenames:
if open_output_each_time:
# for each input file, construct output filename
sigfile = os.path.basename(filename) + ".sig"
if args.output_dir:
sigfile = os.path.join(args.output_dir, sigfile)
# does it already exist? skip if so.
if os.path.exists(sigfile) and not args.force:
notify("skipping {} - already done", filename)
continue # go on to next file.
# nope? ok, let's save to it.
assert not save_sigs
save_sigs = sourmash_args.SaveSignaturesToLocation(sigfile)
#
# calculate signatures!
#
# now, set up to iterate over sequences.
with screed.open(filename) as screed_iter:
if not screed_iter:
notify(f"no sequences found in '{filename}'?!")
continue
# open output for signatures
if open_output_each_time:
save_sigs.open()
# or... is this the first time to write something to args.output?
elif first_file_for_output:
save_sigs = sourmash_args.SaveSignaturesToLocation(args.output)
save_sigs.open()
first_file_for_output = False
# make a new signature for each sequence?
if args.singleton:
n_calculated = 0
for n, record in enumerate(screed_iter):
sigs = signatures_factory()
try:
add_seq(
sigs,
record.sequence,
args.input_is_protein,
args.check_sequence,
)
except ValueError as exc:
error(f"ERROR when reading from '{filename}' - ")
error(str(exc))
sys.exit(-1)
n_calculated += len(sigs)
set_sig_name(sigs, filename, name=record.name)
save_sigs_to_location(sigs, save_sigs)
notify(
"calculated {} signatures for {} sequences in {}",
n_calculated,
n + 1,
filename,
)
# nope; make a single sig for the whole file
else:
sigs = signatures_factory()
# consume & calculate signatures
notify(f"... reading sequences from {filename}")
name = None
for n, record in enumerate(screed_iter):
if n % 10000 == 0:
if n:
notify("\r...{} {}", filename, n, end="")
elif args.name_from_first:
name = record.name
try:
add_seq(
sigs,
record.sequence,
args.input_is_protein,
args.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)
save_sigs_to_location(sigs, save_sigs)
notify(
f"calculated {len(sigs)} signatures for {n+1} sequences in {filename}"
)
# if not args.output, close output for every input filename.
if open_output_each_time:
save_sigs.close()
notify(
f"saved {len(save_sigs)} signature(s) to '{save_sigs.location}'. Note: signature license is CC0."
)
save_sigs = None
# if --output-dir specified, all collected signatures => args.output,
# and we need to close here.
if args.output and save_sigs is not None:
save_sigs.close()
notify(
f"saved {len(save_sigs)} signature(s) to '{save_sigs.location}'. Note: signature license is CC0."
)
def _compute_merged(args, signatures_factory):
# make a signature for the whole file
sigs = signatures_factory()
total_seq = 0
for filename in args.filenames:
# consume & calculate signatures
notify("... reading sequences from {}", filename)
n = None
with screed.open(filename) as f:
for n, record in enumerate(f):
if n % 10000 == 0 and n:
notify("\r... {} {}", filename, n, end="")
add_seq(
sigs, record.sequence, args.input_is_protein, args.check_sequence
)
if n is not None:
notify("... {} {} sequences", filename, n + 1)
total_seq += n + 1
else:
notify(f"no sequences found in '{filename}'?!")
if total_seq:
set_sig_name(sigs, filename, name=args.merge)
notify(
"calculated 1 signature for {} sequences taken from {} files",
total_seq,
len(args.filenames),
)
# at end, save!
save_siglist(sigs, args.output)
def add_seq(sigs, seq, input_is_protein, check_sequence):
for sig in sigs:
if input_is_protein:
sig.add_protein(seq)
else:
sig.add_sequence(seq, not check_sequence)
def set_sig_name(sigs, filename, name=None):
if filename == "-": # if stdin, set filename to empty.
filename = ""
for sig in sigs:
if name is not None:
sig._name = name
sig.filename = filename
def save_siglist(siglist, sigfile_name):
"Save multiple signatures to a filename."
# save!
with sourmash_args.SaveSignaturesToLocation(sigfile_name) as save_sig:
for ss in siglist:
save_sig.add(ss)
notify(f"saved {len(save_sig)} signature(s) to '{save_sig.location}'")
def save_sigs_to_location(siglist, save_sig):
"Save multiple signatures to an already-open location."
import sourmash
for ss in siglist:
save_sig.add(ss)
class ComputeParameters(RustObject):
__dealloc_func__ = lib.computeparams_free
def __init__(
self,
*,
ksizes=(21, 31, 51),
seed=42,
protein=False,
dayhoff=False,
hp=False,
dna=True,
num_hashes=500,
track_abundance=False,
scaled=0,
):
self._objptr = lib.computeparams_new()
self.seed = seed
self.ksizes = ksizes
self.protein = protein
self.dayhoff = dayhoff
self.hp = hp
self.dna = dna
self.num_hashes = num_hashes
self.track_abundance = track_abundance
self.scaled = scaled
@classmethod
def from_manifest_row(cls, row):
"convert a CollectionManifest row into a ComputeParameters object"
is_dna = is_protein = is_dayhoff = is_hp = False
if row["moltype"] == "DNA":
is_dna = True
elif row["moltype"] == "protein":
is_protein = True
elif row["moltype"] == "hp":
is_hp = True
elif row["moltype"] == "dayhoff":
is_dayhoff = True
else:
assert 0
if is_dna:
ksize = row["ksize"]
else:
ksize = row["ksize"] * 3
p = cls(
ksizes=[ksize],
seed=DEFAULT_MMHASH_SEED,
protein=is_protein,
dayhoff=is_dayhoff,
hp=is_hp,
dna=is_dna,
num_hashes=row["num"],
track_abundance=row["with_abundance"],
scaled=row["scaled"],
)
return p
def to_param_str(self):
"Convert object to equivalent params str."
pi = []
if self.dna:
pi.append("dna")
elif self.protein:
pi.append("protein")
elif self.hp:
pi.append("hp")
elif self.dayhoff:
pi.append("dayhoff")
else:
assert 0 # must be one of the previous
if self.dna:
kstr = [f"k={k}" for k in self.ksizes]
else:
# for protein, divide ksize by three.
kstr = [f"k={k//3}" for k in self.ksizes]
assert kstr
pi.extend(kstr)
if self.num_hashes != 0:
pi.append(f"num={self.num_hashes}")
elif self.scaled != 0:
pi.append(f"scaled={self.scaled}")
else:
assert 0
if self.track_abundance:
pi.append("abund")
# noabund is default
if self.seed != DEFAULT_MMHASH_SEED:
pi.append(f"seed={self.seed}")
# self.seed
return ",".join(pi)
def __repr__(self):
return f"ComputeParameters(ksizes={self.ksizes}, seed={self.seed}, protein={self.protein}, dayhoff={self.dayhoff}, hp={self.hp}, dna={self.dna}, num_hashes={self.num_hashes}, track_abundance={self.track_abundance}, scaled={self.scaled})"
def __eq__(self, other):
return (
self.ksizes == other.ksizes
and self.seed == other.seed
and self.protein == other.protein
and self.dayhoff == other.dayhoff
and self.hp == other.hp
and self.dna == other.dna
and self.num_hashes == other.num_hashes
and self.track_abundance == other.track_abundance
and self.scaled == other.scaled
)
@staticmethod
def from_args(args):
ptr = lib.computeparams_new()
ret = ComputeParameters._from_objptr(ptr)
for arg, value in vars(args).items():
try:
getattr(type(ret), arg).fset(ret, value)
except AttributeError:
pass
return ret
@property
def seed(self):
return self._methodcall(lib.computeparams_seed)
@seed.setter
def seed(self, v):
return self._methodcall(lib.computeparams_set_seed, v)
@property
def ksizes(self):
size = ffi.new("uintptr_t *")
ksizes_ptr = self._methodcall(lib.computeparams_ksizes, size)
size = size[0]
ksizes = ffi.unpack(ksizes_ptr, size)
lib.computeparams_ksizes_free(ksizes_ptr, size)
return ksizes
@ksizes.setter
def ksizes(self, v):
return self._methodcall(lib.computeparams_set_ksizes, list(v), len(v))
@property
def protein(self):
return self._methodcall(lib.computeparams_protein)
@protein.setter
def protein(self, v):
return self._methodcall(lib.computeparams_set_protein, v)
@property
def dayhoff(self):
return self._methodcall(lib.computeparams_dayhoff)
@dayhoff.setter
def dayhoff(self, v):
return self._methodcall(lib.computeparams_set_dayhoff, v)
@property
def hp(self):
return self._methodcall(lib.computeparams_hp)
@hp.setter
def hp(self, v):
return self._methodcall(lib.computeparams_set_hp, v)
@property
def dna(self):
return self._methodcall(lib.computeparams_dna)
@dna.setter
def dna(self, v):
return self._methodcall(lib.computeparams_set_dna, v)
@property
def moltype(self):
if self.dna:
moltype = "DNA"
elif self.protein:
moltype = "protein"
elif self.hp:
moltype = "hp"
elif self.dayhoff:
moltype = "dayhoff"
else:
assert 0
return moltype
@property
def num_hashes(self):
return self._methodcall(lib.computeparams_num_hashes)
@num_hashes.setter
def num_hashes(self, v):
return self._methodcall(lib.computeparams_set_num_hashes, v)
@property
def track_abundance(self):
return self._methodcall(lib.computeparams_track_abundance)
@track_abundance.setter
def track_abundance(self, v):
return self._methodcall(lib.computeparams_set_track_abundance, v)
@property
def scaled(self):
return self._methodcall(lib.computeparams_scaled)
@scaled.setter
def scaled(self, v):
return self._methodcall(lib.computeparams_set_scaled, int(v))