Exemplo n.º 1
0
 public BaseCalledAllele()
 {
     Filters = new List <FilterType>();
     TotalCoverageByDirection = new int[Constants.NumDirectionTypes];
     StrandBiasResults        = new StrandBiasResults();
     SupportByDirection       = new int[Constants.NumDirectionTypes];
 }
Exemplo n.º 2
0
        /// <summary>
        ///     Assign a strandbias-score to a SNP.
        ///     (using only forward and reverse SNP counts.)
        /// </summary>
        private static StrandBiasResults CalculateStrandBiasResults(int[] coverageByStrandDirection,
                                                                    int[] supportByStrandDirection,
                                                                    int qNoise, double acceptanceCriteria, StrandBiasModel strandBiasModel)
        {
            var forwardSupport   = supportByStrandDirection[(int)DirectionType.Forward];
            var forwardCoverage  = coverageByStrandDirection[(int)DirectionType.Forward];
            var reverseSupport   = supportByStrandDirection[(int)DirectionType.Reverse];
            var reverseCoverage  = coverageByStrandDirection[(int)DirectionType.Reverse];
            var stitchedSupport  = supportByStrandDirection[(int)DirectionType.Stitched];
            var stitchedCoverage = coverageByStrandDirection[(int)DirectionType.Stitched];

            var errorRate = Math.Pow(10, -1 * qNoise / 10f);

            var overallStats = CreateStats(forwardSupport + reverseSupport + stitchedSupport,
                                           forwardCoverage + reverseCoverage + stitchedCoverage, errorRate, errorRate, strandBiasModel);
            var forwardStats = CreateStats(forwardSupport + stitchedSupport / 2,
                                           forwardCoverage + stitchedCoverage / 2,
                                           errorRate, errorRate, strandBiasModel);
            var reverseStats = CreateStats(reverseSupport + stitchedSupport / 2,
                                           reverseCoverage + stitchedCoverage / 2,
                                           errorRate, errorRate, strandBiasModel);

            var results = new StrandBiasResults
            {
                ForwardStats = forwardStats,
                ReverseStats = reverseStats,
                OverallStats = overallStats
            };

            results.StitchedStats = CreateStats(stitchedSupport, stitchedCoverage, errorRate, errorRate,
                                                strandBiasModel);

            var biasResults = AssignBiasScore(overallStats, forwardStats, reverseStats);

            results.BiasScore               = biasResults[0];
            results.GATKBiasScore           = biasResults[1];
            results.CovPresentOnBothStrands = ((forwardStats.Coverage > 0) && (reverseStats.Coverage > 0));
            results.VarPresentOnBothStrands = ((forwardStats.Support > 0) && (reverseStats.Support > 0));

            //not really fair to call it biased if coverage is in one direction..
            //its ambiguous if variant is found in only one direction.
            if (!results.CovPresentOnBothStrands)
            {
                results.BiasScore     = 0;
                results.GATKBiasScore = double.NegativeInfinity;
            }

            var testResults = MathOperations.GetTValue(forwardStats.Frequency, reverseStats.Frequency,
                                                       forwardStats.Coverage,
                                                       reverseStats.Coverage, acceptanceCriteria);

            results.TestScore      = testResults[0];
            results.TestAcceptable = ValueAcceptable(acceptanceCriteria, testResults[0], testResults[1]);
            results.BiasAcceptable = (results.BiasScore < acceptanceCriteria);

            return(results);
        }
Exemplo n.º 3
0
 public CalledAllele()
 {
     Filters = new List <FilterType>();
     EstimatedCoverageByDirection = new int[Constants.NumDirectionTypes];
     StrandBiasResults            = new StrandBiasResults();
     SupportByDirection           = new int[Constants.NumDirectionTypes];
     ReadCollapsedCounts          = new int[Constants.NumReadCollapsedTypes];
     Genotype = Genotype.HomozygousRef;
     Type     = AlleleCategory.Reference;
 }
Exemplo n.º 4
0
        public static CalledAllele DeepCopy(CalledAllele originalAllele)
        {
            if (originalAllele == null)
            {
                return(null);
            }

            var allele = new CalledAllele
            {
                Chromosome        = originalAllele.Chromosome,
                Coordinate        = originalAllele.Coordinate,
                Reference         = originalAllele.Reference,
                Alternate         = originalAllele.Alternate,
                Genotype          = originalAllele.Genotype,
                GenotypeQscore    = originalAllele.GenotypeQscore,
                VariantQscore     = originalAllele.VariantQscore,
                FractionNoCalls   = originalAllele.FractionNoCalls,
                NoiseLevelApplied = originalAllele.NoiseLevelApplied,
                TotalCoverage     = originalAllele.TotalCoverage,
                AlleleSupport     = originalAllele.AlleleSupport,
                ReferenceSupport  = originalAllele.ReferenceSupport,
                Type = originalAllele.Type,
                SupportByDirection           = new int[Constants.NumDirectionTypes],
                EstimatedCoverageByDirection = new int[Constants.NumDirectionTypes],
                ReadCollapsedCounts          = new int[Constants.NumReadCollapsedTypes],
                StrandBiasResults            = StrandBiasResults.DeepCopy(originalAllele.StrandBiasResults),
                Filters = new List <FilterType>()
            };

            foreach (var filter in originalAllele.Filters)
            {
                allele.Filters.Add(filter);
            }

            for (int i = 0; i < Constants.NumDirectionTypes; i++)
            {
                allele.SupportByDirection[i]           = originalAllele.SupportByDirection[i];
                allele.EstimatedCoverageByDirection[i] = originalAllele.EstimatedCoverageByDirection[i];
                allele.ReadCollapsedCounts[i]          = originalAllele.ReadCollapsedCounts[i];
            }
            return(allele);
        }
Exemplo n.º 5
0
        public static string StatsToString(StrandBiasResults stat)
        {
            var           delimiter = "\t";
            StringBuilder builder   = new StringBuilder();

            string[] statsData = new string[3 * 5];

            StringHelper(stat.OverallStats, statsData, 0, 3, delimiter);
            StringHelper(stat.ForwardStats, statsData, 1, 3, delimiter);
            StringHelper(stat.ReverseStats, statsData, 2, 3, delimiter);

            foreach (string t in statsData)
            {
                builder.Append(t);
            }

            //raw data
            //for (int i = 0; i < Constants.NumDirectionTypes; i++)
            //    builder.Append(stat.CoverageByStrandDirection[i] + "\t");
            builder.Append(stat.ForwardStats.Coverage + delimiter);
            builder.Append(stat.ReverseStats.Coverage + delimiter);
            builder.Append(stat.StitchedStats.Coverage + delimiter);

            //for (int i = 0; i < Constants.NumDirectionTypes; i++)
            //    builder.Append(SupportByStrandDirection[i] + "\t");
            builder.Append(stat.ForwardStats.Support + delimiter);
            builder.Append(stat.ReverseStats.Support + delimiter);
            builder.Append(stat.StitchedStats.Support + delimiter);

            //results

            builder.Append(stat.BiasScore + delimiter);
            builder.Append(stat.BiasAcceptable + delimiter);

            builder.Append(stat.VarPresentOnBothStrands + delimiter);
            builder.Append(stat.CovPresentOnBothStrands + delimiter);
            return(builder.ToString());
        }
Exemplo n.º 6
0
        public static StrandBiasResults DeepCopy(StrandBiasResults originalSBresults)
        {
            if (originalSBresults == null)
            {
                return(null);
            }

            var sb = new StrandBiasResults()
            {
                BiasAcceptable          = originalSBresults.BiasAcceptable,
                BiasScore               = originalSBresults.BiasScore,
                GATKBiasScore           = originalSBresults.GATKBiasScore,
                VarPresentOnBothStrands = originalSBresults.VarPresentOnBothStrands,
                CovPresentOnBothStrands = originalSBresults.CovPresentOnBothStrands,
                TestAcceptable          = originalSBresults.TestAcceptable,
                TestScore               = originalSBresults.TestScore,
                ForwardStats            = StrandBiasStats.DeepCopy(originalSBresults.ForwardStats),
                OverallStats            = StrandBiasStats.DeepCopy(originalSBresults.OverallStats),
                ReverseStats            = StrandBiasStats.DeepCopy(originalSBresults.ReverseStats),
                StitchedStats           = StrandBiasStats.DeepCopy(originalSBresults.StitchedStats),
            };

            return(sb);
        }
Exemplo n.º 7
0
        public static CalledAllele ConvertUnpackedVariant(VcfVariant v)
        {
            if (v == null)
            {
                return(null);
            }

            if (v.VariantAlleles.Count() > 1)
            {
                throw new ApplicationException("This method does not yet handle crushed vcf format.");
            }


            var genotypeQscore              = 0;
            var referenceSupport            = 0;
            var altSupport                  = 0;
            var genotypeString              = "";
            var isRef                       = ((v.VariantAlleles.Count() == 1) && v.VariantAlleles[0] == ".");
            var totalCoverage               = Int32.Parse(v.InfoFields["DP"]);
            var variantQuality              = v.Quality;
            var numAlts                     = 1;
            var noiseLevel                  = 1;
            var fractionNocalls             = 0f;
            var strandBiasInGATKScaleCoords = -100f;

            if (v.Genotypes.Count > 0)
            {
                if (v.Genotypes[0].ContainsKey("GQ"))
                {
                    genotypeQscore = Int32.Parse(v.Genotypes[0]["GQ"]);
                }
                else if (v.Genotypes[0].ContainsKey("GQX"))
                {
                    genotypeQscore = Int32.Parse(v.Genotypes[0]["GQX"]);
                }
                genotypeString = v.Genotypes[0]["GT"];

                if (v.Genotypes[0].ContainsKey("NL"))
                {
                    noiseLevel = Int32.Parse(v.Genotypes[0]["NL"]);
                }

                if (v.Genotypes[0].ContainsKey("NC"))
                {
                    fractionNocalls = float.Parse(v.Genotypes[0]["NC"]);
                }

                if (v.Genotypes[0].ContainsKey("SB"))
                {
                    strandBiasInGATKScaleCoords = float.Parse(v.Genotypes[0]["SB"]);
                }

                var ADstring = new string[] { "0", "0" };

                if (v.Genotypes[0].ContainsKey("AD"))
                {
                    ADstring = v.Genotypes[0]["AD"].Split(',');
                }

                var VFstring = new string[] { "0", "0" };
                if (v.Genotypes[0].ContainsKey("VF"))
                {
                    VFstring = v.Genotypes[0]["VF"].Split(',');
                }

                referenceSupport = int.Parse(ADstring[0]);
                altSupport       = isRef ? 0 : int.Parse(ADstring[1]);

                if (isRef)
                {
                    numAlts = 0;
                }
                else
                {
                    numAlts = VFstring.Length; //note this, method should never get a value here >1. yet.
                }
            }

            var alleleType = Map(v).Type;

            if (isRef)
            {
                alleleType = AlleleCategory.Reference;
            }

            var strandBiasResults = new StrandBiasResults();

            strandBiasResults.GATKBiasScore = strandBiasInGATKScaleCoords;

            var allele = new CalledAllele()
            {
                Chromosome        = v.ReferenceName,
                Coordinate        = v.ReferencePosition,
                Reference         = v.ReferenceAllele,
                Alternate         = v.VariantAlleles[0],
                TotalCoverage     = totalCoverage,
                AlleleSupport     = isRef ? referenceSupport : altSupport,
                ReferenceSupport  = referenceSupport,
                VariantQscore     = (int)variantQuality,
                GenotypeQscore    = genotypeQscore,
                Type              = alleleType,
                Genotype          = MapGTString(genotypeString, numAlts),
                Filters           = MapFilterString(v.Filters),
                NoiseLevelApplied = noiseLevel,
                StrandBiasResults = strandBiasResults,
                FractionNoCalls   = fractionNocalls
            };

            return(allele);
        }