@Override public CloseableIterator<SAMRecord> queryAlignmentStart(final String sequence, final int start) { final SAMFileHeader fileHeader = getFileHeader(); final int referenceIndex = fileHeader.getSequenceIndex(sequence); return new CRAMIntervalIterator(new QueryInterval[]{new QueryInterval(referenceIndex, start, -1)}, true); }
@Override public CloseableIterator<SAMRecord> queryAlignmentStart(final String sequence, final int start) { final SAMFileHeader fileHeader = getFileHeader(); final int referenceIndex = fileHeader.getSequenceIndex(sequence); return new CRAMIntervalIterator(new QueryInterval[]{new QueryInterval(referenceIndex, start, -1)}, true); }
@Override public BAMIndex getIndex() { if (!hasIndex()) throw new SAMException("No index is available for this CRAM file."); if (mIndex == null) { final SAMSequenceDictionary dictionary = getFileHeader() .getSequenceDictionary(); if (mIndexFile.getName().endsWith(BAMIndex.BAI_INDEX_SUFFIX)) { mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexFile, dictionary, mEnableIndexMemoryMapping) : new DiskBasedBAMFileIndex(mIndexFile, dictionary, mEnableIndexMemoryMapping); return mIndex; } if (!mIndexFile.getName().endsWith(CRAIIndex.CRAI_INDEX_SUFFIX)) return null; // convert CRAI into BAI: final SeekableStream baiStream; try { baiStream = SamIndexes.asBaiSeekableStreamOrNull(new SeekableFileStream(mIndexFile), iterator.getSAMFileHeader().getSequenceDictionary()); } catch (IOException e) { throw new RuntimeException(e); } mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(baiStream, getFileHeader().getSequenceDictionary()) : new DiskBasedBAMFileIndex(baiStream, getFileHeader().getSequenceDictionary()); } return mIndex; }
@Override public BAMIndex getIndex() { if (!hasIndex()) throw new SAMException("No index is available for this CRAM file."); if (mIndex == null) { final SAMSequenceDictionary dictionary = getFileHeader() .getSequenceDictionary(); if (mIndexFile.getName().endsWith(BAMIndex.BAMIndexSuffix)) { mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexFile, dictionary, mEnableIndexMemoryMapping) : new DiskBasedBAMFileIndex(mIndexFile, dictionary, mEnableIndexMemoryMapping); return mIndex; } if (!mIndexFile.getName().endsWith(CRAIIndex.CRAI_INDEX_SUFFIX)) return null; // convert CRAI into BAI: final SeekableStream baiStream; try { baiStream = SamIndexes.asBaiSeekableStreamOrNull(new SeekableFileStream(mIndexFile), iterator.getSAMFileHeader().getSequenceDictionary()); } catch (IOException e) { throw new RuntimeException(e); } mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(baiStream, getFileHeader().getSequenceDictionary()) : new DiskBasedBAMFileIndex(baiStream, getFileHeader().getSequenceDictionary()); } return mIndex; }
@Test public void testCRAIIndexerFromStream() throws IOException { final File CRAMFile = new File("src/test/resources/htsjdk/samtools/cram/test2.cram"); final File refFile = new File("src/test/resources/htsjdk/samtools/cram/auxf.fa"); ReferenceSource refSource = new ReferenceSource(refFile); // get the header to use CRAMFileReader reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); SAMFileHeader samHeader = reader.getFileHeader(); reader.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CRAMCRAIIndexer craiIndexer = new CRAMCRAIIndexer(bos, samHeader); craiIndexer.writeIndex(new SeekableFileStream(CRAMFile), bos); List<CRAIEntry> craiEntries = CRAMCRAIIndexer.readIndex(new ByteArrayInputStream(bos.toByteArray())).getCRAIEntries(); Assert.assertEquals(craiEntries.size(), 1); }
@Test public void testCRAIIndexerFromContainer() throws IOException { final File CRAMFile = new File("src/test/resources/htsjdk/samtools/cram/test2.cram"); final File refFile = new File("src/test/resources/htsjdk/samtools/cram/auxf.fa"); ReferenceSource refSource = new ReferenceSource(refFile); CRAMFileReader reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); SAMFileHeader samHeader = reader.getFileHeader(); Iterator<SAMRecord> it = reader.getIterator(); while(it.hasNext()) { SAMRecord samRec = it.next(); } reader.close(); FileInputStream fis = new FileInputStream(CRAMFile); CramContainerIterator cit = new CramContainerIterator(fis); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CRAMCRAIIndexer craiIndexer = new CRAMCRAIIndexer(bos, samHeader); while (cit.hasNext()) { craiIndexer.processContainer(cit.next()); } craiIndexer.finish(); bos.close(); List<CRAIEntry> craiEntries = CRAMCRAIIndexer.readIndex(new ByteArrayInputStream(bos.toByteArray())).getCRAIEntries(); Assert.assertEquals(craiEntries.size(), 1); }
@Test public void test() throws IOException { CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(indexBytes), source, ValidationStringency.SILENT); for (SAMSequenceRecord sequenceRecord : reader.getFileHeader().getSequenceDictionary().getSequences()) { final CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart(sequenceRecord.getSequenceName(), 1); Assert.assertNotNull(iterator); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), sequenceRecord.getSequenceName()); Assert.assertEquals(record.getAlignmentStart(), 1); } }