annotate get_feature_info.py @ 7:515e7181d5e9 draft default tip

planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit dd31db8a912937667a46f5c7c1aafd389edf596b
author earlhaminst
date Mon, 05 Dec 2022 16:29:26 +0000
parents 0618e3bd5138
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
1 # A simple tool to connect to the Ensembl server and retrieve feature
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
2 # information using the Ensembl REST API.
2
950d9d11b6fb planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 099d38157cec200f0a343579ca9babcd8acb266f
earlhaminst
parents: 1
diff changeset
3 from __future__ import print_function
950d9d11b6fb planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 099d38157cec200f0a343579ca9babcd8acb266f
earlhaminst
parents: 1
diff changeset
4
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
5 import json
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
6 import optparse
4
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
7 from itertools import islice
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
8
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
9 import requests
2
950d9d11b6fb planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 099d38157cec200f0a343579ca9babcd8acb266f
earlhaminst
parents: 1
diff changeset
10 from six.moves.urllib.parse import urljoin
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
11
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
12 parser = optparse.OptionParser()
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
13 parser.add_option('-i', '--input', help='List of Ensembl IDs')
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
14 parser.add_option('-e', '--expand', type='choice', choices=['0', '1'],
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
15 default='0',
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
16 help='Expands the search to include any connected features. e.g. If the object is a gene, its transcripts, translations and exons will be returned as well.')
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
17
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
18 parser.add_option('-f', '--format', type='choice',
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
19 choices=['full', 'condensed'], default='full',
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
20 help='Specify the formats to emit from this endpoint')
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
21 options, args = parser.parse_args()
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
22 if options.input is None:
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
23 raise Exception('-i option must be specified')
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
24
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
25
6
0618e3bd5138 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 17c65045b726d0695814bfe761e534f6521786f1"
earlhaminst
parents: 5
diff changeset
26 server = 'https://rest.ensembl.org'
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
27 ext = 'lookup/id'
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
28
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
29 headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
30 params = dict((k, getattr(options, k)) for k in ['format', 'expand'])
4
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
31
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
32 first = True
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
33
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
34 print('{')
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
35
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
36 with open(options.input) as f:
4
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
37 while True:
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
38 ids = [line.strip() for line in islice(f, 50)]
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
39 if not ids:
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
40 break
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
41 if not first:
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
42 print(",")
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
43 data = {'ids': ids}
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
44 r = requests.post(urljoin(server, ext), params=params, headers=headers,
6
0618e3bd5138 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 17c65045b726d0695814bfe761e534f6521786f1"
earlhaminst
parents: 5
diff changeset
45 data=json.dumps(data), allow_redirects=False)
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
46
4
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
47 if not r.ok:
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
48 r.raise_for_status()
1
98aba0efe77a planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
earlhaminst
parents:
diff changeset
49
4
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
50 print(r.text[1:-1])
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
51
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
52 first = False
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
53
0602dcf02768 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 2
diff changeset
54 print('}')