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 all 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.getId());
ITF8.writeUnsignedITF8(lenParams.params.length, byteArrayOutputStream);
byteArrayOutputStream.write(lenParams.params);

byteArrayOutputStream.write((byte) byteParams.id.ordinal());
byteArrayOutputStream.write((byte) byteParams.id.getId());
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().getId());
final byte[] lenBytes = lenEncoding.toByteArray();
ITF8.writeUnsignedITF8(lenBytes.length, byteArrayOutputStream);
byteArrayOutputStream.write(lenBytes);

byteArrayOutputStream.write((byte) byteEncoding.id().ordinal());
byteArrayOutputStream.write((byte) byteEncoding.id().getId());
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().getMethodId());
outputStream.write(getContentType().getContentTypeId());

ITF8.writeUnsignedITF8(getContentId(), outputStream);
ITF8.writeUnsignedITF8(compressedContentSize, outputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@
*/
package htsjdk.samtools.cram.structure;

/**
* The block compression methods specified by Section 8 of the CRAM spec
*/
public enum BlockCompressionMethod {
RAW, GZIP, BZIP2, LZMA, RANS
RAW(0),
GZIP(1),
BZIP2(2),
LZMA(3),
RANS(4);

private final int methodId;

/**
* The block compression methods specified by Section 8 of the CRAM spec
* @param id the number assigned to each block compression method in the CRAM spec
*/
BlockCompressionMethod(final int id) {
methodId = id;
}

/**
* @return the number assigned to each block compression method in the CRAM spec
*/
public int getMethodId() {
return methodId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@
*/
package htsjdk.samtools.cram.structure;

/**
* The block content types specified by Section 8.1 of the CRAM spec
*/
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 contentTypeId;

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

/**
* @return the ID assigned to each content type in the CRAM spec
*/
public int getContentTypeId() {
return contentTypeId;
}
}
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.getId()));
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.getId()));
ITF8.writeUnsignedITF8(params.params.length, mapBuffer);
mapBuffer.put(params.params);
}
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/htsjdk/samtools/cram/structure/EncodingID.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,64 @@
package htsjdk.samtools.cram.structure;

/**
* Encoding ID as defined in the CRAM specs. These are basically ways to serialize a data series.
* Encoding ID as specified by Section 3 of the CRAM spec. These are basically ways to serialize a data series.
*/
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 id;

/**
* 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) {
this.id = id;
}

/**
* @return the number assigned to each encoding in the CRAM spec
*/
public int getId() {
return id;
}
}