Discussion:
[Rdkit-discuss] Reverse scale of svg coordinates to atom coordinates
Greg Landrum
2017-06-21 15:18:39 UTC
Permalink
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-***@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-***@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
>
Hongbin Yang
2017-06-21 15:23:45 UTC
Permalink
Hi, Esben Jannik Bjerrum,
    I have studied how to do it, though it may not be the best way. In `rdMolDraw` there are `getAtomCoords` and `getDrawCoords` in C++ APIs    http://www.rdkit.org/docs/cppapi/classRDKit_1_1MolDraw2D.html#abd327050dfa838543103d1f2c5f28f23     But these APIs are not wrapped for python.  So you may have to add the wrapper into the source and compile it manually.
Here are some codes I made in older version:### in `rdkit/Code/GraphMol/MolDraw2D/Wrap` ###namespace RDKit {....Point2D getDrawCoordsViaId(MolDraw2D &self, int at_num) {

return self.getDrawCoords(at_num);

}

Point2D getAtomCoordsViaId(MolDraw2D &self, int at_num) {

return self.getAtomCoords(at_num);

} 

}...BOOST_PYTHON_MODULE(rdMolDraw2D) {...python::class_<RDKit::MolDraw2D,boost::noncopyable>("MolDraw2D",docString.c_str(),python::no_init)    .def ...)    .def("getDrawCoords", RDKit::getDrawCoordsViaId, "a")     .def("getAtomCoords", RDKit::getAtomCoordsViaId,"b")

    ;...

(I don't know why I added "a" and "b" and they may not necessary. I am not good at C++ and boost-python)        BTW, I wonder is it possible to open these APIs officially ?

Hongbin Yang 

 From: Esben Jannik Bjerrum via Rdkit-discussDate: 2017-06-21 22:38To: rdkit-***@lists.sourceforge.netSubject: [Rdkit-discuss] Reverse scale of svg coordinates to atom coordinates
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.0svg_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
http://dk.linkedin.com/in/esbenbjerrum
http://www.wildcardconsulting.dk
Loading...