Skip to content

Commit

Permalink
Add chainID attribute to TPR reader
Browse files Browse the repository at this point in the history
  • Loading branch information
pgbarletta committed Sep 5, 2023
1 parent 2853201 commit faf4f42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 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 @@ -371,27 +376,31 @@ def do_mtop(data, fver, tpr_resid_from_one=False):
(new_resnames,
new_moltypes,
new_molnums,
perres_segids
perres_segids,
perres_chainIDs
)
) = squash_by(resids,
resnames,
moltypes,
molnums,
segids)
segids,
chainIDs)
residueids = Resids(new_resids)
residuenames = Resnames(new_resnames)
residue_moltypes = Moltypes(new_moltypes)
residue_molnums = Molnums(new_molnums)

segidx, perseg_segids = squash_by(perres_segids)[:2]
segids = Segids(perseg_segids)
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],
segids, chainIDs],
atom_resindex=residx,
residue_segindex=segidx)
top.add_TopologyAttr(Bonds([bond for bond in bonds if bond]))
Expand Down
2 changes: 1 addition & 1 deletion testsuite/MDAnalysisTests/topology/test_tprparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
class TPRAttrs(ParserBase):
parser = MDAnalysis.topology.TPRParser.TPRParser
expected_attrs = ['ids', 'names', 'elements',
'resids', 'resnames',
'resids', 'resnames', 'chainIDs',
'moltypes', 'molnums', 'charges',
'bonds', 'angles', 'dihedrals', 'impropers']

Expand Down

0 comments on commit faf4f42

Please sign in to comment.