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);
        }
        public static void Recalibrate(SignatureSorterResultFiles countsFilePaths, VQROptions options)
        {
            string vcfFileName = Path.GetFileName(options.VcfPath);
            string vcfOut      = Path.Combine(options.OutputDirectory, vcfFileName + ".recal");

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

            try
            {
                //Read in the results files that have the data for the error modes detected.
                //Decide which types of variants we want to re-Qscore
                var recalibrationData = GetRecalibrationTables(countsFilePaths, options);

                //Update Vcf, variant by variant, based on the table data.
                VcfUpdater <QualityRecalibrationData> .UpdateVcfAlleleByAllele(vcfOut, options, false, recalibrationData, UpdateAllele, CanSkipVcfLines,
                                                                               VQRVcfWriter.GetVQRVcfFileWriter);

                //let the user know it worked
                if (File.Exists(vcfOut))
                {
                    Logger.WriteToLog("The following vcf was recalibrated: " + options.VcfPath);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteToLog("Recalibrate failed for " + options.VcfPath);
                Logger.WriteToLog("Exception: " + ex);
            }
        }
Beispiel #3
0
        // idea is to keep track of the disparity between two pools as a measure of FFPE degradation,
        // or overall oxidation affecting tissue sample.


        //possible SNP changes:
        //
        //
        // *    A   C   G   T
        //  A   *   1   2   3
        //  C   4   *   5   6
        //  G   7   8   *   9
        //  T   10  11  12  *
        //

        public static SignatureSorterResultFiles StrainVcf(VQROptions options)
        {
            var variantList = new List <CalledAllele>()
            {
            };
            var basicCountsData       = new CountData();
            var edgeVariantsCountData = new EdgeIssueCountData(options.ExtentofEdgeRegion);

            string basicCountsPath  = CleanUpOldFiles(options.VcfPath, options.OutputDirectory, ".counts");
            string edgeCountsPath   = CleanUpOldFiles(options.VcfPath, options.OutputDirectory, ".edgecounts");
            string edgeVariantsPath = CleanUpOldFiles(options.VcfPath, options.OutputDirectory, ".edgevariants");

            using (AlleleReader readerA = new AlleleReader(options.VcfPath))
            {
                while (readerA.GetNextVariants(out variantList))
                {
                    foreach (var variant in variantList)
                    {
                        try
                        {
                            basicCountsData.Add(variant);
                            edgeVariantsCountData.Add(variant, edgeVariantsPath);
                        }


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

                //The edge issue filter trails N variants behind.
                //The following code cleans out the buffer, processing anything left behind in the buffer.
                for (int i = 0; i < options.ExtentofEdgeRegion; i++)
                {
                    edgeVariantsCountData.Add(null, edgeVariantsPath);
                }

                if (options.LociCount > 0)
                {
                    basicCountsData.ForceTotalPossibleMutations(options.LociCount);
                    edgeVariantsCountData.ForceTotalPossibleMutations(options.LociCount);
                }

                if (options.DoBasicChecks)
                {
                    CountsFileWriter.WriteCountsFile(basicCountsPath, basicCountsData);
                }

                if (options.DoAmpliconPositionChecks)
                {
                    CountsFileWriter.WriteCountsFile(edgeCountsPath, edgeVariantsCountData);
                }
            }

            return(new SignatureSorterResultFiles(basicCountsPath, edgeCountsPath, edgeVariantsPath));
        }
 public VQROptionsParser()
 {
     Options = new VQROptions();
 }
        private static QualityRecalibrationData GetRecalibrationTables(SignatureSorterResultFiles resultsFilePaths,
                                                                       VQROptions options)
        {
            CountData BasicCounts = null;
            CountData EdgeCounts  = null;

            QualityRecalibrationData recalibrationData = new QualityRecalibrationData();

            if (options.DoBasicChecks)
            {
                if (!File.Exists(resultsFilePaths.BasicCountsFilePath))
                {
                    Logger.WriteToLog("Cannot do basic recalibration. Cannot find {0} ", resultsFilePaths.BasicCountsFilePath);
                }
                else
                {
                    Logger.WriteToLog("Found counts file: {0} ", resultsFilePaths.BasicCountsFilePath);

                    BasicCounts = CountsFileReader.ReadCountsFile(resultsFilePaths.BasicCountsFilePath);
                    recalibrationData.BasicLookupTable = GetPhredScaledCalibratedRates(options.BamFilterParams.MinimumBaseCallQuality, options.ZFactor, BasicCounts);


                    //if no work to do here...
                    if ((recalibrationData.BasicLookupTable == null) || (recalibrationData.BasicLookupTable.Count == 0))
                    {
                        Logger.WriteToLog("No general recalibration needed.");
                    }
                    else
                    {
                        Logger.WriteToLog("General mutation bias detected. This sample may have sample-specific prep issues such as FFPE or oxidation damage.");
                    }
                }
            }

            if (options.DoAmpliconPositionChecks)
            {
                if (!File.Exists(resultsFilePaths.AmpliconEdgeCountsFilePath))
                {
                    Logger.WriteToLog("Cannot do amplicon-position based recalibration. Cannot find {0} ", resultsFilePaths.AmpliconEdgeCountsFilePath);
                }
                else
                {
                    Logger.WriteToLog("Found counts file: {0} ", resultsFilePaths.AmpliconEdgeCountsFilePath);


                    EdgeCounts = CountsFileReader.ReadCountsFile(resultsFilePaths.AmpliconEdgeCountsFilePath);
                    recalibrationData.AmpliconEdgeVariantsLookupTable = GetPhredScaledCalibratedRates(options.BamFilterParams.MinimumBaseCallQuality, options.ZFactor, EdgeCounts);
                    recalibrationData.AmpliconEdgeVariantsList        = VariantListReader.ReadVariantListFile(resultsFilePaths.AmpliconEdgeSuspectListFilePath);


                    if ((recalibrationData.AmpliconEdgeVariantsLookupTable == null) || (recalibrationData.AmpliconEdgeVariantsLookupTable.Count == 0))
                    {
                        Logger.WriteToLog("No position-in-amplicon recalibration needed.");
                    }
                }
            }

            //compare edge-issues with FFPE-like issues.
            //Did the bulk of variants appear to come from the edge of amplicons..?
            //Look at the diff in percents.
            //If a variant is X more likely to be called when its by an edge - thats an estimate of the error.
            if (options.DoBasicChecks && options.DoAmpliconPositionChecks)
            {
                recalibrationData.EdgeRiskLookupTable = GetPhredScaledCalibratedRatesForEdges(options.BamFilterParams.MinimumBaseCallQuality, options.AlignmentWarningThreshold, BasicCounts, EdgeCounts);
            }


            return(recalibrationData);
        }