1 | """ |
---|
2 | Contains the UCSC proxy |
---|
3 | """ |
---|
4 | |
---|
5 | from galaxy.web.base.controller import * |
---|
6 | |
---|
7 | import sys |
---|
8 | from galaxy import web, util |
---|
9 | |
---|
10 | import re, urllib, logging |
---|
11 | |
---|
12 | log = logging.getLogger( __name__ ) |
---|
13 | |
---|
14 | class UCSCProxy( BaseController ): |
---|
15 | |
---|
16 | def create_display(self, store): |
---|
17 | """Creates a more meaningulf display name""" |
---|
18 | track = store.get('hgta_track','no track') |
---|
19 | table = store.get('hgta_table','no table') |
---|
20 | region = store.get('hgta_regionType','') |
---|
21 | if region not in [ 'genome', 'encode']: |
---|
22 | region = store.get('position','') |
---|
23 | if track == table: |
---|
24 | display = 'UCSC: %s (%s)' % (track, region) |
---|
25 | else: |
---|
26 | display = 'UCSC: %s, %s (%s)' % (track, table, region) |
---|
27 | return display |
---|
28 | |
---|
29 | @web.expose |
---|
30 | def index(self, trans, init=False, **kwd): |
---|
31 | base_url = None |
---|
32 | params = dict(kwd) |
---|
33 | try: |
---|
34 | store = params.get("__GALAXY__", None) |
---|
35 | if store: |
---|
36 | store = util.string_to_object(store) |
---|
37 | else: |
---|
38 | store = {} |
---|
39 | UCSC_URL = 'UCSC_URL' |
---|
40 | base_url = store.get(UCSC_URL, "http://genome.ucsc.edu/cgi-bin/hgTables?") |
---|
41 | params = dict(kwd) |
---|
42 | params['init'] = init |
---|
43 | |
---|
44 | if not init: |
---|
45 | for key, value in kwd.items(): |
---|
46 | store[key] = value |
---|
47 | try: del store["__GALAXY__"] |
---|
48 | except: pass |
---|
49 | else: |
---|
50 | store = {} |
---|
51 | |
---|
52 | if init == "1": |
---|
53 | base_url = "http://genome.ucsc.edu/cgi-bin/hgTables?" |
---|
54 | params['db'] = 'hg17' |
---|
55 | if init == "2": |
---|
56 | base_url = "http://genome-test.cse.ucsc.edu/cgi-bin/hgTables?" |
---|
57 | params['db'] = 'hg17' |
---|
58 | if init == "3": |
---|
59 | base_url = "http://archaea.ucsc.edu/cgi-bin/hgTables?" |
---|
60 | |
---|
61 | store[UCSC_URL] = base_url |
---|
62 | |
---|
63 | try: del params["__GALAXY__"] |
---|
64 | except: pass |
---|
65 | url = base_url + urllib.urlencode(params) |
---|
66 | |
---|
67 | page = urllib.urlopen(url) |
---|
68 | content = page.info().get('Content-type', '') |
---|
69 | except Exception, exc: |
---|
70 | trans.log_event( "Proxy Error -> %s" % str(exc) ) |
---|
71 | msg = 'There has been a problem connecting to <i>%s</i> <p> <b>%s<b>' % (base_url, exc) |
---|
72 | return msg |
---|
73 | |
---|
74 | if content.startswith('text/plain'): |
---|
75 | params['display'] = self.create_display(store) |
---|
76 | params['dbkey'] = store.get('db', '*') |
---|
77 | params['tool_id'] = 'ucsc_proxy' |
---|
78 | params['proxy_url'] = base_url |
---|
79 | params['runtool_btn'] = 'T' |
---|
80 | #url = "/echo?" + urllib.urlencode(params) |
---|
81 | url = "/tool_runner/index?" + urllib.urlencode(params) |
---|
82 | trans.response.send_redirect(url) |
---|
83 | else: |
---|
84 | try: |
---|
85 | text = page.read() |
---|
86 | |
---|
87 | # Serialize store into a form element |
---|
88 | store_text = "<INPUT TYPE=\"HIDDEN\" NAME=\"__GALAXY__\" ID=\"__GALAXY__\" VALUE=\"" \ |
---|
89 | + util.object_to_string(store) + "\" \>" |
---|
90 | |
---|
91 | # Remove text regions that should not be exposed |
---|
92 | for key,value in altered_regions.items(): |
---|
93 | text = text.replace(key,value) |
---|
94 | # Capture only the forms |
---|
95 | newtext = beginning |
---|
96 | for form in re.finditer("(?s)(<FORM.*?)(</FORM>)",text): |
---|
97 | newtext = newtext + form.group(1) + store_text + form.group(2) |
---|
98 | if 'hgta_doLookupPosition' in params: |
---|
99 | lookup = re.search("(?s).*?(<H2>.*</PRE>)",text) |
---|
100 | if lookup: |
---|
101 | newtext = newtext + lookup.group(1) |
---|
102 | # if these keys are in the params, then pass the content through |
---|
103 | passthruContent = ['hgta_doSummaryStats', 'hgta_doSchema', 'hgta_doSchemaDb'] |
---|
104 | for k in passthruContent: |
---|
105 | if k in params: |
---|
106 | content = re.search("(?s)CONTENT TABLES.*?-->(.*/TABLE>)",text) |
---|
107 | if content: |
---|
108 | newtext = newtext + "<TABLE>" + content.group(1) |
---|
109 | |
---|
110 | newtext = newtext + ending |
---|
111 | return newtext |
---|
112 | except KeyError, exc: |
---|
113 | log.error(str(exc)) |
---|
114 | trans.log_event( "Proxy Error -> %s" % str(exc) ) |
---|
115 | msg = 'There has been a problem connecting to <i>%s</i> <p> <b>%s<b>' % (base_url, exc) |
---|
116 | return msg |
---|
117 | |
---|
118 | # HTML for generating the proxy page. |
---|
119 | beginning = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
---|
120 | <html> |
---|
121 | |
---|
122 | <head> |
---|
123 | <title>Galaxy</title> |
---|
124 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
125 | <link href="/static/style/base.css" rel="stylesheet" type="text/css" /> |
---|
126 | <script language="javascript" type="text/javascript"> |
---|
127 | function changeTarget(target) |
---|
128 | { |
---|
129 | document.forms['mainForm'].target = target; |
---|
130 | } |
---|
131 | </script> |
---|
132 | </head> |
---|
133 | |
---|
134 | <body> |
---|
135 | <div class="toolForm" id="ucsc_proxy"> |
---|
136 | <div class="toolFormTitle">UCSC Table Browser</div> |
---|
137 | |
---|
138 | <div class="toolFormBody"> |
---|
139 | ''' |
---|
140 | ending = ''' |
---|
141 | <P>This is a proxy to the data services provided by the <a href=\"http://genome.ucsc.edu\" target=\"_blank\">UCSC Genome Browser</a>'s <a href=\"http://genome.ucsc.edu/cgi-bin/hgTables\" target=\"_blank\">Table Browser.</a></P> |
---|
142 | </div> |
---|
143 | </div> |
---|
144 | </body> |
---|
145 | </html>''' |
---|
146 | |
---|
147 | # This is a mess of mappings of text to make the proxy friendlier to |
---|
148 | # galaxy users. |
---|
149 | altered_regions = { |
---|
150 | '"../cgi-bin/hgTables' : '"/ucsc_proxy/index', |
---|
151 | '<TR><TD>\n<B>output file:</B> <INPUT TYPE=TEXT NAME="hgta_outFileName" SIZE=29 VALUE=""> (leave blank to keep output in browser)</TD></TR>\n<TR><TD>\n<B>file type returned: </B><INPUT TYPE=RADIO NAME="hgta_compressType" VALUE="none" CHECKED> plain text  <INPUT TYPE=RADIO NAME="hgta_compressType" VALUE="gzip" > gzip compressed</TD></TR>' : '<INPUT TYPE=HIDDEN NAME="hgta_compressType" VALUE="none" /><INPUT TYPE=HIDDEN NAME="hgta_outFileName" VALUE="" />', |
---|
152 | ' <P>To reset <B>all</B> user cart settings (including custom tracks), \n<A HREF="/cgi-bin/cartReset?destination=/cgi-bin/hgTables">click here</A>.' : '', |
---|
153 | 'ACTION="../cgi-bin/hgTables"' : 'ACTION="/ucsc_proxy/index"', |
---|
154 | '<A HREF="/goldenPath/help/customTrack.html" TARGET=_blank>custom track</A>' : '<A HREF="http://genome.ucsc.edu/goldenPath/help/customTrack.html" TARGET=_blank>custom track</A>', |
---|
155 | '<INPUT TYPE=RADIO NAME="hgta_regionType" VALUE="genome" onClick="regionType=\'genome\';" CHECKED>' : '<INPUT TYPE=RADIO NAME="hgta_regionType" VALUE="genome" onClick="regionType=\'genome\';">', |
---|
156 | '<INPUT TYPE=RADIO NAME="hgta_regionType" VALUE="range" onClick="regionType=\'range\';">' : '<INPUT TYPE=RADIO NAME="hgta_regionType" VALUE="range" onClick="regionType=\'range\';" CHECKED>', |
---|
157 | "<OPTION VALUE=bed>" : "<OPTION VALUE=bed SELECTED>" , |
---|
158 | '<INPUT TYPE=SUBMIT NAME="hgta_doSchema" VALUE="describe table schema">' : '<INPUT TYPE=SUBMIT NAME="hgta_doSchema" VALUE="describe table schema" onClick="changeTarget(\'_blank\')" onMouseOut="changeTarget(\'_self\')">' |
---|
159 | } |
---|