Skip to content

Commit

Permalink
Conform to updated interop test structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnbroad committed Jan 14, 2025
1 parent a37b995 commit 282322a
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public FQZModels(final FQZParams fqzParams) {
}
length = new ByteModel[4];
for (int i = 0; i < 4; i++) {
length[i] = new ByteModel(256); //TODO: Magic 256 ?
length[i] = new ByteModel(256);
}
reverse = new ByteModel(2);
duplicate = new ByteModel(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

public class FQZParams {
//TODO: share this with the decoder/encoder
private static final int NUMBER_OF_SYMBOLS = 256;

final FQZGlobalFlags fqzFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class FQZState {
public void setSelector(int selector) { this.selector = selector; }

//TODO: it is super confusing that this is called selectorTable, since that is also a thing in FQZParams as well,
// but there it is an array - is this the index ?
// but there it is an array
public int getSelectorTable() { return selectorTable; }
public void setSelectorTable(int selectorTable) { this.selectorTable = selectorTable; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,14 @@ public TokenStreams(final ByteBuffer inputByteBuffer, final int useArith, final
final ByteBuffer uncompressedTokenStream;
if (useArith != 0) {
final RangeDecode rangeDecode = new RangeDecode();
uncompressedTokenStream = rangeDecode.uncompress(ByteBuffer.wrap(compressedTokenStream));
uncompressedTokenStream = rangeDecode.uncompress(CompressionUtils.wrap(compressedTokenStream));
} else {
final RANSNx16Decode ransDecode = new RANSNx16Decode();
uncompressedTokenStream = ransDecode.uncompress(ByteBuffer.wrap(compressedTokenStream));
uncompressedTokenStream = ransDecode.uncompress(CompressionUtils.wrap(compressedTokenStream));
}
getStreamsForPos(tokenPosition)[tokenType] = uncompressedTokenStream;
}
}
//displayTokenStreamSizes();
}

private void displayTokenStreamSizes() {
for (int t = 0; t < TOTAL_TOKEN_TYPES; t++) {
System.out.print(String.format("%s ", typeToString(t)));
}
System.out.println();
for (int pos = 0; pos < tokenStreams.length; pos++) {
System.out.print(String.format("Pos %2d: ", pos));
for (int typ = 0; typ < tokenStreams[pos].length; typ++) {
final ByteBuffer bf = tokenStreams[pos][typ];
if (bf == null) {
System.out.print(String.format("%8s", "null"));
} else {
System.out.print(String.format("%8d", bf.limit()));
}
}
System.out.println();
}
}

private String typeToString(int i) {
switch (i) {
case TOKEN_TYPE:
return "TOKEN_TYPE";
case TOKEN_STRING:
return "TOKEN_STRING";
case TOKEN_CHAR:
return "TOKEN_CHAR";
case TOKEN_DIGITS0:
return "TOKEN_DIGITS0";
case TOKEN_DZLEN:
return "TOKEN_DZLEN";
case TOKEN_DUP:
return "TOKEN_DUP";
case TOKEN_DIFF:
return "TOKEN_DIFF";
case TOKEN_DIGITS:
return "TOKEN_DIGITS";
case TOKEN_DELTA:
return "TOKEN_DELTA";
case TOKEN_DELTA0:
return "TOKEN_DELTA0";
case TOKEN_MATCH:
return "TOKEN_MATCH";
case TOKEN_END:
return "TOKEN_END";
case TOKEN_NOP:
return "NOP";
default:
throw new CRAMException("Invalid name tokenizer tokenType: " + i);
}
}

public ByteBuffer[] getStreamsForPos(final int pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ private ByteBuffer uncompress(final ByteBuffer inBuffer, final int outSize) {
}
outBuffer.rewind();
return outBuffer;

}

private void uncompressOrder0(
Expand Down
91 changes: 47 additions & 44 deletions src/test/java/htsjdk/samtools/cram/CRAMInteropTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,60 @@
import java.util.List;

/**
* Interop test data originates in a separate repository, currently at https://github.com/samtools/htslib, in htscodecs,
* but we keep a copy in htsjdk so we can use it for round trip tests in CI without needing to clone a second repo.
* Interop test data originates in a separate repository, currently at hts-specs/test, so we keep a copy in
* htsjdk so we can use it for round trip tests in CI without needing to clone a second repo.
*/
public class CRAMInteropTestUtils {
public static final String INTEROP_TEST_FILES_PATH = "src/test/resources/htsjdk/samtools/cram/htslib_interop/";
public static final String INTEROP_TEST_FILES_PATH = "src/test/resources/htsjdk/hts-specs/test/cram/codecs";
public static final String GZIP_PATH = "gzip/";
public static final String GZIP_SUFFIX = ".gz";

/**
* @return the name and location of the local interop test data as specified by the
* variable INTEROP_TEST_FILES_PATH
*/
public static Path getCRAM31_Htslib_InteropTestDataLocation() {
return Paths.get(INTEROP_TEST_FILES_PATH + "cram31/");
public static Path getCRAMInteropTestDataLocation() {
return Paths.get(INTEROP_TEST_FILES_PATH);
}

// return a list of all encoded test data files in the interop/<compressedDir> directory
protected static List<Path> getCRAMInteropCompressedPaths(final String compressedDir) throws IOException {
final List<Path> paths = new ArrayList<>();
Files.newDirectoryStream(
CRAMInteropTestUtils.getCRAMInteropTestDataLocation().resolve(compressedDir),
path -> Files.isRegularFile(path))
.forEach(path -> paths.add(path));
return paths;
}

// Given a compressed test file path, return the corresponding uncompressed file path
protected static final Path getUnCompressedPathForCompressedPath(final Path compressedInteropPath) {
final String uncompressedFileName = getUncompressedFileName(compressedInteropPath);
return compressedInteropPath.getParent().getParent()
.resolve(CRAMInteropTestUtils.GZIP_PATH + uncompressedFileName);
}

private static final String getUncompressedFileName(final Path compressedPath) {
// Returns original filename from compressed file name
final String fileName = compressedPath.getFileName().toString();
final int lastDotIndex = fileName.lastIndexOf(".");
if (lastDotIndex >= 0) {
final String compressedFileName = fileName.substring(0, lastDotIndex);
return compressedFileName + CRAMInteropTestUtils.GZIP_SUFFIX;
} else {
throw new CRAMException("The name of the compressed file should contain a period followed by a number that" +
"indicates the order of compression. Actual compressed file name = "+ fileName);
}
}

// return a list of all raw test files in the hts-specs interop test directory (these files are raw in
// the sense that they are not compressed by any CRAM codec, although they ARE gzip compressed for inclusion
// in the repo)
protected static final List<Path> getRawCRAMInteropTestFiles() throws IOException {
final List<Path> paths = new ArrayList<>();
Files.newDirectoryStream(CRAMInteropTestUtils.getCRAMInteropTestDataLocation().resolve(GZIP_PATH))
.forEach(path -> paths.add(path));
return paths;
}

// the input files have embedded newlines that the test remove before round-tripping...
Expand All @@ -45,43 +87,4 @@ protected static final byte[] filterEmbeddedNewlines(final byte[] rawBytes) thro
}
}

// return a list of all encoded test data files in the htscodecs/tests/dat/<compressedDir> directory
protected static List<Path> getInteropCompressedFilePaths(final String compressedDir) throws IOException {
final List<Path> paths = new ArrayList<>();
Files.newDirectoryStream(
CRAMInteropTestUtils.getCRAM31_Htslib_InteropTestDataLocation().resolve("dat/"+compressedDir),
path -> Files.isRegularFile(path))
.forEach(path -> paths.add(path));
return paths;
}

// Given a compressed test file path, return the corresponding uncompressed file path
protected static final Path getUnCompressedFilePath(final Path compressedInteropPath) {
final String uncompressedFileName = getUncompressedFileName(compressedInteropPath.getFileName().toString());
// Example compressedInteropPath: ../dat/r4x8/q4.1 => unCompressedFilePath: ../dat/q4
return compressedInteropPath.getParent().getParent().resolve(uncompressedFileName);
}

private static final String getUncompressedFileName(final String compressedFileName) {
// Returns original filename from compressed file name
final int lastDotIndex = compressedFileName.lastIndexOf(".");
if (lastDotIndex >= 0) {
return compressedFileName.substring(0, lastDotIndex);
} else {
throw new CRAMException("The format of the compressed File Name is not as expected. " +
"The name of the compressed file should contain a period followed by a number that" +
"indicates the order of compression. Actual compressed file name = "+ compressedFileName);
}
}

// return a list of all raw test files in the htscodecs/tests/dat directory
protected static final List<Path> getInteropRawTestFiles() throws IOException {
final List<Path> paths = new ArrayList<>();
Files.newDirectoryStream(
CRAMInteropTestUtils.getCRAM31_Htslib_InteropTestDataLocation().resolve("dat"),
path -> (Files.isRegularFile(path)) && !Files.isHidden(path))
.forEach(path -> paths.add(path));
return paths;
}

}
8 changes: 5 additions & 3 deletions src/test/java/htsjdk/samtools/cram/FQZCompInteropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class FQZCompInteropTest extends HtsjdkTest {

Expand All @@ -31,10 +32,10 @@ public Object[][] getDecodeOnlyTestCases() throws IOException {
// compressed testfile path, uncompressed testfile path,
// FQZComp decoder
final List<Object[]> testCases = new ArrayList<>();
for (Path path : CRAMInteropTestUtils.getInteropCompressedFilePaths(COMPRESSED_FQZCOMP_DIR)) {
for (Path path : CRAMInteropTestUtils.getCRAMInteropCompressedPaths(COMPRESSED_FQZCOMP_DIR)) {
Object[] objects = new Object[]{
path,
CRAMInteropTestUtils.getUnCompressedFilePath(path),
CRAMInteropTestUtils.getUnCompressedPathForCompressedPath(path),
new FQZCompDecode()
};
testCases.add(objects);
Expand All @@ -49,7 +50,8 @@ public void testDecodeOnly(
final Path compressedFilePath,
final Path uncompressedInteropPath,
final FQZCompDecode fqzcompDecode) throws IOException {
try (final InputStream uncompressedInteropStream = Files.newInputStream(uncompressedInteropPath);
try (final InputStream uncompressedInteropStream =
new GZIPInputStream(Files.newInputStream(uncompressedInteropPath));
final InputStream preCompressedInteropStream = Files.newInputStream(compressedFilePath)
) {
// preprocess the uncompressed data (to match what the htscodecs-library test harness does)
Expand Down
Loading

0 comments on commit 282322a

Please sign in to comment.