Draw hydrogen bonds.#1961
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1961 +/- ##
==========================================
+ Coverage 46.59% 47.26% +0.67%
==========================================
Files 88 88
Lines 23118 23141 +23
Branches 6005 6013 +8
==========================================
+ Hits 10771 10937 +166
+ Misses 11197 11056 -141
+ Partials 1150 1148 -2
Continue to review full report at Codecov.
|
|
All checks pass! 💯 |
alongd
left a comment
There was a problem hiding this comment.
Looking good! I added some questions, please take a look
| if cycle is None or len(cycle0) > len(cycle): cycle = cycle0 | ||
| cycle0 = cycle1 | ||
| if cycle is None: | ||
| break |
There was a problem hiding this comment.
could you explain this addition?
There was a problem hiding this comment.
Without it trying to draw
1 O u0 p2 c0 {5,S} {8,S}
2 O u0 p2 c0 {6,S} {7,S}
3 O u0 p3 c-1 {6,S} {8,H}
4 O u0 p2 c0 {6,D} {9,H}
5 N u0 p1 c0 {1,S} {7,H} {9,S} {10,S}
6 N u0 p0 c+1 {2,S} {3,S} {4,D}
7 H u0 p0 c0 {2,S} {5,H}
8 H u0 p0 c0 {1,S} {3,H}
9 H u0 p0 c0 {4,H} {5,S}
10 H u0 p0 c0 {5,S}
leads to
Traceback (most recent call last):
File "/Users/rwest/Code/RMG-Py/rmgpy/molecule/draw.py", line 209, in draw
self._generate_coordinates()
File "/Users/rwest/Code/RMG-Py/rmgpy/molecule/draw.py", line 366, in _generate_coordinates
self._generate_ring_system_coordinates(backbone)
File "/Users/rwest/Code/RMG-Py/rmgpy/molecule/draw.py", line 564, in _generate_ring_system_coordinates
atoms.remove(cycle)
ValueError: list.remove(x): x not in list
If you replace the break with a continue then you enter and endless loop (due to the while)
With the break we get the image

...which is not great - the H is being drawn exactly on top of an O which hides it - but at least it doesn't crash.
| (`x2`,`y2`), and update the bounding rectangle if necessary. | ||
|
|
||
| For a dashed line, `dashed` be a list of on/off segment lengths, | ||
| or `True` in which case [3.5,3.5] is used. |
There was a problem hiding this comment.
Minor: please use double "`" marks for True, a single mark on each side represents a link in rst (double, a code)
| if dashed: | ||
| cr.set_dash([3.5]) | ||
| if dashed is True: | ||
| dashed = [3.5] |
There was a problem hiding this comment.
The docsting says [3.5, 3.5], here it's [3.5], which is correct?
Could you also provide a general explanation for why dash is sometimes a bool and sometimes a list? I understand it wasn't introduced by this PR.
There was a problem hiding this comment.
Other places in the code were already calling this method with dashed=True and get a default dash size of 3.5. I wanted a way to specify the dash size. I guess it could be done with an extra optional argument dash_size instead of overloading this one if you think that's better.
([3.5] is equivalent to [3.5, 3.5] when specifying dash sizes in cairo, but to avoid people needing to know that I've changed it to [3.5, 3.5] in a fixup commit)
There was a problem hiding this comment.
If this arg is currently being used only as a bool, let's make a new one, unless you think it's very redundant
| """ | ||
| ) | ||
| try: | ||
| from cairocffi import PDFSurface |
There was a problem hiding this comment.
Are cairocffi and cairo OS-dependent? If not, can we assume one of them should be present?
There was a problem hiding this comment.
I don't know - I just copied boilerplate from all the other drawing tests. Changing them all is out of scope of this PR.
| os.unlink(path) | ||
| polycycle = Molecule(smiles="C123CC4CC1COCC2CCC34") | ||
| surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, file_format='pdf', target=path) | ||
| surface, cr, (xoff, yoff, width, height) = self.drawer.draw(polycycle, file_format='pdf', target=path) |
|
I addressed your suggestions with fixup commits (so you can review them independently) but they should be squashed on rebasing to merge. |
|
@rwest, please do what you think is right re ading |
1. Don't remove an H if it has an H bond. 2. Draw H-bonds as dotted lines. 3. Fix a problem that occurred with a complicated system of fused rings, if you have two rings that share more than two atoms, then we can't draw it well. Never have been able to (that's why we punted this task to RDKit whenever possible). But after this fix, at least we don't crash.
Just checks we don't crash - doesn't check they look good. But should increase the code coverage at least.
A copy/paste slip meant it wasn't actually drawing the polycyclic molecule. (Note that this test just ensures it doesn't crash, it doesn't see how it looks, else someone might have noticed it was drowing the wrong thing)
The codacy code quality tests were failing because pylint detected unused variables. Prefixing them with an underscore (or with 'dummy_') avoids this test.
Sometimes when drawing fused ring systems where the rings share more than 2 atoms, the algorithm can't cope and doesn't place some ring systems. Those "unplaced" atoms all end up piled on top of each other at the origin. I don't know how to fix that fused ring system problem (usually we get RDKit to do it, which works fine) so this change detects atoms that are on top of each other and pushes them apart a bit. They may not be pushed in the best direction, but at least they won't get drawn on top of each other, so you can see the atoms in the drawing.
|
OK, all fixups rebased, tests passing again. Should I merge, or wait for someone else to? |
This was added in PR #1961 which indroduced the ability to draw hydrogen bonds. It was removed without explanation in ec95153 which made drawing work for fragments. That broke drawing of hydrogen bonds. The unit test for drawing hydrogen bonds exists, but just checks that a PDF is made, not what it looks like. So this wasn't detected.
Pull request #1961 introduced the ability to draw hydrogen bonds. To do so, Hydrogen atoms that have H bonds shouldn't be removed from the drawings. This check was removed without explanation in ec95153 which was designed to make drawing work for fragments. That broke drawing of hydrogen bonds. This pull request fixes it, and also removes some code duplication.
This was added in PR #1961 which indroduced the ability to draw hydrogen bonds. It was removed without explanation in ec95153 which made drawing work for fragments. That broke drawing of hydrogen bonds. The unit test for drawing hydrogen bonds exists, but just checks that a PDF is made, not what it looks like. So this wasn't detected.

Motivation or Problem
Hydrogen atoms were not drawn explicitly, and (therefore) hydrogen bonds were not drawn. Structures held together (or apart) with hydrogen bonds would just collapse on top of themselves. See before/after example below.
Description of Changes
Testing
Before:

After:
