1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# $Id: MolImage.py 2 2006-05-06 22:54:39Z glandrum $
#
# Copyright (C) 2004, 2005 Rational Discovery LLC
# All Rights Reserved
#
import Chem
from Chem.Draw.MolDrawing import MolDrawing
from Chem import TemplateAlign
from sping.SVG.pidSVG import SVGCanvas as Canvas
from mod_python import apache
from utils import cactvs
import sys,os,tempfile
def gif(req,smiles,width=100,height=100,highlight='[]',frame=0,
dblSize=0,**kwargs):
req.content_type='image/gif'
width=int(width)
height=int(height)
frame=int(frame)
dblSize = int(dblSize)
# FIX: unsafe:
highlight = eval(highlight)
imgD = ''
if smiles:
fName = tempfile.mktemp('.gif')
cactvs.SmilesToGif(smiles,fName,(width,height),dblSize=dblSize,frame=frame)
if os.path.exists(fName):
imgD = open(fName,'rb').read()
try:
os.unlink(fName)
except OSError:
pass
return imgD
def svg(req,smiles,width=100,height=100,highlight='[]',frame=0,
template='',numbers=0,
**kwargs):
req.content_type='image/svg+xml'
width=int(width)
height=int(height)
frame=int(frame)
# FIX: unsafe:
highlight = eval(highlight)
imgD = ''
mol = None
if smiles:
mol = Chem.MolFromSmiles(smiles)
if mol:
if kwargs.get('kekulize',True):
Chem.Kekulize(mol)
if template and highlight:
try:
patt = Chem.MolFromSmiles(template)
Chem.Compute2DCoords(patt)
TemplateAlign.AlignMolToTemplate2D(mol,patt,highlight)
except:
Chem.Compute2DCoords(mol)
else:
Chem.Compute2DCoords(mol)
canvas = Canvas(size=(width,height))
drawer = MolDrawing(canvas=canvas)
if numbers and numbers!='0':
drawer.includeAtomNumbers=True
drawer.atomNumberOffset=1
drawer.noCarbonSymbols = 1
drawer.AddMol(mol,highlightAtoms=highlight)
svg = canvas._txt+'</svg>'
else:
svg = ''
return svg
|