Skip to content

Commit

Permalink
Update CLI to support reading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Jul 17, 2024
1 parent 8e7625b commit e731a69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/guide/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ exceptions:

- The input file for Pyrodigal can only be in FASTA format, GenBank is not
supported.
- Pyrodigal supports getting the input sequences through a pipe to *stdin*,
however the stream cannot be compressed.
- The GenBank output of Pyrodigal is a full GenBank record including the
input sequence, unlike Prodigal which only outputs the features section.

Expand Down
13 changes: 9 additions & 4 deletions pyrodigal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def zopen(path, mode='r', encoding=None, errors=None, newline=None) -> typing.It

def argument_parser(
prog: str = __name__,
version: str = __version__
version: str = __version__,
input_required: bool = True,
) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog=prog, add_help=False)
parser.add_argument(
Expand Down Expand Up @@ -95,7 +96,10 @@ def argument_parser(
default=11,
)
parser.add_argument(
"-i", metavar="input_file", required=True, help="Specify FASTA input file."
"-i",
metavar="input_file",
required=input_required,
help="Specify FASTA input file.",
)
parser.add_argument(
"-m",
Expand Down Expand Up @@ -186,11 +190,12 @@ def main(
argv: typing.Optional[typing.List[str]] = None,
stdout: typing.TextIO = sys.stdout,
stderr: typing.TextIO = sys.stderr,
stdin: typing.TextIO = sys.stdin,
*,
gene_finder_factory: typing.Callable[..., GeneFinder] = GeneFinder,
argument_parser: typing.Callable[[], argparse.ArgumentParser] = argument_parser,
) -> int:
parser = argument_parser()
parser = argument_parser(input_required=stdin.isatty())
args = parser.parse_args(argv)

with contextlib.ExitStack() as ctx:
Expand Down Expand Up @@ -222,7 +227,7 @@ def main(
training_info = None

# open input (with support for compressed files)
input_file = ctx.enter_context(zopen(args.i))
input_file = stdin if args.i is None else ctx.enter_context(zopen(args.i))

# initialize the ORF finder
gene_finder = gene_finder_factory(
Expand Down

0 comments on commit e731a69

Please sign in to comment.