Skip to content

Commit c06f952

Browse files
author
yash-puligundla
committed
Add Range encode and decode for EXT flag
1 parent 119c76e commit c06f952

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/main/java/htsjdk/samtools/cram/compression/range/RangeDecode.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ public ByteBuffer uncompressStream(final ByteBuffer inBuffer, int outSize) {
6464
return ByteBuffer.wrap(data);
6565
} else if (rangeParams.isExternalCompression()){
6666
byte[] extCompressedBytes = new byte[inBuffer.remaining()];
67-
inBuffer.get( extCompressedBytes,inBuffer.position(), inBuffer.remaining());
67+
int extCompressedBytesIdx = 0;
68+
int start = inBuffer.position();
69+
int end = inBuffer.limit();
70+
for (int i = start; i < end; i++) {
71+
extCompressedBytes[extCompressedBytesIdx] = inBuffer.get();
72+
extCompressedBytesIdx++;
73+
}
6874
uncompressEXT(extCompressedBytes, outBuffer);
6975
} else if (rangeParams.isRLE()){
7076
switch (rangeParams.getOrder()) {
@@ -90,8 +96,7 @@ public ByteBuffer uncompressStream(final ByteBuffer inBuffer, int outSize) {
9096
if (rangeParams.isPack() && packMappingTable.length > 0) {
9197
outBuffer = decodePack(outBuffer, packMappingTable, numSymbols, packDataLength);
9298
}
93-
94-
outBuffer.position(0);
99+
outBuffer.rewind();
95100
return outBuffer;
96101

97102
}

src/main/java/htsjdk/samtools/cram/compression/range/RangeEncode.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package htsjdk.samtools.cram.compression.range;
22

33
import htsjdk.samtools.cram.CRAMException;
4+
import htsjdk.samtools.cram.compression.BZIP2ExternalCompressor;
45
import java.nio.ByteBuffer;
56
import java.nio.ByteOrder;
67
import java.util.ArrayList;
@@ -15,7 +16,7 @@ public ByteBuffer compress(final ByteBuffer inBuffer, final RangeParams rangePar
1516
return EMPTY_BUFFER;
1617
}
1718

18-
final ByteBuffer outBuffer = allocateOutputBuffer(inBuffer.remaining());
19+
ByteBuffer outBuffer = allocateOutputBuffer(inBuffer.remaining());
1920
outBuffer.order(ByteOrder.BIG_ENDIAN);
2021
final int formatFlags = rangeParams.getFormatFlags();
2122
outBuffer.put((byte) (formatFlags));
@@ -67,8 +68,14 @@ public ByteBuffer compress(final ByteBuffer inBuffer, final RangeParams rangePar
6768
outBuffer.rewind(); // set position to 0
6869
return outBuffer;
6970
} else if (rangeParams.isExternalCompression()){
70-
71-
// TODO
71+
byte[] rawBytes = new byte[inputBuffer.remaining()];
72+
inputBuffer.get( rawBytes,inBuffer.position(), inputBuffer.remaining());
73+
final BZIP2ExternalCompressor compressor = new BZIP2ExternalCompressor();
74+
final byte [] extCompressedBytes = compressor.compress(rawBytes);
75+
outBuffer.put(extCompressedBytes);
76+
outBuffer.limit(outBuffer.position());
77+
outBuffer.rewind(); // set position to 0
78+
return outBuffer;
7279
} else if (rangeParams.isRLE()){
7380
switch (rangeParams.getOrder()) {
7481
case ZERO:
@@ -85,17 +92,7 @@ public ByteBuffer compress(final ByteBuffer inBuffer, final RangeParams rangePar
8592
}
8693

8794
}
88-
89-
// // step 1: Encode meta-data
90-
// var pack_meta
91-
// if (flags & ARITH_PACK)
92-
// [pack_meta, src, e_len] = this.encodePack(src)
93-
//
94-
// // step 2: Write any meta data
95-
// if (flags & ARITH_PACK)
96-
// this.stream.WriteStream(pack_meta)
97-
98-
return inBuffer;
95+
return outBuffer;
9996
}
10097

10198
private ByteBuffer compressOrder0 (
@@ -268,7 +265,6 @@ private ByteBuffer compressRLEOrder1 (
268265
return outBuffer;
269266
}
270267

271-
272268
protected ByteBuffer allocateOutputBuffer(final int inSize) {
273269

274270
// same as the allocateOutputBuffer in RANS4x8Encode and RANSNx16Encode

src/test/java/htsjdk/samtools/cram/CRAMInteropTestUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package htsjdk.samtools.cram;
22

3-
import java.io.*;
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
67
import java.nio.file.Paths;

src/test/java/htsjdk/samtools/cram/compression/range/RangeTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public Object[][] getRangeCodecs() {
7979
RangeParams.PACK_FLAG_MASK,
8080
RangeParams.PACK_FLAG_MASK | RangeParams. ORDER_FLAG_MASK,
8181
RangeParams.PACK_FLAG_MASK | RangeParams.RLE_FLAG_MASK,
82-
RangeParams.PACK_FLAG_MASK | RangeParams.RLE_FLAG_MASK | RangeParams.ORDER_FLAG_MASK);
82+
RangeParams.PACK_FLAG_MASK | RangeParams.RLE_FLAG_MASK | RangeParams.ORDER_FLAG_MASK,
83+
RangeParams.EXT_FLAG_MASK,
84+
RangeParams.EXT_FLAG_MASK | RangeParams.PACK_FLAG_MASK);
8385
final List<Object[]> testCases = new ArrayList<>();
8486
for (Integer rangeParamsFormatFlag : rangeParamsFormatFlagList) {
8587
Object[] objects = new Object[]{

0 commit comments

Comments
 (0)