Skip to content

Commit

Permalink
test(autogenerated): sort alleles in diplotype and skip slow genes
Browse files Browse the repository at this point in the history
  • Loading branch information
BinglanLi committed Mar 7, 2024
1 parent d17ca4a commit d75fe39
Showing 1 changed file with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.pharmgkb.pharmcat.haplotype.model.GeneCall;
import org.pharmgkb.pharmcat.haplotype.model.Result;
import org.pharmgkb.pharmcat.util.DataManager;
import org.pharmgkb.pharmcat.util.HaplotypeNameComparator;

import static org.pharmgkb.pharmcat.Constants.isLowestFunctionGene;

Expand All @@ -47,12 +48,14 @@
* @author Mark Woon
*/
public class AutogeneratedVcfTester implements AutoCloseable {
private static final List<String> sf_slowGenes = List.of("RYR1");
private static final ResultSerializer sf_resultSerializer = new ResultSerializer();
private final Path m_outputDir;
private final boolean m_saveData;
private final boolean m_exactMatchOnly;
private final boolean m_fuzzyMatch;
private final boolean m_testCyp2d6;
private final boolean m_skipSlowGenes;
private int m_numTests;
private final SortedMap<String, Integer> m_geneTestTotals = new TreeMap<>();
private final SortedMap<String, Integer> m_geneTestWarnings = new TreeMap<>();
Expand All @@ -62,14 +65,15 @@ public class AutogeneratedVcfTester implements AutoCloseable {


private AutogeneratedVcfTester(Path outputDir, boolean saveData, boolean exactMatchOnly, boolean fuzzyMatch,
boolean testCyp2d6) throws IOException {
boolean testCyp2d6, boolean skipSlowGenes) throws IOException {
Preconditions.checkArgument(!(exactMatchOnly && fuzzyMatch));
m_outputDir = outputDir;
m_errorWriter = new ErrorWriter(m_outputDir);
m_saveData = saveData;
m_exactMatchOnly = exactMatchOnly;
m_fuzzyMatch = fuzzyMatch;
m_testCyp2d6 = testCyp2d6;
m_skipSlowGenes = skipSlowGenes;
m_quiet = Boolean.parseBoolean(System.getenv("PHARMCAT_TEST_QUIET"));
}

Expand All @@ -89,6 +93,7 @@ public static void main(String[] args) {
.addOption("e", "exact-match-only", "only pass if matcher produces single exact match")
.addOption("f", "fuzzy-match", "pass if matcher produces any match")
.addOption("cyp2d6", "cyp2d6", "run CYP2D6 tests")
.addOption("skipSlowGenes", "skip-slow-genes", "skip slow genes")
;

cliHelper.execute(args, cli -> {
Expand All @@ -103,7 +108,7 @@ public static void main(String[] args) {
}

try (AutogeneratedVcfTester tester = new AutogeneratedVcfTester(outputDir, cliHelper.hasOption("s"),
exact, fuzzy, cliHelper.hasOption("nocyp2d6"))) {
exact, fuzzy, cliHelper.hasOption("cyp2d6"), cliHelper.hasOption("skipSlowGenes"))) {
Stopwatch stopwatch = Stopwatch.createStarted();

if (cliHelper.hasOption("g")) {
Expand All @@ -123,11 +128,13 @@ public static void main(String[] args) {
return 0;

} catch (Exception ex) {
//noinspection CallToPrintStackTrace
ex.printStackTrace();
return 1;
}
});
} catch (Exception ex) {
//noinspection CallToPrintStackTrace
ex.printStackTrace();
}
}
Expand All @@ -139,7 +146,11 @@ private void testAllGenes(Path vcfDir) throws Exception {
try (DirectoryStream<Path> geneDirStream = Files.newDirectoryStream(vcfDir)) {
for (Path geneDir : geneDirStream) {
if (Files.isDirectory(geneDir)) {
if (geneDir.getFileName().toString().equals("CYP2D6") && !m_testCyp2d6) {
String gene = geneDir.getFileName().toString();
if (gene.equals("CYP2D6") && !m_testCyp2d6) {
continue;
}
if (m_skipSlowGenes && sf_slowGenes.contains(gene)) {
continue;
}
testGene(geneDir);
Expand Down Expand Up @@ -198,7 +209,7 @@ private void test(String gene, NamedAlleleMatcher namedAlleleMatcher, VcfFile vc
String expectedDiplotype = vcfReader.getVcfMetadata().getRawProperties().get("PharmCATnamedAlleles").get(0);

List<String> expectedAlleles = Arrays.asList(expectedDiplotype.split("/"));
Collections.sort(expectedAlleles);
expectedAlleles.sort(new HaplotypeNameComparator());
expectedDiplotype = String.join("/", expectedAlleles);

boolean hasUnknownCall = expectedAlleles.contains("?");
Expand Down Expand Up @@ -257,7 +268,7 @@ private void test(String gene, Path vcfFile, DefinitionReader definitionReader,
boolean gotExpectedInTopPair = false;
List<DiplotypeMatch> topPairs = new ArrayList<>();
List<DiplotypeMatch> alternatePairs = new ArrayList<>();
if (matches.size() > 0) {
if (!matches.isEmpty()) {
int topScore = matches.iterator().next().getScore();
for (DiplotypeMatch match : matches) {
boolean isMatch = isMatch(expectedDiplotype, match);
Expand All @@ -276,7 +287,7 @@ private void test(String gene, Path vcfFile, DefinitionReader definitionReader,
}

if (hasUnknownCall || hasComboCall) {
if (topPairs.size() > 0) {
if (!topPairs.isEmpty()) {
fail(gene, vcfFile, result, topPairs, alternatePairs, "no call (" + expectedDiplotype + ")", null, exemption);
}
return;
Expand All @@ -288,7 +299,7 @@ private void test(String gene, Path vcfFile, DefinitionReader definitionReader,
String extraMsg = null;
if (gotExpected) {
List<String> errors = checkOverlaps(definitionReader.getPositions(gene), matches);
if (errors.size() > 0) {
if (!errors.isEmpty()) {
if (errors.size() == 1) {
extraMsg = errors.get(0);
} else {
Expand Down Expand Up @@ -319,7 +330,6 @@ private void test(String gene, Path vcfFile, DefinitionReader definitionReader,
}

private List<String> checkOverlaps(VariantLocus[] positions, Set<DiplotypeMatch> matches) {
//noinspection UnstableApiUsage
Set<Set<DiplotypeMatch>> combinations = Sets.combinations(matches, 2);
List<String> overlaps = new ArrayList<>();
for (Set<DiplotypeMatch> combo : combinations) {
Expand All @@ -337,7 +347,7 @@ private List<String> checkOverlaps(VariantLocus[] positions, Set<DiplotypeMatch>
StringBuilder errBuilder = new StringBuilder();
for (int x = 0; x < size; x += 1) {
if (!Objects.requireNonNull(alleles1[x]).equals(alleles2[x])) {
if (errBuilder.length() > 0) {
if (!errBuilder.isEmpty()) {
errBuilder.append("\n");
}
errBuilder.append(" Mismatch in ")
Expand Down Expand Up @@ -372,7 +382,7 @@ private String buildAllele(DiplotypeMatch m, int x) {
bases.add(allele);
}
}
if (bases.size() > 0) {
if (!bases.isEmpty()) {
return String.join(",", bases);
}
return null;
Expand All @@ -384,7 +394,7 @@ private String buildAllele(DiplotypeMatch m, int x) {
*/
private static boolean isMatch(String expected, DiplotypeMatch match) {
List<String> rezAlleles = Arrays.asList(match.getName().split("/"));
Collections.sort(rezAlleles);
rezAlleles.sort(new HaplotypeNameComparator());
String rez = String.join("/", rezAlleles);
return rez.equals(expected);
}
Expand Down Expand Up @@ -423,7 +433,7 @@ private void saveData(Path vcfFile, Result result) throws IOException {
}

private static boolean isFuzzyMatch(List<String> expectedAlleles, Collection<DiplotypeMatch> matches) {
Collections.sort(expectedAlleles);
expectedAlleles.sort(new HaplotypeNameComparator());
String expected = String.join("/", expectedAlleles);
return matches.stream()
.anyMatch(dm -> isMatch(expected, dm));
Expand Down Expand Up @@ -454,7 +464,7 @@ private static class ErrorMessage implements Comparable<ErrorMessage> {
gene = geneCall.getGene();
MatchData matchData = geneCall.getMatchData();
StringBuilder keyBuilder = new StringBuilder(geneCall.getGene());
if (matchData.getMissingPositions().size() > 0) {
if (!matchData.getMissingPositions().isEmpty()) {
missingPositions = matchData.getMissingPositions().stream()
.map(VariantLocus::toString)
.collect(Collectors.joining(", "));
Expand All @@ -474,12 +484,12 @@ private static class ErrorMessage implements Comparable<ErrorMessage> {
isWarning = warn;
actual = topPairs.stream()
.map(DiplotypeMatch::getName)
.collect(Collectors.joining(", "));
.collect(Collectors.joining("; "));
actualFuzzyMatch = isFuzzyMatch(expectedAlleles, topPairs);
if (alternatePairs.size() > 0) {
if (!alternatePairs.isEmpty()) {
alt = alternatePairs.stream()
.map(m -> m.getName() + " (" + m.getScore() + ")")
.collect(Collectors.joining(", "));
.collect(Collectors.joining("; "));
altFuzzyMatch = isFuzzyMatch(expectedAlleles, alternatePairs);
} else {
noAlt = true;
Expand Down Expand Up @@ -522,7 +532,7 @@ private static class ErrorMessage implements Comparable<ErrorMessage> {
.append(" Expected: ").append(expectedDiplotype)
.append("\n")
.append(" Actual: ").append(actual);
if (topPairs.size() > 0) {
if (!topPairs.isEmpty()) {
errBuilder.append(" (")
.append(topPairs.get(0).getScore())
.append(")");
Expand Down Expand Up @@ -677,7 +687,7 @@ private void printSummary(int numTests, SortedMap<String, Integer> geneTestTotal
m_writer.println("| ---------- | -------- | -------- |");
for (String gene : geneTestTotals.keySet()) {
int warnings = geneTestWarnings.computeIfAbsent(gene, c -> 0);
int failures = geneTestFailures.computeIfAbsent(gene, c -> 0);;
int failures = geneTestFailures.computeIfAbsent(gene, c -> 0);
m_writer.println(String.format("| %-10s | %7s | %7s |", gene, numFormatter.format(warnings), numFormatter.format(failures)));
}
}
Expand Down

0 comments on commit d75fe39

Please sign in to comment.