Hi Don,
On Sat, Jun 11, 2011 at 12:32 AM, Donald Keidel <[email protected]> wrote:
> Hi,
>
> Today I checked out the lastest version from the svn. I took only the
> trunk and built this and ran a python script I used with a previous
> version to depict a molecule. However, I received the following error:
>
> AttributeError: type object 'MolDrawing' has no attribute 'registerCanvas'
>
the registerCanvas() function was removed in the Q1 2011 release. I
forgot to mention this specifically in the release notes (they only
mention that the drawing code has been refactored).
>
> I did the following in the python script much like the people at cinfony
> do to depict with aggdraw:
>
<snip>
There were a number of changes to the drawing interface that make what
you want to do somewhat easier. The biggest one is that
Draw.MolToImage will automatically take care of finding a working
canvas and will return a PIL image that you can use in the rest of
what you're doing. I'm attaching a version of your code that works for
at least simple stuff, you should definitely go through it and make
sure everything is really ok. One expansion in capability you may or
may not want: this no longer requires aggdraw. It tries cairo, then
aggdraw, then sping (built in). If you want to force use of aggdraw,
add at the top of the file:
import os
os.environ['RDKIT_CANVAS']='agg'
One big caveat: drawing on a non-white background doesn't work. It
didn't work before either, so this is nothing new. If there's a real
demand for this feature, it can be added to a future RDKit release.
-greg
import os, sys, copy
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem.Draw.MolDrawing import MolDrawing
# PIL and Tkinter
try:
import Tkinter as tk
import ImageTk as PILtk
except:
PILtk = None
tk=None
def draw(Mol, show=True, filename=None, update=False, usecoords=False,
width=300, height=300, background='white', atoms='color'):
"""Create a 2D depiction of the molecule. This method was taken from cinfony.
Optional parameters:
show -- display on screen (default is True)
filename -- write to file (default is None)
update -- update the coordinates of the atoms to those
determined by the structure diagram generator
(default is False)
usecoords -- don't calculate 2D coordinates, just use
the current coordinates (default is False)
Aggdraw is used for 2D depiction. Tkinter and
Python Imaging Library are required for image display.
Python code found at: /usr/local/RDKit_2010_12_1/rdkit
"""
if usecoords:
confId = -1
else:
if update:
AllChem.Compute2DCoords(Mol)
confId = -1
else:
confId = Mol.GetNumConformers()
AllChem.Compute2DCoords(Mol, clearConfs = False)
if show or filename:
Chem.Kekulize(Mol)
if atoms == 'color' and background == 'white' :
print 'Color on white'
MolDrawing.elemDict[6] = (0,0,0)
elif atoms == 'color' and background == 'black':
print 'Color on black'
MolDrawing.elemDict[6] = (1,1,1)
elif atoms == 'white' and background == 'black':
print 'White on black'
MolDrawing.elemDict = {
16: (0,0,0),
17: (0,0,0),
35: (0,0,0),
6: (1,1,1),
0: (0,0,0),
7: (0,0,0),
8: (0,0,0),
9: (0,0,0),
15: (0,0,0)
}
elif atoms == 'black' and background == 'white':
print 'Black on white'
MolDrawing.elemDict = {
16: (0,0,0),
17: (0,0,0),
35: (0,0,0),
6: (0,0,0),
0: (0,0,0),
7: (0,0,0),
8: (0,0,0),
9: (0,0,0),
15: (0,0,0)
}
MolDrawing.bondLineWidth = 2.5
img = Draw.MolToImage(Mol,size=(width,height),
confId=confId,kekulize=False,
wedgeBonds=True)
if filename: # Note: overwrite is allowed
img.save(filename)
if show:
if not tk:
errormessage = ("Tkinter or Python Imaging "
"Library not found, but is required for image "
"display. See installation instructions for "
"more information.")
raise ImportError, errormessage
root = tk.Tk()
root.title((hasattr(Mol, "title") and Mol.title)
or Mol.__str__().rstrip())
frame = tk.Frame(root, colormap="new",visual='truecolor').pack()
imagedata = PILtk.PhotoImage(img)
label = tk.Label(frame, image=imagedata).pack()
quitbutton = tk.Button(root, text="Close",command=root.destroy).pack(fill=tk.X)
root.mainloop()
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss