from openff.toolkit.topology import Molecule, Topology
smileses = [('ethanol', 'CCO'),
('reversed_ethanol', 'OCC'),
('cyclohexane', 'C1CCCCC1'),
('benzene', 'c1ccccc1'),
('acetate', 'CC(=O)[O-]'),
('chlorine', '[Cl-]'),
('methane', 'C'),
('adamantane', 'C1C2CC3CC1CC(C2)C3')
]
mols = []
for name, smiles in smileses:
mol = Molecule.from_smiles(smiles)
mols.append(mol)
topology1 = Topology.from_molecules(mols[:4])
topology1.add_molecule(mols[0])
topology1.add_molecule(mols[1])
topology2 = Topology.from_molecules(mols[3:])
topology2.add_molecule(mols[0])
topology2.add_molecule(mols[1])
print(topology1.n_topology_molecules, topology2.n_topology_molecules)
def combine_topologies(topology1, topology2):
import copy
topology1 = copy.deepcopy(topology1)
topology2 = copy.deepcopy(topology2)
for topology_molecule in topology2.topology_molecules:
new_mol = topology_molecule.reference_molecule.remap(topology_molecule._ref_to_top_index)
topology1.add_molecule(new_mol)
return topology1
combined_top = combine_topologies(topology1, topology2)
# If we successfuly maintained the atom orders
combined_top.topology_molecules[-1].atom(0)
|