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] fix tax argument parsing #2218

Merged
merged 15 commits into from
Aug 29, 2022
4 changes: 2 additions & 2 deletions src/sourmash/cli/tax/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def subparser(subparsers):
aliases=['annotate'],
usage=usage)
subparser.add_argument(
'-g', '--gather-csv', nargs='*', default = [],
'-g', '--gather-csv', nargs='*', default = [], action='extend',
help='CSV output files from sourmash gather'
)
subparser.add_argument(
Expand All @@ -36,7 +36,7 @@ def subparser(subparsers):
)
subparser.add_argument(
'-t', '--taxonomy-csv', '--taxonomy', metavar='FILE',
nargs="+", required=True,
nargs='*', required=True, action="extend",
help='database lineages CSV'
)
subparser.add_argument(
Expand Down
11 changes: 8 additions & 3 deletions src/sourmash/cli/tax/genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def subparser(subparsers):
aliases=['classify'],
usage=usage)
subparser.add_argument(
'-g', '--gather-csv', nargs='*', default = [],
'-g', '--gather-csv', action='extend', nargs='*', default = [],
help='CSVs output by sourmash gather for this sample'
)
subparser.add_argument(
Expand All @@ -54,7 +54,7 @@ def subparser(subparsers):
)
subparser.add_argument(
'-t', '--taxonomy-csv', '--taxonomy', metavar='FILE',
nargs='+', required=True,
nargs='*', required=True, action='extend',
help='database lineages CSV'
)
subparser.add_argument(
Expand Down Expand Up @@ -82,7 +82,8 @@ def subparser(subparsers):
help='fail quickly if taxonomy is not available for an identifier',
)
subparser.add_argument(
'-F', '--output-format', default=['csv_summary'], nargs='+', choices=["csv_summary", "krona", "human", "lineage_csv"],
'-F', '--output-format', default=[], nargs='*', action='extend',
choices=["csv_summary", "krona", "human", "lineage_csv"],
help='choose output format(s)',
)
subparser.add_argument(
Expand All @@ -102,4 +103,8 @@ def main(args):
if not args.rank:
if any(x in ["krona"] for x in args.output_format):
raise ValueError(f"Rank (--rank) is required for krona output format.")
if not args.output_format:
# change to "human" for 5.0
args.output_format = ["csv_summary"]

return sourmash.tax.__main__.genome(args)
2 changes: 1 addition & 1 deletion src/sourmash/cli/tax/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def subparser(subparsers):
)
subparser.add_argument(
'-t', '--taxonomy-csv', '--taxonomy', metavar='FILE',
nargs="+", required=True,
nargs="+", required=True, action="extend",
help='database lineages'
)
subparser.add_argument(
Expand Down
10 changes: 7 additions & 3 deletions src/sourmash/cli/tax/metagenome.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def subparser(subparsers):
aliases=['summarize'],
usage=usage)
subparser.add_argument(
'-g', '--gather-csv', nargs='*', default = [],
'-g', '--gather-csv', action="extend", nargs='*', default = [],
help='CSVs from sourmash gather'
)
subparser.add_argument(
Expand All @@ -51,7 +51,7 @@ def subparser(subparsers):
)
subparser.add_argument(
'-t', '--taxonomy-csv', '--taxonomy', metavar='FILE',
nargs='+', required=True,
action="extend", nargs='+', required=True,
help='database lineages CSV'
)
subparser.add_argument(
Expand All @@ -67,7 +67,8 @@ def subparser(subparsers):
help='fail quickly if taxonomy is not available for an identifier',
)
subparser.add_argument(
'-F', '--output-format', default=['csv_summary'], nargs='+', choices=["human", "csv_summary", "krona", "lineage_summary", "kreport"],
'-F', '--output-format', default=[], nargs='*', action="extend",
choices=["human", "csv_summary", "krona", "lineage_summary", "kreport"],
help='choose output format(s)',
)
subparser.add_argument(
Expand All @@ -89,4 +90,7 @@ def main(args):
if not args.rank:
if any(x in ["krona", "lineage_summary"] for x in args.output_format):
raise ValueError(f"Rank (--rank) is required for krona and lineage_summary output formats.")
if not args.output_format:
# change to "human" for 5.0
args.output_format = ["csv_summary"]
return sourmash.tax.__main__.metagenome(args)
2 changes: 1 addition & 1 deletion src/sourmash/cli/tax/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def subparser(subparsers):
)
subparser.add_argument(
'-t', '--taxonomy-csv', '--taxonomy', metavar='FILE',
nargs="+", required=True,
nargs="+", required=True, action="extend",
help='database lineages'
)
subparser.add_argument(
Expand Down
1 change: 1 addition & 0 deletions src/sourmash/tax/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def prepare(args):
notify("loading taxonomies...")
try:
tax_assign = MultiLineageDB.load(args.taxonomy_csv,
force=args.force,
keep_full_identifiers=args.keep_full_identifiers,
keep_identifier_versions=args.keep_identifier_versions)
except ValueError as exc:
Expand Down
10 changes: 7 additions & 3 deletions src/sourmash/tax/tax_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,8 @@ def _save_csv(self, fp):
@classmethod
def load(cls, locations, **kwargs):
"Load one or more taxonomies from the given location(s)"
force = kwargs.get('force', False)

if isinstance(locations, str):
raise TypeError("'locations' should be a list, not a string")

Expand All @@ -1098,12 +1100,14 @@ def load(cls, locations, **kwargs):
loaded = True
except (ValueError, csv.Error) as exc:
# for the last loader, just pass along ValueError...
raise ValueError(f"cannot read taxonomy assignments from '{location}': {str(exc)}")
if not force:
raise ValueError(f"cannot read taxonomy assignments from '{location}': {str(exc)}")

# nothing loaded, goodbye!
if not loaded:
if not loaded and not force:
raise ValueError(f"cannot read taxonomy assignments from '{location}'")

tax_assign.add(this_tax_assign)
if loaded:
tax_assign.add(this_tax_assign)

return tax_assign
Loading