|
| 1 | +package htsjdk.samtools; |
| 2 | + |
| 3 | +import htsjdk.samtools.cram.build.CramIO; |
| 4 | +import htsjdk.samtools.cram.structure.Container; |
| 5 | +import htsjdk.samtools.cram.structure.CramHeader; |
| 6 | +import htsjdk.samtools.util.RuntimeIOException; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStream; |
| 10 | + |
| 11 | +/** |
| 12 | + * Rewrite a series of containers to a new stream. The CRAM header and SAMFileHeader containers are automatically |
| 13 | + * written to the stream when this class is instantiated. An EOF container is automatically written when |
| 14 | + * {@link #finish()} is called. |
| 15 | + */ |
| 16 | +public class CRAMContainerStreamRewriter { |
| 17 | + private final OutputStream outputStream; |
| 18 | + private final String outputStreamIdentifier; |
| 19 | + private final CramHeader cramHeader; |
| 20 | + private final SAMFileHeader samFileHeader; |
| 21 | + private final CRAMIndexer cramIndexer; |
| 22 | + |
| 23 | + private long streamOffset = 0L; |
| 24 | + private long recordCounter = 0L; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a CRAMContainerStreamWriter for writing SAM records into a series of CRAM |
| 28 | + * containers on an output stream, with an optional output index. |
| 29 | + * |
| 30 | + * @param outputStream where to write the CRAM stream. |
| 31 | + * @param samFileHeader {@link SAMFileHeader} to be used. Sort order is determined by the sortOrder property of this arg. |
| 32 | + * @param outputStreamIdentifier used for display in error message display |
| 33 | + * @param indexer CRAM indexer. Can be null if no index is required. |
| 34 | + */ |
| 35 | + public CRAMContainerStreamRewriter( |
| 36 | + final OutputStream outputStream, |
| 37 | + final CramHeader cramHeader, |
| 38 | + final SAMFileHeader samFileHeader, |
| 39 | + final String outputStreamIdentifier, |
| 40 | + final CRAMIndexer indexer) { |
| 41 | + this.outputStream = outputStream; |
| 42 | + this.cramHeader = cramHeader; |
| 43 | + this.samFileHeader = samFileHeader; |
| 44 | + this.outputStreamIdentifier = outputStreamIdentifier; |
| 45 | + this.cramIndexer = indexer; |
| 46 | + |
| 47 | + //TODO: update the SAMFileHeader with a program group to leave a paper trail? |
| 48 | + streamOffset = CramIO.writeCramHeader(cramHeader, outputStream); |
| 49 | + streamOffset += Container.writeSAMFileHeaderContainer(cramHeader.getCRAMVersion(), samFileHeader, outputStream); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Writes a container to a stream, updating the (stream-relative) global record counter and byte offsets. |
| 54 | + * |
| 55 | + * Since this method mutates the values in the container, the container is no longer valid in the context |
| 56 | + * of the stream from which it originated. |
| 57 | + * |
| 58 | + * @param container the container to emit to the stream. the container must conform to the version and sort |
| 59 | + * order specified in the CRAM header and SAM header provided to the constructor |
| 60 | + * {@link #CRAMContainerStreamRewriter(OutputStream, CramHeader, SAMFileHeader, String, CRAMIndexer)}. |
| 61 | + * All the containers serialized to a single stream using this method must have originated from the |
| 62 | + * same original context(/stream), obtained via {@link htsjdk.samtools.cram.build.CramContainerIterator}. |
| 63 | + */ |
| 64 | + public void rewriteContainer(final Container container) { |
| 65 | + // update the container and slices with the correct global record counter and byte offsets |
| 66 | + // (required for indexing) |
| 67 | + container.relocateContainer(recordCounter, streamOffset); |
| 68 | + |
| 69 | + // re-serialize the entire container and slice(s), block by block |
| 70 | + streamOffset += container.write(cramHeader.getCRAMVersion(), outputStream); |
| 71 | + recordCounter += container.getContainerHeader().getNumberOfRecords(); |
| 72 | + |
| 73 | + if (cramIndexer != null) { |
| 74 | + cramIndexer.processContainer(container, ValidationStringency.SILENT); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Finish writing to the stream. Flushes the record cache and optionally emits an EOF container. |
| 80 | + */ |
| 81 | + public void finish() { |
| 82 | + try { |
| 83 | + CramIO.writeCramEOF(cramHeader.getCRAMVersion(), outputStream); |
| 84 | + outputStream.flush(); |
| 85 | + if (cramIndexer != null) { |
| 86 | + cramIndexer.finish(); |
| 87 | + } |
| 88 | + } catch (final IOException e) { |
| 89 | + throw new RuntimeIOException(String.format("IOException closing stream for %s", outputStreamIdentifier)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments