| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Copyright 2014, Ben Langmead <[email protected]> |
| 5 |
|
| 6 |
This file is part of Bowtie 2. |
| 7 |
|
| 8 |
Bowtie 2 is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
Bowtie 2 is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
""" |
| 21 |
|
| 22 |
import os |
| 23 |
import sys |
| 24 |
import gzip |
| 25 |
import inspect |
| 26 |
import logging |
| 27 |
import argparse |
| 28 |
import platform |
| 29 |
import subprocess |
| 30 |
|
| 31 |
|
| 32 |
def get_gunzip_size(fn): |
| 33 |
size = 0 |
| 34 |
with gzip.open(fn) as f: |
| 35 |
while True: |
| 36 |
data = f.read(8192) |
| 37 |
size += len(data) |
| 38 |
if not data: |
| 39 |
break |
| 40 |
return size |
| 41 |
|
| 42 |
def sais_enabled(build_bin_spec): |
| 43 |
output = subprocess.check_output([build_bin_spec, "--version"], universal_newlines=True) |
| 44 |
|
| 45 |
return output.find("USE_SAIS") != -1 |
| 46 |
|
| 47 |
def main(): |
| 48 |
parser = argparse.ArgumentParser(add_help = False) |
| 49 |
|
| 50 |
parser.add_argument('--large-index', action='store_true') |
| 51 |
parser.add_argument('--verbose', action='store_true') |
| 52 |
|
| 53 |
group = parser.add_mutually_exclusive_group() |
| 54 |
group.add_argument('--debug', action='store_true') |
| 55 |
group.add_argument('--sanitized', action='store_true') |
| 56 |
|
| 57 |
logging.basicConfig(level=logging.ERROR, |
| 58 |
format='%(levelname)s: %(message)s' |
| 59 |
) |
| 60 |
delta = 200 |
| 61 |
small_index_max_size= 4 * 1024**3 - delta |
| 62 |
build_bin_name = 'bowtie2-build' |
| 63 |
build_bin_s = 'bowtie2-build-s' |
| 64 |
build_bin_l = 'bowtie2-build-l' |
| 65 |
curr_script = os.path.realpath(inspect.getsourcefile(main)) |
| 66 |
ex_path = os.path.dirname(curr_script) |
| 67 |
build_bin_spec = os.path.join(ex_path,build_bin_s) |
| 68 |
|
| 69 |
if sais_enabled(build_bin_spec): |
| 70 |
small_index_max_size = 2 * 1024 ** 3 - delta |
| 71 |
|
| 72 |
script_options, argv = parser.parse_known_args() |
| 73 |
|
| 74 |
if script_options.verbose: |
| 75 |
logging.getLogger().setLevel(logging.INFO) |
| 76 |
|
| 77 |
if script_options.debug: |
| 78 |
build_bin_spec += "-debug" |
| 79 |
build_bin_l += "-debug" |
| 80 |
|
| 81 |
if script_options.sanitized: |
| 82 |
build_bin_spec += "-sanitized" |
| 83 |
build_bin_l += "-sanitized" |
| 84 |
|
| 85 |
fastas = [] |
| 86 |
if "-c" not in argv and len(argv) >= 2: |
| 87 |
for index in reversed(range(len(argv) - 1)): |
| 88 |
arg = argv[index] |
| 89 |
if arg.startswith("-") or arg.isdigit(): |
| 90 |
break |
| 91 |
fastas.insert(0, arg) |
| 92 |
argv.remove(arg) |
| 93 |
if fastas: |
| 94 |
fastas = ",".join(fastas) |
| 95 |
argv.insert(len(argv) - 1, fastas) |
| 96 |
|
| 97 |
if script_options.large_index: |
| 98 |
build_bin_spec = os.path.join(ex_path, build_bin_l) |
| 99 |
elif fastas: |
| 100 |
tot_size = 0 |
| 101 |
for fn in fastas.split(","): |
| 102 |
if os.path.exists(fn): |
| 103 |
if fn.endswith(".gz"): |
| 104 |
tot_size += get_gunzip_size(fn) |
| 105 |
elif fn.endswith(".zst"): |
| 106 |
sys.stderr.write( |
| 107 |
"{} cannot determine the uncompressed size of ZSTD compressed files.\ |
| 108 |
Please make sure that the uncompressed file size is suitable for\ |
| 109 |
building the current index".format( |
| 110 |
curr_script |
| 111 |
) |
| 112 |
) |
| 113 |
else: |
| 114 |
statinfo = os.stat(fn) |
| 115 |
tot_size += statinfo.st_size |
| 116 |
if tot_size > small_index_max_size: |
| 117 |
build_bin_spec = os.path.join(ex_path, build_bin_l) |
| 118 |
|
| 119 |
if "Windows" in platform.platform(): |
| 120 |
build_bin_spec += '.exe' |
| 121 |
|
| 122 |
if not os.path.exists(build_bin_spec): |
| 123 |
sys.stderr.write( |
| 124 |
"{0} does not exist, try running `[g]make {0}'\n".format( |
| 125 |
os.path.basename(build_bin_spec) |
| 126 |
) |
| 127 |
) |
| 128 |
sys.exit(1) |
| 129 |
|
| 130 |
argv.insert(0, "basic-0") |
| 131 |
argv.insert(0, "--wrapper") |
| 132 |
argv.insert(0, build_bin_spec) |
| 133 |
logging.info("Command: %s" % " ".join(argv)) |
| 134 |
sys.exit(subprocess.call(argv)) |
| 135 |
|
| 136 |
|
| 137 |
if __name__ == "__main__": |
| 138 |
main() |