/** * Complete the index, and close the output stream. * * @param finalVirtualOffset the virtual offset at which the next record would start if it were added to the file * @param dataFileLength the length of the data file in bytes */ public void finish(final long finalVirtualOffset, final long dataFileLength) { finish(finalVirtualOffset, dataFileLength, null, null); }
/** * Process a record for the index: the offset of every <i>n</i>th record will be written to the index. * * @param virtualOffset virtual file pointer of the record */ public void processRecord(final long virtualOffset) { if (recordCount++ % granularity == 0) { writeVirtualOffset(virtualOffset); } }
final SBIIndexWriter indexWriter = new SBIIndexWriter(out, granularity); while (true) { try { final int blockSize = byteBuffer.getInt(0); indexWriter.processRecord(recordStart); InputStreamUtils.skipFully(blockIn, blockSize); } catch (EOFException e) { indexWriter.finish(recordStart, in.length());
private SBIIndex fromSAMRecords(final File bamFile, final long granularity) throws IOException { final BAMFileReader bamFileReader = bamFileReader(bamFile); try (CloseableIterator<SAMRecord> iterator = bamFileReader.getIterator(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { final SBIIndexWriter indexWriter = new SBIIndexWriter(out, granularity); while (iterator.hasNext()) { processAlignment(indexWriter, iterator.next()); } indexWriter.finish(bamFileReader.getVirtualFilePointer(), bamFile.length()); return SBIIndex.load(new ByteArrayInputStream(out.toByteArray())); } }
private void processAlignment(final SBIIndexWriter indexWriter, final SAMRecord rec) { final SAMFileSource source = rec.getFileSource(); if (source == null) { throw new SAMException("No source (virtual file offsets); needed for indexing on BAM Record " + rec); } final BAMFileSpan filePointer = (BAMFileSpan) source.getFilePointer(); indexWriter.processRecord(filePointer.getFirstOffset()); }
final SBIIndexWriter indexWriter = new SBIIndexWriter(out, granularity); while (true) { try { final int blockSize = byteBuffer.getInt(0); indexWriter.processRecord(recordStart); InputStreamUtils.skipFully(blockIn, blockSize); } catch (RuntimeEOFException e) { indexWriter.finish(recordStart, in.length());
/** * Complete the index, and close the output stream. * * @param finalVirtualOffset the virtual offset at which the next record would start if it were added to the file * @param dataFileLength the length of the data file in bytes */ public void finish(final long finalVirtualOffset, final long dataFileLength) { finish(finalVirtualOffset, dataFileLength, null, null); }
/** * Process a record for the index: the offset of every <i>n</i>th record will be written to the index. * * @param virtualOffset virtual file pointer of the record */ public void processRecord(final long virtualOffset) { if (recordCount++ % granularity == 0) { writeVirtualOffset(virtualOffset); } }
/** * Complete the index, and close the output stream. * * @param finalVirtualOffset the virtual offset at which the next record would start if it were added to the file * @param dataFileLength the length of the data file in bytes * @param md5 the MD5 hash of the data file, or null if not specified * @param uuid the UUID for the data file, or null if not specified */ public void finish(final long finalVirtualOffset, final long dataFileLength, final byte[] md5, final byte[] uuid) { if (md5 != null && md5.length != 16) { throw new IllegalArgumentException("Invalid MD5 length: " + md5.length); } if (uuid != null && uuid.length != 16) { throw new IllegalArgumentException("Invalid UUID length: " + uuid.length); } final SBIIndex.Header header = new SBIIndex.Header(dataFileLength, md5 == null ? EMPTY_MD5 : md5, uuid == null ? EMPTY_UUID : uuid, recordCount, granularity); finish(header, finalVirtualOffset); }
void finish(final SBIIndex.Header header, final long finalVirtualOffset) { // complete writing the temp offsets file writeVirtualOffset(finalVirtualOffset); tempOffsetsCodec.close(); try (BinaryCodec binaryCodec = new BinaryCodec(out); InputStream tempOffsets = new BufferedInputStream(Files.newInputStream(tempOffsetsFile))) { // header binaryCodec.writeBytes(SBIIndex.SBI_MAGIC); binaryCodec.writeLong(header.getFileLength()); binaryCodec.writeBytes(header.getMd5()); binaryCodec.writeBytes(header.getUuid()); binaryCodec.writeLong(header.getTotalNumberOfRecords()); binaryCodec.writeLong(header.getGranularity()); binaryCodec.writeLong(virtualOffsetCount); // offsets IOUtil.copyStream(tempOffsets, out); } catch (IOException e) { throw new RuntimeIOException(e); } finally { try { Files.delete(tempOffsetsFile); } catch (IOException e) { throw new RuntimeIOException(e); } } } }
/** * Complete the index, and close the output stream. * * @param finalVirtualOffset the virtual offset at which the next record would start if it were added to the file * @param dataFileLength the length of the data file in bytes * @param md5 the MD5 hash of the data file, or null if not specified * @param uuid the UUID for the data file, or null if not specified */ public void finish(final long finalVirtualOffset, final long dataFileLength, final byte[] md5, final byte[] uuid) { if (md5 != null && md5.length != 16) { throw new IllegalArgumentException("Invalid MD5 length: " + md5.length); } if (uuid != null && uuid.length != 16) { throw new IllegalArgumentException("Invalid UUID length: " + uuid.length); } final SBIIndex.Header header = new SBIIndex.Header(dataFileLength, md5 == null ? EMPTY_MD5 : md5, uuid == null ? EMPTY_UUID : uuid, recordCount, granularity); finish(header, finalVirtualOffset); }
void finish(final SBIIndex.Header header, final long finalVirtualOffset) { // complete writing the temp offsets file writeVirtualOffset(finalVirtualOffset); tempOffsetsCodec.close(); try (BinaryCodec binaryCodec = new BinaryCodec(out); InputStream tempOffsets = new BufferedInputStream(Files.newInputStream(tempOffsetsFile))) { // header binaryCodec.writeBytes(SBIIndex.SBI_MAGIC); binaryCodec.writeLong(header.getFileLength()); binaryCodec.writeBytes(header.getMd5()); binaryCodec.writeBytes(header.getUuid()); binaryCodec.writeLong(header.getTotalNumberOfRecords()); binaryCodec.writeLong(header.getGranularity()); binaryCodec.writeLong(virtualOffsetCount); // offsets IOUtil.copyStream(tempOffsets, out); } catch (IOException e) { throw new RuntimeIOException(e); } finally { try { Files.delete(tempOffsetsFile); } catch (IOException e) { throw new RuntimeIOException(e); } } } }