private void doWrite(final OutputStream outputStream) throws IOException { outputStream.write(getCompressionMethod().getMethodId()); outputStream.write(getContentType().getContentTypeId()); ITF8.writeUnsignedITF8(getContentId(), outputStream); ITF8.writeUnsignedITF8(getCompressedContentSize(), outputStream); ITF8.writeUnsignedITF8(getUncompressedContentSize(), outputStream); outputStream.write(getCompressedContent()); }
@Override public String toString() { final byte[] uncompressed = getUncompressedContent(); final byte[] compressed = getCompressedContent(); final String raw = Arrays.toString(Arrays.copyOf(uncompressed, Math.min(5, uncompressed.length))); final String comp = Arrays.toString(Arrays.copyOf(compressed, Math.min(5, compressed.length))); return String.format("compression method=%s, content type=%s, id=%d, raw size=%d, compressed size=%d, raw=%s, comp=%s.", getCompressionMethod().name(), getContentType().name(), getContentId(), getUncompressedContentSize(), getCompressedContentSize(), raw, comp); } }
final BlockContentType contentType = BlockContentType.byId(inputStream.read()); final int contentId = ITF8.readUnsignedITF8(inputStream); final int compressedSize = ITF8.readUnsignedITF8(inputStream);
final BlockContentType contentType = BlockContentType.byId(inputStream.read()); final int contentId = ITF8.readUnsignedITF8(inputStream); final int compressedSize = ITF8.readUnsignedITF8(inputStream);
@Override public String toString() { final byte[] uncompressed = getUncompressedContent(); final byte[] compressed = getCompressedContent(); final String raw = Arrays.toString(Arrays.copyOf(uncompressed, Math.min(5, uncompressed.length))); final String comp = Arrays.toString(Arrays.copyOf(compressed, Math.min(5, compressed.length))); return String.format("compression method=%s, content type=%s, id=%d, raw size=%d, compressed size=%d, raw=%s, comp=%s.", getCompressionMethod().name(), getContentType().name(), getContentId(), getUncompressedContentSize(), getCompressedContentSize(), raw, comp); } }
private void doWrite(final OutputStream outputStream) throws IOException { outputStream.write(getCompressionMethod().getMethodId()); outputStream.write(getContentType().getContentTypeId()); ITF8.writeUnsignedITF8(getContentId(), outputStream); ITF8.writeUnsignedITF8(getCompressedContentSize(), outputStream); ITF8.writeUnsignedITF8(getUncompressedContentSize(), outputStream); outputStream.write(getCompressedContent()); }
private static void readSliceBlocks(final int major, final Slice slice, final InputStream inputStream) { slice.external = new HashMap<>(); for (int i = 0; i < slice.nofBlocks; i++) { final Block block = Block.read(major, inputStream); switch (block.getContentType()) { case CORE: slice.coreBlock = block; break; case EXTERNAL: if (slice.embeddedRefBlockContentID == block.getContentId()) { slice.embeddedRefBlock = block; } slice.external.put(block.getContentId(), block); break; default: throw new RuntimeException("Not a slice block, content type id " + block.getContentType().name()); } } }
private static void readSliceBlocks(final int major, final Slice slice, final InputStream inputStream) throws IOException { slice.external = new HashMap<>(); for (int i = 0; i < slice.nofBlocks; i++) { final Block block = Block.read(major, inputStream); switch (block.getContentType()) { case CORE: slice.coreBlock = block; break; case EXTERNAL: if (slice.embeddedRefBlockContentID == block.getContentId()) { slice.embeddedRefBlock = block; } slice.external.put(block.getContentId(), block); break; default: throw new RuntimeException("Not a slice block, content type id " + block.getContentType().name()); } } }
/** * Read a COMPRESSION_HEADER Block from an InputStream and return its contents as a CompressionHeader * We do this instead of reading the InputStream directly because the Block content may be compressed * * @param cramVersion the CRAM version * @param blockStream the stream to read from * @return a new CompressionHeader from the input */ public static CompressionHeader read(final int cramVersion, final InputStream blockStream) { final Block block = Block.read(cramVersion, blockStream); if (block.getContentType() != BlockContentType.COMPRESSION_HEADER) throw new RuntimeIOException("Compression Header Block expected, found: " + block.getContentType().name()); try (final ByteArrayInputStream internalStream = new ByteArrayInputStream(block.getUncompressedContent())) { final CompressionHeader header = new CompressionHeader(); header.internalRead(internalStream); return header; } catch (final IOException e) { throw new RuntimeIOException(e); } }
/** * Read a COMPRESSION_HEADER Block from an InputStream and return its contents as a CompressionHeader * We do this instead of reading the InputStream directly because the Block content may be compressed * * @param cramVersion the CRAM version * @param blockStream the stream to read from * @return a new CompressionHeader from the input */ public static CompressionHeader read(final int cramVersion, final InputStream blockStream) { final Block block = Block.read(cramVersion, blockStream); if (block.getContentType() != BlockContentType.COMPRESSION_HEADER) throw new RuntimeIOException("Compression Header Block expected, found: " + block.getContentType().name()); try (final ByteArrayInputStream internalStream = new ByteArrayInputStream(block.getUncompressedContent())) { final CompressionHeader header = new CompressionHeader(); header.internalRead(internalStream); return header; } catch (final IOException e) { throw new RuntimeIOException(e); } }
private static void readSliceHeader(final int major, final Slice slice, final InputStream readInputStream) throws IOException { slice.headerBlock = Block.read(major, readInputStream); if (slice.headerBlock.getContentType() != BlockContentType.MAPPED_SLICE) throw new RuntimeException("Slice Header Block expected, found: " + slice.headerBlock.getContentType().name()); final InputStream parseInputStream = new ByteArrayInputStream(slice.headerBlock.getUncompressedContent()); slice.sequenceId = ITF8.readUnsignedITF8(parseInputStream); slice.alignmentStart = ITF8.readUnsignedITF8(parseInputStream); slice.alignmentSpan = ITF8.readUnsignedITF8(parseInputStream); slice.nofRecords = ITF8.readUnsignedITF8(parseInputStream); slice.globalRecordCounter = LTF8.readUnsignedLTF8(parseInputStream); slice.nofBlocks = ITF8.readUnsignedITF8(parseInputStream); slice.contentIDs = CramIntArray.array(parseInputStream); slice.embeddedRefBlockContentID = ITF8.readUnsignedITF8(parseInputStream); slice.refMD5 = new byte[16]; InputStreamUtils.readFully(parseInputStream, slice.refMD5, 0, slice.refMD5.length); final byte[] bytes = InputStreamUtils.readFully(parseInputStream); if (major >= CramVersions.CRAM_v3.major) { slice.sliceTags = BinaryTagCodec.readTags(bytes, 0, bytes.length, ValidationStringency.DEFAULT_STRINGENCY); SAMBinaryTagAndValue tags = slice.sliceTags; while (tags != null) { log.debug(String.format("Found slice tag: %s", SAMTag.makeStringTag(tags.tag))); tags = tags.getNext(); } } }
private static void readSliceHeader(final int major, final Slice slice, final InputStream readInputStream) { slice.headerBlock = Block.read(major, readInputStream); if (slice.headerBlock.getContentType() != BlockContentType.MAPPED_SLICE) throw new RuntimeException("Slice Header Block expected, found: " + slice.headerBlock.getContentType().name()); final InputStream parseInputStream = new ByteArrayInputStream(slice.headerBlock.getUncompressedContent()); slice.sequenceId = ITF8.readUnsignedITF8(parseInputStream); slice.alignmentStart = ITF8.readUnsignedITF8(parseInputStream); slice.alignmentSpan = ITF8.readUnsignedITF8(parseInputStream); slice.nofRecords = ITF8.readUnsignedITF8(parseInputStream); slice.globalRecordCounter = LTF8.readUnsignedLTF8(parseInputStream); slice.nofBlocks = ITF8.readUnsignedITF8(parseInputStream); slice.contentIDs = CramIntArray.array(parseInputStream); slice.embeddedRefBlockContentID = ITF8.readUnsignedITF8(parseInputStream); slice.refMD5 = new byte[16]; InputStreamUtils.readFully(parseInputStream, slice.refMD5, 0, slice.refMD5.length); final byte[] bytes = InputStreamUtils.readFully(parseInputStream); if (major >= CramVersions.CRAM_v3.major) { slice.sliceTags = BinaryTagCodec.readTags(bytes, 0, bytes.length, ValidationStringency.DEFAULT_STRINGENCY); SAMBinaryTagAndValue tags = slice.sliceTags; while (tags != null) { log.debug(String.format("Found slice tag: %s", SAMTag.makeStringTag(tags.tag))); tags = tags.getNext(); } } }