comparison sra_fetch.py @ 13:45031bbf6b27 draft

better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
author Matt Shirley <mdshw5@gmail.com>
date Mon, 17 Jun 2013 16:17:33 -0400
parents ffdd41766195
children 082fd6374582
comparison
equal deleted inserted replaced
12:b77840618b8f 13:45031bbf6b27
1 import sys
2 import os
1 from ftplib import FTP 3 from ftplib import FTP
2 import sys 4 import argparse
3 5
4 # Get accession number from argument 6 def main(args):
5 accession = sys.argv[1] 7 """ Get accession number from argument """
6 outfile = sys.argv[2] 8 prefix = args.accession[0:3]
7 prefix = accession[0:3] 9 middle = args.accession[3:6]
8 middle = accession[3:6] 10 suffix = args.accession[6:9]
9 suffix = accession[6:9]
10 11
11 # NCBI SRA FTP site 12 ftp = FTP('ftp-trace.ncbi.nih.gov')
12 ftp = FTP('ftp-trace.ncbi.nih.gov')
13 13
14 # Open file and transfer requested SRA as a file 14 # Open file and transfer requested SRA as a file
15 # Try to change the working directory until it works 15 # Try to change the working directory until it works
16 sra = open(outfile, 'wb') 16 with open(args.out, 'wb') as sra:
17 ftp.login('ftp') 17 ftpPath = os.path.join('/sra/sra-instant/reads/ByRun/sra/',
18 connected = False 18 prefix,
19 while not connected: 19 prefix + middle,
20 try: 20 prefix + middle + suffix)
21 ftp.cwd('/sra/sra-instant/reads/ByRun/sra/' + 21 ftp.login('ftp')
22 prefix + '/' + 22 connected = False
23 prefix + middle + '/' + 23 while not connected:
24 prefix + middle + suffix + '/') 24 try:
25 connected = True 25 ftp.cwd(ftpPath)
26 except: 26 connected = True
27 pass 27 except:
28 28 pass
29 ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write) 29 ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write)
30 ftp.quit() 30 ftp.quit()
31
32 def arguments():
33 parser = argparse.ArgumentParser(description="Download an SRA from the NCBI SRA FTP")
34 parser.add_argument('accession', type=str, help="SRA accession ex: SRR000001")
35 parser.add_argument('-o', '--out', type=str, help="Name for SRA file ")
36 args = parser.parse_args()
37 return args
38
39 if __name__ == "__main__":
40 args = arguments()
41 main(args)