Exemplo n.º 1
0
        public void CallRefs(ICallableNeighborhood completedNbhd)
        {
            var suckedUpRefCalls = completedNbhd.UsedRefCountsLookup;
            Dictionary <int, CalledAllele> possibleRefs = new Dictionary <int, CalledAllele>();
            var variantsRecalledByMnvCaller             = completedNbhd.GetOriginalVcfVariants();

            foreach (var allele in variantsRecalledByMnvCaller)
            {
                int suckedUpRefCallCount = 0;
                if (suckedUpRefCalls.ContainsKey(allele.ReferencePosition))
                {
                    suckedUpRefCallCount = suckedUpRefCalls[allele.ReferencePosition].Counts;
                }

                var newRef = ReCallAsRef(allele, suckedUpRefCallCount);

                if (!possibleRefs.ContainsKey(newRef.ReferencePosition))
                {
                    completedNbhd.NbhdGTcalculator.SetGenotypes(new List <CalledAllele> {
                        newRef
                    });
                    possibleRefs.Add(newRef.ReferencePosition, newRef);
                }
            }

            completedNbhd.CalledRefs = possibleRefs;
        }
Exemplo n.º 2
0
        private static int OrderWithNeighborhood(ICallableNeighborhood nbhdWithMNVs, CalledAllele originalVariant)
        {
            if (originalVariant.Chromosome != nbhdWithMNVs.ReferenceName)
            {
                return(1);
            }

            if (originalVariant.ReferencePosition > nbhdWithMNVs.LastPositionOfInterestInVcf)
            {
                return(1);
            }

            if ((originalVariant.ReferencePosition <= nbhdWithMNVs.LastPositionOfInterestInVcf) && (originalVariant.ReferencePosition >= nbhdWithMNVs.FirstPositionOfInterest))
            {
                return(0);
            }

            return(-1);
        }
Exemplo n.º 3
0
        public void CallMNVs(ICallableNeighborhood completedNbhd)
        {
            var foundMNVs = new Dictionary <int, List <CalledAllele> >();

            foreach (var mnv in completedNbhd.CandidateVariants)
            {
                bool isReference = (mnv.Type == Pisces.Domain.Types.AlleleCategory.Reference);

                if (CallCandidate(mnv, isReference))
                {
                    AddFilters(mnv, isReference);

                    if (!foundMNVs.ContainsKey(mnv.ReferencePosition))
                    {
                        foundMNVs.Add(mnv.ReferencePosition, new List <CalledAllele>());
                    }

                    foundMNVs[mnv.ReferencePosition].Add(mnv);
                }
            }

            completedNbhd.CalledVariants = foundMNVs;
        }
Exemplo n.º 4
0
        public List <Tuple <CalledAllele, string> > WriteVariantsUptoIncludingNbhd(
            IVcfFileWriter <CalledAllele> writer, List <Tuple <CalledAllele, string> > alleleTuplesLeftoverList, ICallableNeighborhood nbhdWithMNVs)
        {
            var alleleTuplesInProcessList           = new List <Tuple <CalledAllele, string> >();
            var alleleTuplesReadyToWriteList        = new List <Tuple <CalledAllele, string> >();
            var alleleTuplesInCurrentVcfNbhdList    = new List <Tuple <CalledAllele, string> >();
            var alleleTuplesToGroupWithNextNbhdList = new List <Tuple <CalledAllele, string> >();

            int  orderWithRespectToNbhd = -1;
            bool quittingNbhd           = false;
            bool takenCareOfLeftOvers   = !alleleTuplesLeftoverList.Any();

            while (true)
            {
                if (quittingNbhd)
                {
                    break;
                }

                if (takenCareOfLeftOvers)
                {
                    alleleTuplesInProcessList = GetNextBlockOfOriginalAlleleTuplesFromVcfVar();
                }
                else
                {
                    alleleTuplesInProcessList = GetNextSetOfAlleleTuplesToProcess(
                        writer, alleleTuplesLeftoverList, nbhdWithMNVs.ReferenceName);
                    takenCareOfLeftOvers = true;
                }

                if (!alleleTuplesInProcessList.Any())
                {
                    break;
                }

                foreach (var originalAlleleTuple in alleleTuplesInProcessList)
                {
                    if (quittingNbhd)
                    {
                        alleleTuplesToGroupWithNextNbhdList.Add(originalAlleleTuple);
                        continue;
                    }

                    orderWithRespectToNbhd = OrderWithNeighborhood(nbhdWithMNVs, originalAlleleTuple.Item1);
                    switch (orderWithRespectToNbhd)
                    {
                    case (-1):    //if we are before the nbhd, write straight to vcf
                        alleleTuplesReadyToWriteList.Add(originalAlleleTuple);
                        break;

                    case (0):     //in the nbhd
                        alleleTuplesInCurrentVcfNbhdList.Add(originalAlleleTuple);
                        break;

                    default:
                        //if we are either ahead of our nbhd, or gone into the wrong chr)
                        //Close out the nbhd and finish up.
                        var mergedVariants = GetMergedListOfVariants(nbhdWithMNVs, alleleTuplesInCurrentVcfNbhdList);
                        alleleTuplesReadyToWriteList.AddRange(mergedVariants);
                        alleleTuplesToGroupWithNextNbhdList.Add(originalAlleleTuple);
                        quittingNbhd = true;
                        break;
                    }
                }
            }

            //close out any remaining nbhd
            if (orderWithRespectToNbhd == 0)
            {
                var mergedVariants = GetMergedListOfVariants(nbhdWithMNVs, alleleTuplesInCurrentVcfNbhdList);
                alleleTuplesReadyToWriteList.AddRange(mergedVariants);
            }

            var alleleTuplesAfterAdjustmentList = VcfMergerUtils.AdjustForcedAllele(alleleTuplesReadyToWriteList);

            WriteDistinctVcfLines(writer, alleleTuplesAfterAdjustmentList);

            return(alleleTuplesToGroupWithNextNbhdList);
        }
Exemplo n.º 5
0
        public static List <Tuple <CalledAllele, string> > GetMergedListOfVariants(
            ICallableNeighborhood completedNbhd, List <Tuple <CalledAllele, string> > originalVariantsInsideRange)
        {
            var mergedVariantList = new List <Tuple <CalledAllele, string> >();
            Dictionary <int, List <CalledAllele> > foundMNVS = completedNbhd.CalledVariants;

            //track which variants got used for MNV phasing.
            var indexesOfVariantsRecalledByMnvCaller = completedNbhd.GetOriginalVcfVariants();

            //decide which of the original variants we keep, and which get replaced.
            for (int i = 0; i < originalVariantsInsideRange.Count; i++)
            {
                var  originalCall          = originalVariantsInsideRange[i];
                var  currentPosition       = originalCall.Item1.ReferencePosition;
                bool variantWasAlreadyUsed = CheckIfUsed(indexesOfVariantsRecalledByMnvCaller, originalCall.Item1);

                if (foundMNVS.ContainsKey(currentPosition))
                {
                    //add all the MNVs
                    foreach (var mnv in foundMNVS[currentPosition])
                    {
                        if (mnv.IsSameAllele(originalCall.Item1) &&
                            mnv.AlleleSupport == originalCall.Item1.AlleleSupport &&
                            mnv.TotalCoverage == originalCall.Item1.TotalCoverage &&
                            mnv.ReferenceSupport == originalCall.Item1.ReferenceSupport)
                        {
                            mergedVariantList.Add(originalCall);
                        }
                        else
                        {
                            mergedVariantList.Add(new Tuple <CalledAllele, string>(mnv, ""));
                        }
                    }
                    foundMNVS[currentPosition] = new List <CalledAllele>();//empty out the list, but leave the fact that there were MNVs here.

                    //add back any original variants,
                    //so long as they were not used by the caller, and not reference
                    if (!(variantWasAlreadyUsed) &&
                        (originalCall.Item1.Type != Pisces.Domain.Types.AlleleCategory.Reference))
                    {
                        mergedVariantList.Add(originalCall);
                    }

                    continue;
                }

                //Else, we did not find any MNVs here.
                //Then this position should be either the original call from the vcf,
                //or a new reference call converted from a variant that got used by the MNV caller.
                if (variantWasAlreadyUsed)
                {
                    var newRef = completedNbhd.CalledRefs[originalCall.Item1.ReferencePosition];

                    //sometimes several variants were used, all at the same position. we dont need to add new references for all of them.
                    if ((mergedVariantList.Count == 0) ||
                        (mergedVariantList.Last().Item1.ReferencePosition != currentPosition))
                    {
                        mergedVariantList.Add(new Tuple <CalledAllele, string>(newRef, ""));
                    }
                }
                else //wasnt used for MNV calling
                {
                    mergedVariantList.Add(originalCall);
                }
            }

            //in case we called any MNVs past the edge of the input VCF.
            foreach (var mnvsLeft in foundMNVS)
            {
                foreach (var mnv in mnvsLeft.Value)
                {
                    mergedVariantList.Add(new Tuple <CalledAllele, string>(mnv, ""));
                }
            }

            mergedVariantList.Sort(new AlleleTupleCompareByLociAndAllele());

            return(mergedVariantList);
        }