djay0529 / mdanalysis

Automatically exported from code.google.com/p/mdanalysis
0 stars 0 forks source link

MOL2 writer fails if not loaded from MOL2 #227

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Using MDAnalysis 0.9.0, I cannot write a MOL2 file if I loaded from anything 
that isn't a MOL2 file.

What steps will reproduce the problem?
 import MDAnalysis; from MDAnalysis.tests.datafiles import merge_ligand as LIGAND_PDB
 u = MDAnalysis.Universe(LIGAND_PDB)
 u.atoms.write("fromPDB.mol2")

What is the expected output? What do you see instead?

This should write a mol2 file of the ligand. Instead I get an AttributeError 
because the mol2 reader is trying to access "substructure", which is not 
available in all readers (in fact, it is only a part of the mol2 reader):

  AttributeError: 'PrimitivePDBReader' object has no attribute 'substructure'

(see full trace below).

Thus, I consider the MOL2 writer broken because it can only be used in a 
limited set of circumstances at the moment. 

1) It should be rewritten to cope with ALL input topologies. Given our improved 
topology analysis capabilities, this should be possible.

2) Appropriate test cases need to be added.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-1a7bca870f90> in <module>()
----> 1 u2.atoms.write("fromPDB.mol2")

/Volumes/Data/oliver/Biop/Library/python/mdanalysis/package/MDAnalysis/core/Atom
Group.py in write(self, filename, format, filenamefmt, **kwargs)
   2329             raise ValueError("No writer found for format: {}".format(filename))
   2330         else:
-> 2331             writer.write(self.atoms)
   2332             if coords:  # only these writers have a close method
   2333                 writer.close()

/Volumes/Data/oliver/Biop/Library/python/mdanalysis/package/MDAnalysis/coordinat
es/MOL2.py in write(self, obj)
    344         for framenumber in xrange(start, len(traj), step):
    345             traj[framenumber]
--> 346             self.write_next_timestep(obj)
    347 
    348         self.close()

/Volumes/Data/oliver/Biop/Library/python/mdanalysis/package/MDAnalysis/coordinat
es/MOL2.py in write_next_timestep(self, obj)
    357         or a :class:`~MDAnalysis.core.AtomGroup.Universe`.
    358         """
--> 359         block = self.encode_block(obj)
    360         self.file.writelines(block)

/Volumes/Data/oliver/Biop/Library/python/mdanalysis/package/MDAnalysis/coordinat
es/MOL2.py in encode_block(self, obj)
    305         bond_lines = "\n".join(bond_lines)
    306 
--> 307         substructure = traj.substructure[traj.frame]
    308         substructure = ["@<TRIPOS>SUBSTRUCTURE\n"] + substructure
    309 

AttributeError: 'PrimitivePDBReader' object has no attribute 'substructure'

Original issue reported on code.google.com by orbeckst on 19 Mar 2015 at 6:03

GoogleCodeExporter commented 9 years ago
This is a won't fix: in absence of mol2 atom types it is hard to infer those 
using just the PDB atom names/atom types. 

It is possible to generate mol2 atom types, see rdkit (open source, MIT 
license) for converting between mol2 and pdb files,

http://www.rdkit.org/docs/GettingStartedInPython.html

and specifically 

http://www.rdkit.org/Python_Docs/rdkit.Chem.rdmolfiles-module.html#MolToPDBFile

Original comment by jan...@gmail.com on 22 Mar 2015 at 2:11

GoogleCodeExporter commented 9 years ago
Should probably raise a NotImplementedError instead then?

try:
    substructure = traj.substructure[traj.frame]
except AttributeError:
    raise NotImplementedError("No MOL2 atom types found in traj")

And later a mol2 atom generator could be patched in the except branch.

Original comment by richardjgowers on 22 Mar 2015 at 7:30

GoogleCodeExporter commented 9 years ago
That seems a good idea. If we then properly document the behavior it should be 
fine. 

@Jan: can you please do this?

If you have a simple example how to use other tools (as you said in the last 
comment) the it would be great to have this in the doc. 

Am Mar 22, 2015 um 12:30 schrieb mdanalysis@googlecode.com:

Original comment by orbeckst on 22 Mar 2015 at 8:36