Mercurial > repos > matt-shirley > sra_tools
changeset 14:082fd6374582 draft
fallback to getopt for fetch_sra.py
author | Matt Shirley <mdshw5@gmail.com> |
---|---|
date | Mon, 17 Jun 2013 17:07:57 -0400 |
parents | 45031bbf6b27 |
children | 078b8e168b0c |
files | sra_fetch.py |
diffstat | 1 files changed, 33 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/sra_fetch.py Mon Jun 17 16:17:33 2013 -0400 +++ b/sra_fetch.py Mon Jun 17 17:07:57 2013 -0400 @@ -1,19 +1,24 @@ import sys import os from ftplib import FTP -import argparse +try: + import argparse +except ImportError: + import getopt def main(args): """ Get accession number from argument """ - prefix = args.accession[0:3] - middle = args.accession[3:6] - suffix = args.accession[6:9] + if module_exists('argparse'): + args = dict(vars(args)) + prefix = args['accession'][0:3] + middle = args['accession'][3:6] + suffix = args['accession'][6:9] ftp = FTP('ftp-trace.ncbi.nih.gov') - + print args # Open file and transfer requested SRA as a file # Try to change the working directory until it works - with open(args.out, 'wb') as sra: + with open(args['out'], 'wb') as sra: ftpPath = os.path.join('/sra/sra-instant/reads/ByRun/sra/', prefix, prefix + middle, @@ -29,13 +34,33 @@ ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write) ftp.quit() +def module_exists(module_name): + try: + __import__(module_name) + except ImportError: + return False + else: + return True + def arguments(): parser = argparse.ArgumentParser(description="Download an SRA from the NCBI SRA FTP") - parser.add_argument('accession', type=str, help="SRA accession ex: SRR000001") + parser.add_argument('-a', '--accession', type=str, help="SRA accession ex: SRR000001") parser.add_argument('-o', '--out', type=str, help="Name for SRA file ") args = parser.parse_args() return args if __name__ == "__main__": - args = arguments() + if module_exists('argparse'): + args = arguments() + else: + ## fall back to getopt for python < 2.7 + args = dict() + options, remainder = getopt.getopt(sys.argv[1:], 'a:o:', ['accession=', 'out=']) + print options + for opt, arg in options: + if opt in ('-a', '--accession'): + args['accession'] = arg + elif opt in ('-o', '--out'): + args['out'] = arg + main(args)