def repairPointPdbRecord(tstD=None, tstFileName=False): '''checks and repairs pointpdbrecord if it has no data in it''' same = True if tstD is None: # hasn't been read in already tstD = tstdata.tstData( tstFileName, necessaryKeys=tstdata.tstData.necessaryKeysForPocket) lastPdbNum = tstD.dict['POINT_PDB_RECORD'][0][1]-1 for pointPdb in tstD.dict['POINT_PDB_RECORD']: pdbNum = pointPdb[1]-1 if pdbNum != lastPdbNum: same = False if same: # needs repaired, otherwise this is over pdbD = pdb.pdbData() for line in tstD.dict['PDB_RECORD']: pdbD.processLine(line) ptCoords = {} for pointXyz in tstD.dict['POINT_XYZ']: ptCoords[pointXyz[0]] = tuple(pointXyz[1:]) coordToNearbyAtoms = pdbD.getNearbyAtoms(ptCoords.values()) newPointPdbRec = [] for pointPdb in tstD.dict['POINT_PDB_RECORD']: atomFound = coordToNearbyAtoms[ptCoords[pointPdb[0]]][0] newPointPdbRec.append([pointPdb[0], atomFound]) #replace old record with new record tstD.dict['POINT_PDB_RECORD'] = newPointPdbRec if tstFileName: tstFile = open(tstFileName, 'a') # append into file tstdata.writeEntryIntegers( tstD.dict['POINT_PDB_RECORD'], "POINT_PDB_RECORD", "END POINT_PDB_RECORD", tstFile) tstFile.close()
def tstPocketMap( tstFileName, phiFileName, tstD=None, ligandFileName=None, nearbyDistance=0., appendTst=True, doPCA=True): '''pocket mapping algorithm, finds all pockets on entire surface, puts in tree and graph data structure, various outputs''' print "read tst file" if tstD is None: # hasn't been read in already tstD = tstdata.tstData( tstFileName, necessaryKeys=tstdata.tstData.necessaryKeysForPocket) print "repairing nearby points if necessary" repairPointPdbRecord(tstD, tstFileName) print "calculating charges" chargeXyz, hydroXyz = calculateCharges(tstD, charge.charge()) print "calculating curvatures" edgeCurv, ptCurv, ptWeighCurv = tstCurvature.tstEdgeCurvature( tstD.dict['TRIANGLE_POINT'], tstD.dict['POINT_XYZ'], tstD.dict['POINT_TRIANGLE'], tstD.dict['POINT_NEIGHBOR']) tstD.dict['POINT_CURVATURE_EDGE'] = ptWeighCurv tstD.dict['CHARGE_XYZ'] = chargeXyz tstD.dict['HYDROPHOBIC_XYZ'] = hydroXyz print "read in phi file" phiData = phi(phiFileName) # read in the phimap print "making mesh data structure" meshData, gridData = meshConstruct( tstD, phiData, tstFileName, threshold="auto", cavities=True) meshData.setPtHydro(tstD.dict['HYDROPHOBIC_XYZ']) meshData.setPtCurvature(tstD.dict['POINT_CURVATURE_EDGE']) gridSize = 1.0/phiData.scale tstPdbRecord = tstD.dict['PDB_RECORD'] meshData.setSurfaceArea(tstD.dict['TRIANGLE_POINT']) del tstD, phiData, gridData # not needed, reclaim memory pdbD = pdb.pdbData() pdbD.processLines(tstPdbRecord) pointAtomList = meshData.calculateNearbyAtoms(pdbD, nearbyDistance) meshData.setVolume(gridSize) print "calculating travel depth" meshData.calculateTravelDistance("traveldepth", [0], [2, 3, 5]) pointTravelDepth = meshData.getSurfaceTravelDistance("traveldepth") if ligandFileName is not None: # if there is a ligand, read it ligand = pdb.pdbData(ligandFileName) ligandXYZR = ligand.getHeavyAtomXYZRadius() nodeWithinSet = meshData.getWithinNodesNoInside(ligandXYZR) bestIU = 0. # intersection / union, 1 is perfect #print nodeWithinSet, len(nodeWithinSet) print "pocket mapping starting" if ligandFileName is not None and len(nodeWithinSet) > 0: outFileName = ligandFileName #tstdebug.nodeDebug(nodeWithinSet, \ # filename = tstFileName+".within.ligand.py") localMaxima, borders, tm3tree, surfNodeToLeaf = meshData.pocketMapping( 'traveldepth', [2, 3, 5], pointAtomList, pdbD, outName=outFileName + ".", groupName='group', ligandNodes=nodeWithinSet, doPCA=doPCA) else: outFileName = tstFileName localMaxima, borders, tm3tree, surfNodeToLeaf = meshData.pocketMapping( 'traveldepth', [2, 3, 5], pointAtomList, pdbD, outName=outFileName + ".", groupName='group', doPCA=doPCA) #print len(localMaxima), len(borders), tm3tree, len(surfNodeToLeaf) #tstdebug.nodeDebug(localMaxima, \ # filename=tstFileName+".localmaxima.pocketmap.py") #tstdebug.nodeDebug(borders, \ # filename=tstFileName+".borders.pocketmap.py") #tstdebug.nodeDebug(meshData.getSurfaceNodes(), \ # filename=tstFileName+".groups.pocketmap.py", name='group') tm3tree.write(outFileName + ".tree.tm3") #tm3tree.writeTNV(tstFileName + ".tree.tnv") #doesn't seem to import into treemap correctly if appendTst: # turn off sometimes since appends to tst file print "appending data to tst file" surfNodes = meshData.getSurfaceNodes() pointLeafList = [] for aNode in surfNodes: if aNode not in surfNodeToLeaf: print aNode, aNode.distances leafNum = 0 # made up and wrong for testing else: leafNum = surfNodeToLeaf[aNode] pointLeafList.append([aNode, int(leafNum)]) #print pointLeafList leafToGroup = tm3tree.getLeafToGroup() leafGroupList = [] leafKeyMax = max(leafToGroup.keys()) for leaf in xrange(leafKeyMax): tempList = [leaf + 1] try: tempList.extend(leafToGroup[leaf + 1]) except KeyError: pass # means leaf doesn't exist leafGroupList.append(tempList) #print leafGroupList tstFile = open(tstFileName, 'a') tstdata.writeEntryIntegers( pointLeafList, "POINT_LEAF LIST", "END POINT_LEAF", tstFile) tstdata.writeEntryIntegers( leafGroupList, "LEAF_GROUP LIST", "END LEAF_GROUP", tstFile) tstdata.writeEntryIntegers( pointAtomList, "POINT_NEARBY_ATOM LIST", "END POINT_NEARBY_ATOM", tstFile) #also write curvature and charge data here tstdata.writeEntrySingleFloat( ptWeighCurv, "POINT_CURVATURE_EDGE LIST", "END POINT_CURVATURE_EDGE", tstFile) tstdata.writeEntrySingleFloat( chargeXyz, "CHARGE_XYZ", "END CHARGE_XYZ", tstFile) tstdata.writeEntrySingleFloat( hydroXyz, "HYDROPHOBIC_XYZ", "END HYDROPHOBIC_XYZ", tstFile) #write data to file tstFile.write("DEPTH_TRAVEL_DIST\n") for line in pointTravelDepth: lineOut = "%8d" % line[0] for count in xrange(1, len(line)): lineOut += "%+9.4f " % line[count] noPlusLine = string.replace(lineOut, "+", " ") tstFile.write(noPlusLine) tstFile.write("\n") tstFile.write("END DEPTH_TRAVEL_DIST\n") tstFile.close() print "pocket mapping complete"