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

implementation for the new CLI PR #811

Merged
merged 6 commits into from
Dec 27, 2019
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 sourmash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
from . import sbtmh
from . import sbt_storage
from . import signature
from . import sig
from . import cli
from . import commands

from pkg_resources import get_distribution, DistributionNotFound

Expand Down
2 changes: 1 addition & 1 deletion sourmash/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def main(arglist=None):
else:
mod = getattr(sourmash.cli, args.cmd)
mainmethod = getattr(mod, 'main')
mainmethod(args)
return mainmethod(args)


if __name__ == '__main__':
Expand Down
75 changes: 34 additions & 41 deletions sourmash/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from argparse import ArgumentParser
import sys

import sourmash

from . import utils

# Commands
from . import categorize
from . import compare
from . import compute
from . import gather
from . import import_csv
from . import info
from . import index
from . import multigather
from . import plot
from . import search
from . import watch

# Subcommand groups
from . import lca
from . import sbt
from . import signature


DEFAULT_LOAD_K = 31
VERSION = '2.2.0'
from . import sig
from . import storage


class SourmashParser(ArgumentParser):
Expand All @@ -36,77 +40,66 @@ def print_citation(self):
notify("== Please cite Brown and Irber (2016), doi:10.21105/joss.00027. ==\n")
self._citation_printed = True

def _subparser_from_name(self, name):
"""Given a name, get the subparser instance registered with this parser."""
container = self._actions
if name is None:
return None
for action in container:
if action.choices is None:
continue
elif name in action.choices:
return action.choices[name]

def parse_args(self, args=None, namespace=None):
if (args is None and len(sys.argv) == 1) or (args is not None and len(args) == 0):
self.print_help()
raise SystemExit(0)
raise SystemExit(1)
args = super(SourmashParser, self).parse_args(args=args, namespace=namespace)
if ('quiet' not in args or not args.quiet) and self.citation:
self.print_citation()

if 'subcmd' in args and args.subcmd is None:
self._subparser_from_name(args.cmd).print_help()
raise SystemExit(1)

# BEGIN: dirty hacks to simultaneously support new and previous interface
if hasattr(args, 'subcmd') and args.subcmd == 'import':
args.subcmd = 'ingest'
if hasattr(args, 'cmd') and args.cmd == 'sbt_combine':
args.cmd = 'sbt'
args.subcmd = 'combine'
if hasattr(args, 'cmd') and args.cmd == 'index':
args.cmd = 'sbt'
args.subcmd = 'index'
if hasattr(args, 'cmd') and args.cmd == 'categorize':
if hasattr(args, 'cmd') and args.cmd == 'migrate':
args.cmd = 'sbt'
args.subcmd = 'categorize'
if hasattr(args, 'cmd') and args.cmd == 'watch':
args.subcmd = 'sbt'
args.subcmd = 'watch'
args.subcmd = 'migrate'
if hasattr(args, 'subcmd') and args.subcmd == 'compare_csv':
args.subcmd = 'compare'
# END: dirty hacks to simultaneously support new and previous interface
return args


def add_moltype_args(parser):
parser.add_argument(
'--protein', dest='protein', action='store_true',
help='choose a protein signature; by default, a nucleotide signature is used'
)
parser.add_argument(
'--dayhoff', dest='dayhoff', action='store_true',
help='build Dayhoff-encoded amino acid signatures'
)
parser.add_argument(
'--hp', '--hydrophobic-polar', dest='hp', action='store_true',
help='build hydrophobic-polar-encoded amino acid signatures'
)


def add_ksize_arg(parser, default=21):
parser.add_argument(
'-k', '--ksize', metavar='K', default=None, type=int,
help='k-mer size; default={d}'.format(d=default)
)


def get_parser():
commands = ['compute', 'compare', 'search', 'plot', 'gather', 'lca', 'sbt', 'info', 'signature']
commands = ['compute', 'compare', 'search', 'plot', 'gather', 'index',
'lca', 'sbt', 'info', 'sig', 'categorize', 'watch', 'storage',
'multigather', 'migrate', 'sbt_combine', 'import_csv']
commandstr = ' -- '.join(sorted(commands))

desc = 'Compute, compare, manipulate, and analyze MinHash sketches of DNA sequences.'
parser = SourmashParser(prog='sourmash', description=desc)
parser._optionals.title = 'Options'
parser.add_argument('-v', '--version', action='version', version='sourmash '+ VERSION)
parser.add_argument('-v', '--version', action='version', version='sourmash '+ sourmash.VERSION)
parser.add_argument('-q', '--quiet', action='store_true', help='don\'t print citation information')
sub = parser.add_subparsers(
title='Commands', dest='cmd', metavar='cmd', help=commandstr,
description='Invoke "sourmash <cmd> --help" for more details on executing each command.'
)
for cmd in commands:
if cmd in ('migrate', 'sbt_combine'):
continue
getattr(sys.modules[__name__], cmd).subparser(sub)
# BEGIN: dirty hacks to simultaneously support new and previous interface
sbt.categorize.subparser(sub)
sbt.combine.alt_subparser(sub)
sbt.index.subparser(sub)
sbt.watch.subparser(sub)
sbt.migrate.subparser(sub)
# END: dirty hacks to simultaneously support new and previous interface
parser._action_groups.reverse()
return parser
14 changes: 11 additions & 3 deletions sourmash/cli/sbt/categorize.py → sourmash/cli/categorize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sourmash
from sourmash.cli.utils import add_ksize_arg
import argparse

from sourmash.cli.utils import add_ksize_arg, add_moltype_args


def subparser(subparsers):
subparser = subparsers.add_parser('categorize')
Expand All @@ -22,7 +24,13 @@ def subparser(subparsers):
'--ignore-abundance', action='store_true',
help='do NOT use k-mer abundances if present'
)
add_moltype_args(subparser)

# TODO: help messages in these
subparser.add_argument('--csv', type=argparse.FileType('at'))
subparser.add_argument('--load-csv', default=None)


def main(args):
print(args)
import sourmash
return sourmash.commands.categorize(args)
6 changes: 4 additions & 2 deletions sourmash/cli/compare.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from argparse import FileType
import sourmash

from sourmash.cli.utils import add_ksize_arg, add_moltype_args


def subparser(subparsers):
subparser = subparsers.add_parser('compare')
subparser.add_argument(
Expand Down Expand Up @@ -36,4 +37,5 @@ def subparser(subparsers):


def main(args):
print(args)
import sourmash
return sourmash.commands.compare(args)
13 changes: 9 additions & 4 deletions sourmash/cli/compute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from argparse import FileType
import sourmash

from sourmash._minhash import get_minhash_default_seed
from sourmash.cli.utils import add_ksize_arg, add_moltype_args
from sourmash.cli.utils import (
add_ksize_arg,
add_moltype_args,
add_construct_moltype_args
)


def subparser(subparsers):
Expand All @@ -24,7 +28,7 @@ def subparser(subparsers):
'--scaled', type=float, default=0,
help='choose number of hashes as 1 in FRACTION of input k-mers'
)
add_moltype_args(sketch_args)
add_construct_moltype_args(sketch_args)
sketch_args.add_argument(
'--input-is-protein', action='store_true',
help='Consume protein sequences - no translation needed.'
Expand Down Expand Up @@ -123,4 +127,5 @@ def subparser(subparsers):


def main(args):
print(args)
from sourmash.command_compute import compute
return compute(args)
5 changes: 4 additions & 1 deletion sourmash/cli/gather.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from argparse import FileType

from sourmash.cli.utils import add_ksize_arg, add_moltype_args


def subparser(subparsers):
subparser = subparsers.add_parser('gather')
subparser.add_argument('query', help='query signature')
Expand Down Expand Up @@ -50,4 +52,5 @@ def subparser(subparsers):


def main(args):
print(args)
import sourmash
return sourmash.commands.gather(args)
17 changes: 17 additions & 0 deletions sourmash/cli/import_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from argparse import FileType
import sys


def subparser(subparsers):
subparser = subparsers.add_parser('import_csv')
subparser.add_argument('mash_csvfile', help='CSV file with mash sketches')
subparser.add_argument(
'-o', '--output', type=FileType('wt'),
default=sys.stdout,
help='save signature generated from data here'
)


def main(args):
import sourmash
return sourmash.commands.import_csv(args)
5 changes: 3 additions & 2 deletions sourmash/cli/sbt/index.py → sourmash/cli/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sourmash
from sourmash.cli.utils import add_moltype_args, add_ksize_arg


def subparser(subparsers):
subparser = subparsers.add_parser('index')
subparser.add_argument('sbt_name', help='name to save SBT into')
Expand Down Expand Up @@ -46,4 +46,5 @@ def subparser(subparsers):


def main(args):
print(args)
import sourmash
return sourmash.commands.index(args)
2 changes: 1 addition & 1 deletion sourmash/cli/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def subparser(subparsers):
subparser = subparsers.add_parser('info')
subparser.add_argument(
'--verbose', action='store_true',
'-v', '--verbose', action='store_true',
help='report versions of khmer and screed'
)

Expand Down
4 changes: 3 additions & 1 deletion sourmash/cli/lca/classify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import FileType


def subparser(subparsers):
subparser = subparsers.add_parser('classify')
subparser.add_argument('--db', nargs='+', action='append')
Expand All @@ -26,4 +27,5 @@ def subparser(subparsers):


def main(args):
print(args)
import sourmash
return sourmash.lca.command_classify.classify(args)
77 changes: 27 additions & 50 deletions sourmash/cli/lca/compare.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
def subparser(subparsers):
subparser = subparsers.add_parser('compare')
subparser.add_argument('csv1', help='taxonomy spreadsheet output by classify')
subparser.add_argument('csv2', help='custom taxonomy spreadsheet')
subparser.add_argument(
'-q', '--quiet', action='store_true',
help='suppress non-error output'
)
subparser.add_argument(
'-d', '--debug', action='store_true',
help='output debugging output'
)
subparser.add_argument(
'-C', '--start-column', metavar='C', default=2, type=int,
help='column at which taxonomic assignments start; default=2'
)
subparser.add_argument(
'--tabs', action='store_true',
help='input spreadsheet is tab-delimited; default is commas'
)
subparser.add_argument(
'--no-headers', action='store_true',
help='no headers present in taxonomy spreadsheet'
)
subparser.add_argument('-f', '--force', action='store_true')

# Dirty hack to simultaneously support new and previous interface
# If desired, this function can be removed with a major version bump.
subparser = subparsers.add_parser('compare_csv')
subparser.add_argument('csv1', help='taxonomy spreadsheet output by classify')
subparser.add_argument('csv2', help='custom taxonomy spreadsheet')
subparser.add_argument(
'-q', '--quiet', action='store_true',
help='suppress non-error output'
)
subparser.add_argument(
'-d', '--debug', action='store_true',
help='output debugging output'
)
subparser.add_argument(
'-C', '--start-column', metavar='C', default=2, type=int,
help='column at which taxonomic assignments start; default=2'
)
subparser.add_argument(
'--tabs', action='store_true',
help='input spreadsheet is tab-delimited; default is commas'
)
subparser.add_argument(
'--no-headers', action='store_true',
help='no headers present in taxonomy spreadsheet'
)
subparser.add_argument('-f', '--force', action='store_true')
for cmd in ('compare', 'compare_csv'):
subparser = subparsers.add_parser(cmd)
subparser.add_argument('csv1', help='taxonomy spreadsheet output by classify')
subparser.add_argument('csv2', help='custom taxonomy spreadsheet')
subparser.add_argument(
'-q', '--quiet', action='store_true',
help='suppress non-error output'
)
subparser.add_argument(
'-d', '--debug', action='store_true',
help='output debugging output'
)
subparser.add_argument(
'-C', '--start-column', metavar='C', default=2, type=int,
help='column at which taxonomic assignments start; default=2'
)
subparser.add_argument(
'--tabs', action='store_true',
help='input spreadsheet is tab-delimited; default is commas'
)
subparser.add_argument(
'--no-headers', action='store_true',
help='no headers present in taxonomy spreadsheet'
)
subparser.add_argument('-f', '--force', action='store_true')


def main(args):
print(args)
import sourmash
return sourmash.lca.command_compare_csv.compare_csv(args)
4 changes: 3 additions & 1 deletion sourmash/cli/lca/gather.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import FileType


def subparser(subparsers):
subparser = subparsers.add_parser('gather')
subparser.add_argument('query')
Expand Down Expand Up @@ -27,4 +28,5 @@ def subparser(subparsers):


def main(args):
print(args)
import sourmash
return sourmash.lca.gather_main(args)
Loading