Пример #1
0
 /// <returns> FASTA format of the input alignment</returns>
 public override string Format(Alignment alignment)
 {
     System.Text.StringBuilder buffer = new System.Text.StringBuilder();
     System.Text.StringBuilder s1 = new System.Text.StringBuilder();
     System.Text.StringBuilder s2 = new System.Text.StringBuilder();
     s1.Append(alignment.Sequence1);
     s2.Append(alignment.Sequence2);
     buffer.Append(Format(new Sequence(s1.ToString(), alignment.Name1, "", SequenceType.Protein)));
     buffer.Append(Format(new Sequence(s2.ToString(), alignment.Name2, "", SequenceType.Protein)));
     return buffer.ToString();
 }
        /// <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;
        }
Пример #3
0
 /// <summary> Returns CLUSTAL format of the alignment</summary>
 public override string Format(Alignment alignment)
 {
     string[] sequences = new string[] {new string(alignment.Sequence1), new string(alignment.Sequence2)};
     string[] names = new string[] {alignment.Name1, alignment.Name2};
     return Format(names, sequences);
 }
 /// <summary> Formats alignment</summary>
 /// <returns> formatted alignment </returns>
 public abstract string Format(Alignment alignment);
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <summary> Formats an alignment object to the Pair_FORMAT format</summary>
        /// <param name="alignment">alignment object to be formated </param>
        /// <returns> string of the alignment pair-formatted </returns>
        public override string Format(Alignment alignment)
        {
            char[] sequence1 = alignment.Sequence1;
            char[] sequence2 = alignment.Sequence2;
            char[] markup = alignment.MarkupLine;

            int length = sequence1.Length > sequence2.Length ? sequence2.Length : sequence1.Length;

            string name1 = AdjustName(alignment.Name1);
            string name2 = AdjustName(alignment.Name2);

            StringBuilder buffer = new StringBuilder();

            StringBuilder preMarkup = new StringBuilder();
            for (int j = 0; j < NAME_WIDTH + 1 + POSITION_WIDTH + 1; j++)
            {
                preMarkup.Append(BLANK);
            }

            int oldPosition1, position1 = 1 + alignment.Start1;
            int oldPosition2, position2 = 1 + alignment.Start2;

            char[] subsequence1;
            char[] subsequence2;
            char[] submarkup;
            int line;

            char c1, c2;

            for (int i = 0; i*SEQUENCE_WIDTH < length; i++)
            {
                oldPosition1 = position1;
                oldPosition2 = position2;

                line = ((i + 1)*SEQUENCE_WIDTH) < length ? (i + 1)*SEQUENCE_WIDTH : length;

                subsequence1 = new char[line - i*SEQUENCE_WIDTH];
                subsequence2 = new char[line - i*SEQUENCE_WIDTH];
                submarkup = new char[line - i*SEQUENCE_WIDTH];

                for (int j = i*SEQUENCE_WIDTH, k = 0; j < line; j++, k++)
                {
                    subsequence1[k] = sequence1[j];
                    subsequence2[k] = sequence2[j];
                    submarkup[k] = markup[j];
                    c1 = subsequence1[k];
                    c2 = subsequence2[k];
                    if (c1 == c2)
                    {
                        position1++;
                        position2++;
                    }
                    else if (c1 == Alignment.GAP)
                    {
                        position2++;
                    }
                    else if (c2 == Alignment.GAP)
                    {
                        position1++;
                    }
                    else
                    {
                        position1++;
                        position2++;
                    }
                }

                buffer.Append(name1);
                buffer.Append(BLANK);
                buffer.Append(AdjustPosition((oldPosition1).ToString()));
                buffer.Append(BLANK);
                buffer.Append(subsequence1);
                buffer.Append(BLANK);
                buffer.Append(AdjustPosition(((position1 - 1)).ToString()));
                buffer.Append(Commons.LineSeparator);

                buffer.Append(preMarkup);
                buffer.Append(submarkup);
                buffer.Append(Commons.LineSeparator);

                buffer.Append(name2);
                buffer.Append(BLANK);
                buffer.Append(AdjustPosition((oldPosition2).ToString()));
                buffer.Append(BLANK);
                buffer.Append(subsequence2);
                buffer.Append(BLANK);
                buffer.Append(AdjustPosition(((position2 - 1)).ToString()));
                buffer.Append(Commons.LineSeparator);

                buffer.Append(Commons.LineSeparator);
            }
            return buffer.ToString();
        }