public static Tuple <Dictionary <int, List <LoadTestResult> >, List <Domain.Models.Alleles.CandidateAllele> > LoadReads
            (List <BamAlignment> reads, PiscesApplicationOptions options, ChrReference chrRef,
            bool expectedvariants, string expectedLoading, string expectedDirectionString)
        {
            RegionStateManager manager = new RegionStateManager(expectStitchedReads: true);
            var variantFinder          = new CandidateVariantFinder(options.BamFilterParameters.MinimumBaseCallQuality, options.MaxSizeMNV, options.MaxGapBetweenMNV, options.CallMNVs);
            var candidateVariants      = new List <Domain.Models.Alleles.CandidateAllele>();

            try
            {
                foreach (var b in reads)
                {
                    if (b == null)
                    {
                        continue;
                    }

                    var r = new Read(chrRef.Name, b);
                    // find candidate variants
                    candidateVariants = variantFinder.FindCandidates(r, chrRef.Sequence, chrRef.Name).ToList();
                    // track in state manager
                    manager.AddCandidates(candidateVariants);
                    manager.AddAlleleCounts(r);
                }

                Dictionary <int, List <LoadTestResult> > countResults = GetCountsFromManager(manager);
                var loadingResults = Tuple.Create(countResults, candidateVariants);

                return(loadingResults);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #2
0
        private ISomaticVariantCaller CreateMockVariantCaller(VcfFileWriter vcfWriter, ApplicationOptions options, ChrReference chrRef, MockAlignmentExtractor mae, IStrandBiasFileWriter biasFileWriter = null, string intervalFilePath = null)
        {
            var config = new AlignmentSourceConfig
            {
                MinimumMapQuality  = options.MinimumMapQuality,
                OnlyUseProperPairs = options.OnlyUseProperPairs,
            };

            IAlignmentStitcher stitcher = null;

            if (options.StitchReads)
            {
                if (options.UseXCStitcher)
                {
                    stitcher = new XCStitcher(options.MinimumBaseCallQuality);
                }
                else
                {
                    stitcher = new BasicStitcher(options.MinimumBaseCallQuality);
                }
            }

            var mateFinder      = options.StitchReads ? new AlignmentMateFinder(MAX_FRAGMENT_SIZE) : null;
            var RegionPadder    = new RegionPadder(chrRef, null);
            var alignmentSource = new AlignmentSource(mae, mateFinder, stitcher, config);
            var variantFinder   = new CandidateVariantFinder(options.MinimumBaseCallQuality, options.MaxSizeMNV, options.MaxGapBetweenMNV, options.CallMNVs);
            var alleleCaller    = new AlleleCaller(new VariantCallerConfig
            {
                IncludeReferenceCalls        = options.OutputgVCFFiles,
                MinVariantQscore             = options.MinimumVariantQScore,
                MaxVariantQscore             = options.MaximumVariantQScore,
                VariantQscoreFilterThreshold = options.FilteredVariantQScore > options.MinimumVariantQScore ? options.FilteredVariantQScore : (int?)null,
                MinCoverage                = options.MinimumCoverage,
                MinFrequency               = options.MinimumFrequency,
                EstimatedBaseCallQuality   = options.AppliedNoiseLevel == -1 ? options.MinimumBaseCallQuality : options.AppliedNoiseLevel,
                StrandBiasModel            = options.StrandBiasModel,
                StrandBiasFilterThreshold  = options.StrandBiasAcceptanceCriteria,
                FilterSingleStrandVariants = options.FilterOutVariantsPresentOnlyOneStrand,
                GenotypeModel              = options.GTModel
            });
            var stateManager = new RegionStateManager();

            return(new SomaticVariantCaller(
                       alignmentSource,
                       variantFinder,
                       alleleCaller,
                       vcfWriter,
                       stateManager,
                       chrRef,
                       RegionPadder,
                       biasFileWriter));
        }
        public static ISomaticVariantCaller CreateMockVariantCaller(VcfFileWriter vcfWriter, ApplicationOptions options, ChrReference chrRef, MockAlignmentExtractor mockAlignmentExtractor, IStrandBiasFileWriter biasFileWriter = null, string intervalFilePath = null)
        {
            var config = new AlignmentSourceConfig
            {
                MinimumMapQuality  = options.MinimumMapQuality,
                OnlyUseProperPairs = options.OnlyUseProperPairs,
            };


            //var mateFinder = options.StitchReads ? new AlignmentMateFinder() : null;
            AlignmentMateFinder mateFinder = null;
            var alignmentSource            = new AlignmentSource(mockAlignmentExtractor, mateFinder, config);
            var variantFinder      = new CandidateVariantFinder(options.MinimumBaseCallQuality, options.MaxSizeMNV, options.MaxGapBetweenMNV, options.CallMNVs);
            var coverageCalculator = new CoverageCalculator();

            var alleleCaller = new AlleleCaller(new VariantCallerConfig
            {
                IncludeReferenceCalls        = options.OutputgVCFFiles,
                MinVariantQscore             = options.MinimumVariantQScore,
                MaxVariantQscore             = options.MaximumVariantQScore,
                VariantQscoreFilterThreshold = options.FilteredVariantQScore > options.MinimumVariantQScore ? options.FilteredVariantQScore : (int?)null,
                MinCoverage                = options.MinimumDepth,
                MinFrequency               = options.MinimumFrequency,
                EstimatedBaseCallQuality   = options.AppliedNoiseLevel == -1 ? options.MinimumBaseCallQuality : options.AppliedNoiseLevel,
                StrandBiasModel            = options.StrandBiasModel,
                StrandBiasFilterThreshold  = options.StrandBiasAcceptanceCriteria,
                FilterSingleStrandVariants = options.FilterOutVariantsPresentOnlyOneStrand,
                ChrReference               = chrRef
            },
                                                coverageCalculator: coverageCalculator,
                                                variantCollapser: options.Collapse ? new VariantCollapser(null, coverageCalculator) : null);

            var stateManager = new RegionStateManager(
                expectStitchedReads: mockAlignmentExtractor.SourceIsStitched,
                trackOpenEnded: options.Collapse, trackReadSummaries: options.CoverageMethod == CoverageMethod.Approximate);

            //statmanager is an allele source
            Assert.Equal(0, stateManager.GetAlleleCount(1, AlleleType.A, DirectionType.Forward));


            return(new SomaticVariantCaller(
                       alignmentSource,
                       variantFinder,
                       alleleCaller,
                       vcfWriter,
                       stateManager,
                       chrRef,
                       null,
                       biasFileWriter));
        }
        public void AddAlleleCounts(Read alignment)
        {
            if (!_readLength.HasValue)
            {
                _readLength = alignment.ReadLength;
            }

            var lastPosition = alignment.Position - 1;

            var cigarData = alignment.CigarData;

            var deletionLength               = 0;
            var lengthBeforeDeletion         = alignment.ReadLength;
            var endsInDeletion               = cigarData.HasOperationAtOpIndex(0, 'D', true);
            var endsInDeletionBeforeSoftclip = cigarData.HasOperationAtOpIndex(1, 'D', true) && cigarData.HasOperationAtOpIndex(0, 'S', true);

            if (endsInDeletion || endsInDeletionBeforeSoftclip)
            {
                deletionLength = (int)(endsInDeletionBeforeSoftclip ? cigarData[cigarData.Count - 2].Length :
                                       cigarData[cigarData.Count - 1].Length);
                lengthBeforeDeletion = (int)(endsInDeletionBeforeSoftclip ? alignment.ReadLength - cigarData[cigarData.Count - 1].Length :  alignment.ReadLength);
            }

            var positionMapLength      = alignment.PositionMap.Length;
            var alignmentEndPosition   = alignment.EndPosition;
            var alignmentStartPosition = alignment.Position;

            for (var positionMapIndex = 0; positionMapIndex < positionMapLength; positionMapIndex++)
            {
                DirectionType directionType = alignment.SequencedBaseDirectionMap[positionMapIndex];


                if ((endsInDeletionBeforeSoftclip) && positionMapIndex == lengthBeforeDeletion)
                {
                    if (CandidateVariantFinder.CheckDeletionQuality(alignment, positionMapIndex, _minBasecallQuality))
                    {
                        for (var j = 1; j < deletionLength + 1; j++) // add any terminal deletion counts
                        {
                            var anchorIndex = NumAnchorIndexes - 1;  // Last index
                            AddAlleleCount(j + lastPosition, AlleleType.Deletion, directionType, anchorIndex);
                            AddCollapsedReadCount(j + lastPosition, alignment, directionType);
                        }
                    }
                }

                var position = alignment.PositionMap.GetPositionAtIndex(positionMapIndex);

                if (position == -1)
                {
                    continue; // not mapped to reference
                }

                var anchorType = GetAnchorType(alignmentEndPosition, position, alignmentStartPosition);

                //if the deletion is of decent quality, add it to the counts matix
                if (CandidateVariantFinder.CheckDeletionQuality(alignment, positionMapIndex, _minBasecallQuality))
                {
                    for (var j = lastPosition + 1; j < position; j++) // add any deletion counts
                    {
                        AddAlleleCount(j, AlleleType.Deletion, directionType, anchorType);
                        AddCollapsedReadCount(j, alignment, directionType);
                    }
                }

                var alleleType = AlleleHelper.GetAlleleType(alignment.Sequence[positionMapIndex]);
                if (alignment.Qualities[positionMapIndex] < _minBasecallQuality)
                {
                    alleleType = AlleleType.N; // record this event as a no call
                }
                AddAlleleCount(position, alleleType, directionType, anchorType);

                if (alleleType != AlleleType.N)
                {
                    AddCollapsedReadCount(position, alignment, directionType);
                    AddAmpliconCount(_trackAmpliconCounts, position, alignment.GetAmpliconNameIfExists());
                }

                AddAlleleBaseQuality(position, alleleType, directionType, Math.Pow(10, -1 * (int)alignment.Qualities[positionMapIndex] / 10f), anchorType);
                lastPosition = position;
            }

            if (endsInDeletion)
            {
                if (CandidateVariantFinder.CheckDeletionQuality(alignment, alignment.SequencedBaseDirectionMap.Length - 1, _minBasecallQuality))
                {
                    for (var j = 1; j < deletionLength + 1; j++) // add any terminal deletion counts
                    {
                        DirectionType directionType =
                            alignment.SequencedBaseDirectionMap[alignment.SequencedBaseDirectionMap.Length - 1];

                        var anchorIndex = NumAnchorIndexes - 1;                                            // Last index

                        AddAlleleCount(j + lastPosition, AlleleType.Deletion, directionType, anchorIndex); // ends in deletion -> within 1
                        AddCollapsedReadCount(j + lastPosition, alignment, directionType);
                    }
                }
            }

            // add coverage summary
            if (_trackReadSummaries)
            {
                var coverageSummary = alignment.GetCoverageSummary();
                var block           = GetBlock(coverageSummary.ClipAdjustedEndPosition);
                // store by end position so we can always be forward looking
                block.AddReadSummary(coverageSummary.ClipAdjustedEndPosition, coverageSummary);
            }
        }
Beispiel #5
0
 public DirectionType GetSupportDirection(CandidateAllele variant, Read read)
 {
     return(CandidateVariantFinder.GetSupportDirection(variant, read, VariantStartInRead));
 }
        public DirectionType GetDeletionDirectionForStitchedRead(CandidateAllele variant, Read read, int deletionLength)
        {
            int zeroBasedVariantStartInRead = VariantStartInRead - 1;

            return(CandidateVariantFinder.GetDeletionDirectionForStitchedRead(read, zeroBasedVariantStartInRead, zeroBasedVariantStartInRead + 1));
        }