Skip to content

Instantly share code, notes, and snippets.

@martenson
Created January 23, 2015 15:48
Show Gist options
  • Save martenson/7ce1ddb19c199b2df61c to your computer and use it in GitHub Desktop.
Save martenson/7ce1ddb19c199b2df61c to your computer and use it in GitHub Desktop.
NistController
"""
Controller for integration with NIST data.
Place this in: <galaxy_home>/lib/galaxy/webapps/galaxy/controllers
and then it can be accessed via URL:
http://<galaxy_url>/nist_controller/get_nistdata
e.g.
http://dev1.ab.wurnet.nl:8088/nist_controller/get_nistdata
"""
import urllib2
from galaxy.web.base.controller import BaseUIController, url_for, error, web
class NistController( BaseUIController ):
"""
Provides some services for the NIST html report
"""
@web.expose
def get_nistdata( self, trans, casnr=None ):
"""
Generate a redirect to NIST webbook web app
casNr example: "C537268"
"""
nist_response = _fire_query_and_return_content("http://webbook.nist.gov/cgi/cbook.cgi?JCAMP="+ casnr + "&Index=0&Type=Mass")
peak_table = nist_response.split("##PEAK TABLE=(XY..XY)")[1].split("##END=")[0]
peak_table = peak_table.replace("\n", " ").strip()
return peak_table
def _fire_query_and_return_content(url):
'''
This method will fire the HTTP call and
return the results
'''
try:
data = urllib2.urlopen(url).read()
return data
except urllib2.HTTPError, e:
raise Exception( "HTTP error for URL: " + url + " : %s - " % e.code + e.reason)
except urllib2.URLError, e:
raise Exception( "Network error: %s" % e.reason.args[1] + ". Error accessing remote service [" + url + "] ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment