Skip to content

Commit

Permalink
Adapt TPR parser as required by MDAnalysis#363
Browse files Browse the repository at this point in the history
See MDAnalysis#572

Some other changes where also done:
* `MDAnalysis/core/__init__.py` import `selection` rather than
  `Selection` following the module renaming in commit
  78abe4e
* Add messages when some assertions fail in
  `MDAnalysisTests/topology/base.py`
  • Loading branch information
jbarnoud committed Dec 13, 2015
1 parent 39b964b commit e4a48ee
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 207 deletions.
2 changes: 1 addition & 1 deletion package/MDAnalysis/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,5 @@ class flagsDocs(object):


import groups
import Selection
import selection
import Timeseries
9 changes: 1 addition & 8 deletions package/MDAnalysis/topology/TPRParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,12 @@ def parse(self):

if th.bTop:
tpr_top = U.do_mtop(data, V, self._u)
structure = {
'atoms': tpr_top.atoms,
'bonds': tpr_top.bonds,
'angles': tpr_top.angles,
'dihedrals': tpr_top.dihe,
'impropers': tpr_top.impr
}
else:
msg = "{0}: No topology found in tpr file".format(self.filename)
logger.critical(msg)
raise IOError(msg)

return structure
return tpr_top

# THE FOLLOWING CODE IS WORKING FOR TPX VERSION 58, BUT SINCE THESE INFO IS
# NOT INTERESTED, SO IT'S NOT COVERED IN ALL VERSIONS. PARSING STOPS HERE.
Expand Down
1 change: 0 additions & 1 deletion package/MDAnalysis/topology/tpr/obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"bIr", "bTop", "bX", "bV", "bF", "bBox"])
Box = namedtuple("Box", "size rel v")
Mtop = namedtuple("Mtop", "nmoltype moltypes nmolblock")
TPRTopology = namedtuple("TPRTopology", "atoms, bonds, angles, dihe, impr")
Params = namedtuple("Params", "atnr ntypes functype reppow fudgeQQ iparams")
Atom = namedtuple("Atom", ["m", "q", "mB", "qB", "tp", "typeB", "ptype", "resind", "atomnumber"])
Atoms = namedtuple("Atoms", "atoms nr nres type typeB atomnames resnames")
Expand Down
92 changes: 77 additions & 15 deletions package/MDAnalysis/topology/tpr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
from . import obj
from . import setting as S

import numpy as np
from ..base import TopologyReader, squash_by
from ...core.topologyattrs import (
Atomids,
Atomnames,
Atomtypes,
Masses,
Charges,
Resids,
Resnames,
Segids,
Bonds,
Angles,
Dihedrals,
Impropers
)
from ...core.topology import Topology

def ndo_int(data, n):
"""mimic of gmx_fio_ndo_real in gromacs"""
return [data.unpack_int() for i in xrange(n)]
Expand Down Expand Up @@ -162,7 +180,19 @@ def do_mtop(data, fver, u):

mtop = obj.Mtop(nmoltype, moltypes, nmolblock)

ttop = obj.TPRTopology(*[[] for i in xrange(5)])
bonds = []
angles = []
dihedrals = []
impropers = []

atomids = []
segids = []
resids = []
resnames = []
atomnames = []
atomtypes = []
charges = []
masses = []

atom_start_ndx = 0
res_start_ndx = 0
Expand All @@ -175,20 +205,20 @@ def do_mtop(data, fver, u):
for j in xrange(mb.molb_nmol):
mt = mtop.moltypes[mb.molb_type] # mt: molecule type
for atomkind in mt.atomkinds:
ttop.atoms.append(Atom(atomkind.id + atom_start_ndx,
atomkind.name,
atomkind.type,
atomkind.resname,
atomkind.resid + res_start_ndx,
segid,
atomkind.mass,
atomkind.charge,
universe=u))
atomids.append(atomkind.id + atom_start_ndx)
segids.append(segid)
resids.append(atomkind.resid + res_start_ndx)
resnames.append(atomkind.resname)
atomnames.append(atomkind.name)
atomtypes.append(atomkind.type)
charges.append(atomkind.charge)
masses.append(atomkind.mass)

# remap_ method returns [blah, blah, ..] or []
ttop.bonds.extend(mt.remap_bonds(atom_start_ndx))
ttop.angles.extend(mt.remap_angles(atom_start_ndx))
ttop.dihe.extend(mt.remap_dihe(atom_start_ndx))
ttop.impr.extend(mt.remap_impr(atom_start_ndx))
bonds.extend(mt.remap_bonds(atom_start_ndx))
angles.extend(mt.remap_angles(atom_start_ndx))
dihedrals.extend(mt.remap_dihe(atom_start_ndx))
impropers.extend(mt.remap_impr(atom_start_ndx))

atom_start_ndx += mt.number_of_atoms()
res_start_ndx += mt.number_of_residues()
Expand All @@ -201,7 +231,39 @@ def do_mtop(data, fver, u):
# mtop_ffparams_cmap_grid_grid_spacing = 0.1
# mtop_ffparams_cmap_grid_cmapdata = 'NULL'
# do_groups(data, symtab)
return ttop

atomids = Atomids(np.array(atomids, dtype=np.int32))
atomnames = Atomnames(np.array(atomnames, dtype=object))
atomtypes = Atomtypes(np.array(atomtypes, dtype=object))
charges = Charges(np.array(charges, dtype=np.float32))
masses = Masses(np.array(masses, dtype=np.float32))

segids = np.array(segids, dtype=object)
resids = np.array(resids, dtype=np.int32)
resnames = np.array(resnames, dtype=object)
(residx, new_resids,
(new_resnames, perres_segids)) = squash_by(resids, resnames, segids)
residueids = Resids(new_resids)
residuenames = Resnames(new_resnames)

segidx, perseg_segids = squash_by(perres_segids)[:2]
segids = Segids(perseg_segids)

top = Topology(len(atomids), len(new_resids), len(perseg_segids),
attrs=[atomids, atomnames, atomtypes,
charges, masses,
residueids, residuenames,
segids],
atom_resindex=residx,
residue_segindex=segidx)
top.add_TopologyAttr(Bonds([bond for bond in bonds if bond]))
top.add_TopologyAttr(Angles([angle for angle in angles if angle]))
top.add_TopologyAttr(Dihedrals([dihedral for dihedral in dihedrals
if dihedral]))
top.add_TopologyAttr(Impropers([improper for improper in impropers
if improper]))

return top


def do_symstr(data, symtab):
Expand Down
Loading

0 comments on commit e4a48ee

Please sign in to comment.