Mercurial > repos > rnateam > aresite2
comparison aresite2.py @ 0:a203c4ed9166 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/aresite2 commit f437f5e8c6c856c6f0bb8d8ea6e34f5441ee088f
author | rnateam |
---|---|
date | Thu, 02 Feb 2017 19:32:30 -0500 |
parents | |
children | ed3a79e21230 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a203c4ed9166 |
---|---|
1 # A simple tool to connect to the AREsite server and retrieve feature | |
2 # information using the AREsite REST Interface. | |
3 # Parts of this code are from https://toolshed.g2.bx.psu.edu/repos/earlhaminst/ensembl_get_feature_info | |
4 import json | |
5 import optparse | |
6 import sys | |
7 import urllib.request, urllib.parse, urllib.error | |
8 import urllib.request, urllib.error, urllib.parse | |
9 import time | |
10 import requests | |
11 from six.moves.urllib.parse import urljoin | |
12 | |
13 usage = "usage: %prog [options] arg1 arg2" | |
14 parser = optparse.OptionParser(usage=usage) | |
15 parser.add_option('-g', '--gene', help='Gene ID to search for') | |
16 parser.add_option('-m', '--motif', help='Motif to look for', default='ATTTA', type=str) | |
17 parser.add_option('-s', '--species', type='choice', | |
18 choices=['Homo_sapiens', 'Mus_musculus', 'Danio_rerio', 'Drosophila_melanogaster', 'Caenorhabditis_elegans'], default='Homo_sapiens', | |
19 help='Specify the species to investigate') | |
20 options, args = parser.parse_args() | |
21 | |
22 if options.gene is None: | |
23 raise Exception('- Specify the gene you want to look for!') | |
24 | |
25 if "," in options.motif : | |
26 raise Exception('- Please only search for single motifs at once') | |
27 | |
28 class AREsiteRestClient(object): | |
29 def __init__(self, server='http://rna.tbi.univie.ac.at/AREsite2/api/', reqs_per_sec=1): | |
30 self.server = server | |
31 self.reqs_per_sec = reqs_per_sec | |
32 self.req_count = 0 | |
33 self.last_req = 0 | |
34 | |
35 def perform_rest_action(self, endpoint, hdrs=None, params=None): | |
36 if hdrs is None: | |
37 hdrs = {} | |
38 | |
39 if 'Content-Type' not in hdrs: | |
40 hdrs['Content-Type'] = 'application/json' | |
41 | |
42 if params: | |
43 endpoint += '?' + urllib.parse.urlencode(params) | |
44 | |
45 data = None | |
46 | |
47 # check if we need to rate limit ourselves | |
48 if self.req_count >= self.reqs_per_sec: | |
49 delta = time.time() - self.last_req | |
50 if delta < 1: | |
51 time.sleep(1 - delta) | |
52 self.last_req = time.time() | |
53 self.req_count = 0 | |
54 | |
55 try: | |
56 request = urllib.request.Request(self.server + endpoint, headers=hdrs) | |
57 response = urllib.request.urlopen(request) | |
58 content = response.read().decode('utf-8') | |
59 if content: | |
60 data = json.loads(content) | |
61 self.req_count += 1 | |
62 | |
63 except urllib2.HTTPError as e: | |
64 # check if we are being rate limited by the server | |
65 if e.code == 429: | |
66 if 'Retry-After' in e.headers: | |
67 retry = e.headers['Retry-After'] | |
68 time.sleep(float(retry)) | |
69 self.perform_rest_action(endpoint, hdrs, params) | |
70 else: | |
71 sys.stderr.write('Request failed for {0}: Status code: {1.code} Reason: {1.reason}\n'.format(endpoint, e)) | |
72 | |
73 return data | |
74 | |
75 def get_motifs(self, species, gene, motifs): | |
76 query = str('?query={0}&species={1}&list={2}'.format(gene, species, motifs)) | |
77 if query: | |
78 aresite = self.perform_rest_action( | |
79 query | |
80 ) | |
81 return aresite | |
82 return None | |
83 | |
84 def run(species, gene, motifs): | |
85 client = AREsiteRestClient() | |
86 aresite = client.get_motifs(species, gene, motifs) | |
87 if aresite: | |
88 | |
89 mots = aresite["exact_motifs"] | |
90 starts = aresite["motif_starts"] | |
91 ends = aresite["motif_ends"] | |
92 chrs = aresite["chromosomes"] | |
93 strands = aresite["strands"] | |
94 transcripts = aresite["transcripts"] | |
95 genes = aresite["genes"] | |
96 evh = aresite["hur_evidence"] | |
97 evt = aresite["ttp_evidence"] | |
98 eva = aresite["auf_evidence"] | |
99 anno = aresite["annotation"] | |
100 | |
101 aresite = zip(chrs,starts,ends,mots,anno,strands,genes,transcripts,evh,evt,eva) | |
102 | |
103 def getKey(item): | |
104 return item[1] | |
105 | |
106 aresite = sorted(aresite, key=getKey) | |
107 | |
108 for site in aresite: | |
109 print("\t".join(site)) | |
110 | |
111 | |
112 if __name__ == '__main__': | |
113 run(options.species, options.gene, options.motif) |