-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembl_batch_xml_retrieval.py
77 lines (65 loc) · 2.07 KB
/
embl_batch_xml_retrieval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
#embl_genome_batch_retrieval.py
#Gabriel Kigundu
#May 26, 2017
docstring = """
Having a list of embl accessions, this program downloads the sequences corresponding to those accessions.
For more info, visit www.ebi.ac.uk/ena/browse/data-retrieval-rest#retrieval_multiple_identifiers
"""
#embl allows retrieval of data using the base url http://www.ebi.ac.uk/ena/data/view/
import os
import sys
import argparse
import requests
from argparse import RawDescriptionHelpFormatter
infile=''
outfile=''
readfile=False
writefile = False
parser = argparse.ArgumentParser(description=docstring, formatter_class=RawDescriptionHelpFormatter)
parser.add_argument('-o', '-out', '--output', help='Output folder name', required=False)
## show values ##
if not sys.stdin.isatty():
infile = sys.stdin
parser.add_argument('-i','-in','--input', help='Input file name',required=False)
else:
parser.add_argument('-i','-in','--input', help='Input file name',required=True)
readfile = True
infile = open(parser.parse_args().input, 'r')
args = parser.parse_args()
if args.output is not None:
if args.output.endswith(os.sep):
args.output = args.output[:-1]
if not os.path.exists(args.output):
try:
os.makedirs(args.output)
except Exception as e:
print(e)
sys.exit('could not create output folder')
writefile = True
#read accessions
accessions = []
for line in infile:
line = line.strip()
accessions.append(line)
#remove last comma
#acc = acc[:-1]
#create url
baseurl = 'http://www.ebi.ac.uk/ena/data/view/display=xml&'
count = 0
for acc in accessions:
count += 1
url = baseurl + acc
request = requests.get(url).text
if writefile:
temp_output = os.path.join(args.output, acc)
with open(temp_output, 'w') as outfile:
outfile.write(request)
outfile.write('\n')
print(count)
else:
print(request)
if readfile:
print ("Input file: %s" % args.input )
infile.close()
sys.exit("embl_genome_batch_retrieval.py complete" )