-
Notifications
You must be signed in to change notification settings - Fork 78
/
__init__.py
1169 lines (911 loc) · 40.9 KB
/
__init__.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""An Abstract Base Class for collections of signatures, plus implementations.
APIs and functionality
----------------------
Index classes support three sets of API functionality -
'select(...)', which selects subsets of signatures based on ksize, moltype,
and other criteria, including picklists.
'find(...)', and the 'search', 'gather', and 'counter_gather' implementations
built on top of 'find', which search for signatures that match a query.
'signatures()', which yields all signatures in the Index subject to the
selection criteria.
Classes defined in this file
----------------------------
Index - abstract base class for all Index objects.
LinearIndex - simple in-memory storage of signatures.
LazyLinearIndex - lazy selection and linear search of signatures.
ZipFileLinearIndex - simple on-disk storage of signatures.
MultiIndex - in-memory storage and selection of signatures from multiple
index objects, using manifests. All signatures are kept in memory.
StandaloneManifestIndex - load manifests directly, and do lazy loading of
signatures on demand. No signatures are kept in memory.
CounterGather - an ancillary class returned by the 'counter_gather()' method.
"""
import os
import sourmash
from abc import abstractmethod, ABC
from collections import namedtuple, Counter
from sourmash.search import (make_jaccard_search_query,
make_containment_query,
calc_threshold_from_bp)
from sourmash.manifest import CollectionManifest
from sourmash.logging import debug_literal
from sourmash.signature import load_signatures, save_signatures
from sourmash.minhash import (flatten_and_downsample_scaled,
flatten_and_downsample_num,
flatten_and_intersect_scaled)
# generic return tuple for Index.search and Index.gather
IndexSearchResult = namedtuple('Result', 'score, signature, location')
class Index(ABC):
# this will be removed soon; see sourmash#1894.
is_database = False
# 'manifest', when set, implies efficient selection and direct
# access to signatures. Signatures may be stored in the manifest
# or loaded on demand from disk depending on the class, however.
manifest = None
@abstractmethod
def __len__(self):
"Return the number of signatures in this Index object."
@property
def location(self):
"Return a resolvable location for this index, if possible."
return None
@abstractmethod
def signatures(self):
"Return an iterator over all signatures in the Index object."
def signatures_with_location(self):
"Return an iterator over tuples (signature, location) in the Index."
for ss in self.signatures():
yield ss, self.location
def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, internal_location).
Unlike 'signatures_with_location()', this iterator should return
_all_ signatures in the object, not just those that remain after
selection/filtering.
This is an internal API for use in generating manifests, and may
change without warning.
This method should be implemented separately for each Index object.
"""
raise NotImplementedError
@abstractmethod
def insert(self, signature):
""" """
@abstractmethod
def save(self, path, storage=None, sparseness=0.0, structure_only=False):
""" """
@classmethod
@abstractmethod
def load(cls, location, leaf_loader=None, storage=None,
print_version_warning=True):
""" """
def find(self, search_fn, query, **kwargs):
"""Use search_fn to find matching signatures in the index.
search_fn follows the protocol in JaccardSearch objects.
Generator. Returns 0 or more IndexSearchResult objects.
"""
# first: is this query compatible with this search?
search_fn.check_is_compatible(query)
# ok! continue!
# this set of signatures may be heterogenous in scaled/num values;
# define some processing functions to downsample appropriately.
query_mh = query.minhash
assert not query_mh.track_abundance
if query_mh.scaled:
# make query and subject compatible w/scaled.
query_scaled = query_mh.scaled
def prepare_subject(subj_mh):
return flatten_and_downsample_scaled(subj_mh, query_scaled)
def prepare_query(query_mh, subj_mh):
return flatten_and_downsample_scaled(query_mh, subj_mh.scaled)
else: # num
query_num = query_mh.num
def prepare_subject(subj_mh):
return flatten_and_downsample_num(subj_mh, query_num)
def prepare_query(query_mh, subj_mh):
return flatten_and_downsample_num(query_mh, subj_mh.num)
# now, do the search!
for subj, location in self.signatures_with_location():
subj_mh = prepare_subject(subj.minhash)
# note: we run prepare_query here on the original query minhash.
query_mh = prepare_query(query.minhash, subj_mh)
assert not query_mh.track_abundance
assert not subj_mh.track_abundance
shared_size, total_size = query_mh.intersection_and_union_size(subj_mh)
query_size = len(query_mh)
subj_size = len(subj_mh)
score = search_fn.score_fn(query_size,
shared_size,
subj_size,
total_size)
if search_fn.passes(score):
# note: here we yield the original signature, not the
# downsampled minhash.
if search_fn.collect(score, subj):
yield IndexSearchResult(score, subj, location)
def search_abund(self, query, *, threshold=None, **kwargs):
"""Return list of IndexSearchResult with angular similarity above 'threshold'.
Results will be sorted by similarity, highest to lowest.
"""
if not query.minhash.track_abundance:
raise TypeError("'search_abund' requires query signature with abundance information")
# check arguments
if threshold is None:
raise TypeError("'search_abund' requires 'threshold'")
threshold = float(threshold)
# do the actual search:
matches = []
for subj, loc in self.signatures_with_location():
if not subj.minhash.track_abundance:
raise TypeError("'search_abund' requires subject signatures with abundance information")
score = query.similarity(subj, downsample=True)
if score >= threshold:
matches.append(IndexSearchResult(score, subj, loc))
# sort!
matches.sort(key=lambda x: -x.score)
return matches
def search(self, query, *, threshold=None,
do_containment=False, do_max_containment=False,
best_only=False, **kwargs):
"""Return list of IndexSearchResult with similarity above 'threshold'.
Results will be sorted by similarity, highest to lowest.
Optional arguments accepted by all Index subclasses:
* do_containment: default False. If True, use Jaccard containment.
* best_only: default False. If True, allow optimizations that
may. May discard matches better than threshold, but first match
is guaranteed to be best.
"""
# check arguments
if threshold is None:
raise TypeError("'search' requires 'threshold'")
threshold = float(threshold)
search_obj = make_jaccard_search_query(do_containment=do_containment,
do_max_containment=do_max_containment,
best_only=best_only,
threshold=threshold)
# do the actual search:
matches = list(self.find(search_obj, query, **kwargs))
# sort!
matches.sort(key=lambda x: -x.score)
return matches
def prefetch(self, query, threshold_bp, **kwargs):
"""Return all matches with minimum overlap.
Generator. Returns 0 or more IndexSearchResult namedtuples.
"""
if not self: # empty database? quit.
raise ValueError("no signatures to search")
# default best_only to False
best_only = kwargs.get('best_only', False)
search_fn = make_containment_query(query.minhash, threshold_bp,
best_only=best_only)
for sr in self.find(search_fn, query, **kwargs):
yield sr
def best_containment(self, query, threshold_bp=None, **kwargs):
"""Return the match with the best Jaccard containment in the Index.
Returns an IndexSearchResult namedtuple or None.
"""
results = self.prefetch(query, threshold_bp, best_only=True, **kwargs)
results = sorted(results,
key=lambda x: (-x.score, x.signature.md5sum()))
try:
return next(iter(results))
except StopIteration:
return None
def peek(self, query_mh, *, threshold_bp=0):
"""Mimic CounterGather.peek() on top of Index.
This is implemented for situations where we don't want to use
'prefetch' functionality. It is a light wrapper around the
'best_containment(...)' method.
"""
from sourmash import SourmashSignature
# build a signature to use with self.gather...
query_ss = SourmashSignature(query_mh)
# run query!
try:
result = self.best_containment(query_ss, threshold_bp=threshold_bp)
except ValueError:
result = None
if not result:
return []
# if matches, calculate intersection & return.
intersect_mh = flatten_and_intersect_scaled(result.signature.minhash,
query_mh)
return [result, intersect_mh]
def consume(self, intersect_mh):
"Mimic CounterGather.consume on top of Index. Yes, this is backwards."
pass
def counter_gather(self, query, threshold_bp, **kwargs):
"""Returns an object that permits 'gather' on top of the
current contents of this Index.
The default implementation uses `prefetch` underneath, and returns
the results in a `CounterGather` object. However, alternate
implementations need only return an object that meets the
public `CounterGather` interface, of course.
"""
with query.update() as prefetch_query:
prefetch_query.minhash = prefetch_query.minhash.flatten()
# find all matches and construct a CounterGather object.
counter = CounterGather(prefetch_query)
for result in self.prefetch(prefetch_query, threshold_bp, **kwargs):
counter.add(result.signature, location=result.location)
# tada!
return counter
@abstractmethod
def select(self, ksize=None, moltype=None, scaled=None, num=None,
abund=None, containment=None):
"""Return Index containing only signatures that match requirements.
Current arguments can be any or all of:
* ksize
* moltype
* scaled
* num
* containment
'select' will raise ValueError if the requirements are incompatible
with the Index subclass.
'select' may return an empty object or None if no matches can be
found.
"""
def select_signature(ss, *, ksize=None, moltype=None, scaled=0, num=0,
containment=False, abund=None, picklist=None):
"Check that the given signature matches the specified requirements."
# ksize match?
if ksize and ksize != ss.minhash.ksize:
return False
# moltype match?
if moltype and moltype != ss.minhash.moltype:
return False
# containment requires scaled; similarity does not.
if containment:
if not scaled:
raise ValueError("'containment' requires 'scaled' in Index.select'")
if not ss.minhash.scaled:
return False
# 'scaled' and 'num' are incompatible
if scaled:
if ss.minhash.num:
return False
if num:
# note, here we check if 'num' is identical; this can be
# changed later.
if ss.minhash.scaled or num != ss.minhash.num:
return False
if abund:
# note: minhash w/abund can always be flattened
if not ss.minhash.track_abundance:
return False
if picklist is not None and ss not in picklist:
return False
return True
class LinearIndex(Index):
"""An Index for a collection of signatures. Can load from a .sig file.
Note: See MultiIndex for an in-memory class that uses manifests.
Concrete class; signatures held in memory; does not use manifests.
"""
def __init__(self, _signatures=None, filename=None):
self._signatures = []
if _signatures:
self._signatures = list(_signatures)
self.filename = filename
@property
def location(self):
return self.filename
def signatures(self):
return iter(self._signatures)
def __bool__(self):
return bool(self._signatures)
def __len__(self):
return len(self._signatures)
def insert(self, node):
self._signatures.append(node)
def save(self, path):
with open(path, 'wt') as fp:
save_signatures(self.signatures(), fp)
@classmethod
def load(cls, location, filename=None):
"Load signatures from a JSON signature file."
si = load_signatures(location, do_raise=True)
if filename is None:
filename=location
lidx = LinearIndex(si, filename=filename)
return lidx
def select(self, **kwargs):
"""Return new LinearIndex containing only signatures that match req's.
Does not raise ValueError, but may return an empty Index.
"""
siglist = []
for ss in self._signatures:
if select_signature(ss, **kwargs):
siglist.append(ss)
return LinearIndex(siglist, self.location)
class LazyLinearIndex(Index):
"""An Index for lazy linear search of another database.
Wrapper class; does not use manifests.
One of the main purposes of this class is to _force_ linear 'find'
on index objects. So if this class wraps an SBT, for example, the
SBT find method will be overriden with the linear 'find' from the
base class. There are very few situations where this is an improvement,
so use this class wisely!
A few notes:
* selection criteria defined by 'select' are only executed when
signatures are actually requested (hence, 'lazy').
* this class stores the provided index 'db' in memory. If you need
a class that does lazy loading of signatures from disk and does not
store signatures in memory, see StandaloneManifestIndex.
* if you want efficient manifest-based selection, consider
MultiIndex (signatures in memory).
"""
def __init__(self, db, selection_dict={}):
self.db = db
self.selection_dict = dict(selection_dict)
def signatures(self):
"Return the selected signatures."
db = self.db.select(**self.selection_dict)
for ss in db.signatures():
yield ss
def signatures_with_location(self):
"Return the selected signatures, with a location."
db = self.db.select(**self.selection_dict)
for tup in db.signatures_with_location():
yield tup
def __bool__(self):
try:
next(iter(self.signatures()))
return True
except StopIteration:
return False
def __len__(self):
db = self.db.select(**self.selection_dict)
return len(db)
def insert(self, node):
raise NotImplementedError
def save(self, path):
raise NotImplementedError
@classmethod
def load(cls, path):
raise NotImplementedError
def select(self, **kwargs):
"""Return new object yielding only signatures that match req's.
Does not raise ValueError, but may return an empty Index.
"""
selection_dict = dict(self.selection_dict)
for k, v in kwargs.items():
if k in selection_dict:
if selection_dict[k] != v:
raise ValueError(f"cannot select on two different values for {k}")
selection_dict[k] = v
return LazyLinearIndex(self.db, selection_dict)
class ZipFileLinearIndex(Index):
"""\
A read-only collection of signatures in a zip file.
Does not support `insert` or `save`.
Concrete class; signatures dynamically loaded from disk; uses manifests.
"""
is_database = True
def __init__(self, storage, *, selection_dict=None,
traverse_yield_all=False, manifest=None, use_manifest=True):
self.storage = storage
self.selection_dict = selection_dict
self.traverse_yield_all = traverse_yield_all
self.use_manifest = use_manifest
# do we have a manifest already? if not, try loading.
if use_manifest:
if manifest is not None:
debug_literal('ZipFileLinearIndex using passed-in manifest')
self.manifest = manifest
else:
self._load_manifest()
else:
self.manifest = None
if self.manifest is not None:
assert not self.selection_dict, self.selection_dict
if self.selection_dict:
assert self.manifest is None
def _load_manifest(self):
"Load a manifest if one exists"
try:
manifest_data = self.storage.load('SOURMASH-MANIFEST.csv')
except (KeyError, FileNotFoundError):
self.manifest = None
else:
debug_literal(f'found manifest on load for {self.storage.path}')
# load manifest!
from io import StringIO
manifest_data = manifest_data.decode('utf-8')
manifest_fp = StringIO(manifest_data)
self.manifest = CollectionManifest.load_from_csv(manifest_fp)
def __bool__(self):
"Are there any matching signatures in this zipfile? Avoid calling len."
try:
next(iter(self.signatures()))
except StopIteration:
return False
return True
def __len__(self):
"calculate number of signatures."
# use manifest, if available.
m = self.manifest
if self.manifest is not None:
return len(m)
# otherwise, iterate across all signatures.
n = 0
for _ in self.signatures():
n += 1
return n
@property
def location(self):
return self.storage.path
def insert(self, signature):
raise NotImplementedError
def save(self, path):
raise NotImplementedError
@classmethod
def load(cls, location, traverse_yield_all=False, use_manifest=True):
"Class method to load a zipfile."
from ..sbt_storage import ZipStorage
# we can only load from existing zipfiles in this method.
if not os.path.exists(location):
raise FileNotFoundError(location)
storage = ZipStorage(location)
return cls(storage, traverse_yield_all=traverse_yield_all,
use_manifest=use_manifest)
def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, internal_location).
Note: does not limit signatures to subsets.
"""
# list all the files, without using the Storage interface; currently,
# 'Storage' does not provide a way to list all the files, so :shrug:.
for filename in self.storage._filenames():
# should we load this file? if it ends in .sig OR we are forcing:
if filename.endswith('.sig') or \
filename.endswith('.sig.gz') or \
self.traverse_yield_all:
sig_data = self.storage.load(filename)
for ss in load_signatures(sig_data):
yield ss, filename
def signatures(self):
"Load all signatures in the zip file."
selection_dict = self.selection_dict
manifest = None
if self.manifest is not None:
manifest = self.manifest
assert not selection_dict
# yield all signatures found in manifest
for filename in manifest.locations():
data = self.storage.load(filename)
for ss in load_signatures(data):
# in case multiple signatures are in the file, check
# to make sure we want to return each one.
if ss in manifest:
yield ss
# no manifest! iterate.
else:
storage = self.storage
# if no manifest here, break Storage class encapsulation
# and go for all the files. (This is necessary to support
# ad-hoc zipfiles that have no manifests.)
for filename in storage._filenames():
# should we load this file? if it ends in .sig OR force:
if filename.endswith('.sig') or \
filename.endswith('.sig.gz') or \
self.traverse_yield_all:
if selection_dict:
select = lambda x: select_signature(x,
**selection_dict)
else:
select = lambda x: True
data = self.storage.load(filename)
for ss in load_signatures(data):
if select(ss):
yield ss
def select(self, **kwargs):
"Select signatures in zip file based on ksize/moltype/etc."
# if we have a manifest, run 'select' on the manifest.
manifest = self.manifest
traverse_yield_all = self.traverse_yield_all
if manifest is not None:
manifest = manifest.select_to_manifest(**kwargs)
return ZipFileLinearIndex(self.storage,
selection_dict=None,
traverse_yield_all=traverse_yield_all,
manifest=manifest,
use_manifest=True)
else:
# no manifest? just pass along all the selection kwargs to
# the new ZipFileLinearIndex.
assert manifest is None
if self.selection_dict:
# combine selects...
d = dict(self.selection_dict)
for k, v in kwargs.items():
if k in d:
if d[k] is not None and d[k] != v:
raise ValueError(f"incompatible select on '{k}'")
d[k] = v
kwargs = d
return ZipFileLinearIndex(self.storage,
selection_dict=kwargs,
traverse_yield_all=traverse_yield_all,
manifest=None,
use_manifest=False)
class CounterGather:
"""This is an ancillary class that is used to implement "fast
gather", post-prefetch. It tracks and summarize matches for
efficient min-set-cov/'gather'.
The class constructor takes a query MinHash that must be scaled, and
then takes signatures that have overlaps with the query (via 'add').
After all overlapping signatures have been loaded, the 'peek'
method is then used at each stage of the 'gather' procedure to
find the best match, and the 'consume' method is used to remove
a match from this counter.
This particular implementation maintains a collections.Counter that
is used to quickly find the best match when 'peek' is called, but
other implementations are possible ;).
Note that redundant matches (SourmashSignature objects) with
duplicate md5s are collapsed inside the class, because we use the
md5sum as a key into the dictionary used to store matches.
"""
def __init__(self, query):
"Constructor - takes a query SourmashSignature."
query_mh = query.minhash
if not query_mh.scaled:
raise ValueError('gather requires scaled signatures')
# track query
self.orig_query_mh = query_mh.copy().flatten()
self.scaled = query_mh.scaled
# use these to track loaded matches & their locations
self.siglist = {}
self.locations = {}
# ...and also track overlaps with the progressive query
self.counter = Counter()
# fence to make sure we do add matches once query has started.
self.query_started = 0
def add(self, ss, *, location=None, require_overlap=True):
"Add this signature in as a potential match."
if self.query_started:
raise ValueError("cannot add more signatures to counter after peek/consume")
# upon insertion, count & track overlap with the specific query.
overlap = self.orig_query_mh.count_common(ss.minhash, True)
if overlap:
md5 = ss.md5sum()
self.counter[md5] = overlap
self.siglist[md5] = ss
self.locations[md5] = location
# note: scaled will be max of all matches.
self.downsample(ss.minhash.scaled)
elif require_overlap:
raise ValueError("no overlap between query and signature!?")
def downsample(self, scaled):
"Track highest scaled across all possible matches."
if scaled > self.scaled:
self.scaled = scaled
return self.scaled
def signatures(self):
"Return all signatures."
for ss in self.siglist.values():
yield ss
@property
def union_found(self):
"""Return a MinHash containing all found hashes in the query.
This calculates the union of the found matches, intersected
with the original query.
"""
orig_query_mh = self.orig_query_mh
# create empty MinHash from orig query
found_mh = orig_query_mh.copy_and_clear()
# for each match, intersect match with query & then add to found_mh.
for ss in self.siglist.values():
intersect_mh = flatten_and_intersect_scaled(ss.minhash,
orig_query_mh)
found_mh.add_many(intersect_mh)
return found_mh
def peek(self, cur_query_mh, *, threshold_bp=0):
"Get next 'gather' result for this database, w/o changing counters."
self.query_started = 1
# empty? nothing to search.
counter = self.counter
if not counter:
return []
siglist = self.siglist
assert siglist
scaled = self.downsample(cur_query_mh.scaled)
cur_query_mh = cur_query_mh.downsample(scaled=scaled)
if not cur_query_mh: # empty query? quit.
return []
# CTB: could probably remove this check unless debug requested.
if cur_query_mh.contained_by(self.orig_query_mh, downsample=True) < 1:
raise ValueError("current query not a subset of original query")
# are we setting a threshold?
try:
x = calc_threshold_from_bp(threshold_bp, scaled, len(cur_query_mh))
threshold, n_threshold_hashes = x
except ValueError:
# too high to ever match => exit
return []
# Find the best match using the internal Counter.
most_common = counter.most_common()
dataset_id, match_size = most_common[0]
# below threshold? no match!
if match_size < n_threshold_hashes:
return []
## at this point, we have a legitimate match above threshold!
# pull match and location.
match = siglist[dataset_id]
# calculate containment
# CTB: this check is probably redundant with intersect_mh calc, below.
cont = cur_query_mh.contained_by(match.minhash, downsample=True)
assert cont
assert cont >= threshold
# calculate intersection of this "best match" with query.
match_mh = match.minhash.downsample(scaled=scaled).flatten()
intersect_mh = cur_query_mh & match_mh
location = self.locations[dataset_id]
# build result & return intersection
return (IndexSearchResult(cont, match, location), intersect_mh)
def consume(self, intersect_mh):
"Maintain the internal counter by removing the given hashes."
self.query_started = 1
if not intersect_mh:
return
siglist = self.siglist
counter = self.counter
most_common = counter.most_common()
# Prepare counter for finding the next match by decrementing
# all hashes found in the current match in other datasets;
# remove empty datasets from counter, too.
for (dataset_id, _) in most_common:
# CTB: note, remaining_mh may not be at correct scaled here.
# this means that counters that _should_ be empty might not
# _be_ empty in some situations. This does not
# lead to incorrect results, merely potentially overfull
# 'counter' objects. The tradeoffs to fixing this would
# need to be examined! (This could be fixed in self.downsample().)
remaining_mh = siglist[dataset_id].minhash
intersect_count = intersect_mh.count_common(remaining_mh,
downsample=True)
if intersect_count:
counter[dataset_id] -= intersect_count
if counter[dataset_id] == 0:
del counter[dataset_id]
class MultiIndex(Index):
"""
Load a collection of signatures, and retain their original locations.
One specific use for this is when loading signatures from a directory;
MultiIndex will record which specific files provided which
signatures.
Creates a manifest on load.
Note: this is an in-memory collection, and does not do lazy loading:
all signatures are loaded upon instantiation and kept in memory.
There are a variety of loading functions:
* `load` takes a list of already-loaded Index objects,
together with a list of their locations.
* `load_from_directory` traverses a directory to load files within.
* `load_from_path` takes an arbitrary pathname and tries to load it
as a directory, or as a .sig file.
* `load_from_pathlist` takes a text file full of pathnames and tries
to load them all.
Concrete class; signatures held in memory; builds and uses manifests.
"""
def __init__(self, manifest, parent, *, prepend_location=False):
"""Constructor; takes manifest containing signatures, together with
the top-level location.
"""
self.manifest = manifest
self.parent = parent
self.prepend_location = prepend_location
if prepend_location and self.parent is None:
raise ValueError("must set 'parent' if 'prepend_location' is set")
@property
def location(self):
return self.parent
def signatures(self):
for row in self.manifest.rows:
yield row['signature']
def signatures_with_location(self):
for row in self.manifest.rows:
loc = row['internal_location']
# here, 'parent' may have been removed from internal_location
# for directories; if so, add it back in.
if self.prepend_location:
loc = os.path.join(self.parent, loc)
yield row['signature'], loc
def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, location)
CTB note: here, 'internal_location' is the source file for the
index. This is a special feature of this (in memory) class.
"""
for row in self.manifest.rows:
yield row['signature'], row['internal_location']
def __len__(self):
if self.manifest is None:
return 0
return len(self.manifest)
def insert(self, *args):
raise NotImplementedError
@classmethod
def load(cls, index_list, source_list, parent, *, prepend_location=False):
"""Create a MultiIndex from already-loaded indices.
Takes two arguments: a list of Index objects, and a matching list
of source strings (filenames, etc.) If the source is not None,
then it will be used to override the location provided by the
matching Index object.
"""
assert len(index_list) == len(source_list)
# yield all signatures + locations
def sigloc_iter():
for idx, iloc in zip(index_list, source_list):
# override internal location if location is explicitly provided
if iloc is None:
iloc = idx.location
for ss in idx.signatures():
yield ss, iloc
# build manifest; note, ALL signatures are stored in memory.
# CTB: could do this on demand?
# CTB: should we use get_manifest functionality?
# CTB: note here that the manifest is created by iteration
# *even if it already exists.* This could be changed to be more
# efficient... but for now, use StandaloneManifestIndex if you
# want to avoid this when loading from multiple files.
manifest = CollectionManifest.create_manifest(sigloc_iter())
# create!
return cls(manifest, parent, prepend_location=prepend_location)
@classmethod
def load_from_directory(cls, pathname, *, force=False):
"""Create a MultiIndex from a directory.
Takes directory path plus optional boolean 'force'. Attempts to
load all files ending in .sig or .sig.gz, by default; if 'force' is
True, will attempt to load _all_ files, ignoring errors.
Will not load anything other than JSON signature files.
"""
from ..sourmash_args import traverse_find_sigs
if not os.path.isdir(pathname):
raise ValueError(f"'{pathname}' must be a directory.")
index_list = []
source_list = []
traversal = traverse_find_sigs([pathname], yield_all_files=force)
for thisfile in traversal:
try:
idx = LinearIndex.load(thisfile)
index_list.append(idx)
rel = os.path.relpath(thisfile, pathname)
source_list.append(rel)
except (IOError, sourmash.exceptions.SourmashError) as exc:
if force:
continue # ignore error
else:
raise ValueError(exc) # stop loading!
# did we load anything? if not, error
if not index_list:
raise ValueError(f"no signatures to load under directory '{pathname}'")
return cls.load(index_list, source_list, pathname,
prepend_location=True)