@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); }
/** * Read all the chains and load into an OverlapDetector. * @param chainFile File in UCSC chain format. * @return OverlapDetector will all Chains from reader loaded into it. */ static OverlapDetector<Chain> loadChains(final File chainFile) { final BufferedLineReader reader = new BufferedLineReader(IOUtil.openFileForReading(chainFile)); final OverlapDetector<Chain> ret = new OverlapDetector<Chain>(0, 0); Chain chain; while ((chain = Chain.loadChain(reader, chainFile.toString())) != null) { ret.addLhs(chain, chain.interval); } reader.close(); return ret; }
@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)); }
/** * Creates a new OverlapDetector with no trim and the given set of intervals. */ public static <T extends Locatable> OverlapDetector<T> create(final List<T> intervals) { final OverlapDetector<T> detector = new OverlapDetector<>(0, 0); detector.addAll(intervals, intervals); return detector; }
Threadsafe(final VcfFileSegmentGenerator segmenter, final List<File> vcfs, final IntervalList intervals) { if (intervals != null) { final List<Interval> uniques = intervals.uniqued(false).getIntervals(); this.intervalsOfInterestDetector = new OverlapDetector<>(0, 0); intervalsOfInterestDetector.addAll(uniques, uniques); } else { intervalsOfInterestDetector = null; final Interval segmentInterval = segment.correspondingInterval(); final OverlapDetector<VcfFileSegment> vcfSpecificDetector = multiSegmentDetectorPerFile.get(segment.vcf()); if (vcfSpecificDetector.getOverlaps(segmentInterval).isEmpty()) { vcfSpecificDetector.addLhs(segment, new Interval(segment.contig(), segment.start(), segment.stop())); } else { throw new IllegalArgumentException(String.format( "Provided segmenting strategy produced overlapping intervals; %s overlaps with: %s", segment, Joiner.on(", ").join(vcfSpecificDetector.getOverlaps(segmentInterval)) ));
@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 }
overlapIntervals.addall(list.getIntervals()); final OverlapDetector<Integer> detector = new OverlapDetector<>(0, 0); final int dummy = -1; // NB: since we don't actually use the returned objects, we can use a dummy value for (final Interval interval : overlapIntervals.sorted().uniqued()) { detector.addLhs(dummy, interval); list.getHeader().getSequenceDictionary()); for (final Interval interval : list.getIntervals()) { if (detector.overlapsAny(interval)) { merged.add(interval);
/** * 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(); }
@Test public void testSamLocusAndReferenceIterator() { final File reference = new File(TEST_DATA_DIR, "Homo_sapiens_assembly18.trimmed.fasta"); final File samFile = new File(TEST_DATA_DIR, "simpleSmallFile.sam"); final ReferenceSequenceFile referenceSequenceFile = new FastaSequenceFile(reference, false); final ReferenceSequenceFileWalker referenceSequenceFileWalker = new ReferenceSequenceFileWalker(referenceSequenceFile); final SamReader samReader = SamReaderFactory.makeDefault().open(samFile); final SamLocusIterator samLocusIterator = new SamLocusIterator(samReader); final SamLocusAndReferenceIterator samLocusAndReferences = new SamLocusAndReferenceIterator(referenceSequenceFileWalker, samLocusIterator); IntervalList intervalList = new IntervalList(samReader.getFileHeader()); intervalList.add(new Interval("chrM", 1, 36)); intervalList.add(new Interval("chr20", 8401, 8460)); OverlapDetector<Interval> overlapDetector = new OverlapDetector<>(0, 0); overlapDetector.addAll(intervalList.getIntervals(), intervalList.getIntervals()); for (final SamLocusAndReferenceIterator.SAMLocusAndReference samLocusAndReference : samLocusAndReferences) { // The sam file only has coverage in the intervals that are within 'intervalList', and there the coverage should // be exactly 2 since there are two overlapping, paired reads. This is what this test is testing: Assert.assertEquals(samLocusAndReference.getRecordAndOffsets().size(), overlapDetector.overlapsAny(samLocusAndReference.getLocus()) ? 2 : 0, "Position:" + samLocusAndReference.getLocus().toString()); // all the reads are equal to the reference...this is what this test is testing. for (final SamLocusIterator.RecordAndOffset recordAndOffset : samLocusAndReference.getRecordAndOffsets()) Assert.assertTrue(SequenceUtil.basesEqual(samLocusAndReference.getReferenceBase(), recordAndOffset.getReadBase()), "Record: " + recordAndOffset.getRecord() + " Position:" + samLocusAndReference.getLocus().toString()); } }
/** * Derived class may override this method in order to return a different set of genes for * picking transcripts for computing coverage, or to cache the value returned by geneOverlapDetector.getAll(), * because it can be expensive to compute repeatedly. */ protected Set<Gene> getGenesForPickTranscripts() { return geneOverlapDetector.getAll(); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectsAddAll() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,100) ); final OverlapDetector<Locatable> targetDetector = OverlapDetector.create(input); targetDetector.addAll(null, Arrays.asList(new Interval("2",10,100))); }
@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(expectedExceptions = IllegalArgumentException.class) public void testNullObjectAddLHS() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,100) ); final OverlapDetector<Locatable> targetDetector = OverlapDetector.create(input); targetDetector.addLhs(null, new Interval("2",10,100)); }
/** Adds all items to the overlap detector. */ public void addAll(List<T> objects, List<Interval> intervals) { if (objects.size() != intervals.size()) { throw new IllegalArgumentException("Objects and intervals must be the same size."); } for (int i=0; i<objects.size(); ++i) { addLhs(objects.get(i), intervals.get(i)); } }
private boolean overlaps(VariantContext v) { if (intervals == null) { return true; } final Interval interval = new Interval(v.getContig(), v.getStart(), v.getEnd()); return overlapDetector.overlapsAny(interval); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOverlapsNullArg() throws Exception { final List<Locatable> input = Arrays.asList( new Interval("1",10,100) ); final OverlapDetector<Locatable> targetDetector = OverlapDetector.create(input); targetDetector.overlapsAny(null); }
overlapDetector = OverlapDetector.create(intervals);
/** * Prepare to filter out SAMRecords that do not overlap the given list of * intervals * @param intervals */ public IntervalKeepPairFilter(final List<Interval> intervals) { this.intervalOverlapDetector = new OverlapDetector<>(0, 0); this.intervalOverlapDetector.addAll(intervals, intervals); }
Threadsafe(final VcfFileSegmentGenerator segmenter, final List<File> vcfs, final IntervalList intervals) { if (intervals != null) { final List<Interval> uniques = intervals.uniqued(false).getIntervals(); this.intervalsOfInterestDetector = new OverlapDetector<>(0, 0); intervalsOfInterestDetector.addAll(uniques, uniques); } else { intervalsOfInterestDetector = null; final Interval segmentInterval = segment.correspondingInterval(); final OverlapDetector<VcfFileSegment> vcfSpecificDetector = multiSegmentDetectorPerFile.get(segment.vcf()); if (vcfSpecificDetector.getOverlaps(segmentInterval).isEmpty()) { vcfSpecificDetector.addLhs(segment, new Interval(segment.contig(), segment.start(), segment.stop())); } else { throw new IllegalArgumentException(String.format( "Provided segmenting strategy produced overlapping intervals; %s overlaps with: %s", segment, Joiner.on(", ").join(vcfSpecificDetector.getOverlaps(segmentInterval)) ));
overlapIntervals.addall(list.getIntervals()); final OverlapDetector<Integer> detector = new OverlapDetector<>(0, 0); final int dummy = -1; // NB: since we don't actually use the returned objects, we can use a dummy value for (final Interval interval : overlapIntervals.sorted().uniqued()) { detector.addLhs(dummy, interval); list.getHeader().getSequenceDictionary()); for (final Interval interval : list.getIntervals()) { if (detector.overlapsAny(interval)) { merged.add(interval);