Exemplo n.º 1
0
        public void CreateSimpleStatsAndVerifyAlphabetIsReturned()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "A");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            Assert.AreEqual(Alphabets.DNA, stats.Alphabet);
        }
Exemplo n.º 2
0
        static void DumpStatsForOneSequence()
        {
            ISequence oneSequence = null;

            var parser = SequenceParsers.FindParserByFileName(Filename);

            if (parser != null)
            {
                oneSequence = parser.Parse().First();
                parser.Close();
            }

            if (oneSequence == null)
            {
                Console.WriteLine("Could not load sequence.");
                return;
            }

            SequenceStatistics stats =
                new SequenceStatistics(oneSequence);

            foreach (var symbol in oneSequence.Alphabet)
            {
                Console.WriteLine("{0} = Count={1}, Fraction={2}",
                                  (char)symbol, stats.GetCount(symbol),
                                  stats.GetFraction(symbol));
            }
        }
Exemplo n.º 3
0
        public void CreateSimpleStatsAndVerifyCount()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            Assert.AreEqual(18, stats.TotalCount);
        }
Exemplo n.º 4
0
        public void CreateSimpleStatsAndVerifyAlphabetIsReturned()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "A");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            Assert.AreEqual(Alphabets.DNA, stats.Alphabet);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Iterate through each sequence and calculate the symbol content.
        /// </summary>
        public override void ContentBySequence()
        {
            if (this.Sequences == null)
            {
                throw new ArgumentNullException("Sequences");
            }

            //int i = 0;
            //foreach (var seqObj in this.sequences)
            Parallel.ForEach(this.Sequences, (seqObj, state, i) =>
            {
                if (Worker != null && WorkerArgs != null && Worker.CancellationPending)
                {
                    WorkerArgs.Cancel = true;
                    state.Stop();
                }

                // Instead of storing all SequenceStatistics objects, just calculate the required GC content values here.
                SequenceStatistics seqStats      = new SequenceStatistics(seqObj);
                double gcPercent                 = GCContentBySequence(seqStats, seqObj.Count);
                GCContentBySequenceArray[(int)i] = gcPercent;

                //i++;
            }
                             );
            HasRunContentBySequence = true;
        }
Exemplo n.º 6
0
        public void ValidateDnaSparseSequenceConstAlpIndexByteList()
        {
            byte[] byteArrayObj = encodingObj.GetBytes("AGCT");

            IEnumerable <byte> seqItems =
                new List <Byte>()
            {
                byteArrayObj[0], byteArrayObj[1], byteArrayObj[2], byteArrayObj[3]
            };

            SparseSequence sparseSeq = new SparseSequence(Alphabets.DNA, 4, seqItems);

            Assert.IsNotNull(sparseSeq);
            Assert.IsNotNull(sparseSeq.Statistics);
            Assert.AreEqual(8, sparseSeq.Count);
            SequenceStatistics seqStatObj = sparseSeq.Statistics;

            Assert.AreEqual(1, seqStatObj.GetCount('A'));
            Assert.AreEqual(1, seqStatObj.GetCount('G'));
            Assert.AreEqual(1, seqStatObj.GetCount('C'));
            Assert.AreEqual(1, seqStatObj.GetCount('T'));

            Console.WriteLine("SparseSequence BVT: Validation of SparseSequence(alp, index, seq items) constructor is completed");
            ApplicationLog.WriteLine("SparseSequence BVT: Validation of SparseSequence(alp, index, seq items) constructor is completed");
        }
Exemplo n.º 7
0
        public void CreateSimpleStatsAndVerifyCount()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            Assert.AreEqual(18, stats.TotalCount);
        }
Exemplo n.º 8
0
        public void ValidateSequenceStatisticsToString()
        {
            ISequence seq            = new Sequence(Alphabets.DNA, "ATCGATCG");
            var       seqStats       = new SequenceStatistics(seq);
            string    actualString   = seqStats.ToString();
            string    expectedString = "A - 2\r\nC - 2\r\nG - 2\r\nT - 2\r\n".Replace("\r\n", System.Environment.NewLine);

            Assert.AreEqual(actualString, expectedString);

            // Gets the expected sequence from the Xml
            List <ISequence>        seqsList;
            IEnumerable <ISequence> sequences = null;
            string filePath = this.utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                                   Constants.FilePathNode);

            using (var reader = File.OpenRead(filePath))
            {
                var parser = new FastAParser();
                {
                    parser.Alphabet = Alphabets.Protein;
                    sequences       = parser.Parse(reader);

                    //Create a list of sequences.
                    seqsList = sequences.ToList();
                }
            }

            foreach (ISequence Sequence in seqsList)
            {
                seqStats = new SequenceStatistics(Sequence);
                string seqStatStr = seqStats.ToString();
                Assert.IsTrue(seqStatStr.Contains(" - "));
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Calculate GC content for a read
        /// </summary>
        /// <param name="seqStats">SequenceStatistics object</param>
        /// <param name="length">The length of the read</param>
        /// <returns>GC percentage of a read</returns>
        public double GCContentBySequence(SequenceStatistics seqStats, long length)
        {
            if (seqStats == null)
            {
                throw new ArgumentNullException("seqStats");
            }

            return(100 * (double)(seqStats.GetCount('G') + seqStats.GetCount('C') + seqStats.GetCount('S')) / length);
        }
Exemplo n.º 10
0
        /// <summary>
        /// The execution method for the activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        /// <returns>The execution status.</returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            SequenceData = new string(Sequence.ToArray().Select(a => (char)a).ToArray());
            ID           = Sequence.ID;
            DisplayID    = Sequence.ID;
            Statistics   = new SequenceStatistics(Sequence);

            return(ActivityExecutionStatus.Closed);
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Main method
        /// </summary>
        public static void Main()
        {
            var array = new double[] { 3, 5, 2.12, 4, 23.1, 12, 3.32, 32, 14 };
            SequenceStatistics stat = new SequenceStatistics(array);

            stat.PrintAverageValue();
            stat.PrintMaxElement();
            stat.PrintMinElement();
        }
Exemplo n.º 12
0
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            SequenceData = Sequence.ToString();
            ID           = Sequence.ID;
            DisplayID    = Sequence.DisplayID;
            Statistics   = Sequence.Statistics;

            return(ActivityExecutionStatus.Closed);
        }
Exemplo n.º 13
0
        public void TestSequenceStatisticsToString()
        {
            ISequence          seq            = new Sequence(Alphabets.DNA, "ATCGATCG");
            SequenceStatistics seqStats       = new SequenceStatistics(seq);
            string             actualString   = seqStats.ToString();
            string             expectedString = "A - 2\r\nC - 2\r\nG - 2\r\nT - 2\r\n".Replace("\r\n", Environment.NewLine);

            Assert.AreEqual(actualString, expectedString);
        }
Exemplo n.º 14
0
    public ListSequence(IEnumerable <T> enumerable, T endSentinel)
    {
        Assert.ArgumentNotNull(enumerable, nameof(enumerable));
        _list             = enumerable is IReadOnlyList <T> list ? list : enumerable.ToList();
        _endSentinelValue = endSentinel;

        _index = 0;

        _stats = default;
    }
Exemplo n.º 15
0
        public void CreateStatsAndCheckToString()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            string expectedValue = "- - 6\r\nA - 3\r\nC - 3\r\nG - 3\r\nT - 3\r\n".Replace("\r\n", Environment.NewLine);
            string actualValue   = stats.ToString();

            Assert.AreEqual(expectedValue, actualValue);
        }
Exemplo n.º 16
0
    public ListSequence(IReadOnlyList <T> list, T endSentinel)
    {
        Assert.ArgumentNotNull(list, nameof(list));
        _list             = list;
        _endSentinelValue = endSentinel;

        _index = 0;

        _stats = default;
    }
Exemplo n.º 17
0
        private IEnumerable <TItem> BaseStatistics <TItem>(string fileName, string content,
                                                           Func <SequenceStatistics, TItem> selector)
        {
            var sequences = _sequenceProvider.Provide(fileName, content);

            return(sequences.ToList().Select(x =>
            {
                var stats = new SequenceStatistics(x);
                return selector(stats);
            }));
        }
Exemplo n.º 18
0
        /// <summary>
        /// When implemented in a derived class, performs the execution of the activity.
        /// </summary>
        /// <param name="context">The execution context under which the activity executes.</param>
        protected override string Execute(CodeActivityContext context)
        {
            var sequence = Sequence.Get(context);

            StringBuilder buff = new StringBuilder();

            buff.AppendLine(sequence.ID);
            buff.Append("Statistics: ");
            buff.Append(sequence.Count);
            buff.Append(" Total");

            var statistics = new SequenceStatistics(sequence);

            foreach (var symbol in sequence.Alphabet)
            {
                buff.AppendFormat(" - {0}:", (char)symbol);
                buff.Append(statistics.GetCount(symbol));
            }

            buff.AppendLine();
            buff.AppendLine();

            for (int i = 0; i < sequence.Count; i++)
            {
                if ((i % 50) == 0)
                {
                    string        num   = (i + 1).ToString(CultureInfo.InvariantCulture);
                    int           pad   = 5 - num.Length;
                    StringBuilder buff2 = new StringBuilder();
                    for (int j = 0; j < pad; j++)
                    {
                        buff2.Append(' ');
                    }
                    buff2.Append(num);
                    buff.Append(buff2);
                }

                if ((i % 10) == 0)
                {
                    buff.Append(' ');
                }

                buff.Append((char)sequence[i]);

                if ((i % 50) == 49)
                {
                    buff.AppendLine();
                }
            }
            buff.AppendLine();

            return(buff.ToString());
        }
Exemplo n.º 19
0
        public void ValidateDnaSparseSequenceConstAlpIndexByte()
        {
            byte[] byteArrayObj = Encoding.ASCII.GetBytes("AGCT");
            var    sparseSeq    = new SparseSequence(Alphabets.DNA, 1, byteArrayObj[0]);

            Assert.IsNotNull(sparseSeq);
            Assert.IsNotNull(sparseSeq.Statistics);
            SequenceStatistics seqStatObj = sparseSeq.Statistics;

            Assert.AreEqual(1, seqStatObj.GetCount('A'));
            ApplicationLog.WriteLine("SparseSequence BVT: Validation of SparseSequence(alp, index, byte) constructor is completed");
        }
Exemplo n.º 20
0
        public void CreateStatsWithMixedcaseLetterSequence()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "aAaAaAaA");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            Assert.AreEqual(8, stats.GetCount('A'));
            Assert.AreEqual(8, stats.GetCount(65));
            Assert.AreEqual(8, stats.GetCount('a'));

            Assert.AreEqual(1.0, stats.GetFraction('A'));
            Assert.AreEqual(1.0, stats.GetFraction(65));
            Assert.AreEqual(1.0, stats.GetFraction('a'));
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            var parser = SequenceParsers.FindParserByFileName(Filename);

            if (parser == null)
            {
                Console.WriteLine("No parser found.");
                return;
            }

            List <ISequence> allSequences = parser
                                            .Parse()
                                            .ToList();

            IAlphabet alphabet = allSequences[0].Alphabet;

            long totalColums = allSequences.Min(s => s.Count);

            //byte[] data = new byte[allSequences.Count];
            for (int column = 0; column < totalColums; column++)
            {
                //    for (int row = 0; row < allSequences.Count; row++)
                //        data[row] = allSequences[row][column];
                //ISequence newSequence = new Sequence(alphabet, data);

                ISequence newSequence = new Sequence(AmbiguousRnaAlphabet.Instance,
                                                     allSequences
                                                     .Select(s => s[column]).ToArray());
                SequenceStatistics stats =
                    new SequenceStatistics(newSequence);
                var TopCount =
                    alphabet
                    .Where(symbol => !alphabet.CheckIsGap(symbol))
                    .Select(symbol =>
                            new
                {
                    Symbol    = (char)symbol,
                    Count     = stats.GetCount(symbol),
                    Frequency = stats.GetFraction(symbol)
                })
                    .Where(tc => tc.Count > 0)
                    .OrderByDescending(c => c.Count)
                    .FirstOrDefault();

                if (TopCount != null)
                {
                    Console.WriteLine("{0}: {1} = {2} of {3}",
                                      column, TopCount.Symbol, TopCount.Count, totalColums);
                }
            }
        }
Exemplo n.º 22
0
        public void ValidateProteinSparseSequenceConstAlpIndexByte()
        {
            byte[]         byteArrayObj = encodingObj.GetBytes("KIEG");
            SparseSequence sparseSeq    = new SparseSequence(Alphabets.Protein, 1, byteArrayObj[0]);

            Assert.IsNotNull(sparseSeq);
            Assert.IsNotNull(sparseSeq.Statistics);
            SequenceStatistics seqStatObj = sparseSeq.Statistics;

            Assert.AreEqual(1, seqStatObj.GetCount('K'));

            Console.WriteLine("SparseSequence P1: Validation of SparseSequence(alp, index, byte) constructor is completed");
            ApplicationLog.WriteLine("SparseSequence P1: Validation of SparseSequence(alp, index, byte) constructor is completed");
        }
Exemplo n.º 23
0
        public void CreateSimpleStatsWithSingleLetterSequence()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "A");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            Assert.AreEqual(1, stats.GetCount('A'));
            Assert.AreEqual(1, stats.GetCount(65));
            Assert.AreEqual(0, stats.GetCount('C'));
            Assert.AreEqual(0, stats.GetCount('G'));
            Assert.AreEqual(0, stats.GetCount('T'));

            Assert.AreEqual(1.0, stats.GetFraction('A'));
            Assert.AreEqual(1.0, stats.GetFraction(65));
        }
Exemplo n.º 24
0
        public void CreateSimpleStatsWithSingleLetterSequence()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "A");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            Assert.AreEqual(1, stats.GetCount('A'));
            Assert.AreEqual(1, stats.GetCount(65));
            Assert.AreEqual(0, stats.GetCount('C'));
            Assert.AreEqual(0, stats.GetCount('G'));
            Assert.AreEqual(0, stats.GetCount('T'));

            Assert.AreEqual(1.0, stats.GetFraction('A'));
            Assert.AreEqual(1.0, stats.GetFraction(65));
        }
Exemplo n.º 25
0
        public void CreateStatsAndConsumeEnumerable()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            int loopCounts = 0;

            foreach (var value in stats.SymbolCounts)
            {
                Assert.AreEqual(value.Item2, stats.GetCount(value.Item1));
                loopCounts++;
            }

            Assert.AreEqual(5, loopCounts);
        }
Exemplo n.º 26
0
        public void CreateStatsWithSeveralMixedcaseLetterSequence()
        {
            ISequence          sequence = new Sequence(Alphabets.DNA, "a-c-g-t-A-C-G-T-");
            SequenceStatistics stats    = new SequenceStatistics(sequence);

            Assert.AreEqual(2, stats.GetCount('A'));
            Assert.AreEqual(2, stats.GetCount('a'));
            Assert.AreEqual(2, stats.GetCount('C'));
            Assert.AreEqual(2, stats.GetCount('c'));
            Assert.AreEqual(2, stats.GetCount('G'));
            Assert.AreEqual(2, stats.GetCount('g'));
            Assert.AreEqual(2, stats.GetCount('T'));
            Assert.AreEqual(2, stats.GetCount('t'));
            Assert.AreEqual(8, stats.GetCount('-'));

            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('A'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('C'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('G'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('T'));
            Assert.AreEqual(.5, stats.GetFraction('-'));
            Assert.AreEqual(.5, stats.GetFraction(45));
        }
Exemplo n.º 27
0
        public void ValidateProteinSparseSequenceConstAlpIndexByteList()
        {
            byte[] byteArrayObj = Encoding.ASCII.GetBytes("KIEG");

            IEnumerable <byte> seqItems =
                new List <Byte> {
                byteArrayObj[0], byteArrayObj[1], byteArrayObj[2], byteArrayObj[3]
            };

            var sparseSeq = new SparseSequence(Alphabets.Protein, 4, seqItems);

            Assert.IsNotNull(sparseSeq);
            Assert.IsNotNull(sparseSeq.Statistics);
            Assert.AreEqual(8, sparseSeq.Count);
            SequenceStatistics seqStatObj = sparseSeq.Statistics;

            Assert.AreEqual(1, seqStatObj.GetCount('K'));
            Assert.AreEqual(1, seqStatObj.GetCount('I'));
            Assert.AreEqual(1, seqStatObj.GetCount('E'));
            Assert.AreEqual(1, seqStatObj.GetCount('G'));

            ApplicationLog.WriteLine("SparseSequence P1: Validation of SparseSequence(alp, index, seq items) constructor is completed");
        }
Exemplo n.º 28
0
    public void testSobol()
    {
        //("Testing Sobol sequences up to dimension "
            //              + PPMT_MAX_DIM + "...");

            List<double> point;
            double tolerance = 1.0e-15;

            // testing max dimensionality
            int dimensionality =(int)SobolRsg.PPMT_MAX_DIM;
            ulong seed = 123456;
            SobolRsg rsg=new SobolRsg(dimensionality, seed);
            int points = 100, i;
            for (i=0; i<points; i++)
            {
                point = rsg.nextSequence().value;
                if (point.Count!=dimensionality) {
                    Assert.Fail("Sobol sequence generator returns "+
                                " a sequence of wrong dimensionality: " + point.Count
                                + " instead of  " + dimensionality);
                }
            }

            // testing homogeneity properties
            dimensionality = 33;
            seed = 123456;
            rsg = new SobolRsg(dimensionality, seed);
            SequenceStatistics stat=new SequenceStatistics(dimensionality);
            List<double> mean;
            int k = 0;
            for (int j=1; j<5; j++) { // five cycle
                points = (int)(Utils.Pow(2.0, j)-1); // base 2
                for (; k<points; k++) {
                    point = rsg.nextSequence().value;
                    stat.add(point);
                }
                mean = stat.mean();
                for (i=0; i<dimensionality; i++) {
                    double error = Math.Abs(mean[i]-0.5);
                    if (error > tolerance) {
                        Assert.Fail(i+1 + " dimension: "
                                   // + QL_FIXED
                                    + "mean (" + mean[i]
                                    + ") at the end of the " + j+1
                                    + " cycle in Sobol sequence is not " + 0.5
                                    //+ QL_SCIENTIFIC
                                    + " (error = " + error + ")");
                    }
                }
            }

            // testing first dimension (van der Corput sequence)
            double[]  vanderCorputSequenceModuloTwo= {
                // first cycle (zero excluded)
                0.50000,
                // second cycle
                0.75000, 0.25000,
                // third cycle
                0.37500, 0.87500, 0.62500, 0.12500,
                // fourth cycle
                0.18750, 0.68750, 0.93750, 0.43750, 0.31250, 0.81250, 0.56250, 0.06250,
                // fifth cycle
                0.09375, 0.59375, 0.84375, 0.34375, 0.46875, 0.96875, 0.71875, 0.21875,
                0.15625, 0.65625, 0.90625, 0.40625, 0.28125, 0.78125, 0.53125, 0.03125
            };

            dimensionality = 1;
            rsg = new SobolRsg(dimensionality);
            points = (int)(Utils.Pow(2.0, 5))-1; // five cycles
            for (i=0; i<points; i++) {
                point = rsg.nextSequence().value;
                double error =Math.Abs(point[0]-vanderCorputSequenceModuloTwo[i]);
                if (error > tolerance) {
                    Assert.Fail(i+1 + " draw ("
                                //+ QL_FIXED
                                + point[0]
                                + ") in 1-D Sobol sequence is not in the "
                                + "van der Corput sequence modulo two: "
                                + "it should have been "
                                + vanderCorputSequenceModuloTwo[i]
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }
    }
Exemplo n.º 29
0
    public void testHalton()
    {
        //("Testing Halton sequences...");

            List<double> point;
            double tolerance = 1.0e-15;

            // testing "high" dimensionality
            int dimensionality = (int)SobolRsg.PPMT_MAX_DIM;
            HaltonRsg rsg = new HaltonRsg(dimensionality, 0, false, false);
            int points = 100, i, k;
            for (i = 0; i < points; i++)
            {
                point = rsg.nextSequence().value;
                if (point.Count != dimensionality)
                {
                    Assert.Fail("Halton sequence generator returns "+
                    " a sequence of wrong dimensionality: " + point.Count
                    + " instead of  " + dimensionality)
                    ;
                }
            }

            // testing first and second dimension (van der Corput sequence)
            double[] vanderCorputSequenceModuloTwo = {
                                                         // first cycle (zero excluded)
                                                         0.50000,
                                                         // second cycle
                                                         0.25000, 0.75000,
                                                         // third cycle
                                                         0.12500, 0.62500, 0.37500, 0.87500,
                                                         // fourth cycle
                                                         0.06250, 0.56250, 0.31250, 0.81250, 0.18750, 0.68750, 0.43750,
                                                         0.93750,
                                                         // fifth cycle
                                                         0.03125, 0.53125, 0.28125, 0.78125, 0.15625, 0.65625, 0.40625,
                                                         0.90625,
                                                         0.09375, 0.59375, 0.34375, 0.84375, 0.21875, 0.71875, 0.46875,
                                                         0.96875,
                                                     };

            dimensionality = 1;
            rsg = new HaltonRsg(dimensionality, 0, false, false);
            points = (int) (Math.Pow(2.0, 5)) - 1; // five cycles
            for (i = 0; i < points; i++)
            {
                point = rsg.nextSequence().value;
                double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]);
                if (error > tolerance)
                {
                    Assert.Fail(i + 1 + " draw ("
                                + /*QL_FIXED*/ + point[0]
                                + ") in 1-D Halton sequence is not in the "
                                + "van der Corput sequence modulo two: "
                                + "it should have been "
                                + vanderCorputSequenceModuloTwo[i]
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }

            double[] vanderCorputSequenceModuloThree = {
                                                           // first cycle (zero excluded)
                                                           1.0/3, 2.0/3,
                                                           // second cycle
                                                           1.0/9, 4.0/9, 7.0/9, 2.0/9, 5.0/9, 8.0/9,
                                                           // third cycle
                                                           1.0/27, 10.0/27, 19.0/27, 4.0/27, 13.0/27, 22.0/27,
                                                           7.0/27, 16.0/27, 25.0/27, 2.0/27, 11.0/27, 20.0/27,
                                                           5.0/27, 14.0/27, 23.0/27, 8.0/27, 17.0/27, 26.0/27
                                                       };

            dimensionality = 2;
            rsg = new HaltonRsg(dimensionality, 0, false, false);
            points = (int) (Math.Pow(3.0, 3)) - 1; // three cycles of the higher dimension
            for (i = 0; i < points; i++)
            {
                point = rsg.nextSequence().value;
                double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]);
                if (error > tolerance)
                {
                    Assert.Fail("First component of " + i + 1
                                + " draw (" + /*QL_FIXED*/ + point[0]
                                + ") in 2-D Halton sequence is not in the "
                                + "van der Corput sequence modulo two: "
                                + "it should have been "
                                + vanderCorputSequenceModuloTwo[i]
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
                error = Math.Abs(point[1] - vanderCorputSequenceModuloThree[i]);
                if (error > tolerance)
                {
                    Assert.Fail("Second component of " + i + 1
                                + " draw (" + /*QL_FIXED*/ + point[1]
                                + ") in 2-D Halton sequence is not in the "
                                + "van der Corput sequence modulo three: "
                                + "it should have been "
                                + vanderCorputSequenceModuloThree[i]
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }

            // testing homogeneity properties
            dimensionality = 33;
            rsg = new HaltonRsg(dimensionality, 0, false, false);
            SequenceStatistics stat = new SequenceStatistics(dimensionality);
            List<double> mean; //, stdev, variance, skewness, kurtosis;
            k = 0;
            int j;
            for (j = 1; j < 5; j++)
            {
                // five cycle
                points = (int) (Math.Pow(2.0, j)) - 1; // base 2
                for (; k < points; k++)
                {
                    point = rsg.nextSequence().value;
                    stat.add(point);
                }
                mean = stat.mean();
                double error = Math.Abs(mean[0] - 0.5);
                if (error > tolerance)
                {
                    Assert.Fail("First dimension mean (" + /*QL_FIXED*/ + mean[0]
                                + ") at the end of the " + j + 1
                                + " cycle in Halton sequence is not " + 0.5
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }

            // reset generator and gaussianstatistics
            rsg = new HaltonRsg(dimensionality, 0, false, false);
            stat.reset(dimensionality);
            k = 0;
            for (j = 1; j < 3; j++)
            {
                // three cycle
                points = (int) (Math.Pow(3.0, j)) - 1; // base 3
                for (; k < points; k++)
                {
                    point = rsg.nextSequence().value;
                    stat.add(point);
                }
                mean = stat.mean();
                double error = Math.Abs(mean[1] - 0.5);
                if (error > tolerance)
                {
                    Assert.Fail("Second dimension mean (" + /*QL_FIXED*/ + mean[1]
                                + ") at the end of the " + j + 1
                                + " cycle in Halton sequence is not " + 0.5
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }
    }
Exemplo n.º 30
0
        /// <summary>
        /// The execution method for the activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        /// <returns>The execution status.</returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            StringBuilder buff = new StringBuilder();

            buff.AppendLine(Sequence.ID);
            buff.Append("Statistics: ");
            buff.Append(Sequence.Count);
            buff.Append(" Total");

            var statistics = new SequenceStatistics(Sequence);

            if (Sequence.Alphabet == Alphabets.DNA)
            {
                buff.Append(" - G: ");
                buff.Append(statistics.GetCount(Alphabets.DNA.G));
                buff.Append(" - A: ");
                buff.Append(statistics.GetCount(Alphabets.DNA.A));
                buff.Append(" - T: ");
                buff.Append(statistics.GetCount(Alphabets.DNA.T));
                buff.Append(" - C: ");
                buff.Append(statistics.GetCount(Alphabets.DNA.C));
            }
            else if (Sequence.Alphabet == Alphabets.RNA)
            {
                buff.Append(" - G: ");
                buff.Append(statistics.GetCount(Alphabets.RNA.G));
                buff.Append(" - A: ");
                buff.Append(statistics.GetCount(Alphabets.RNA.A));
                buff.Append(" - U: ");
                buff.Append(statistics.GetCount(Alphabets.RNA.U));
                buff.Append(" - C: ");
                buff.Append(statistics.GetCount(Alphabets.RNA.C));
            }

            buff.AppendLine();
            buff.AppendLine();

            for (int i = 0; i < Sequence.Count; i++)
            {
                if ((i % 50) == 0)
                {
                    string        num   = (i + 1).ToString();
                    int           pad   = 5 - num.Length;
                    StringBuilder buff2 = new StringBuilder();
                    for (int j = 0; j < pad; j++)
                    {
                        buff2.Append(' ');
                    }
                    buff2.Append(num);
                    buff.Append(buff2.ToString());
                }

                if ((i % 10) == 0)
                {
                    buff.Append(' ');
                }

                buff.Append((char)Sequence[i]);

                if ((i % 50) == 49)
                {
                    buff.AppendLine();
                }
            }
            buff.AppendLine();

            Data = buff.ToString();

            return(ActivityExecutionStatus.Closed);
        }
Exemplo n.º 31
0
        public void CreateStatsWithSeveralMixedcaseLetterSequence()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "a-c-g-t-A-C-G-T-");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            Assert.AreEqual(2, stats.GetCount('A'));
            Assert.AreEqual(2, stats.GetCount('a'));
            Assert.AreEqual(2, stats.GetCount('C'));
            Assert.AreEqual(2, stats.GetCount('c'));
            Assert.AreEqual(2, stats.GetCount('G'));
            Assert.AreEqual(2, stats.GetCount('g'));
            Assert.AreEqual(2, stats.GetCount('T'));
            Assert.AreEqual(2, stats.GetCount('t'));
            Assert.AreEqual(8, stats.GetCount('-'));

            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('A'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('C'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('G'));
            Assert.AreEqual(2.0 / 16.0, stats.GetFraction('T'));
            Assert.AreEqual(.5, stats.GetFraction('-'));
            Assert.AreEqual(.5, stats.GetFraction(45));
        }
Exemplo n.º 32
0
        public void CreateStatsAndConsumeEnumerable()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            int loopCounts = 0;
            foreach (var value in stats.SymbolCounts)
            {
                Assert.AreEqual(value.Item2, stats.GetCount(value.Item1));
                loopCounts++;
            }

            Assert.AreEqual(5,loopCounts);
        }
Exemplo n.º 33
0
        public void CreateStatsWithNullSequence()
        {
            var stats = new SequenceStatistics(null);

            Assert.IsNotNull(stats);
        }
Exemplo n.º 34
0
        public void CreateStatsWithMixedcaseLetterSequence()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "aAaAaAaA");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            Assert.AreEqual(8, stats.GetCount('A'));
            Assert.AreEqual(8, stats.GetCount(65));
            Assert.AreEqual(8, stats.GetCount('a'));

            Assert.AreEqual(1.0, stats.GetFraction('A'));
            Assert.AreEqual(1.0, stats.GetFraction(65));
            Assert.AreEqual(1.0, stats.GetFraction('a'));
        }
Exemplo n.º 35
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SequenceStatistics obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Exemplo n.º 36
0
        public void ValidateSequenceStatisticsToString()
        {
            ISequence seq = new Sequence(Alphabets.DNA, "ATCGATCG");
            var seqStats = new SequenceStatistics(seq);
            string actualString = seqStats.ToString();
            string expectedString = "A - 2\r\nC - 2\r\nG - 2\r\nT - 2\r\n".Replace("\r\n", System.Environment.NewLine);
            Assert.AreEqual(actualString, expectedString);

            // Gets the expected sequence from the Xml
            List<ISequence> seqsList;
            IEnumerable<ISequence> sequences = null;
            string filePath = this.utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                              Constants.FilePathNode);
            using (var reader = File.OpenRead(filePath))
            {
                var parser = new FastAParser();
                {
                    parser.Alphabet = Alphabets.Protein;
                    sequences = parser.Parse(reader);

                    //Create a list of sequences.
                    seqsList = sequences.ToList();
                }
            }

            foreach (ISequence Sequence in seqsList)
            {
                seqStats = new SequenceStatistics(Sequence);
                string seqStatStr = seqStats.ToString();
                Assert.IsTrue(seqStatStr.Contains(" - "));
            }
        }
Exemplo n.º 37
0
    public void testSobol()
    {
        //("Testing Sobol sequences up to dimension "
        //              + PPMT_MAX_DIM + "...");

        List <double> point;
        double        tolerance = 1.0e-15;

        // testing max dimensionality
        int      dimensionality = (int)SobolRsg.PPMT_MAX_DIM;
        ulong    seed = 123456;
        SobolRsg rsg = new SobolRsg(dimensionality, seed);
        int      points = 100, i;

        for (i = 0; i < points; i++)
        {
            point = rsg.nextSequence().value;
            if (point.Count != dimensionality)
            {
                Assert.Fail("Sobol sequence generator returns " +
                            " a sequence of wrong dimensionality: " + point.Count
                            + " instead of  " + dimensionality);
            }
        }

        // testing homogeneity properties
        dimensionality = 33;
        seed           = 123456;
        rsg            = new SobolRsg(dimensionality, seed);
        SequenceStatistics stat = new SequenceStatistics(dimensionality);
        List <double>      mean;
        int k = 0;

        for (int j = 1; j < 5; j++)                // five cycle
        {
            points = (int)(Utils.Pow(2.0, j) - 1); // base 2
            for (; k < points; k++)
            {
                point = rsg.nextSequence().value;
                stat.add(point);
            }
            mean = stat.mean();
            for (i = 0; i < dimensionality; i++)
            {
                double error = Math.Abs(mean[i] - 0.5);
                if (error > tolerance)
                {
                    Assert.Fail(i + 1 + " dimension: "
                                // + QL_FIXED
                                + "mean (" + mean[i]
                                + ") at the end of the " + j + 1
                                + " cycle in Sobol sequence is not " + 0.5
                                //+ QL_SCIENTIFIC
                                + " (error = " + error + ")");
                }
            }
        }

        // testing first dimension (van der Corput sequence)
        double[] vanderCorputSequenceModuloTwo =
        {
            // first cycle (zero excluded)
            0.50000,
            // second cycle
            0.75000, 0.25000,
            // third cycle
            0.37500, 0.87500, 0.62500, 0.12500,
            // fourth cycle
            0.18750, 0.68750, 0.93750, 0.43750, 0.31250, 0.81250, 0.56250, 0.06250,
            // fifth cycle
            0.09375, 0.59375, 0.84375, 0.34375, 0.46875, 0.96875, 0.71875, 0.21875,
            0.15625, 0.65625, 0.90625, 0.40625, 0.28125, 0.78125, 0.53125, 0.03125
        };

        dimensionality = 1;
        rsg            = new SobolRsg(dimensionality);
        points         = (int)(Utils.Pow(2.0, 5)) - 1; // five cycles
        for (i = 0; i < points; i++)
        {
            point = rsg.nextSequence().value;
            double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]);
            if (error > tolerance)
            {
                Assert.Fail(i + 1 + " draw ("
                            //+ QL_FIXED
                            + point[0]
                            + ") in 1-D Sobol sequence is not in the "
                            + "van der Corput sequence modulo two: "
                            + "it should have been "
                            + vanderCorputSequenceModuloTwo[i]
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
        }
    }
Exemplo n.º 38
0
    public void testHalton()
    {
        //("Testing Halton sequences...");

        List <double> point;
        double        tolerance = 1.0e-15;

        // testing "high" dimensionality
        int       dimensionality = (int)SobolRsg.PPMT_MAX_DIM;
        HaltonRsg rsg = new HaltonRsg(dimensionality, 0, false, false);
        int       points = 100, i, k;

        for (i = 0; i < points; i++)
        {
            point = rsg.nextSequence().value;
            if (point.Count != dimensionality)
            {
                Assert.Fail("Halton sequence generator returns " +
                            " a sequence of wrong dimensionality: " + point.Count
                            + " instead of  " + dimensionality)
                ;
            }
        }

        // testing first and second dimension (van der Corput sequence)
        double[] vanderCorputSequenceModuloTwo   =
        {
            // first cycle (zero excluded)
            0.50000,
            // second cycle
            0.25000, 0.75000,
            // third cycle
            0.12500, 0.62500, 0.37500, 0.87500,
            // fourth cycle
            0.06250, 0.56250, 0.31250, 0.81250, 0.18750, 0.68750, 0.43750,
            0.93750,
            // fifth cycle
            0.03125, 0.53125, 0.28125, 0.78125, 0.15625, 0.65625, 0.40625,
            0.90625,
            0.09375, 0.59375, 0.34375, 0.84375, 0.21875, 0.71875, 0.46875,
            0.96875,
        };

        dimensionality = 1;
        rsg            = new HaltonRsg(dimensionality, 0, false, false);
        points         = (int)(Math.Pow(2.0, 5)) - 1; // five cycles
        for (i = 0; i < points; i++)
        {
            point = rsg.nextSequence().value;
            double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]);
            if (error > tolerance)
            {
                Assert.Fail(i + 1 + " draw ("
                            + /*QL_FIXED*/ +point[0]
                            + ") in 1-D Halton sequence is not in the "
                            + "van der Corput sequence modulo two: "
                            + "it should have been "
                            + vanderCorputSequenceModuloTwo[i]
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
        }

        double[] vanderCorputSequenceModuloThree =
        {
            // first cycle (zero excluded)
            1.0 / 3,    2.0 / 3,
            // second cycle
            1.0 / 9,    4.0 / 9,   7.0 / 9,  2.0 / 9,   5.0 / 9,   8.0 / 9,
            // third cycle
            1.0 / 27, 10.0 / 27, 19.0 / 27, 4.0 / 27, 13.0 / 27, 22.0 / 27,
            7.0 / 27, 16.0 / 27, 25.0 / 27, 2.0 / 27, 11.0 / 27, 20.0 / 27,
            5.0 / 27, 14.0 / 27, 23.0 / 27, 8.0 / 27, 17.0 / 27, 26.0 / 27
        };

        dimensionality = 2;
        rsg            = new HaltonRsg(dimensionality, 0, false, false);
        points         = (int)(Math.Pow(3.0, 3)) - 1; // three cycles of the higher dimension
        for (i = 0; i < points; i++)
        {
            point = rsg.nextSequence().value;
            double error = Math.Abs(point[0] - vanderCorputSequenceModuloTwo[i]);
            if (error > tolerance)
            {
                Assert.Fail("First component of " + i + 1
                            + " draw (" + /*QL_FIXED*/ +point[0]
                            + ") in 2-D Halton sequence is not in the "
                            + "van der Corput sequence modulo two: "
                            + "it should have been "
                            + vanderCorputSequenceModuloTwo[i]
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
            error = Math.Abs(point[1] - vanderCorputSequenceModuloThree[i]);
            if (error > tolerance)
            {
                Assert.Fail("Second component of " + i + 1
                            + " draw (" + /*QL_FIXED*/ +point[1]
                            + ") in 2-D Halton sequence is not in the "
                            + "van der Corput sequence modulo three: "
                            + "it should have been "
                            + vanderCorputSequenceModuloThree[i]
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
        }

        // testing homogeneity properties
        dimensionality = 33;
        rsg            = new HaltonRsg(dimensionality, 0, false, false);
        SequenceStatistics stat = new SequenceStatistics(dimensionality);
        List <double>      mean; //, stdev, variance, skewness, kurtosis;

        k = 0;
        int j;

        for (j = 1; j < 5; j++)
        {
            // five cycle
            points = (int)(Math.Pow(2.0, j)) - 1;      // base 2
            for (; k < points; k++)
            {
                point = rsg.nextSequence().value;
                stat.add(point);
            }
            mean = stat.mean();
            double error = Math.Abs(mean[0] - 0.5);
            if (error > tolerance)
            {
                Assert.Fail("First dimension mean (" + /*QL_FIXED*/ +mean[0]
                            + ") at the end of the " + j + 1
                            + " cycle in Halton sequence is not " + 0.5
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
        }

        // reset generator and gaussianstatistics
        rsg = new HaltonRsg(dimensionality, 0, false, false);
        stat.reset(dimensionality);
        k = 0;
        for (j = 1; j < 3; j++)
        {
            // three cycle
            points = (int)(Math.Pow(3.0, j)) - 1;      // base 3
            for (; k < points; k++)
            {
                point = rsg.nextSequence().value;
                stat.add(point);
            }
            mean = stat.mean();
            double error = Math.Abs(mean[1] - 0.5);
            if (error > tolerance)
            {
                Assert.Fail("Second dimension mean (" + /*QL_FIXED*/ +mean[1]
                            + ") at the end of the " + j + 1
                            + " cycle in Halton sequence is not " + 0.5
                            //+ QL_SCIENTIFIC
                            + " (error = " + error + ")");
            }
        }
    }
Exemplo n.º 39
0
        public void CreateStatsAndCheckToString()
        {
            ISequence sequence = new Sequence(Alphabets.DNA, "ACGT--ACGT--ACGT--");
            SequenceStatistics stats = new SequenceStatistics(sequence);

            string expectedValue = "- - 6\r\nA - 3\r\nC - 3\r\nG - 3\r\nT - 3\r\n".Replace("\r\n", Environment.NewLine);
            string actualValue = stats.ToString();

            Assert.AreEqual(expectedValue, actualValue);
        }
Exemplo n.º 40
0
 public void TestSequenceStatisticsToString()
 {
     ISequence seq = new Sequence(Alphabets.DNA, "ATCGATCG");
     SequenceStatistics seqStats = new SequenceStatistics(seq);
     string actualString = seqStats.ToString();
     string expectedString = "A - 2\r\nC - 2\r\nG - 2\r\nT - 2\r\n".Replace("\r\n", Environment.NewLine);
     Assert.AreEqual(actualString, expectedString);
 }
Exemplo n.º 41
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SequenceStatistics obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }