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

TPR reader sets chainID from molblock #4281

Merged
merged 8 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions package/MDAnalysis/topology/tpr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
Moltypes,
Molnums,
Segids,
ChainIDs,
Bonds,
Angles,
Dihedrals,
Expand Down Expand Up @@ -308,6 +309,7 @@ def do_mtop(data, fver, tpr_resid_from_one=False):

atomids = []
segids = []
chainIDs = []
resids = []
resnames = []
atomnames = []
Expand All @@ -328,11 +330,13 @@ def do_mtop(data, fver, tpr_resid_from_one=False):
# naming is kind of arbitrary
molblock = mtop.moltypes[mb.molb_type].name.decode('utf-8')
segid = f"seg_{i}_{molblock}"
chainID = molblock[14:] if molblock[:14] == "Protein_chain_" else molblock
for j in range(mb.molb_nmol):
mt = mtop.moltypes[mb.molb_type] # mt: molecule type
for atomkind in mt.atomkinds:
atomids.append(atomkind.id + atom_start_ndx)
segids.append(segid)
chainIDs.append(chainID)
resids.append(atomkind.resid + res_start_ndx)
resnames.append(atomkind.resname.decode())
atomnames.append(atomkind.name.decode())
Expand Down Expand Up @@ -362,6 +366,7 @@ def do_mtop(data, fver, tpr_resid_from_one=False):
moltypes = np.array(moltypes, dtype=object)
molnums = np.array(molnums, dtype=np.int32)
segids = np.array(segids, dtype=object)
chainIDs = np.array(chainIDs, dtype=object)
resids = np.array(resids, dtype=np.int32)
if tpr_resid_from_one:
resids += 1
Expand All @@ -385,15 +390,28 @@ def do_mtop(data, fver, tpr_resid_from_one=False):

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,
residue_moltypes, residue_molnums,
segids],
atom_resindex=residx,
residue_segindex=segidx)
chainIDs = ChainIDs(chainIDs)

top = Topology(
len(atomids),
len(new_resids),
len(perseg_segids),
attrs=[
atomids,
atomnames,
atomtypes,
charges,
masses,
residueids,
residuenames,
residue_moltypes,
residue_molnums,
segids,
chainIDs,
],
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
Expand Down
19 changes: 15 additions & 4 deletions testsuite/MDAnalysisTests/topology/test_tprparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@

class TPRAttrs(ParserBase):
parser = MDAnalysis.topology.TPRParser.TPRParser
expected_attrs = ['ids', 'names', 'elements',
'resids', 'resnames',
'moltypes', 'molnums', 'charges',
'bonds', 'angles', 'dihedrals', 'impropers']
expected_attrs = [
"ids",
"names",
"elements",
"resids",
"resnames",
"chainIDs",
"moltypes",
"molnums",
"charges",
"bonds",
"angles",
"dihedrals",
"impropers",
]

def test_moltypes(self, top):
moltypes = top.moltypes.values
Expand Down