private Set<Interval> parseIntervalLists(final Set<String> intervalLists){ // Parse the PARs final Set<Interval> intervals = new HashSet<>(intervalLists.size()); for (final String par : intervalLists) { final String[] splits1 = par.split(":"); final String[] splits2 = splits1[1].split("-"); intervals.add(new Interval(splits1[0], Integer.parseInt(splits2[0]), Integer.parseInt(splits2[1]))); } return intervals; } /**
@Test public void testAddTwice() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,100), new Interval("1",10,100) ); final OverlapDetector<Locatable> detector = OverlapDetector.create(input); final Set<Locatable> overlaps = detector.getOverlaps(new Interval("1", 50, 200)); Assert.assertEquals(overlaps.size(), 1); Assert.assertEquals(overlaps, Collections.singleton(new Interval("1",10,100))); } }
/** * Converts a GenomeLoc to a picard interval. * @param loc The GenomeLoc. * @param locIndex The loc index for use in the file. * @return The picard interval. */ private static htsjdk.samtools.util.Interval toInterval(GenomeLoc loc, int locIndex) { return new htsjdk.samtools.util.Interval(loc.getContig(), loc.getStart(), loc.getStop(), false, "interval_" + locIndex); }
/** * Returns a new Interval that represents the intersection between the two intervals. */ public Interval intersect(final Interval that) { if (!intersects(that)) throw new IllegalArgumentException(that + " does not intersect " + this); return new Interval(this.getContig(), Math.max(this.getStart(), that.getStart()), Math.min(this.getEnd(), that.getEnd()), this.negativeStrand, this.name + " intersection " + that.name); }
/** * Creates an IntervalList from the given sequence name * * @param header header to use to create IntervalList * @param sequenceName name of sequence in header * @return a new intervalList with given header that contains the reference name */ public static IntervalList fromName(final SAMFileHeader header, final String sequenceName) { final IntervalList ref = new IntervalList(header); ref.add(new Interval(sequenceName, 1, header.getSequence(sequenceName).getSequenceLength())); return ref; }
@Test public void testUnionSamePosition() { final IntervalList iList= new IntervalList(fileHeader); final List<Interval> intervals = Arrays.asList( new Interval("1", 2, 100, true, "test1"), new Interval("1", 2, 100, true, "test2") ); iList.addall(intervals); final List<Interval> uniqued = iList.uniqued().getIntervals(); Assert.assertEquals(uniqued.size(),1); Assert.assertEquals(uniqued.get(0).getName(),"test1|test2"); }
@Test public void testSimpleOverlap() { final IntervalList intervalList = new IntervalList(header); intervalList.add(new Interval("2", 167166899, 167166899)); final VCFFileReader reader = getReader(CEU_TRIOS_SNPS_VCF); final Iterator<VariantContext> iterator = new ByIntervalListVariantContextIterator(reader, intervalList); Assert.assertTrue(iterator.hasNext()); final VariantContext ctx = iterator.next(); Assert.assertEquals(ctx.getStart(), 167166899); Assert.assertFalse(iterator.hasNext()); reader.close(); }
@Override public boolean apply(final VariantContext vc) { final boolean include = !intervalsOfInterestDetector.getOverlaps(new Interval(vc.getContig(), vc.getStart(), vc.getEnd())).isEmpty(); if (!include) LOG.debug("Filtering variant at ", vc.getContig(), ":", vc.getStart(), "-", vc.getEnd()); return include; } }
@Test public void ensureOverlapExclusionTest() { final OverlapDetector<Interval> oneTinyIntervalDetector = new OverlapDetector<Interval>(0, 0); final Interval theInterval = new Interval("1", 5, 10); oneTinyIntervalDetector.addLhs(theInterval, theInterval); final VcfFileSegmentGenerator noFilter = VcfFileSegmentGenerator.byWholeContigSubdividingWithWidth(TEN_MILLION); Assert.assertEquals(Iterables.size(noFilter.forVcf(VCF_WITH_LOGS_OF_GAPS)), 382); // The number of subdivisions of 10 million of this vcf final VcfFileSegmentGenerator allFiltered = VcfFileSegmentGenerator.excludingNonOverlaps(noFilter, oneTinyIntervalDetector); Assert.assertEquals(Iterables.size(allFiltered.forVcf(VCF_WITH_LOGS_OF_GAPS)), 1); } }
@Test public void testCoverageGetTotalOverflow() { final Interval interval = new Interval("chr1", 1, 2); final TargetMetricsCollector.Coverage coverage = new TargetMetricsCollector.Coverage(interval, 0); for (int offset = 0; offset <= interval.length(); offset++) { coverage.addBase(offset, Integer.MAX_VALUE - 1); } Assert.assertEquals((long)coverage.getTotal(), 2 * (long)(Integer.MAX_VALUE - 1)); } }
@Test public void testNoOverlapDifferentContig() { final IntervalList intervalList = new IntervalList(header); intervalList.add(new Interval("3", 167166899, 167166899)); final VCFFileReader reader = getReader(CEU_TRIOS_SNPS_VCF); final Iterator<VariantContext> iterator = new ByIntervalListVariantContextIterator(reader, intervalList); Assert.assertFalse(iterator.hasNext()); reader.close(); }
@Test(dataProvider = "testFromSequenceData") public void testFromSequenceName(final Path intervalList, final String referenceName, final Integer length) { final IntervalList intervals = IntervalList.fromPath(intervalList); final IntervalList test = IntervalList.fromName(intervals.getHeader(), referenceName); Assert.assertEquals(test.getIntervals(), CollectionUtil.makeList(new Interval(referenceName, 1, length))); }
@Test public void testNoVariants() { final IntervalList intervalList = new IntervalList(header); intervalList.add(new Interval(this.dict.getSequence(0).getSequenceName(), 1, 100)); final VCFFileReader reader = getReader(EMPTY_VCF); final Iterator<VariantContext> iterator = new ByIntervalListVariantContextIterator(reader, intervalList); Assert.assertFalse(iterator.hasNext()); reader.close(); }
@Test public void testReverseComplementFailureDoesNotErrorOut() { final VariantContextBuilder builder = new VariantContextBuilder().source("test").loc("chr1", 1, 4); final Allele originalRef = Allele.create("CCCC", true); final Allele originalAlt = Allele.create("C", false); builder.alleles(Arrays.asList(originalRef, originalAlt)); final Interval interval = new Interval("chr1", 1, 4, true, "test "); final String reference = "ATGATGATGA"; final ReferenceSequence refSeq = new ReferenceSequence("chr1", 10, reference.getBytes()); // we don't actually care what the results are here -- we just want to make sure that it doesn't fail final VariantContextBuilder result = LiftoverUtils.reverseComplementVariantContext(builder.make(), interval, refSeq); }
@Test(dataProvider = "indelFlipData") public void testFlipIndel(final VariantContext source, final ReferenceSequence reference, final VariantContext result) { final LiftOver liftOver = new LiftOver(CHAIN_FILE); final Interval originalLocus = new Interval(source.getContig(), source.getStart(), source.getEnd()); final Interval target = liftOver.liftOver(originalLocus); if (target != null && !target.isNegativeStrand()) { throw new RuntimeException("not reversed"); } final VariantContext flipped = LiftoverUtils.liftVariant(source, target, reference, false, false); VcfTestUtils.assertEquals(flipped, result); }
@Test(dataProvider = "indelFlipDataWithOriginalAllele") public void testFlipIndelWithOriginalAlleles(final VariantContext source, final ReferenceSequence reference, final VariantContext result) { final LiftOver liftOver = new LiftOver(CHAIN_FILE); final Interval originalLocus = new Interval(source.getContig(), source.getStart(), source.getEnd()); final Interval target = liftOver.liftOver(originalLocus); if (target != null && !target.isNegativeStrand()) { throw new RuntimeException("not reversed"); } final VariantContext flipped = LiftoverUtils.liftVariant(source, target, reference, false, true); VcfTestUtils.assertEquals(flipped, result); }
@Test(dataProvider = "snpWithChangedRef") public void snpWithChangedRef(final VariantContext source, final ReferenceSequence reference, final VariantContext result) { final LiftOver liftOver = new LiftOver(POSITIVE_CHAIN_FILE); final Interval originalLocus = new Interval(source.getContig(), source.getStart(), source.getEnd()); final Interval target = liftOver.liftOver(originalLocus); final VariantContext flipped = LiftoverUtils.swapRefAlt(source, LiftoverUtils.DEFAULT_TAGS_TO_REVERSE, LiftoverUtils.DEFAULT_TAGS_TO_DROP); VcfTestUtils.assertEquals(flipped, result); }
@Test(dataProvider = "indelNoFlipData") public void testLiftOverSimpleIndels(final LiftOver liftOver, final ReferenceSequence reference, final VariantContext source, final VariantContext result) { final Interval target = liftOver.liftOver(new Interval(source.getContig(), source.getStart(), source.getEnd()), .95); VariantContextBuilder vcb = LiftoverUtils.liftSimpleVariantContext(source, target); VcfTestUtils.assertEquals(vcb == null ? null : vcb.make(), result); }
private IntervalList buildIntervalList(final int start, final int end) { final SAMFileHeader header = new SAMFileHeader(); header.addSequence(new SAMSequenceRecord("CONTIG", 100000000)); final IntervalList intervals = new IntervalList(header); if (0 < start) intervals.add(new Interval("CONTIG", start, end)); return intervals; }
private static IntervalList composeIntervalList(final IntervalList source, final String chromosome, final int... segmentsByPair) { final IntervalList intervals = new IntervalList(source.getHeader()); for (int i = 0; i < segmentsByPair.length; i += 2) { final Interval parentInterval = lookupIntervalContainingLocus(source, chromosome, segmentsByPair[i]); intervals.add(new Interval(chromosome, segmentsByPair[i], segmentsByPair[i + 1], parentInterval.isNegativeStrand(), parentInterval.getName())); } return intervals; }