Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] refactor _signatures_with_internal #1896

Merged
merged 3 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/sourmash/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def signatures_with_location(self):
yield ss, self.location

def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, location, internal_location).
"""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.
Expand Down Expand Up @@ -600,7 +604,7 @@ def load(cls, location, traverse_yield_all=False, use_manifest=True):
use_manifest=use_manifest)

def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, location, internal_location).
"""Return an iterator of tuples (ss, internal_location).
Note: does not limit signatures to subsets.
"""
Expand All @@ -615,7 +619,7 @@ def _signatures_with_internal(self):
self.traverse_yield_all:
fp = zf.open(zipinfo)
for ss in load_signatures(fp):
yield ss, zf.filename, zipinfo.filename
yield ss, zipinfo.filename

def signatures(self):
"Load all signatures in the zip file."
Expand Down Expand Up @@ -888,14 +892,14 @@ def signatures_with_location(self):
yield row['signature'], loc

def _signatures_with_internal(self):
"""Return an iterator of tuples (ss, parent, location)
"""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.
"""
parent = self.parent
for row in self.manifest.rows:
yield row['signature'], parent, row['internal_location']
yield row['signature'], row['internal_location']


def __len__(self):
Expand Down
3 changes: 2 additions & 1 deletion src/sourmash/lca/lca_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ def signatures(self):
yield v

def _signatures_with_internal(self):
"Return all of the signatures in this LCA database."
for idx, ss in self._signatures.items():
yield ss, self.location, idx
yield ss, idx

def select(self, ksize=None, moltype=None, num=0, scaled=0, abund=None,
containment=False, picklist=None):
Expand Down
2 changes: 1 addition & 1 deletion src/sourmash/sbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _signatures_with_internal(self):
"""
for k in self.leaves():
ss = k.data
yield ss, self.location, k._path
yield ss, k._path

def select(self, ksize=None, moltype=None, num=0, scaled=0,
containment=False, abund=None, picklist=None):
Expand Down
9 changes: 1 addition & 8 deletions src/sourmash/sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,9 @@ def get_manifest(idx, *, require=True, rebuild=False):

debug_literal(f"get_manifest: no manifest found / rebuild={rebuild}")

# CTB: CollectionManifest.create_manifest wants (ss, iloc).
# so this is an adaptor function! Might want to just change
# what `create_manifest` takes.
def manifest_iloc_iter(idx):
for (ss, loc, iloc) in idx._signatures_with_internal():
yield ss, iloc

# need to build one...
try:
m = CollectionManifest.create_manifest(manifest_iloc_iter(idx),
m = CollectionManifest.create_manifest(idx._signatures_with_internal(),
include_signature=False)
debug_literal("get_manifest: rebuilt manifest.")
except NotImplementedError:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ def test_multi_index_load_from_directory():
# also check internal locations and parent value --
assert mi.parent.endswith('prot/protein')

ilocs = [ x[2] for x in mi._signatures_with_internal() ]
ilocs = [ x[1] for x in mi._signatures_with_internal() ]
assert endings[0] in ilocs, ilocs
assert endings[1] in ilocs, ilocs

Expand Down
6 changes: 3 additions & 3 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_generate_manifest():

rows = []
siglist = []
for (sig, _, loc) in loader._signatures_with_internal():
for (sig, loc) in loader._signatures_with_internal():
row = index.CollectionManifest.make_manifest_row(sig, loc)
rows.append(row)
siglist.append(sig)
Expand All @@ -43,7 +43,7 @@ def test_manifest_to_picklist():

rows = []
siglist = []
for (sig, _, loc) in loader._signatures_with_internal():
for (sig, loc) in loader._signatures_with_internal():
row = index.CollectionManifest.make_manifest_row(sig, loc)
rows.append(row)
siglist.append(sig)
Expand All @@ -64,7 +64,7 @@ def test_save_load_manifest():

rows = []
siglist = []
for (sig, _, loc) in loader._signatures_with_internal():
for (sig, loc) in loader._signatures_with_internal():
row = index.CollectionManifest.make_manifest_row(sig, loc)
rows.append(row)
siglist.append(sig)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class FakeIndex(LinearIndex):
was_called = 0
def _signatures_with_internal(self):
self.was_called = 1
return [(ss47, "fakeloc", "fakeiloc")]
return [(ss47, "fakeiloc")]

idx = FakeIndex([sig47])

Expand All @@ -471,7 +471,7 @@ class FakeIndex(LinearIndex):

def _signatures_with_internal(self):
self.was_called = 1
return [(ss47, "fakeloc", "fakeiloc")]
return [(ss47, "fakeiloc")]

idx = FakeIndex([sig47])

Expand Down