diff --git a/ksrates_cli.py b/ksrates_cli.py index 2cfcdfaede10ba61a7b1fef6aa70fa0aa79391e7..df9a10255cb15120bf89657f8a3868e1e177dd26 100644 --- a/ksrates_cli.py +++ b/ksrates_cli.py @@ -18,7 +18,10 @@ def generate_config(filename): Generates the configuration file for the rate-adjustment. The configuration file name is given by argument FILENAME. + \b FILENAME: configuration file name + \b + Example: ksrates generate-config config_file.txt """ from ksrates.generate_configfile import generate_configfile generate_configfile(filename) @@ -32,7 +35,11 @@ def init(config_file, expert, nextflow): """ Initializes rate-adjustment from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example 1: ksrates init config_file.txt + Example 2: ksrates init config_file.txt --expert path/to/config_expert.txt """ from ksrates.setup_correction import setup_correction click.format_filename(config_file) @@ -49,11 +56,14 @@ def init(config_file, expert, nextflow): @click.option("--n-threads", type=int, default=4, help="Number of threads (default: 4)") def paralogs_ks(config_file, expert, n_threads): """ - Performs paralog Ks estimation for the focal species through wgd. + Performs paralog Ks estimation through wgd for the focal species. Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates paralogs-ks config_file.txt --n-threads 4 """ from ksrates.wgd_paralogs import wgd_paralogs click.format_filename(config_file) @@ -66,13 +76,13 @@ def paralogs_ks(config_file, expert, n_threads): @cli.command(context_settings={'help_option_names': ['-h', '--help']}, short_help="Performs ortholog Ks estimation.") @click.argument('config_file', type=click.Path(exists=True)) -@click.option('--expert', type=click.Path(exists=True), help="User-defined path to the expert configuration file") @click.argument("species1") @click.argument("species2") +@click.option('--expert', type=click.Path(exists=True), help="User-defined path to the expert configuration file") @click.option("--n-threads", type=int, default=4, help="Number of threads (default: 4)") def orthologs_ks(config_file, expert, species1, species2, n_threads): """ - Performs ortholog Ks estimation for SPECIES1 and SPECIES2 through wgd. + Performs ortholog Ks estimation through wgd for SPECIES1 and SPECIES2. Takes parameters from CONFIG_FILE. @@ -80,6 +90,8 @@ def orthologs_ks(config_file, expert, species1, species2, n_threads): CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species SPECIES1: first of the two species involved in the ortholog Ks estimation SPECIES2: second of the two species involved in the ortholog Ks estimation + \b + Example: ksrates orthologs-ks config_file.txt species1 species2 --n-threads 4 """ from ksrates.wgd_orthologs import wgd_orthologs click.format_filename(config_file) @@ -101,7 +113,10 @@ def orthologs_analysis(config_file, expert, ortholog_pairs): Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates orthologs-analysis config_file.txt """ from ksrates.compute_peaks import compute_peaks click.format_filename(config_file) @@ -125,7 +140,10 @@ def orthologs_adjustment(config_file, expert, trios): Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates orthologs-adjustment config_file.txt """ from ksrates.correct import correct click.format_filename(config_file) @@ -151,7 +169,10 @@ def plot_paralogs(config_file, expert, adjustment_table, paranome_table, anchors Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates plot-paralogs config_file.txt """ from ksrates.plot_paralogs import plot_paralogs_distr click.format_filename(config_file) @@ -182,7 +203,10 @@ def plot_tree(config_file, expert, adjustment_table, nextflow): Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates plot-tree config_file.txt """ from ksrates.plot_tree import plot_tree_rates click.format_filename(config_file) @@ -205,7 +229,10 @@ def plot_orthologs(config_file, expert, trios): Takes parameters from CONFIG_FILE. + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates plot-orthologs config_file.txt """ from ksrates.plot_orthologs import plot_orthologs_distr click.format_filename(config_file) @@ -239,8 +266,11 @@ def paralogs_analyses(config_file, expert, paranome_table, anchors_table, recipr If extra methods are asked through the expert configuration file, performs all methods available for the analysis type(s) selected. Takes parameters from CONFIG_FILE. - + + \b CONFIG_FILE: configuration file to set up the rate-adjustment relative to the focal species + \b + Example: ksrates paralogs-analyses config_file.txt """ from ksrates.paralogs_analyses import paralogs_analyses_methods click.format_filename(config_file) @@ -270,6 +300,104 @@ def paralogs_analyses(config_file, expert, paranome_table, anchors_table, recipr adjustment_table, anchorpoints, multiplicons, segments, list_elements, multiplicon_pairs) +@cli.command(context_settings={'help_option_names': ['-h', '--help']}, short_help="Performs multiple paralog Ks estimations.") +@click.argument('config_sources', nargs=-1, required=True, type=click.Path(exists=True)) +@click.option('--expert', type=click.Path(exists=True), help="User-defined path to the expert configuration file") +@click.option("--n-threads", type=int, default=4, help="Number of threads (default: 4)") +def paralogs_ks_multi(config_sources, expert, n_threads): + """ + Performs paralog Ks estimation through wgd for multiple focal species by looping through the configuration files provided as argument. + + Takes parameters from CONFIG_SOURCES. + + \b + CONFIG_SOURCES: one or more ksrates configuration files and/or directories containing such files + \b + Example 1: ksrates paralogs-ks-multi config_file1.txt config_file2.txt + Example 2: ksrates paralogs-ks-multi config_file*.txt + Example 3: ksrates paralogs-ks-multi config_file*.txt config_dir + """ + import os + from ksrates.wgd_paralogs import wgd_paralogs + if expert: + click.format_filename(expert) + else: + expert = "" + + # Process any provided source of configuration files (either files or directories containing files) + for source in config_sources: + source = os.path.abspath(source) + + # If configuration source is a file, use it directly to launch the command + if os.path.isfile(source): + click.format_filename(source) + wgd_paralogs(source, expert, n_threads) + print("") + + # Else if configuration source is a directory, loop through the files thereby contained and use them + elif os.path.isdir(source): + for config_file in os.listdir(source): + config_file = os.path.join(source, config_file) + click.format_filename(config_file) + wgd_paralogs(config_file, expert, n_threads) + print("") + + +@cli.command(context_settings={'help_option_names': ['-h', '--help']}, short_help="Delete all ortholog BLAST tables.") +@click.argument('orthologs_dir_path', type=click.Path(exists=True)) +@click.option("--dry-run", is_flag=True, help=("Dry run that only simulates deletion")) +def orthologs_ks_cleanup(orthologs_dir_path, dry_run): + """ + Deletes all ortholog BLAST tables from an ortholog_distributions directory to free disk space. + + Ortholog BLAST tables ("species1_species2.blast.tsv") easily weight up to 500MB and + are of little use after that the related ortholog Ks estimates have been estimated ("species1_species2.ks.tsv"). + Therefore, when numerous ortholog pipelines have already been run, the BLAST tables can take up quite some GBs of disk space + and it might be convenient to delete them in bulk through this command. + + Note the paralog BLAST tables within the paralog_distributions directory are instead meant to be preserved and + will not be affected by this command. + + Takes parameters from ORTHOLOGS_DIR_PATH. + + \b + ORTHOLOGS_DIR_PATH: path to the ortholog distribution directory containing the BLAST tables to be deleted + \b + Example: ksrates orthologs-ks-cleanup test/ortholog_distribution + """ + import os + import glob + abspath = os.path.abspath(orthologs_dir_path) + if os.path.basename(abspath) == "paralog_distributions": + print("This command is meant to be used for ortholog BLAST tables, not for paralog ones.") + print("Nothing will be deleted. Exiting.") + return + + blast_list = glob.glob(f"{abspath}/wgd_*/*.blast.tsv") + if len(blast_list) == 0: + print("There are no ortholog BLAST tables to be removed") + else: + if dry_run: + print(f'Listing {len(blast_list)} ortholog BLAST tables within "{abspath}":') + for blast in blast_list: + print(f"{os.path.basename(blast)}") + print("Done") + + else: + print(f'Removing {len(blast_list)} ortholog BLAST tables within "{abspath}":') + text = input("Confirm deleting (y/N)? ").lower() + if text == "y" or text == "yes": + for blast in blast_list: + os.remove(blast) + print(f"Deleted: {os.path.basename(blast)}") + print("Done") + + elif text == "n" or text == "no" or text == "": + print("Cancelled") + + else: + print('Please choose between "y" or "n". Cancelled.') + # For debugging # Syntax: python3 ksrates_cli.py [command] [args] if __name__ == "__main__":