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.");
}
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);
}
}
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;
}
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);
}
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.
}
}
}
Aggregations