Re: [Rdkit-discuss] New Drawing code: Fixed sized molecules

2018-06-14 Thread Thomas Strunz
I now was able to adapt the GitHub c++ example to Python with some adaption 
(enhancement).


The code in the sample on GitHub generates images of different sizes depending 
on the molecule. For my use-case the image size should be fixed (else I will 
get layout /design issues) but the molecule should take up only as much space 
as needed (depending on the desired size, dots per angstrom for example) and if 
it is too large it should shrink to fit the image and also center it.


Here the code ( 2 functions from my class):


def molToImage(self, mol, image_width = 300, image_height = 150, dpa=30, 
kekulize=True):
mol = rdMolDraw2D.PrepareMolForDrawing(mol, kekulize=kekulize)
w, h, minV, maxV = self.getScaleForDrawing(mol, image_width, 
image_height, dpa)
drawer = rdMolDraw2D.MolDraw2DSVG(image_width, image_height) #fixed 
image size
drawer.SetScale(w, h, minV, maxV)
# re-center image
x_offset = int((image_width - w) / 2)
y_offset = int((image_height - h) / 2)
drawer.SetOffset(x_offset, y_offset)
#draw
drawer.DrawMolecule(mol)
drawer.FinishDrawing()
svg = drawer.GetDrawingText()
# It seems that the svg renderer used doesn't quite hit the spec.
# Here are some fixes to make it work in the notebook, although I think
# the underlying issue needs to be resolved at the generation step
return svg.replace('svg:','')




def getScaleForDrawing(self, mol, image_width, image_height, dpa):

minV = Point2D()
maxV = Point2D()
cnf = mol.GetConformer()

minV.x = maxV.x = cnf.GetAtomPosition(0).x
minV.y = maxV.y = cnf.GetAtomPosition(0).y

for i in range(mol.GetNumAtoms()):
minV.x = min(minV.x, cnf.GetAtomPosition(i).x)
minV.y = min(minV.y, cnf.GetAtomPosition(i).y)
maxV.x = max(maxV.x, cnf.GetAtomPosition(i).x)
maxV.y = max(maxV.y, cnf.GetAtomPosition(i).y)

w = int(dpa * (maxV.x - minV.x))
h = int(dpa * (maxV.y - minV.y))

#shrink to fit
if w > image_width or h > image_height:

rw = w/image_width
rh = h/image_height

ratio = max(rw,rh)

w = int(w/ratio)
h = int(h/ratio)
return (w, h, minV, maxV)


Best Regards,

Thomas



Von: Thomas Strunz 
Gesendet: Mittwoch, 13. Juni 2018 14:28
An: rdkit-discuss@lists.sourceforge.net
Betreff: [Rdkit-discuss] New Drawing code: Fixed sized molecules


When using the "new" drawing code according to


http://rdkit.blogspot.com/2015/02/new-drawing-code.html<http://rdkit.blogspot.com/2015/02/new-drawing-code.htm>


I also want to be able to control the size of the molecule (not image) so if 
for example I have to depict multiple molecules smaller ones are not drawn in 
an oversized fashion. Therefore I would want to control the font size and bond 
length, somewhat similar to shown here:


https://iwatobipen.wordpress.com/2017/11/03/draw-high-quality-molecular-image-in-rdkit-rdkit/


Also this would be for a web service hence drawer.GetDrawingText() is for sure 
preferred vs Draw.MolToFile and then reading in the temp file again.


I did find this sample showing this:


https://github.com/rdkit/rdkit/pull/1355/commits/8141baaa7a990e68632ab1b8445671fbcc3ca2f6


But it's in c++ and not python and not very understandable. So how can this be 
done in Python? Is there a convenience function i haven't found yet?

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss


[Rdkit-discuss] New Drawing code: Fixed sized molecules

2018-06-13 Thread Thomas Strunz
When using the "new" drawing code according to


http://rdkit.blogspot.com/2015/02/new-drawing-code.html


I also want to be able to control the size of the molecule (not image) so if 
for example I have to depict multiple molecules smaller ones are not drawn in 
an oversized fashion. Therefore I would want to control the font size and bond 
length, somewhat similar to shown here:


https://iwatobipen.wordpress.com/2017/11/03/draw-high-quality-molecular-image-in-rdkit-rdkit/


Also this would be for a web service hence drawer.GetDrawingText() is for sure 
preferred vs Draw.MolToFile and then reading in the temp file again.


I did find this sample showing this:


https://github.com/rdkit/rdkit/pull/1355/commits/8141baaa7a990e68632ab1b8445671fbcc3ca2f6


But it's in c++ and not python and not very understandable. So how can this be 
done in Python? Is there a convenience function i haven't found yet?

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss