def needleman_wunsch_matrix(self,seq1, seq2):
        """
        fill in the DP matrix according to the Needleman-Wunsch algorithm.
        Returns the matrix of scores and the matrix of pointers
        """
     
        indel = -1 # indel penalty
     
        n = len(seq1)
        m = len(seq2)

        s = np.zeros( (n+1, m+1) ) # DP matrix
        ptr = np.zeros( (n+1, m+1), dtype=int  ) # matrix of pointers
     
        ##### INITIALIZE SCORING MATRIX (base case) #####
     
        for i in range(1, n+1) :
            s[i,0] = indel * i
        for j in range(1, m+1):
            s[0,j] = indel * j
     
        ########## INITIALIZE TRACEBACK MATRIX ##########
     
        # Tag first row by LEFT, indicating initial "-"s
        ptr[0,1:] = self.LEFT
     
        # Tag first column by UP, indicating initial "-"s
        ptr[1:,0] = self.UP
     
        #####################################################
     
        for i in range(1,n+1):
            for j in range(1,m+1): 
                # match
                simpleAlign=Alignment()
                simpleAlign.needleman_wunsch(seq1[i-1], seq2[j-1])
                score=simpleAlign.score
                s[i,j] = s[i-1,j-1]+ score
                

                # indel penalty
                if s[i-1,j] + indel > s[i,j] :
                    s[i,j] = s[i-1,j] + indel
                    ptr[i,j] = self.UP
                # indel penalty
                if s[i, j-1] + indel > s[i,j]:
                    s[i,j] = s[i, j-1] + indel
                    ptr[i,j] = self.LEFT
     
        return s, ptr
Beispiel #2
0
    def needleman_wunsch_matrix(self, seq1, seq2):
        """
        fill in the DP matrix according to the Needleman-Wunsch algorithm.
        Returns the matrix of scores and the matrix of pointers
        """

        indel = -1  # indel penalty

        n = len(seq1)
        m = len(seq2)

        s = np.zeros((n + 1, m + 1))  # DP matrix
        ptr = np.zeros((n + 1, m + 1), dtype=int)  # matrix of pointers

        ##### INITIALIZE SCORING MATRIX (base case) #####

        for i in range(1, n + 1):
            s[i, 0] = indel * i
        for j in range(1, m + 1):
            s[0, j] = indel * j

        ########## INITIALIZE TRACEBACK MATRIX ##########

        # Tag first row by LEFT, indicating initial "-"s
        ptr[0, 1:] = self.LEFT

        # Tag first column by UP, indicating initial "-"s
        ptr[1:, 0] = self.UP

        #####################################################

        for i in range(1, n + 1):
            for j in range(1, m + 1):
                # match
                simpleAlign = Alignment()
                simpleAlign.needleman_wunsch(seq1[i - 1], seq2[j - 1])
                score = simpleAlign.score
                s[i, j] = s[i - 1, j - 1] + score

                # indel penalty
                if s[i - 1, j] + indel > s[i, j]:
                    s[i, j] = s[i - 1, j] + indel
                    ptr[i, j] = self.UP
                # indel penalty
                if s[i, j - 1] + indel > s[i, j]:
                    s[i, j] = s[i, j - 1] + indel
                    ptr[i, j] = self.LEFT

        return s, ptr