Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRAM: Be explicit about spec IDs instead of using ordinal() #1221

Merged
merged 4 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public static EncodingParams toParam(final EncodingParams lenParams,
final EncodingParams byteParams) {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
byteArrayOutputStream.write((byte) lenParams.id.ordinal());
byteArrayOutputStream.write((byte) lenParams.id.getSpecId());
ITF8.writeUnsignedITF8(lenParams.params.length, byteArrayOutputStream);
byteArrayOutputStream.write(lenParams.params);

byteArrayOutputStream.write((byte) byteParams.id.ordinal());
byteArrayOutputStream.write((byte) byteParams.id.getSpecId());
ITF8.writeUnsignedITF8(byteParams.params.length, byteArrayOutputStream);
byteArrayOutputStream.write(byteParams.params);
} catch (final IOException e) {
Expand All @@ -64,12 +64,12 @@ public static EncodingParams toParam(final EncodingParams lenParams,
public byte[] toByteArray() {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
byteArrayOutputStream.write((byte) lenEncoding.id().ordinal());
byteArrayOutputStream.write((byte) lenEncoding.id().getSpecId());
final byte[] lenBytes = lenEncoding.toByteArray();
ITF8.writeUnsignedITF8(lenBytes.length, byteArrayOutputStream);
byteArrayOutputStream.write(lenBytes);

byteArrayOutputStream.write((byte) byteEncoding.id().ordinal());
byteArrayOutputStream.write((byte) byteEncoding.id().getSpecId());
final byte[] byteBytes = byteEncoding.toByteArray();
ITF8.writeUnsignedITF8(byteBytes.length, byteArrayOutputStream);
byteArrayOutputStream.write(byteBytes);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/htsjdk/samtools/cram/structure/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ private void doWrite(final OutputStream outputStream) throws IOException {
if (!isCompressed()) compress();
if (!isUncompressed()) uncompress();

outputStream.write(getMethod().ordinal());
outputStream.write(getContentType().ordinal());
outputStream.write(getMethod().getSpecValue());
outputStream.write(getContentType().getSpecTypeId());

ITF8.writeUnsignedITF8(getContentId(), outputStream);
ITF8.writeUnsignedITF8(compressedContentSize, outputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,23 @@
package htsjdk.samtools.cram.structure;

public enum BlockCompressionMethod {
RAW, GZIP, BZIP2, LZMA, RANS
RAW(0),
GZIP(1),
BZIP2(2),
LZMA(3),
RANS(4);

private final int specValue;

/**
* The block compression methods specified by Section 8 of the CRAM spec
* @param value the number assigned to each block compression method in the CRAM spec
*/
BlockCompressionMethod(final int value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of these is inconsistent and "spec" seems a bit strange in the name. I would drop spec and just go with getId or getTypeId as appropriate. The javadoc is a good addition, it would make sense to put it as class level javadoc and on the getter, as well since users will never actually see the enum constructor.

specValue = value;
}

public int getSpecValue() {
return specValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,24 @@
package htsjdk.samtools.cram.structure;

public enum BlockContentType {
FILE_HEADER, COMPRESSION_HEADER, MAPPED_SLICE, RESERVED, EXTERNAL, CORE
FILE_HEADER(0),
COMPRESSION_HEADER(1),
MAPPED_SLICE(2),
RESERVED(3),
EXTERNAL(4),
CORE(5);

private final int specTypeId;

/**
* The block content type IDs specified by Section 8.1 of the CRAM spec
* @param id the number assigned to each content type ID in the CRAM spec
*/
BlockContentType(final int id) {
specTypeId = id;
}

public int getSpecTypeId() {
return specTypeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void write(final OutputStream outputStream) throws IOException {
mapBuffer.put((byte) encodingKey.name().charAt(1));

final EncodingParams params = encodingMap.get(encodingKey);
mapBuffer.put((byte) (0xFF & params.id.ordinal()));
mapBuffer.put((byte) (0xFF & params.id.getSpecId()));
ITF8.writeUnsignedITF8(params.params.length, mapBuffer);
mapBuffer.put(params.params);
}
Expand All @@ -283,7 +283,7 @@ void write(final OutputStream outputStream) throws IOException {
ITF8.writeUnsignedITF8(encodingKey, mapBuffer);

final EncodingParams params = tMap.get(encodingKey);
mapBuffer.put((byte) (0xFF & params.id.ordinal()));
mapBuffer.put((byte) (0xFF & params.id.getSpecId()));
ITF8.writeUnsignedITF8(params.params.length, mapBuffer);
mapBuffer.put(params.params);
}
Expand Down
34 changes: 24 additions & 10 deletions src/main/java/htsjdk/samtools/cram/structure/EncodingID.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,55 @@ public enum EncodingID {
/**
* "Do nothing" encoding. Should throw an exception when trying reading or writing with this encoding.
*/
NULL,
NULL(0),
/**
* Shove the data into a byte array for compressing later with a generic compressor like GZIP.
*/
EXTERNAL,
EXTERNAL(1),
/**
* 'naf said: http://en.wikipedia.org/wiki/Golomb_coding
*/
GOLOMB,
GOLOMB(2),
/**
* http://en.wikipedia.org/wiki/Huffman_coding
*/
HUFFMAN,
HUFFMAN(3),
/**
* A byte array serialized as [length][elements]
*/
BYTE_ARRAY_LEN,
BYTE_ARRAY_LEN(4),
/**
* A byte array serialized as [elements][stop]
*/
BYTE_ARRAY_STOP,
BYTE_ARRAY_STOP(5),
/**
* http://en.wikipedia.org/wiki/Beta_Code
*/
BETA,
BETA(6),
/**
* Subexponential codes, see the CRAM specs for details.
*/
SUBEXPONENTIAL,
SUBEXPONENTIAL(7),
/**
* A variant of GOLOMB encoding: http://en.wikipedia.org/wiki/Golomb_coding
*/
GOLOMB_RICE,
GOLOMB_RICE(8),
/**
* http://en.wikipedia.org/wiki/Elias_gamma_coding
*/
GAMMA
GAMMA(9);

private final int specId;

/**
* The encodings specified by Section 3 of the CRAM spec
* @param id the number assigned to each encoding in the CRAM spec
*/
EncodingID(final int id) {
specId = id;
}

public int getSpecId() {
return specId;
}
}