From e140f5fc801e6a273633611a22f3a47ea6a7b51a Mon Sep 17 00:00:00 2001 From: Chunyu Ma Date: Mon, 18 Mar 2024 21:33:45 -0400 Subject: [PATCH] add better instruction when entering the command --- yacht/__init__.py | 50 +++++++++++++++++++++-- yacht/download_default_ref_db.py | 3 +- yacht/download_demofiles.py | 3 +- yacht/download_pretrained_ref_db.py | 3 +- yacht/make_training_data_from_sketches.py | 3 -- yacht/run_YACHT.py | 3 -- yacht/sketch_ref_genomes.py | 2 - yacht/sketch_sample.py | 2 - yacht/standardize_yacht_output.py | 3 +- 9 files changed, 51 insertions(+), 21 deletions(-) diff --git a/yacht/__init__.py b/yacht/__init__.py index 328dda8..68a1b27 100644 --- a/yacht/__init__.py +++ b/yacht/__init__.py @@ -1,4 +1,5 @@ import argparse +import sys from . import ( download_default_ref_db, @@ -12,10 +13,48 @@ ) from .utils import __version__ +# Custom help action +class CustomHelpAction(argparse.Action): + def __init__(self, option_strings, dest, **kwargs): + super(CustomHelpAction, self).__init__(option_strings=option_strings, dest=dest, nargs=0, **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + print_custom_help_exit() + +def print_version(): + print(f"== This is YACHT version {utils.__version__} ==") + print("== Please cite https://doi.org/10.1093/bioinformatics/btae047. ==") + +def print_custom_help_exit(): + print_version() + print(""" +YACHT is a mathematically rigorous hypothesis test for the presence or absence of organisms +in a metagenomic sample, based on average nucleotide identity (ANI). + +Usage instructions: + yacht download demo -h download YACHT demo files + yacht download default_ref_db -h download default raw reference databases + yacht download pretrained_ref_db -h download pretrained databases + + yacht sketch ref -h sketch reference genomes + yacht sketch sample -h sketch metagenomics samples + + yacht train -h pre-process the reference genomes + + yacht run -h run the YACHT algorithm + + yacht convert -h convert YACHT result to other popular output formats + +Options: + -h, --help show this help message and exit + -v, --version show program's version number and exit +""") + sys.exit() def main(): - parser = argparse.ArgumentParser(prog="yacht") - parser.add_argument("--version", action="version", version=f"YACHT {utils.__version__}") + parser = argparse.ArgumentParser(prog="yacht", add_help=False) + parser.add_argument("-v", "--version", action="store_true", help="show program's version number and exit") + parser.add_argument("-h", "--help", action=CustomHelpAction, help="show this help message and exit") subparsers = parser.add_subparsers(dest="command") # Train command @@ -86,10 +125,15 @@ def main(): sketch_sample_parser.set_defaults(func=sketch_sample.main) args = parser.parse_args() + + if args.version: + print_version() + sys.exit() + if "func" in args: args.func(args) else: - parser.print_help() + print_custom_help_exit() if __name__ == "__main__": diff --git a/yacht/download_default_ref_db.py b/yacht/download_default_ref_db.py index 8d4edbc..ee038fd 100644 --- a/yacht/download_default_ref_db.py +++ b/yacht/download_default_ref_db.py @@ -6,7 +6,7 @@ import os from .utils import create_output_folder, check_download_args # Import global variables -from .utils import BASE_URL, __version__ +from .utils import BASE_URL # Configure Loguru logger logger.remove() @@ -16,7 +16,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument("--database", choices=["genbank", "gtdb"], required=True) parser.add_argument( "--db_version", diff --git a/yacht/download_demofiles.py b/yacht/download_demofiles.py index 0e89e44..a0551d0 100644 --- a/yacht/download_demofiles.py +++ b/yacht/download_demofiles.py @@ -5,7 +5,7 @@ import argparse from loguru import logger # Import global variables -from .utils import GITHUB_API_URL, GITHUB_RAW_URL, __version__ +from .utils import GITHUB_API_URL, GITHUB_RAW_URL # Configure Loguru logger logger.remove() @@ -17,7 +17,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument("--outfolder", help="Output folder.", default="demo") diff --git a/yacht/download_pretrained_ref_db.py b/yacht/download_pretrained_ref_db.py index 8aeab71..43fd9b4 100644 --- a/yacht/download_pretrained_ref_db.py +++ b/yacht/download_pretrained_ref_db.py @@ -8,7 +8,7 @@ import zipfile from .utils import create_output_folder, check_download_args # Import global variables -from .utils import ZENODO_COMMUNITY_URL, __version__ +from .utils import ZENODO_COMMUNITY_URL # Configure Loguru logger logger.remove() @@ -20,7 +20,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument("--database", choices=["genbank", "gtdb"], required=True) parser.add_argument( "--db_version", choices=["genbank-2022.03", "rs214"], required=True diff --git a/yacht/make_training_data_from_sketches.py b/yacht/make_training_data_from_sketches.py index 9a0830f..ffbdb93 100644 --- a/yacht/make_training_data_from_sketches.py +++ b/yacht/make_training_data_from_sketches.py @@ -17,9 +17,6 @@ def add_arguments(parser): - parser.add_argument( - "--version", action="version", version=f"YACHT {utils.__version__}" - ) parser.add_argument( "--ref_file", help="Location of the Sourmash signature database file. " diff --git a/yacht/run_YACHT.py b/yacht/run_YACHT.py index 9d5e5d0..50b888d 100644 --- a/yacht/run_YACHT.py +++ b/yacht/run_YACHT.py @@ -21,9 +21,6 @@ def add_arguments(parser): - parser.add_argument( - "--version", action="version", version=f"YACHT {utils.__version__}" - ) parser.add_argument( "--json", type=str, diff --git a/yacht/sketch_ref_genomes.py b/yacht/sketch_ref_genomes.py index a97c397..b20bde0 100644 --- a/yacht/sketch_ref_genomes.py +++ b/yacht/sketch_ref_genomes.py @@ -6,7 +6,6 @@ from pathlib import Path from loguru import logger # Import global variables -from .utils import __version__ # Configure Loguru logger logger.remove() @@ -16,7 +15,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument("--infile", help="Input file or folder path.", required=True) parser.add_argument("--kmer", type=int, help="K-mer size.", default=31) parser.add_argument("--scaled", type=int, help="Scaled factor.", default=1000) diff --git a/yacht/sketch_sample.py b/yacht/sketch_sample.py index a59c8c2..1b26fb8 100644 --- a/yacht/sketch_sample.py +++ b/yacht/sketch_sample.py @@ -7,7 +7,6 @@ from loguru import logger # Import global variables -from .utils import __version__ # Configure Loguru logger logger.remove() @@ -18,7 +17,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument( "--infile", nargs="+", diff --git a/yacht/standardize_yacht_output.py b/yacht/standardize_yacht_output.py index f168c38..8fdac04 100644 --- a/yacht/standardize_yacht_output.py +++ b/yacht/standardize_yacht_output.py @@ -11,7 +11,7 @@ import biom import argparse from biom.util import biom_open -from .utils import get_cami_profile, __version__ +from .utils import get_cami_profile from collections import OrderedDict from loguru import logger @@ -23,7 +23,6 @@ def add_arguments(parser): - parser.add_argument("--version", action="version", version=f"YACHT {__version__}") parser.add_argument( "--yacht_output", type=str,