예제 #1
0
/*>REAL MDMBasedScore(char **SeqTable, int nseq, int pos, int MaxInMatrix)
   -----------------------------------------------------------------------
   Calculate the score for a given position in the alignment using the
   MDM Method

   11.09.96 Original   By: ACRM
   17.09.96 Changed score to LONG rather than ULONG since return value
            from CalcMDMScore() can be -ve!
            Added MaxInMatrix
*/
REAL MDMBasedScore(char **SeqTable, int nseq, int pos, int MaxInMatrix)
{
   int   i, j,
         count;
   LONG  score;
   char  res1,
         res2;
   
   count = 0;
   score = 0L;
   for(i=0; i<nseq-1; i++)
   {
      res1 = SeqTable[i][pos];
      if(res1 == ' ')
         res1 = '-';
      
      for(j=i+1; j<nseq; j++)
      {
         res2 = SeqTable[j][pos];
         if(res2 == ' ')
            res2 = '-';
         count++;
         score += blCalcMDMScore(res1, res2);
      }
   }

   return(((REAL)score/(REAL)count)/(REAL)MaxInMatrix);
}
예제 #2
0
/*>REAL valdarMatrixScore(char res1, char res2, int MaxInMatrix)
   -------------------------------------------------------------
   Calculate the score for a given position in the alignment for valdar01
   method.
   
   20.08.2015 Original   By: TCN
*/
REAL valdarMatrixScore(char res1, char res2, int MaxInMatrix)
{
    LONG score = 0L;

    if (res1 == '-' || res2 == '-')
    {
       return ((REAL)0.0);
    }
    else
    {
        score = blCalcMDMScore(res1, res2);
        return ((REAL)score / (REAL)(MaxInMatrix));
    }

    return ((REAL)0.0);
}
예제 #3
0
int CalcMDMScore(char resa, char resb)
{
   DEPRECATED("CalcMDMScore()","blCalcMDMScore()");
   return(blCalcMDMScore(resa, resb));
}
예제 #4
0
/*>int blAffinealign(char *seq1, int length1, char *seq2, int length2, 
                     BOOL verbose, BOOL identity, int penalty, int penext, 
                     char *align1, char *align2, int *align_len)
   ---------------------------------------------------------------------
*//**

   \param[in]     *seq1         First sequence
   \param[in]     length1       First sequence length
   \param[in]     *seq2         Second sequence
   \param[in]     length2       Second sequence length
   \param[in]     verbose       Display N&W matrix
   \param[in]     identity      Use identity matrix
   \param[in]     penalty       Gap insertion penalty value
   \param[in]     penext        Extension penalty
   \param[out]    *align1       Sequence 1 aligned
   \param[out]    *align2       Sequence 2 aligned
   \param[out]    *align_len    Alignment length
   \return                         Alignment score (0 on error)
            
   Perform simple N&W alignment of seq1 and seq2. No window is used, so
   will be slow for long sequences.

   Note that you must allocate sufficient memory for the aligned 
   sequences.
   The easy way to do this is to ensure that align1 and align2 are
   of length (length1+length2).

-  07.10.92 Adapted from original written while at NIMR
-  08.10.92 Split into separate routines
-  09.10.92 Changed best structure to simple integers, moved 
            SearchForBest() into TraceBack()
-  21.08.95 Was only filling in the bottom right cell at initialisation
            rather than all the right hand column and bottom row
-  11.07.96 Changed calls to calcscore() to CalcMDMScore()
-  06.03.00 Changed name to affinealign() (the routine align() is
            provided as a backwards compatible wrapper). Added penext 
            parameter. Now supports affine gap penalties with separate
            opening and extension penalties. The code now maintains
            the path as it goes.
-  07.07.14 Use bl prefix for functions By: CTP
**************************************************************************
******   NOTE AND CHANGES SHOULD BE PROPAGATED TO affinealignuc()   ******
**************************************************************************
*/
int blAffinealign(char *seq1, 
                  int  length1, 
                  char *seq2, 
                  int  length2, 
                  BOOL verbose, 
                  BOOL identity, 
                  int  penalty, 
                  int  penext,
                  char *align1, 
                  char *align2,
                  int  *align_len)
{
   XY    **dirn   = NULL;
   int   **matrix = NULL,
         maxdim,
         i,    j,    k,    l,
         i1,   j1,
         dia,  right, down,
         rcell, dcell, maxoff,
         match = 1,
         thisscore,
         gapext,
         score;
   
   maxdim = MAX(length1, length2);
   
   /* Initialise the score matrix                                       */
   if((matrix = (int **)blArray2D(sizeof(int), maxdim, maxdim))==NULL)
      return(0);
   if((dirn   = (XY **)blArray2D(sizeof(XY), maxdim, maxdim))==NULL)
      return(0);
      
   for(i=0;i<maxdim;i++)
   {
      for(j=0;j<maxdim;j++)
      {
         matrix[i][j] = 0;
         dirn[i][j].x = -1;
         dirn[i][j].y = -1;
      }
   }
    
   /* Fill in scores up the right hand side of the matrix               */
   for(j=0; j<length2; j++)
   {
      if(identity)
      {
         if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
      }
      else
      {
         matrix[length1-1][j] = blCalcMDMScore(seq1[length1-1], seq2[j]);
      }
   }

   /* Fill in scores along the bottom row of the matrix                 */
   for(i=0; i<length1; i++)
   {
      if(identity)
      {
         if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
      }
      else
      {
         matrix[i][length2-1] = blCalcMDMScore(seq1[i], seq2[length2-1]);
      }
   }

   i = length1 - 1;
   j = length2 - 1;
   
   /* Move back along the diagonal                                      */
   while(i > 0 && j > 0)
   {
      i--;
      j--;

      /* Fill in the scores along this row                              */
      for(i1 = i; i1 > -1; i1--)
      {
         dia   = matrix[i1+1][j+1];

         /* Find highest score to right of diagonal                     */
         rcell = i1+2;
         if(i1+2 >= length1)  right = 0;
         else                 right = matrix[i1+2][j+1] - penalty;
         
         gapext = 1;
         for(k = i1+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j+1] - (penalty + gapext*penext);
            
            if(thisscore > right) 
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j+2;
         if(j+2 >= length2)  down = 0;
         else                down   = matrix[i1+1][j+2] - penalty;
         
         gapext = 1;
         for(l = j+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i1+1][l] - (penalty + gapext*penext);

            if(thisscore > down) 
            {
               down = thisscore;
               dcell = l;
            }
         }
         
         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i1][j] = dia;
            dirn[i1][j].x = i1+1;
            dirn[i1][j].y = j+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i1][j] = right;
               dirn[i1][j].x = rcell;
               dirn[i1][j].y = j+1;
            }
            else
            {
               matrix[i1][j] = down;
               dirn[i1][j].x = i1+1;
               dirn[i1][j].y = dcell;
            }
         }
       
         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
         }
         else
         {
            matrix[i1][j] += blCalcMDMScore(seq1[i1],seq2[j]);
         }
      }

      /* Fill in the scores in this column                              */
      for(j1 = j; j1 > -1; j1--)
      {
         dia   = matrix[i+1][j1+1];
         
         /* Find highest score to right of diagonal                     */
         rcell = i+2;
         if(i+2 >= length1)   right = 0;
         else                 right = matrix[i+2][j1+1] - penalty;

         gapext = 1;
         for(k = i+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j1+1] - (penalty + gapext*penext);
            
            if(thisscore > right) 
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j1+2;
         if(j1+2 >= length2)  down = 0;
         else                 down = matrix[i+1][j1+2] - penalty;

         gapext = 1;
         for(l = j1+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i+1][l] - (penalty + gapext*penext);
            
            if(thisscore > down) 
            {
               down = thisscore;
               dcell = l;
            }
         }

         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i][j1] = dia;
            dirn[i][j1].x = i+1;
            dirn[i][j1].y = j1+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i][j1] = right;
               dirn[i][j1].x = rcell;
               dirn[i][j1].y = j1+1;
            }
            else
            {
               matrix[i][j1] = down;
               dirn[i][j1].x = i+1;
               dirn[i][j1].y = dcell;
            }
         }
       
         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
         }
         else
         {
            matrix[i][j1] += blCalcMDMScore(seq1[i],seq2[j1]);
         }
      }
   } 
   
   score = TraceBack(matrix, dirn, length1, length2,
                     seq1, seq2, align1, align2, align_len);

   if(verbose)
   {
      printf("Matrix:\n-------\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("%3d ",matrix[i][j]);
         }
         printf("\n");
      }

      printf("Path:\n-----\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
         }
         printf("\n");
      }
   }
    
   blFreeArray2D((char **)matrix, maxdim, maxdim);
   blFreeArray2D((char **)dirn,   maxdim, maxdim);
    
   return(score);
}