-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_ena_details.py
141 lines (105 loc) · 4.39 KB
/
get_ena_details.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright William Lees
#
# This source code, and any executable file compiled or derived from it, is governed by the European Union Public License v. 1.2,
# the English version of which is available here: https://perma.cc/DK5U-NDVE
#
# Get accession detains from ENA
import requests
import argparse
import sys
import re
import html
import time
import xml.etree.ElementTree as ET
def get_ena_project_details(prj_id):
ret = {}
if len(prj_id) < 5 or (prj_id[:5] != 'PRJEB'):
raise ValueError('bady formatted project id: %s' % (prj_id))
try:
r = requests.get('https://www.ebi.ac.uk/ena/browser/api/xml/%s' % prj_id)
if r.status_code != 200:
raise ValueError('Unexpected response from ENA: status %d' % r.status_code)
foo = r.content.decode("utf-8")
root = ET.fromstring(r.content)
except Exception as e:
exc_type, exc_value = sys.exc_info()[:2]
raise ValueError('Error fetching project info from ENA: %s' % (exc_value))
titles = root.findall("./PROJECT/TITLE")
if len(titles) > 0:
ret['title'] = titles[0].text if len(titles) > 0 else ''
ret['url'] = 'https://www.ebi.ac.uk/ena/browser/api/xml/%s' % prj_id
else:
raise ValueError('Error fetching nucleotide info from ENA: %s' % (root.text))
return ret
def get_ena_nuc_details(nuc_id):
ret = {}
if len(nuc_id) < 5:
raise ValueError('badly formatted nucleotide sequence record accession number: %s' % (nuc_id))
try:
title = ''
request = 'https://www.ebi.ac.uk/ena/browser/api/xml/%s' % nuc_id
r = requests.get(request)
if r.status_code == 200:
root = ET.fromstring(r.content)
entries = root.findall("./entry/description")
title = entries[0].text
else:
request = 'https://www.ebi.ac.uk/ena/browser/api/embl/%s' % nuc_id
r = requests.get(request)
if r.status_code == 200:
lines = r.content.decode('utf-8')
lines = lines.split('\n')
for line in lines:
if line[:4] == 'DE ':
title += line[4:]
else:
raise ValueError('Unexpected response from ENA: status %d' % r.status_code)
except Exception as e:
exc_type, exc_value = sys.exc_info()[:2]
raise ValueError('Error fetching nucleotide info from ENA: %s' % (exc_value))
if len(title) > 0:
ret['title'] = title
ret['url'] = request
else:
raise ValueError('Error fetching nucleotide info from ENA: %s' % (root.text))
return ret
def get_ena_srr_details(srr_id):
ret = {}
if len(srr_id) < 5:
raise ValueError('badly formatted sequence set accession number: %s' % (srr_id))
try:
r = requests.get('https://www.ebi.ac.uk/ena/browser/api/xml/%s' % srr_id)
if r.status_code != 200:
raise ValueError('Unexpected response from ENA: status %d' % r.status_code)
root = ET.fromstring(r.content)
except Exception as e:
exc_type, exc_value = sys.exc_info()[:2]
raise ValueError('Error fetching nucleotide info from ENA: %s' % (exc_value))
titles = root.findall("./EXPERIMENT/TITLE")
if len(titles) == 0:
titles = root.findall("./RUN/TITLE")
if len(titles) > 0:
ret['title'] = titles[0].text
ret['url'] = 'https://www.ebi.ac.uk/ena/data/view/' + srr_id
else:
raise ValueError('Error fetching nucleotide info from ENA: %s' % (root.text))
return ret
def get_ena_samn_details(sam_id):
ret = {}
if len(sam_id) < 5:
raise ValueError('badly formatted sample accession number: %s' % (sam_id))
try:
r = requests.get('https://www.ebi.ac.uk/ena/browser/api/xml/%s' % sam_id)
if r.status_code != 200:
raise ValueError('Unexpected response from ENA: status %d' % r.status_code)
root = ET.fromstring(r.content)
except Exception as e:
exc_type, exc_value = sys.exc_info()[:2]
raise ValueError('Error fetching nucleotide info from ENA: %s' % (exc_value))
titles = root.findall("./SAMPLE/TITLE")
if len(titles) > 0:
ret['title'] = titles[0].text if len(titles) > 0 else ''
ret['url'] = 'https://www.ebi.ac.uk/ena/data/view/' + sam_id
else:
raise ValueError('Error fetching nucleotide info from ENA: %s' % (root.text))
return ret