Skip to content

Commit

Permalink
Relaxed exact match to comparison at double precision of 2E-6 to acco…
Browse files Browse the repository at this point in the history
…unt for Java 8 and Java 11 differences on Travis.
  • Loading branch information
samuelklee committed Jan 27, 2022
1 parent 532533a commit aef0006
Showing 1 changed file with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.broadinstitute.hellbender.tools.copynumber;

import htsjdk.samtools.SAMException;
import htsjdk.samtools.util.IOUtil;
import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.testutils.ArgumentsBuilder;
Expand All @@ -20,9 +21,12 @@
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Integration tests for {@link ModelSegments}. We test for input validation across various run modes of the tool
Expand Down Expand Up @@ -57,6 +61,8 @@ public final class ModelSegmentsIntegrationTest extends CommandLineProgramTest {
private static final SampleLocatableMetadata TUMOR_1_EXPECTED_METADATA = new CopyRatioCollection(TUMOR_1_DENOISED_COPY_RATIOS_FILE).getMetadata();
private static final SampleLocatableMetadata TUMOR_2_EXPECTED_METADATA = new CopyRatioCollection(TUMOR_2_DENOISED_COPY_RATIOS_FILE).getMetadata();
private static final SampleLocatableMetadata NORMAL_EXPECTED_METADATA = new AllelicCountCollection(NORMAL_ALLELIC_COUNTS_FILE).getMetadata();

private static final double DOUBLE_PRECISION = 2E-6;

@Test
public void testMetadata() {
Expand Down Expand Up @@ -219,7 +225,7 @@ private static void assertOutputFilesSingleSample(final File outputDir,
ModelSegments.SEGMENTS_FILE_SUFFIX,
ModelSegments.COPY_RATIO_MODEL_PARAMETER_FILE_SUFFIX,
ModelSegments.ALLELE_FRACTION_MODEL_PARAMETER_FILE_SUFFIX)) {
IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + fileTag + suffix),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + fileTag + suffix));
}
Expand All @@ -238,31 +244,31 @@ private static void assertOutputFilesSingleSample(final File outputDir,
Assert.assertEquals(TUMOR_1_EXPECTED_METADATA.getSampleName(), alleleFractionParameters.getMetadata().getSampleName());
}

IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.COPY_RATIO_SEGMENTS_FOR_CALLER_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.COPY_RATIO_SEGMENTS_FOR_CALLER_FILE_SUFFIX));
final CopyRatioSegmentCollection copyRatioSegments = new CopyRatioSegmentCollection(
new File(outputDir, outputPrefix + ModelSegments.COPY_RATIO_SEGMENTS_FOR_CALLER_FILE_SUFFIX));
Assert.assertEquals(TUMOR_1_EXPECTED_METADATA, copyRatioSegments.getMetadata());

IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.COPY_RATIO_LEGACY_SEGMENTS_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.COPY_RATIO_LEGACY_SEGMENTS_FILE_SUFFIX));
IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.ALLELE_FRACTION_LEGACY_SEGMENTS_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.ALLELE_FRACTION_LEGACY_SEGMENTS_FILE_SUFFIX));

AllelicCountCollection hetAllelicCounts = null;
if (isAllelicCountsPresent) {
IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.HET_ALLELIC_COUNTS_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.HET_ALLELIC_COUNTS_FILE_SUFFIX));
hetAllelicCounts = new AllelicCountCollection(
new File(outputDir, outputPrefix + ModelSegments.HET_ALLELIC_COUNTS_FILE_SUFFIX));
Assert.assertEquals(TUMOR_1_EXPECTED_METADATA, hetAllelicCounts.getMetadata());
}
if (isNormalAllelicCountsPresent) { //if this is true, case sample allelic counts will be present
IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.NORMAL_HET_ALLELIC_COUNTS_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.NORMAL_HET_ALLELIC_COUNTS_FILE_SUFFIX));
final AllelicCountCollection hetNormalAllelicCounts = new AllelicCountCollection(
Expand Down Expand Up @@ -483,8 +489,58 @@ private static void assertOutputFilesMultipleSamples(final File outputDir,
Assert.assertFalse(new File(outputDir, outputPrefix + ModelSegments.HET_ALLELIC_COUNTS_FILE_SUFFIX).exists());
Assert.assertFalse(new File(outputDir, outputPrefix + ModelSegments.NORMAL_HET_ALLELIC_COUNTS_FILE_SUFFIX).exists());

IOUtil.assertFilesEqual(
assertFilesEqualUpToDoublePrecision(
new File(outputDir, outputPrefix + ModelSegments.PICARD_INTERVAL_LIST_FILE_SUFFIX),
new File(EXACT_MATCH_EXPECTED_SUB_DIR, outputPrefix + ModelSegments.PICARD_INTERVAL_LIST_FILE_SUFFIX));
}

/**
* See https://github.com/broadinstitute/gatk/pull/7652#issuecomment-1023649096.
*/
public static void assertFilesEqualUpToDoublePrecision(final File f1,
final File f2) {
try {
IOUtil.assertFilesEqual(f1, f2);
} catch (final SAMException e) {
logger.warn(String.format("Files %s and %s failed exact-match test, attempting comparison with double precision %f...%n",
f1, f2, DOUBLE_PRECISION));
try {
final List<String> lines1 = Files.lines(f1.toPath()).collect(Collectors.toList());
final List<String> lines2 = Files.lines(f2.toPath()).collect(Collectors.toList());
Assert.assertEquals(lines1.size(), lines2.size(),
String.format("Files %s and %s do not have the same number of lines.", f1, f2));

for (int i = 0; i < lines1.size(); i++) {
final String line1 = lines1.get(i);
final String line2 = lines2.get(i);
if (line1.equals(line2)) {
continue;
}
final List<String> splitLine1 = Arrays.asList(line1.split("\t"));
final List<String> splitLine2 = Arrays.asList(line2.split("\t"));
Assert.assertEquals(splitLine1.size(), splitLine2.size(),
String.format("Line %d does not have the same number of fields in files %s and %s.", i, f1, f2));
for (int j = 0; j < splitLine1.size(); j++) {
final String field1 = splitLine1.get(j);
final String field2 = splitLine2.get(j);
if (field1.equals(field2)) {
continue;
}
try {
final double double1 = Double.parseDouble(field1);
final double double2 = Double.parseDouble(field2);
Assert.assertEquals(double1, double2, DOUBLE_PRECISION,
String.format("Field %d in line %d in files %s and %s is not equivalent up to double precision %f: %f != %f.",
j, i, f1, f2, DOUBLE_PRECISION, double1, double2));
} catch (final NumberFormatException nfe) {
Assert.fail(String.format("Non-double field %d in line %d in files %s and %s is not identical: %s != %s.",
j, i, f1, f2, field1, field2));
}
}
}
} catch (final IOException ioe) {
Assert.fail(String.format("Encountered IOException when trying to compare %s and %s: %s", f1, f2, ioe));
}
}
}
}

0 comments on commit aef0006

Please sign in to comment.