Example #1
0
        public float BuildSmithWaterman(string str1, string str2)
        {
            float[,] matrix = new float[str1.Length + 1, str2.Length + 1];

            matrix[0, 0] = 0;

            // fill first row and column with 0
            for (int i = 1; i <= str1.Length; i++)
            {
                matrix[i, 0] = 0;
            }
            for (int j = 1; j <= str2.Length; j++)
            {
                matrix[0, j] = 0;
            }

            for (int i = 1; i <= str1.Length; i++)
            {
                for (int j = 1; j <= str2.Length; j++)
                {
                    float matchCost = this.CostFunction.GetCost(str1, i - 1, str2, j - 1);

                    float scoreDiag = matrix[i - 1, j - 1];       // match/mismatch
                    float scoreUp   = matrix[i - 1, j];           // deletion
                    float scoreLeft = matrix[i, j - 1];           // insertion

                    matrix[i, j] = MathExtension.Max(0,
                                                     scoreDiag + matchCost,
                                                     scoreLeft - this.gapCost,
                                                     scoreUp - this.gapCost);
                }
            }

            return(matrix[str1.Length, str2.Length]);
        }