comparison get_genetree.py @ 1:98aba0efe77a draft

planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/Ensembl-REST commit aaf8d501c3a92ed415fdf9293a65468c72aae984-dirty
author earlhaminst
date Mon, 12 Dec 2016 07:47:26 -0500
parents
children 950d9d11b6fb
comparison
equal deleted inserted replaced
0:f0018341e9f6 1:98aba0efe77a
1 # A simple tool to connect to the Ensembl server and retrieve genetree using
2 # the Ensembl REST API.
3 import optparse
4 from urlparse import urljoin
5
6 import requests
7
8 parser = optparse.OptionParser()
9 parser.add_option('--id_type', type='choice', default='gene_id',
10 choices=['gene_id', 'gene_tree_id'], help='Input type')
11 parser.add_option('-i', '--input', help='Ensembl ID')
12 parser.add_option('--format', type='choice',
13 choices=['json', 'orthoxml', 'phyloxml', 'nh'],
14 default='json', help='Output format')
15 parser.add_option('-s', '--sequence', type='choice',
16 choices=['protein', 'cdna', 'none'], default='protein',
17 help='The type of sequence to bring back. Setting it to none results in no sequence being returned')
18
19 parser.add_option('-g', '--species', type='choice',
20 choices=['ensembl', 'ensemblgenomes'], default='ensembl',
21 help='Specify the genome databases for vertebrates and other eukaryotic species')
22
23 parser.add_option('-a', '--aligned', type='choice', choices=['0', '1'],
24 default='0', help='Return the aligned string if true. Otherwise, return the original sequence (no insertions)')
25 parser.add_option('-c', '--cigar_line', type='choice', choices=['0', '1'],
26 default='0',
27 help='Return the aligned sequence encoded in CIGAR format')
28 parser.add_option('--nh_format', type='choice',
29 choices=['full', 'display_label_composite', 'simple', 'species', 'species_short_name', 'ncbi_taxon', 'ncbi_name', 'njtree', 'phylip'],
30 default='simple',
31 help='The format of a NH (New Hampshire) request')
32 options, args = parser.parse_args()
33 if options.input is None:
34 raise Exception('-i option must be specified')
35
36 server = 'http://rest.%s.org' % options.species
37
38 if options.id_type == 'gene_id':
39 ext = 'genetree/member/id'
40 elif options.id_type == 'gene_tree_id':
41 ext = 'genetree/id'
42
43 if options.format == 'json':
44 content_type = 'application/json'
45 elif options.format == 'orthoxml':
46 content_type = 'text/x-orthoxml+xml'
47 elif options.format == 'phyloxml':
48 content_type = 'text/x-phyloxml+xml'
49 elif options.format == 'nh':
50 content_type = 'text/x-nh'
51 headers = {'Content-Type': content_type}
52 params = dict((k, getattr(options, k)) for k in ['sequence', 'aligned', 'cigar_line', 'nh_format'])
53 r = requests.get(urljoin(server, '/'.join([ext, options.input])), params=params, headers=headers)
54
55 if not r.ok:
56 r.raise_for_status()
57
58 print r.text