Permalink
Browse files

Updated substruct service to accept GET or POST. If POST is specified…

… then it accepts two form elements called query (a SMARTS pattern string) and target (a comma separate string of one or more SMILES). Return is a plain/text doc with N rows for N input SMILES, each row being true, false or fail if the pattern matched the SMILES, didn't match or failed to parse respectively
  • Loading branch information...
rajarshi committed Feb 3, 2009
1 parent e4d7c6b commit fbbf5743c60051cd156a4a23295405acd9b9a751
Showing with 57 additions and 8 deletions.
  1. +11 −0 substruct/posttest.py
  2. +46 −8 substruct/substruct.py
View
@@ -0,0 +1,11 @@
+import urllib2
+import urllib
+
+url = 'http://rguha.ath.cx/~rguha/cicc/rest/substruct/'
+values = {'target' : 'CCC(=O)CC,c1ccccc1,C(=O)CC(=O)COC', 'query' : 'c' }
+data = urllib.urlencode(values)
+req = urllib2.Request(url, data)
+response = urllib2.urlopen(req)
+the_page = response.read()
+
+print the_page
View
@@ -1,27 +1,65 @@
from mod_python import apache
+from mod_python import util
from pybel import *
-def handler(req):
- req.content_type = 'text/plain'
- uriParts = req.uri.split('/')
-
+def handle_get(uriParts):
target = uriParts[-2]
query = uriParts[-1]
try:
mol = readstring("smi", target)
except IOError:
- return apache.HTTP_BAD_REQUEST
+ raise RuntimeError
try:
pat = Smarts(query)
except IOError:
- return apache.HTTP_BAD_REQUEST
+ raise RuntimeError
if pat.findall(mol):
- req.write('true')
+ return "true"
else:
- req.write('false')
+ return "false"
+def handle_post(req):
+ fd = util.FieldStorage(req)
+ query = fd['query']
+ targets = fd['target']
+ if targets == "": raise RuntimeError
+ targets = [x.strip() for x in targets.split(",")]
+
+ try:
+ pat = Smarts(query)
+ except IOError:
+ raise RuntimeError
+
+ result = []
+ for target in targets:
+ try:
+ mol = readstring("smi", target)
+ if pat.findall(mol): result.append("true")
+ else: result.append("false")
+ except IOError:
+ result.append("fail")
+
+ return '\n'.join(result)
+
+def handler(req):
+ req.content_type = 'text/plain'
+ uriParts = req.uri.split('/')
+
+ if req.method == 'GET':
+ try:
+ value = handle_get(uriParts)
+ req.write(value)
+ except RuntimeError:
+ return apache.HTTP_BAD_REQUEST
+ elif req.method == 'POST':
+ try:
+ values = handle_post(req)
+ req.write(values)
+ except RuntimeError: return apache.HTTPD_BAD_REQUEST
+
return apache.OK
+

0 comments on commit fbbf574

Please sign in to comment.