diff --git a/polyply/src/topology.py b/polyply/src/topology.py index 1435413c..6101d3d7 100644 --- a/polyply/src/topology.py +++ b/polyply/src/topology.py @@ -388,7 +388,11 @@ def add_positions_from_file(self, path, skip_res=[], resolution='mol'): for meta_mol in self.molecules: for meta_node in meta_mol.nodes: resname = meta_mol.nodes[meta_node]["resname"] - mol_nodes = meta_mol.nodes[meta_node]['graph'].nodes + # the fragment graph nodes are not sorted so we sort them by index + # as defined in the itp-file to capture cases, where the molecule + # graph nodes are permuted with respect to the index + idx_nodes = nx.get_node_attributes(meta_mol.nodes[meta_node]['graph'], "index") + mol_nodes = sorted(idx_nodes, key=idx_nodes.get) # skip residue if resname is to be skipped or # if the no more coordinates are available # in that case we want to build the node and diff --git a/polyply/tests/test_topology.py b/polyply/tests/test_topology.py index 76230c89..92b7810b 100644 --- a/polyply/tests/test_topology.py +++ b/polyply/tests/test_topology.py @@ -18,9 +18,11 @@ import textwrap import pytest import math +import numpy as np import vermouth.forcefield import vermouth.molecule from vermouth.molecule import Interaction +from vermouth.pdb.pdb import read_pdb import polyply.src.meta_molecule from polyply import TEST_DATA from polyply.src.topology import Topology @@ -87,9 +89,12 @@ def test_add_positions_from_pdb(): """ top = Topology.from_gmx_topfile(TEST_DATA + "/topology_test/pdb.top", "test") top.add_positions_from_file(TEST_DATA + "/topology_test/test.pdb") - for meta_mol in top.molecules: + + pdb_mols = read_pdb(TEST_DATA + "/topology_test/test.pdb") + for idx, meta_mol in enumerate(top.molecules): for node in meta_mol.molecule.nodes: - assert "position" in meta_mol.molecule.nodes[node].keys() + ref_pos = pdb_mols[idx].nodes[node]['position'] + assert np.all(ref_pos == meta_mol.molecule.nodes[node]['position']) for meta_mol in top.molecules: for node in meta_mol.nodes: