private Block roundTrip(final Block in, final Version version) throws IOException { byte[] written; try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { in.write(version.major, os); written = os.toByteArray(); } try (final InputStream is = new ByteArrayInputStream(written)) { return Block.read(version.major, is); } }
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()); } } }
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()); } } }
/** * 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(); } } }
Block.read(version.major, cramSeekableStream); long blockByteSyze = cramSeekableStream.position() - firstBlockStart - crcByteSize; cramSeekableStream.seek(firstBlockStart);