/** * Prepare to iterate through the given SAM records, skipping non-primary alignments. * * @param samReader must be coordinate sorted * @param intervalList Either the list of desired intervals, or null. Note that if an intervalList is * passed in that is not coordinate sorted, it will eventually be coordinated sorted by this class. */ public EdgeReadIterator(final SamReader samReader, final IntervalList intervalList) { this(samReader, intervalList, samReader.hasIndex()); }
/** * Prepare to iterate through the given SAM records, skipping non-primary alignments. * * @param samReader must be coordinate sorted * @param intervalList Either the list of desired intervals, or null. Note that if an intervalList is * passed in that is not coordinate sorted, it will eventually be coordinated sorted by this class. */ public EdgeReadIterator(final SamReader samReader, final IntervalList intervalList) { this(samReader, intervalList, samReader.hasIndex()); }
/** * Prepare to iterate through the given SAM records, skipping non-primary alignments. Do not use * BAM index even if available. * * @param samReader must be coordinate sorted * @param intervalList Either the list of desired intervals, or null. Note that if an intervalList is * passed in that is not coordinate sorted, it will eventually be coordinated sorted by this class. */ public SamLocusIterator(final SamReader samReader, final IntervalList intervalList) { this(samReader, intervalList, samReader.hasIndex()); }
/** * Prepare to iterate through the given SAM records, skipping non-primary alignments. Do not use * BAM index even if available. * * @param samReader must be coordinate sorted * @param intervalList Either the list of desired intervals, or null. Note that if an intervalList is * passed in that is not coordinate sorted, it will eventually be coordinated sorted by this class. */ public SamLocusIterator(final SamReader samReader, final IntervalList intervalList) { this(samReader, intervalList, samReader.hasIndex()); }
@Override public boolean hasIndex() { return this.reader.hasIndex(); }
void processHeader() { contigsByReference = Maps.newHashMap(); for (Contig contig : requestedContigs) { contigsByReference.put(contig.referenceName != null ? contig.referenceName : "", contig); } if (contigsByReference.size() == 0 || contigsByReference.containsKey("*")) { LOG.info("Outputting unmapped reads shard "); output.output(new BAMShard(filePath, null, new Contig("*", 0, -1))); } allReferences = contigsByReference.size() == 0 || contigsByReference.containsKey(""); LOG.info("All references = " + allReferences); LOG.info("BAM has index = " + reader.hasIndex()); LOG.info("BAM has browseable index = " + reader.indexing().hasBrowseableIndex()); LOG.info("Class for index = " + reader.indexing().getIndex().getClass().getName()); }
void processHeader() { contigsByReference = Maps.newHashMap(); for (Contig contig : requestedContigs) { contigsByReference.put(contig.referenceName != null ? contig.referenceName : "", contig); } if (contigsByReference.size() == 0 || contigsByReference.containsKey("*")) { LOG.info("Outputting unmapped reads shard "); output.output(new BAMShard(filePath, null, new Contig("*", 0, -1))); } allReferences = contigsByReference.size() == 0 || contigsByReference.containsKey(""); LOG.info("All references = " + allReferences); LOG.info("BAM has index = " + reader.hasIndex()); LOG.info("BAM has browseable index = " + reader.indexing().hasBrowseableIndex()); LOG.info("Class for index = " + reader.indexing().getIndex().getClass().getName()); }
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(); } }
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 = "NoIndexCRAMTest") public void CRAMNoIndexTest(final String inputFile, final String referenceFile) 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.assertFalse(reader.hasIndex()); } }
void openFile() throws IOException { final BAMIO.ReaderAndIndex r = BAMIO.openBAMAndExposeIndex(storageClient, filePath, ValidationStringency.DEFAULT_STRINGENCY); reader = r.reader; indexStream = r.index; header = reader.getFileHeader(); hasIndex = reader.hasIndex() && reader.indexing().hasBrowseableIndex(); LOG.info("Has index = " + hasIndex); if (hasIndex) { index = new BAMFileIndexImpl( IOUtil.maybeBufferedSeekableStream(indexStream),header.getSequenceDictionary()); } else { index = null; } }
void openFile() throws IOException { final BAMIO.ReaderAndIndex r = BAMIO.openBAMAndExposeIndex(storageClient, filePath, ValidationStringency.DEFAULT_STRINGENCY); reader = r.reader; indexStream = r.index; header = reader.getFileHeader(); hasIndex = reader.hasIndex() && reader.indexing().hasBrowseableIndex(); LOG.info("Has index = " + hasIndex); if (hasIndex) { index = new BAMFileIndexImpl( IOUtil.maybeBufferedSeekableStream(indexStream),header.getSequenceDictionary()); } else { index = null; } }
@Test public void checkHasIndexForStreamingPathBamWithFileIndex() throws IOException { InputResource bam = new NeverFilePathInputResource(localBam.toPath()); InputResource index = new FileInputResource(localBamIndex); // ensure that the index is being used, not checked in queryInputResourcePermutation try (final SamReader reader = SamReaderFactory.makeDefault().open(new SamInputResource(bam, index))) { Assert.assertTrue(reader.hasIndex()); } }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void exhaustInputResourcePermutation(final SamInputResource resource) throws IOException { try(final SamReader reader = SamReaderFactory.makeDefault().open(resource)) { LOG.info(String.format("Reading from %s ...", resource)); final List<SAMRecord> slurped = Iterables.slurp(reader); final SAMFileHeader fileHeader = reader.getFileHeader(); reader.hasIndex(); reader.indexing().hasBrowseableIndex(); /* Ensure all tests have read the same records in the same order or, if this is the first test, set it as the template. */ observedHeaders.add(fileHeader); observedRecordOrdering.add(slurped); } Assert.assertEquals(observedHeaders.size(), 1, "read different headers than other testcases"); Assert.assertEquals(observedRecordOrdering.size(), 1, "read different records than other testcases"); }
@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); } }
public static boolean bamHasIndex(String bam) throws IOException{ /* ------------------------------------------------------ */ /* This chunk prepares SamReader from local bam or URL bam */ UrlValidator urlValidator = new UrlValidator(); SamReaderFactory srf=SamReaderFactory.make(); srf.validationStringency(ValidationStringency.SILENT); SamReader samReader; if(urlValidator.isValid(bam)){ samReader = SamReaderFactory.makeDefault().open( SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai")) ); } else { samReader= srf.open(new File(bam)); } /* ------------------------------------------------------ */ // SamReaderFactory srf=SamReaderFactory.make(); // srf.validationStringency(ValidationStringency.SILENT); // SamReader samReader = srf.open(new File(bam)); boolean hasIndex= samReader.hasIndex(); samReader.close(); return hasIndex; }
@BeforeClass public void init() throws IOException { final SamReader reader = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES).open(bamFile); Assert.assertTrue(reader.hasIndex()); Assert.assertTrue(reader.indexing().hasBrowseableIndex()); bamIndex = new GATKBAMIndexFromDataSource(bamFile, reader.getFileHeader(), reader.indexing().getBrowseableIndex()); reader.close(); }
Assert.assertTrue(reader.hasIndex()); final Iterator<SAMRecord> bamIt = reader.iterator(); int nReadsFromBam = 0;
@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."); } } }
public static long countSamTotalRecord(final File samFile) { final SamReader reader = SamReaderFactory.make().open(samFile); assert reader.hasIndex(); long total = 0; for (int i = 0; i < reader.getFileHeader().getSequenceDictionary().size(); i++) { total += reader.indexing().getIndex().getMetaData(i).getAlignedRecordCount(); total += reader.indexing().getIndex().getMetaData(i).getUnalignedRecordCount(); } return total; } }