From 7059404c3a672db51a62264d06cec17c11ffb383 Mon Sep 17 00:00:00 2001 From: Jiajie Pu Date: Tue, 24 Sep 2024 18:08:56 +0800 Subject: [PATCH] Fix IndexError in `set_vsc_parameters()` due to misplaced right parenthesis in string's format method. Issue: When the VSG FASTA file was missing, or compressed in the .bz2 format, the program failed to output proper error messages and instead raised an "IndexError: Replacement index 1 out of range for positional args tuple." Resolution: The right parenthesis has been correctly placed to ensure the format method is properly closed. --- metaphlan/metaphlan.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/metaphlan/metaphlan.py b/metaphlan/metaphlan.py index cdf307b..da354d6 100755 --- a/metaphlan/metaphlan.py +++ b/metaphlan/metaphlan.py @@ -398,18 +398,12 @@ def set_mapping_arguments(index, bowtie2_db): def set_vsc_parameters(index, bowtie2_db): - # The corresponding VSG file downloaded from metaphlan_databases site - # is actually a bz2 compressed file (as of version vJun23, 202403) - # http://cmprod1.cibio.unitn.it/biobakery4/metaphlan_databases/ vsc_fna = os.path.join(bowtie2_db, "{}_VSG.fna".format(index)) - vsc_fna_bz2 = "{}.bz2".format(vsc_fna) vsc_vinfo = os.path.join(bowtie2_db, "{}_VINFO.csv".format(index)) - if (not os.path.isfile(vsc_fna)) and (not os.path.isfile(vsc_fna_bz2)): - sys.stderr.write("Error:\n {0} or {0}.bz2 file not found in bowtie2db folder ({1}). Re-download MetaPhlAn database.".format("{}_VSG.fna".format(index), bowtie2_db)) + if not os.path.isfile(vsc_fna): + sys.stderr.write("Error:\n {} file not found in bowtie2db folder ({}). Re-download MetaPhlAn database.".format("{}_VSG.fna".format(index), bowtie2_db)) sys.exit(1) - else: - vsc_fna = vsc_fna if os.path.isfile(vsc_fna) else vsc_fna_bz2 if not os.path.isfile(vsc_vinfo): sys.stderr.write("Error:\n {} file not found in bowtie2db folder ({}). Re-download MetaPhlAn database.".format("{}_VINFO.csv".format(index), bowtie2_db)) @@ -1447,14 +1441,6 @@ def main(): if pars['profile_vsc']: try: - # vsc_fna might be a bz2 compressed file - if vsc_fna.endswith(".bz2"): - temp_vsc_fna = os.path.join(viralTempFolder, os.path.basename(vsc_fna)[:-4]) - with bz2.open(vsc_fna, "rt") as infh, open(temp_vsc_fna, "wt") as outfh: - SeqIO.write(SeqIO.parse(infh, "fasta"), outfh, "fasta") - - vsc_fna = temp_vsc_fna - VSCs_markers = SeqIO.index(vsc_fna, "fasta") except Exception as e: sys.stderr.write('Error: "{}"\nCould not access to {}.\n'.format(e,vsc_fna))