Beispiel #1
0
 def __makeTree(self, lineList, flexRes=False):
     # initialize
     nodeStack = []
     atomToParentNode = 0  # first atom after BRANCH goes to parent
     tor_number = 1
     atomIndex = 0
     self.activeInFile = []
     self.inactiveInFile = []
     # process lines/build tree
     for lineStr in lineList:
         if debug: print(lineStr)
         wordList = string.split(lineStr)
         if not wordList: continue  # skip the loop
         #
         # Here lies the main switch for the PDBQ tags
         #
         if wordList[0] == 'HETATM' or wordList[0] == 'ATOM':
             # pdb is one-based; we are zero-based
             if atomToParentNode or flexRes:
                 # The first atom after the BRANCH goes to the parent
                 nodeStack[-1].parent.atomList.append(atomIndex)
                 atomToParentNode = None  # unset; set in BRANCH (below)
                 if debug:
                     print("add atom (parent): ", atomIndex,
                           nodeStack[-1].parent)
                 flexRes = False
             else:
                 nodeStack[-1].atomList.append(atomIndex)
                 if debug: print("add atom: ", atomIndex, nodeStack[-1])
             atomIndex = atomIndex + 1
         elif (wordList[0] == 'TORS' or wordList[0] == 'BRANCH'):
             atomToParentNode = 1  # set; unset in HETATM (above)
             newNode = TreeNode(parent=nodeStack[-1])
             newNode.number = tor_number
             newNode.bond = (int(wordList[1]) - 1, int(wordList[2]) - 1)
             newNode.atomList = []
             tor_number = tor_number + 1
             nodeStack.append(newNode)
             if debug: print("push node: ", newNode)
         elif (wordList[0] == 'ENDTORS' or wordList[0] == 'ENDBRANCH'):
             nodeStack.pop()
         elif wordList[0] == 'ROOT':
             rootNode = TreeNode()
             rootNode.number = 0
             rootNode.bond = (None, None)
             rootNode.atomList = []
             nodeStack.append(rootNode)
             if debug: print("push root: ", rootNode)
         elif wordList[0] == 'ENDROOT':
             pass
         elif wordList[0] == 'REMARK' and 'between' in wordList:
             # record state of each torsion
             if 'I' in wordList:
                 self.inactiveInFile.append(wordList)
             if 'A' in wordList:
                 self.activeInFile.append(wordList)
         else:  # ignore it
             pass
     return rootNode
 def __makeTree(self, lineList, flexRes=False):
     # initialize 
     nodeStack = []
     atomToParentNode = 0 # first atom after BRANCH goes to parent
     tor_number = 1
     atomIndex = 0
     self.activeInFile = []
     self.inactiveInFile = []
     # process lines/build tree
     for lineStr in lineList:
         if debug: print lineStr
         wordList = string.split(lineStr)
         if not wordList: continue # skip the loop
         #
         # Here lies the main switch for the PDBQ tags
         #
         if wordList[0] == 'HETATM' or wordList[0] == 'ATOM':
             # pdb is one-based; we are zero-based
             if atomToParentNode or flexRes:
                 # The first atom after the BRANCH goes to the parent
                 nodeStack[-1].parent.atomList.append(atomIndex)
                 atomToParentNode = None # unset; set in BRANCH (below)
                 if debug: print "add atom (parent): ", atomIndex, nodeStack[-1].parent
                 flexRes = False
             else:
                 nodeStack[-1].atomList.append(atomIndex)
                 if debug: print "add atom: ", atomIndex, nodeStack[-1]
             atomIndex = atomIndex + 1
         elif (wordList[0] == 'TORS' or wordList[0] == 'BRANCH'):
             atomToParentNode = 1 # set; unset in HETATM (above)
             newNode = TreeNode(parent=nodeStack[-1])
             newNode.number = tor_number
             newNode.bond = (int(wordList[1])-1, int(wordList[2])-1)
             newNode.atomList = []
             tor_number = tor_number + 1;
             nodeStack.append(newNode)
             if debug: print "push node: ", newNode
         elif (wordList[0] == 'ENDTORS' or wordList[0] == 'ENDBRANCH'):
             nodeStack.pop()
         elif wordList[0] == 'ROOT':
             rootNode = TreeNode()
             rootNode.number = 0
             rootNode.bond = (None, None)
             rootNode.atomList = []
             nodeStack.append(rootNode)
             if debug: print "push root: ", rootNode
         elif wordList[0] == 'ENDROOT':
             pass
         elif wordList[0] == 'REMARK' and 'between' in wordList:
             # record state of each torsion 
             if 'I' in wordList:
                 self.inactiveInFile.append(wordList)
             if 'A' in wordList:
                 self.activeInFile.append(wordList)
         else: # ignore it
             pass
     return rootNode
Beispiel #3
0
    def __buildNode(self, bnd, fromAt, startAt, root=0, allNodes=[]):
        # this is called with an activeTors bnd;
        # always making a newNode
        # first add the fromAtom
        newNode = TreeNode()
        newNode.number = self.tor_number
        allNodes.append(newNode)
        self.tor_number = self.tor_number + 1
        newNode.atoms_to_move = 0
        # fix this: root need special handling
        if root:
            adjAts = [fromAt]
            fromAt.tt_ind = 0
            # atomIndex counts # of atoms put in torTree, 0-based
            self.atomIndex = 0
            atomList = [0, 1]
            ats = [fromAt, startAt]
        else:
            adjAts = [startAt]
            atomList = []
            # ats = []
        # first expand the adjAts to include all atoms linked
        # to startAt by inactive bonds
        for at in adjAts:
            if at._used: continue
            at._used = 1
            for b in at.bonds:
                # recursively add all rootatoms
                # ie atoms connected to rootatoms by non-rotatable bonds
                at2 = b.atom1
                if at2 == at:
                    at2 = b.atom2
                if at2._used: continue
                if not b.activeTors:
                    if not hasattr(at2, 'tt_ind'):
                        # and at2!=startAt and at2!=fromAt:
                        self.atomIndex = self.atomIndex + 1
                        at2.tt_ind = self.atomIndex
                    if at2 not in adjAts:
                        adjAts.append(at2)
                        if at2 != startAt and at2 not in atomList:
                            atomList.append(at2.tt_ind)
                            # ats.append(at2)
        # have to redo this after loop to get breadth first:
        for at in adjAts:
            for b in at.bonds:
                at2 = b.atom1
                if at2 == at:
                    at2 = b.atom2
                if at2._used: continue
                if not hasattr(at2, 'tt_ind'):
                    # and at2!=startAt and at2!=fromAt:
                    self.atomIndex = self.atomIndex + 1
                    at2.tt_ind = self.atomIndex
                if b.activeTors:
                    nnode, allNodes = self.__buildNode(b, at, at2, 0, allNodes)
                    newNode.children.append(nnode)
                    # keep track of number of atoms in subtree
                    newNode.atoms_to_move = newNode.atoms_to_move + nnode.atoms_to_move
                    # make sure at and at2 are in this node
                    ##test atoms in this bond
                    for a in [at, at2]:
                        if a != fromAt and a != startAt:
                            ##if a.tt_ind not in atomList:
                            atomList.append(a.tt_ind)

        # finally, set atoms to move to include atoms in this node
        d = {}
        for a in atomList:
            d[a] = 0
        atList = list(d.keys())
        atList.sort()
        # newNode.atomList = atomList
        if root:
            newNode.bond = (None, None)
        else:
            newNode.bond = (fromAt.tt_ind, startAt.tt_ind)
        newNode.atoms_to_move = newNode.atoms_to_move + len(atList)
        newNode.atomList = atList
        # newNode.ats = ats
        return newNode, allNodes
    def __buildNode(self, bnd, fromAt, startAt, root=0, allNodes=[]):
        # this is called with an activeTors bnd; 
        # always making a newNode
        # first add the fromAtom
        newNode = TreeNode()
        newNode.number = self.tor_number
        allNodes.append(newNode)
        self.tor_number = self.tor_number + 1
        newNode.atoms_to_move = 0
        #fix this: root need special handling
        if root:
            adjAts = [fromAt]
            fromAt.tt_ind = 0
            #atomIndex counts # of atoms put in torTree, 0-based
            self.atomIndex = 0
            atomList = [0, 1]
            ats = [fromAt, startAt]
        else:
            adjAts = [startAt]
            atomList = []
            #ats = []
        #first expand the adjAts to include all atoms linked
        #to startAt by inactive bonds
        for at in adjAts:
            if at._used: continue
            at._used = 1
            for b in at.bonds:
                # recursively add all rootatoms
                # ie atoms connected to rootatoms by non-rotatable bonds
                at2 = b.atom1
                if at2==at:
                    at2 = b.atom2
                if at2._used: continue
                if not b.activeTors:
                    if not hasattr(at2, 'tt_ind'):
                        #and at2!=startAt and at2!=fromAt:
                        self.atomIndex = self.atomIndex + 1
                        at2.tt_ind = self.atomIndex
                    if at2 not in adjAts:
                        adjAts.append(at2)
                        if at2!=startAt and at2 not in atomList:
                            atomList.append(at2.tt_ind)
                            #ats.append(at2)
        #have to redo this after loop to get breadth first:
        for at in adjAts:
            for b in at.bonds:
                at2 = b.atom1
                if at2==at:
                    at2 = b.atom2
                if at2._used: continue
                if not hasattr(at2, 'tt_ind'):
                    #and at2!=startAt and at2!=fromAt:
                    self.atomIndex = self.atomIndex + 1
                    at2.tt_ind = self.atomIndex
                if b.activeTors:
                    nnode, allNodes = self.__buildNode(b, at, at2, 0, allNodes)
                    newNode.children.append(nnode)
                    #keep track of number of atoms in subtree
                    newNode.atoms_to_move = newNode.atoms_to_move + nnode.atoms_to_move
                    #make sure at and at2 are in this node
                    ##test atoms in this bond
                    for a in [at, at2]:
                        if a!=fromAt and a!=startAt:
                            ##if a.tt_ind not in atomList:
                            atomList.append(a.tt_ind)

        #finally, set atoms to move to include atoms in this node
        d = {}
        for a in atomList:
            d[a] = 0
        atList = d.keys()
        atList.sort()
        #newNode.atomList = atomList
        if root:
            newNode.bond = (None, None)
        else:
            newNode.bond = (fromAt.tt_ind, startAt.tt_ind)
        newNode.atoms_to_move = newNode.atoms_to_move + len(atList)
        newNode.atomList = atList
        #newNode.ats = ats
        return newNode, allNodes