File: MolImage.py

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (71 lines) | stat: -rwxr-xr-x 1,896 bytes parent folder | download
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
# $Id$
#
#  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 Exception:
        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