def test_get_rmsd(self): mm = MoleculeMatcher() mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t3.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t4.xyz")).pymatgen_mol self.assertEqual('{0:7.3}'.format(mm.get_rmsd(mol1, mol2)), "0.00488")
def test_from_file(self): if not babelio.babel_loaded: raise SkipTest("OpenBabel not present. Skipping...") adaptor = BabelMolAdaptor.from_file(os.path.join(test_dir, "Ethane_e.pdb"), "pdb") mol = adaptor.pymatgen_mol self.assertEqual(mol.formula, "H6 C2")
def test_group_molecules(self): mm = MoleculeMatcher(tolerance=0.001) filename_list = None with open(os.path.join(test_dir, "mol_list.txt")) as f: filename_list = [line.strip() for line in f.readlines()] mol_list = [BabelMolAdaptor.from_file(os.path.join(test_dir, f)).pymatgen_mol\ 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] grouped_text = None 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 read_mol(filename): """ Reads a molecule based on file extension. For example, anything ending in a "xyz" is assumed to be a XYZ file. Supported formats include xyz, gaussian input (gjf|g03|g09|com|inp), Gaussian output (.out|and pymatgen's JSON serialized molecules. Using openbabel, many more extensions are supported but requires openbabel to be installed. Args: filename: A filename to read from. Returns: A Molecule object. """ fname = os.path.basename(filename) if fnmatch(fname.lower(), "*.xyz*"): return XYZ.from_file(filename).molecule elif any([ fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["gjf", "g03", "g09", "com", "inp"] ]): return GaussianInput.from_file(filename).molecule elif any([ fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["out", "lis", "log"] ]): return GaussianOutput(filename).final_structure elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"): with zopen(filename) as f: s = json.load(f, cls=PMGJSONDecoder) if type(s) != Molecule: raise IOError("File does not contain a valid serialized " "molecule") return s else: m = re.search("\.(pdb|mol|mdl|sdf|sd|ml2|sy2|mol2|cml|mrv)", filename.lower()) if m: return BabelMolAdaptor.from_file(filename, m.group(1)).pymatgen_mol raise ValueError("Unrecognized file extension!")
def read_mol(filename): """ Reads a molecule based on file extension. For example, anything ending in a "xyz" is assumed to be a XYZ file. Supported formats include xyz, gaussian input (gjf|g03|g09|com|inp), Gaussian output (.out|and pymatgen's JSON serialized molecules. Using openbabel, many more extensions are supported but requires openbabel to be installed. Args: filename: A filename to read from. Returns: A Molecule object. """ fname = os.path.basename(filename) if fnmatch(fname.lower(), "*.xyz*"): return XYZ.from_file(filename).molecule elif any([fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["gjf", "g03", "g09", "com", "inp"]]): return GaussianInput.from_file(filename).molecule elif any([fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["out", "lis", "log"]]): return GaussianOutput(filename).final_structure elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"): with zopen(filename) as f: s = json.load(f, cls=PMGJSONDecoder) if type(s) != Molecule: raise IOError("File does not contain a valid serialized " "molecule") return s else: m = re.search("\.(pdb|mol|mdl|sdf|sd|ml2|sy2|mol2|cml|mrv)", filename.lower()) if m: return BabelMolAdaptor.from_file(filename, m.group(1)).pymatgen_mol raise ValueError("Unrecognized file extension!")
def test_from_file(self): adaptor = BabelMolAdaptor.from_file( os.path.join(test_dir, "Ethane_e.pdb"), "pdb") mol = adaptor.pymatgen_mol self.assertEqual(mol.formula, "H6 C2")
def test_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 = BabelMolAdaptor.from_file(os.path.join(test_dir, "benzene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "benzene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "benzene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t2.xyz")).pymatgen_mol self.assertFalse(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "c1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "c2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t3.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t4.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "j1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "j2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "ethene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "ethene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "toluene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "toluene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "cyclohexane1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "cyclohexane2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "oxygen1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "oxygen2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mm = MoleculeMatcher(tolerance=0.001, mapper=mapper) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t3.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t4.xyz")).pymatgen_mol self.assertFalse(mm.fit(mol1, mol2))
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 = BabelMolAdaptor.from_file(os.path.join( test_dir, "benzene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join( test_dir, "benzene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join( test_dir, "benzene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t2.xyz")).pymatgen_mol self.assertFalse(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "c1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "c2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t3.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t4.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "j1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "j2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join( test_dir, "ethene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join( test_dir, "ethene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join( test_dir, "toluene1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join( test_dir, "toluene2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file( os.path.join(test_dir, "cyclohexane1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file( os.path.join(test_dir, "cyclohexane2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mol1 = BabelMolAdaptor.from_file(os.path.join( test_dir, "oxygen1.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join( test_dir, "oxygen2.xyz")).pymatgen_mol self.assertTrue(mm.fit(mol1, mol2)) mm = MoleculeMatcher(tolerance=0.001, mapper=mapper) mol1 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t3.xyz")).pymatgen_mol mol2 = BabelMolAdaptor.from_file(os.path.join(test_dir, "t4.xyz")).pymatgen_mol self.assertFalse(mm.fit(mol1, mol2))