Hi Esben,

The call you are looking for is drawer.GetDrawCoords(atomIdx). This was,
unfortunately, missing from the last release. It will be in the next one:

In [14]: mol = Chem.MolFromSmiles('CC')
    ...: mc = rdMolDraw2D.PrepareMolForDrawing(mol)
    ...: drawer = rdMolDraw2D.MolDraw2DSVG(300,300)
    ...: drawer.DrawMolecule(mc)
    ...: drawer.FinishDrawing()
    ...:

In [15]: svg = drawer.GetDrawingText()

In [16]: print(svg)
<?xml version='1.0' encoding='iso-8859-1'?>
<svg:svg version='1.1' baseProfile='full'
              xmlns:svg='http://www.w3.org/2000/svg'
                      xmlns:rdkit='http://www.rdkit.org/xml'
                      xmlns:xlink='http://www.w3.org/1999/xlink'
                  xml:space='preserve'
width='300px' height='300px' >
<svg:rect style='opacity:1.0;fill:#FFFFFF;stroke:none' width='300'
height='300' x='0' y='0'> </svg:rect>
<svg:path d='M 13.6364,150 286.364,150'
style='fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1'
/>
</svg:svg>

In [18]: list(drawer.GetDrawCoords(0))
Out[18]: [13.636363636363628, 150.0]

In [19]: list(drawer.GetDrawCoords(1))
Out[19]: [286.3636363636364, 150.0]



Best,
-greg


On Wed, Jun 21, 2017 at 4:38 PM, Esben Jannik Bjerrum via Rdkit-discuss <
rdkit-discuss@lists.sourceforge.net> wrote:

> Hi RDkitters,
>    I'm experimenting a bit with an application with some user
> interactivity. I get the SVG coordinates from the Mol SVG drawing from the
> user interaction and need to get back to the atom coordinates with the goal
> of identifying the atom nearest the selected coordinates (or is there a
> smarter way to achieve that goal?).
>
> Is this possible from Python currently?
>
> I see that there is a MolDraw2D::getAtomCoords function in the cpp code
> for MolDraw2D, but I can't see it from the Python side, and there don't
> seem to be a way to get the scaling from the MolDraw2DSVG object.
>
> As I understand it, the coordinates from the molecule are offset and
> scaled (and flipped for y) to fit the drawing canvas of the specified size.
> To get back to the original atom coordinates I must somehow reverse the
> transformation. Here's some script snippets illustrating what I try to
> achieve.
>
> #Get som SVG depiction of a mol
> mol = Chem.MolFromSmiles('CCCC')
> mc = Chem.Mol(mol.ToBinary())
> rdDepictor.Compute2DCoords(mc)
> drawer = rdMolDraw2D.MolDraw2DSVG(300,300)
> drawer.DrawMolecule(mc)
> drawer.FinishDrawing()
> svg = drawer.GetDrawingText().replace('svg:','')
>
> #....
> #Visualization and User interaction code here gives SVG coordinates
> #....
> svg_x = 271.0
> svg_y = 237.0
>
> #Is there a function to scale back the coordinates? alternatively get the
> scaling and the offset from drawer and handle it manually
> atomcoords = drawer.getAtomCoords((svg_x, svg_y))
> #But this function doesn't exist:-(
>
> #...
> #Continue working with the rdkit mol
>
> I would welcome some hints or suggestions.
>
> Esben Jannik Bjerrum
> cand.pharm, Ph.D
>
> /Sent from my Ubuntu Touch Phone
>
> Phone +45 2823 8009 <+45%2028%2023%2080%2009>
> http://dk.linkedin.com/in/esbenbjerrum
> http://www.wildcardconsulting.dk
>
> ------------------------------------------------------------
> ------------------
> 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
>
>
------------------------------------------------------------------------------
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

Reply via email to