from pymatgen.core.structure import Molecule molecule = Molecule.from_file("molecule.xyz")
def test_get_bonds(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t1.xyz")) msc = MoleculeStructureComparator() # noinspection PyProtectedMember bonds = msc._get_bonds(mol1) bonds_ref = [(0, 1), (0, 2), (0, 3), (0, 23), (3, 4), (3, 5), (5, 6), (5, 7), (7, 8), (7, 9), (7, 21), (9, 10), (9, 11), (9, 12), (12, 13), (12, 14), (12, 15), (15, 16), (15, 17), (15, 18), (18, 19), (18, 20), (18, 21), (21, 22), (21, 23), (23, 24), (23, 25)] self.assertEqual(bonds, bonds_ref) mol2 = Molecule.from_file(os.path.join(test_dir, "MgBH42.xyz")) bonds = msc._get_bonds(mol2) self.assertEqual(bonds, [(1, 3), (2, 3), (3, 4), (3, 5), (6, 8), (7, 8), (8, 9), (8, 10)]) msc = MoleculeStructureComparator(ignore_ionic_bond=False) bonds = msc._get_bonds(mol2) self.assertEqual(bonds, [(0, 1), (0, 2), (0, 3), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 3), (2, 3), (3, 4), (3, 5), (6, 8), (7, 8), (8, 9), (8, 10)]) mol1 = Molecule.from_file(os.path.join(test_dir, "molecule_with_halogen_bonds_1.xyz")) msc = MoleculeStructureComparator() # noinspection PyProtectedMember bonds = msc._get_bonds(mol1) self.assertEqual(bonds, [(0, 12), (0, 13), (0, 14), (0, 15), (1, 12), (1, 16), (1, 17), (1, 18), (2, 4), (2, 11), (2, 19), (3, 5), (3, 10), (3, 20), (4, 6), (4, 10), (5, 11), (5, 12), (6, 7), (6, 8), (6, 9)])
def setUpClass(cls): polymer_chain = Molecule.from_file(os.path.join(test_dir, "polymer_chain.xyz")) polymer_linear = Molecule.from_file(os.path.join(test_dir, "polymer_linear.xyz")) cls.polymer_matrix = Molecule.from_file(os.path.join(test_dir, "polymer_matrix.xyz")) charges = [-0.1187, 0.0861, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.1187, 0.0861, 0.0861, 0.0861] polymer_linear.add_site_property("charge", charges) topology = Topology.from_molecule(polymer_linear) cls.polymer_linear_ff_decorated = Molecule.from_file( os.path.join(test_dir,"polymer_linear.xyz")) ff_map = ['C2', 'H3', 'H2', 'H3', 'O', 'C3', 'H2', 'H3', 'C2', 'H3', 'H2', 'O', 'C2', 'H3', 'H2', 'C3', 'H2', 'H3', 'O', 'C3', 'H2', 'H3', 'C2', 'H3', 'H2', 'O', 'C2', 'H3', 'H2', 'C3', 'H2', 'H3', 'O', 'C3', 'H2', 'H3', 'C2', 'H3', 'H2', 'O', 'C2', 'H3', 'H2', 'C3', 'H2', 'H3', 'O', 'C3', 'H2', 'H3', 'H2'] cls.polymer_linear_ff_decorated.add_site_property("ff_map", ff_map) atoms = OrderedDict([("C", "C"), ("H", "H"), ("O", "O")]) bonds = OrderedDict([((u'C', u'O'), [1000, 1.4115]), ((u'C', u'H'), [1000, 1.1041]), ((u'C', u'C'), [1000, 1.5075])]) pairs = OrderedDict([((u'O', u'O'), [75844.8, 0.2461, 396.9]), ((u'H', u'H'), [2649.6, 0.2674, 27.22]), ((u'C', u'C'), [14976.0, 0.3236, 637.6])]) angles = OrderedDict([((u'C', u'C', u'H'), [42.9, 110.1]), ((u'H', u'C', u'H'), [38.5, 109.47]), ((u'H', u'C', u'O'), [56.0, 109.48]), ((u'C', u'C', u'O'), [86.0, 108.54]), ((u'C', u'O', u'C'), [74.5, 108.05])]) dihedrals = OrderedDict([((u'H', u'C', u'O', u'C'), [0.0, 0.0, -0.73, 0.0]), ((u'H', u'C', u'C', u'H'), [0.0, 0.0, 0.28, 0.0]), ((u'C', u'C', u'O', u'C'), [1.76, 0.67, 0.04, 0.0]), ((u'H', u'C', u'C', u'O'), [0.0, 0.0, 0.28, 0.0]), ((u'O', u'C', u'C', u'O'), [0.41, -2.1, -0.6, -0.82])]) forcefield = ForceField(atoms, bonds, angles, dihedrals=dihedrals, pairs=pairs) cls.molecules = [polymer_chain] * 3 cls.mols_number = [7, 3, 1] box_size = [[0.0, 50], [0.0, 50], [0.0, 50]] cls.topologies = [topology] * len(cls.molecules) cls.lammps_ff_data_1 = LammpsForceFieldData.from_forcefield_and_topology( cls.molecules, cls.mols_number, box_size, cls.polymer_matrix, forcefield, cls.topologies)
def setUpClass(cls): cls.pc = Molecule.from_file( os.path.join(test_dir, "PC.xyz")) cls.pos_pc = Molecule.from_file( os.path.join(test_dir, "PC.xyz")) cls.pos_pc.set_charge_and_spin(charge=1) cls.pc_edges = [[5, 10], [5, 12], [5, 11], [5, 3], [3, 7], [3, 4], [3, 0], [4, 8], [4, 9], [4, 1], [6, 1], [6, 0], [6, 2]] cls.pc_frag1 = Molecule.from_file( os.path.join(test_dir, "PC_frag1.xyz")) cls.pc_frag1_edges = [[0, 2], [4, 2], [2, 1], [1, 3]] cls.tfsi = Molecule.from_file(os.path.join(test_dir, "TFSI.xyz")) cls.tfsi_edges = [14,1],[1,4],[1,5],[1,7],[7,11],[7,12],[7,13],[14,0],[0,2],[0,3],[0,6],[6,8],[6,9],[6,10]
def test_are_equal(self): msc1 = MoleculeStructureComparator() mol1 = Molecule.from_file(os.path.join(test_dir, "t1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t2.xyz")) mol3 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) self.assertFalse(msc1.are_equal(mol1, mol2)) self.assertTrue(msc1.are_equal(mol2, mol3)) thio1 = Molecule.from_file(os.path.join(test_dir, "thiophene1.xyz")) thio2 = Molecule.from_file(os.path.join(test_dir, "thiophene2.xyz")) # noinspection PyProtectedMember msc2 = MoleculeStructureComparator( priority_bonds=msc1._get_bonds(thio1)) self.assertTrue(msc2.are_equal(thio1, thio2))
def setUp(self): cyclohexene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/cyclohexene.xyz")) self.cyclohexene = MoleculeGraph.with_empty_graph(cyclohexene, edge_weight_name="strength", edge_weight_units="") self.cyclohexene.add_edge(0, 1, weight=1.0) self.cyclohexene.add_edge(1, 2, weight=1.0) self.cyclohexene.add_edge(2, 3, weight=2.0) self.cyclohexene.add_edge(3, 4, weight=1.0) self.cyclohexene.add_edge(4, 5, weight=1.0) self.cyclohexene.add_edge(5, 0, weight=1.0) self.cyclohexene.add_edge(0, 6, weight=1.0) self.cyclohexene.add_edge(0, 7, weight=1.0) self.cyclohexene.add_edge(1, 8, weight=1.0) self.cyclohexene.add_edge(1, 9, weight=1.0) self.cyclohexene.add_edge(2, 10, weight=1.0) self.cyclohexene.add_edge(3, 11, weight=1.0) self.cyclohexene.add_edge(4, 12, weight=1.0) self.cyclohexene.add_edge(4, 13, weight=1.0) self.cyclohexene.add_edge(5, 14, weight=1.0) self.cyclohexene.add_edge(5, 15, weight=1.0) butadiene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/butadiene.xyz")) self.butadiene = MoleculeGraph.with_empty_graph(butadiene, edge_weight_name="strength", edge_weight_units="") self.butadiene.add_edge(0, 1, weight=2.0) self.butadiene.add_edge(1, 2, weight=1.0) self.butadiene.add_edge(2, 3, weight=2.0) self.butadiene.add_edge(0, 4, weight=1.0) self.butadiene.add_edge(0, 5, weight=1.0) self.butadiene.add_edge(1, 6, weight=1.0) self.butadiene.add_edge(2, 7, weight=1.0) self.butadiene.add_edge(3, 8, weight=1.0) self.butadiene.add_edge(3, 9, weight=1.0) ethylene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/ethylene.xyz")) self.ethylene = MoleculeGraph.with_empty_graph(ethylene, edge_weight_name="strength", edge_weight_units="") self.ethylene.add_edge(0, 1, weight=2.0) self.ethylene.add_edge(0, 2, weight=1.0) self.ethylene.add_edge(0, 3, weight=1.0) self.ethylene.add_edge(1, 4, weight=1.0) self.ethylene.add_edge(1, 5, weight=1.0) warnings.simplefilter("ignore")
def setUp(self): self.polymer_linear = Molecule.from_file( os.path.join(test_dir, "polymer_linear.xyz")) charges = [-0.1187, 0.0861, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.1187, 0.0861, 0.0861, 0.0861] ff_map = ["C3", "H3", "H3", "H3", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C3", "H3", "H3", "H3"] self.polymer_linear.add_site_property("charge", charges) self.polymer_linear.add_site_property("ff_map", ff_map) self.topology = Topology.from_molecule(self.polymer_linear) self.forcefield = ForceField.from_file( os.path.join(test_dir, "ffmap_data.yaml")) box_size = [[0.0, 50], [0.0, 50], [0.0, 50]] self.lammps_ff_data = LammpsForceFieldData.from_forcefield_and_topology( [self.polymer_linear], [1], box_size, self.polymer_linear, self.forcefield, [self.topology])
def test_permuted_atoms_order(self): # This test shows very poor rmsd result, because the `KabschMatcher` # is not capable to handle arbitrary atom's order mol2 = Molecule.from_file( os.path.join(test_dir, "Si_cluster_permuted.xyz")) _, rmsd = self.mm.fit(mol2) self.assertAlmostEqual(rmsd, 2.7962454578966454, places=6)
def test_mol_to_mol_graph(self): mol = Molecule.from_file( (test_dir / "molecules" / "li2co3_1.xyz").as_posix()) mg = MoleculeGraph.with_local_env_strategy(mol, OpenBabelNN()) mg = metal_edge_extender(mg) self.assertEqual(mg, mol_to_mol_graph(mol))
def test_isomorphic(self): ethylene = Molecule.from_file( os.path.join( os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/ethylene.xyz", ) ) # switch carbons ethylene[0], ethylene[1] = ethylene[1], ethylene[0] eth_copy = MoleculeGraph.with_edges( ethylene, { (0, 1): {"weight": 2}, (1, 2): {"weight": 1}, (1, 3): {"weight": 1}, (0, 4): {"weight": 1}, (0, 5): {"weight": 1}, }, ) # If they are equal, they must also be isomorphic eth_copy = copy.deepcopy(self.ethylene) self.assertTrue(self.ethylene.isomorphic_to(eth_copy)) self.assertFalse(self.butadiene.isomorphic_to(self.ethylene))
def test_isomorphic_to(self): ethylene = Molecule.from_file( os.path.join( os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/ethylene.xyz", ) ) # switch carbons ethylene[0], ethylene[1] = ethylene[1], ethylene[0] eth_copy = MoleculeGraph.with_edges( ethylene, { (0, 1): {"weight": 2}, (1, 2): {"weight": 1}, (1, 3): {"weight": 1}, (0, 4): {"weight": 1}, (0, 5): {"weight": 1}, }, ) # If they are equal, they must also be isomorphic eth_copy = copy.deepcopy(self.ethylene) self.assertTrue(self.ethylene.isomorphic_to(eth_copy)) self.assertFalse(self.butadiene.isomorphic_to(self.ethylene))
def setUpClass(cls): ethanol_coords = [ [0.00720, -0.56870, 0.00000], [-1.28540, 0.24990, 0.00000], [1.13040, 0.31470, 0.00000], [0.03920, -1.19720, 0.89000], [0.03920, -1.19720, -0.89000], [-1.31750, 0.87840, 0.89000], [-1.31750, 0.87840, -0.89000], [-2.14220, -0.42390, -0.00000], [1.98570, -0.13650, -0.00000], ] water_coords = [ [9.626, 6.787, 12.673], [9.626, 8.420, 12.673], [10.203, 7.604, 12.673], ] cls.ethanol_atoms = ["C", "C", "O", "H", "H", "H", "H", "H", "H"] cls.water_atoms = ["H", "H", "O"] ethanol = Molecule(cls.ethanol_atoms, ethanol_coords) water = Molecule(cls.water_atoms, water_coords) cls.mols = [ethanol, water] cls.cocktail = Molecule.from_file( os.path.join(PymatgenTest.TEST_FILES_DIR, "lammps", "cocktail.xyz")) cls.packmol_config = [{"number": 1}, {"number": 15}]
def test_spherical(self): a = PointGroupAnalyzer(CH4) self.assertEqual(a.sch_symbol, "Td") self.assertEqual(len(a.get_pointgroup()), 24) a = PointGroupAnalyzer(PF6) self.assertEqual(a.sch_symbol, "Oh") self.assertEqual(len(a.get_pointgroup()), 48) m = Molecule.from_file(os.path.join(test_dir_mol, "c60.xyz")) a = PointGroupAnalyzer(m) self.assertEqual(a.sch_symbol, "Ih") cube_species = ["C", "C", "C", "C", "C", "C", "C", "C"] cube_coords = [ [0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1], ] m = Molecule(cube_species, cube_coords) a = PointGroupAnalyzer(m, 0.1) self.assertEqual(a.sch_symbol, "Oh")
def test_get_atoms_from_molecule_dyn(self): from ase.constraints import FixAtoms molecule = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz")) molecule.add_site_property("selective_dynamics", [[False] * 3] * len(molecule)) atoms = aio.AseAtomsAdaptor.get_atoms(molecule) self.assertEqual(atoms.constraints[0].get_indices().tolist(), [atom.index for atom in atoms])
def test_create(self): # Test with too many coordinates with self.assertRaises(ValueError): too_many_coords = GSMIsomerInput(bonds_formed=[(0, 1), (1, 2)], angles=[(4, 5, 6), (9, 10, 11)], torsions=[(3, 4, 7, 8)]) # Test with non-integer indices with self.assertRaises(ValueError): non_integer = GSMIsomerInput(bonds_broken=[("pi", "three")]) # Test with wrong number of indices with self.assertRaises(ValueError): wrong_indices = GSMIsomerInput(bonds_formed=[(1, 2, 3)]) with self.assertRaises(ValueError): wrong_indices = GSMIsomerInput(bonds_broken=[(1, 2, 3)]) with self.assertRaises(ValueError): wrong_indices = GSMIsomerInput(angles=[(1, 3)]) with self.assertRaises(ValueError): wrong_indices = GSMIsomerInput(torsions=[(1, 2, 3)]) with self.assertRaises(ValueError): wrong_indices = GSMIsomerInput(out_of_planes=[(1, 2, 3, 4, 5)]) # Test with molecule - indices too high mol = Molecule.from_file(os.path.join(test_dir, "ethane.mol")) with self.assertRaises(ValueError): too_high = GSMIsomerInput(molecule=mol, bonds_broken=[(7, 9)]) # Test good good = GSMIsomerInput(molecule=mol, bonds_broken=[(0, 1)]) self.assertEqual(good.molecule, mol) self.assertEqual(good.bonds_broken, [(0, 1)])
def test_permuted_atoms_order(self): # This task should fail, because `KabschMatcher` is not capable # to handle arbitrary atom's order mol2 = Molecule.from_file( os.path.join(test_dir, "Si2O_cluster_permuted.xyz")) with self.assertRaises(ValueError): _, rmsd = self.mm.fit(mol2)
def find_mol_entry_from_xyz_and_charge(mol_entries, xyz_file_path, charge): """ given a file 'molecule.xyz', find the mol_entry corresponding to the molecule graph with given charge """ target_mol_graph = MoleculeGraph.with_local_env_strategy( Molecule.from_file(xyz_file_path), OpenBabelNN()) # correction to the molecule graph target_mol_graph = metal_edge_extender(target_mol_graph) match = False index = -1 while not match: index += 1 mol_entry = mol_entries[index] species_mol_graph = mol_entry.mol_graph if mol_entry.charge == charge: match = target_mol_graph.isomorphic_to(species_mol_graph) if match: return mol_entry else: return None
def test_get_molecule(self): benzene_file = join(files_dir, "benzene.mol") benzene_pmg = Molecule.from_file(benzene_file) benzene_moltherm = get_molecule(benzene_file) species_no_h = [ e for e in benzene_moltherm.species if str(e).upper() != "H" ] self.assertEqual(species_no_h, benzene_pmg.species) # Test that implicit hydrogens are added appropriately species = sorted([str(e) for e in benzene_moltherm.species]) self.assertEqual( species, ["C", "C", "C", "C", "C", "C", "H", "H", "H", "H", "H", "H"]) # Ensure that get_molecule is deterministic, always produces the same # molecule self.assertEqual(benzene_moltherm, get_molecule(benzene_file)) coords = np.array([[-7.59858151e-01, 1.16908119e+00, -1.61105859e-03], [-1.39065495e+00, -7.49583582e-02, -9.63095317e-04], [-6.28683825e-01, -1.24326334e+00, 6.60526465e-04], [7.64084196e-01, -1.16752892e+00, 1.63614012e-03], [1.39488100e+00, 7.65106237e-02, 9.87135623e-04], [6.32909871e-01, 1.24481561e+00, -6.36441101e-04], [-1.35352141e+00, 2.07932532e+00, -2.87509442e-03], [-2.47578162e+00, -1.33964201e-01, -1.72330284e-03], [-1.12014718e+00, -2.21251339e+00, 1.16530208e-03], [1.35774746e+00, -2.07777305e+00, 2.90204589e-03], [2.48000766e+00, 1.35516465e-01, 1.74638272e-03], [1.12437322e+00, 2.21406566e+00, -1.14215271e-03]]) self.assertTrue(np.allclose(benzene_moltherm.cart_coords, coords))
def test_get_atoms_from_molecule_mags(self): molecule = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz")) atoms = aio.AseAtomsAdaptor.get_atoms(molecule) mags = [1.0] * len(molecule) molecule.add_site_property("magmom", mags) atoms = aio.AseAtomsAdaptor.get_atoms(molecule) self.assertFalse(atoms.has("initial_magmoms")) self.assertEqual(atoms.get_magnetic_moments().tolist(), mags)
def test_missmatched_atom_order(self): mol1 = Molecule.from_file(os.path.join(test_dir, "benzene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "benzene2.xyz")) mm = KabschMatcher(mol1) with self.assertRaises(ValueError): _, rmsd = mm.fit(mol2) mol1 = Molecule.from_file(os.path.join(test_dir, "c1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "c2.xyz")) mm = KabschMatcher(mol1) with self.assertRaises(ValueError): _, rmsd = mm.fit(mol2)
def test_to_and_from_dict(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mm_source = HungarianOrderMatcher(mol1) d_source = mm_source.as_dict() mm_target = HungarianOrderMatcher.from_dict(d_source) self.assertDictEqual(d_source, mm_target.as_dict())
def test_to_and_from_dict(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mm_source = GeneticOrderMatcher(mol1, threshold=0.3) d_source = mm_source.as_dict() mm_target = GeneticOrderMatcher.from_dict(d_source) self.assertDictEqual(d_source, mm_target.as_dict())
def write_xyz(in_file, out_dir): """ Write xyz file. """ molecule_name = in_file.split('/')[-1][:14] mol = Molecule.from_file(in_file) fout = os.path.join(out_dir, molecule_name + '.xyz') mol.to(fmt='xyz', filename=fout)
def setUp(self): warnings.simplefilter("ignore") self.file = os.path.join(test_dir, "func_group_test.mol") self.mol = Molecule.from_file(self.file) self.strat = OpenBabelNN() self.mg = MoleculeGraph.with_local_env_strategy(self.mol, self.strat) self.extractor = FunctionalGroupExtractor(self.mg)
def test_get_atoms_from_molecule(self): m = Molecule.from_file( os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz")) atoms = aio.AseAtomsAdaptor.get_atoms(m) ase_composition = Composition(atoms.get_chemical_formula()) self.assertEqual(ase_composition, m.composition) self.assertTrue(atoms.cell is None or not atoms.cell.any()) self.assertTrue(atoms.get_pbc() is None or not atoms.get_pbc().any())
def test_structural_change(self): t1 = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change", "t1.xyz")) t2 = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change", "t2.xyz")) t3 = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change", "t3.xyz")) thio_1 = Molecule.from_file( os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change", "thiophene1.xyz") ) thio_2 = Molecule.from_file( os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change", "thiophene2.xyz") ) frag_1 = Molecule.from_file( os.path.join( PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "test_structure_change", "frag_1.xyz" ) ) frag_2 = Molecule.from_file( os.path.join( PymatgenTest.TEST_FILES_DIR, "molecules", "new_qchem_files", "test_structure_change", "frag_2.xyz" ) ) self.assertEqual(check_for_structure_changes(t1, t1), "no_change") self.assertEqual(check_for_structure_changes(t2, t3), "no_change") self.assertEqual(check_for_structure_changes(t1, t2), "fewer_bonds") self.assertEqual(check_for_structure_changes(t2, t1), "more_bonds") self.assertEqual(check_for_structure_changes(thio_1, thio_2), "unconnected_fragments") self.assertEqual(check_for_structure_changes(frag_1, frag_2), "bond_change")
def test_get_atoms_from_molecule(self): m = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz")) atoms = aio.AseAtomsAdaptor.get_atoms(m) ase_composition = Composition(atoms.get_chemical_formula()) self.assertEqual(ase_composition, m.composition) self.assertTrue(atoms.cell is None or not atoms.cell.any()) self.assertTrue(atoms.get_pbc() is None or not atoms.get_pbc().any()) self.assertEqual(atoms.get_chemical_symbols(), [s.species_string for s in m]) self.assertFalse(atoms.has("initial_magmoms"))
def test_disordered(self, molecule_filepath): m = Molecule.from_file(molecule_filepath) assert not m.is_ordered with pytest.raises( ValueError, match=r'^MoleculeSystem does not handle disordered structures.$' ): MoleculeSystem(m).to_coord_string()
def setUp(self): warnings.simplefilter("ignore") self.file = os.path.join(test_dir, "func_group_test.mol") self.mol = Molecule.from_file(self.file) self.strat = OpenBabelNN() self.mg = MoleculeGraph.with_local_env_strategy(self.mol, self.strat, reorder=False, extend_structure=False) self.extractor = FunctionalGroupExtractor(self.mg)
def test_dihedral(self): a = PointGroupAnalyzer(C2H4) self.assertEqual(a.sch_symbol, "D2h") self.assertEqual(len(a.get_pointgroup()), 8) a = PointGroupAnalyzer(BF3) self.assertEqual(a.sch_symbol, "D3h") self.assertEqual(len(a.get_pointgroup()), 12) m = Molecule.from_file(os.path.join(test_dir_mol, "b12h12.xyz")) a = PointGroupAnalyzer(m) self.assertEqual(a.sch_symbol, "Ih")
def test_spherical(self): a = PointGroupAnalyzer(CH4) self.assertEqual(a.sch_symbol, "Td") self.assertEqual(len(a.get_pointgroup()), 24) a = PointGroupAnalyzer(PF6) self.assertEqual(a.sch_symbol, "Oh") self.assertEqual(len(a.get_pointgroup()), 48) m = Molecule.from_file(os.path.join(test_dir_mol, "c60.xyz")) a = PointGroupAnalyzer(m) self.assertEqual(a.sch_symbol, "Ih")
def test_dihedral(self): a = PointGroupAnalyzer(C2H4) self.assertEqual(a.sch_symbol, "D2h") self.assertEqual(len(a.get_pointgroup()), 8) a = PointGroupAnalyzer(BF3) self.assertEqual(a.sch_symbol, "D3h") self.assertEqual(len(a.get_pointgroup()), 12) m = Molecule.from_file(os.path.join(test_dir_mol, "b12h12.xyz")) a = PointGroupAnalyzer(m) self.assertEqual(a.sch_symbol, "D5d")
def setUpClass(cls): polymer_chain = Molecule.from_file(os.path.join(test_dir,"polymer_chain.xyz")) polymer_linear = Molecule.from_file(os.path.join(test_dir,"polymer_linear.xyz")) cls.polymer_matrix = Molecule.from_file(os.path.join(test_dir,"polymer_matrix.xyz")) charges = [-0.1187, 0.0861, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.0326, 0.0861, 0.0861, -0.0326, 0.0861, 0.0861, -0.2792, -0.1187, 0.0861, 0.0861, 0.0861] polymer_linear.add_site_property("charge", charges) topology = Topology.from_molecule(polymer_linear) atoms = OrderedDict([("C","C"), ("H","H"), ("O", "O")]) bonds = OrderedDict([((u'C', u'O'), [1000, 1.4115]), ((u'C', u'H'), [1000, 1.1041]), ((u'C', u'C'), [1000, 1.5075])]) pairs = OrderedDict([((u'O', u'O'), [75844.8, 0.2461, 396.9]), ((u'H', u'H'), [2649.6, 0.2674, 27.22]), ((u'C', u'C'), [14976.0, 0.3236, 637.6])]) angles = OrderedDict([((u'C', u'C', u'H'), [42.9, 110.1]), ((u'H', u'C', u'H'), [38.5, 109.47]), ((u'H', u'C', u'O'), [56.0, 109.48]), ((u'C', u'C', u'O'), [86.0, 108.54]), ((u'C', u'O', u'C'), [74.5, 108.05])]) dihedrals = OrderedDict([((u'H', u'C', u'O', u'C'), [0.0, 0.0, -0.73, 0.0]), ((u'H', u'C', u'C', u'H'), [0.0, 0.0, 0.28, 0.0]), ((u'C', u'C', u'O', u'C'), [1.76, 0.67, 0.04, 0.0]), ((u'H', u'C', u'C', u'O'), [0.0, 0.0, 0.28, 0.0]), ((u'O', u'C', u'C', u'O'), [0.41, -2.1, -0.6, -0.82])]) forcefield =ForceField(atoms, bonds, angles, dihedrals=dihedrals, pairs=pairs) cls.molecules = [polymer_chain] * 3 cls.mols_number = [7, 3, 1] box_size = [[0.0, 50], [0.0, 50], [0.0, 50]] cls.topologies = [topology] * len(cls.molecules) cls.lammps_ff_data_1 = LammpsForceFieldData.from_forcefield_and_topology( cls.molecules, cls.mols_number, box_size, cls.polymer_matrix, forcefield, cls.topologies)
def test_get_bonds(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t1.xyz")) msc = MoleculeStructureComparator() # noinspection PyProtectedMember bonds = msc._get_bonds(mol1) bonds_ref = [(0, 1), (0, 2), (0, 3), (0, 23), (3, 4), (3, 5), (5, 6), (5, 7), (7, 8), (7, 9), (7, 21), (9, 10), (9, 11), (9, 12), (12, 13), (12, 14), (12, 15), (15, 16), (15, 17), (15, 18), (18, 19), (18, 20), (18, 21), (21, 22), (21, 23), (23, 24), (23, 25)] self.assertEqual(bonds, bonds_ref) mol2 = Molecule.from_file(os.path.join(test_dir, "MgBH42.xyz")) bonds = msc._get_bonds(mol2) self.assertEqual(bonds, [(1, 3), (2, 3), (3, 4), (3, 5), (6, 8), (7, 8), (8, 9), (8, 10)]) msc = MoleculeStructureComparator(ignore_ionic_bond=False) bonds = msc._get_bonds(mol2) self.assertEqual(bonds, [(0, 1), (0, 2), (0, 3), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 3), (2, 3), (3, 4), (3, 5), (6, 8), (7, 8), (8, 9), (8, 10)])
def test_group_molecules(self): mm = MoleculeMatcher(tolerance=0.001) with open(os.path.join(test_dir, "mol_list.txt")) as f: filename_list = [line.strip() for line in f.readlines()] mol_list = [Molecule.from_file(os.path.join(test_dir, f)) for f in filename_list] mol_groups = mm.group_molecules(mol_list) filename_groups = [[filename_list[mol_list.index(m)] for m in g] for g in mol_groups] with open(os.path.join(test_dir, "grouped_mol_list.txt")) as f: grouped_text = f.read().strip() self.assertEqual(str(filename_groups), grouped_text)
def test_bucket_mol_entries(): C2H4_entry = MoleculeEntry( Molecule.from_file(os.path.join(test_dir, "C2H4.xyz")), energy=0.0, enthalpy=0.0, entropy=0.0, ) LiEC_RO_entry = MoleculeEntry( Molecule.from_file(os.path.join(test_dir, "LiEC_RO.xyz")), energy=0.0, enthalpy=0.0, entropy=0.0, ) C1Li1O3_entry = MoleculeEntry( Molecule.from_file(os.path.join(test_dir, "C1Li1O3.xyz")), energy=0.0, enthalpy=0.0, entropy=0.0, ) bucket = bucket_mol_entries([C2H4_entry, LiEC_RO_entry, C1Li1O3_entry]) ref_dict = { "C2 H4": { 5: { 0: [C2H4_entry] } }, "C3 H4 Li1 O3": { 11: { 0: [LiEC_RO_entry] } }, "C1 Li1 O3": { 5: { 0: [C1Li1O3_entry] } }, } assert bucket == ref_dict
def test_constraints_file(self): constraints = {"atoms": [8, 1, 2], "force_constant": 0.5} mol = Molecule.from_file(os.path.join(test_dir, "crest_in.xyz")) cin = CRESTInput(molecule=mol, constraints=constraints) with open(os.path.join(expected_output_dir, "expected_constrains.txt")) as f: exp_con = f.read() self.assertEqual( exp_con.strip(), cin.constrains_template(molecule=mol, reference_fnm="crest_in.xyz", constraints=constraints).strip(), )
def test_cyclic(self): a = PointGroupAnalyzer(H2O2) self.assertEqual(a.sch_symbol, "C2") self.assertEqual(len(a.get_pointgroup()), 2) a = PointGroupAnalyzer(H2O) self.assertEqual(a.sch_symbol, "C2v") self.assertEqual(len(a.get_pointgroup()), 4) a = PointGroupAnalyzer(NH3) self.assertEqual(a.sch_symbol, "C3v") self.assertEqual(len(a.get_pointgroup()), 6) cs2 = Molecule.from_file(test_dir_mol / "Carbon_Disulfide.xyz") a = PointGroupAnalyzer(cs2, eigen_tolerance=0.001) self.assertEqual(a.sch_symbol, "C2v")
def test_cyclic(self): a = PointGroupAnalyzer(H2O2) self.assertEqual(a.sch_symbol, "C2") self.assertEqual(len(a.get_pointgroup()), 2) a = PointGroupAnalyzer(H2O) self.assertEqual(a.sch_symbol, "C2v") self.assertEqual(len(a.get_pointgroup()), 4) a = PointGroupAnalyzer(NH3) self.assertEqual(a.sch_symbol, "C3v") self.assertEqual(len(a.get_pointgroup()), 6) cs2 = Molecule.from_file(os.path.join(test_dir_mol, "Carbon_Disulfide.xyz")) a = PointGroupAnalyzer(cs2, eigen_tolerance=0.001) self.assertEqual(a.sch_symbol, "C2v")
def test_to_and_from_dict(self): msc1 = MoleculeStructureComparator() d1 = msc1.as_dict() d2 = MoleculeStructureComparator.from_dict(d1).as_dict() self.assertEqual(d1, d2) thio1 = Molecule.from_file(os.path.join(test_dir, "thiophene1.xyz")) # noinspection PyProtectedMember msc2 = MoleculeStructureComparator( bond_length_cap=0.2, priority_bonds=msc1._get_bonds(thio1), priority_cap=0.5 ) d1 = msc2.as_dict() d2 = MoleculeStructureComparator.from_dict(d1).as_dict() self.assertEqual(d1, d2)
def test_to_and_from_dict(self): msc1 = MoleculeStructureComparator() d1 = msc1.as_dict() d2 = MoleculeStructureComparator.from_dict(d1).as_dict() self.assertEqual(d1, d2) thio1 = Molecule.from_file(os.path.join(test_dir, "thiophene1.xyz")) # noinspection PyProtectedMember msc2 = MoleculeStructureComparator( bond_length_cap=0.2, priority_bonds=msc1._get_bonds(thio1), priority_cap=0.5) d1 = msc2.as_dict() d2 = MoleculeStructureComparator.from_dict(d1).as_dict() self.assertEqual(d1, d2)
def test_to_from_file_string(self): for fmt in ["xyz", "json", "g03", "yaml"]: s = self.mol.to(fmt=fmt) self.assertIsNotNone(s) m = IMolecule.from_str(s, fmt=fmt) self.assertEqual(m, self.mol) self.assertIsInstance(m, IMolecule) self.mol.to(filename="CH4_testing.xyz") self.assertTrue(os.path.exists("CH4_testing.xyz")) os.remove("CH4_testing.xyz") self.mol.to(filename="CH4_testing.yaml") self.assertTrue(os.path.exists("CH4_testing.yaml")) mol = Molecule.from_file("CH4_testing.yaml") self.assertEqual(self.mol, mol) os.remove("CH4_testing.yaml")
def test_spherical(self): a = PointGroupAnalyzer(CH4) self.assertEqual(a.sch_symbol, "Td") self.assertEqual(len(a.get_pointgroup()), 24) a = PointGroupAnalyzer(PF6) self.assertEqual(a.sch_symbol, "Oh") self.assertEqual(len(a.get_pointgroup()), 48) m = Molecule.from_file(os.path.join(test_dir_mol, "c60.xyz")) a = PointGroupAnalyzer(m) self.assertEqual(a.sch_symbol, "Ih") cube_species = ["C", "C", "C", "C", "C", "C", "C", "C"] cube_coords = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]] m = Molecule(cube_species, cube_coords) a = PointGroupAnalyzer(m, 0.1) self.assertEqual(a.sch_symbol, "Oh")
def setUp(self): # DMOE = Dimethoxyethane self.dmoe = Molecule.from_file(os.path.join(test_dir, "dmoe.xyz")) dmoe_charges = [-0.10, 0.09, 0.09, 0.09, -0.34, -0.01, 0.09, 0.09, -0.01, 0.09, 0.09, -0.34, -0.10, 0.09, 0.09, 0.09] self.dmoe.add_site_property("charge", dmoe_charges) ff_map = ["C3", "H3", "H3", "H3", "O", "C2", "H2", "H2", "C2", "H2", "H2", "O", "C3", "H3", "H3", "H3"] self.dmoe.add_site_property("ff_map", ff_map) self.topology = Topology.from_molecule(self.dmoe, ff_map="ff_map") self.forcefield = ForceField.from_file( os.path.join(test_dir, "ffmap_data.yaml")) self.box_size = [[0.0, 10], [0.0, 10], [0.0, 10]] self.lammps_ff_data = \ LammpsForceFieldData.from_forcefield_and_topology( [self.dmoe], [1], self.box_size, self.dmoe, self.forcefield, [self.topology])
def test_tricky(self): m = Molecule.from_file(test_dir_mol / "dh.xyz") a = PointGroupAnalyzer(m, 0.1) self.assertEqual(a.sch_symbol, "D*h")
def setUpClass(cls): polymer_chain = Molecule.from_file(os.path.join(test_dir, "polymer_chain.xyz")) box_size = [[0.0, 20.0], [0.0, 20.0], [0.0, 20.0]] cls.lammps_data = LammpsData.from_structure(polymer_chain, box_size)
def copy(self): return Structure.from_sites(self) # test if __name__ == '__main__': # the following example require: # acetic_acid.xyz and POSCAR.mp-21276_PbS # create lead acetate ligand # from 3 molecules: 2 acetic acid + 1 Pb import os PACKAGE_PATH = os.path.dirname(__file__) mol0 = Molecule.from_file( os.path.join(PACKAGE_PATH, "test_files", "acetic_acid.xyz")) mol1 = Molecule.from_file( os.path.join(PACKAGE_PATH, "test_files", "acetic_acid.xyz")) mol2 = Molecule(["Pb"], [[0, 0, 0]]) mols = [mol0, mol1, mol2] # center of mass distances in angstrom # example: 3 molecules and cm_dist = [4,2], # center of mass of mol1 is moved from mol0 in 1,0,0 direction by 4 A # mol2 is moved from the center of mass of the combined mol0+mol1 molecule by 2 A # in a direction that is perpendicular to the first moving direction and the # molecule vector of one of the molecules # for n molecules the size of cm_dist must be n-1 cm_dist = [1, 2] # optional parmater # example: angle={'0':{}, '1':{'0':90}, '2':{} }
def test_strange_inchi(self): mm = MoleculeMatcher(tolerance=0.05, mapper=InchiMolAtomMapper()) mol1 = Molecule.from_file(os.path.join(test_dir, "k1.sdf")) mol2 = Molecule.from_file(os.path.join(test_dir, "k2.sdf")) self.assertTrue(mm.fit(mol1, mol2))
def setUp(self): cyclohexene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/cyclohexene.xyz")) self.cyclohexene = MoleculeGraph.with_empty_graph(cyclohexene, edge_weight_name="strength", edge_weight_units="") self.cyclohexene.add_edge(0, 1, weight=1.0) self.cyclohexene.add_edge(1, 2, weight=1.0) self.cyclohexene.add_edge(2, 3, weight=2.0) self.cyclohexene.add_edge(3, 4, weight=1.0) self.cyclohexene.add_edge(4, 5, weight=1.0) self.cyclohexene.add_edge(5, 0, weight=1.0) self.cyclohexene.add_edge(0, 6, weight=1.0) self.cyclohexene.add_edge(0, 7, weight=1.0) self.cyclohexene.add_edge(1, 8, weight=1.0) self.cyclohexene.add_edge(1, 9, weight=1.0) self.cyclohexene.add_edge(2, 10, weight=1.0) self.cyclohexene.add_edge(3, 11, weight=1.0) self.cyclohexene.add_edge(4, 12, weight=1.0) self.cyclohexene.add_edge(4, 13, weight=1.0) self.cyclohexene.add_edge(5, 14, weight=1.0) self.cyclohexene.add_edge(5, 15, weight=1.0) butadiene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/butadiene.xyz")) self.butadiene = MoleculeGraph.with_empty_graph(butadiene, edge_weight_name="strength", edge_weight_units="") self.butadiene.add_edge(0, 1, weight=2.0) self.butadiene.add_edge(1, 2, weight=1.0) self.butadiene.add_edge(2, 3, weight=2.0) self.butadiene.add_edge(0, 4, weight=1.0) self.butadiene.add_edge(0, 5, weight=1.0) self.butadiene.add_edge(1, 6, weight=1.0) self.butadiene.add_edge(2, 7, weight=1.0) self.butadiene.add_edge(3, 8, weight=1.0) self.butadiene.add_edge(3, 9, weight=1.0) ethylene = Molecule.from_file(os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files/graphs/ethylene.xyz")) self.ethylene = MoleculeGraph.with_empty_graph(ethylene, edge_weight_name="strength", edge_weight_units="") self.ethylene.add_edge(0, 1, weight=2.0) self.ethylene.add_edge(0, 2, weight=1.0) self.ethylene.add_edge(0, 3, weight=1.0) self.ethylene.add_edge(1, 4, weight=1.0) self.ethylene.add_edge(1, 5, weight=1.0) self.pc = Molecule.from_file( os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "PC.xyz")) self.pc_edges = [[5, 10], [5, 12], [5, 11], [5, 3], [3, 7], [3, 4], [3, 0], [4, 8], [4, 9], [4, 1], [6, 1], [6, 0], [6, 2]] self.pc_frag1 = Molecule.from_file( os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "PC_frag1.xyz")) self.pc_frag1_edges = [[0, 2], [4, 2], [2, 1], [1, 3]] self.tfsi = Molecule.from_file( os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "TFSI.xyz")) self.tfsi_edges = [14, 1], [1, 4], [1, 5], [1, 7], [7, 11], [7, 12], [7, 13], [ 14, 0], [0, 2], [0, 3], [0, 6], [6, 8], [6, 9], [6, 10] warnings.simplefilter("ignore")
def test_cdi_23(self): mm = MoleculeMatcher(tolerance=0.05, mapper=InchiMolAtomMapper()) mol1 = Molecule.from_file(os.path.join(test_dir, "cdi_23_1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "cdi_23_2.xyz")) self.assertFalse(mm.fit(mol1, mol2))
def test_thiane_ethynyl(self): mm = MoleculeMatcher(tolerance=0.05, mapper=InchiMolAtomMapper()) mol1 = Molecule.from_file(os.path.join(test_dir, "thiane_ethynyl1.sdf")) mol2 = Molecule.from_file(os.path.join(test_dir, "thiane_ethynyl2.sdf")) self.assertFalse(mm.fit(mol1, mol2))
def test_get_rmsd(self): mm = MoleculeMatcher() mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t4.xyz")) self.assertEqual('{0:7.3}'.format(mm.get_rmsd(mol1, mol2)), "0.00488")
d["@class"] = self.__class__.__name__ d['name'] = self.composition.formula return d def copy(self): return Structure.from_sites(self) # test if __name__ == '__main__': # the following example require: # acetic_acid.xyz and POSCAR.mp-21276_PbS # create lead acetate ligand # from 3 molecules: 2 acetic acid + 1 Pb mol0 = Molecule.from_file("acetic_acid.xyz") mol1 = Molecule.from_file("acetic_acid.xyz") mol2 = Molecule(["Pb"], [[0, 0, 0]]) mols = [mol0, mol1, mol2] # center of mass distances in angstrom # example: 3 molecules and cm_dist = [4,2], # center of mass of mol1 is moved from mol0 in 1,0,0 direction by 4 A # mol2 is moved from the center of mass of the combined mol0+mol1 molecule by 2 A # in a direction that is perpendicular to the first moving direction and the # molecule vector of one of the molecules # for n molecules the size of cm_dist must be n-1 cm_dist = [1, 2] # optional parmater # example: angle={'0':{}, '1':{'0':90}, '2':{} } # rotate mol1 with respect to mol0 by 90 degreeen around and axis that is normal
def setUpClass(cls): polymer = Molecule.from_file(os.path.join(test_dir, "polymer.xyz")) cls.topology = Topology.from_molecule(polymer, tol=0.1) cls.forcefield = ForceField.from_file(os.path.join(test_dir, "ff_data.yaml"))
def __init__(self, molecule, optimize=False): """ Instantiation method for FunctionalGroupExtractor. :param molecule: Either a filename, a pymatgen.core.structure.Molecule object, or a pymatgen.analysis.graphs.MoleculeGraph object. :param optimize: Default False. If True, then the input molecule will be modified, adding Hydrogens, performing a simple conformer search, etc. """ self.molgraph = None if isinstance(molecule, str): try: if optimize: obmol = BabelMolAdaptor.from_file(molecule, file_format="mol") # OBMolecule does not contain pymatgen Molecule information # So, we need to wrap the obmol in a BabelMolAdapter obmol.add_hydrogen() obmol.make3d() obmol.localopt() self.molecule = obmol.pymatgen_mol else: self.molecule = Molecule.from_file(molecule) except OSError: raise ValueError("Input must be a valid molecule file, a " "Molecule object, or a MoleculeGraph object.") elif isinstance(molecule, Molecule): if optimize: obmol = BabelMolAdaptor(molecule) obmol.add_hydrogen() obmol.make3d() obmol.localopt() self.molecule = obmol.pymatgen_mol else: self.molecule = molecule elif isinstance(molecule, MoleculeGraph): if optimize: obmol = BabelMolAdaptor(molecule.molecule) obmol.add_hydrogen() obmol.make3d() obmol.localopt() self.molecule = obmol.pymatgen_mol else: self.molecule = molecule.molecule self.molgraph = molecule else: raise ValueError("Input to FunctionalGroupExtractor must be" "str, Molecule, or MoleculeGraph.") if self.molgraph is None: self.molgraph = MoleculeGraph.with_local_env_strategy(self.molecule, OpenBabelNN(), reorder=False, extend_structure=False) # Assign a specie and coordinates to each node in the graph, # corresponding to the Site in the Molecule object self.molgraph.set_node_attributes() self.species = nx.get_node_attributes(self.molgraph.graph, "specie")
def fit_with_mapper(self, mapper): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] mol1 = Molecule(["C", "H", "H", "H", "H"], coords) op = SymmOp.from_origin_axis_angle([0, 0, 0], [0.1, 0.2, 0.3], 60) rotcoords = [op.operate(c) for c in coords] mol2 = Molecule(["C", "H", "H", "H", "H"], rotcoords) mm = MoleculeMatcher(mapper=mapper) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "benzene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "benzene2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "benzene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t2.xyz")) self.assertFalse(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "c1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "c2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t4.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "j1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "j2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "ethene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "ethene2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "toluene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "toluene2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "cyclohexane1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "cyclohexane2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mol1 = Molecule.from_file(os.path.join(test_dir, "oxygen1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "oxygen2.xyz")) self.assertTrue(mm.fit(mol1, mol2)) mm = MoleculeMatcher(tolerance=0.001, mapper=mapper) mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t4.xyz")) self.assertFalse(mm.fit(mol1, mol2))