Exemplo n.º 1
0
        public void TestLISWithCrossAndOverlap()
        {
            // Create a list of Mum classes.
            List <Match> MUM = new List <Match>();
            Match        mum;

            mum = new Match();
            mum.ReferenceSequenceOffset = 0;
            mum.Length = 5;
            mum.QuerySequenceOffset = 5;
            MUM.Add(mum);

            mum = new Match();
            mum.ReferenceSequenceOffset = 3;
            mum.Length = 5;
            mum.QuerySequenceOffset = 0;
            MUM.Add(mum);

            // ILongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
            LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
            IList <Match> lisList            = lis.SortMum(MUM);
            IList <Match> lisList1           = lis.GetLongestSequence(lisList);

            List <Match> expectedOutput = new List <Match>();

            mum = new Match();
            mum.ReferenceSequenceOffset = 0;
            mum.Length = 5;
            mum.QuerySequenceOffset = 5;
            expectedOutput.Add(mum);

            Assert.IsTrue(CompareMumList(lisList1, expectedOutput));
        }
Exemplo n.º 2
0
        public void FindMultipleLength()
        {
            var array  = new[] { 3, 6, 7, 12 };
            int result = LongestIncreasingSubsequence.FindMultipleLength(array);

            Assert.Equal(3, result);
        }
Exemplo n.º 3
0
        public void FindLength()
        {
            var array  = new[] { 7, 2, 1, 3, 8, 4, 9, 1, 2, 6, 5, 9, 3, 8, 1 };
            int result = LongestIncreasingSubsequence.FindLength(array);

            Assert.Equal(5, result);
        }
        public void TestRecursiveLIS()
        {
            var lisCount = LongestIncreasingSubsequence.LISRecursive(new[] { 10, 22, 9, 33, 21, 50, 41, 60 });

            Assert.IsTrue(lisCount == 5);

            lisCount = LongestIncreasingSubsequence.LIS_DP(new[] { 10, 22, 9, 33, 21, 50, 41, 60 });
            Assert.IsTrue(lisCount == 5);
        }
    public void Test3IncreasingNumbers()
    {
        var sequence    = new int[] { 1, 2, 3 };
        var expectedSeq = new int[] { 1, 2, 3 };
        var actualSeq   = LongestIncreasingSubsequence.
                          FindLongestIncreasingSubsequence(sequence);

        CollectionAssert.AreEqual(expectedSeq, actualSeq);
    }
    public void Test11Numbers()
    {
        var sequence    = new int[] { 3, 14, 5, 12, 15, 7, 8, 9, 11, 10, 1 };
        var expectedSeq = new int[] { 3, 5, 7, 8, 9, 11 };
        var actualSeq   = LongestIncreasingSubsequence.
                          FindLongestIncreasingSubsequence(sequence);

        CollectionAssert.AreEqual(expectedSeq, actualSeq);
    }
Exemplo n.º 7
0
        public void TestEqualNumbers()
        {
            var sequence    = new int[] { 1, 1, 1 };
            var expectedSeq = new int[] { 1 };
            var actualSeq   = LongestIncreasingSubsequence.
                              FindLongestIncreasingSubsequence(sequence);

            CollectionAssert.AreEqual(expectedSeq, actualSeq);
            Assert.AreEqual(1, actualSeq.Length);
        }
Exemplo n.º 8
0
        public void LongestIncreasingSubsequence_Length()
        {
            // Arrange
            int[] input = { 4, 7, 12, 45, 3, 21, 22, 65, 89, 9, 56, 2 };

            // Act
            var length = LongestIncreasingSubsequence.Length(input);

            // Assert
            Assert.AreEqual(7, length);
        }
        public void TestFindLongestIncreasingSubsequence()
        {
            IEnumerable <int> sub = LongestIncreasingSubsequence.FindLongestIncreasingSubsequence(
                new int[] { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 });

            Assert.AreEqual(6, sub.Count());

            Assert.IsTrue(sub.SequenceEqual(new int[] { 0, 2, 6, 9, 11, 15 }) ||
                          sub.SequenceEqual(new int[] { 0, 4, 6, 9, 11, 15 }) ||
                          sub.SequenceEqual(new int[] { 0, 4, 6, 9, 13, 15 }));
        }
    public void TestPerformance5000Numbers()
    {
        var sequence = Enumerable.Range(1, 5000).ToArray();

        sequence[500]  = 0;
        sequence[2000] = 0;
        sequence[4999] = 0;
        var actualSeq = LongestIncreasingSubsequence.
                        FindLongestIncreasingSubsequence(sequence);

        Assert.AreEqual(4997, actualSeq.Length);
    }
        public void longest_increasing_subsequenceTest()
        {
            var l = new LongestIncreasingSubsequence();

            Assert.AreEqual("[12,15,19]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 24, 12, 15, 15, 19 })));
            Assert.AreEqual("[5,19,28,29,83]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 5, 19, 5, 81, 50, 28, 29, 1, 83, 23 })));
            Assert.AreEqual("[2,3,7,8,18]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 2, 15, 3, 7, 8, 6, 18 })));
            Assert.AreEqual("[84]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 84 })));
            Assert.AreEqual("[14,19,47]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 96, 25, 14, 22, 19, 88, 3, 73, 47 })));
            Assert.AreEqual("[1,2,3]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 1, 2, 3 })));
            Assert.AreEqual("[1]", Util.ArrayToString(l.longest_increasing_subsequence(new[] { 3, 2, 1 })));
        }
Exemplo n.º 12
0
        public void Test23Numbers()
        {
            var sequence = new int[] { 3, 14, 5, 12, 15, 7, 8, 9, 11, 10,
                                       1, 12, 13, 14, 20, 15, 30, 16, 17, 40, 18, 19, 20 };
            var expectedSeq = new int[] { 3, 5, 7, 8, 9, 11, 12, 13, 14,
                                          15, 16, 17, 18, 19, 20 };
            var actualSeq = LongestIncreasingSubsequence.
                            FindLongestIncreasingSubsequence(sequence);

            CollectionAssert.AreEqual(expectedSeq, actualSeq);
            Assert.AreEqual(15, actualSeq.Length);
        }
Exemplo n.º 13
0
        public void TestLISWithCross1()
        {
            // Create a list of Mum classes.
            List <MaxUniqueMatch> MUM = new List <MaxUniqueMatch>();
            MaxUniqueMatch        mum = null;

            mum = new MaxUniqueMatch();
            mum.FirstSequenceStart    = 0;
            mum.FirstSequenceMumOrder = 1;
            mum.Length = 4;
            mum.SecondSequenceStart    = 4;
            mum.SecondSequenceMumOrder = 1;
            MUM.Add(mum);

            mum = new MaxUniqueMatch();
            mum.FirstSequenceStart    = 4;
            mum.FirstSequenceMumOrder = 2;
            mum.Length = 3;
            mum.SecondSequenceStart    = 0;
            mum.SecondSequenceMumOrder = 2;
            MUM.Add(mum);

            mum = new MaxUniqueMatch();
            mum.FirstSequenceStart    = 10;
            mum.FirstSequenceMumOrder = 3;
            mum.Length = 3;
            mum.SecondSequenceStart    = 10;
            mum.SecondSequenceMumOrder = 3;
            MUM.Add(mum);

            ILongestIncreasingSubsequence lis     = new LongestIncreasingSubsequence();
            IList <MaxUniqueMatch>        lisList = lis.GetLongestSequence(MUM);

            List <MaxUniqueMatch> expectedOutput = new List <MaxUniqueMatch>();

            mum = new MaxUniqueMatch();
            mum.FirstSequenceStart    = 0;
            mum.FirstSequenceMumOrder = 1;
            mum.Length = 4;
            mum.SecondSequenceStart    = 4;
            mum.SecondSequenceMumOrder = 1;
            expectedOutput.Add(mum);

            mum = new MaxUniqueMatch();
            mum.FirstSequenceStart    = 10;
            mum.FirstSequenceMumOrder = 3;
            mum.Length = 3;
            mum.SecondSequenceStart    = 10;
            mum.SecondSequenceMumOrder = 3;
            expectedOutput.Add(mum);

            Assert.IsTrue(this.CompareMumList(lisList, expectedOutput));
        }
Exemplo n.º 14
0
        public void LengthOfLISTests()
        {
            LongestIncreasingSubsequence obj = new LongestIncreasingSubsequence();

            var arr = new int[] { 0, 4, 12, 2, 10, 6, 9, 13, 3, 11, 7, 15 };
            var x   = obj.LengthOfLIS(arr);//6

            arr = new int[] { 10, 9, 2, 5, 3, 7, 101, 18 };
            x   = obj.LengthOfLIS(arr);//4

            arr = new int[] {};
            x   = obj.LengthOfLIS(arr);//0
        }
Exemplo n.º 15
0
        public void LengthOfLIS_ShouldReturn_LengthOfLongestIncreasingSubsequence(
            int[] nums,
            int expectedResult)
        {
            // Arrange
            var increasingSubsequence = new LongestIncreasingSubsequence();

            // Act
            var result = increasingSubsequence.LengthOfLIS(nums);

            // Assert
            result.Should().Be(expectedResult);
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            //int[] A = { 1, 2, 3, 4, 5, 10, 20 };
            //Console.WriteLine(TripletCount.countTriplets(A, A.Length));
            // Console.WriteLine(SubArraySum.printSubArraySumOptimized(A, A.Length, 33).ToString());

            //int[] A = { -1 ,-2, -3, -2, 5 };
            // Console.WriteLine(Kadane.maximumSubArraySum(A, A.Length));
            //Console.WriteLine(Kadane.maximumSubArraySumBruteForce(A, A.Length));
            //Console.WriteLine(MaximumSubArrayProduct.maxSubArrayProduct(A, A.Length));

            /*int[] A = { 1, 2, 3, 5 };
             *
             * Console.WriteLine(MissingNumberInArray.MissingNumberUsingXOR(A));
             * Console.WriteLine(MissingNumberInArray.MissingNumberUsingSum(A));
             */
            int[] A = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7 };
            Console.WriteLine(LongestIncreasingSubsequence.LongestIncreasingSubSequenceLengthBruteForce(A));

            Console.ReadKey();
        }
Exemplo n.º 17
0
        public void TestSortMum()
        {
            // Create a list of Mum classes.
            List <Match> MUM = new List <Match>();
            Match        mum;

            mum = new Match();
            mum.ReferenceSequenceOffset = 4;
            mum.Length = 3;
            mum.QuerySequenceOffset = 0;
            MUM.Add(mum);

            mum = new Match();
            mum.ReferenceSequenceOffset = 0;
            mum.Length = 3;
            mum.QuerySequenceOffset = 3;
            MUM.Add(mum);

            LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
            IList <Match> lisList            = lis.SortMum(MUM);

            List <Match> expectedOutput = new List <Match>();

            mum = new Match();
            mum.ReferenceSequenceOffset = 0;
            mum.Length = 3;
            mum.QuerySequenceOffset = 3;
            expectedOutput.Add(mum);

            mum = new Match();
            mum.ReferenceSequenceOffset = 4;
            mum.Length = 3;
            mum.QuerySequenceOffset = 0;
            expectedOutput.Add(mum);

            Assert.IsTrue(CompareMumList(lisList, expectedOutput));
        }
Exemplo n.º 18
0
        private static void Main(string[] args)
        {
            try
            {
                Stopwatch stopWatchMumUtil  = Stopwatch.StartNew();
                Stopwatch stopWatchInterval = new Stopwatch();
                Console.Error.WriteLine(SplashString());
                if (args.Length > 0)
                {
                    CommandLineOptions myArgs = ProcessCommandLine(args);

                    TimeSpan writetime = new TimeSpan();
                    LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
                    IEnumerable <Match>          mums;
                    if (myArgs.PerformLISOnly)
                    {
                        stopWatchInterval.Restart();
                        IList <Match> parsedMUMs = ParseMums(myArgs.FileList[0]);
                        stopWatchInterval.Stop();

                        if (myArgs.Verbose)
                        {
                            Console.Error.WriteLine();
                            Console.Error.WriteLine("  Processed MUM file: {0}", Path.GetFullPath(myArgs.FileList[0]));
                            Console.Error.WriteLine("        Total MUMs: {0:#,000}", parsedMUMs.Count);
                            Console.Error.WriteLine("            Read/Processing time: {0}", stopWatchInterval.Elapsed);
                        }

                        stopWatchInterval.Restart();
                        IList <Match> sortedMUMs = lis.SortMum(parsedMUMs);
                        stopWatchInterval.Stop();

                        if (myArgs.Verbose)
                        {
                            Console.Error.WriteLine();
                            Console.Error.WriteLine("  Sort MUM time: {0}", stopWatchInterval.Elapsed);
                        }

                        stopWatchInterval.Restart();
                        if (sortedMUMs.Count != 0)
                        {
                            mums = lis.GetLongestSequence(sortedMUMs);

                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine();
                                Console.Error.WriteLine("  Perform LIS time: {0}", stopWatchInterval.Elapsed);
                            }

                            stopWatchInterval.Restart();
                            WriteMums(mums);
                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine();
                                Console.Error.WriteLine("  Write MUM time: {0}", stopWatchInterval.Elapsed);
                            }

                            stopWatchMumUtil.Stop();
                        }
                        else
                        {
                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine();
                                Console.Error.WriteLine("  Perform LIS time: {0}", stopWatchInterval.Elapsed);
                            }

                            stopWatchInterval.Restart();
                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine();
                                Console.Error.WriteLine("  Write MUM time: {0}", stopWatchInterval.Elapsed);
                            }

                            stopWatchMumUtil.Stop();
                        }
                    }
                    else
                    {
                        FileInfo refFileinfo   = new FileInfo(myArgs.FileList[0]);
                        long     refFileLength = refFileinfo.Length;
                        refFileinfo = null;

                        stopWatchInterval.Restart();
                        IEnumerable <ISequence> referenceSequences = ParseFastA(myArgs.FileList[0]);
                        ISequence referenceSequence = referenceSequences.First();
                        stopWatchInterval.Stop();
                        if (myArgs.Verbose)
                        {
                            Console.Error.WriteLine();
                            Console.Error.WriteLine("  Processed Reference FastA file: {0}", Path.GetFullPath(myArgs.FileList[0]));
                            Console.Error.WriteLine("        Length of first Sequence: {0:#,000}", referenceSequence.Count);
                            Console.Error.WriteLine("            Read/Processing time: {0}", stopWatchInterval.Elapsed);
                            Console.Error.WriteLine("            File Size           : {0}", refFileLength);
                        }

                        FileInfo queryFileinfo   = new FileInfo(myArgs.FileList[1]);
                        long     queryFileLength = queryFileinfo.Length;
                        refFileinfo = null;

                        stopWatchInterval.Restart();
                        IEnumerable <ISequence> querySequences = ParseFastA(myArgs.FileList[1]);
                        stopWatchInterval.Stop();
                        if (myArgs.Verbose)
                        {
                            Console.Error.WriteLine();
                            Console.Error.WriteLine("      Processed Query FastA file: {0}", Path.GetFullPath(myArgs.FileList[1]));
                            Console.Error.WriteLine("            Read/Processing time: {0}", stopWatchInterval.Elapsed);
                            Console.Error.WriteLine("            File Size           : {0}", queryFileLength);
                        }

                        if (myArgs.ReverseOnly)
                        {
                            stopWatchInterval.Restart();
                            querySequences = ReverseComplementSequenceList(querySequences);
                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine("         Reverse Complement time: {0}", stopWatchInterval.Elapsed);
                            }
                        }
                        else if (myArgs.Both)
                        {   // add the reverse complement sequences to the query list too
                            stopWatchInterval.Restart();
                            querySequences = AddReverseComplementsToSequenceList(querySequences);
                            stopWatchInterval.Stop();
                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine("     Add Reverse Complement time: {0}", stopWatchInterval.Elapsed);
                            }
                        }

                        TimeSpan mummerTime = new TimeSpan(0, 0, 0);
                        stopWatchInterval.Restart();
                        Sequence seq = referenceSequence as Sequence;
                        if (seq == null)
                        {
                            throw new ArgumentException("MUMmer supports only Sequence class");
                        }

                        MUMmer mummer = new MUMmer(seq);
                        stopWatchInterval.Stop();
                        if (myArgs.Verbose)
                        {
                            Console.Error.WriteLine("Suffix tree construction time: {0}", stopWatchInterval.Elapsed);
                        }

                        mummer.LengthOfMUM = myArgs.Length;
                        mummer.NoAmbiguity = myArgs.NoAmbiguity;
                        long   querySeqCount  = 0;
                        double sumofSeqLength = 0;
                        if (myArgs.MaxMatch)
                        {
                            foreach (ISequence querySeq in querySequences)
                            {
                                stopWatchInterval.Restart();
                                IList <Match> mumList = GetMumsForLIS(mummer.GetMatchesUniqueInReference(querySeq));
                                if (mumList.Count != 0)
                                {
                                    mums = lis.GetLongestSequence(lis.SortMum(mumList));
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();
                                    WriteMums(mums, referenceSequence, querySeq, myArgs);
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                                else
                                {
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                            }

                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine("             Number of query Sequences: {0}", querySeqCount);
                                Console.Error.WriteLine("             Average length of query Sequences: {0}", sumofSeqLength / querySeqCount);
                                Console.Error.WriteLine("  Compute GetMumsMaxMatch() with LIS time: {0}", mummerTime);
                            }
                        }
                        else if (myArgs.Mum)
                        {
                            foreach (ISequence querySeq in querySequences)
                            {
                                stopWatchInterval.Restart();
                                IList <Match> mumList = GetMumsForLIS(mummer.GetMatchesUniqueInReference(querySeq));
                                if (mumList.Count != 0)
                                {
                                    mums = lis.GetLongestSequence(lis.SortMum(mumList));
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();
                                    WriteMums(mums, referenceSequence, querySeq, myArgs);
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                                else
                                {
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                            }

                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine("             Number of query Sequences: {0}", querySeqCount);
                                Console.Error.WriteLine("             Average length of query Sequences: {0}", sumofSeqLength / querySeqCount);
                                Console.Error.WriteLine("       Compute GetMumsMum() with LIS time: {0}", mummerTime);
                            }
                        }
                        else if (myArgs.Mumreference)
                        {
                            // NOTE:
                            //     mum3.GetMUMs() this really implements the GetMumReference() functionality
                            // mums = mum3.GetMumsReference( referenceSequences[0], querySequences);     // should be
                            foreach (ISequence querySeq in querySequences)
                            {
                                stopWatchInterval.Restart();
                                IList <Match> mumList = GetMumsForLIS(mummer.GetMatchesUniqueInReference(querySeq));
                                if (mumList.Count != 0)
                                {
                                    mums = lis.GetLongestSequence(lis.SortMum(mumList));
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();

                                    // do sort
                                    // WriteLongestIncreasingSubsequences
                                    WriteMums(mums, referenceSequence, querySeq, myArgs);
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                                else
                                {
                                    stopWatchInterval.Stop();
                                    mummerTime = mummerTime.Add(stopWatchInterval.Elapsed);
                                    stopWatchInterval.Restart();
                                    stopWatchInterval.Stop();
                                    writetime = writetime.Add(stopWatchInterval.Elapsed);
                                    querySeqCount++;
                                    sumofSeqLength += querySeq.Count;
                                }
                            }

                            if (myArgs.Verbose)
                            {
                                Console.Error.WriteLine("             Number of query Sequences: {0}", querySeqCount);
                                Console.Error.WriteLine("             Average length of query Sequences: {0}", sumofSeqLength / querySeqCount);
                                Console.Error.WriteLine(" Compute GetMumsReference() time: {0}", mummerTime);
                            }
                        }
                        else
                        {
                            // cannot happen as argument processing already asserted one of the three options must be specified
                            Console.Error.WriteLine("\nError: one of /maxmatch, /mum, /mumreference options must be specified.");
                            Environment.Exit(-1);

                            // kill the error about unitialized use of 'mums' in the next block...the compiler does not recognize
                            // Environment.Exit() as a no-return function
                            throw new Exception("Never hit this");
                        }
                    }

                    if (myArgs.Verbose)
                    {
                        Console.Error.WriteLine("                WriteMums() time: {0}", writetime);
                    }

                    stopWatchMumUtil.Stop();
                    if (myArgs.Verbose)
                    {
                        Console.Error.WriteLine("           Total LisUtil Runtime: {0}", stopWatchMumUtil.Elapsed);
                    }
                }
                else
                {
                    Console.WriteLine(Resources.LisUtilHelp);
                }
            }
            catch (Exception ex)
            {
                DisplayException(ex);
            }
        }
Exemplo n.º 19
0
 public void Setup()
 {
     solution = new LongestIncreasingSubsequence();
 }
Exemplo n.º 20
0
        /// <summary>
        /// This method is considered as main execute method which defines the
        /// step by step algorithm. Derived class flows the defined flow by this
        /// method. Store generated MUMs in properties MUMs, SortedMUMs.
        /// Alignment first finds MUMs for all the query sequence, and then
        /// runs pairwise algorithm on gaps to produce alignments.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence.</param>
        /// <param name="querySequenceList">List of input sequences.</param>
        /// <returns>A list of sequence alignments.</returns>
        private IList <IPairwiseSequenceAlignment> AlignmentWithAccumulatedMUMs(
            ISequence referenceSequence,
            IEnumerable <ISequence> querySequenceList)
        {
            // Get MUMs
            this.mums = new Dictionary <ISequence, IEnumerable <Match> >();
            IList <IPairwiseSequenceAlignment> results   = new List <IPairwiseSequenceAlignment>();
            IPairwiseSequenceAlignment         alignment = null;
            IEnumerable <Match> mum;

            if (this.Validate(referenceSequence, querySequenceList))
            {
                // Safety check for public methods to ensure that null
                // inputs are handled.
                if (referenceSequence == null || querySequenceList == null)
                {
                    return(null);
                }

                Sequence seq = referenceSequence as Sequence;
                if (seq == null)
                {
                    throw new ArgumentException(Properties.Resource.OnlySequenceClassSupported);
                }

                MUMmer mummer = new MUMmer(seq);
                mummer.LengthOfMUM = this.LengthOfMUM;
                mummer.NoAmbiguity = this.AmbigiousMatchesAllowed;
                foreach (ISequence sequence in querySequenceList)
                {
                    if (sequence.Equals(referenceSequence))
                    {
                        continue;
                    }

                    alignment = new PairwiseSequenceAlignment(referenceSequence, sequence);

                    // Step2 : streaming process is performed with the query sequence
                    if (this.MaximumMatchEnabled)
                    {
                        mum = mummer.GetMatches(sequence);
                    }
                    else
                    {
                        mum = mummer.GetMatchesUniqueInReference(sequence);
                    }

                    this.mums.Add(sequence, mum);

                    // Step3(a) : sorted mum list based on reference sequence
                    LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
                    IList <Match> sortedMumList      = lis.SortMum(GetMumsForLIS(mum));

                    if (sortedMumList.Count > 0)
                    {
                        // Step3(b) : LIS using greedy cover algorithm
                        IList <Match> finalMumList = lis.GetLongestSequence(sortedMumList);

                        if (finalMumList.Count > 0)
                        {
                            // Step 4 : get all the gaps in each sequence and call
                            // pairwise alignment
                            alignment.PairwiseAlignedSequences.Add(
                                this.ProcessGaps(referenceSequence, sequence, finalMumList));
                        }

                        results.Add(alignment);
                    }
                    else
                    {
                        IList <IPairwiseSequenceAlignment> sequenceAlignment = this.RunPairWise(
                            referenceSequence,
                            sequence);

                        foreach (IPairwiseSequenceAlignment pairwiseAlignment in sequenceAlignment)
                        {
                            results.Add(pairwiseAlignment);
                        }
                    }
                }
            }

            return(results);
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            Console.WriteLine("Start running Data Structures");

            /////----------Arrays-------------//
            Console.Write("Running Arrays problems");
            //ArrrayRotation arrrayRotation = new ArrrayRotation();
            //ArrayFindSumPair arrayFindSumPair = new ArrayFindSumPair();
            //Array_i__i array_I__ = new Array_i__i();
            //SecondOccurenceValue secondOccurenceValue = new SecondOccurenceValue();
            //FirstRepeatWordSentence firstRepeatWordSentence = new FirstRepeatWordSentence();

            /////----------LinkedLists-------------//
            //Console.Write("Running LinkedLists problems");
            //SimpleLinkedList simpleLinkedList = new SimpleLinkedList();

            ///-----------Math--------------//
            //Console.Write("\n Running Math problems");
            //EvenNumber evenNumber = new EvenNumber();
            //PrimeNumber primeNumber = new PrimeNumber();
            //OddNumber oddNumber = new OddNumber();
            //PalidroneNumber palidroneNumber = new PalidroneNumber();
            //RoundNumberMultiple roundNumberMultiple = new RoundNumberMultiple();

            /////----------Queues-------------//
            //Console.Write("Running Queues problems");
            //MaxSubArraysSize maxSubArraysSize = new MaxSubArraysSize();

            /////----------Search-------------//
            //Console.Write("Running Search problems");
            //BinarySearch binarySearch = new BinarySearch();
            //LinearSearch linearSearch = new LinearSearch();

            /////----------Sort--------------//
            //Console.Write("\n Running Sort problems");
            //BubbleSort bubbleSort = new BubbleSort();
            //InsertionSort insertSort = new InsertionSort();
            //SelectionSort selectSort = new SelectionSort();

            /////-----------Strings-----------//
            Console.Write("\n Running Strings problems");
            //PandigitalConcatenation pandigitalConcatenation = new PandigitalConcatenation();
            //StringCopy stringCopy = new StringCopy();
            //MakeCheckStringPalindrome makeCheckStringPalindrome = new MakeCheckStringPalindrome();
            //PalindroneString palindroneString = new PalindroneString();
            //CheckStringWheatherNumber checkStringWheatherNumber = new CheckStringWheatherNumber();
            //QuickCheckSameCharInString quickCheckSameCharInString = new QuickCheckSameCharInString();
            //RemovePunctuations removePunctuations = new RemovePunctuations();
            //InitialsName initialsName = new InitialsName();
            //WayFairInInterviewQuestion wayFairInInterviewQuestion = new WayFairInInterviewQuestion();
            //WayFairIn2 wayFairIn2 = new WayFairIn2();

            /////-----------Trees------------//
            //Console.Write("\n Running Trees problems");
            //BinaryTree binaryTree = new BinaryTree();

            /////-----------Amazon------------//
            Console.Write("\n Running Trees problems");

            /////-----------Amazon Recursion------------//
            //LetterCombinationsPhoneNumber letterCombinationsPhoneNumber = new LetterCombinationsPhoneNumber();
            //GenerateParentheses generateParentheses = new GenerateParentheses();
            //SingleWordDFS singleWordDFS = new SingleWordDFS();
            //LengthOfLongestSubstring lengthOfLongestSubstring = new LengthOfLongestSubstring();
            //MyAtoi myAtoi = new MyAtoi();
            LongestIncreasingSubsequence longestIncreasingSubsequence = new LongestIncreasingSubsequence();

            //PhoneDigits phoneDigits = new PhoneDigits();
        }
Exemplo n.º 22
0
        /// <summary>
        /// Validates most of the find matches suffix tree test cases with varying parameters.
        /// </summary>
        /// <param name="nodeName">Node name which needs to be read for execution.</param>
        /// <param name="isFilePath">Is File Path?</param>
        /// <param name="LISActionType">LIS action type enum</param>
        static void ValidateFindMatchSuffixGeneralTestCases(string nodeName, bool isFilePath,
                                                            LISParameters LISActionType)
        {
            ISequence referenceSeq      = null;
            ISequence querySeq          = null;
            string    referenceSequence = string.Empty;
            string    querySequence     = string.Empty;

            if (isFilePath)
            {
                // Gets the reference sequence from the configurtion file
                string filePath = Utility._xmlUtil.GetTextValue(nodeName,
                                                                Constants.FilePathNode);

                Assert.IsNotNull(filePath);
                ApplicationLog.WriteLine(string.Format(null,
                                                       "MUMmer BVT : Successfully validated the File Path '{0}'.", filePath));

                FastaParser       parser        = new FastaParser();
                IList <ISequence> referenceSeqs = parser.Parse(filePath);
                referenceSeq      = referenceSeqs[0];
                referenceSequence = referenceSeq.ToString();

                // Gets the reference sequence from the configurtion file
                string queryFilePath = Utility._xmlUtil.GetTextValue(nodeName,
                                                                     Constants.SearchSequenceFilePathNode);

                Assert.IsNotNull(queryFilePath);
                ApplicationLog.WriteLine(string.Format(null,
                                                       "MUMmer BVT : Successfully validated the Search File Path '{0}'.", queryFilePath));

                FastaParser       queryParser = new FastaParser();
                IList <ISequence> querySeqs   = queryParser.Parse(queryFilePath);
                querySeq      = querySeqs[0];
                querySequence = querySeq.ToString();
            }
            else
            {
                // Gets the reference sequence from the configurtion file
                referenceSequence = Utility._xmlUtil.GetTextValue(nodeName,
                                                                  Constants.SequenceNode);

                string seqAlp = Utility._xmlUtil.GetTextValue(nodeName,
                                                              Constants.AlphabetNameNode);

                referenceSeq = new Sequence(Utility.GetAlphabet(seqAlp),
                                            referenceSequence);

                querySequence = Utility._xmlUtil.GetTextValue(nodeName,
                                                              Constants.SearchSequenceNode);

                seqAlp = Utility._xmlUtil.GetTextValue(nodeName,
                                                       Constants.SearchSequenceAlphabetNode);

                querySeq = new Sequence(Utility.GetAlphabet(seqAlp),
                                        querySequence);
            }

            string mumLength = Utility._xmlUtil.GetTextValue(nodeName, Constants.MUMLengthNode);

            // Builds the suffix for the reference sequence passed.
            ISuffixTreeBuilder suffixTreeBuilder = new KurtzSuffixTreeBuilder();
            SequenceSuffixTree suffixTree        = suffixTreeBuilder.BuildSuffixTree(referenceSeq);

            IList <MaxUniqueMatch> matches = suffixTreeBuilder.FindMatches(suffixTree, querySeq,
                                                                           long.Parse(mumLength, null));

            switch (LISActionType)
            {
            case LISParameters.FindUniqueMatches:
                // Validates the Unique Matches.
                ApplicationLog.WriteLine("MUMmer BVT : Validating the Unique Matches");
                Assert.IsTrue(ValidateUniqueMatches(matches, nodeName, LISActionType));
                break;

            case LISParameters.PerformLIS:
                // Validates the Unique Matches.
                ApplicationLog.WriteLine("MUMmer BVT : Validating the Unique Matches using LIS");
                LongestIncreasingSubsequence lisObj     = new LongestIncreasingSubsequence();
                IList <MaxUniqueMatch>       lisMatches = lisObj.GetLongestSequence(matches);
                Assert.IsTrue(ValidateUniqueMatches(lisMatches, nodeName, LISActionType));
                break;

            default:
                break;
            }

            Console.WriteLine(string.Format(null,
                                            "MUMmer BVT : Successfully validated the all the unique matches for the sequence '{0}' and '{1}'.",
                                            referenceSequence, querySequence));
            ApplicationLog.WriteLine(string.Format(null,
                                                   "MUMmer BVT : Successfully validated the all the unique matches for the sequence '{0}' and '{1}'.",
                                                   referenceSequence, querySequence));
        }
Exemplo n.º 23
0
        public void LisListDynamic_Int_ReturnsExpected(IList <int> items, IEnumerable <IEnumerable <int> > expected)
        {
            var actual = LongestIncreasingSubsequence.LisListDynamic(items);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 24
0
        public void LisLengthDynamic_Int_ReturnsExpected(IList <int> items, int expected)
        {
            var actual = LongestIncreasingSubsequence.LisLengthDynamic(items);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 25
0
        public void CheckLongestIncreasingSubsequence(int[] array, int expectedResult)
        {
            var result = LongestIncreasingSubsequence.FindLisLength(array);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
 public int Test(int[] inputArray, int arraySize)
 {
     return(LongestIncreasingSubsequence
            .GetSizeOfLongestIncreasingSubsequence(inputArray, arraySize));
 }