def _build_system(mol, constrained): if constrained: parsley = ForceField("openff-1.0.0.offxml") else: parsley = ForceField("openff_unconstrained-1.0.0.offxml") mol = Molecule.from_file(get_data_file_path("molecules/" + mol), file_format="sdf") if type(mol) == Molecule: off_top = mol.to_topology() positions = mol.conformers[0] elif type(mol) == list: # methane_multiconformer case is a list of two mols off_top = Topology() for mol_i in mol: off_top.add_molecule(mol_i) positions = (np.vstack([mol[0].conformers[0], mol[1].conformers[0]]) * unit.angstrom) from openff.toolkit.utils.toolkits import ( AmberToolsToolkitWrapper, RDKitToolkitWrapper, ToolkitRegistry, ) toolkit_registry = ToolkitRegistry( toolkit_precedence=[RDKitToolkitWrapper, AmberToolsToolkitWrapper]) omm_sys = parsley.create_openmm_system(off_top, toolkit_registry=toolkit_registry) return omm_sys, positions, off_top
def test_to_file_vsites(self): """ Checks that Topology.to_file() doesn't write vsites """ from tempfile import NamedTemporaryFile from openff.toolkit.topology import Molecule, Topology mol = Molecule.from_pdb_and_smiles( get_data_file_path("systems/test_systems/1_ethanol.pdb"), "CCO") carbons = [atom for atom in mol.atoms if atom.atomic_number == 6] positions = mol.conformers[0] mol.add_bond_charge_virtual_site( (carbons[0], carbons[1]), 0.1 * unit.angstrom, charge_increments=[0.1, 0.05] * unit.elementary_charge, ) topology = Topology() topology.add_molecule(mol) count = 0 # The file should be printed out with 9 atoms and 0 virtualsites, so we check to ensure that thtere are only 9 HETATM entries with NamedTemporaryFile(suffix=".pdb") as iofile: topology.to_file(iofile.name, positions) data = open(iofile.name).readlines() for line in data: if line.startswith("HETATM"): count = count + 1 assert count == 9
def test_topology_virtualsites_atom_indexing(self): """ Add multiple instances of the same molecule, but in a different order, and ensure that virtual site particles are indexed correctly """ topology = Topology() topology.add_molecule(create_ethanol()) topology.add_molecule(create_ethanol()) topology.add_molecule(create_reversed_ethanol()) # Add a virtualsite to the reference ethanol for ref_mol in topology.reference_molecules: atoms = [ref_mol.atoms[i] for i in [0, 1]] ref_mol._add_bond_charge_virtual_site( atoms, 0.5 * unit.angstrom, ) virtual_site_topology_atom_indices = [(0, 1), (9, 10), (26, 25)] for top_vs, expected_indices in zip( topology.topology_virtual_sites, virtual_site_topology_atom_indices): assert (tuple([at.topology_particle_index for at in top_vs.atoms]) == expected_indices) assert top_vs.atom( 0).topology_particle_index == expected_indices[0] assert top_vs.atom( 1).topology_particle_index == expected_indices[1]
def test_get_virtual_site(self): """Test Topology.virtual_site function (get virtual site from index)""" topology = Topology() topology.add_molecule(self.ethane_from_smiles_w_vsites) assert topology.n_topology_virtual_sites == 2 topology.add_molecule(self.propane_from_smiles_w_vsites) assert topology.n_topology_virtual_sites == 4 with self.assertRaises(Exception) as context: topology_vsite = topology.virtual_site(-1) with self.assertRaises(Exception) as context: topology_vsite = topology.virtual_site(4) topology_vsite1 = topology.virtual_site(0) topology_vsite2 = topology.virtual_site(1) topology_vsite3 = topology.virtual_site(2) topology_vsite4 = topology.virtual_site(3) assert topology_vsite1.type == "BondChargeVirtualSite" assert topology_vsite2.type == "MonovalentLonePairVirtualSite" assert topology_vsite3.type == "MonovalentLonePairVirtualSite" assert topology_vsite4.type == "BondChargeVirtualSite" n_equal_atoms = 0 for topology_atom in topology.topology_atoms: for vsite in topology.topology_virtual_sites: for vsite_atom in vsite.atoms: if topology_atom == vsite_atom: n_equal_atoms += 1 # There are four virtual sites -- Two BondCharges with 2 atoms, and two MonovalentLonePairs with 3 atoms assert n_equal_atoms == 10
def test_topology_virtual_site_particle_start_index(self): topology = Topology() topology.add_molecule(self.propane_from_smiles_w_vsites) assert topology.virtual_site( 0).topology_virtual_particle_start_index == 11 assert topology.virtual_site( 1).topology_virtual_particle_start_index == 13
def test_n_topology_atoms(self): """Test n_atoms function""" topology = Topology() assert topology.n_topology_atoms == 0 assert topology.n_topology_bonds == 0 topology.add_molecule(self.ethane_from_smiles) assert topology.n_topology_atoms == 8 assert topology.n_topology_bonds == 7
def test_is_bonded(self): """Test Topology.virtual_site function (get virtual site from index)""" topology = Topology() topology.add_molecule(self.propane_from_smiles_w_vsites) topology.assert_bonded(0, 1) topology.assert_bonded(1, 0) topology.assert_bonded(1, 2) # C-H bond topology.assert_bonded(0, 4) with self.assertRaises(Exception) as context: topology.assert_bonded(0, 2)
def test_topology_virtual_site_n_particles(self): """ Test if the virtual sites report the correct number of particles """ topology = Topology() topology.add_molecule(self.propane_from_smiles_w_vsites) assert topology.virtual_site(0).n_particles == 2 assert topology.virtual_site(1).n_particles == 1 topology = Topology() topology.add_molecule(self.tip5_water) assert topology.virtual_site(0).n_particles == 2
def test_n_topology_virtual_sites(self): """Test n_atoms function""" topology = Topology() assert topology.n_topology_virtual_sites == 0 topology.add_molecule(self.ethane_from_smiles_w_vsites) assert topology.n_topology_virtual_sites == 2 topology = Topology() assert topology.n_topology_virtual_sites == 0 topology.add_molecule(self.tip5_water) assert topology.n_topology_virtual_sites == 1 assert topology.n_topology_particles == 5
def test_to_file_multi_molecule_different_order(self): """ Checks for the following if Topology.to_write maintains the order of atoms for the same molecule with different indexing """ from tempfile import NamedTemporaryFile from openff.toolkit.tests.test_forcefield import ( create_ethanol, create_reversed_ethanol, ) from openff.toolkit.topology import Molecule, Topology topology = Topology() topology.add_molecule(create_ethanol()) topology.add_molecule(create_reversed_ethanol()) mol = Molecule.from_pdb_and_smiles( get_data_file_path("systems/test_systems/1_ethanol.pdb"), "CCO") positions = mol.conformers[0] # Make up coordinates for the second ethanol by translating the first by 10 angstroms # (note that this will still be a gibberish conformation, since the atom order in the second molecule is different) positions = np.concatenate( [positions, positions + 10.0 * unit.angstrom]) element_order = [] with NamedTemporaryFile(suffix=".pdb") as iofile: topology.to_file(iofile.name, positions) data = open(iofile.name).readlines() for line in data: if line.startswith("HETATM"): element_order.append(line.strip()[-1]) assert element_order == [ "C", "C", "O", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "O", "C", "C", ]