Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New molecule drawer doesn't work properly. #98

Closed
faribas opened this issue Aug 20, 2012 · 4 comments
Closed

New molecule drawer doesn't work properly. #98

faribas opened this issue Aug 20, 2012 · 4 comments

Comments

@faribas
Copy link
Contributor

faribas commented Aug 20, 2012

My RMG-Py job crashed with this error:

File "/home/fariba/Code/RMG-Py/rmg.py", line 136, in <module>
    cProfile.runctx(command, global_vars, local_vars, stats_file)
  File "/home/fariba/lib/python2.7/cProfile.py", line 49, in runctx
    prof = prof.runctx(statement, globals, locals)
  File "/home/fariba/lib/python2.7/cProfile.py", line 140, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 327, in execute
    self.saveEverything()
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 471, in saveEverything
    self.saveOutputHTML()
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 613, in saveOutputHTML
    saveOutputHTML(os.path.join(self.outputDirectory, 'output.html'), self.reactionModel)
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/output.py", line 95, in saveOutputHTML
    MoleculeDrawer().draw(spec.molecule[0], 'png', fstr)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 207, in draw
    self.__generateCoordinates()
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 373, in __generateCoordinates
    self.__generateNeighborCoordinates(backbone)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 697, in __generateNeighborCoordinates
    self.__generateFunctionalGroupCoordinates(atom0, atom1)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 819, in __generateFunctionalGroupCoordinates
    self.__generateFunctionalGroupCoordinates(atom1, atom)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 819, in __generateFunctionalGroupCoordinates
    self.__generateFunctionalGroupCoordinates(atom1, atom)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 760, in __generateFunctionalGroupCoordinates
    center += coordinates_cycle[atoms.index(atom),:]
TypeError: 'NoneType' object has no attribute '__getitem__'

Apparently, the new molecule drawer can't draw levoglucosan " C12C(C(C(C(O2)OC1)O)O)O ", drawing this molecule will fail with the same error message:

xception Value: 'NoneType' object has no attribute '__getitem__'
Traceback:
File "/export/thishost/comocheng/virtualenvs/rmgweb/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/export/thishost/comocheng/RMG-website/rmgweb/main/views.py" in drawMolecule
  208.     surface, cr, rect = MoleculeDrawer().draw(molecule, format='png')
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in draw
  207.             self.__generateCoordinates()
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in __generateCoordinates
  373.         self.__generateNeighborCoordinates(backbone)
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in __generateNeighborCoordinates
  697.                         self.__generateFunctionalGroupCoordinates(atom0, atom1)
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in __generateFunctionalGroupCoordinates
  819.                     self.__generateFunctionalGroupCoordinates(atom1, atom)
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in __generateFunctionalGroupCoordinates
  819.                     self.__generateFunctionalGroupCoordinates(atom1, atom)
File "/export/thishost/comocheng/RMG-website/../RMG-Py/rmgpy/molecule/draw.py" in __generateFunctionalGroupCoordinates
  760.                 center += coordinates_cycle[atoms.index(atom),:]
@faribas
Copy link
Contributor Author

faribas commented Aug 21, 2012

Thanks Josh for your help but my job crashed again with this error message:

File "/home/fariba/Code/RMG-Py/rmg.py", line 136, in <module>
    cProfile.runctx(command, global_vars, local_vars, stats_file)
  File "/home/fariba/lib/python2.7/cProfile.py", line 49, in runctx
    prof = prof.runctx(statement, globals, locals)
  File "/home/fariba/lib/python2.7/cProfile.py", line 140, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 393, in execute
    self.saveEverything()
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 471, in saveEverything
    self.saveOutputHTML()
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/main.py", line 613, in saveOutputHTML
    saveOutputHTML(os.path.join(self.outputDirectory, 'output.html'), self.reactionModel)
  File "/home/fariba/Code/RMG-Py/rmgpy/rmg/output.py", line 95, in saveOutputHTML
    MoleculeDrawer().draw(spec.molecule[0], 'png', fstr)
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 203, in draw
    self.__findRingGroups()
  File "/home/fariba/Code/RMG-Py/rmgpy/molecule/draw.py", line 302, in __findRingGroups
    self.ringSystem.append(cycle)
AttributeError: MoleculeDrawer instance has no attribute 'ringSystem'

@rwest
Copy link
Member

rwest commented Aug 22, 2012

I think this is now fixed (by one or two of sean's commits)

@stroiano
Copy link

Yes, my commit (which I reverted last night to try to match Josh's since he has a way better understanding of the situation; copy here: 8a44adc) does prevent this exception from being raised, but I don't believe it's a proper fix of the underlying issue.

From __findRingGroups:

# Find all of the cycles in the molecule
self.cycles = self.molecule.getSmallestSetOfSmallestRings()
self.ringSystems = []

# If the molecule contains cycles, find them and group them
if len(self.cycles) > 0:
    # Split the list of cycles into groups
    # Each atom in the molecule should belong to exactly zero or one such groups
    for cycle in self.cycles:
        found = False
        for ringSystem in self.ringSystems:
            for ring in ringSystem:
                if any([atom in ring for atom in cycle]) and not found:
                    self.ringSystem.append(cycle)
                    found = True
        if not found:
            self.ringSystems.append([cycle])

Changing the line self.ringSystem.append(cycle) to self.ringSystems.append([cycle]) does indeed prevent this error (since there is no self.ringSystem), but logically it just doesn't seem right; the result is that every element in self.ringSystems ends up being a list of just one cycle.

It seems to me that the most sensible line here (which I believe is what Josh meant to write in the first place) is just ringSystem.append(cycle) (added in aab0f42). However, this then leads to:

ERROR:root:Error while drawing molecule C1CCC2=C(C1)CCCC2: <Atom 'C'> is not in list
Traceback (most recent call last):
  File "rmgpy/molecule/draw.py", line 207, in draw
    self.__generateCoordinates()
  File "rmgpy/molecule/draw.py", line 334, in __generateCoordinates
    self.__generateRingSystemCoordinates(backbone)
  File "rmgpy/molecule/draw.py", line 500, in __generateRingSystemCoordinates
    center1 += coordinates[atoms.index(atom),:]
ValueError: <Atom 'C'> is not in list

Clearly there's something else wrong here, but I don't know the code well enough yet to make any more guesses.

Also of note though, the other line that I changed in my initial commit was setting __generateRingSystemCoordinates to return coordinates. This is probably not the intended behavior of the function though, as it appears to update self.coordinates appropriately.

Thoughts, @jwallen?

@stroiano
Copy link

Alright, things make a bit more sense to me now. This last error stems from atoms becoming blank at some point for any ringSystem with multiple cycles. Through changing three lines, I was able to generate a decent (though slightly distorted) result:

C1CCC2=C(C1)CCCC2

Our web server appears to be randomly flipping one of the rings for some reason, so the above image may look strange here. This is the consistent result that I get locally though:

C1CCC2=C(C1)CCCC2

I don't want to commit this just yet since I've already been making a mess lately as it is, but here's the diff:

diff --git a/rmgpy/molecule/draw.py b/rmgpy/molecule/draw.py
index 0eac468..e9a5bc5 100644
--- a/rmgpy/molecule/draw.py
+++ b/rmgpy/molecule/draw.py
@@ -497,7 +497,7 @@ class MoleculeDrawer:
                 if found:
                     center1 = numpy.zeros(2, numpy.float64)
                     for atom in cycle1:
-                        center1 += coordinates[atoms.index(atom),:]
+                        center1 += coordinates[cycle1.index(atom),:]
                     center1 /= len(cycle1)
                     center0 += center1
                     count += 1
@@ -542,15 +542,16 @@ class MoleculeDrawer:
                 radius = numpy.linalg.norm(center - coordinates[index0,:])

             startAngle = 0.0; endAngle = 0.0
             if len(commonAtoms) == 1:
                 # We will use the full 360 degrees to place the other atoms in the cycle
                 startAngle = math.atan2(-vector[1], vector[0])
                 endAngle = startAngle + 2 * math.pi
             elif len(commonAtoms) >= 2:
                 # Divide other atoms in cycle equally among unused angle
-                vector = coordinates[atoms.index(commonAtoms[-1]),:] - center
+                vector = coordinates[cycle.index(commonAtoms[-1]),:] - center
                 startAngle = math.atan2(vector[1], vector[0])
-                vector = coordinates[atoms.index(commonAtoms[0]),:] - center
+                vector = coordinates[cycle.index(commonAtoms[0]),:] - center
                 endAngle = math.atan2(vector[1], vector[0])

Looking at some of the polycyclics near the bottom of this page shows the result is still pretty bizarre, but at least they draw without error.

rwest pushed a commit that referenced this issue Aug 22, 2012
Fixes #98 (hopefully)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants