Ejemplo n.º 1
0
        static TDistance ComputeDistance(PairwiseAlignment alignment)
        {
            TDistance result = 0;

            if (_alignmentType == AlignmentType.Nucleic)
            {
                switch (_distanceFunction)
                {
                case DistanceFunctionType.PercentIdentity:
                    result = (TDistance)(1.0f - alignment.PercentIdentity);
                    break;

                case DistanceFunctionType.Kimura2:
                    result = (TDistance)(alignment.ComputeKimuraDistance());
                    break;

                case DistanceFunctionType.JukesCantor:
                    result = (TDistance)(alignment.ComputeJukesCantorDistance());
                    break;
                }
            }
            else
            {
                result = (TDistance)(1.0f - alignment.PercentSimilarity);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary> Aligns two sequences by Smith-Waterman algorithm</summary>
        /// <param name="s1">sequene #1 </param>
        /// <param name="s2">sequene #2 </param>
        /// <param name="matrix">scoring matrix </param>
        /// <param name="o">open gap penalty </param>
        /// <param name="e">extend gap penalty </param>
        /// <returns> alignment object contains the two aligned sequences,
        /// the alignment score and alignment statistics</returns>
        /// <seealso cref="Sequence"/>
        /// <seealso cref="Matrix"/>
        public PairwiseAlignment Align(Sequence s1, Sequence s2, ScoringMatrix matrix, float gapOpenPenalty,
                                       float gapExtensionPenalty)
        {
            if (s1 == null)
            {
                Console.WriteLine("Error: S1 is null");
                throw new SmithWatermanGotohException("S1 is null");
            }

            if (s2 == null)
            {
                Console.WriteLine("Error: S2 is null");
                throw new SmithWatermanGotohException("S2 is null");
            }

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

            var pointers = new Directions[m * n];

            // Initializes the boundaries of the traceback matrix to STOP.
            for (int i = 0, k = 0; i < m; i++, k += n)
            {
                pointers[k] = Directions.STOP;
            }

            for (int j = 1; j < n; j++)
            {
                pointers[j] = Directions.STOP;
            }

            var sizesOfVerticalGaps   = new short[m * n];
            var sizesOfHorizontalGaps = new short[m * n];

            for (int i = 0, k = 0; i < m; i++, k += n)
            {
                for (int j = 0; j < n; j++)
                {
                    sizesOfVerticalGaps[k + j] = sizesOfHorizontalGaps[k + j] = 1;
                }
            }

            _scoringMatrix       = matrix;
            _gapOpenPenalty      = gapOpenPenalty;
            _gapExtensionPenalty = gapExtensionPenalty;


            Cell cell = Construct(s1, s2, pointers, sizesOfVerticalGaps, sizesOfHorizontalGaps);
            PairwiseAlignment alignment = Traceback(s1.Residues, s2.Residues, pointers, cell, sizesOfVerticalGaps,
                                                    sizesOfHorizontalGaps);

            alignment.Name1            = s1.Label;
            alignment.Name2            = s2.Label;
            alignment.GapOpenPenalty   = _gapOpenPenalty;
            alignment.GapExtendPenalty = _gapExtensionPenalty;
            alignment.Matrix           = matrix.Name;

            return(alignment);
        }
Ejemplo n.º 3
0
        static int ComputeBlock(Block block, PartialMatrix <TDistance> matrix, bool isDiagonal)
        {
            int count = 0;
            SmithWatermanGotoh aligner       = new SmithWatermanGotoh(_alignmentType);
            ScoringMatrix      scoringMatrix = ScoringMatrix.Load(_scoringMatrixName);

            if (isDiagonal)
            {
                for (int i = block.RowRange.StartIndex; i <= block.RowRange.EndIndex; i++)
                {
                    Sequence si = _sequences[i];

                    for (int j = block.ColumnRange.StartIndex; j < i; j++)
                    {
                        Sequence sj = _sequences[j];

                        PairwiseAlignment alignment = aligner.Align(si, sj, scoringMatrix, _gapOpen, _gapExtension);

                        if ((_writeAlignments) && (_writeAlignmentsWriter != null))
                        {
                            WriteAlignment(_writeAlignmentsWriter, alignment);
                        }

                        matrix[i, j] = matrix[j, i] = ComputeDistance(alignment);

                        count++;
                    }
                }
            }
            else
            {
                for (int i = block.RowRange.StartIndex; i <= block.RowRange.EndIndex; i++)
                {
                    Sequence si = _sequences[i];

                    for (int j = block.ColumnRange.StartIndex; j <= block.ColumnRange.EndIndex; j++)
                    {
                        Sequence sj = _sequences[j];

                        PairwiseAlignment alignment = aligner.Align(si, sj, scoringMatrix, _gapOpen, _gapExtension);

                        if ((_writeAlignments) && (_writeAlignmentsWriter != null))
                        {
                            WriteAlignment(_writeAlignmentsWriter, alignment);
                        }

                        matrix[i, j] = ComputeDistance(alignment);

                        count++;
                    }
                }
            }

            return(count);
        }
Ejemplo n.º 4
0
        static void WriteAlignment(StreamWriter writer, PairwiseAlignment alignment)
        {
            double kimeraDistance          = alignment.ComputeKimuraDistance();
            double jukesCantorDistance     = alignment.ComputeJukesCantorDistance();
            double percentIdentityDistance = alignment.PercentIdentity;

            writer.WriteLine(">{0} Kimera: {1}, JukesCantor: {2}, PercentIdentity: {3}", alignment.Name1, kimeraDistance.ToString("F5"), jukesCantorDistance.ToString("F5"), alignment.PercentIdentity.ToString("F5"));
            writer.WriteLine(alignment.Sequence1);
            writer.WriteLine(">{0} Kimera: {1}, JukesCantor: {2}, PercentIdentity: {3}", alignment.Name2, kimeraDistance.ToString("F5"), jukesCantorDistance.ToString("F5"), alignment.PercentIdentity.ToString("F5"));
            writer.WriteLine(alignment.Sequence2);
            writer.WriteLine();
        }
Ejemplo n.º 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 PairwiseAlignment Traceback(string s1, string s2, Directions[] pointers, Cell cell,
                                            short[] sizesOfVerticalGaps, short[] sizesOfHorizontalGaps)
        {
            int n = s2.Length + 1;

            var alignment = new PairwiseAlignment();

            alignment.Score = cell.Score;

            float[,] scores = _scoringMatrix.Scores;

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

            var reversed1 = new char[maxlen];   // reversed sequence #1
            var reversed2 = new char[maxlen];   // reversed sequence #2
            var 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++] = s1[--i];
                        reversed2[len2++] = PairwiseAlignment.GAP;
                        reversed3[len3++] = Markups.GAP;
                        k -= n;
                        gaps++;
                    }
                    break;

                case Directions.DIAGONAL:
                    c1 = s1[--i];
                    c2 = s2[--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++] = PairwiseAlignment.GAP;
                        reversed2[len2++] = s2[--j];
                        reversed3[len3++] = Markups.GAP;
                        gaps++;
                    }
                    break;

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


            alignment.Matrix           = _scoringMatrix.Name;
            alignment.Gaps             = gaps;
            alignment.GapOpenPenalty   = _gapOpenPenalty;
            alignment.GapExtendPenalty = _gapExtensionPenalty;
            alignment.Score            = cell.Score;
            alignment.Sequence1        = Reverse(reversed1, len1);
            alignment.MarkupLine       = Reverse(reversed3, len3);
            alignment.Sequence2        = Reverse(reversed2, len2);
            alignment.Start1           = i;
            alignment.Start2           = j;
            alignment.Identity         = identity;
            alignment.Similarity       = similarity;

            return(alignment);
        }