/** * Returns true if the record overlaps any intervals in list, false otherwise. * * @param refSequence Reference contig name where record maps * @param start Record alignment start * @param end Record alignment end * @return true if SAMRecord overlaps any intervals in list */ private boolean hasOverlaps(final String refSequence, final int start, final int end) { final Interval readInterval = new Interval(refSequence, start, end); final Collection<Interval> overlapsRead = intervalOverlapDetector.getOverlaps(readInterval); return !overlapsRead.isEmpty(); }
/** * Returns true if the record overlaps any intervals in list, false otherwise. * * @param refSequence Reference contig name where record maps * @param start Record alignment start * @param end Record alignment end * @return true if SAMRecord overlaps any intervals in list */ private boolean hasOverlaps(final String refSequence, final int start, final int end) { final Interval readInterval = new Interval(refSequence, start, end); final Collection<Interval> overlapsRead = intervalOverlapDetector.getOverlaps(readInterval); return !overlapsRead.isEmpty(); }
private boolean overlaps(VariantContext v) { if (intervals == null) { return true; } final Interval interval = new Interval(v.getContig(), v.getStart(), v.getEnd()); Collection<Interval> overlaps = overlapDetector.getOverlaps(interval); return !overlaps.isEmpty(); } }
@Test(dataProvider = "intervalsSameContig") public void testOverlap(final List<Locatable> input, final Interval query, final List<Locatable> expected) throws Exception { final OverlapDetector<Locatable> targetDetector = OverlapDetector.create(input); final Set<Locatable> actual = targetDetector.getOverlaps(query); Assert.assertEquals(actual, new HashSet<>(expected)); Assert.assertEquals(targetDetector.overlapsAny(query), !expected.isEmpty()); Assert.assertEquals(new HashSet<>(targetDetector.getAll()), new HashSet<>(input)); }
@Test(dataProvider = "intervalsMultipleContigs") public void testOverlap(final List<Locatable> input, final Locatable query, final Collection<Locatable> expected) throws Exception { final OverlapDetector<Locatable> targetDetector = new OverlapDetector<>(0, 0); targetDetector.addAll(input, input); final Collection<Locatable> actual = targetDetector.getOverlaps(query); Assert.assertEquals(actual, expected); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullArgGetOverlaps() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,100) ); final OverlapDetector<Locatable> targetDetector = OverlapDetector.create(input); targetDetector.getOverlaps(null); }
@Test public void testLotsOfTinyIntervals() throws Exception { final List<Locatable> input = new ArrayList<>(); final int n = 1000000; for (int i = 0; i < n; i++) { input.add(new Interval("1", 3*i+1, 3*i+2)); //1:1-2, 1:4-5, 1:7-8 } final OverlapDetector<Locatable> detector = OverlapDetector.create(input); final Set<Locatable> overlapping = detector.getOverlaps(new Interval("1", 1, 3 * n + 2)); Assert.assertEquals(new HashSet<>(input), overlapping); }
public List<PartialLiftover> diagnosticLiftover(final Interval interval) { final List<PartialLiftover> ret = new ArrayList<PartialLiftover>(); if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); } for (final Chain chain : chains.getOverlaps(interval)) { Interval intersectingChain = interval.intersect(chain.interval); final TargetIntersection targetIntersection = targetIntersection(chain, intersectingChain); if (targetIntersection == null) { ret.add(new PartialLiftover(intersectingChain, chain.id)); } else { Interval toInterval = createToInterval(interval.getName(), targetIntersection); float percentLiftedOver = targetIntersection.intersectionLength/(float)interval.length(); ret.add(new PartialLiftover(intersectingChain, toInterval, targetIntersection.chain.id, percentLiftedOver)); } } return ret; }
@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))); } }
public List<PartialLiftover> diagnosticLiftover(final Interval interval) { final List<PartialLiftover> ret = new ArrayList<PartialLiftover>(); if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); } for (final Chain chain : chains.getOverlaps(interval)) { Interval intersectingChain = interval.intersect(chain.interval); final TargetIntersection targetIntersection = targetIntersection(chain, intersectingChain); if (targetIntersection == null) { ret.add(new PartialLiftover(intersectingChain, chain.id)); } else { Interval toInterval = createToInterval(interval.getName(), interval.isNegativeStrand(), targetIntersection); float percentLiftedOver = targetIntersection.intersectionLength/(float)interval.length(); ret.add(new PartialLiftover(intersectingChain, toInterval, targetIntersection.chain.id, percentLiftedOver)); } } return ret; }
@Override public boolean apply(final VariantContext vc) { if (vc.getStart() == vc.getEnd()) { // This isn't a multi-allelic segment, so it can only be produced by a single segment. return true; } final Collection<VcfFileSegment> intersectingSegments = multiSegmentDetectorPerFile.get(sourceSegment.vcf()).getOverlaps(new Interval(vc.getContig(), vc.getStart(), vc.getEnd())); if (intersectingSegments.size() < 2) { // There's only one segment that produces this variant return true; } // The convention is: only emit the VC if it is produced from the first segment that can produce it in the list. final int sourceSegmentIndex = segments.indexOf(sourceSegment); LOG.debug("Found wide variant spanning multiple source segments: ", vc); for (final VcfFileSegment intersectingSegment : intersectingSegments) { if (segments.indexOf(intersectingSegment) < sourceSegmentIndex) { // There is a segment that produces this variant earlier in the segment list, exclude it. return false; } } LOG.debug("Emitting wide variant because it belongs to first segment: ", vc); return true; } }
public List<PartialLiftover> diagnosticLiftover(final Interval interval) { final List<PartialLiftover> ret = new ArrayList<PartialLiftover>(); if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); } for (final Chain chain : chains.getOverlaps(interval)) { Interval intersectingChain = interval.intersect(chain.interval); final TargetIntersection targetIntersection = targetIntersection(chain, intersectingChain); if (targetIntersection == null) { ret.add(new PartialLiftover(intersectingChain, chain.id)); } else { Interval toInterval = createToInterval(interval.getName(), interval.isNegativeStrand(), targetIntersection); float percentLiftedOver = targetIntersection.intersectionLength/(float)interval.length(); ret.add(new PartialLiftover(intersectingChain, toInterval, targetIntersection.chain.id, percentLiftedOver)); } } return ret; }
@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; } }
@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 testNoOverlapsAny() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,110) ); final OverlapDetector<Locatable> trimmedTargetDetector = new OverlapDetector(20,20); trimmedTargetDetector.addAll(input, input); Assert.assertFalse(trimmedTargetDetector.overlapsAny( new Interval("1",50,85)));//no overlap because of trim Assert.assertTrue(trimmedTargetDetector.getOverlaps( new Interval("1",50,85)).isEmpty());//no overlap because of trim final OverlapDetector<Locatable> untrimmedTargetDetector = new OverlapDetector(0,0); untrimmedTargetDetector.addAll(input, input); Assert.assertTrue(untrimmedTargetDetector.overlapsAny( new Interval("1",50,85)));//overlaps - no trim }
/** Calculates a few statistics about the bait design that can then be output. */ void calculateStatistics(final IntervalList targets, final IntervalList baits) { this.TARGET_TERRITORY = (int) targets.getUniqueBaseCount(); this.TARGET_COUNT = targets.size(); this.BAIT_TERRITORY = (int) baits.getUniqueBaseCount(); this.BAIT_COUNT = baits.size(); this.DESIGN_EFFICIENCY = this.TARGET_TERRITORY / (double) this.BAIT_TERRITORY; // Figure out the intersection between all targets and all baits final IntervalList tmp = new IntervalList(targets.getHeader()); final OverlapDetector<Interval> detector = new OverlapDetector<Interval>(0, 0); detector.addAll(baits.getIntervals(), baits.getIntervals()); for (final Interval target : targets) { final Collection<Interval> overlaps = detector.getOverlaps(target); if (overlaps.isEmpty()) { this.ZERO_BAIT_TARGETS++; } else { for (final Interval i : overlaps) tmp.add(target.intersect(i)); } } tmp.uniqued(); this.BAIT_TARGET_TERRITORY_INTERSECTION = (int) tmp.getBaseCount(); }
/** Calculates a few statistics about the bait design that can then be output. */ void calculateStatistics(final IntervalList targets, final IntervalList baits) { this.TARGET_TERRITORY = (int) targets.getUniqueBaseCount(); this.TARGET_COUNT = targets.size(); this.BAIT_TERRITORY = (int) baits.getUniqueBaseCount(); this.BAIT_COUNT = baits.size(); this.DESIGN_EFFICIENCY = this.TARGET_TERRITORY / (double) this.BAIT_TERRITORY; // Figure out the intersection between all targets and all baits final IntervalList tmp = new IntervalList(targets.getHeader()); final OverlapDetector<Interval> detector = new OverlapDetector<Interval>(0, 0); detector.addAll(baits.getIntervals(), baits.getIntervals()); for (final Interval target : targets) { final Collection<Interval> overlaps = detector.getOverlaps(target); if (overlaps.isEmpty()) { this.ZERO_BAIT_TARGETS++; } else { for (final Interval i : overlaps) tmp.add(target.intersect(i)); } } tmp.uniqued(); this.BAIT_TARGET_TERRITORY_INTERSECTION = (int) tmp.getBaseCount(); }
/** * A utility function for generating the intersection of two IntervalLists, checks for equal dictionaries. * * @param list1 the first IntervalList * @param list2 the second IntervalList * @return the intersection of list1 and list2. */ public static IntervalList intersection(final IntervalList list1, final IntervalList list2) { final IntervalList result; // Ensure that all the sequence dictionaries agree and merge the lists SequenceUtil.assertSequenceDictionariesEqual(list1.getHeader().getSequenceDictionary(), list2.getHeader().getSequenceDictionary()); result = new IntervalList(list1.getHeader().clone()); final OverlapDetector<Interval> detector = new OverlapDetector<>(0, 0); detector.addAll(list1.getIntervals(), list1.getIntervals()); for (final Interval i : list2.getIntervals()) { final Collection<Interval> as = detector.getOverlaps(i); for (final Interval j : as) { final Interval tmp = i.intersect(j); result.add(tmp); } } return result.uniqued(); }
/** * A utility function for generating the intersection of two IntervalLists, checks for equal dictionaries. * * @param list1 the first IntervalList * @param list2 the second IntervalList * @return the intersection of list1 and list2. */ public static IntervalList intersection(final IntervalList list1, final IntervalList list2) { final IntervalList result; // Ensure that all the sequence dictionaries agree and merge the lists SequenceUtil.assertSequenceDictionariesEqual(list1.getHeader().getSequenceDictionary(), list2.getHeader().getSequenceDictionary()); result = new IntervalList(list1.getHeader().clone()); final OverlapDetector<Interval> detector = new OverlapDetector<>(0, 0); detector.addAll(list1.getIntervals(), list1.getIntervals()); for (final Interval i : list2.getIntervals()) { final Collection<Interval> as = detector.getOverlaps(i); for (final Interval j : as) { final Interval tmp = i.intersect(j); result.add(tmp); } } return result.uniqued(); }
/** * A utility function for generating the intersection of two IntervalLists, checks for equal dictionaries. * * @param list1 the first IntervalList * @param list2 the second IntervalList * @return the intersection of list1 and list2. */ public static IntervalList intersection(final IntervalList list1, final IntervalList list2) { final IntervalList result; // Ensure that all the sequence dictionaries agree and merge the lists SequenceUtil.assertSequenceDictionariesEqual(list1.getHeader().getSequenceDictionary(), list2.getHeader().getSequenceDictionary()); result = new IntervalList(list1.getHeader().clone()); final OverlapDetector<Interval> detector = new OverlapDetector<Interval>(0, 0); detector.addAll(list1.getIntervals(), list1.getIntervals()); for (final Interval i : list2.getIntervals()) { final Collection<Interval> as = detector.getOverlaps(i); for (final Interval j : as) { final Interval tmp = i.intersect(j); result.add(tmp); } } return result.uniqued(); }