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 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')