annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
1 import sys
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
2 import os
0
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
3 from ftplib import FTP
13
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
4 import argparse
0
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
5
13
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
6 def main(args):
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
7 """ Get accession number from argument """
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
8 prefix = args.accession[0:3]
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
9 middle = args.accession[3:6]
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
10 suffix = args.accession[6:9]
0
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
11
13
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
12 ftp = FTP('ftp-trace.ncbi.nih.gov')
0
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
13
13
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
14 # Open file and transfer requested SRA as a file
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
15 # Try to change the working directory until it works
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
16 with open(args.out, 'wb') as sra:
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
17 ftpPath = os.path.join('/sra/sra-instant/reads/ByRun/sra/',
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
18 prefix,
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
19 prefix + middle,
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
20 prefix + middle + suffix)
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
21 ftp.login('ftp')
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
22 connected = False
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
23 while not connected:
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
24 try:
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
25 ftp.cwd(ftpPath)
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
26 connected = True
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
27 except:
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
28 pass
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
29 ftp.retrbinary('RETR ' + prefix + middle + suffix + '.sra', sra.write)
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
30 ftp.quit()
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
31
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
32 def arguments():
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
33 parser = argparse.ArgumentParser(description="Download an SRA from the NCBI SRA FTP")
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
34 parser.add_argument('accession', type=str, help="SRA accession ex: SRR000001")
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
35 parser.add_argument('-o', '--out', type=str, help="Name for SRA file ")
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
36 args = parser.parse_args()
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
37 return args
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
38
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
39 if __name__ == "__main__":
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
40 args = arguments()
45031bbf6b27 better sra_fetch code, compliant (?) datatype, updated dependencies in tool wrappers
Matt Shirley <mdshw5@gmail.com>
parents: 0
diff changeset
41 main(args)