public static void main(String[] args) throws IOException { Log.setGlobalLogLevel(LogLevel.INFO); File bamFile = new File(args[0]); File outCramFile = new File(args[1]); ReferenceSource source = new ReferenceSource(new File(args[2])); int maxThreads = Integer.valueOf(args[3]); BAMFileReader reader = new BAMFileReader(bamFile, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); OutputStream os = new FileOutputStream(outCramFile); CRAMFileWriter writer = new CRAMFileWriter(os, source, reader.getFileHeader(), outCramFile.getName(), maxThreads); CloseableIterator<SAMRecord> iterator = reader.getIterator(); while (iterator.hasNext()) { SAMRecord record = iterator.next(); writer.addAlignment(record); } writer.close(); reader.close(); }
throw new UnsupportedOperationException("Cannot query stream-based BAM file"); mCurrentIterator = createIndexIterator(intervals, contained); return mCurrentIterator;
private CloseableIterator<SAMRecord> createIndexIterator(final QueryInterval[] intervals, final boolean contained) { assertIntervalsOptimized(intervals); BAMFileSpan span = getFileSpan(intervals, getIndex()); // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(span == null ? null : span.toCoordinateArray()); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator, new BAMQueryMultipleIntervalsIteratorFilter(intervals, contained)); }
@Override protected SAMFileHeader readHeader(InputStream is) throws IOException { try { BAMFileReader reader = new BAMFileReader(is, null, false, false, ValidationStringency.SILENT, null); return reader.getFileHeader(); } catch (IOException e) { throw e; } catch (Exception e) { return null; } } },
/** Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. */ static long findVirtualOffsetOfFirstRecord(final File bam) throws IOException { final BAMFileReader reader = new BAMFileReader(bam, null, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final long offset = reader.mFirstRecordPointer; reader.close(); return offset; }
final BAMFileReader bam = new BAMFileReader(inputBamFile, null, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); if (!bam.hasIndex()) { throw new SAMException("No index for bam file " + inputBamFile); int nRefs = bam.getFileHeader().getSequenceDictionary().size(); for (int i = 0; i < nRefs; i++) { final SAMSequenceRecord seq = bam.getFileHeader().getSequence(i); if (seq == null) continue; final String sequenceName = seq.getSequenceName();
/** * Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. * The caller is responsible for closing the stream. */ static long findVirtualOffsetOfFirstRecord(final SeekableStream seekableStream) throws IOException { final BAMFileReader reader = new BAMFileReader(seekableStream, (SeekableStream) null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); return reader.mFirstRecordPointer; }
final BAMFileReader bam = new BAMFileReader(inputBamFile, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); if (!bam.hasIndex() || bam.getIndexType() == null) { throw new SAMException("No index for bam file " + inputBamFile); int nRefs = bam.getFileHeader().getSequenceDictionary().size(); for (int i = 0; i < nRefs; i++) { final SAMSequenceRecord seq = bam.getFileHeader().getSequence(i); if (seq == null) continue; final String sequenceName = seq.getSequenceName();
private BAMFileReader(final BlockCompressedInputStream compressedInputStream, final SeekableStream indexStream, final boolean eagerDecode, final String source, final ValidationStringency validationStringency, final SAMRecordFactory factory) throws IOException { mIndexStream = indexStream; mIsSeekable = true; mCompressedInputStream = compressedInputStream; mStream = new BinaryCodec(new DataInputStream(mCompressedInputStream)); this.eagerDecode = eagerDecode; this.mValidationStringency = validationStringency; this.samRecordFactory = factory; this.mFileHeader = readHeader(this.mStream, this.mValidationStringency, source); mFirstRecordPointer = mCompressedInputStream.getFilePointer(); }
@Test (expectedExceptions = SAMFormatException.class) public static void testGetIndexException() { Assert.assertNotNull(bamFileReaderWrong.getIndex()); }
/** * Retrieves the index for the given file type. Ensure that the index is of the specified type. * @return An index of the given type. */ @Override public BAMIndex getIndex() { if(!hasIndex()) throw new SAMException("No index is available for this BAM file."); if(mIndex == null) { if (mIndexFile != null) mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexFile, getFileHeader().getSequenceDictionary(), mEnableIndexMemoryMapping) : new DiskBasedBAMFileIndex(mIndexFile, getFileHeader().getSequenceDictionary(), mEnableIndexMemoryMapping); else mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexStream, getFileHeader().getSequenceDictionary()) : new DiskBasedBAMFileIndex(mIndexStream, getFileHeader().getSequenceDictionary()); } return mIndex; }
iterator = bamFileReader.createIndexIterator(queryIntervals, false, split.getIntervalFilePointers()); } else if (boundedTraversal && split.getIntervalFilePointers() == null) { iterator = bamFileReader.queryUnmapped(); } else { iterator = bamFileReader.getIterator(splitSpan);
/** * Returns the virtual file offset of the first record in a BAM file - i.e. the virtual file * offset after skipping over the text header and the sequence records. */ public static long findVirtualOffsetOfFirstRecordInBam(final SeekableStream seekableStream) { try { return BAMFileReader.findVirtualOffsetOfFirstRecord(seekableStream); } catch (final IOException ioe) { throw new RuntimeEOFException(ioe); } }
@Test(dataProvider = "longCigarsData") public void testClearAttributesDoesntVoidLongCigar(final int numOps) throws Exception { final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate); final Cigar cigar = Cigar.fromCigarOperators(getCigarOperatorsForTest(numOps)); builder.addFrag("frag1", 0, 1, false, false, cigar.toString(), null, 30); //encode as BAM into ByteArray final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final BAMFileWriter writer = new BAMFileWriter(baos, null)) { writer.setHeader(builder.getHeader()); builder.getRecords().forEach(writer::addAlignment); } //read from ByteArray final BAMFileReader reader = new BAMFileReader(new ByteArrayInputStream(baos.toByteArray()), null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final CloseableIterator<SAMRecord> iterator = reader.getIterator(); iterator.hasNext(); final SAMRecord recordFromBAM = iterator.next(); //clear attributes before explicitly accessing cigar or attributes recordFromBAM.clearAttributes(); // see that cigar is unscathed Assert.assertNotNull(recordFromBAM.getCigar()); Assert.assertFalse(BAMRecord.isSentinelCigar(recordFromBAM.getCigar(), recordFromBAM.getReadLength())); }
/** * Retrieves the index for the given file type. Ensure that the index is of the specified type. * @return An index of the given type. */ @Override public BAMIndex getIndex() { if(!hasIndex()) throw new SAMException("No index is available for this BAM file."); if(mIndex == null) { SamIndexes samIndex = getIndexType(); if (samIndex == null) { mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexStream, getFileHeader().getSequenceDictionary()) : new DiskBasedBAMFileIndex(mIndexStream, getFileHeader().getSequenceDictionary()); } else if (samIndex.equals(SamIndexes.BAI)) { mIndex = mEnableIndexCaching ? new CachingBAMFileIndex(mIndexFile, getFileHeader().getSequenceDictionary(), mEnableIndexMemoryMapping) : new DiskBasedBAMFileIndex(mIndexFile, getFileHeader().getSequenceDictionary(), mEnableIndexMemoryMapping); } else if (samIndex.equals(SamIndexes.CSI)) { mIndex = new CSIIndex(mIndexFile, mEnableIndexMemoryMapping, getFileHeader().getSequenceDictionary()); } else { throw new SAMFormatException("Unsupported BAM index file: " + mIndexFile.getName()); } } return mIndex; }
BAMIndex idx = samReader.indexing().getIndex(); QueryInterval[] queryIntervals = prepareQueryIntervals(intervals, dict); fileToSpan.put(bamFile, BAMFileReader.getFileSpan(queryIntervals, idx));
@Test public static void testGetIndexOK() { Assert.assertTrue(bamFileReaderBAI.hasIndex()); Assert.assertTrue(bamFileReaderCSI.hasIndex()); Assert.assertTrue(bamFileReaderNull.hasIndex()); Assert.assertNotNull(bamFileReaderBAI.getIndex()); Assert.assertNotNull(bamFileReaderCSI.getIndex()); Assert.assertNotNull(bamFileReaderNull.getIndex()); }
@Test public void exhaustivelyTestIndexTest () throws IOException { BAMFileReader bamFileReader1 = new BAMFileReader(BAM_FILE, BAI_FILE, true, false, ValidationStringency.DEFAULT_STRINGENCY, new DefaultSAMRecordFactory()); bamFileReader1.enableIndexCaching(true); BAMFileReader bamFileReader2 = new BAMFileReader(BAM_FILE, CSI_FILE, true, false, ValidationStringency.DEFAULT_STRINGENCY, new DefaultSAMRecordFactory()); final SamReader samFileReader1 = new SamReader.PrimitiveSamReaderToSamReaderAdapter(bamFileReader1, null); final SamReader samFileReader2 = new SamReader.PrimitiveSamReaderToSamReaderAdapter(bamFileReader2, null); int baiCount = BamIndexValidator.exhaustivelyTestIndex(samFileReader1); int csiCount = BamIndexValidator.exhaustivelyTestIndex(samFileReader2); Assert.assertEquals(baiCount, csiCount); }
if (intervals != null) { QueryInterval[] queryIntervals = BAMInputFormat.prepareQueryIntervals(intervals, header.getSequenceDictionary()); iterator = bamFileReader.createIndexIterator(queryIntervals, false, split.getIntervalFilePointers()); } else { BAMFileSpan splitSpan = new BAMFileSpan(new Chunk(virtualStart, virtualEnd)); iterator = bamFileReader.getIterator(splitSpan);
/** * @param advance Trick to enable subclass to do more setup before advancing */ BAMFileIterator(final boolean advance) { this.bamRecordCodec = new BAMRecordCodec(getFileHeader(), samRecordFactory); this.bamRecordCodec.setInputStream(BAMFileReader.this.mStream.getInputStream(), BAMFileReader.this.mStream.getInputFileName()); if (advance) { advance(); } }