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] autodetect FASTA/FASTQ files if given as signatures #1078

Merged
merged 5 commits into from
Jul 13, 2020
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: 0 additions & 2 deletions sourmash/sig/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def cat(args):
try:
this_siglist = sourmash_args.load_file_as_signatures(sigfile, traverse=True)
except Exception as exc:
error('\nError while reading signatures from {}:'.format(sigfile))
error(str(exc))
error('(continuing)')

Expand Down Expand Up @@ -177,7 +176,6 @@ def describe(args):
for k in this_siglist:
siglist.append((k, sigfile))
except Exception as exc:
error('\nError while reading signatures from {}:'.format(sigfile))
error(str(exc))
error('(continuing)')

Expand Down
22 changes: 22 additions & 0 deletions sourmash/sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import itertools
from enum import Enum

import screed

from sourmash import load_sbt_index
from sourmash.lca.lca_db import load_single_database
import sourmash.exceptions
Expand Down Expand Up @@ -65,6 +67,11 @@ def calculate_moltype(args, default=None):


def load_query_signature(filename, ksize, select_moltype, select_md5=None):
"""Load a single signature to use as a query.

Uses load_file_as_signatures underneath, so can load from collections
and indexed databases.
"""
try:
sl = load_file_as_signatures(filename, ksize=ksize,
select_moltype=select_moltype)
Expand Down Expand Up @@ -404,6 +411,21 @@ def _load_database(filename, traverse, traverse_yield_all):
except:
pass
Comment on lines 411 to 412
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it better to pass here, then try again below (rather than try/except within the exception)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, because we have so many things to try. if we did nested try/excepts it would end up indenting to the right.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks!


if not loaded:
successful_screed_load = False
it = None
try:
# CTB: could be kind of time consuming for big record, but at the
# moment screed doesn't expose format detection cleanly.
with screed.open(filename) as it:
record = next(iter(it))
successful_screed_load = True
except:
pass

if successful_screed_load:
raise OSError("Error while reading signatures from '{}' - got sequences instead! Is this a FASTA/FASTQ file?".format(filename))

if not loaded:
raise OSError("Error while reading signatures from '{}'.".format(filename))

Expand Down
10 changes: 10 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ def test_load_index_3():

sigs = list(idx.signatures())
assert len(sigs) == 2


def test_load_fasta_as_signature():
# try loading a fasta file - should fail with informative exception
testfile = utils.get_test_data('short.fa')

with pytest.raises(OSError) as e:
idx = sourmash.load_file_as_index(testfile)

assert "Error while reading signatures from '{}' - got sequences instead! Is this a FASTA/FASTQ file?".format(testfile) in str(e)