16 import atomicDominoUtilities
21 def __init__(self, model, protein, parameterFileName):
23 self.protein = protein
24 self.namesToParticles = atomicDominoUtilities.makeNamesToParticles(
26 self.readParameters(parameterFileName)
27 self.wroteNativeProtein = 0
30 def readParameters(self, parameterFileName):
31 self.parameters = atomicDominoUtilities.readParameters(
35 currentMem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
36 currentMem *= 10 ** -6
37 print "log memory: %s" % currentMem
38 if (currentMem > self.maxMem):
39 self.maxMem = currentMem
42 def getParam(self, paramName):
43 paramValue = self.parameters[paramName]
46 def loadDominoHelpers(self):
47 self.subsetStateScores = {}
48 self.subsetRestraintScores = {}
49 self.subsetStateFailures = {}
50 self.nodesToAssignmentCount = {}
52 self.totalAssignments = 0
53 outputDir = self.getParam(
"output_directory")
54 mtreeCytoscapeAssignmentFile = self.getParam(
55 "mtree_cytoscape_assignment_file")
56 self.mtreeCytoscapeAssignmentFh = open(
57 os.path.join(outputDir, mtreeCytoscapeAssignmentFile),
'w')
58 self.mtreeCytoscapeAssignmentFh.write(
"Assignments\n")
60 self.mtNamesToIndices = {}
62 self.subsetNamesToAssignmentFiles = {}
68 def logTimePoint(self, reset):
71 timeDifference = newTime - self.timePoint
72 timeDifference = round(timeDifference, 0)
73 self.timePoint = newTime
77 self.timePoint = newTime
80 def createParticleStatesTable(self):
82 for particleName
in self.particleInfo.keys():
85 dataArray = self.particleInfo[particleName]
86 for i
in range(len(dataArray)):
87 [state, center] = dataArray[i]
88 statesToCenters[state] = center
91 for stateIndex
in sorted(statesToCenters.keys()):
93 statesList.append(vector3d)
97 self.dominoPst.set_particle_states(
98 self.namesToParticles[particleName],
101 def quickParticleName(self, particle):
102 return atomicDominoUtilities.quickParticleName(particle)
104 def filterAssignments(self, assignments, subset, nodeIndex, rssft):
110 for r
in IMP.get_restraints([self.model.get_root_restraint_set()]):
111 restraintList.append(r)
112 dg = IMP.get_dependency_graph(restraintList)
119 sFilter = rssft.get_subset_filter(subset, filteredSubsets)
121 filteredAssignments = []
122 for assignment
in assignments:
124 if (sFilter
is None or sFilter.get_is_ok(assignment)):
127 filteredAssignments.append(assignment)
130 fraction = (passedCounter * 1.0) / (stateCounter * 1.0)
131 print "%s states passed out of %s total for this subset (fraction %s)" % (passedCounter, stateCounter, fraction)
132 if (passedCounter == 0):
133 print "subset %s had 0 assignments (out of %s) pass. Exiting..." % (subset, stateCounter)
136 return filteredAssignments
140 def quickSubsetName(self, subset):
141 cleanName = self.cleanVertexName(subset)
142 atomNameList = cleanName.split(
" ")
143 sortedList = sorted(atomNameList)
145 name =
" ".join(sortedList)
148 def getNodeIndexList(self):
149 return self.mt.get_vertices()
151 def getLeafNodeIndexList(self):
152 leafNodeIndexList = []
153 for subset
in self.subsets:
154 index = self.getMtIndexForSubset(self.quickSubsetName(subset))
155 leafNodeIndexList.append(index)
156 return leafNodeIndexList
159 def createSampler(self):
162 s.set_merge_tree(self.mt)
164 filterTables.append(self.rssft)
165 s.set_subset_filter_tables(filterTables)
167 s.set_assignments_table(self.lat)
169 if (self.getParam(
"cross_subset_filtering") == 1):
170 s.set_use_cross_subset_filtering(1)
176 def createSubsets(self):
177 self.initializeParticleStatesTable()
179 [self.model.get_root_restraint_set()],
181 print "interaction graph:"
184 print "junction tree:"
186 self.subsetNamesToSubsets = {}
190 for index
in mt.get_vertices():
191 subset = mt.get_vertex_name(index)
192 subsetName = self.cleanVertexName(subset)
193 self.mtNamesToIndices[subsetName] = index
196 for subset
in subsets:
197 print "created subset %s" % subset
198 subsetName = self.quickSubsetName(subset)
199 self.subsetNamesToSubsets[subsetName] = subset
207 self.subsets = subsets
209 self.parentSiblingMap = {}
210 self.parentSiblingMap[self.mt.get_vertices()[-1]] = {}
211 self.createSiblingMap(self.mt.get_vertices()[-1])
213 def getMtIndexForSubset(self, subsetName):
214 for index
in self.mt.get_vertices():
215 mtNode = self.mt.get_vertex_name(index)
216 mtName = self.cleanVertexName(mtNode)
217 mtName = mtName.rstrip()
218 mtName = mtName.lstrip()
220 mtNameList = mtName.split(
" ")
221 subsetNameList = subsetName.split(
" ")
222 mtNameListSorted = sorted(mtNameList)
223 subsetNameListSorted = sorted(subsetNameList)
224 if (
" ".join(subsetNameListSorted) ==
" ".join(mtNameListSorted)):
227 print "did not find merge tree index for subset name %s" % subsetName
230 def getDominoParticleNames(self):
231 particles = self.dominoPst.get_particles()
232 particleNameList = []
233 for particle
in particles:
234 pName = self.quickParticleName(particle)
235 particleNameList.append(pName)
237 return particleNameList
239 def getSubsetNameList(self):
241 for subset
in self.subsets:
242 name = self.quickSubsetName(subset)
243 subsetNameList.append(name)
244 return subsetNameList
246 def getMtIndexToParticles(self):
248 mtIndexToParticles = {}
249 allVertices = self.mt.get_vertices()
250 for nodeIndex
in allVertices:
251 subset = self.mt.get_vertex_name(nodeIndex)
252 particleList = self.quickSubsetName(subset)
253 mtIndexToParticles[nodeIndex] = particleList
254 print "createtMtIndexToParticles: index %s is particleList %s " % (nodeIndex, particleList)
256 return mtIndexToParticles
258 def readTrajectoryFile(
271 scoreOutputFh = open(scoreOutputFile,
'w')
276 print "execption in loading frame %s" % i
278 score = self.model.evaluate(
False)
286 rmsd = self.calculateNativeRmsd(flexibleAtoms)
287 scoreOutputFh.write(
"%s\t%s\t%s\n" % (i, score, rmsd))
288 if (score < bestScore):
292 if (rmsd < bestRmsd):
295 if (skipDomino == 0):
296 for atomName
in atomList:
298 particle = self.namesToParticles[atomName]
301 gridIndex = self.snapToGrid(particle)
302 center = self.grid.get_center(gridIndex)
304 for coordinate
in center:
305 pythonCenter.append(coordinate)
309 if ((gridIndex
in self.particleStatesSeen[atomName]) == 0):
314 self.particleStatesSeen[atomName].keys())
315 self.particleStatesSeen[
317 gridIndex] = currentSize
318 state = self.particleStatesSeen[atomName][gridIndex]
319 self.particleInfo[atomName].append([state, pythonCenter])
321 print "didn't add domino states due to skip parameters"
322 return [bestScore, bestScoreFrame, bestRmsd, bestRmsdFrame]
326 def getParticleGridIndex(self, leaf):
328 coordinates = xyzDecorator.get_coordinates()
330 extendedIndex = self.grid.get_extended_index(coordinates)
331 if (self.grid.get_has_index(extendedIndex) == 0):
333 if (self.getParam(
"grid_type") ==
"sparse"):
334 self.grid.add_voxel(extendedIndex, 1)
336 self.grid.add_voxel(extendedIndex)
338 index = self.grid.get_index(extendedIndex)
343 def snapToGrid(self, particle):
345 index = self.getParticleGridIndex(particle)
346 center = self.grid.get_center(index)
348 xyzDecorator.set_coordinates(center)
353 def discretizeNativeProtein(self):
355 outputDir = self.getParam(
"output_directory")
356 nativeSnappedFile = self.getParam(
"native_protein_snapped_output_file")
360 self.snapToGrid(leaf)
369 def getPeptideCa(self):
373 peptideIndicesToResidues = {}
374 for residueH
in residues:
376 chainId = chain.get_id()
377 residue = residueH.get_as_residue()
378 if (chainId == self.getParam(
"peptide_chain")):
379 peptideIndicesToResidues[residue.get_index()] = residue
383 minPeptide = min(sorted(peptideIndicesToResidues.keys()))
384 maxPeptide = max(sorted(peptideIndicesToResidues.keys()))
385 centerPeptide = round(((maxPeptide - minPeptide) / 2 + minPeptide), 0)
389 centerName = atomicDominoUtilities.makeParticleName(
390 self.getParam(
"peptide_chain"),
393 centerParticle = self.namesToParticles[centerName]
394 return centerParticle
398 def createGrid(self):
402 gridSpacing = float(self.getParam(
"grid_spacing"))
403 bufferSpace = float(self.getParam(
"grid_buffer_space"))
405 protBb += bufferSpace
407 if (self.getParam(
"grid_type") ==
"sparse"):
408 ca = self.getPeptideCa()
411 gridSpacing, xyzCa.get_coordinates())
419 def initializeParticleStatesTable(self):
422 restrainedParticles = atomicDominoUtilities.getRestrainedParticles(
423 self.protein, self.model, self.namesToParticles)
425 for p
in restrainedParticles:
430 dominoPst.set_particle_states(p, xyzStates)
432 self.dominoPst = dominoPst
434 def createUniqueLeafAssignments(self, particleNameList, particleInfo):
436 size = len(particleInfo[particleNameList[0]])
439 for i
in range(size):
441 for particleName
in particleNameList:
442 dataArray = particleInfo[particleName]
443 [state, center] = dataArray[i]
444 nextAssignment.append(state)
445 allAssignments.append(nextAssignment)
448 uniqueAssignments = {}
449 for assignment
in allAssignments:
452 for index
in assignment:
453 stateString = stateString + str(index) +
"_"
454 uniqueAssignments[stateString] = assignment
455 finalAssignments = []
458 for stateString
in uniqueAssignments.keys():
459 assignmentList = uniqueAssignments[stateString]
461 finalAssignments.append(assignment)
462 return finalAssignments
466 def readMdTrajectory(self, atomList, flexibleAtoms):
469 outputDir = self.getParam(
"output_directory")
470 trajectoryFile = self.getParam(
"md_trajectory_output_file")
471 fullFile = os.path.join(outputDir, trajectoryFile)
472 rh = RMF.open_rmf_file(fullFile)
473 IMP.rmf.set_hierarchies(rh, [self.protein])
474 framesToRead = atomicDominoUtilities.getMdIntervalFrames(
475 rh, int(self.getParam(
"md_interval")), self.protein)
476 print "preparing to read md frames %s" % framesToRead
482 particleStatesSeen = {}
483 for atomName
in atomList:
484 particle = self.namesToParticles[atomName]
485 particleInfo[atomName] = []
486 particleStatesSeen[atomName] = {}
488 self.particleStatesSeen = particleStatesSeen
489 self.particleInfo = particleInfo
492 mdScoreOutputFile = os.path.join(
494 self.getParam(
"md_score_output_file"))
498 bestRmsdFrame] = self.readTrajectoryFile(
508 self.singlePdbResults(
511 self.getParam(
"best_md_score_output_file"))
514 self.singlePdbResults(
517 self.getParam(
"best_md_rmsd_output_file"))
518 self.singlePdbResults(
521 self.getParam(
"final_md_frame_output_file"))
523 self.bestMdScore = round(bestMdScore, 2)
524 self.bestMdRmsd = round(bestRmsd, 2)
525 self.bestMdScoreFrame = bestScoreFrame
526 self.bestMdRmsdFrame = bestRmsdFrame
528 def readCgTrajectories(self, atomList, flexibleAtoms):
530 cgFileName = self.getParam(
"cg_output_file")
531 bestCgScore = 10000000
533 bestCgRmsd = 10000000
536 outputDir = self.getParam(
"output_directory")
537 trajectoryFile = self.getParam(
"md_trajectory_output_file")
538 fullFile = os.path.join(outputDir, trajectoryFile)
539 rh = RMF.open_rmf_file(fullFile)
540 IMP.rmf.set_hierarchies(rh, [self.protein])
541 framesToRead = atomicDominoUtilities.getMdIntervalFrames(
542 rh, int(self.getParam(
"cg_interval")), self.protein)
544 skipCgDomino = int(self.getParam(
"skip_cg_domino"))
546 if (len(framesToRead) > 0):
547 for cgNumber
in framesToRead:
549 outputDir = self.getParam(
"output_directory")
550 fullCgFileName = os.path.join(
552 (cgFileName, cgNumber))
553 rh = RMF.open_rmf_file(fullCgFileName)
554 IMP.rmf.set_hierarchies(rh, [self.protein])
557 frameCount = IMP.rmf.get_number_of_frames(rh, self.protein)
560 if (frameCount > 20):
561 startFrameCount = frameCount - 20
563 for i
in range(startFrameCount, frameCount):
567 cgScoreOutputFile = os.path.join(
569 (self.getParam(
"cg_score_output_file"), cgNumber))
570 [cgScore, cgScoreFrame, cgRmsd, cgRmsdFrame] = self.readTrajectoryFile(
571 atomList, rh, cgFrames, cgScoreOutputFile, skipCgDomino, flexibleAtoms)
572 print "cg number %s rmsd %s score %s" % (cgNumber, cgRmsd, cgScore)
574 if (cgScore < bestCgScore):
575 bestCgScore = cgScore
576 bestCgScoreFile = fullCgFileName
577 if (cgRmsd < bestCgRmsd):
579 bestCgRmsdFile = fullCgFileName
582 self.singlePdbResults(
585 self.getParam(
"best_cg_score_output_file"))
586 self.singlePdbResults(
589 self.getParam(
"best_cg_rmsd_output_file"))
590 self.singlePdbResults(
592 (cgFileName, framesToRead[-1]), -1, self.getParam(
"final_cg_frame_output_file"))
593 finalCgRmsd = self.calculateNativeRmsd(flexibleAtoms)
594 print "final cg rmsd is %s " % finalCgRmsd
595 self.bestCgScore = round(bestCgScore, 2)
596 self.bestCgRmsd = round(bestCgRmsd, 2)
597 self.bestCgScoreFile = bestCgScoreFile
598 self.bestCgRmsdFile = bestCgRmsdFile
600 def singlePdbResults(self, trajectoryFile, frame, outputPdbFile):
602 fullTrajectoryFile = os.path.join(
603 self.getParam(
"output_directory"),
605 fullOutputFile = os.path.join(
606 self.getParam(
"output_directory"),
608 rh = RMF.open_rmf_file(fullTrajectoryFile)
609 IMP.rmf.set_hierarchies(rh, [self.protein])
611 frame = IMP.rmf.get_number_of_frames(rh, self.protein) - 1
615 def calculateRmsd(self, otherProtein, flexibleAtoms):
616 otherNamesToParticles = atomicDominoUtilities.makeNamesToParticles(
620 for pName
in otherNamesToParticles.keys():
621 if ((pName
in flexibleAtoms) == 0):
623 otherParticle = otherNamesToParticles[pName]
624 modelParticle = self.namesToParticles[pName]
627 otherParticle).get_coordinates(
631 modelParticle).get_coordinates(
636 def calculateNativeRmsd(self, flexibleAtoms):
638 if (self.wroteNativeProtein == 0):
639 pdbName = self.getParam(
"native_pdb_input_file")
645 self.wroteNativeProtein = 1
647 return self.calculateRmsd(self.nativeProtein, flexibleAtoms)
649 def calculateTrajectoryRmsd(
654 pdbName = self.getParam(
"native_pdb_input_file")
660 outputDir = self.getParam(
"output_directory")
661 fullFile = os.path.join(outputDir, trajectoryFile)
662 print "open calculate traj rmf %s" % fullFile
663 rh = RMF.open_rmf_file(fullFile)
664 IMP.rmf.set_hierarchies(rh, [otherProtein])
665 if (trajectoryFrame == -1):
666 trajectoryFrame = IMP.rmf.get_number_of_frames(
667 rh, otherProtein) - 1
669 return self.calculateRmsd(otherProtein, flexibleAtoms)
671 def createAllSubsetAssignments(self):
675 self.model.get_root_restraint_set(),
678 leafNodeIndexList = self.getLeafNodeIndexList()
680 for nodeIndex
in leafNodeIndexList:
682 subset = self.mt.get_vertex_name(nodeIndex)
683 particleNameList = []
684 for particle
in subset:
685 particleNameList.append(self.quickParticleName(particle))
686 print "creating initial assignments for leaf %s" % nodeIndex
688 assignments = self.createUniqueLeafAssignments(
689 particleNameList, self.particleInfo)
690 filteredAssignments = self.filterAssignments(
691 assignments, subset, nodeIndex, rssft)
695 for assignment
in filteredAssignments:
696 packedAssignmentContainer.add_assignment(assignment)
697 lat.set_assignments(subset, packedAssignmentContainer)
703 root = self.mt.get_vertices()[-1]
704 completeAc = self.loadAssignments(root)
705 self.completeAc = completeAc
707 def loadAssignments(self, nodeIndex):
709 children = self.mt.get_out_neighbors(nodeIndex)
710 subset = self.mt.get_vertex_name(nodeIndex)
711 heapCount = int(self.getParam(
"heap_count"))
713 heapCount, self.rssft.get_subset_filter(subset, []))
714 if len(children) == 0:
715 print "computing assignments for leaf %s" % nodeIndex
717 self.sampler.load_vertex_assignments(nodeIndex, mine)
718 print "leaf node %s has %s leaf assignments" % (nodeIndex, mine.get_number_of_assignments())
720 if (children[0] > children[1]):
721 children = [children[1], children[0]]
723 firstAc = self.loadAssignments(children[0])
724 secondAc = self.loadAssignments(children[1])
726 self.sampler.load_vertex_assignments(
727 nodeIndex, firstAc, secondAc, mine)
729 timeDifference = self.logTimePoint(0)
730 print "Done Parent %s Assignments %s first child %s second child %s time %s" % (nodeIndex, mine.get_number_of_assignments(), firstAc.get_number_of_assignments(),
731 secondAc.get_number_of_assignments(), timeDifference)
732 self.totalAssignments += mine.get_number_of_assignments()
736 def writeOutput(self, flexibleAtoms, startTime):
738 bestDominoScore = 100000
740 finalAssignments = self.completeAc.get_assignments()
741 for assignment
in finalAssignments:
743 self.dominoPst.get_subset(),
746 score = self.model.evaluate(
False)
747 if (score < bestDominoScore):
748 bestAssignment = assignment
749 bestDominoScore = round(score, 2)
750 print "best domino score is %s " % bestDominoScore
751 print "best md score is %s" % self.bestMdScore
752 print "best md rmsd is %s" % self.bestMdRmsd
753 print "best cg score is %s" % self.bestCgScore
754 print "best cg rmsd is %s" % self.bestCgRmsd
755 print "merge tree contained %s total assignments" % self.totalAssignments
758 self.dominoPst.get_subset(),
761 dominoVsMdRmsd = round(
762 self.calculateTrajectoryRmsd(
764 "md_trajectory_output_file"),
765 self.bestMdScoreFrame,
772 os.path.join(self.getParam(
"output_directory"),
773 self.getParam(
"minimum_domino_score_pdb")))
775 dominoVsCgRmsd = round(
776 self.calculateTrajectoryRmsd(
777 self.bestCgScoreFile,
781 dominoMinimizedScore = round(self.model.evaluate(
False), 2)
782 dominoRmsd = round(self.calculateNativeRmsd(flexibleAtoms), 2)
784 runTime = round(time.time() - startTime, 2)
785 print "final domino score (after cg): %s" % dominoMinimizedScore
786 print "final domino rmsd: %s" % dominoRmsd
787 print "best domino rmsd with best md score: %s" % dominoVsMdRmsd
788 print "domino rmsd with best cg score: %s" % dominoVsCgRmsd
789 print "Final Results\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (self.bestMdScore, self.bestMdRmsd, self.bestCgScore, self.bestCgRmsd, bestDominoScore, dominoRmsd, dominoMinimizedScore, dominoVsCgRmsd, self.totalAssignments, self.maxMem, runTime)
794 def createSubsetFromParticles(self, particleNames):
795 particleNameList = particleNames.split(
" ")
797 for particleName
in particleNameList:
798 particle = self.namesToParticles[particleName]
799 particleList.append(particle)
802 return [subset, particleList]
804 def createHdf5AssignmentContainer(self, index, particleNames, read):
805 root = self.getAssignmentContainerRoot(index, read)
806 print "got root for index %s" % index
809 dataset = root.get_child_index_data_set_2d(str(index))
811 dataset = root.add_child_index_data_set_2d(str(index))
812 print "added child index dataset"
814 [subset, particleOrder] = self.createSubsetFromParticles(particleNames)
815 print "created subset for index %s" % index
816 hdf5Ac = IMP.domino.create_assignments_container(
817 dataset, subset, particleOrder)
818 print "returning from create"
820 for nextInt
in order:
821 print "next int is %s" % nextInt
822 return [subset, hdf5Ac]
824 def loadAssignmentsParallel(
829 mtIndexToSubsetOrder,
833 if ((
"firstChild" in mtIndexToNodeInfo[nodeIndex]) == 0):
834 print "writing file for leaf index %s" % nodeIndex
836 self.createAssignmentsParallel(
843 beginTime = self.logTimePoint(1)
844 firstChildIndex = mtIndexToNodeInfo[nodeIndex][
"firstChild"]
845 [firstSubset, firstAc] = self.createHdf5AssignmentContainer(
846 firstChildIndex, mtIndexToSubsetOrder[firstChildIndex], 1)
847 firstAcCreateTime = self.logTimePoint(0)
849 secondChildIndex = mtIndexToNodeInfo[nodeIndex][
"secondChild"]
850 [secondSubset, secondAc] = self.createHdf5AssignmentContainer(
851 secondChildIndex, mtIndexToSubsetOrder[secondChildIndex], 1)
852 secondAcCreateTime = self.logTimePoint(0)
854 print "getting assignments for nodeIndex %s first child %s second child %s" % (nodeIndex, firstChildIndex, secondChildIndex)
855 firstChildParticles = mtIndexToSubsetOrder[firstChildIndex]
856 secondChildParticles = mtIndexToSubsetOrder[secondChildIndex]
857 myParticles = mtIndexToParticles[nodeIndex]
859 print "first child particles %s\nsecond child particles %s\nmy particles; %s\n" % (firstChildParticles, secondChildParticles, myParticles)
860 for p
in firstSubset:
861 print "next particle in first subset: %s" % self.quickParticleName(p)
863 for p
in secondSubset:
864 print "next particle in second subset: %s" % self.quickParticleName(p)
866 for assignment
in firstAc.get_assignments():
867 print "next assignment for first child %s: %s" % (firstChildIndex, assignment)
869 for assignment
in secondAc.get_assignments():
870 print "next assignment for second child %s: %s" % (secondChildIndex, assignment)
872 [mySubset, myAc] = self.createHdf5AssignmentContainer(
873 nodeIndex, mtIndexToParticles[nodeIndex], 0)
874 print "done creating hdf5"
875 prepTime = self.logTimePoint(0)
877 self.model.get_root_restraint_set(),
879 rssf = rssft.get_subset_filter(mySubset, [])
891 heapTime = self.logTimePoint(0)
893 addTime = self.logTimePoint(0)
894 for assignment
in myAc.get_assignments():
895 print "loadAssignmentsParallel next assignment for %s: %s" % (nodeIndex, assignment)
896 doneTime = self.logTimePoint(0)
897 firstChildCount = firstAc.get_number_of_assignments()
898 secondChildCount = secondAc.get_number_of_assignments()
899 print "first count: %s second count: %s begin: %s firstAc: %s secondAc: %s prep: %s heap: %s add: %s done: %s" % (firstChildCount, secondChildCount, beginTime, firstAcCreateTime, secondAcCreateTime, prepTime, heapTime, addTime, doneTime)
900 subsetOrder = self.getSubsetOrderList(mySubset)
903 def createAssignmentsParallel(
909 subsetName = mtIndexToParticles[nodeIndex]
910 print "starting assignments parallel leaf index %s subset name %s" % (nodeIndex, subsetName)
911 [subset, particleList] = self.createSubsetFromParticles(subsetName)
913 particleNameList = subsetName.split(
" ")
916 finalAssignments = self.createUniqueLeafAssignments(
917 particleNameList, particleInfo)
924 self.model.get_root_restraint_set(),
926 filteredAssignments = self.filterAssignments(
927 finalAssignments, subset, nodeIndex, rssft)
928 root = self.getAssignmentContainerRoot(nodeIndex, 0)
929 dataset = root.add_child_index_data_set_2d(str(nodeIndex))
930 dataset.set_size([0, len(subset)])
932 hdf5AssignmentContainer = IMP.domino.create_assignments_container(
933 dataset, subset, particleOrder)
934 for assignment
in filteredAssignments:
935 hdf5AssignmentContainer.add_assignment(assignment)
936 for assignment
in hdf5AssignmentContainer.get_assignments():
937 print "hdf5 assignment container node %s next assignmet %s" % (nodeIndex, assignment)
940 subsetOrder = self.getSubsetOrderList(subset)
941 print "leaf node returning with order %s" % subsetOrder
946 def writePymolData(self):
948 outputDir = self.getParam(
"output_directory")
950 pymolInteractions = self.getParam(
"pymol_interactions_file")
956 pymolSubsets = self.getParam(
"pymol_subsets_file")
964 def cleanVertexName(self, vertexName):
968 nodeRe = re.compile(
'Subset\(\[(.*?)\s*\]')
969 secondNodeRe = re.compile(
'\[(.*?)\s*\]')
970 node = nodeRe.search(str(vertexName))
971 secondNode = secondNodeRe.search(str(vertexName))
975 foundName = node.group(1)
977 foundName = secondNode.group(1)
978 vertexNameFinal = foundName.replace(
'"',
'')
979 return vertexNameFinal
981 def getSubsetOrderList(self, subset):
983 for particle
in subset:
984 name = self.quickParticleName(particle)
985 subsetOrderList.append(name)
986 subsetOrder =
" ".join(subsetOrderList)
989 def checkAssignments(self, subset, nodeIndex, particleOrder):
990 print "reading back in assignments for leaf index %s" % nodeIndex
991 root = self.getAssignmentContainerRoot(nodeIndex, 1)
992 dataset = root.get_child_index_data_set_2d(str(nodeIndex))
993 hdf5 = IMP.domino.create_assignments_container(
994 dataset, subset, particleOrder)
995 for assignment
in hdf5.get_assignments():
996 print "leaf index %s read back in %s" % (nodeIndex, assignment)
998 def createSamplerLite(self):
1001 if (self.getParam(
"cross_subset_filtering") == 1):
1002 s.set_use_cross_subset_filtering(1)
1006 def createSiblingMap(self, parentIndex):
1008 children = self.mt.get_out_neighbors(parentIndex)
1009 if (len(children) > 0):
1010 firstChild = children[0]
1011 secondChild = children[1]
1012 self.parentSiblingMap[firstChild] = {}
1013 self.parentSiblingMap[firstChild][
"sibling"] = secondChild
1014 self.parentSiblingMap[firstChild][
"parent"] = parentIndex
1016 self.parentSiblingMap[secondChild] = {}
1017 self.parentSiblingMap[secondChild][
"sibling"] = firstChild
1018 self.parentSiblingMap[secondChild][
"parent"] = parentIndex
1019 print "created map for parent %s first child %s second child %s" % (parentIndex, firstChild, secondChild)
1021 self.parentSiblingMap[parentIndex][
"firstChild"] = firstChild
1022 self.parentSiblingMap[parentIndex][
"secondChild"] = secondChild
1024 self.createSiblingMap(firstChild)
1025 self.createSiblingMap(secondChild)
1027 def getMtIndexToNodeInfo(self):
1028 return self.parentSiblingMap
1030 def getLeafParentNodeIndexList(self):
1031 leafParentNodeIndexList = {}
1032 leafNodeIndexList = self.getLeafNodeIndexList()
1033 for leafIndex
in leafNodeIndexList:
1034 parent = self.parentSiblingMap[leafIndex][
"parent"]
1035 leafParentNodeIndexList[parent] = 1
1036 return leafParentNodeIndexList
1038 def getMtIndexToNameList(selt):
1040 for index
in self.mt.get_vertices():
1041 name = self.mt.get_vertex_name(index)
1042 mtIndexToNames[index] = name
1043 return mtIndexToNames
1045 def getAssignmentContainerRoot(self, subsetIndex, read):
1046 outputDir = self.getParam(
"output_directory")
1047 filePrefix = self.getParam(
"subset_assignment_output_file")
1048 assignmentFileName = os.path.join(
1050 (filePrefix, subsetIndex))
1051 print "creating hdf5 file with name %s" % assignmentFileName
1054 root = RMF.open_hdf5_file(assignmentFileName)
1056 root = RMF.create_hdf5_file(assignmentFileName)
1063 def writeVisualization(self):
1065 self.writeCytoscapeIgInput()
1066 self.writeCytoscapeJtInput()
1067 self.writeCytoscapeMtInput()
1068 self.writeCytoscapeScripts()
1069 self.writePymolData()
1071 def writeCytoscapeScripts(self):
1072 outputDir = self.getParam(
"output_directory")
1073 mTreeCytoscapeInput = self.getParam(
"mtree_cytoscape_input_file")
1074 mTreeCytoscapeAssignments = self.getParam(
1075 "mtree_cytoscape_assignment_file")
1076 mTreeCytoscapeAtomChains = self.getParam(
1077 "mtree_cytoscape_atom_chain_file")
1078 mTreeCytoscapeAtomSummary = self.getParam(
1079 "mtree_cytoscape_atom_summary_file")
1081 mTreeCytoscapeScript = self.getParam(
"mtree_cytoscape_script")
1082 mTreeFh = open(os.path.join(outputDir, mTreeCytoscapeScript),
'w')
1084 "network import file=%s\n" %
1085 os.path.join(outputDir, mTreeCytoscapeInput))
1087 "node import attributes file=\"%s\"\n" %
1088 os.path.join(outputDir, mTreeCytoscapeAssignments))
1090 "node import attributes file=\"%s\"\n" %
1091 os.path.join(outputDir, mTreeCytoscapeAtomSummary))
1093 "node import attributes file=\"%s\"\n" %
1094 os.path.join(outputDir, mTreeCytoscapeAtomChains))
1095 mTreeFh.write(
"layout jgraph-tree\n")
1097 def getGraphStructure(self, graph, fileName, separator):
1099 graphLogWrite = open(fileName,
'w')
1100 graph.show(graphLogWrite)
1101 graphLogWrite.close()
1104 graphLogRead = open(fileName,
'r')
1106 nodeRe = re.compile('^(\d+)\[label\=\"\[*(.*?)\s*\]*\"')
1107 separatorEscape =
"\\" + separator
1108 edgeString =
"^(\d+)\-%s(\d+)" % separatorEscape
1109 edgeRe = re.compile(edgeString)
1115 for line
in graphLogRead:
1118 node = nodeRe.search(line)
1120 nodeNumber = node.group(1)
1121 atomString = node.group(2)
1122 nodesToNames[nodeNumber] = atomString
1126 edge = edgeRe.search(line)
1128 firstNode = edge.group(1)
1129 secondNode = edge.group(2)
1131 if (firstNode
in nodesToNodes):
1132 firstNodeDict = nodesToNodes[firstNode]
1133 firstNodeDict[secondNode] = 1
1134 nodesToNodes[firstNode] = firstNodeDict
1136 return [nodesToNames, nodesToNodes]
1138 def writeEdgeFile(self, nodesToNodes, edgeFileName):
1140 outputDir = self.getParam(
"output_directory")
1141 graphInputFile = open(os.path.join(outputDir, edgeFileName),
'w')
1142 for firstNode
in nodesToNodes.keys():
1143 nodeDict = nodesToNodes[firstNode]
1144 for secondNode
in nodeDict.keys():
1145 graphInputFile.write(
"%s ttt %s\n" % (firstNode, secondNode))
1146 graphInputFile.close()
1148 def writeCytoscapeIgInput(self):
1149 outputDir = self.getParam(
"output_directory")
1150 igOutputFile = self.getParam(
"ig_output_file")
1151 [nodesToNames, nodesToNodes] = self.getGraphStructure(
1152 self.ig, os.path.join(outputDir, igOutputFile),
"-")
1156 self.getParam(
"ig_cytoscape_input_file"))
1159 igResiduesFile = self.getParam(
"ig_cytoscape_residues_file")
1160 peptideChain = self.getParam(
"peptide_chain")
1162 subsetResidueFile = open(os.path.join(outputDir, igResiduesFile),
'w')
1163 subsetResidueFile.write(
"ResidueNumber\n")
1164 for nodeNumber
in nodesToNames.keys():
1165 nodeName = nodesToNames[nodeNumber]
1167 [nodeChain, residueNumber,
1168 nodeAtom] = atomicDominoUtilities.getAtomInfoFromName(nodeName)
1169 if (nodeChain == peptideChain):
1170 subsetResidueFile.write(
1172 (nodeNumber, residueNumber))
1176 subsetResidueFile.write(
"%s = 100\n" % nodeNumber)
1178 def writeCytoscapeMtInput(self):
1179 outputDir = self.getParam(
"output_directory")
1180 mTreeOutputFile = self.getParam(
"mtree_output_file")
1181 [nodesToNames, nodesToNodes] = self.getGraphStructure(
1182 self.mt, os.path.join(outputDir, mTreeOutputFile),
">")
1186 self.getParam(
"mtree_cytoscape_input_file"))
1187 self.writeNodeNameAttributes(
1188 nodesToNames, self.getParam(
"mtree_cytoscape_atom_name_file"), self.getParam(
1189 "mtree_cytoscape_atom_summary_file"),
1190 self.getParam(
"mtree_cytoscape_atom_chain_file"))
1192 def writeCytoscapeJtInput(self):
1194 outputDir = self.getParam(
"output_directory")
1195 jTreeOutputFile = self.getParam(
"jtree_output_file")
1196 [nodesToNames, nodesToNodes] = self.getGraphStructure(
1197 self.jt, os.path.join(outputDir, jTreeOutputFile),
"-")
1201 self.getParam(
"jtree_cytoscape_input_file"))
1203 self.writeNodeNameAttributes(
1204 nodesToNames, self.getParam(
"jtree_cytoscape_atom_name_file"), self.getParam(
1205 "jtree_cytoscape_atom_summary_file"),
1206 self.getParam(
"jtree_cytoscape_atom_chain_file"))
1210 jtreeCytoscapeEdgeFile = self.getParam(
"jtree_cytoscape_edge_file")
1211 edgeWeightFile = open(
1212 os.path.join(outputDir,
1213 jtreeCytoscapeEdgeFile),
1215 edgeWeightFile.write(
"SubsetOverlap (class=Integer)\n")
1216 for firstNode
in nodesToNodes.keys():
1217 nodeDict = nodesToNodes[firstNode]
1218 for secondNode
in nodeDict.keys():
1219 firstNodeAtoms = nodesToNames[firstNode].split(
" ")
1220 secondNodeAtoms = nodesToNames[secondNode].split(
" ")
1222 val
for val
in firstNodeAtoms
if val
in secondNodeAtoms]
1223 edgeWeightFile.write(
1224 "%s (pp) %s = %s\n" %
1225 (firstNode, secondNode, len(intersection)))
1226 edgeWeightFile.close()
1228 def getAtomTypeCounts(self, atomNames):
1230 atomNames = atomNames.lstrip(
'[')
1231 atomNames = atomNames.rstrip(
']')
1232 atomNames = atomNames.rstrip()
1234 atomList = atomNames.split(
" ")
1235 peptideAtomCount = 0
1236 proteinAtomCount = 0
1237 for atom
in atomList:
1238 [chain, residue, atom] = atomicDominoUtilities.getAtomInfoFromName(
1240 if (chain == self.getParam(
"peptide_chain")):
1241 peptideAtomCount += 1
1243 proteinAtomCount += 1
1244 return [peptideAtomCount, proteinAtomCount]
1246 def writeNodeNameAttributes(
1253 outputDir = self.getParam(
"output_directory")
1254 subsetAtomNameFile = open(os.path.join(outputDir, atomNameFile),
'w')
1255 subsetAtomSummaryFile = open(
1256 os.path.join(outputDir, atomSummaryFile),
'w')
1257 subsetAtomChainFile = open(os.path.join(outputDir, atomChainFile),
'w')
1259 subsetAtomNameFile.write(
"Atom names\n")
1260 subsetAtomSummaryFile.write(
"Atom Summary\n")
1261 subsetAtomChainFile.write(
"Atom chain\n")
1262 for node
in nodesToNames.keys():
1263 atomNames = nodesToNames[node]
1264 subsetAtomNameFile.write(
"%s = %s\n" % (node, atomNames))
1265 [peptideAtomCount, proteinAtomCount] = self.getAtomTypeCounts(
1268 subsetAtomSummaryFile.write(
1270 (node, proteinAtomCount, peptideAtomCount))
1273 if (proteinAtomCount == 0):
1274 subsetAtomChainFile.write(
"%s = 1\n" % node)
1275 elif (peptideAtomCount == 0):
1276 subsetAtomChainFile.write(
"%s = 2\n" % node)
1278 subsetAtomChainFile.write(
"%s = 3\n" % node)
1280 subsetAtomChainFile.close()
1281 subsetAtomSummaryFile.close()
1282 subsetAtomNameFile.close()
Chain get_chain(Hierarchy h)
InteractionGraph get_interaction_graph(ScoringFunctionAdaptor rs, const kernel::ParticlesTemp &pst)
void write_pdb(const Selection &mhd, base::TextOutput out, unsigned int model=1)
Grid3D< int, SparseGridStorage3D< int, UnboundedGridStorage3D > > SparseUnboundedIntGrid3D
kernel::ParticlesTemp get_order(const Subset &s, const SubsetFilterTables &sft)
void set_log_level(LogLevel l)
Set the current global log level.
SubsetGraph get_junction_tree(const InteractionGraph &ig)
Simple conjugate gradients optimizer.
Sample best solutions using Domino.
algebra::BoundingBoxD< 3 > get_bounding_box(const Hierarchy &h)
Get a bounding box for the Hierarchy.
Subsets get_subsets(const SubsetGraph &g)
Gets all of the Subsets of a SubsetGraph.
Filter a configuration of the subset using the kernel::Model thresholds.
Represent a subset of the particles being optimized.
void load_frame(RMF::FileConstHandle file, unsigned int frame)
Grid3D< double, DenseGridStorage3D< double > > DenseDoubleGrid3D
double get_rmsd(const core::XYZs &s0, const core::XYZs &s1, const IMP::algebra::Transformation3D &tr_for_second)
MergeTree get_balanced_merge_tree(const SubsetGraph &junction_tree)
display::Geometries get_subset_graph_geometry(const SubsetGraph &ig)
Select all non-alternative ATOM records.
Hierarchies get_by_type(Hierarchy mhd, GetByType t)
void load_merged_assignments(const Subset &first_subset, AssignmentContainer *first, const Subset &second_subset, AssignmentContainer *second, const SubsetFilterTablesTemp &filters, AssignmentContainer *ret)
Fill in assignments for an internal node.
A decorator for a particle with x,y,z coordinates.
See IMP.core for more information.
See IMP.algebra for more information.
Store a configuration of a subset.
Write a CGO file with the geometry.
static XYZ decorate_particle(::IMP::kernel::Particle *p)
display::Geometries get_interaction_graph_geometry(const InteractionGraph &ig)
void load_particle_states(const Subset &s, const Assignment &ss, const ParticleStatesTable *pst)
void read_pdb(base::TextInput input, int model, Hierarchy h)
Hierarchies get_leaves(const Selection &h)
See IMP.rmf for more information.
See IMP.domino for more information.
Class for storing model, its restraints, constraints, and particles.