def mol2inchi(s): openbabel.obErrorLog.SetOutputLevel(-1) conv = openbabel.OBConversion() conv.SetInAndOutFormats('mol', 'inchi') conv.AddOption("F", conv.OUTOPTIONS) conv.AddOption("T", conv.OUTOPTIONS) conv.AddOption("x", conv.OUTOPTIONS, "noiso") conv.AddOption("w", conv.OUTOPTIONS) obmol = openbabel.OBMol() if not conv.ReadString(obmol, str(s)): return None inchi = conv.WriteString(obmol, True) # second argument is trimWhitespace if inchi == '': return None else: return inchi
def testSpecifyBonds(self): mol = pybel.readstring("smi", "ICBr") bv = self.createBitVec(4, (1, 2, 3)) bondbv = self.createBitVec(2, (1, )) ans = ["I[CH2].[Br]", "IC.Br", "IC*.Br*"] ans_atomorder = [[1, 2, 3], [1, 2, 3], [1, 2, 3, 3, 2]] ans_bondorder = [[0], [0], [0, 1, 1]] for option in range(3): nmol = ob.OBMol() atomorder = ob.vectorUnsignedInt() bondorder = ob.vectorUnsignedInt() ok = mol.OBMol.CopySubstructure(nmol, bv, bondbv, option, atomorder, bondorder) self.assertTrue(ok) self.assertEqual( pybel.Molecule(nmol).write("smi").rstrip(), ans[option]) self.assertEqual(ans_atomorder[option], list(atomorder)) self.assertEqual(ans_bondorder[option], list(bondorder))
def calculate(self, mol): temp_file = 'temp.mol' mol.write(temp_file) try: obConversion = openbabel.OBConversion() obConversion.SetInFormat("mol") OBMol = openbabel.OBMol() obConversion.ReadFile(OBMol, temp_file) OBMol.PerceiveBondOrders() finally: os.system('rm temp.mol') forcefield = openbabel.OBForceField.FindForceField(self._forcefield) outcome = forcefield.Setup(OBMol) if not outcome: raise ForceFieldSetupError( f"{self._forcefield} could not be setup for {mol}") yield forcefield.Energy()
def test_from_OBMol(configuration): """Test creating a structure from an OBMol object.""" obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("smi", "mdl") obConversion.AddOption("3") mol = openbabel.OBMol() obConversion.ReadString(mol, "C=CO") # Add hydrogens mol.AddHydrogens() # Get coordinates for a 3-D structure builder = openbabel.OBBuilder() builder.Build(mol) configuration.from_OBMol(mol) assert configuration.n_atoms == 7 assert configuration.bonds.bondorders == [2, 1, 1, 1, 1, 1]
def pybel_opt(smile, steps): obconversion = ob.OBConversion() obconversion.SetInFormat('smi') obmol = ob.OBMol() obconversion.ReadString(obmol, smile) pbmol = pb.Molecule(obmol) pbmol.make3D(forcefield="uff", steps=50) pbmol.localopt(forcefield="gaff", steps=200) pbmol.localopt(forcefield="mmff94", steps=100) f_f = pb._forcefields["uff"] f_f.Setup(pbmol.OBMol) f_f.ConjugateGradients(steps, 1.0e-9) f_f.GetCoordinates(pbmol.OBMol) #print(f_f.Energy(), f_f.GetUnit()) return pybel2ase(pbmol)
def _crd2frag(symbols, crds, pbc=False, cell=None, return_bonds=False): atomnumber = len(symbols) all_atoms = Atoms(symbols=symbols, positions=crds, pbc=pbc, cell=cell) if pbc: repeated_atoms = all_atoms.repeat(2)[atomnumber:] tree = cKDTree(crds) d = tree.query(repeated_atoms.get_positions(), k=1)[0] nearest = d < 5 ghost_atoms = repeated_atoms[nearest] realnumber = np.where(nearest)[0] % atomnumber all_atoms += ghost_atoms # Use openbabel to connect atoms mol = openbabel.OBMol() mol.BeginModify() for idx, (num, position) in enumerate( zip(all_atoms.get_atomic_numbers(), all_atoms.positions)): atom = mol.NewAtom(idx) atom.SetAtomicNum(int(num)) atom.SetVector(*position) mol.ConnectTheDots() mol.PerceiveBondOrders() mol.EndModify() bonds = [] for ii in range(mol.NumBonds()): bond = mol.GetBond(ii) a = bond.GetBeginAtom().GetId() b = bond.GetEndAtom().GetId() bo = bond.GetBO() if a >= atomnumber and b >= atomnumber: # duplicated continue elif a >= atomnumber: a = realnumber[a - atomnumber] elif b >= atomnumber: b = realnumber[b - atomnumber] bonds.extend([[a, b, bo], [b, a, bo]]) bonds = np.array(bonds, ndmin=2).reshape((-1, 3)) graph = csr_matrix((bonds[:, 2], (bonds[:, 0], bonds[:, 1])), shape=(atomnumber, atomnumber)) frag_numb, frag_index = connected_components(graph, 0) if return_bonds: return frag_numb, frag_index, graph return frag_numb, frag_index
def testResidueCopying(self): smi = "C[C@@H](C(=O)N[C@@H](CS)C(=O)O)N" # H-Ala-Cys-OH mol = pybel.readstring("smi", smi).OBMol ob.cvar.chainsparser.PerceiveChains(mol) mol.SetChainsPerceived() residues = list(ob.OBResidueIter(mol)) self.assertEqual(len(residues), 2) # Copy just the Cys N bv = self.createBitVec(mol.NumAtoms() + 1, (5, )) nmol = ob.OBMol() mol.CopySubstructure(nmol, bv) self.assertEqual(len(list(ob.OBResidueIter(nmol))), 1) pdb = pybel.Molecule(nmol).write("pdb") atoms = [line for line in pdb.split("\n") if line.startswith("ATOM")] self.assertEqual(len(atoms), 1) cysN = "ATOM 1 N CYS A 2" self.assertTrue(atoms[0].startswith(cysN)) # Copy the Cys N and Ca bv = self.createBitVec(mol.NumAtoms() + 1, (5, 6)) nmol.Clear() mol.CopySubstructure(nmol, bv) self.assertEqual(len(list(ob.OBResidueIter(nmol))), 1) pdb = pybel.Molecule(nmol).write("pdb") atoms = [line for line in pdb.split("\n") if line.startswith("ATOM")] self.assertEqual(len(atoms), 2) self.assertTrue(atoms[0].startswith(cysN)) cysCa = "ATOM 2 CA CYS A 2" self.assertTrue(atoms[1].startswith(cysCa)) # Copy the Ala C and Cys N bv = self.createBitVec(mol.NumAtoms() + 1, (3, 5)) nmol.Clear() mol.CopySubstructure(nmol, bv) self.assertEqual(len(list(ob.OBResidueIter(nmol))), 2) pdb = pybel.Molecule(nmol).write("pdb") atoms = [line for line in pdb.split("\n") if line.startswith("ATOM")] self.assertEqual(len(atoms), 2) alaC = "ATOM 1 C ALA A 1" self.assertTrue(atoms[0].startswith(alaC)) cysN = "ATOM 2 N CYS A 2" self.assertTrue(atoms[1].startswith(cysN))
def read_mol(self, mol_src, pdb=False): mol_file = os.path.join(self.root_dir, mol_src) if self.verbose: print('Reading ' + mol_file) assert os.path.isfile(mol_file), 'file does not exist' mol = ob.OBMol() if pdb: assert self.read_pdb(mol, mol_file), 'failed to read mol' else: assert self.read_sdf(mol, mol_file), 'failed to read mol' mol.AddHydrogens() assert mol.NumAtoms() > 0, 'mol has zero atoms' mol.SetTitle(mol_src) return mol
def rd_mol_to_ob_mol(rd_mol): ''' Convert an RWMol to an OBMol, copying over the elements, coordinates, formal charges, bonds and aromaticity. ''' ob_mol = ob.OBMol() ob_mol.BeginModify() rd_conf = rd_mol.GetConformer(0) for idx, rd_atom in enumerate(rd_mol.GetAtoms()): ob_atom = ob_mol.NewAtom() ob_atom.SetAtomicNum(rd_atom.GetAtomicNum()) ob_atom.SetFormalCharge(rd_atom.GetFormalCharge()) ob_atom.SetAromatic(rd_atom.GetIsAromatic()) ob_atom.SetImplicitHCount(rd_atom.GetNumExplicitHs()) rd_coords = rd_conf.GetAtomPosition(idx) ob_atom.SetVector(rd_coords.x, rd_coords.y, rd_coords.z) for rd_bond in rd_mol.GetBonds(): # OB uses 1-indexing, rdkit uses 0 i = rd_bond.GetBeginAtomIdx() + 1 j = rd_bond.GetEndAtomIdx() + 1 bond_type = rd_bond.GetBondType() if bond_type == Chem.BondType.SINGLE: bond_order = 1 elif bond_type == Chem.BondType.DOUBLE: bond_order = 2 elif bond_type == Chem.BondType.TRIPLE: bond_order = 3 else: raise Exception('unknown bond type {}'.format(bond_type)) ob_mol.AddBond(i, j, bond_order) ob_bond = ob_mol.GetBond(i, j) ob_bond.SetAromatic(rd_bond.GetIsAromatic()) ob_mol.EndModify() return ob_mol
def makeopenbabel(atomcoords, atomnos, charge=0, mult=1): """Create an Open Babel molecule.""" _check_openbabel(_found_openbabel) obmol = ob.OBMol() for i in range(len(atomnos)): # Note that list(atomcoords[i]) is not equivalent!!! # For now, only take the last geometry. # TODO: option to export last geometry or all geometries? coords = atomcoords[-1][i].tolist() atomno = int(atomnos[i]) obatom = ob.OBAtom() obatom.SetAtomicNum(atomno) obatom.SetVector(*coords) obmol.AddAtom(obatom) obmol.ConnectTheDots() obmol.PerceiveBondOrders() obmol.SetTotalSpinMultiplicity(mult) obmol.SetTotalCharge(int(charge)) return obmol
def GetResidueMolecule(protein, residueNumber): """ Gets the OBMol object corresponding to a residue number on ACE2 protein or Spike protein protein: 'ace2' or 'spike' residueNumber: The residue Number returns: The OBMol object representing the molecule """ resMol = ob.OBMol() for res in ob.OBResidueIter(protein): if res.GetNum() == residueNumber: for atom in ob.OBResidueAtomIter(res): resMol.AddAtom(atom) break return resMol
def to_ob_mol(mol, return_mapping=False): """ Convert a molecular structure to an OpenBabel OBMol object. Uses `OpenBabel <http://openbabel.org/>`_ to perform the conversion. """ if openbabel is None: raise DependencyError('OpenBabel is not installed. Please install or use RDKit.') # Sort the atoms to ensure consistent output mol.sort_atoms() atoms = mol.vertices ob_atom_ids = {} # dictionary of OB atom IDs obmol = openbabel.OBMol() for atom in atoms: a = obmol.NewAtom() if atom.element.symbol == 'X': a.SetAtomicNum(78) # not sure how to do this with linear scaling when this might not be Pt else: a.SetAtomicNum(atom.number) if atom.element.isotope != -1: a.SetIsotope(atom.element.isotope) a.SetFormalCharge(atom.charge) # a.SetImplicitHCount(0) # the default is 0 ob_atom_ids[atom] = a.GetId() orders = {1: 1, 2: 2, 3: 3, 4: 4, 1.5: 5} for atom1 in mol.vertices: for atom2, bond in atom1.edges.items(): if bond.is_hydrogen_bond(): continue index1 = atoms.index(atom1) index2 = atoms.index(atom2) if index1 < index2: order = orders[bond.order] obmol.AddBond(index1 + 1, index2 + 1, order) obmol.AssignSpinMultiplicity(True) if return_mapping: return obmol, ob_atom_ids return obmol
def to_obmol(self, explicit_hydrogen: Optional[bool] = True) -> ob.OBMol: obmol = ob.OBMol() for a in self.atoms: obatom = ob.OBAtom() obatom.SetAtomicNum(a.Z) obatom.SetVector(*a.position.squeeze()) obmol.AddAtom(obatom) obmol.ConnectTheDots() obmol.PerceiveBondOrders() if not explicit_hydrogen: obmol.DeleteHydrogens() # NOTE: empirically, this appears to renumber the atoms correctly # (i.e. it maintains the original order as specified by self.atoms while # removing hydrogens & renumbering sequentially) # this is consistent with how to_graph works return obmol
def atoms_to_pybel(aseatoms, infer_bonds=True): ''' Convert ASE Atoms isntance into the json format compatible with Args: aseatoms : ase.Atoms Instance of Atoms from ase package infer_bonds : bool If `True` bonds will be inferred using openbabel Returns: mol : dist A dictionary with the json format of the molecule ''' from openbabel import pybel from openbabel import openbabel as ob obmol = ob.OBMol() obmol.BeginModify() for atom in aseatoms: obatom = obmol.NewAtom() obatom.SetAtomicNum(int(atom.number)) obatom.SetVector(*atom.position.tolist()) # If there is no bond data, try to infer them if infer_bonds: obmol.ConnectTheDots() obmol.PerceiveBondOrders() # Check for unit cell data if any(aseatoms.pbc): uc = ob.OBUnitCell() uc.SetData(*(ob.vector3(*v) for v in aseatoms.get_cell())) uc.SetSpaceGroup('P1') obmol.CloneData(uc) obmol.EndModify() mol = pybel.Molecule(obmol) return mol
def convert_by_obabel(mol, cache_dir=os.path.join(os.getcwd(), '.cache'), obabel_path="obabel"): if not os.path.exists(cache_dir): os.mkdir(cache_dir) if mol.HasProp("_Name"): name = mol.GetProp("_Name") else: name = f"mol{int(time.time())}" mol_file_in = os.path.join(cache_dir, f'{name}.mol') mol_file_out = os.path.join(cache_dir, f'{name}_obabel.mol') Chem.MolToMolFile(mol, mol_file_in, kekulize=False) obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("mol", "mol") mol = openbabel.OBMol() obConversion.ReadFile(mol, mol_file_in) obConversion.WriteFile(mol, mol_file_out) mol_obabel = Chem.MolFromMolFile(mol_file_out, removeHs=False, sanitize=False) return mol_obabel
def canonicalize(smiles, engine="openbabel"): """Standardize SMILES strings into canonical SMILES strings through the specified engine. (Important in optimizing prediction results.) Input: Output: """ if engine == "openbabel": obconversion = openbabel.OBConversion() obconversion.SetInAndOutFormats("smi", "can") obmol = openbabel.OBMol() obconversion.ReadString(obmol, smiles) outMDL = obconversion.WriteString(obmol) can = outMDL.rstrip() elif engine == "rdkit": mol = Chem.MolFromSmiles(smiles) # , sanitize=True) can = Chem.MolToSmiles(mol) else: raise AttributeError("Engine must be either 'openbabel' or 'rdkit'.") return can
def get_framework(fname,rep): #Define openbabel file conversion type. "from" and "to" obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("cif", "CONTCAR") #Define openbabel molecule type object. mol = openbabel.OBMol() obConversion.ReadFile(mol, fname) #Convert and write obConversion.WriteFile(mol, 'CONTCAR_frame') #Read using ASE, generate atoms type object. frmwrk_atoms = vasp.read_vasp('CONTCAR_frame') #Repeat atoms. if len(rep) > 0: frmwrk_rep = frmwrk_atoms.repeat(rep) return frmwrk_rep
def _pybel_opt(self, smile, steps): """Optimize a molecule using force field and pybel (needed for complex SMILES).""" obconversion = ob.OBConversion() obconversion.SetInFormat("smi") obmol = ob.OBMol() obconversion.ReadString(obmol, smile) pbmol = pb.Molecule(obmol) pbmol.make3D(forcefield="uff", steps=50) pbmol.localopt(forcefield="gaff", steps=200) pbmol.localopt(forcefield="mmff94", steps=100) f_f = pb._forcefields["uff"] # pylint: disable=protected-access f_f.Setup(pbmol.OBMol) f_f.ConjugateGradients(steps, 1.0e-9) f_f.GetCoordinates(pbmol.OBMol) species = [chemical_symbols[atm.atomicnum] for atm in pbmol.atoms] positions = np.asarray([atm.coords for atm in pbmol.atoms]) return self.make_ase(species, positions)
def check_format(path): """Check if a file is file of SMILES strings. Parameters ---------- path : str or Path """ if isinstance(path, str): path = Path(path) path.expanduser().resolve() obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("smi", "mol") obMol = openbabel.OBMol() result = True try: obConversion.ReadFile(obMol, str(path)) except BaseException(): result = False return result
def get_mrlogP_descriptors(self, smiles: str, moltitle: str): # Descriptors are morgan, fp4, then USRCAT mrlogP_descriptor_length = 128 + 128 + 60 obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("smi", "mdl") ob_mol = openbabel.OBMol() # Create RDKit and OpenBabel molecules rdkit_mol = Chem.AddHs(smiles_to_3dmol(smiles, "querymol")) obConversion.ReadString(ob_mol, smiles) # Generate Morgan/ECFP4 morgan_fingerprint = AllChem.GetMorganFingerprintAsBitVect( Chem.RemoveHs(rdkit_mol), 2, 128).ToBitString() # Generate USRCAT usrcat_descriptors = GetUSRCAT(rdkit_mol) # Generate FP4 fp4fp = openbabel.vectorUnsignedInt() fingerprinter = openbabel.OBFingerprint.FindFingerprint("FP4") fingerprinter.GetFingerprint(ob_mol, fp4fp) openbabel.OBFingerprint.Fold(fingerprinter, fp4fp, 128) logP_descriptors = np.full((mrlogP_descriptor_length), np.nan) for i, v in enumerate(morgan_fingerprint): logP_descriptors[i] = float(v) fp4_p1 = [float(x) for x in list(format(fp4fp[0], '032b'))] fp4_p2 = [float(x) for x in list(format(fp4fp[1], '032b'))] fp4_p3 = [float(x) for x in list(format(fp4fp[2], '032b'))] fp4_p4 = [float(x) for x in list(format(fp4fp[3], '032b'))] logP_descriptors[128:256] = fp4_p1 + fp4_p2 + fp4_p3 + fp4_p4 for i, v in enumerate(usrcat_descriptors): logP_descriptors[256 + i] = float(v) return logP_descriptors
def from_smiles(self, smiles, name=None): """Create the system from a SMILES string. Parameters ---------- smiles : str The SMILES string name : str = None The name of the molecule Returns ------- None """ save = self.name obConversion = openbabel.OBConversion() obConversion.SetInAndOutFormats("smi", "mdl") obConversion.AddOption("3") mol = openbabel.OBMol() obConversion.ReadString(mol, smiles) # Add hydrogens mol.AddHydrogens() # Get coordinates for a 3-D structure builder = openbabel.OBBuilder() builder.Build(mol) # molfile = obConversion.WriteString(mol) # self.from_molfile_text(molfile) self.from_OBMol(mol) if name is not None: self.name = name else: self.name = save
def __init__(self, mol): """ Initializes with pymatgen Molecule or OpenBabel"s OBMol. Args: mol: pymatgen's Molecule/IMolecule or OpenBabel OBMol """ if isinstance(mol, IMolecule): if not mol.is_ordered: raise ValueError( "OpenBabel Molecule only supports ordered molecules.") # For some reason, manually adding atoms does not seem to create # the correct OBMol representation to do things like force field # optimization. So we go through the indirect route of creating # an XYZ file and reading in that file. obmol = ob.OBMol() obmol.BeginModify() for site in mol: coords = list(site.coords) atomno = site.specie.Z obatom = ob.OBAtom() obatom.thisown = 0 obatom.SetAtomicNum(atomno) obatom.SetVector(*coords) obmol.AddAtom(obatom) del obatom obmol.ConnectTheDots() obmol.PerceiveBondOrders() obmol.SetTotalSpinMultiplicity(mol.spin_multiplicity) obmol.SetTotalCharge(int(mol.charge)) obmol.Center() obmol.EndModify() self._obmol = obmol elif isinstance(mol, ob.OBMol): self._obmol = mol elif isinstance(mol, pb.Molecule): self._obmol = mol.OBMol
def make_ob_mol(coords, types, bonds, typer): ''' Create an OBMol from numpy arrays of coords and types, optional bonds, and an AtomTyper. No atomic properties other than the elements and coordinates are set. Also returns a list of the created atoms in the same order as struct, which is needed for other methods that add hydrogens and change the indexing of atoms in the molecule. ''' ob_mol = ob.OBMol() ob_mol.BeginModify() atoms = [] for coord, type_vec in zip(coords, types): atom = ob_mol.NewAtom() x, y, z = [float(c) for c in coord] atom.SetVector(x, y, z) atom_type = typer.get_atom_type(type_vec) atom.SetAtomicNum(atom_type.atomic_num) atoms.append(atom) n_atoms = len(atoms) if bonds is not None: for i, atom_i in enumerate(atoms): for j, atom_j in enumerate(atoms): if bonds[i,j]: ob_mol.AddBond( atom_i.GetIdx(), atom_j.GetIdx(), 1, 0 ) ob_mol.EndModify() return ob_mol, atoms
def rd_mol_to_ob_mol(rd_mol): """ liGAN conversion between RDKit and Open Babel https://github.com/mattragoza/liGAN """ ob_mol = ob.OBMol() rd_conf = rd_mol.GetConformer(0) for i, rd_atom in enumerate(rd_mol.GetAtoms()): atomic_num = rd_atom.GetAtomicNum() rd_coords = rd_conf.GetAtomPosition(i) x = rd_coords.x y = rd_coords.y z = rd_coords.z ob_atom = ob_mol.NewAtom() ob_atom.SetAtomicNum(atomic_num) ob_atom.SetAromatic(rd_atom.GetIsAromatic()) ob_atom.SetVector(x, y, z) for k, rd_bond in enumerate(rd_mol.GetBonds()): i = rd_bond.GetBeginAtomIdx() + 1 j = rd_bond.GetEndAtomIdx() + 1 bond_type = rd_bond.GetBondType() bond_flags = 0 if bond_type == Chem.BondType.AROMATIC: bond_order = 1 bond_flags |= ob.OB_AROMATIC_BOND elif bond_type == Chem.BondType.SINGLE: bond_order = 1 elif bond_type == Chem.BondType.DOUBLE: bond_order = 2 elif bond_type == Chem.BondType.TRIPLE: bond_order = 3 ob_mol.AddBond(i, j, bond_order, bond_flags) return ob_mol
def test_building_a_molecule(self): mol = ob.OBMol() C = add_atom(mol, 6) N = add_atom(mol, 7) # XXX Why can't I do mol.AddBond(C, N, 3)? mol.AddBond(C.GetIdx(), N.GetIdx(), 3) self.assertEqual(C.ImplicitHydrogenCount(), 1) C.IncrementImplicitValence( ) # Is this how to increment the implicit hcount? self.assertEqual(C.ImplicitHydrogenCount(), 2) conv = ob.OBConversion() conv.SetOutFormat("can") s = conv.WriteString(mol).strip() # XXX How does this work when the ImplicitHydrogenCount is 2?? XXX self.assertEqual(s, "C#N") # I can even add an atom this way. (Why are there 2 ways?) O = ob.OBAtom() O.SetAtomicNum(8) mol.AddAtom(O) O.SetImplicitValence(2) s = conv.WriteString(mol).strip() self.assertEqual(s, "C#N.O")
def openbabelConvert(input_file, input_format, output_format): """ Converts the file from the input format to the output format specified. It uses the openbabel features Parameters ---------- input_file: str The path of the input file to convert input_format: str The input file format output_format: str The output file format Returns ------- outfile: str The output file generated """ from openbabel import openbabel import tempfile input_format = input_format[1:] if input_format.startswith( ".") else input_format file = tempfile.NamedTemporaryFile(delete=True, suffix="." + output_format) file.close() outfile = file.name _ = openbabel.OBConversion() _.SetInAndOutFormats(input_format, output_format) _mol = openbabel.OBMol() _.ReadFile(_mol, input_file) _.WriteFile(_mol, outfile) return outfile
def mol_converter(mol_input, input_type, output_type): """Convert molecular representations using openbabel. Convert for instance from smiles to inchi or inchi to inchikey. Args: ---- mol_input: str Input data, e.g. inchi or smiles. input_type: str Define input type (as named in openbabel). E.g. "smi"for smiles and "inchi" for inchi. output_type: str Define input type (as named in openbabel). E.g. "smi"for smiles and "inchi" for inchi. """ conv = ob.OBConversion() conv.SetInAndOutFormats(input_type, output_type) mol = ob.OBMol() if conv.ReadString(mol, mol_input): mol_output = conv.WriteString(mol) else: print("Error when converting", mol_input) mol_output = None return mol_output
def add_structures(self, path_structures: str): """ Add a new structure (if path_structures is a file) or a set of structures (if path_structures is a folder) in the project_folder/Input/Sets/Set_name directory. Structures are converted to the pdb files using openbabel. The GAFF atomtype of each atom is detected using the atomtype program from AmberTools. This is required for the reindexing of atoms once a molecular forcefield is uploaded but has no impact in the forcefield used for simulations. Finally, for each structure, a Crystal object is created and stored in the Project object. A list of stored structures can be printed by typing "print(<project_name>.initial_crystals)". :param path_structures: Path to a structure file or a folder containg the structures. """ import os from openbabel import openbabel from PyPol.utilities import create, get_identifier from PyPol.crystals import Crystal import progressbar if not path_structures.endswith("/") and os.path.isdir(path_structures): path_structures += "/" available_file_formats_ob = ("abinit", "acesout", "acr", "adfband", "adfdftb", "adfout", "alc", "aoforce", "arc", "axsf", "bgf", "box", "bs", "c09out", "c3d1", "c3d2", "caccrt", "can", "car", "castep", "ccc", "cdjson", "cdx", "cdxml", "cif", "ck", "cml", "cmlr", "cof", "CONFIG", "CONTCAR", "CONTFF", "crk2d", "crk3d", "ct", "cub", "cube", "dallog", "dalmol", "dat", "dmol", "dx", "ent", "exyz", "fa", "fasta", "fch", "fchk", "fck", "feat", "fhiaims", "fract", "fs", "fsa", "g03", "g09", "g16", "g92", "g94", "g98", "gal", "gam", "gamess", "gamin", "gamout", "got", "gpr", "gro", "gukin", "gukout", "gzmat", "hin", "HISTORY", "inchi", "inp", "ins", "jin", "jout", "log", "lpmd", "mcdl", "mcif", "MDFF", "mdl", "ml2", "mmcif", "mmd", "mmod", "mol", "mol2", "mold", "molden", "molf", "moo", "mop", "mopcrt", "mopin", "mopout", "mpc", "mpo", "mpqc", "mrv", "msi", "nwo", "orca", "out", "outmol", "output", "pc", "pcjson", "pcm", "pdb", "pdbqt", "png", "pos", "POSCAR", "POSFF", "pqr", "pqs", "prep", "pwscf", "qcout", "res", "rsmi", "rxn", "sd", "sdf", "siesta", "smi", "smiles", "smy", "sy2", "t41", "tdd", "text", "therm", "tmol", "txt", "txyz", "unixyz", "VASP", "vmol", "xml", "xsf", "xtc", "xyz", "yob") items = list() if os.path.isdir(path_structures): items = [f for f in os.listdir(path_structures) if os.path.isfile(path_structures + f)] elif os.path.isfile(path_structures): items = [os.path.basename(path_structures)] path_structures = os.path.dirname(path_structures) + "/" print(items, path_structures) else: print("No such file or directory") print("Importing structures from folder {}".format(path_structures)) bar = progressbar.ProgressBar(maxval=len(items)).start() nbar = 1 for item in items: id_name, extension = get_identifier(path_structures + item) path_id = self._path_input_structures + id_name path_structure_pdb = path_id + "/pc.pdb" path_structure_mol2 = path_id + "/pc.mol2" path_structure_ac = path_id + "/pc.ac" if extension in available_file_formats_ob: # Create folder in Input create(path_id, arg_type='dir', backup=True) os.chdir(path_id) os.system("cp {}{} {}".format(path_structures, item, path_id)) # Change file format to pdb ob_conversion = openbabel.OBConversion() ob_conversion.SetInAndOutFormats(extension, "pdb") mol = openbabel.OBMol() ob_conversion.ReadFile(mol, path_structures + item) ob_conversion.WriteFile(mol, path_structure_pdb) else: print("Ignore structure '{}': unknown file format".format(item)) continue # Convert structure to mol2 to identify atomtype ob_conversion = openbabel.OBConversion() ob_conversion.SetInAndOutFormats("pdb", "mol2") mol = openbabel.OBMol() ob_conversion.ReadFile(mol, path_structure_pdb) ob_conversion.WriteFile(mol, path_structure_mol2) os.system(self._atomtype + " -i " + path_structure_mol2 + " -f mol2 -p gaff -o " + path_structure_ac) new_crystal = Crystal._loadfrompdb(id_name, path_structure_pdb, include_atomtype=True) new_crystal._index = len(self._initial_crystals) new_crystal._save_pdb(path_structure_pdb) self._initial_crystals.append(new_crystal) bar.update(nbar) nbar += 1 bar.finish() print("=" * 100)
def clone(self): return Molecule(ob.OBMol(self.OBMol))
def testNonexistentAtom(self): mol = pybel.readstring("smi", "ICBr") bv = self.createBitVec(10, (9, )) nmol = ob.OBMol() ok = mol.OBMol.CopySubstructure(nmol, bv) self.assertFalse(ok)