Skip to content

Commit

Permalink
Add FQZComp Interop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-puligundla committed Feb 7, 2024
1 parent 66d7889 commit a967465
Showing 1 changed file with 32 additions and 58 deletions.
90 changes: 32 additions & 58 deletions src/test/java/htsjdk/samtools/cram/FQZCompInteropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,66 @@

public class FQZCompInteropTest extends HtsjdkTest {

public static final String COMPRESSED_FQZComp_DIR = "fqzcomp";
public static final String COMPRESSED_FQZCOMP_DIR = "fqzcomp";

@DataProvider(name = "allFQZCompFiles")
public Object[][] getAllFQZCompCodecsForRoundTrip() throws IOException {
// uses the available compressed interop test files
@DataProvider(name = "decodeOnlyTestCases")
public Object[][] getDecodeOnlyTestCases() throws IOException {

// params:
// compressed testfile path, uncompressed testfile path, FQZComp decoder
// compressed testfile path, uncompressed testfile path,
// FQZComp decoder
final List<Object[]> testCases = new ArrayList<>();
for (Path path : getInteropFQZCompCompressedFiles()) {
for (Path path : CRAMInteropTestUtils.getInteropCompressedFilePaths(COMPRESSED_FQZCOMP_DIR)) {
Object[] objects = new Object[]{
path,
getFQZCompUnCompressedFilePath(path),
new FQZCompDecode(),
CRAMInteropTestUtils.getUnCompressedFilePath(path),
new FQZCompDecode()
};
testCases.add(objects);
}
return testCases.toArray(new Object[][]{});
}

@Test(description = "Test if CRAM Interop Test Data is available")
public void testGetHTSCodecsCorpus() {
public void testHtsCodecsCorpusIsAvailable() {
if (!CRAMInteropTestUtils.isInteropTestDataAvailable()) {
throw new SkipException(String.format("CRAM Interop Test Data is not available at %s",
CRAMInteropTestUtils.INTEROP_TEST_FILES_PATH));
}
}

@Test(
dependsOnMethods = "testGetHTSCodecsCorpus",
dataProvider = "allFQZCompFiles",
description = "Uncompress the existing compressed file using htsjdk FQZComp Codec and compare it with the original file.")
public void testFQZCompPreCompressed(
@Test (
dependsOnMethods = "testHtsCodecsCorpusIsAvailable",
dataProvider = "decodeOnlyTestCases",
description = "Uncompress the existing compressed file using htsjdk FQZcomp codec and compare it with the original file.")
public void testDecodeOnly(
final Path compressedFilePath,
final Path uncompressedFilePath,
// final NameTokenisationEncode unsusednameTokenisationEncode,
final FQZCompDecode fqzCompDecode) throws IOException {
// final Path compressedFilePath= Paths.get("../htscodecs_1/tests/dat/fqzcomp/q4.1");
// final Path uncompressedFilePath = Paths.get("../htscodecs_1/tests/dat/q4");
try(final InputStream preCompressedInteropStream = Files.newInputStream(compressedFilePath);
final InputStream unCompressedInteropStream = Files.newInputStream(uncompressedFilePath)){
final Path uncompressedInteropPath,
final FQZCompDecode fqzcompDecode) throws IOException {
try (final InputStream uncompressedInteropStream = Files.newInputStream(uncompressedInteropPath);
final InputStream preCompressedInteropStream = Files.newInputStream(compressedFilePath)
) {
// preprocess the uncompressed data (to match what the htscodecs-library test harness does)
// by filtering out the embedded newlines, and then round trip through FQZComp codec
// and compare the results

final ByteBuffer preCompressedInteropBytes = ByteBuffer.wrap(IOUtils.toByteArray(preCompressedInteropStream));
final ByteBuffer unCompressedInteropBytes = ByteBuffer.wrap(IOUtils.toByteArray(unCompressedInteropStream));

// Use htsjdk to uncompress the precompressed file from htscodecs repo
final ByteBuffer uncompressedHtsjdkBytes = fqzCompDecode.uncompress(preCompressedInteropBytes);
final ByteBuffer uncompressedHtsjdkBytes = fqzcompDecode.uncompress(preCompressedInteropBytes);

final ByteBuffer uncompressedInteropBytes = ByteBuffer.wrap(IOUtils.toByteArray(uncompressedInteropStream));
// TODO!!: the precompressed interop files seem different to the other codecs.
// For htslib compression using FQZComp, "\n" was not filtered out of these test files
// unlike what we observed in the RANS, Range or Name Tokenizer compressed interop test files.

// Compare the htsjdk uncompressed bytes with the original input file from htscodecs repo
Assert.assertEquals(uncompressedHtsjdkBytes, unCompressedInteropBytes);
Assert.assertEquals(uncompressedHtsjdkBytes, uncompressedInteropBytes);
} catch (final NoSuchFileException ex){
throw new SkipException("Skipping testNameTokenizationPrecompressed as either input file " +
throw new SkipException("Skipping testDecodeOnly as either input file " +
"or precompressed file is missing.", ex);
}
}

// return a list of all FQZComp encoded test data files in the htscodecs/tests/dat/fqzcomp directory
private List<Path> getInteropFQZCompCompressedFiles() throws IOException {
final List<Path> paths = new ArrayList<>();
Files.newDirectoryStream(
CRAMInteropTestUtils.getInteropTestDataLocation().resolve("dat/"+COMPRESSED_FQZComp_DIR),
path -> Files.isRegularFile(path))
.forEach(path -> paths.add(path));
return paths;
}

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

public static final String getUncompressedFileName(final String compressedFileName) {
// Returns original filename from compressed file name
int lastDotIndex = compressedFileName.lastIndexOf(".");
if (lastDotIndex >= 0) {
String fileName = compressedFileName.substring(0, lastDotIndex);
return fileName;
} 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 type of compression. Actual compressed file name = "+ compressedFileName);
}
}





}

0 comments on commit a967465

Please sign in to comment.