CanvasCommon.PloidyInfo.GetReferenceCopyNumber C# (CSharp) Method

GetReferenceCopyNumber() public method

Given a segment, return the expected copy number - normally this is 2, but based on the reference ploidy bed file, it could be something else. For XX samples, reference ploidy is 0 on chrY; for XY samples, reference ploidy is 1 on chrX+chrY
public GetReferenceCopyNumber ( CanvasSegment segment ) : int
segment CanvasSegment
return int
        public int GetReferenceCopyNumber(CanvasSegment segment)
        {
            if (!PloidyByChromosome.ContainsKey(segment.Chr)) return 2;
            int[] baseCounts = new int[5];
            baseCounts[2] = segment.End - segment.Begin;

            foreach (PloidyInterval interval in this.PloidyByChromosome[segment.Chr])
            {
                if (interval.Ploidy == 2) continue;
                int overlapStart = Math.Max(segment.Begin, interval.Start);
                if (overlapStart > segment.End) continue;
                int overlapEnd = Math.Min(segment.End, interval.End);
                int overlapBases = overlapEnd - overlapStart;
                if (overlapBases < 0) continue;
                baseCounts[2] -= overlapBases;
                baseCounts[interval.Ploidy] += overlapBases; // ASSUMPTION: Bed file ploidy shouldn't be >4 (i.e. we wouldn't handle an XXXXXY genome):
            }
            int bestCount = 0;
            int referenceCN = 2;
            for (int CN = 0; CN < baseCounts.Length; CN++)
            {
                if (baseCounts[CN] > bestCount)
                {
                    bestCount = baseCounts[CN];
                    referenceCN = CN;
                }
            }
            return referenceCN;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Outputs the copy number calls to a text file.
        /// </summary>
        private static void WriteVariants(IReadOnlyCollection <List <CanvasSegment> > segments, PloidyInfo ploidy, GenomeMetadata genome,
                                          BgzipOrStreamWriter writer, bool isPedigreeInfoSupplied = true, int?denovoQualityThreshold = null)
        {
            var nSamples = segments.Count;

            foreach (GenomeMetadata.SequenceMetadata chromosome in genome.Sequences)
            {
                for (int segmentIndex = 0; segmentIndex < segments.First().Count; segmentIndex++)
                {
                    var firstSampleSegment = segments.First()[segmentIndex];
                    if (!isPedigreeInfoSupplied && segments.Select(sample => sample[segmentIndex].Filter == "PASS").Any() && segments.Count > 1)
                    {
                        firstSampleSegment.Filter = "PASS";
                    }
                    if (!firstSampleSegment.Chr.Equals(chromosome.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    var referenceCopyNumbers = segments.Select(segment => ploidy?.GetReferenceCopyNumber(segment[segmentIndex]) ?? 2).ToList();
                    var currentSegments      = segments.Select(x => x[segmentIndex]).ToList();
                    var cnvTypes             = new List <CnvType>();
                    for (int sampleIndex = 0; sampleIndex < nSamples; sampleIndex++)
                    {
                        cnvTypes.Add(currentSegments[sampleIndex].GetCnvType(referenceCopyNumbers[sampleIndex]));
                    }
                    CnvType cnvType;
                    if (cnvTypes.TrueForAll(x => x == CnvType.Reference))
                    {
                        cnvType = CnvType.Reference;
                    }
                    else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.Loss))
                    {
                        cnvType = CnvType.Loss;
                    }
                    else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.Gain))
                    {
                        cnvType = CnvType.Gain;
                    }
                    else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.LossOfHeterozygosity))
                    {
                        cnvType = CnvType.LossOfHeterozygosity;
                    }
                    else
                    {
                        cnvType = CnvType.ComplexCnv;
                    }

                    WriteInfoField(writer, firstSampleSegment, cnvType, denovoQualityThreshold, isMultisample: segments.Count > 1);
                    //  FORMAT field
                    if (segments.Count == 1)
                    {
                        WriteSingleSampleInfo(writer, firstSampleSegment);
                    }
                    else
                    {
                        WriteFormatField(writer, currentSegments);
                    }
                }
            }
        }
All Usage Examples Of CanvasCommon.PloidyInfo::GetReferenceCopyNumber