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] require scaled signatures for containment #1381

Merged
merged 7 commits into from
Mar 9, 2021
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
2 changes: 2 additions & 0 deletions src/sourmash/minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ def contained_by(self, other, downsample=False):
"""\
Calculate how much of self is contained by other.
"""
if not (self.scaled and other.scaled):
raise TypeError("can only calculate containment for scaled MinHashes")
if not len(self):
return 0.0

Expand Down
13 changes: 10 additions & 3 deletions src/sourmash/sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def load_dbs_and_sigs(filenames, query, is_similarity_query, *, cache_size=None)
assert dbtype == DatabaseType.SIGLIST

siglist = _select_sigs(db, moltype=query_moltype, ksize=query_ksize)
siglist = filter_compatible_signatures(query, siglist, 1)
siglist = filter_compatible_signatures(query, siglist, True)
linear = LinearIndex(siglist, filename=filename)
databases.append(linear)

Expand Down Expand Up @@ -311,8 +311,15 @@ def load_dbs_and_sigs(filenames, query, is_similarity_query, *, cache_size=None)
# signature file
elif dbtype == DatabaseType.SIGLIST:
siglist = _select_sigs(db, moltype=query_moltype, ksize=query_ksize)
siglist = filter_compatible_signatures(query, siglist, False)
siglist = list(siglist)
try:
# CTB: it's not clear to me that filter_compatible_signatures
# should fail here, on incompatible signatures; but that's
# what we have it doing currently. Revisit.
siglist = filter_compatible_signatures(query, siglist, False)
siglist = list(siglist)
except ValueError:
siglist = []

if not siglist:
notify("no compatible signatures found in '{}'", filename)
sys.exit(-1)
Expand Down
17 changes: 16 additions & 1 deletion tests/test__minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,29 @@ def test_div_zero(track_abundance):

def test_div_zero_contained(track_abundance):
# verify that empty MHs do not yield divide by zero errors for contained_by
mh = MinHash(1, 4, track_abundance=track_abundance)
mh = MinHash(0, 4, scaled=1, track_abundance=track_abundance)
mh2 = mh.copy_and_clear()

mh.add_sequence('ATGC')
assert mh.contained_by(mh2) == 0
assert mh2.contained_by(mh) == 0


def test_contained_requires_scaled(track_abundance):
# verify that MHs of size 1 stay size 1, & act properly as bottom sketches.
mh = MinHash(1, 4, track_abundance=track_abundance)
assert mh.moltype == 'DNA'

mh.add_sequence('ATGC')
a = mh.hashes

mh.add_sequence('GCAT') # this will not get added; hash > ATGC
b = mh.hashes

with pytest.raises(TypeError):
mh.contained_by(mh)


def test_bytes_dna(track_abundance):
mh = MinHash(1, 4, track_abundance=track_abundance)
mh.add_sequence('ATGC')
Expand Down
62 changes: 56 additions & 6 deletions tests/test_sourmash.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ def test_compare_no_matching_sigs(c):
query = utils.get_test_data('lca/TARA_ASE_MAG_00031.sig')

with pytest.raises(ValueError) as exc:
c.last_result.status, c.last_result.out, c.last_result.err = c.run_sourmash('compare', '-k', '100', query,
fail_ok=True)
c.last_result.status, c.last_result.out, c.last_result.err = \
c.run_sourmash('compare', '-k', '100', query, fail_ok=True)

print(c.last_result.out)
print(c.last_result.err)
Expand Down Expand Up @@ -1345,7 +1345,8 @@ def test_search_containment():
testdata1 = utils.get_test_data('short.fa')
testdata2 = utils.get_test_data('short2.fa')
status, out, err = utils.runscript('sourmash',
['compute', testdata1, testdata2],
['compute', testdata1, testdata2,
'--scaled', '1'],
in_directory=location)

status, out, err = utils.runscript('sourmash',
Expand All @@ -1354,7 +1355,7 @@ def test_search_containment():
in_directory=location)
print(status, out, err)
assert '1 matches' in out
assert '95.8%' in out
assert '95.6%' in out


def test_search_containment_sbt():
Expand All @@ -1363,7 +1364,8 @@ def test_search_containment_sbt():
testdata1 = utils.get_test_data('short.fa')
testdata2 = utils.get_test_data('short2.fa')
status, out, err = utils.runscript('sourmash',
['compute', testdata1, testdata2],
['compute', testdata1, testdata2,
'--scaled', '1'],
in_directory=location)

status, out, err = utils.runscript('sourmash',
Expand All @@ -1378,7 +1380,7 @@ def test_search_containment_sbt():
in_directory=location)
print(status, out, err)
assert '1 matches' in out
assert '95.8%' in out
assert '95.6%' in out


def test_search_gzip():
Expand Down Expand Up @@ -1570,6 +1572,37 @@ def test_search_metagenome_traverse():
assert '13 matches; showing first 3:' in out


@utils.in_thisdir
def test_search_incompatible(c):
num_sig = utils.get_test_data('num/47.fa.sig')
scaled_sig = utils.get_test_data('47.fa.sig')

with pytest.raises(ValueError) as exc:
c.run_sourmash("search", scaled_sig, num_sig, fail_ok=True)
assert c.last_result.status != 0
print(c.last_result.out)
print(c.last_result.err)
assert 'incompatible - cannot compare.' in c.last_result.err
assert 'was calculated with --scaled,' in c.last_result.err


@utils.in_tempdir
def test_search_traverse_incompatible(c):
searchdir = c.output('searchme')
os.mkdir(searchdir)

num_sig = utils.get_test_data('num/47.fa.sig')
scaled_sig = utils.get_test_data('47.fa.sig')
shutil.copyfile(num_sig, c.output('searchme/num.sig'))
shutil.copyfile(scaled_sig, c.output('searchme/scaled.sig'))

c.run_sourmash("search", scaled_sig, c.output('searchme'))
print(c.last_result.out)
print(c.last_result.err)
assert 'incompatible - cannot compare.' in c.last_result.err
assert 'was calculated with --scaled,' in c.last_result.err


# explanation: you cannot downsample a scaled SBT to match a scaled
# signature, so make sure that when you try such a search, it fails!
# (you *can* downsample a signature to match an SBT.)
Expand Down Expand Up @@ -3246,6 +3279,23 @@ def test_gather_metagenome_traverse():
'NC_011294.1 Salmonella enterica subsp...' in out))


@utils.in_tempdir
def test_gather_traverse_incompatible(c):
searchdir = c.output('searchme')
os.mkdir(searchdir)

num_sig = utils.get_test_data('num/47.fa.sig')
scaled_sig = utils.get_test_data('47.fa.sig')
shutil.copyfile(num_sig, c.output('searchme/num.sig'))
shutil.copyfile(scaled_sig, c.output('searchme/scaled.sig'))

c.run_sourmash("gather", scaled_sig, c.output('searchme'))
print(c.last_result.out)
print(c.last_result.err)
assert 'incompatible - cannot compare.' in c.last_result.err
assert 'was calculated with --scaled,' in c.last_result.err


def test_gather_metagenome_output_unassigned():
with utils.TempDirectory() as location:
testdata_glob = utils.get_test_data('gather/GCF_000195995*g')
Expand Down