From e731a6907959eebf63fbb9b9baf9718f498dde80 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 17 Jul 2024 12:10:23 +0200 Subject: [PATCH] Update CLI to support reading from stdin --- docs/guide/cli.rst | 2 ++ pyrodigal/cli.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/guide/cli.rst b/docs/guide/cli.rst index f81808f..6429fba 100644 --- a/docs/guide/cli.rst +++ b/docs/guide/cli.rst @@ -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. diff --git a/pyrodigal/cli.py b/pyrodigal/cli.py index bff579c..9ab5bee 100644 --- a/pyrodigal/cli.py +++ b/pyrodigal/cli.py @@ -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( @@ -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", @@ -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: @@ -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(