def test_from_file(self): filepath = os.path.join(test_dir, 'MethylPyrrolidine_drawn.gjf') gau = GaussianInput.from_file(filepath) self.assertEqual(gau.molecule.composition.formula, "H11 C5 N1") self.assertIn("opt", gau.route_parameters) self.assertEqual(gau.route_parameters["geom"], "connectivity") self.assertEqual(gau.functional, "b3lyp") self.assertEqual(gau.basis_set, "6-311+g(d,p)") filepath = os.path.join(test_dir, "g305_hb.txt") with open(filepath) as f: txt = f.read() toks = txt.split("--link1--") for i, t in enumerate(toks): lines = t.strip().split("\n") lines = [l.strip() for l in lines] gau = GaussianInput.from_string("\n".join(lines)) self.assertIsNotNone(gau.molecule) if i == 0: mol = gau.molecule ans = """Molecule Summary (H4 O2) Reduced Formula: H2O Sites (6) 1 O 0.000000 0.000000 0.000000 2 O 0.000000 0.000000 2.912902 3 H 0.892596 0.000000 -0.373266 4 H 0.143970 0.000219 0.964351 5 H -0.582554 0.765401 3.042783 6 H -0.580711 -0.766761 3.043012""" self.assertEqual(str(mol), ans)
def test_from_file(self): filepath = os.path.join(test_dir, 'MethylPyrrolidine_drawn.gjf') gau = GaussianInput.from_file(filepath) self.assertEqual(gau.molecule.composition.formula, "H11 C5 N1") self.assertIn("opt", gau.route_parameters) self.assertEqual(gau.route_parameters["geom"], "connectivity") self.assertEqual(gau.functional, "b3lyp") self.assertEqual(gau.basis_set, "6-311+g(d,p)") filepath = os.path.join(test_dir, "g305_hb.txt") with open(filepath) as f: txt = f.read() toks = txt.split("--link1--") for i, t in enumerate(toks): lines = t.strip().split("\n") lines = [l.strip() for l in lines] gau = GaussianInput.from_string("\n".join(lines)) self.assertIsNotNone(gau.molecule) if i == 0: mol = gau.molecule ans = """Molecule Summary (H4 O2) Reduced Formula: H2O Charge = 0, Spin Mult = 1 Sites (6) 1 O 0.000000 0.000000 0.000000 2 O 0.000000 0.000000 2.912902 3 H 0.892596 0.000000 -0.373266 4 H 0.143970 0.000219 0.964351 5 H -0.582554 0.765401 3.042783 6 H -0.580711 -0.766761 3.043012""" self.assertEqual(str(mol), ans)
def test_from_file(self): filepath = os.path.join(test_dir, "MethylPyrrolidine_drawn.gjf") gau = GaussianInput.from_file(filepath) self.assertEqual(gau.molecule.composition.formula, "H11 C5 N1") self.assertIn("opt", gau.route_parameters) self.assertEqual(gau.route_parameters["geom"], "connectivity") self.assertEqual(gau.functional, "b3lyp") self.assertEqual(gau.basis_set, "6-311+g(d,p)")
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!")