@Override public SAMRecordIterator query(final QueryInterval[] intervals, final boolean contained) { return new AssertingIterator(this.reader.query(intervals, contained)); }
void openFile() throws IOException { LOG.info("Processing shard " + shard); final SamReader reader = BAMIO.openBAM(storageClient, shard.file, options.getStringency()); iterator = null; if (reader.hasIndex() && reader.indexing() != null) { if (filter == Filter.UNMAPPED_ONLY) { LOG.info("Processing unmapped"); iterator = reader.queryUnmapped(); } else if (shard.span != null) { LOG.info("Processing span for " + shard.contig); iterator = reader.indexing().iterator(shard.span); } else if (shard.contig.referenceName != null && !shard.contig.referenceName.isEmpty()) { LOG.info("Processing all bases for " + shard.contig); iterator = reader.query(shard.contig.referenceName, (int) shard.contig.start, (int) shard.contig.end, false); } } if (iterator == null) { LOG.info("Processing all reads"); iterator = reader.iterator(); } }
public CloseableIterator<PicardAlignment> query(String sequence, int start, int end, boolean contained) { if (sequenceDictionary != null && !sequenceDictionary.containsKey(sequence)) { return EMPTY_ITERATOR; } else { CloseableIterator<SAMRecord> iter = null; try { synchronized (reader) { iter = reader.query(sequence, start + 1, end, contained); } } catch (IllegalArgumentException e) { log.error("Error querying for sequence: " + sequence, e); return new EmptyAlignmentIterator(); } return new WrappedIterator(iter); } }
void openFile() throws IOException { LOG.info("Processing shard " + shard); final SamReader reader = BAMIO.openBAM(storageClient, shard.file, options.getStringency()); iterator = null; if (reader.hasIndex() && reader.indexing() != null) { if (filter == Filter.UNMAPPED_ONLY) { LOG.info("Processing unmapped"); iterator = reader.queryUnmapped(); } else if (shard.span != null) { LOG.info("Processing span for " + shard.contig); iterator = reader.indexing().iterator(shard.span); } else if (shard.contig.referenceName != null && !shard.contig.referenceName.isEmpty()) { LOG.info("Processing all bases for " + shard.contig); iterator = reader.query(shard.contig.referenceName, (int) shard.contig.start, (int) shard.contig.end, false); } } if (iterator == null) { LOG.info("Processing all reads"); iterator = reader.iterator(); } }
@Test(dataProvider="singleIntervalOverlapping") public void testQuerySingleIntervalContainedFalse( final File cramFileName, final File referenceFileName, final QueryInterval interval, final String[] expectedNames) throws IOException { doQueryTest( reader -> reader.query(new QueryInterval[]{interval}, false), cramFileName, referenceFileName, expectedNames ); }
final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false); writeReads(out, it, newOrder, contig.getSequenceName());
@Test(dataProvider="singleIntervalContained") public void testQuerySingleIntervalContainedTrue( final File cramFileName, final File referenceFileName, final QueryInterval interval, final String[] expectedNames) throws IOException { doQueryTest( reader -> reader.query(new QueryInterval[]{interval}, true), cramFileName, referenceFileName, expectedNames ); }
@Test(dataProvider = "SmallCRAMTest") public void CRAMIndexTest(final String inputFile, final String referenceFile, QueryInterval queryInterval, String expectedReadName) throws IOException { final File input = new File(TEST_DATA_DIR, inputFile); final File reference = new File(TEST_DATA_DIR, referenceFile); try(final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(reference).open(input)) { Assert.assertTrue(reader.hasIndex()); final CloseableIterator<SAMRecord> iterator = reader.query(new QueryInterval[]{queryInterval}, false); Assert.assertTrue(iterator.hasNext()); SAMRecord r1 = iterator.next(); Assert.assertEquals(r1.getReadName(), expectedReadName); } }
@Test(dataProvider = "queryIntervalsData") public void testQueryIntervals(final boolean contained, final int expected) { final SamReader reader = SamReaderFactory.makeDefault().enable().open(BAM_FILE); final CloseableIterator<SAMRecord> it = reader.query("chr1", 202661637, 202661812, contained); Assert.assertEquals(countElements(it), expected); }
@Test public void testCRAMReaderWithCRAIFromNonFilePath() throws IOException { final File cramFile = new File(TEST_DATA_DIR, "cram/cramQueryWithCRAI.cram"); final File cramIndex = new File(TEST_DATA_DIR, "cram/cramQueryWithCRAI.cram.crai"); final File referenceFile = new File(TEST_DATA_DIR, "cram/human_g1k_v37.20.21.10M-10M200k.fasta"); try (final FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix())) { final Path jimfsCRAM = jimfs.getPath("acram.cram"); final Path jimfsCRAI = jimfs.getPath("acram.crai"); Files.copy(cramFile.toPath(), jimfsCRAM); Files.copy(cramIndex.toPath(), jimfsCRAI); final SamReaderFactory factory = SamReaderFactory.makeDefault() .referenceSource(new ReferenceSource(referenceFile)) .validationStringency(ValidationStringency.SILENT); // force SamReaderFactory through the CRAM code path for a CRAM with an index that can't be rendered as a file try (final SamReader cramReader = factory.open(jimfsCRAM)) { final SAMRecordIterator samIt = cramReader.query("20", 1, 1, false); Assert.assertNotNull(samIt); }; } }
final SamReader reader = SamReaderFactory.makeDefault().open(BAM_FILE); for (final QueryInterval interval : intervals) { consumeAll(singleIntervalRecords, reader.query(referenceNames.get(interval.referenceIndex), interval.start, interval.end, contained)); consumeAll(multiIntervalRecords, reader.query(optimizedIntervals, contained)); final Iterator<SAMRecord> singleIntervalRecordIterator = singleIntervalRecords.iterator(); boolean failed = false;
private void runLocalRemoteTest(final URL bamURL, final File bamFile, final String sequence, final int startPos, final int endPos, final boolean contained) { verbose("Testing query " + sequence + ":" + startPos + "-" + endPos + " ..."); final SamReader reader1 = SamReaderFactory.makeDefault() .disable(SamReaderFactory.Option.EAGERLY_DECODE) .open(SamInputResource.of(bamFile).index(BAM_INDEX_FILE)); final SamReader reader2 = SamReaderFactory.makeDefault() .disable(SamReaderFactory.Option.EAGERLY_DECODE) .open(SamInputResource.of(bamURL).index(BAM_INDEX_FILE)); final Iterator<SAMRecord> iter1 = reader1.query(sequence, startPos, endPos, contained); final Iterator<SAMRecord> iter2 = reader2.query(sequence, startPos, endPos, contained); final List<SAMRecord> records1 = new ArrayList<SAMRecord>(); final List<SAMRecord> records2 = new ArrayList<SAMRecord>(); while (iter1.hasNext()) { records1.add(iter1.next()); } while (iter2.hasNext()) { records2.add(iter2.next()); } assertTrue(records1.size() > 0); assertEquals(records1.size(), records2.size()); for (int i = 0; i < records1.size(); i++) { //System.out.println(records1.get(i).format()); assertEquals(records1.get(i).getSAMString(), records2.get(i).getSAMString()); } }
final SAMRecordIterator recordIterator = samReader.query( firstContig.referenceName, (int)firstContig.start + 1, (int)firstContig.end + 1, false);
final SAMRecordIterator recordIterator = samReader.query( firstContig.referenceName, (int)firstContig.start + 1, (int)firstContig.end + 1, false);
public <T> BamIterator<T> iterator(Region region, AlignmentFilters<SAMRecord> filters, AlignmentOptions options, Class<T> clazz) throws IOException { init(); checkBaiFileExists(); if (options == null) { options = new AlignmentOptions(); } SAMRecordIterator samRecordIterator = samReader.query(region.getChromosome(), region.getStart(), region.getEnd(), options.isContained()); return getAlignmentIterator(filters, options, clazz, samRecordIterator); }
@Test(dataProvider="singleIntervalOverlapping") public void testQuerySequenceContainedFalse( final File cramFileName, final File referenceFileName, final QueryInterval interval, final String[] expectedNames) throws IOException { doQueryTest( reader -> reader.query( reader.getFileHeader().getSequence(interval.referenceIndex).getSequenceName(), interval.start, interval.end, false ), cramFileName, referenceFileName, expectedNames ); }
@Test(dataProvider="singleIntervalContained") public void testQuerySequenceContainedTrue( final File cramFileName, final File referenceFileName, final QueryInterval interval, final String[] expectedNames) throws IOException { doQueryTest( reader -> reader.query( reader.getFileHeader().getSequence(interval.referenceIndex).getSequenceName(), interval.start, interval.end, true ), cramFileName, referenceFileName, expectedNames ); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void queryInputResourcePermutation(final SamInputResource resource) throws IOException { try(final SamReader reader = SamReaderFactory.makeDefault().open(resource)) { LOG.info(String.format("Query from %s ...", resource)); if (reader.hasIndex()) { final StopWatch stopWatch = new StopWatch(); stopWatch.start(); final SAMRecordIterator q1 = reader.query("chr1", 500000, 100000000, true); observedRecordOrdering1.add(Iterables.slurp(q1)); q1.close(); final SAMRecordIterator q20 = reader.query("chr20", 1, 1000000, true); observedRecordOrdering20.add(Iterables.slurp(q20)); q20.close(); final SAMRecordIterator q3 = reader.query("chr3", 1, 10000000, true); observedRecordOrdering3.add(Iterables.slurp(q3)); q3.close(); stopWatch.stop(); LOG.info(String.format("Finished queries in %sms", stopWatch.getElapsedTime())); Assert.assertEquals(observedRecordOrdering1.size(), 1, "read different records for chromosome 1"); Assert.assertEquals(observedRecordOrdering20.size(), 1, "read different records for chromosome 20"); Assert.assertEquals(observedRecordOrdering3.size(), 1, "read different records for chromosome 3"); } else if (resource.indexMaybe() != null) { LOG.warn("Resource has an index source, but is not indexed: " + resource); } else { LOG.info("Skipping query operation: no index."); } } }
@Test(dataProvider = "testQueryCounts") public void testQueryCounts(String acc, String reference, int referenceStart, int referenceEnd, boolean contained, int expectedNumMapped, int expectedNumUnmapped) { SamReader reader = SamReaderFactory.make().validationStringency(ValidationStringency.SILENT).open( SamInputResource.of(new SRAAccession(acc)) ); final SAMRecordIterator samRecordIterator = reader.query(reference, referenceStart, referenceEnd, contained); assertCorrectCountsOfMappedAndUnmappedRecords(samRecordIterator, expectedNumMapped, expectedNumUnmapped); }
@Test public void openBAMTest() throws IOException { GCSOptions popts = PipelineOptionsFactory.create().as(GCSOptions.class); final Storage.Objects storageClient = Transport.newStorageClient(popts).build().objects(); SamReader samReader = BAMIO.openBAM(storageClient, TEST_BAM_FNAME, ValidationStringency.DEFAULT_STRINGENCY); SAMRecordIterator iterator = samReader.query("1", 550000, 560000, false); int readCount = 0; while (iterator.hasNext()) { iterator.next(); readCount++; } Assert.assertEquals("Unexpected count of unmapped reads", EXPECTED_UNMAPPED_READS_COUNT, readCount); } }