def convert_babel_to_system(mol): """ Convert a BigDFT fragment to an open babel molecule. Args: mol (openbabel.OBMol): the molecule to convert. Returns: (BigDFT.Systems.System): bigdft system. """ from BigDFT.IO import read_mol2 from openbabel.openbabel import OBConversion # py2 workaround from sys import version_info if version_info[0] < 3: from io import BytesIO as StringIO else: try: from io import StringIO except ImportError: from StringIO import StringIO conv = OBConversion() conv.SetOutFormat("mol2") sval = StringIO(conv.WriteString(mol)) return read_mol2(sval)
def RestoreNumsSDF(f, fold, AuxInfo): #Read molecule from file obconversion = OBConversion() obconversion.SetInFormat("sdf") obmol = OBMol() obconversion.ReadFile(obmol, f) #Get the atoms Hs are connected to oldHcons = GetHcons(fold) #translate the H connected atoms to the new numbering system amap = GetInchiRenumMap(AuxInfo) for i in range(0, len(oldHcons)): oldHcons[i][1] = amap.index(oldHcons[i][1]) + 1 newHcons = [] temp = [] i = 0 for atom in OBMolAtomIter(obmol): idx = atom.GetIdx() anum = atom.GetAtomicNum() #If atom is hydrogen, check what it is connected to if anum == 1: for NbrAtom in OBAtomAtomIter(atom): newHcons.append([idx, NbrAtom.GetIdx()]) #Pick the temporary atom temp.append(atom) for i in range(0, len(newHcons)): conatom = newHcons[i][1] for b in range(0, len(oldHcons)): if conatom == oldHcons[b][1]: amap.append(oldHcons[b][0]) #remove the number, so that it doesn't get added twice oldHcons[b][1] = 0 newmol = OBMol() added = [] for i in range(1, len(amap) + 1): newn = amap.index(i) newmol.AddAtom(temp[newn]) added.append(newn) #Final runthrough to check that all atoms have been added, #tautomeric protons can be missed. If tautomeric proton tracking #is implemented this can be removed for i in range(0, len(temp)): if not i in added: newmol.AddAtom(temp[i]) #Restore the bonds newmol.ConnectTheDots() newmol.PerceiveBondOrders() #Write renumbered molecule to file obconversion.SetOutFormat("sdf") obconversion.WriteFile(newmol, f)
def runvina(infile, outfile, receptor, tmp_file='test.pdbqt', vina=None): obconversion = OBConversion() obconversion.SetInFormat("sdf") obconversion.SetOutFormat("pdbqt") obmol = OBMol() notatend = obconversion.ReadFile(obmol, infile) obmol2 = OBMol(obmol) ofs = pybel.Outputfile("sdf", outfile, overwrite=True) pbar = tqdm() while notatend: pbar.update(1) if obconversion.WriteFile(obmol, tmp_file): try: x = subprocess.check_output([ vina, "--score_only", "--receptor", receptor, "--ligand", tmp_file ], shell=False) # x2 = subprocess.check_output(["/Users/austin/Downloads/rf-score-4/rf-score", "/Users/austin/Downloads/rf-score-4/pdbbind-2014-refined.rf", receptor, tmp_file]) # print(x2) mol2 = pybel.Molecule(obmol2) mol2.data.update({'AutodockVinaRescoreOnly': str(get_aff(x))}) ofs.write(mol2) except subprocess.CalledProcessError as e: print(e) ofs.write(obmol) except ValueError as e: print(e) ofs.write(obmol) else: print("error writing") obmol = OBMol() notatend = obconversion.Read(obmol) obmol2 = OBMol(obmol) pbar.close() print("FAILED")
def compute_smiles(sys): """ Computes the SMILES representation of a given system. Args: sys (BigDFT.System.Systems): the system to compute the representation of. Return: (str): the smiles representation of this molecule. """ from openbabel.openbabel import OBConversion conv = OBConversion() mol = convert_system_to_babel(sys) conv.SetOutFormat("SMI") retstr = conv.WriteString(mol) retstr = retstr.replace("\n", "") retstr = retstr.replace("\t", "") return retstr
def mol_fragments(mole,outfile): obconv = OBConversion() obconv.SetOutFormat("xyz") c = 1 for atom, exp,dft,diff in zip(OBMolAtomIter(mole)): # if this is a carbon atom start a breadth first search for other carbon atoms with depth specified # create a new mol instance new_mol = OBMol() # add this atom # new_mol.AddAtom(atom) fragment_ind = [] l = atom.GetIndex() fragment_ind.append(l) # for iteration depth radius old_queue = [atom] for iteration in range(0, 3): new_queue = [] for a in old_queue: for atom2 in OBAtomAtomIter(a): i = atom2.GetIndex() # if the atom has not been seen before add it to the fragment ind list and to the new molecule if i not in fragment_ind: new_queue.append(atom2) fragment_ind.append(i) # new_mol.AddAtom(atom2) old_queue = copy.copy(new_queue) fragment_ind = [fragment_ind[0]] + sorted(fragment_ind[1:]) for i in fragment_ind: for a in OBMolAtomIter(mole): if a.GetIndex() == i: new_mol.AddAtom(a) f = open(outfile + "frag" + str(l).zfill(3) + ".xyz", "w+") f.write(str(new_mol.NumAtoms()) + "\n\n") i = 0 for atom in OBMolAtomIter(new_mol): f.write(atom.GetType()[0] + " " + str(atom.GetX()) + " " + str(atom.GetY()) + " " + str( atom.GetZ()) + "\n") i+=1 f.close() c += 1
def main(f, settings): """ Find the axis atoms Find all the atoms to be rotated Rotate it and the substituents to the other side of the plane """ obconversion = OBConversion() obconversion.SetInFormat("sdf") obmol = OBMol() obconversion.ReadFile(obmol, f) obmol.ConnectTheDots() #Find the atoms composing furan ring Rings = obmol.GetSSSR() furan = [] for ring in Rings: if len(settings.RingAtoms) == 5: if all(x in ring._path for x in settings.RingAtoms): furan = ring break else: if ring.Size() == 5 and not ring.IsAromatic(): furan = ring break if furan == []: "No five membered rings to rotate. Quitting..." quit() #Find the plane of the 5-membered ring and the outlying atom norm, d, outAtom = FindFuranPlane(obmol, furan) #Find the atoms connected to the outlying atom and sort them #as either part of the ring(axis atoms) or as atoms to be rotated AxisAtoms = [] RotAtoms = [] for NbrAtom in OBAtomAtomIter(outAtom): #if NbrAtom.IsInRingSize(5): if furan.IsInRing(NbrAtom.GetIdx()): AxisAtoms.append(NbrAtom) else: RotAtoms.append(NbrAtom) FindSubstAtoms(NbrAtom, outAtom, RotAtoms) #Simple switch to help detect if the atoms are rotated the right way WasAbove90 = False angle = FindRotAngle(AxisAtoms[0], AxisAtoms[1], outAtom, norm) if angle > 0.5 * pi: WasAbove90 = True rotangle = 2 * (angle - 0.5 * pi) else: WasAbove90 = False rotangle = 2 * (0.5 * pi - angle) OldAtomCoords = outAtom.GetVector() print("Atom " + str(outAtom.GetAtomicNum()) + " will be rotated by " +\ str(rotangle*57.3) + ' degrees') RotateAtom(outAtom, AxisAtoms[0], AxisAtoms[1], rotangle) angle2 = FindRotAngle(AxisAtoms[0], AxisAtoms[1], outAtom, norm) #if the atom is on the same side of the plane as it was, # it has been rotated in the wrong direction if ((angle2 > 0.5 * pi) and WasAbove90) or ((angle2 < 0.5 * pi) and not WasAbove90): #Flip the sign of the rotation angle, restore the coords #and rotate the atom in the opposite direction print("Atom was rotated the wrong way, switching the direction") rotangle = -rotangle outAtom.SetVector(OldAtomCoords) RotateAtom(outAtom, AxisAtoms[0], AxisAtoms[1], rotangle) RotatedAtoms = [] # Index to make sure that atoms are not rotated twice for atom in RotAtoms: if atom not in RotatedAtoms: RotateAtom(atom, AxisAtoms[0], AxisAtoms[1], rotangle) RotatedAtoms.append(atom) else: print("Atom already rotated, skipping") obconversion.SetOutFormat("sdf") obconversion.WriteFile(obmol, f[:-4] + 'rot.sdf')