## ## Rajarshi Guha ## June 2009 ## ## Released under LGPL ## import sys, string, os, cgi, StringIO, tempfile, time, shutil, urllib from mod_python import apache baseurl = 'http://rest.rguha.net/' homedir = '/home/rguha/' tmpdir = 'tmp' max_smiles = 200 pwd = os.path.dirname(__file__) def index2(req): fname = '%s/tmp/xxx' % (pwd) open(fname, 'w').write("Hello\n") return def index(req): s = """ 3D Coordinate Generation
This page allows you to supply SMILES strings or a URL to a SMILES file and get back the 3D coordinates of the moleculesin SD format. This page is based on a web service so you can access this functionality in other envrionements as well. It makes use of the smi23d program. The 3D structures are not necessarily the lowest energy conformations, but are reasonable starting points for further study.

Though the service is reasonably fast it can get slow if very large molecules (> 100 heavy atoms) or large numbers of SMILES are supplied. Right now there is a limit of 200 molecules that can be processed. As we move the service to a better machine, we'll up the limit

NOTE: For some reason this page doesn't work with IE. So Firefox or Safari are the preferred browsers.

%s
""" % (getForm(baseurl+'threed/d3.py/get3d')) req.content_type = 'text/html' return s def getForm(actionPath): s = """
SMILES:
Is URL? :
""" % (actionPath) return s def get3d(req): isurl = req.form.getfirst("isurl", "") if isurl == 'yes': isurl = True else: isurl = False smistr = None ## get the SMILES if not isurl: rows = req.form.getfirst('smiles', '').strip() rows = [x.strip() for x in rows.split('\n')] rows = filter(lambda x: len(x) != 0, rows) smistr = '\n'.join(rows) else: rows = req.form.getfirst('smiles', '').strip() rows = [x.strip() for x in rows.split('\n')] smistr = rows[0] if len(smistr) == 1: req.content_type = 'text/plain' req.write("ERROR: Single atom SMILES are not handled") return apache.HTTP_BAD_REQUEST nsmi = 0 ## we should get the URL if it is one and see how many ## smiles we're trying to do if isurl: urllib.urlcleanup() fname, headers = urllib.urlretrieve(smistr) f = open(fname, 'r') s = '' nsmi = len(f.readlines()) f.close() os.unlink(fname) if nsmi > max_smiles or len(rows) > max_smiles: req.content_type = 'text/html' return """ 3D Coordinate Generation At this point we only allow a maximum of %d SMILES. We plan to increase this limit later on

Go back """ % (max_smiles, baseurl) # if len(smistr) == 0: # req.content_type = 'text/html' # return """ # 3D Coordinate Generation # It seems that you didn't provide any SMILES or a URL to a SMILES file

# Go back # """ import time, random, md5 def uuid(*args): t = long( time.time() * 1000 ) r = long( random.random()*100000000000000000L ) try: #a = socket.gethostbyname( socket.gethostname() ) a = random.random()*100000000000000000L except: a = random.random()*100000000000000000L data = str(t)+' '+str(r)+' '+str(a)+' '+str(args) data = md5.md5(data).hexdigest() return data reqid = uuid() sminame = '%s/tmp/%s.smi' % (pwd, reqid) roughname = '%s/tmp/%sr.sdf' % (pwd, reqid) optname = '%s/tmp/%so.sdf' % (pwd, reqid) f = open(sminame, 'w') f.write(smistr) f.close() cmd_smi2sdf = '%s/src/smi23d/build/smi2sdf -e %s/tmp/errorr.log -o %s -p %s/src/smi23d/build/mmxconst.prm %s' \ % (homedir, pwd, roughname, homedir, sminame) cmd_mengine = '%s/src/smi23d/build/mengine -e %s/tmp/erroro.log -o %s -c %s/src/smi23d/build/mmxconst.prm -p %s/src/smi23d/build/mmff94.prm %s' \ % (homedir, pwd, optname, homedir, homedir, roughname) #req.write(cmd_smi2sdf) #return os.system(cmd_smi2sdf) os.system(cmd_mengine) req.content_type = 'text/plain' req.headers_out['Content-disposition'] = 'attachment; filename="%s"' % (os.path.basename(optname)) for line in open("%s" % (optname), 'r'): req.write(line)