Esempio n. 1
0
        public static string WriteCountsFile(string vcfIn, string outDir)
        {
            var variant    = new VcfVariant();
            var countsPath = Path.Combine(outDir, Path.GetFileName(vcfIn).Replace(".vcf", ".counts"));
            var counter    = new MutationCounter();

            using (VcfReader readerA = new VcfReader(vcfIn))
            {
                counter.StartWriter(countsPath);

                while (readerA.GetNextVariant(variant))
                {
                    try
                    {
                        counter.Add(variant);
                    }

                    catch (Exception ex)
                    {
                        Logger.WriteToLog(string.Format("Fatal error processing vcf; Check {0}, position {1}.  Exception: {2}",
                                                        variant.ReferenceName, variant.ReferencePosition, ex));
                        throw;
                    }
                }

                counter.CloseFalseCallsWriter();
            }

            return(countsPath);
        }
        private static TypeOfUpdateNeeded UpdateAllele(VcfConsumerAppOptions appOptions, QualityRecalibrationData recalibrationData, CalledAllele inAllele, out List <CalledAllele> outAlleles)
        {
            outAlleles = new List <CalledAllele> {
                inAllele
            };
            VQROptions         options        = (VQROptions)appOptions;
            var                cat            = MutationCounter.GetMutationCategory(inAllele);
            TypeOfUpdateNeeded updateHappened = TypeOfUpdateNeeded.NoChangeNeeded;

            if (options.DoBasicChecks && recalibrationData.BasicLookupTable.ContainsKey(cat))
            {
                UpdateVariantQScoreAndRefilter(options.MaxQScore, options.VariantCallingParams.MinimumVariantQScoreFilter, recalibrationData.BasicLookupTable, inAllele, cat, false);
                updateHappened = TypeOfUpdateNeeded.Modify;
            }

            if (options.DoAmpliconPositionChecks &&
                recalibrationData.AmpliconEdgeVariantsLookupTable.ContainsKey(cat) &&
                recalibrationData.AmpliconEdgeVariantsList.ContainsKey(inAllele.Chromosome) &&
                recalibrationData.AmpliconEdgeVariantsList[inAllele.Chromosome].Contains(inAllele.ReferencePosition))
            {
                UpdateVariantQScoreAndRefilter(options.MaxQScore, options.VariantCallingParams.MinimumVariantQScoreFilter, recalibrationData.EdgeRiskLookupTable, inAllele, cat, true);
                updateHappened = TypeOfUpdateNeeded.Modify;
            }

            return(updateHappened);
        }
Esempio n. 3
0
        public Counts()
        {
            var categories = MutationCounter.GetAllMutationCategories();

            foreach (var nucleotideTransitionCategory in categories)
            {
                _countsByCategory.Add(nucleotideTransitionCategory, 0);
            }
        }
Esempio n. 4
0
        public static string WriteCountsFile(string vcfIn, string outDir, int lociCount)
        {
            var variants      = new List <CalledAllele>();
            var countsPath    = Path.Combine(outDir, Path.GetFileName(vcfIn).Replace(".vcf", ".counts"));
            var countsPathOld = Path.Combine(outDir, Path.GetFileName(vcfIn).Replace(".vcf", ".counts.original"));

            if (File.Exists(countsPath))
            {
                if (File.Exists(countsPathOld))
                {
                    File.Delete(countsPathOld);
                }
                File.Copy(countsPath, countsPathOld);
                File.Delete(countsPath);
            }

            var counter = new MutationCounter();

            using (AlleleReader readerA = new AlleleReader(vcfIn))
            {
                counter.StartWriter(countsPath);

                while (readerA.GetNextVariants(out variants))
                {
                    foreach (var variant in variants)
                    {
                        try
                        {
                            counter.Add(variant);
                        }

                        catch (Exception ex)
                        {
                            Logger.WriteToLog(string.Format("Fatal error processing vcf; Check {0}, position {1}.  Exception: {2}",
                                                            variant.Chromosome, variant.ReferencePosition, ex));
                            throw;
                        }
                    }
                }

                if (lociCount > 0)
                {
                    counter.ForceTotalPossibleMutations(lociCount);
                }

                counter.CloseWriter();
            }

            return(countsPath);
        }
Esempio n. 5
0
        public static void Recalibrate(string vcfIn, string vcfOut, string sampleCountsFileName,
                                       int baselineQNoise, double zFactor, int maxQscore, int filterQScore)
        {
            if (!File.Exists(sampleCountsFileName))
            {
                Logger.WriteToLog("Cannot recalibrate. Cannot find {0} ", sampleCountsFileName);
                return;
            }
            else
            {
                Logger.WriteToLog("Found counts file: {0} ", sampleCountsFileName);
            }

            var LookupTable = GetPhredScaledCalibratedRates(baselineQNoise, zFactor, sampleCountsFileName);

            //if no work to do here...
            if ((LookupTable == null) || (LookupTable.Count == 0))
            {
                return;
            }

            if (File.Exists(vcfOut))
            {
                File.Delete(vcfOut);
            }

            using (VcfReader reader = new VcfReader(vcfIn))
                using (StreamWriter writer = new StreamWriter(vcfOut))
                {
                    writer.NewLine = "\n";
                    List <string> headerLines = reader.HeaderLines;
                    foreach (string headerLine in headerLines)
                    {
                        writer.WriteLine(headerLine);
                    }

                    var originalVar = new VcfVariant();
                    while (reader.GetNextVariant(originalVar))
                    {
                        var cat = MutationCounter.GetMutationCategory(originalVar);

                        if (LookupTable.ContainsKey(cat))
                        {
                            UpdateVariant(maxQscore, filterQScore, LookupTable, originalVar, cat);
                        }
                        writer.WriteLine(originalVar);
                    }
                }
        }
        private static void DoRecalibrationWork(string vcfIn, string vcfOut, string sampleCountsFileName,
                                                int baselineQNoise, double zFactor, int maxQscore, int filterQScore, string quotedCommandLineString)
        {
            if (!File.Exists(sampleCountsFileName))
            {
                Logger.WriteToLog("Cannot recalibrate. Cannot find {0} ", sampleCountsFileName);
                return;
            }
            else
            {
                Logger.WriteToLog("Found counts file: {0} ", sampleCountsFileName);
            }

            var LookupTable = GetPhredScaledCalibratedRates(baselineQNoise, zFactor, sampleCountsFileName);

            //if no work to do here...
            if ((LookupTable == null) || (LookupTable.Count == 0))
            {
                Logger.WriteToLog("No recalibration needed.");
                return;
            }

            using (VcfReader reader = new VcfReader(vcfIn))
                using (StreamWriter writer = new StreamWriter(new FileStream(vcfOut, FileMode.CreateNew)))
                {
                    writer.NewLine = "\n";
                    List <string> headerLines = reader.HeaderLines;
                    WriteHeaders(writer, headerLines, quotedCommandLineString);


                    var originalVar = new VcfVariant();
                    while (reader.GetNextVariant(originalVar))
                    {
                        var cat = MutationCounter.GetMutationCategory(originalVar);

                        if (LookupTable.ContainsKey(cat))
                        {
                            UpdateVariant(maxQscore, filterQScore, LookupTable, originalVar, cat);
                        }
                        writer.WriteLine(originalVar);
                    }
                }
        }
Esempio n. 7
0
        public void LoadCountsFile(string file)
        {
            bool inRateSection = false;

            using (StreamReader sr = new StreamReader(file))
            {
                string line;

                while (true)
                {
                    line = sr.ReadLine();

                    if (line == "")
                    {
                        continue;
                    }

                    if (line == null)
                    {
                        break;
                    }

                    if (inRateSection)
                    {
                        string[] Splat = line.Split();

                        if (Splat.Length < 2)
                        {
                            continue;
                        }


                        double result = -1;
                        if (!(double.TryParse(Splat[1], out result)))
                        {
                            throw new ApplicationException("Unable to parse counts from noise file " + file);
                        }

                        switch (Splat[0])
                        {
                        case "AllPossibleVariants":
                            NumPossibleVariants += result;
                            break;

                        case "FalsePosVariantsFound":
                        case "ErrorRate(%)":
                        case "VariantsCountedTowardEstimate":
                        case "ErrorRateEstimate(%)":
                        case "MismatchEstimate(%)":
                            continue;

                        default:
                            MutationCategory category = MutationCounter.GetMutationCategory(Splat[0]);
                            CountsByCategory[category] += result;
                            break;
                        }
                    }
                    if (line.Contains("CountsByCategory"))
                    {
                        inRateSection = true;
                    }
                }
            }
        }