Mercurial > repos > matt-shirley > sra_tools
comparison sra_fetch.py @ 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 | 93a60318b9ca |
comparison
equal
deleted
inserted
replaced
13:45031bbf6b27 | 14:082fd6374582 |
---|---|
1 import sys | 1 import sys |
2 import os | 2 import os |
3 from ftplib import FTP | 3 from ftplib import FTP |
4 import argparse | 4 try: |
5 import argparse | |
6 except ImportError: | |
7 import getopt | |
5 | 8 |
6 def main(args): | 9 def main(args): |
7 """ Get accession number from argument """ | 10 """ Get accession number from argument """ |
8 prefix = args.accession[0:3] | 11 if module_exists('argparse'): |
9 middle = args.accession[3:6] | 12 args = dict(vars(args)) |
10 suffix = args.accession[6:9] | 13 prefix = args['accession'][0:3] |
14 middle = args['accession'][3:6] | |
15 suffix = args['accession'][6:9] | |
11 | 16 |
12 ftp = FTP('ftp-trace.ncbi.nih.gov') | 17 ftp = FTP('ftp-trace.ncbi.nih.gov') |
13 | 18 print args |
14 # Open file and transfer requested SRA as a file | 19 # Open file and transfer requested SRA as a file |
15 # Try to change the working directory until it works | 20 # Try to change the working directory until it works |
16 with open(args.out, 'wb') as sra: | 21 with open(args['out'], 'wb') as sra: |
17 ftpPath = os.path.join('/sra/sra-instant/reads/ByRun/sra/', | 22 ftpPath = os.path.join('/sra/sra-instant/reads/ByRun/sra/', |
18 prefix, | 23 prefix, |
19 prefix + middle, | 24 prefix + middle, |
20 prefix + middle + suffix) | 25 prefix + middle + suffix) |
21 ftp.login('ftp') | 26 ftp.login('ftp') |
27 except: | 32 except: |
28 pass | 33 pass |
29 ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write) | 34 ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write) |
30 ftp.quit() | 35 ftp.quit() |
31 | 36 |
37 def module_exists(module_name): | |
38 try: | |
39 __import__(module_name) | |
40 except ImportError: | |
41 return False | |
42 else: | |
43 return True | |
44 | |
32 def arguments(): | 45 def arguments(): |
33 parser = argparse.ArgumentParser(description="Download an SRA from the NCBI SRA FTP") | 46 parser = argparse.ArgumentParser(description="Download an SRA from the NCBI SRA FTP") |
34 parser.add_argument('accession', type=str, help="SRA accession ex: SRR000001") | 47 parser.add_argument('-a', '--accession', type=str, help="SRA accession ex: SRR000001") |
35 parser.add_argument('-o', '--out', type=str, help="Name for SRA file ") | 48 parser.add_argument('-o', '--out', type=str, help="Name for SRA file ") |
36 args = parser.parse_args() | 49 args = parser.parse_args() |
37 return args | 50 return args |
38 | 51 |
39 if __name__ == "__main__": | 52 if __name__ == "__main__": |
40 args = arguments() | 53 if module_exists('argparse'): |
54 args = arguments() | |
55 else: | |
56 ## fall back to getopt for python < 2.7 | |
57 args = dict() | |
58 options, remainder = getopt.getopt(sys.argv[1:], 'a:o:', ['accession=', 'out=']) | |
59 print options | |
60 for opt, arg in options: | |
61 if opt in ('-a', '--accession'): | |
62 args['accession'] = arg | |
63 elif opt in ('-o', '--out'): | |
64 args['out'] = arg | |
65 | |
41 main(args) | 66 main(args) |