Mercurial > repos > earlhaminst > ensembl_get_sequences
comparison get_feature_info.py @ 7:c79ce2342f1e draft default tip
planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit 8f8808de862973aedbf87abd4dfa9d2dc7219322
author | earlhaminst |
---|---|
date | Mon, 17 Feb 2025 14:49:24 +0000 |
parents | 7af66c2b3831 |
children |
comparison
equal
deleted
inserted
replaced
6:7af66c2b3831 | 7:c79ce2342f1e |
---|---|
1 # A simple tool to connect to the Ensembl server and retrieve feature | 1 # A simple tool to connect to the Ensembl server and retrieve feature |
2 # information using the Ensembl REST API. | 2 # information using the Ensembl REST API. |
3 from __future__ import print_function | |
4 | |
5 import json | 3 import json |
6 import optparse | 4 import optparse |
7 from itertools import islice | 5 from itertools import islice |
6 from urllib.parse import urljoin | |
8 | 7 |
9 import requests | 8 import requests |
10 from six.moves.urllib.parse import urljoin | |
11 | 9 |
12 parser = optparse.OptionParser() | 10 parser = optparse.OptionParser() |
13 parser.add_option('-i', '--input', help='List of Ensembl IDs') | 11 parser.add_option("-i", "--input", help="List of Ensembl IDs") |
14 parser.add_option('-e', '--expand', type='choice', choices=['0', '1'], | 12 parser.add_option( |
15 default='0', | 13 "-e", |
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.') | 14 "--expand", |
15 type="choice", | |
16 choices=["0", "1"], | |
17 default="0", | |
18 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.", | |
19 ) | |
17 | 20 |
18 parser.add_option('-f', '--format', type='choice', | 21 parser.add_option( |
19 choices=['full', 'condensed'], default='full', | 22 "-f", |
20 help='Specify the formats to emit from this endpoint') | 23 "--format", |
24 type="choice", | |
25 choices=["full", "condensed"], | |
26 default="full", | |
27 help="Specify the formats to emit from this endpoint", | |
28 ) | |
21 options, args = parser.parse_args() | 29 options, args = parser.parse_args() |
22 if options.input is None: | 30 if options.input is None: |
23 raise Exception('-i option must be specified') | 31 raise Exception("-i option must be specified") |
24 | 32 |
25 | 33 |
26 server = 'https://rest.ensembl.org' | 34 server = "https://rest.ensembl.org" |
27 ext = 'lookup/id' | 35 ext = "lookup/id" |
28 | 36 |
29 headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} | 37 headers = {"Content-Type": "application/json", "Accept": "application/json"} |
30 params = dict((k, getattr(options, k)) for k in ['format', 'expand']) | 38 params = {k: getattr(options, k) for k in ("format", "expand")} |
31 | 39 |
32 first = True | 40 first = True |
33 | 41 |
34 print('{') | 42 print("{") |
35 | 43 |
36 with open(options.input) as f: | 44 with open(options.input) as f: |
37 while True: | 45 while True: |
38 ids = [line.strip() for line in islice(f, 50)] | 46 ids = [line.strip() for line in islice(f, 50)] |
39 if not ids: | 47 if not ids: |
40 break | 48 break |
41 if not first: | 49 if not first: |
42 print(",") | 50 print(",") |
43 data = {'ids': ids} | 51 data = {"ids": ids} |
44 r = requests.post(urljoin(server, ext), params=params, headers=headers, | 52 r = requests.post( |
45 data=json.dumps(data), allow_redirects=False) | 53 urljoin(server, ext), |
54 params=params, | |
55 headers=headers, | |
56 data=json.dumps(data), | |
57 allow_redirects=False, | |
58 ) | |
46 | 59 |
47 if not r.ok: | 60 if not r.ok: |
48 r.raise_for_status() | 61 r.raise_for_status() |
49 | 62 |
50 print(r.text[1:-1]) | 63 print(r.text[1:-1]) |
51 | 64 |
52 first = False | 65 first = False |
53 | 66 |
54 print('}') | 67 print("}") |