public void GetReferencePloidyIntervals_EmptyVcf_ReferencePloidyIs2() { var ploidyVcfIntervals = Enumerable.Empty <ReferencePloidyInterval>(); var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); AssertSingleReferencePloidy(ploidyVcfIntervals, queryInterval, 2); }
private int[] getPloidyCounts(ReferenceInterval queryIval) { var baseCounts = new int[5]; baseCounts[2] = queryIval.Interval.Length; foreach (PloidyInterval interval in PloidyByChromosome[queryIval.Chromosome]) { if (interval.Ploidy == 2) { continue; } int overlapStart = Math.Max(queryIval.Interval.OneBasedStart - 1, interval.Start - 1); if (overlapStart > interval.End) { continue; } int overlapEnd = Math.Min(queryIval.Interval.OneBasedEnd, interval.End); int overlapBases = overlapEnd - overlapStart; if (overlapBases <= 0) { continue; } baseCounts[2] -= overlapBases; baseCounts[interval.Ploidy] += overlapBases; // ASSUMPTION: Vcf file ploidy shouldn't be >4 (i.e. we wouldn't handle an XXXXXY genome): } return(baseCounts); }
public void GetReferencePloidyIntervals_VcfPloidy1AndSameQueryInterval_PloidyIs1() { var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); var ploidyVcfIntervals = new ReferencePloidyInterval(queryInterval, 1).Yield().ToList(); AssertSingleReferencePloidy(ploidyVcfIntervals, queryInterval, 1); }
public void GetReferencePloidy_QueryContainedWithinPloidy1Region_Returns1() { var ploidyVcfIntervals = CreatePloidyInterval("chrX", new Interval(1, 4), 1).Yield(); var queryInterval = new ReferenceInterval("chrX", new Interval(2, 3)); AssertSingleReferencePloidy(ploidyVcfIntervals, queryInterval, 1); }
private void AssertSingleReferencePloidy( IEnumerable <ReferencePloidyInterval> ploidyVcfIntervals, ReferenceInterval queryInterval, int expectedPloidy) { var referencePloidy = LoadReferencePloidy(ploidyVcfIntervals); var ploidy = referencePloidy.GetSingleReferencePloidy(queryInterval); Assert.Equal(expectedPloidy, ploidy); }
public void GetReferencePloidy_QueryWithNoPloidy1Region_Returns2() { var ploidyVcfIntervals = Enumerable.Empty <ReferencePloidyInterval>(); var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); AssertSingleReferencePloidy(ploidyVcfIntervals, queryInterval, 2); }
private void AssertEqualPloidy( IEnumerable <ReferencePloidyInterval> ploidyVcfIntervals, ReferenceInterval queryInterval, IEnumerable <ReferencePloidyInterval> expectedPloidyIntervals, bool useSymbolicAltAlleleInPloidyVcf = true) { var referencePloidy = LoadReferencePloidy(ploidyVcfIntervals, useSymbolicAltAlleleInPloidyVcf); var referencePloidyIntervals = referencePloidy.GetReferencePloidyIntervals(queryInterval).ToList(); Assert.Equal(expectedPloidyIntervals, referencePloidyIntervals, new EqualityComparer()); }
public void GetReferencePloidyIntervals_VcfTwoAdjacentPloidy1_SinglePloidy1() { var ploidyVcfIntervals = new[] { CreatePloidyInterval("chrX", new Interval(1, 1), 1), CreatePloidyInterval("chrX", new Interval(2, 2), 1) }; var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); AssertSingleReferencePloidy(ploidyVcfIntervals, queryInterval, 1); }
private static IEnumerable <CNInterval> GetKnownCopyNumberWithReferencePloidy(List <CNInterval> knownCnIntervals, ReferencePloidy referencePloidy) { foreach (var knownCnInterval in knownCnIntervals) { var interval = new ReferenceInterval(knownCnInterval.Chromosome, new Interval(knownCnInterval.Start + 1, knownCnInterval.End)); var refPloidy = referencePloidy.GetSingleReferencePloidy(interval); yield return(new CNInterval(knownCnInterval.Chromosome, knownCnInterval.Start, knownCnInterval.End, knownCnInterval.Cn) { ReferenceCopyNumber = refPloidy }); } }
public void GetReferencePloidy_QueryOverlapsMultiplePloidyRegions_ThrowsArgumentException() { var ploidyVcfIntervals = new[] { CreatePloidyInterval("chrX", new Interval(2, 2), 1) }; var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); var referencePloidy = LoadReferencePloidy(ploidyVcfIntervals); var exception = Record.Exception(() => referencePloidy.GetSingleReferencePloidy(queryInterval)); Assert.IsType <ArgumentException>(exception); }
public void GetReferencePloidyIntervals_VcfPloidy1AndPartialOverlapQueryInterval_PloidyIs1And2(bool symbolicAltAllele) { var vcfInterval = new ReferenceInterval("chrX", new Interval(1, 1)); var ploidyVcfIntervals = new ReferencePloidyInterval(vcfInterval, 1).Yield().ToList(); var queryInterval = new ReferenceInterval("chrX", new Interval(1, 2)); var expectedPloidyIntervals = new[] { CreatePloidyInterval("chrX", new Interval(1, 1), 1), CreatePloidyInterval("chrX", new Interval(2, 2), 2) }; AssertEqualPloidy(ploidyVcfIntervals, queryInterval, expectedPloidyIntervals, symbolicAltAllele); }
private bool IsNewSegment(Dictionary <string, bool> starts, string chr, List <SampleGenomicBin> excludeIntervals, uint previousBinEnd, uint end, uint start, ref int excludeIndex, PloidyInfo referencePloidy) { string key = chr + ":" + start; bool newSegment = starts.ContainsKey(key); if (excludeIntervals != null) { while (excludeIndex < excludeIntervals.Count && excludeIntervals[excludeIndex].Stop < previousBinEnd) { excludeIndex++; } if (excludeIndex < excludeIntervals.Count) { // Note: forbiddenZoneMid should never fall inside a bin, becuase these intervals were already excluded // from consideration during the call to CanvasBin. var excludeStart = excludeIntervals[excludeIndex].Start; var excludeStop = excludeIntervals[excludeIndex].Stop; int forbiddenZoneMid = (excludeStart + excludeStop) / 2; if (previousBinEnd < forbiddenZoneMid && end >= forbiddenZoneMid) { //Debug.Assert(previousBinEnd <= excludeStart); //Debug.Assert(start >= excludeStop); newSegment = true; } } } if (previousBinEnd > 0 && _maxInterBinDistInSegment >= 0 && previousBinEnd + _maxInterBinDistInSegment < start && !newSegment) { newSegment = true; } // also start new segment if reference ploidy changes between end of last and end of this; // note that Interval takes 1-based positions, so using "previousBinEnd" effectively // includes the last base of the previous bin, allowing for a change at the bin boundary if (!newSegment && referencePloidy != null) { var refIval = new ReferenceInterval(chr, new Interval(previousBinEnd > 0 ? previousBinEnd : 1, end)); if (!referencePloidy.IsUniformReferencePloidy(refIval)) { newSegment = true; } } return(newSegment); }
/// <summary> /// 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 /// </summary> public bool IsUniformReferencePloidy(ReferenceInterval queryIval) { if (!PloidyByChromosome.ContainsKey(queryIval.Chromosome)) { return(true); } var baseCounts = getPloidyCounts(queryIval); int nonZeroCount = 0; for (int copyNumber = 0; copyNumber < baseCounts.Length; copyNumber++) { if (baseCounts[copyNumber] > 0) { nonZeroCount++; } } return(nonZeroCount < 2); }
public void GetReferencePloidyIntervals_VcfMultiplePloidyAndLargeQuery_ReturnMultiplePloidies() { var ploidyVcfIntervals = new[] { CreatePloidyInterval("chrX", new Interval(2, 2), 1), CreatePloidyInterval("chrX", new Interval(4, 4), 3) }; var queryInterval = new ReferenceInterval("chrX", new Interval(1, 5)); var expectedPloidyIntervals = new[] { CreatePloidyInterval("chrX", new Interval(1, 1), 2), CreatePloidyInterval("chrX", new Interval(2, 2), 1), CreatePloidyInterval("chrX", new Interval(3, 3), 2), CreatePloidyInterval("chrX", new Interval(4, 4), 3), CreatePloidyInterval("chrX", new Interval(5, 5), 2) }; AssertEqualPloidy(ploidyVcfIntervals, queryInterval, expectedPloidyIntervals); }
/// <summary> /// Returns the reference ploidy for <paramref name="referenceInterval"/><para/> /// If <paramref name="referenceInterval"/> spans regions with different ploidy an exception is thrown /// </summary> public static int GetSingleReferencePloidy(this ReferencePloidy referencePloidy, ReferenceInterval referenceInterval) { var referencePloidies = referencePloidy.GetReferencePloidyIntervals(referenceInterval).ToList(); if (referencePloidies.Count != 1) { var differentPloidyIntervals = referencePloidies.Select(ploidy => ploidy.Interval); throw new ArgumentException( $"Reference interval '{referenceInterval}' overlaps regions with different ploidy: '{string.Join(", ", differentPloidyIntervals)}'"); } return(referencePloidies.Single().ReferencePloidy); }
public ReferencePloidyInterval(ReferenceInterval interval, int referencePloidy) { Interval = interval; ReferencePloidy = referencePloidy; }