/// <summary> Returns the alignment of two sequences based on the passed array of pointers</summary>
        /// <param name="s1">sequence #1 </param>
        /// <param name="s2">sequence #2 </param>
        /// <param name="m">scoring matrix </param>
        /// <param name="cell">The cell where the traceback starts. </param>
        /// <returns> <see cref="Alignment"/> with the two aligned sequences and alignment score. </returns>
        /// <seealso cref="Cell"/>
        /// <seealso cref="Alignment"/>
        private Alignment Traceback(Sequence s1, Sequence s2, Matrix m,
			byte[] pointers, Cell cell, short[] sizesOfVerticalGaps, short[] sizesOfHorizontalGaps)
        {
            char[] a1 = s1.ToArray();
            char[] a2 = s2.ToArray();

            float[,] scores = m.Scores;

            int n = s2.Length + 1;

            Alignment alignment = new Alignment();
            alignment.Score = cell.Score;

            int maxlen = s1.Length + s2.Length; // maximum length after the
            // aligned sequences

            char[] reversed1 = new char[maxlen]; // reversed sequence #1
            char[] reversed2 = new char[maxlen]; // reversed sequence #2
            char[] reversed3 = new char[maxlen]; // reversed markup

            int len1 = 0; // length of sequence #1 after alignment
            int len2 = 0; // length of sequence #2 after alignment
            int len3 = 0; // length of the markup line

            int identity = 0; // count of identitcal pairs
            int similarity = 0; // count of similar pairs
            int gaps = 0; // count of gaps

            char c1, c2;

            int i = cell.Row; // traceback start row
            int j = cell.Column; // traceback start col
            int k = i * n;

            bool stillGoing = true; // traceback flag: true -> continue & false
            // -> stop

            while (stillGoing)
            {
                switch (pointers[k + j])
                {
                    case Directions.UP:

                        for (int l = 0, len = sizesOfVerticalGaps[k + j]; l < len; l++)
                        {
                            reversed1[len1++] = a1[--i];
                            reversed2[len2++] = Alignment.GAP;
                            reversed3[len3++] = Markups.GAP;
                            k -= n;
                            gaps++;
                        }
                        break;

                    case Directions.DIAGONAL:
                        c1 = a1[--i];
                        c2 = a2[--j];
                        k -= n;
                        reversed1[len1++] = c1;
                        reversed2[len2++] = c2;
                        if (c1 == c2)
                        {
                            reversed3[len3++] = Markups.IDENTITY;
                            identity++;
                            similarity++;
                        }
                        else if (scores[c1,c2] > 0)
                        {
                            reversed3[len3++] = Markups.SIMILARITY;
                            similarity++;
                        }
                        else
                        {
                            reversed3[len3++] = Markups.MISMATCH;
                        }
                        break;

                    case Directions.LEFT:
                        for (int l = 0, len = sizesOfHorizontalGaps[k + j]; l < len; l++)
                        {
                            reversed1[len1++] = Alignment.GAP;
                            reversed2[len2++] = a2[--j];
                            reversed3[len3++] = Markups.GAP;
                            gaps++;
                        }
                        break;

                    case Directions.STOP:
                        stillGoing = false;
                        break;
                }
            }

            alignment.Sequence1 = Reverse(reversed1, len1);
            alignment.Start1 = i;
            alignment.Sequence2 = Reverse(reversed2, len2);
            alignment.Start2 = j;
            alignment.MarkupLine = Reverse(reversed3, len3);
            alignment.Identity = identity;
            alignment.Gaps = gaps;
            alignment.Similarity = similarity;

            return alignment;
        }
        /// <summary> Constructs directions matrix for the traceback </summary>
        /// <param name="s1">sequence #1 </param>
        /// <param name="s2">sequence #2 </param>
        /// <param name="matrix">scoring matrix </param>
        /// <param name="o">open gap penalty </param>
        /// <param name="e">extend gap penalty </param>
        /// <returns> The cell where the traceback starts. </returns>
        private Cell Construct(Sequence s1, Sequence s2, float[,] matrix, float o,
			float e, byte[] pointers, short[] sizesOfVerticalGaps, short[] sizesOfHorizontalGaps)
        {
            char[] a1 = s1.ToArray();
            char[] a2 = s2.ToArray();

            int m = s1.Length + 1;
            int n = s2.Length + 1;

            float f; // score of alignment x1...xi to y1...yi if xi aligns to yi
            float[] g = new float[n]; // score if xi aligns to a gap after yi
            float h; // score if yi aligns to a gap after xi
            float[] v = new float[n]; // best score of alignment x1...xi to y1...yi
            float vDiagonal;

            g[0] = float.NegativeInfinity;
            h = float.NegativeInfinity;
            v[0] = 0;

            for (int j = 1; j < n; j++)
            {
                g[j] = float.NegativeInfinity;
                v[j] = 0;
            }

            float similarityScore, g1, g2, h1, h2;

            Cell cell = new Cell();

            for (int i = 1, k = n; i < m; i++, k += n)
            {
                h = float.NegativeInfinity;
                vDiagonal = v[0];
                for (int j = 1, l = k + 1; j < n; j++, l++)
                {
                    similarityScore = matrix[a1[i - 1], a2[j - 1]];

                    // Fill the matrices
                    f = vDiagonal + similarityScore;

                    g1 = g[j] - e;
                    g2 = v[j] - o;
                    if (g1 > g2)
                    {
                        g[j] = g1;
                        sizesOfVerticalGaps[l] = (short) (sizesOfVerticalGaps[l - n] + 1);
                    }
                    else
                    {
                        g[j] = g2;
                    }

                    h1 = h - e;
                    h2 = v[j - 1] - o;
                    if (h1 > h2)
                    {
                        h = h1;
                        sizesOfHorizontalGaps[l] = (short) (sizesOfHorizontalGaps[l - 1] + 1);
                    }
                    else
                    {
                        h = h2;
                    }

                    vDiagonal = v[j];
                    v[j] = Max(f, g[j], h, 0);

                    // Determine the traceback direction
                    if (v[j] == 0)
                    {
                        pointers[l] = Directions.STOP;
                    }
                    else if (v[j] == f)
                    {
                        pointers[l] = Directions.DIAGONAL;
                    }
                    else if (v[j] == g[j])
                    {
                        pointers[l] = Directions.UP;
                    }
                    else
                    {
                        pointers[l] = Directions.LEFT;
                    }

                    // Set the traceback start at the current cell i, j and score
                    if (v[j] > cell.Score)
                    {
                        cell.Set(i, j, v[j]);
                    }
                }
            }

            return cell;
        }