Exemplo n.º 1
0
        public void ValidateProbability(
            [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p,
            [Values(new[] { 1, 9 }, new[] { 1, 3, 6 }, new[] { 1, 1, 1, 7 })] int[] x,
            [Values(0.121060821, 0.105815808, 0.000145152)] double res)
        {
            var b = new Multinomial(p, x.Sum());

            AssertHelpers.AlmostEqual(b.Probability(x), res, 12);
        }
        public void ValidateProbabilityLn(int[] x)
        {
            var b = new Multinomial(_largeP, x.Sum());

            AssertHelpers.AlmostEqualRelative(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
        }
        public void ValidateProbability(double[] p, int[] x, double res)
        {
            var b = new Multinomial(p, x.Sum());

            AssertHelpers.AlmostEqualRelative(b.Probability(x), res, 12);
        }
Exemplo n.º 4
0
 public void ValidateProbabilityLn(int[] x)
 {
     var b = new Multinomial(_largeP, x.Sum());
     AssertHelpers.AlmostEqualRelative(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
 }
Exemplo n.º 5
0
 public void ValidateProbability(double[] p, int[] x, double res)
 {
     var b = new Multinomial(p, x.Sum());
     AssertHelpers.AlmostEqualRelative(b.Probability(x), res, 12);
 }
Exemplo n.º 6
0
        public void ValidateProbabilityLn([Values(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new[] { 1, 1, 1, 2, 2, 2, 3, 3, 3 }, new[] { 5, 6, 7, 8, 7, 6, 5, 4, 3 })] int[] x)
        {
            var b = new Multinomial(_largeP, x.Sum());

            AssertHelpers.AlmostEqual(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Calculates the Q scores and phred-scaled genotype posteriors for a 1/2 locus.
        /// This method uses a multinomial distribution to calculate the Q score and posteriors for a 1/2 locus.  The probabilities
        /// of each class is estimated from the input models.
        /// </summary>
        /// <param name="ad">
        /// A three member int array that specifies the allele depths of the reference, AD1 and AD2, in that order.
        /// </param>
        /// <param name="dp">
        /// Total read depth at the locus.
        /// </param>
        /// <param name="means">
        /// An IList containing two members, each a double array specifying the means of the models of AD1 and AD2, in that order.
        /// Each array is three elements long.
        /// </param>
        /// <returns>
        /// A RecalibratedVariant object containing the genotype posteriors and Q scores.
        /// </returns>
        /// <remarks>
        /// The prior probability is specified as: homozygous reference-99%, uniform prior for the other possible genotypes.
        /// If total read depth is greater than 500, Q score and posteriors cannot be estimated so the maximal values are returned.
        /// </remarks>
        public static MixtureModelResult GetMultinomialQScores(int[] ad, int dp, IList <double[]> means)
        {
            if (dp > 500) // Can't be calculated
            {
                return new MixtureModelResult()
                       {
                           GenotypeCategory   = SimplifiedDiploidGenotype.HeterozygousAltRef,
                           QScore             = (int)_maxQScore,
                           GenotypePosteriors = new float[] { _maxQScore, _maxQScore, _maxQScore, _maxQScore, 0, _maxQScore }
                       }
            }
            ;

            double[] tempPosts = new double[6];
            int      postCount = 0;
            double   postNorm  = 0;

            for (int m2 = 0; m2 < means[1].Length; m2++)
            {
                for (int m1 = 0; m1 < means[0].Length; m1++)
                {
                    // Exclude when either allele is hom-alt and the other alelle is not hom-ref
                    if ((m1 == 2 && m2 != 0) ||
                        (m2 == 2 && m1 != 0))
                    {
                        continue;
                    }

                    var p = CalculateProbabilities(m1, m2, means);

                    Multinomial mm    = new Multinomial(p, dp);
                    double      prior = 0.01 / 5;
                    if (m1 == 0 && m2 == 0)
                    {
                        prior = 0.99;
                    }

                    tempPosts[postCount] = mm.Probability(ad) * prior;
                    postNorm             = postNorm + tempPosts[postCount];
                    postCount++;
                }
            }

            float[] gp = new float[tempPosts.Length];
            for (int i = 0; i < tempPosts.Length; i++)
            {
                gp[i] = Math.Min(_maxQScore, MathOperations.PToQ_CapAt300(tempPosts[i] / postNorm));
            }

            int qScore = Math.Min((int)_maxQScore, (int)Math.Round(MathOperations.PToQ_CapAt300(1 - tempPosts[4] / postNorm)));

            return(new MixtureModelResult()
            {
                GenotypeCategory = SimplifiedDiploidGenotype.HeterozygousAltRef,
                QScore = qScore,
                GenotypePosteriors = gp
            });

            // Helper method
            double[] CalculateProbabilities(int genotype1, int genotype2, IList <double[]> probs)
            {
                double[] result = new double[probs[0].Length];
                result[1] = probs[0][genotype1];
                result[2] = probs[1][genotype2];
                result[0] = 1 - result[1] - result[2];

                // Deal with when p is less than or equal to 0
                if (result[0] <= 0)
                {
                    if (genotype1 == 2)
                    {
                        result[0] = 1 - result[1];
                    }
                    else if (genotype2 == 2)
                    {
                        result[0] = 1 - result[2];
                    }
                    else if (genotype1 == 1 && genotype2 == 1)
                    {
                        result[0] = 1 - probs[0][2];
                    }
                }
                return(result);
            }
        }
Exemplo n.º 8
0
 public void ValidateProbabilityLn([Values(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new[] { 1, 1, 1, 2, 2, 2, 3, 3, 3 }, new[] { 5, 6, 7, 8, 7, 6, 5, 4, 3 })] int[] x)
 {
     var b = new Multinomial(_largeP, x.Sum());
     AssertHelpers.AlmostEqual(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
 }
Exemplo n.º 9
0
 public void ValidateProbability(
     [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p, 
     [Values(new[] { 1, 9 }, new[] { 1, 3, 6 }, new[] { 1, 1, 1, 7 })] int[] x, 
     [Values(0.121060821, 0.105815808, 0.000145152)] double res)
 {
     var b = new Multinomial(p, x.Sum());
     AssertHelpers.AlmostEqual(b.Probability(x), res, 12);
 }