Skip to content

Commit

Permalink
Update metaphlan2.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Beghini authored Jul 23, 2020
1 parent b5cad96 commit 7af2ecf
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions metaphlan2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
__author__ = ('Nicola Segata ([email protected]), '
'Duy Tin Truong, '
'Francesco Asnicar ([email protected])')
__version__ = '2.8'
__date__ = '31 May 2018'
__version__ = '2.8.1'
__date__ = '23 Jul 2020'


import sys
Expand Down Expand Up @@ -647,6 +647,8 @@ def read_params(args):
help="The number of CPUs to use for parallelizing the mapping [default 4]")
arg('--install', action='store_true',
help="Only checks if the MetaPhlAn2 DB is installed and installs it if not. All other parameters are ignored.")
arg('--offline', action='store_true',
help="Run MetaPhlAn2 in offline mode, no internet connection is established in order to download the DB.")
arg('--read_min_len', type=int, default=70,
help="Specify the minimum length of the reads to be considered when parsing the input file with "
"'read_fastx.py' script, default value is 70")
Expand Down Expand Up @@ -825,17 +827,20 @@ def download_unpack_tar(FILE_LIST, download_file_name, folder, bowtie2_build, np
os.remove(fna_file)


def check_and_install_database(index, bowtie2_db, bowtie2_build, nproc):
def check_and_install_database(index, bowtie2_db, bowtie2_build, nproc, offline=False):
""" Check if the database is installed, if not download and install """

if len(glob(os.path.join(bowtie2_db, "mpa_{}*".format(index)))) >= 7:
return

# download the tar archive and decompress
sys.stderr.write("\nDownloading MetaPhlAn2 database\nPlease note due to "
"the size this might take a few minutes\n")
download_unpack_tar(FILE_LIST, index, bowtie2_db, bowtie2_build, nproc)
sys.stderr.write("\nDownload complete\n")
elif offline:
sys.stderr.write("No database files found in {}. Exiting.\n".format(bowtie2_db))
exit(1)
else:
# download the tar archive and decompress
sys.stderr.write("\nDownloading MetaPhlAn2 database\nPlease note due to "
"the size this might take a few minutes\n")
download_unpack_tar(FILE_LIST, index, bowtie2_db, bowtie2_build, nproc)
sys.stderr.write("\nDownload complete\n")


def set_mapping_arguments(index, bowtie2_db):
Expand Down Expand Up @@ -1365,7 +1370,7 @@ def metaphlan2():
pars = read_params(sys.argv)

# check if the database is installed, if not then install
check_and_install_database(pars['index'], pars['bowtie2db'], pars['bowtie2_build'], pars['nproc'])
check_and_install_database(pars['index'], pars['bowtie2db'], pars['bowtie2_build'], pars['nproc'], pars['offline'])

if pars['install']:
sys.stderr.write('The database is installed\n')
Expand Down

0 comments on commit 7af2ecf

Please sign in to comment.