| 1 |
#!/usr/bin/env python |
| 2 |
"""Get sequences from NCBI by GenBank/RefSeq ID.""" |
| 3 |
|
| 4 |
from argparse import ArgumentParser, SUPPRESS |
| 5 |
import sys |
| 6 |
|
| 7 |
from .core import download_to_file, generate_url, Config, HAVE_BIOPYTHON |
| 8 |
from .errors import ( |
| 9 |
DownloadError, |
| 10 |
InvalidIdError, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
def main(): |
| 15 |
"""Command line handling.""" |
| 16 |
parser = ArgumentParser() |
| 17 |
|
| 18 |
parser.add_argument('ids', nargs='+', metavar='NCBI-accession') |
| 19 |
parser.add_argument('-m', '--molecule', default="nucleotide", choices=["nucleotide", "protein"], |
| 20 |
help="Molecule type to download. Default: %(default)s") |
| 21 |
parser.add_argument('--api-key', default=SUPPRESS, |
| 22 |
help="Specify USER NCBI API key. More info at https://www.ncbi.nlm.nih.gov/books/NBK25497/") |
| 23 |
if HAVE_BIOPYTHON: |
| 24 |
parser.add_argument('-e', '--extended-validation', action="store", default='none', |
| 25 |
choices=('none', 'loads', 'all', 'correct'), |
| 26 |
help="Perform extended validation. Possible options are 'none' to skip validation, " |
| 27 |
"'loads' to check if the sequence file loads in Biopython, " |
| 28 |
"or 'all' to run all checks. Default: %(default)s") |
| 29 |
parser.add_argument('-F', '--format', action="store", default='genbank', |
| 30 |
choices=('fasta', 'genbank', 'featuretable', 'gff3'), |
| 31 |
help="File format to download nucleotide sequences in. Default: %(default)s") |
| 32 |
parser.add_argument('-o', '--out', default=SUPPRESS, |
| 33 |
help="Single filename to use for the combined output.") |
| 34 |
parser.add_argument('-p', '--prefix', default=SUPPRESS, |
| 35 |
help="Filename prefix to use for output files instead of using the NCBI ID.") |
| 36 |
parser.add_argument('-g', '--range', default=SUPPRESS, |
| 37 |
help="region to subset accession. only for single accession") |
| 38 |
if HAVE_BIOPYTHON: |
| 39 |
parser.add_argument('-r', '--recursive', action="store_true", default=False, |
| 40 |
help="Recursively get all entries of a WGS entry.") |
| 41 |
parser.add_argument('--url', action="store_true", default=False, |
| 42 |
help="Instead of downloading the sequences, just print the URLs to stdout.") |
| 43 |
parser.add_argument('-v', '--verbose', action="store_true", default=False, |
| 44 |
help="Print a progress indicator.") |
| 45 |
|
| 46 |
opts = parser.parse_args() |
| 47 |
|
| 48 |
config = Config.from_args(opts) |
| 49 |
if len(opts.ids) > 1 and config.range != "none": |
| 50 |
raise ValueError("Ambiguous range for multiple ids") |
| 51 |
|
| 52 |
# TODO: Change this to download multiple records at once? |
| 53 |
for i, dl_id in enumerate(opts.ids): |
| 54 |
filename = None |
| 55 |
append = False |
| 56 |
if 'prefix' in opts: |
| 57 |
filename = "{fn}_{i}".format(fn=opts.prefix, i=i) |
| 58 |
elif 'out' in opts: |
| 59 |
filename = opts.out |
| 60 |
append = (i > 0) |
| 61 |
|
| 62 |
try: |
| 63 |
if opts.url: |
| 64 |
print(generate_url(dl_id, config)) |
| 65 |
else: |
| 66 |
download_to_file(dl_id, config, filename, append) |
| 67 |
except InvalidIdError as err: |
| 68 |
print("NCBI Entrez returned error code {e.status_code}, are ID(s) {e.ids} valid?".format(e=err)) |
| 69 |
sys.exit(1) |
| 70 |
except DownloadError as err: |
| 71 |
print(err, file=sys.stderr) |
| 72 |
sys.exit(1) |
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__": |
| 76 |
main() |