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