Search in sources :

Example 26 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class IntervalsSkipListOneContigUnitTest method testManyIntervals.

@Test
public void testManyIntervals() throws Exception {
    ArrayList<Locatable> si = new ArrayList<>();
    final int MAX = 10_000_000;
    for (int start = 1; start < MAX; start += 100) {
        si.add(new SimpleInterval("1", start, start + 10));
        si.add(new SimpleInterval("1", start, start + 200));
    }
    Stopwatch indexing = Stopwatch.createStarted();
    IntervalsSkipListOneContig<Locatable> ints = new IntervalsSkipListOneContig<>(si);
    indexing.stop();
    Stopwatch v1 = Stopwatch.createStarted();
    for (int start = 101; start < MAX; start += 5000) {
        SimpleInterval interval = new SimpleInterval("1", start + 10, start + 11);
        List<Locatable> actual = ints.getOverlappingIgnoringIndex(interval);
        Assert.assertEquals(actual.size(), 3);
        // the one that starts from start-200 ends before our test point.
        for (Locatable l : actual) {
            Assert.assertTrue(interval.overlaps(l));
        }
    }
    v1.stop();
    Stopwatch v2 = Stopwatch.createStarted();
    for (int start = 101; start < MAX; start += 5000) {
        SimpleInterval interval = new SimpleInterval("1", start + 10, start + 11);
        List<Locatable> actual = ints.getOverlapping(interval);
        Assert.assertEquals(actual.size(), 3);
        // the one that starts from start-200 ends before our test point.
        for (Locatable l : actual) {
            Assert.assertTrue(interval.overlaps(l));
        }
    }
    v2.stop();
    System.out.println("non-indexed took " + v1.elapsed(TimeUnit.MILLISECONDS) + " ms, " + " indexed took " + v2.elapsed(TimeUnit.MILLISECONDS) + " ms, plus " + indexing.elapsed(TimeUnit.MILLISECONDS) + " for sorting&indexing.");
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) Locatable(htsjdk.samtools.util.Locatable) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 27 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class ReadCountCollectionUtils method writeReadCountsFromSimpleInterval.

/**
     * Write a read counts file of targets with coverage to file with dummy names
     * @param outWriter Writer to write targets with coverage. Never {@code null}
     * @param outName Name of the output writer. Never {@code null}
     * @param sampleName Name of sample being written. Never {@code null}
     * @param byKeySorted Map of simple-intervals to their copy-ratio. Never {@code null}
     * @param comments Comments to add to header of coverage file.
     */
public static <N extends Number> void writeReadCountsFromSimpleInterval(final Writer outWriter, final String outName, final String sampleName, final SortedMap<SimpleInterval, N> byKeySorted, final String[] comments) {
    Utils.nonNull(outWriter, "Output writer cannot be null.");
    Utils.nonNull(sampleName, "Sample name cannot be null.");
    Utils.nonNull(byKeySorted, "Targets cannot be null.");
    Utils.nonNull(comments, "Comments cannot be null.");
    final boolean areTargetIntervalsAllPopulated = byKeySorted.keySet().stream().allMatch(t -> t != null);
    if (!areTargetIntervalsAllPopulated) {
        throw new UserException("Cannot write target coverage file with any null intervals.");
    }
    try (final TableWriter<ReadCountRecord> writer = writerWithIntervals(outWriter, Collections.singletonList(sampleName))) {
        for (String comment : comments) {
            writer.writeComment(comment);
        }
        for (final Locatable locatable : byKeySorted.keySet()) {
            final SimpleInterval interval = new SimpleInterval(locatable);
            final double coverage = byKeySorted.get(locatable).doubleValue();
            writer.writeRecord(new ReadCountRecord.SingleSampleRecord(new Target(interval), coverage));
        }
    } catch (final IOException e) {
        throw new UserException.CouldNotCreateOutputFile(outName, e);
    }
}
Also used : SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) UserException(org.broadinstitute.hellbender.exceptions.UserException) Locatable(htsjdk.samtools.util.Locatable)

Example 28 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class SegmentUtils method fixLegacyTargetSegmentStarts.

/*===============================================================================================================*
     * PUBLIC METHODS FOR SEGMENT UNION (COMBINING TARGET AND SNP SEGMENTS)                                          *
     *===============================================================================================================*/
/**
     * Legacy target segments from CBS (i.e., from legacy versions of {@link PerformSegmentation})
     * are specified by target-end--target-end intervals.  This method converts these to the corresponding
     * target-start--target-end intervals, returning a new, modifiable list of segments.  Non-legacy segments that are
     * already specified by target-start--target-end intervals are also returned as a new, modifiable list of segments
     * with no changes.
     */
public static List<SimpleInterval> fixLegacyTargetSegmentStarts(final List<SimpleInterval> targetSegments, final TargetCollection<? extends Locatable> targets) {
    Utils.nonNull(targetSegments, "The list of target segments cannot be null.");
    Utils.nonNull(targets, "The collection of targets cannot be null.");
    final List<SimpleInterval> targetSegmentsFixed = new ArrayList<>();
    for (final SimpleInterval segment : targetSegments) {
        try {
            final Locatable firstTarget = targets.targets(segment).get(0);
            Utils.validateArg(firstTarget.getEnd() == segment.getStart() || firstTarget.getStart() == segment.getStart(), "List of targets and segments do not correspond.");
            targetSegmentsFixed.add(new SimpleInterval(segment.getContig(), firstTarget.getStart(), segment.getEnd()));
        } catch (final IndexOutOfBoundsException ex) {
            throw new IllegalArgumentException("List of targets and segments do not correspond.");
        }
    }
    return targetSegmentsFixed;
}
Also used : SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) Locatable(htsjdk.samtools.util.Locatable)

Example 29 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class ReadPileupUnitTest method testSortedIterator.

@Test
public void testSortedIterator() throws Exception {
    final SAMFileHeader header = ArtificialReadUtils.createArtificialSamHeader();
    final GATKRead read1 = ArtificialReadUtils.createArtificialRead(header, "10M");
    final GATKRead read2 = ArtificialReadUtils.createArtificialRead(header, "10M");
    read1.setPosition(new SimpleInterval("22", 200, 210));
    read2.setPosition(new SimpleInterval("22", 208, 218));
    read1.setMatePosition(read2);
    read2.setMatePosition(read1);
    final Locatable loc = new SimpleInterval("22", 208, 208);
    final Map<String, ReadPileup> stratified = new LinkedHashMap<>();
    stratified.put("sample1", new ReadPileup(loc, Arrays.asList(read2), 0));
    stratified.put("sample2", new ReadPileup(loc, Arrays.asList(read1), 9));
    final ReadPileup combined = new ReadPileup(loc, stratified);
    final Iterator<PileupElement> sortedIterator = combined.sortedIterator();
    Assert.assertSame(sortedIterator.next().getRead(), read1);
    Assert.assertSame(sortedIterator.next().getRead(), read2);
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) SAMFileHeader(htsjdk.samtools.SAMFileHeader) Locatable(htsjdk.samtools.util.Locatable) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 30 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class IntervalUtilsUnitTest method testGenomeLocsFromLocatables.

@Test(dataProvider = "genomeLocsFromLocatablesData")
public void testGenomeLocsFromLocatables(final GenomeLocParser parser, final List<? extends Locatable> locatables) {
    final List<GenomeLoc> result = IntervalUtils.genomeLocsFromLocatables(parser, locatables);
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), locatables.size());
    for (int i = 0; i < result.size(); i++) {
        final GenomeLoc resultLoc = result.get(i);
        final Locatable inputLoc = locatables.get(i);
        Assert.assertEquals(resultLoc.getContig(), inputLoc.getContig());
        Assert.assertEquals(resultLoc.getStart(), inputLoc.getStart());
        Assert.assertEquals(resultLoc.getStop(), inputLoc.getEnd());
    }
    // is it real only:
    try {
        result.add(parser.createGenomeLoc("1", 1, 2));
        Assert.fail("the result collection should not allow to call add");
    } catch (final UnsupportedOperationException ex) {
    // ok.
    }
    if (result.size() > 0) {
        try {
            result.set(0, parser.createGenomeLoc("1", 1, 2));
            Assert.fail("the result collection should not allow to call set");
        } catch (final UnsupportedOperationException ex) {
        // ok.
        }
    }
}
Also used : Locatable(htsjdk.samtools.util.Locatable) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Locatable (htsjdk.samtools.util.Locatable)38 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)24 Test (org.testng.annotations.Test)22 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)20 ReadPileup (org.broadinstitute.hellbender.utils.pileup.ReadPileup)11 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)10 SAMFileHeader (htsjdk.samtools.SAMFileHeader)6 ArrayList (java.util.ArrayList)5 UserException (org.broadinstitute.hellbender.exceptions.UserException)5 DataProvider (org.testng.annotations.DataProvider)4 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)3 com.google.common.collect (com.google.common.collect)2 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)2 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)2 OverlapDetector (htsjdk.samtools.util.OverlapDetector)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 java.util (java.util)2 LinkedHashMap (java.util.LinkedHashMap)2 Collectors (java.util.stream.Collectors)2