Ejemplo n.º 1
0
        /// <summary>
        /// Get the alignment using pair wise
        /// </summary>
        /// <param name="seq1">Sequence 1</param>
        /// <param name="seq2">Sequence 2</param>
        /// <returns>A list of sequence alignments.</returns>
        private IList <IPairwiseSequenceAlignment> RunPairWise(ISequence seq1, ISequence seq2)
        {
            IList <IPairwiseSequenceAlignment> sequenceAlignment = null;

            if (PairWiseAlgorithm == null)
            {
                PairWiseAlgorithm = new NeedlemanWunschAligner();
            }

            PairWiseAlgorithm.SimilarityMatrix  = SimilarityMatrix;
            PairWiseAlgorithm.GapOpenCost       = GapOpenCost;
            PairWiseAlgorithm.ConsensusResolver = this.ConsensusResolver;

            if (UseGapExtensionCost)
            {
                PairWiseAlgorithm.GapExtensionCost = GapExtensionCost;
                sequenceAlignment = PairWiseAlgorithm.Align(seq1, seq2);
            }
            else
            {
                sequenceAlignment = PairWiseAlgorithm.AlignSimple(seq1, seq2);
            }

            // MUMmer does not support other aligners. Throw exception.
            //throw new NotSupportedException(Properties.Resource.MUMmerIncompatibleAligner);

            return(sequenceAlignment);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method to align two sequences, this sample code uses NeedlemanWunschAligner.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence for alignment</param>
        /// <param name="querySequence">Query sequence for alignment</param>
        /// <returns>List of IPairwiseSequenceAlignment</returns>
        public static IList<IPairwiseSequenceAlignment> AlignSequences(ISequence referenceSequence, ISequence querySequence)
        {
            // Initialize the Aligner
            NeedlemanWunschAligner aligner = new NeedlemanWunschAligner();

            // Calling align method to do the alignment.
            return aligner.Align(referenceSequence, querySequence);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the MUMmer class.
        /// Constructor for all the pairwise aligner
        /// (NeedlemanWunsch, SmithWaterman, Overlap).
        /// Sets default similarity matrix and gap penalty.
        /// Users will typically reset these using parameters
        /// specific to their particular sequences
        /// and needs.
        /// </summary>
        protected MUMmer()
        {
            // Set default similarity matrix and gap penalty.
            // User will typically choose their own parameters, these
            // defaults are reasonable for many cases.

            // Default is set to 20
            LengthOfMUM = 20;

            SimilarityMatrix = null;

            GapOpenCost = -13; // 5, -4 diagonal matrix for Dna

            // default affine gap is -1
            GapExtensionCost = -8;

            // Set the default alignment algorithm to Smith Waterman
            PairWiseAlgorithm = new NeedlemanWunschAligner();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes static members of the SequenceAligners class.
        /// Static constructor
        /// </summary>
        static SequenceAligners()
        {
            NUCmer = new NucmerPairwiseAligner();
            MUMmer = new MUMmerAligner();
            NeedlemanWunsch = new NeedlemanWunschAligner();
            SmithWaterman = new SmithWatermanAligner();

            var knownAligners = new List<ISequenceAligner> { SmithWaterman, NeedlemanWunsch, MUMmer, NUCmer };

            // Get the registered aligners
            IEnumerable<ISequenceAligner> registeredAligners = GetAligners();
            if (null != registeredAligners)
            {
                knownAligners.AddRange(registeredAligners
                    .Where(aligner => aligner != null 
                        && knownAligners.All(sa => string.Compare(sa.Name, aligner.Name, StringComparison.OrdinalIgnoreCase) != 0)));
            }

            All = knownAligners;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes static members of the SequenceAligners class.
        /// Static constructor
        /// </summary>
        static SequenceAligners()
        {
            NUCmer          = new NucmerPairwiseAligner();
            MUMmer          = new MUMmerAligner();
            NeedlemanWunsch = new NeedlemanWunschAligner();
            SmithWaterman   = new SmithWatermanAligner();

            var knownAligners = new List <ISequenceAligner> {
                SmithWaterman, NeedlemanWunsch, MUMmer, NUCmer
            };

            // Get the registered aligners
            IEnumerable <ISequenceAligner> registeredAligners = GetAligners();

            if (null != registeredAligners)
            {
                knownAligners.AddRange(registeredAligners
                                       .Where(aligner => aligner != null &&
                                              knownAligners.All(sa => string.Compare(sa.Name, aligner.Name, StringComparison.OrdinalIgnoreCase) != 0)));
            }

            All = knownAligners;
        }
Ejemplo n.º 6
0
 public static void Test1BPDeletionCall()
 {
     string seq1seq = "ATACCCCTT";
     string seq2seq = "ATA-CCCTT".Replace("-", String.Empty);
     int[] seq2qual = new int[] { 30, 30, 30, 2, 30, 30, 30, 30 };
     var refseq = new Sequence(AmbiguousDnaAlphabet.Instance, seq1seq, false);
     var query = new Sequence (AmbiguousDnaAlphabet.Instance, seq2seq, false);
     NeedlemanWunschAligner aligner = new NeedlemanWunschAligner ();
     var aln = aligner.Align (refseq, query).First();
     // Need to add in the QV Values.
     ConvertAlignedSequenceToQualSeq(aln, seq2qual);
     var variants = VariantCaller.CallVariants (aln);
     Assert.AreEqual (variants.Count, 1);
     var variant = variants.First ();
     Assert.AreEqual (2, variant.QV);
     Assert.AreEqual (2, variant.StartPosition);
     Assert.AreEqual (VariantType.INDEL, variant.Type);
     var vi = variant as IndelVariant;
     Assert.AreEqual ("C", vi.InsertedOrDeletedBases);
     Assert.AreEqual ('C', vi.HomopolymerBase);
     Assert.AreEqual (4, vi.HomopolymerLengthInReference);
     Assert.AreEqual (true, vi.InHomopolymer);
     Assert.AreEqual (vi.InsertionOrDeletion, IndelType.Deletion);
 }
Ejemplo n.º 7
0
        public AlignedSequence(Sequence read, Sequence reference, Sequence reference_rc)
        {
            // Align the read and reference
            var algo = new NeedlemanWunschAligner ();
            algo.GapOpenCost = -20;
            algo.GapExtensionCost = -19;
            algo.SimilarityMatrix = new DiagonalSimilarityMatrix (20, -20);
            var aln = algo.Align (read, reference).First ().AlignedSequences.First () as PairwiseAlignedSequence;
            var reverse_aln = algo.Align (read, reference_rc).First ().AlignedSequences.First () as PairwiseAlignedSequence;
            Sequence Ref, Query;
            Name = read.ID;
            if (aln.Score < reverse_aln.Score) {
                aln = reverse_aln;
                Query = (aln.Sequences [0] as Sequence).GetReverseComplementedSequence () as Sequence;
                Ref = (aln.Sequences [1] as Sequence).GetReverseComplementedSequence () as Sequence;
                Name = Name + "_RC";
            } else {
                Query = (aln.Sequences [0] as Sequence);
                Ref = (aln.Sequences [1] as Sequence);
            }

            // Now calculate the offsets
            OffSets = new int[reference.Count];
            int refBases = -1;
            for (int i = 0; i < Ref.Count; i++) {
                if (Ref [i] != '-') {
                    refBases++;
                    OffSets [refBases] = i;
                }
            }
            Sequence = Query.ConvertToString ();

            //Console.WriteLine (read.ID);
            //Console.WriteLine ((aln.Sequences [0] as Sequence).ConvertToString ());
            //Console.WriteLine ((aln.Sequences [1] as Sequence).ConvertToString ());
        }
Ejemplo n.º 8
0
        private void InValidateNeedlemanWunschAlignmentWithInvalidSimilarityMatrix(
            string nodeName, bool isTextFile, SimilarityMatrixInvalidTypes invalidType,
            AlignParameters additionalParameter, AlignmentType alignType)
        {
            Sequence aInput = null;
            Sequence bInput = null;

            IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                     Constants.AlphabetNameNode));
            if (isTextFile)
            {
                // Read the xml file for getting both the files for aligning.
                string firstInputFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                            Constants.FilePathNode1);
                string secondInputFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                             Constants.FilePathNode2);

                // Parse the files and get the sequence.
                var parseObjectForFile1 = new FastAParser();
                var parseObjectForFile2 = new FastAParser();
                ISequence inputSequence1 = parseObjectForFile1.Parse(firstInputFilePath).ElementAt(0);
                ISequence inputSequence2 = parseObjectForFile2.Parse(secondInputFilePath).ElementAt(0);

                // Create input sequence for sequence string in different cases.
                GetSequenceWithCaseType(new string(inputSequence1.Select(a => (char) a).ToArray()),
                                        new string(inputSequence2.Select(a => (char) a).ToArray()), alphabet,
                                        SequenceCaseType.LowerCase, out aInput, out bInput);
            }
            else
            {
                string firstInputSequence = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1);
                string secondInputSequence = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2);

                // Create input sequence for sequence string in different cases.
                GetSequenceWithCaseType(firstInputSequence, secondInputSequence, alphabet,
                                        SequenceCaseType.LowerCase, out aInput, out bInput);
            }

            // Create similarity matrix object for a invalid file.
            string blosumFilePath = this.GetSimilarityMatrixFileWithInvalidType(nodeName, invalidType);
            Exception actualExpection = null;

            // For invalid similarity matrix data format; exception will be thrown while instantiating
            SimilarityMatrix sm = null;
            try
            {
                if (invalidType != SimilarityMatrixInvalidTypes.NullSimilarityMatrix)
                {
                    sm = new SimilarityMatrix(new StreamReader(blosumFilePath));
                }
            }
            catch (InvalidDataException ex)
            {
                actualExpection = ex;
            }

            // For non matching similarity matrix exception will be thrown while alignment
            if (actualExpection == null)
            {
                int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode),
                                            null);

                int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                 Constants.GapExtensionCostNode), null);

                // Create NeedlemanWunschAligner instance and set its values.
                var needlemanWunschObj = new NeedlemanWunschAligner();
                if (additionalParameter != AlignParameters.AllParam)
                {
                    needlemanWunschObj.SimilarityMatrix = sm;
                    needlemanWunschObj.GapOpenCost = gapOpenCost;
                    needlemanWunschObj.GapExtensionCost = gapExtensionCost;
                }

                // Align the input sequences and catch the exception.
                switch (additionalParameter)
                {
                    case AlignParameters.AlignList:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(new List<ISequence> {aInput, bInput});
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(new List<ISequence> {aInput, bInput});
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                        }
                        break;
                    case AlignParameters.AlignTwo:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                        }
                        break;
                    case AlignParameters.AllParam:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(sm, gapOpenCost,
                                                             gapExtensionCost, aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(sm, gapOpenCost, aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualExpection = ex;
                                }
                                break;
                        }
                        break;
                    default:
                        break;
                }
            }

            // Validate that expected exception is thrown using error message.
            string expectedErrorMessage =
                this.GetExpectedErrorMeesageWithInvalidSimilarityMatrixType(nodeName, invalidType);
            Assert.AreEqual(expectedErrorMessage, actualExpection.Message);

            ApplicationLog.WriteLine(string.Concat(
                "NeedlemanWunschAligner P2 : Expected Error message is thrown ",
                expectedErrorMessage));
        }
Ejemplo n.º 9
0
        public void ValidateNeedlemanWunschAlignTwoSequencesWithAffineGap()
        {
            /* These tests are designed to verify the NeedlemanWunsh aligner behaves as expected, in particular
             * that it matches the EMBOSS Needle program.  
             * 
             * http://www.ebi.ac.uk/Tools/psa/emboss_needle/nucleotide.html
             * 
             * To run these tests, we can insert the sequences there and select options to set the "more options" to match
             * the options shown below.  Note that you need to set teh "EndGapPenalty" box to TRUE or the gaps at the 
             * end will not be scored appropriately
             */
            var seq1 = new Sequence (DnaAlphabet.Instance, "CAAAAGGGATTGCAAATGTTGGAGTGAATGCCATTACCTACCGGCTAGGAGGAGTAGTACAAAGGAGCTATTATCATATATTT");
            var seq2 = new Sequence (DnaAlphabet.Instance, "CATTATGTATAGGTTATCATGCGAACAATTCAACAGACACTGTAGACACAGTACTAGAAAAGAATGTAAC");
            var na = new NeedlemanWunschAligner ();
            na.GapOpenCost = -10;
            na.GapExtensionCost = -1;
            na.SimilarityMatrix = new SimilarityMatrix (SimilarityMatrix.StandardSimilarityMatrix.EDnaFull);

            var aln = na.Align (seq1, seq2).First ().PairwiseAlignedSequences.First ();
            Assert.AreEqual (32, aln.Score);
            Assert.AreEqual ("CAAAAGGGATTGCAAATGTTGGAGTGAATGCCATTACCTACCGGC----TAGGAGGAGTAGTACAAAGGAGCTAT-TATCATATATTT", aln.FirstSequence.ConvertToString ());
            // Note that EMBOSS puts the "GC" neighboring the first gap after but not before, but these are equivalent
            // from a scoring perspective.
            Assert.AreEqual ("CATTATGTATAGGTTATCATGC---GAA--CAATT--CAACAGACACTGTAGACACAGTACTAGAAAAGA---ATGTAAC--------", aln.SecondSequence.ConvertToString ());
            Assert.AreEqual (42, aln.Metadata ["SimilarityCount"]);


            // Now let's verify the simple alignment is different from the affine alignment and is optima.
            na.GapOpenCost = -1;
            aln = na.AlignSimple (seq1, seq2).First().PairwiseAlignedSequences.First();
            Assert.AreEqual (183, aln.Score); // Needle reports a score of 181

            /* Again, we don't have an exact match here.  Part of this is due to
            the placement of equivalent scores .NET Bio tends to not left align,
            but part is also due to the end state.  NEEDLE appears to prevent a
            insertion followed by a deletion at the very end but this is actually
            favorable.  It allows insertions to follow deletions earlier in the
            alignment, so I am not sure what the problem here is, likely a bug in
            NEEDLE.  As a result .NET Bio has a score of 183 to Needles score of
            181 due to the end change.

            This bug in the Needle program was reported to
            [email protected] on 12/6/2015 with the email below.

                    Hi,

                    There appears to be a bug in the needle program where it does not
                    always return the globally best alignment as it should.

                    This can be easily replicated using the web portal:

                    http://www.ebi.ac.uk/Tools/psa/emboss_needle/nucleotide.html

                    And entering the following options

                    Seq1 = CAAAAGGGATTGCAAATGTTGGAGTGAATGCCATTACCTACCGGCTAGGAGGAGTAGTACAA
                    AGGAGCTATTATCATATATTT
                    Seq2 = CATTATGTATAGGTTATCATGCGAACAATTCAACAGACACTGTAGACACAGTACTAGAAAAG
                    AATGTAAC
                    Matrix: DNAfull
                    GapOpen: 1
                    GapExtend: 1
                    EndGapPenalty: True
                    EndGapOpen: 1
                    EndGapExtend: 1

                    Which produces the following alignment with a score of 181

                    CAAAAGGGATTGCAAATGT-T GGAGTG--A--ATGC---C-ATT--ACCT--AC-C-GGCTAGGAGG-
                    |       |||    |||| |   || |  |  ||||   | |||  ||    || | |  || ||
                    C-------ATT----ATGTAT --AG-GTTATCATGCGAACAATTCAAC--AGACACTG--TA-GA--C

                    AGT-AGTAC-A-AAGG-AGCTATTATCA-TATATTT
                    |   ||||| | ||   ||  |  ||   || |  .
                    A--CAGTACTAGAA--AAG--A--AT--GTA-A——C

                    However, the optimal alignment has a score of 183.  The problem here
                    being that with this particular scoring, a mismatch should never
                    occur as the mismatch penalty (-4 in the matrix), is less than 2
                    times the gap open/extend penalty (-2).  This means that you should
                    always have a deletion followed by an insertion rather than a
                    mismatch.  Needle follows this rule for every position except the
                    last one, where for some reason it tacks on a mismatch at the end.
                    The alignment should have been:

                    CAAAAGGGATTGCAAATGT-T GGAGTG--A--ATGC---C-ATT--ACCT--AC-C-GGCTAGGAGG-
                    |       |||    |||| |   || |  |  ||||   | |||  ||    || | |  || ||
                    C-------ATT----ATGTAT --AG-GTTATCATGCGAACAATTCAAC--AGACACTG--TA-GA--C

                    AGT-AGTAC-A-AAGG-AGCTATTATCA-TATATTT-
                    |   ||||| | ||   ||  |  ||   || |
                    A--CAGTACTAGAA--AAG--A--AT--GTA-A——-C

                    Which gives the correct score of 183.  Note that by default needle
                    does not score end gaps at all, but with the —endweight option
                    enabled, it should have returned the alignment with a score of 183.

                    I don’t suppose anyone is able to fix this?

                    Warm wishes, Nigel


            I placed the sequences returned by either program here to show the
            difference explicitly. */
            //var needleExpect1 = "CAAAAGGGATTGCAAATGT-TGGAGTG--A--ATGC---C-ATT--ACCT--AC-C-GGCTAGGAGG-AGT-AGTAC-A-AAGG-AGCTATTATCA-TATATTT";
            //var needleExpect2 = "C-------ATT----ATGTAT--AG-GTTATCATGCGAACAATTCAAC--AGACACTG--TA-GA--CA--CAGTACTAGAA--AAG--A--AT--GTA-A--C";
            var netBioExpect1 = "CAAAAGGGATTGCAAATGT-T-GG--AGTG-AATGC---CA-TT-A-C---CTACC-GGCTAGGAGG-AGT-AGTAC-A-AAGGA-GCTATTATCA-TATATTT-";
            var netBioExpect2 = "CA-------TT--A--TGTATAGGTTA-T-CA-TGCGAACAATTCAACAGAC-AC-TG--TAG-A--CA--CAGTACTAGAA--AAG--A--AT--GTA-A---C";
                                     
            Assert.AreEqual (netBioExpect1, aln.FirstSequence.ConvertToString ());
            Assert.AreEqual (netBioExpect2, aln.SecondSequence.ConvertToString());

            /* Now verify that an affine gap with the penalties set to the same as the simple alignment
             * produces the same result */
            aln = na.Align (seq1, seq2).First().PairwiseAlignedSequences.First();
            Assert.AreEqual (183, aln.Score); // 
            Assert.AreEqual (netBioExpect1, aln.FirstSequence.ConvertToString ());
            Assert.AreEqual (netBioExpect2, aln.SecondSequence.ConvertToString());
        }
Ejemplo n.º 10
0
        public void NeedlemanWunschProteinSeqAffineGap()
        {
            IPairwiseSequenceAligner nw = new NeedlemanWunschAligner
            {
                SimilarityMatrix = new SimilarityMatrix(SimilarityMatrix.StandardSimilarityMatrix.Blosum62),
                GapOpenCost = -8,
                GapExtensionCost = -1
            };

            ISequence sequence1 = new Sequence(Alphabets.Protein, "HEAGAWGHEE");
            ISequence sequence2 = new Sequence(Alphabets.Protein, "PAWHEAE");
            IList<IPairwiseSequenceAlignment> result = nw.Align(sequence1, sequence2);
            AlignmentHelpers.LogResult(nw, result);

            IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>();
            IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment();
            align.PairwiseAlignedSequences.Add(new PairwiseAlignedSequence
            {
                FirstSequence = new Sequence(Alphabets.Protein,  "HEAGAWGHE-E"),
                SecondSequence = new Sequence(Alphabets.Protein, "P---AW-HEAE"),
                Consensus = new Sequence(AmbiguousProteinAlphabet.Instance, "XEAGAWGHEAE"),
                Score = 5,
                FirstOffset = 0,
                SecondOffset = 0
            });
            expectedOutput.Add(align);

            Assert.IsTrue(AlignmentHelpers.CompareAlignment(result, expectedOutput));
        }
Ejemplo n.º 11
0
        public static void TestSNPCall()
        {
            string seq1seq = "ATCCCCCTT";
            string seq2seq = "ATCCCTCTT";
            int[] seq2qual = new int[] { 30, 30, 30, 30, 5, 3, 30, 30, 30 };
            var refseq = new Sequence(DnaAlphabet.Instance, seq1seq);
            var query = new Sequence (DnaAlphabet.Instance, seq2seq);

            NeedlemanWunschAligner aligner = new NeedlemanWunschAligner ();
            var aln = aligner.Align (refseq, query).First();
            ConvertAlignedSequenceToQualSeq (aln, seq2qual);
            var variants = VariantCaller.CallVariants (aln);
            Assert.AreEqual (variants.Count, 1);
            var variant = variants.First ();
            Assert.AreEqual (3, variant.QV);
            Assert.AreEqual (5, variant.StartPosition);
            Assert.AreEqual (variant.Type, VariantType.SNP);
            var vi = variant as SNPVariant;
            Assert.AreEqual (1, vi.Length);
            Assert.AreEqual ('T', vi.AltBP);
            Assert.AreEqual ('C', vi.RefBP);
            Assert.AreEqual (VariantType.SNP, vi.Type);
            Assert.AreEqual (false, vi.AtEndOfAlignment);
        } 
Ejemplo n.º 12
0
        public static void TestTrickyQVInversions() {
            // This will be hard because normally flip the QV value for a homopolymer, but in this case we won't. 
            // Note the whole notion of flipping is poorly defined.
            string seq1seq = "ATTGC";
            string seq2seq = "ATAGC";
            int[] seq2qual = new int[] { 30, 30, 2, 30, 30 };
            var refseq = new Sequence(DnaAlphabet.Instance, seq1seq);
            var query = new Sequence (DnaAlphabet.Instance, seq2seq);

            var s1rc = refseq.GetReverseComplementedSequence ();
            var s2rc = query.GetReverseComplementedSequence ();

            NeedlemanWunschAligner aligner = new NeedlemanWunschAligner ();
            var aln = aligner.Align (s1rc, s2rc).First();
            VariantCallTests.ConvertAlignedSequenceToQualSeq (aln, seq2qual.Reverse ().ToArray ());
            aln.PairwiseAlignedSequences [0].Sequences [1].MarkAsReverseComplement ();
            var variants = VariantCaller.CallVariants (aln);
            Assert.AreEqual (1, variants.Count);
            var variant = variants.First ();
            Assert.AreEqual (VariantType.SNP, variant.Type);
            Assert.AreEqual (2, variant.QV);

            var vs = variant as SNPVariant; 
            Assert.AreEqual ('T', vs.AltBP);
            Assert.AreEqual ('A', vs.RefBP);
        }
Ejemplo n.º 13
0
        public static void TestReverseComplement1BPIndelCall() {

            string seq1seq = "ATACCCCTTGCGC";
            string seq2seq = "ATA-CCCTTGCGC".Replace("-", String.Empty);
            int[] seq2qual = new int[] { 30, 30, 30, 2, 30, 30, 30, 30, 30, 30, 30, 30 };
            var refseq = new Sequence(DnaAlphabet.Instance, seq1seq);
            var query = new Sequence (DnaAlphabet.Instance, seq2seq);

            var s1rc = refseq.GetReverseComplementedSequence ();
            var s2rc = query.GetReverseComplementedSequence ();

            NeedlemanWunschAligner aligner = new NeedlemanWunschAligner ();
            var aln = aligner.Align (s1rc, s2rc).First();
            VariantCallTests.ConvertAlignedSequenceToQualSeq (aln, seq2qual.Reverse ().ToArray ());
            aln.PairwiseAlignedSequences [0].Sequences [1].MarkAsReverseComplement ();
            var variants = VariantCaller.CallVariants (aln);
            Assert.AreEqual (variants.Count, 1);
            var variant = variants.First ();
            Assert.AreEqual (2, variant.QV);
            Assert.AreEqual (5, variant.StartPosition);
            Assert.AreEqual (VariantType.INDEL, variant.Type);
            var vi = variant as IndelVariant;
            Assert.AreEqual (IndelType.Deletion, vi.InsertionOrDeletion);
            Assert.AreEqual ('G', vi.HomopolymerBase);
            Assert.AreEqual (1, vi.Length);
            Assert.AreEqual (4, vi.HomopolymerLengthInReference);
            Assert.AreEqual (true, vi.InHomopolymer);
            Assert.AreEqual ("G", vi.InsertedOrDeletedBases);
            Assert.AreEqual (false, vi.AtEndOfAlignment);
            Assert.AreEqual (6, vi.EndPosition);
        }
Ejemplo n.º 14
0
        private void ValidateNeedlemanWunschAlignment(string nodeName, AlignParameters alignParam,
                                                      SimilarityMatrixParameters similarityMatrixParam,
                                                      AlignmentType alignType)
        {
            ISequence aInput, bInput;

            IAlphabet alphabet =
                Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode));

            // Parse the files and get the sequence.
            if (alignParam.ToString().Contains("Code"))
            {
                string sequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1);
                string sequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2);

                aInput = new Sequence(alphabet, sequence1);
                bInput = new Sequence(alphabet, sequence2);
            }
            else
            {
                // Read the xml file for getting both the files for aligning.
                string filePath1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode1);
                string filePath2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode2);
                var parseObjectForFile1 = new FastAParser { Alphabet = alphabet };
                ISequence originalSequence1 = parseObjectForFile1.Parse(filePath1).FirstOrDefault();
                Assert.IsNotNull(originalSequence1);
                aInput = new Sequence(alphabet, originalSequence1.ConvertToString());

                var parseObjectForFile2 = new FastAParser { Alphabet = alphabet };
                ISequence originalSequence2 = parseObjectForFile2.Parse(filePath2).FirstOrDefault();
                Assert.IsNotNull(originalSequence2);
                bInput = new Sequence(alphabet, originalSequence2.ConvertToString());
            }

            string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.BlosumFilePathNode);
            SimilarityMatrix sm;

            switch (similarityMatrixParam)
            {
                case SimilarityMatrixParameters.TextReader:
                    using (TextReader reader = new StreamReader(blosumFilePath))
                        sm = new SimilarityMatrix(reader);
                    break;
                case SimilarityMatrixParameters.DiagonalMatrix:
                    string matchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                        Constants.MatchScoreNode);
                    string misMatchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                           Constants.MisMatchScoreNode);
                    sm = new DiagonalSimilarityMatrix(int.Parse(matchValue, null),
                                                      int.Parse(misMatchValue, null));
                    break;
                default:
                    sm = new SimilarityMatrix(new StreamReader(blosumFilePath));
                    break;
            }

            int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode), null);
            int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapExtensionCostNode),
                                             null);

            var needlemanWunschObj = new NeedlemanWunschAligner();
            if (AlignParameters.AllParam != alignParam)
            {
                needlemanWunschObj.SimilarityMatrix = sm;
                needlemanWunschObj.GapOpenCost = gapOpenCost;
            }

            IList<IPairwiseSequenceAlignment> result = null;

            switch (alignParam)
            {
                case AlignParameters.AlignList:
                case AlignParameters.AlignListCode:
                    var sequences = new List<ISequence> {aInput, bInput};
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            needlemanWunschObj.GapExtensionCost = gapExtensionCost;
                            result = needlemanWunschObj.Align(sequences);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(sequences);
                            break;
                    }
                    break;
                case AlignParameters.AllParam:
                case AlignParameters.AllParamCode:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            needlemanWunschObj.GapExtensionCost = gapExtensionCost;
                            result = needlemanWunschObj.Align(sm,
                                                              gapOpenCost, gapExtensionCost, aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(sm, gapOpenCost, aInput, bInput);
                            break;
                    }
                    break;
                case AlignParameters.AlignTwo:
                case AlignParameters.AlignTwoCode:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            needlemanWunschObj.GapExtensionCost = gapExtensionCost;
                            result = needlemanWunschObj.Align(aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(aInput, bInput);
                            break;
                    }
                    break;
                default:
                    break;
            }

            // Read the xml file for getting both the files for aligning.
            string expectedSequence1, expectedSequence2, expectedScore;

            switch (alignType)
            {
                case AlignmentType.Align:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionScoreNode);
                    expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                        Constants.ExpectedGapExtensionSequence1Node);
                    expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                        Constants.ExpectedGapExtensionSequence2Node);
                    break;
                default:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedScoreNode);
                    expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode1);
                    expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode2);
                    break;
            }

            IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>();

            IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment(aInput, bInput);
            var alignedSeq = new PairwiseAlignedSequence
                                 {
                                     FirstSequence = new Sequence(alphabet, expectedSequence1),
                                     SecondSequence = new Sequence(alphabet, expectedSequence2),
                                     Score = Convert.ToInt32(expectedScore, null)
                                 };
            align.PairwiseAlignedSequences.Add(alignedSeq);
            expectedOutput.Add(align);

            ApplicationLog.WriteLine(string.Format("NeedlemanWunschAligner P1 : Final Score '{0}'.", expectedScore));
            ApplicationLog.WriteLine(string.Format("NeedlemanWunschAligner P1 : Aligned First Sequence is '{0}'.", expectedSequence1));
            ApplicationLog.WriteLine(string.Format("NeedlemanWunschAligner P1 : Aligned Second Sequence is '{0}'.", expectedSequence2));

            Assert.IsTrue(CompareAlignment(result, expectedOutput));
        }
Ejemplo n.º 15
0
        private void ValidateNeedlemanWunschAlignment(bool isTextFile, AlignmentParamType alignParam,
                                                      AlignmentType alignType)
        {
            ISequence aInput, bInput;
            IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.AlphabetNameNode));

            if (isTextFile)
            {
                // Read the xml file for getting both the files for aligning.
                string filePath1 = this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.FilePathNode1);
                string filePath2 = this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.FilePathNode2);

                // Parse the files and get the sequence.
                var parseObjectForFile1 = new FastAParser { Alphabet = alphabet };
                aInput = parseObjectForFile1.Parse(filePath1).First();

                var parseObjectForFile2 = new FastAParser { Alphabet = alphabet };
                bInput = parseObjectForFile2.Parse(filePath2).First();
            }
            else
            {
                // Read the xml file for getting both the files for aligning.
                string origSequence1 = this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.SequenceNode1);
                string origSequence2 = this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.SequenceNode2);

                aInput = new Sequence(alphabet, origSequence1);
                bInput = new Sequence(alphabet, origSequence2);
            }

            string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.BlosumFilePathNode);

            var sm = new SimilarityMatrix(new StreamReader(blosumFilePath));
            int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.GapOpenCostNode), null);
            int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(Constants.NeedlemanWunschAlignAlgorithmNodeName, Constants.GapExtensionCostNode), null);

            var needlemanWunschObj = new NeedlemanWunschAligner();
            if (AlignmentParamType.AllParam != alignParam)
            {
                needlemanWunschObj.SimilarityMatrix = sm;
                needlemanWunschObj.GapOpenCost = gapOpenCost;
                needlemanWunschObj.GapExtensionCost = gapExtensionCost;
            }

            IList<IPairwiseSequenceAlignment> result = null;

            switch (alignParam)
            {
                case AlignmentParamType.AlignList:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(new List<ISequence> {aInput, bInput});
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(new List<ISequence> {aInput, bInput});
                            break;
                    }
                    break;
                case AlignmentParamType.AlignTwo:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(aInput, bInput);
                            break;
                    }
                    break;
                case AlignmentParamType.AllParam:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(
                                sm, gapOpenCost, gapExtensionCost, aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(sm, gapOpenCost, aInput, bInput);
                            break;
                    }
                    break;
                default:
                    break;
            }

            // Read the xml file for getting both the files for aligning.
            string expectedSequence1, expectedSequence2, expectedScore;

            switch (alignType)
            {
                case AlignmentType.Align:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedGapExtensionScoreNode);
                    expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedGapExtensionSequence1Node);
                    expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedGapExtensionSequence2Node);
                    break;
                default:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedScoreNode);
                    expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedSequenceNode1);
                    expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(
                        Constants.NeedlemanWunschAlignAlgorithmNodeName,
                        Constants.ExpectedSequenceNode2);
                    break;
            }

            IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>();

            IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment();
            var alignedSeq = new PairwiseAlignedSequence
                                 {
                                     FirstSequence = new Sequence(alphabet, expectedSequence1),
                                     SecondSequence = new Sequence(alphabet, expectedSequence2),
                                     Score = Convert.ToInt32(expectedScore, null),
                                     FirstOffset = Int32.MinValue,
                                     SecondOffset = Int32.MinValue,
                                 };
            align.PairwiseAlignedSequences.Add(alignedSeq);
            expectedOutput.Add(align);

            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner BVT : Final Score '{0}'.", expectedScore));
            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner BVT : Aligned First Sequence is '{0}'.", expectedSequence1));
            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner BVT : Aligned Second Sequence is '{0}'.", expectedSequence2));

            Assert.IsTrue(CompareAlignment(result, expectedOutput));
        }
Ejemplo n.º 16
0
        public void ValidateNeedlemanWunschAlignTwoSequencesWithEndGaps()
        {
            var exp_ref = "-ATTGTATGGCCAACAA-";
            var refseq= new Sequence (DnaAlphabet.Instance, exp_ref.Replace("-", ""));
            var query = new Sequence (DnaAlphabet.Instance, "CATTGTATGGCCAACAAG");
            NeedlemanWunschAligner alner = new NeedlemanWunschAligner();

            var res_affine = alner.Align (refseq, query).First().PairwiseAlignedSequences.First();
            Assert.AreEqual (query.Count, res_affine.FirstSequence.Count);
            Assert.AreEqual (query.Count, res_affine.SecondSequence.Count);
            Assert.AreEqual (exp_ref, res_affine.FirstSequence.ConvertToString ());

            var res_simple = alner.AlignSimple (refseq, query).First ().PairwiseAlignedSequences.First ();
            Assert.AreEqual (query.Count, res_simple.FirstSequence.Count);
            Assert.AreEqual (query.Count, res_simple.SecondSequence.Count);
            Assert.AreEqual (exp_ref, res_simple.FirstSequence.ConvertToString ());

            // now to flip sequence 1 and 2, try it again.
            res_affine = alner.Align (query, refseq).First().PairwiseAlignedSequences.First();
            Assert.AreEqual (query.Count, res_affine.FirstSequence.Count);
            Assert.AreEqual (query.Count, res_affine.SecondSequence.Count);
            Assert.AreEqual (exp_ref, res_affine.SecondSequence.ConvertToString ());

            res_simple = alner.AlignSimple (query, refseq).First ().PairwiseAlignedSequences.First ();
            Assert.AreEqual (query.Count, res_simple.FirstSequence.Count);
            Assert.AreEqual (query.Count, res_simple.SecondSequence.Count);
            Assert.AreEqual (exp_ref, res_simple.SecondSequence.ConvertToString ());

                
        }
Ejemplo n.º 17
0
        private void ValidateNeedlemanWunschAlignment(string nodeName, bool isTextFile, SequenceCaseType caseType,
                                                      AlignParameters additionalParameter, AlignmentType alignType,
                                                      SimilarityMatrixParameters similarityMatrixParam)
        {
            Sequence aInput, bInput;
            IAlphabet alphabet =
                Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode));

            if (isTextFile)
            {
                // Read the xml file for getting both the files for aligning.
                string filePath1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode1);
                string filePath2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode2);

                // Parse the files and get the sequence.

                var parseObjectForFile1 = new FastAParser { Alphabet = alphabet };
                var parseObjectForFile2 = new FastAParser { Alphabet = alphabet };
                ISequence originalSequence1 = parseObjectForFile1.Parse(filePath1).First();
                ISequence originalSequence2 = parseObjectForFile2.Parse(filePath2).First();

                // Create input sequence for sequence string in different cases.
                GetSequenceWithCaseType(originalSequence1.ConvertToString(),
                                        originalSequence2.ConvertToString(), alphabet, caseType, out aInput, out bInput);
            }
            else
            {
                string originalSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1);
                string originalSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2);

                // Create input sequence for sequence string in different cases.
                GetSequenceWithCaseType(
                    originalSequence1,
                    originalSequence2,
                    alphabet,
                    caseType,
                    out aInput,
                    out bInput);
            }

            // Create similarity matrix object for a given file.
            string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.BlosumFilePathNode);

            SimilarityMatrix sm;
            switch (similarityMatrixParam)
            {
                case SimilarityMatrixParameters.TextReader:
                    using (TextReader reader = new StreamReader(blosumFilePath))
                        sm = new SimilarityMatrix(reader);
                    break;
                case SimilarityMatrixParameters.DiagonalMatrix:
                    string matchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                        Constants.MatchScoreNode);
                    string misMatchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                           Constants.MisMatchScoreNode);
                    sm = new DiagonalSimilarityMatrix(int.Parse(matchValue, null),
                                                      int.Parse(misMatchValue, null));
                    break;
                default:
                    sm = new SimilarityMatrix(new StreamReader(blosumFilePath));
                    break;
            }

            int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode), null);
            int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapExtensionCostNode),
                                             null);

            // Create NeedlemanWunschAligner instance and set its values.
            var needlemanWunschObj = new NeedlemanWunschAligner();
            if (additionalParameter != AlignParameters.AllParam)
            {
                needlemanWunschObj.SimilarityMatrix = sm;
                needlemanWunschObj.GapOpenCost = gapOpenCost;
                needlemanWunschObj.GapExtensionCost = gapExtensionCost;
            }
            IList<IPairwiseSequenceAlignment> result = null;

            // Align the input sequences.
            switch (additionalParameter)
            {
                case AlignParameters.AlignList:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(new List<ISequence> {aInput, bInput});
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(new List<ISequence> {aInput, bInput});
                            break;
                    }
                    break;
                case AlignParameters.AlignTwo:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(aInput, bInput);
                            break;
                    }
                    break;
                case AlignParameters.AllParam:
                    switch (alignType)
                    {
                        case AlignmentType.Align:
                            result = needlemanWunschObj.Align(sm, gapOpenCost,
                                                              gapExtensionCost, aInput, bInput);
                            break;
                        default:
                            result = needlemanWunschObj.AlignSimple(sm, gapOpenCost, aInput, bInput);
                            break;
                    }
                    break;
                default:
                    break;
            }

            // Get the expected sequence and scorde from xml config.
            string expectedSequence1, expectedSequence2, expectedScore;

            switch (alignType)
            {
                case AlignmentType.Align:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionScoreNode);
                    switch (caseType)
                    {
                        case SequenceCaseType.LowerCase:
                            expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants
                                                                                    .ExpectedGapExtensionSequence1InLower);
                            expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants
                                                                                    .ExpectedGapExtensionSequence2InLower);
                            break;
                        default:
                            expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants
                                                                                    .ExpectedGapExtensionSequence1Node);
                            expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants
                                                                                    .ExpectedGapExtensionSequence2Node);
                            break;
                    }
                    break;
                default:
                    expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                    Constants.ExpectedScoreNode);
                    switch (caseType)
                    {
                        case SequenceCaseType.LowerCase:
                            expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequence1inLowerNode);
                            expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequence2inLowerNode);
                            break;
                        case SequenceCaseType.LowerUpperCase:
                            expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequence1inLowerNode);
                            expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequenceNode2);
                            break;
                        default:
                            expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequenceNode1);
                            expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                Constants.ExpectedSequenceNode2);
                            break;
                    }
                    break;
            }

            IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>();

            IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment();
            var alignedSeq = new PairwiseAlignedSequence
                                 {
                                     FirstSequence = new Sequence(alphabet, expectedSequence1),
                                     SecondSequence = new Sequence(alphabet, expectedSequence2),
                                     Score = Convert.ToInt32(expectedScore, null),
                                     FirstOffset = Int32.MinValue,
                                     SecondOffset = Int32.MinValue
                                 };
            align.PairwiseAlignedSequences.Add(alignedSeq);
            expectedOutput.Add(align);

            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner P2 : Final Score '{0}'.", expectedScore));
            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner P2 : Aligned First Sequence is '{0}'.",
                                                   expectedSequence1));
            ApplicationLog.WriteLine(string.Format(null, "NeedlemanWunschAligner P2 : Aligned Second Sequence is '{0}'.",
                                                   expectedSequence2));

            Assert.IsTrue(CompareAlignment(result, expectedOutput));
        }
Ejemplo n.º 18
0
        private void InValidateNeedlemanWunschAlignmentWithInvalidSequence(
            string nodeName, bool isTextFile, InvalidSequenceType invalidSequenceType,
            AlignParameters additionalParameter, AlignmentType alignType,
            InvalidSequenceType sequenceType)
        {
            IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName,Constants.AlphabetNameNode));
            Exception actualException = null;
            Sequence aInput = null;

            if (isTextFile)
            {
                // Read the xml file for getting both the files for aligning.
                string filepath = this.GetInputFileNameWithInvalidType(nodeName, invalidSequenceType);

                // Create input sequence for sequence string in different cases.
                try
                {
                    // Parse the files and get the sequence.
                    var parser = new FastAParser { Alphabet = alphabet };
                    var sequence = parser.Parse(filepath).First();
                    aInput = new Sequence(alphabet, sequence.ConvertToString());
                }
                catch (Exception ex)
                {
                    actualException = ex;
                }
            }
            else
            {
                string originalSequence = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.InvalidSequence1);

                // Create input sequence for sequence string in different cases.
                try
                {
                    aInput = new Sequence(alphabet, originalSequence);
                }
                catch (ArgumentException ex)
                {
                    actualException = ex;
                }
            }

            if (null == actualException)
            {
                Sequence bInput = aInput;

                // Create similarity matrix object for a given file.
                string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.BlosumFilePathNode);
                var sm = new SimilarityMatrix(new StreamReader(blosumFilePath));

                int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode), null);
                int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapExtensionCostNode), null);

                // Create NeedlemanWunschAligner instance and set its values.
                var needlemanWunschObj = new NeedlemanWunschAligner();
                if (additionalParameter != AlignParameters.AllParam)
                {
                    needlemanWunschObj.SimilarityMatrix = sm;
                    needlemanWunschObj.GapOpenCost = gapOpenCost;
                    needlemanWunschObj.GapExtensionCost = gapExtensionCost;
                }

                // Align the input sequences and catch the exception.
                switch (additionalParameter)
                {
                    case AlignParameters.AlignList:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(new List<ISequence> {aInput, bInput});
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(new List<ISequence> {aInput, bInput});
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                        }
                        break;
                    case AlignParameters.AlignTwo:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                        }
                        break;
                    case AlignParameters.AllParam:
                        switch (alignType)
                        {
                            case AlignmentType.Align:
                                try
                                {
                                    needlemanWunschObj.Align(sm, gapOpenCost,
                                                             gapExtensionCost, aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                            default:
                                try
                                {
                                    needlemanWunschObj.AlignSimple(sm, gapOpenCost, aInput, bInput);
                                }
                                catch (ArgumentException ex)
                                {
                                    actualException = ex;
                                }
                                break;
                        }
                        break;
                    default:
                        break;
                }
            }

            // Validate Error messages for Invalid Sequence types.
            string expectedErrorMessage = this.GetExpectedErrorMeesageWithInvalidSequenceType(
                nodeName, sequenceType);

            Assert.AreEqual(expectedErrorMessage, actualException.Message);

            ApplicationLog.WriteLine(string.Concat(
                "NeedlemanWunschAligner P2 : Expected Error message is thrown ",
                expectedErrorMessage));
        }
Ejemplo n.º 19
0
        public void NeedlemanWunschDnaSeqSimpleGap()
        {
            IPairwiseSequenceAligner nw = new NeedlemanWunschAligner
            {
                SimilarityMatrix = new DiagonalSimilarityMatrix(2, -1),
                GapOpenCost = -2
            };

            ISequence sequence1 = new Sequence(Alphabets.DNA, "GAATTCAGTTA");
            ISequence sequence2 = new Sequence(Alphabets.DNA, "GGATCGA");
            IList<IPairwiseSequenceAlignment> result = nw.AlignSimple(sequence1, sequence2);
            AlignmentHelpers.LogResult(nw, result);

            IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>();
            IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment();
            align.PairwiseAlignedSequences.Add(new PairwiseAlignedSequence
            {
                FirstSequence = new Sequence(Alphabets.DNA, "GAATTCAGTTA"),
                SecondSequence = new Sequence(Alphabets.DNA, "GGAT-C-G--A"),
                Consensus = new Sequence(AmbiguousDnaAlphabet.Instance, "GRATTCAGTTA"),
                Score = 3,
                FirstOffset = 0,
                SecondOffset = 0
            });
            expectedOutput.Add(align);
            
            Assert.IsTrue(AlignmentHelpers.CompareAlignment(result, expectedOutput));
        }
Ejemplo n.º 20
0
 public void NeedlemanWunschThrowsExceptionWhenTooLarge()
 {
     // What size squared is too large?
     int seq_size = (int)Math.Sqrt ((double)Int32.MaxValue) + 5;
     byte[] seq = new byte[seq_size];
     // Now let's generate sequences of those size
     for (int i = 0; i < seq.Length; i++) {
         seq [i] = (byte)'A';
     }
     var seq1 = new Sequence (DnaAlphabet.Instance, seq, false);
     var na = new NeedlemanWunschAligner ();
     Assert.Throws(typeof(ArgumentOutOfRangeException),
         () => na.Align(seq1, seq1));
 }