Skip to content

Commit

Permalink
more consistent ExternalDataBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthibault79 committed Dec 3, 2018
1 parent b3ff8ed commit f59abd0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static Slice buildSlice(final List<CramCompressionRecord> records,
for (final Integer key : map.keySet()) {
final ExternalCompressor compressor = header.externalCompressors.get(key);
final byte[] rawData = map.get(key).toByteArray();
final ExternalDataBlock externalBlock = new ExternalDataBlock(key, compressor, rawData);
final ExternalDataBlock externalBlock = Block.externalDataBlock(key, compressor, rawData);
slice.external.put(key, externalBlock);
}

Expand Down
21 changes: 20 additions & 1 deletion src/main/java/htsjdk/samtools/cram/structure/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import htsjdk.samtools.cram.CRAMException;
import htsjdk.samtools.cram.common.CramVersions;
import htsjdk.samtools.cram.compression.ExternalCompression;
import htsjdk.samtools.cram.compression.ExternalCompressor;
import htsjdk.samtools.cram.io.*;
import htsjdk.samtools.util.RuntimeIOException;

Expand Down Expand Up @@ -115,7 +116,7 @@ public static SliceHeaderBlock uncompressedSliceHeaderBlock(final byte[] rawCont
}

/**
* Create a new core block with the given uncompressed content.
* Create a new core data block with the given uncompressed content.
* The block will have RAW (no) compression and CORE content type.
*
* @param rawContent the uncompressed content of the block
Expand All @@ -125,6 +126,24 @@ public static CoreDataBlock uncompressedCoreBlock(final byte[] rawContent) {
return new CoreDataBlock(BlockCompressionMethod.RAW, rawContent);
}

/**
* Create a new external data block with the given content ID, compressor, and uncompressed content.
* The block will have EXTERNAL content type.
*
* @param contentId the external identifier for the block
* @param compressor which external compressor to use on this block
* @param rawContent the uncompressed content of the block
* @return a new {@link ExternalDataBlock} object
*/
public static ExternalDataBlock externalDataBlock(final int contentId, final ExternalCompressor compressor, final byte[] rawContent) {
// remove after https://github.com/samtools/htsjdk/issues/1232
if (contentId == Block.NO_CONTENT_ID) {
throw new CRAMException("Valid Content ID required. Given: " + contentId);
}

return new ExternalDataBlock(compressor.getMethod(), compressor.compress(rawContent), contentId);
}

public final BlockCompressionMethod getMethod() {
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ public class ExternalDataBlock extends Block {
this.contentId = contentId;
}

/**
* Create a new external data block with the given content ID, compressor, and uncompressed content.
* The block will have EXTERNAL content type.
*
* @param contentId the external identifier for the block
* @param compressor which external compressor to use on this block
* @param rawContent the uncompressed content of the block
*/
public ExternalDataBlock(final int contentId, final ExternalCompressor compressor, final byte[] rawContent) {
this(compressor.getMethod(), compressor.compress(rawContent), contentId);

// remove after https://github.com/samtools/htsjdk/issues/1232
if (contentId == Block.NO_CONTENT_ID) {
throw new CRAMException("Valid Content ID required. Given: " + contentId);
}
}

@Override
public final int getContentId() {
return contentId;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/htsjdk/samtools/cram/structure/BlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testExternalBlockRoundTrips() {
final byte[] uncompressedData = "A TEST STRING WITH REDUNDANCY AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes();
final byte[] compressedData = compressor.compress(uncompressedData);

final Block extBlock = new ExternalDataBlock(contentID, compressor, uncompressedData);
final Block extBlock = Block.externalDataBlock(contentID, compressor, uncompressedData);

final Block rtBlock2 = roundTrip(extBlock, CramVersions.CRAM_v2_1);
contentCheck(rtBlock2, uncompressedData, compressedData);
Expand Down Expand Up @@ -140,6 +140,6 @@ public void testExternalBlockContentId() {

// not allowed for external
final int contentID = Block.NO_CONTENT_ID;
new ExternalDataBlock(contentID, compressor, uncompressedData);
Block.externalDataBlock(contentID, compressor, uncompressedData);
}
}

0 comments on commit f59abd0

Please sign in to comment.