Skip to content

Commit

Permalink
Add back mutation pattern regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philiptzou committed Feb 17, 2024
1 parent 8dd3cb9 commit cf9ab78
Show file tree
Hide file tree
Showing 15 changed files with 4,793 additions and 4,981 deletions.
2 changes: 1 addition & 1 deletion .latest-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240113194711
20240121063513
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'edu.stanford.hivdb'
version = '3.4.10'
version = '3.4.11'

sourceCompatibility = 15
targetCompatibility = 15
Expand Down
2 changes: 1 addition & 1 deletion sierra-core
2 changes: 1 addition & 1 deletion sierra-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'edu.stanford.hivdb'
version = '3.4.10'
version = '3.4.11'

description = """Sierra Unit Tests"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

package edu.stanford.hivdb.drugresistance.mutationpattern;

//import java.util.List;
//import edu.stanford.hivdb.drugresistance.database.MutationPatterns;
//import edu.stanford.hivdb.testutils.TestUtils;
import edu.stanford.hivdb.drugresistance.algorithm.DrugResistanceAlgorithm;
import edu.stanford.hivdb.drugs.DrugClass;
import edu.stanford.hivdb.hivfacts.HIV;
import edu.stanford.hivdb.testutils.TestUtils;

/**
* Creates 4 files, each containing all patterns of drug-resistance mutations for a drug class
Expand All @@ -34,25 +35,19 @@
* where they can be used for j-unit testing
*
*/
//public class MutationPatternFilesCreator {
// private static final String OUTPUT_DIR = "src/test/resources/MutationPatternsFiles/";
//
// public static void main (String[] args) {
// for (HIVDrugClass drugClass : HIVDrugClass.values()) {
// MutationPatterns mutPatterns = new MutationPatterns(drugClass);
// List<String> orderedMutPatterns = mutPatterns.getOrderedMutPatterns();
//
// String filePath = OUTPUT_DIR + "Patterns" + drugClass + ".txt";
// StringBuffer output = new StringBuffer();
// int patternNo = 0;
// for (String pattern : orderedMutPatterns) {
// patternNo++;
// int count = mutPatterns.getPatternCount(pattern);
// output.append(">" + drugClass + patternNo + " count:" + count + "\n");
// output.append(pattern + "\n");
// }
// output.append("#comment for testing\n" + ">blank line for testing\n");
// TestUtils.writeFile(filePath, output.toString());
// }
// }
//}
public class MutationPatternExpectedsGenerator {
private static final String OUTPUT_DIR = "hivfacts/data/patterns-hiv1/";
private static final HIV hiv = HIV.getInstance();


public static void main (String[] args) {
DrugResistanceAlgorithm<HIV> algorithm = hiv.getDefaultDrugResistAlgorithm();
for (DrugClass<HIV> drugClass : hiv.getMainStrain().getDrugClasses()) {
MutationPatterns mutPatterns = new MutationPatterns(drugClass);

String filePath = OUTPUT_DIR + "patterns-" + drugClass + ".json";
TestUtils.writeFile(filePath, mutPatterns.dumps(algorithm));
System.out.println(filePath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright (C) 2024 Stanford HIVDB team
This file is part of Sierra.
Sierra is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Sierra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Sierra. If not, see <https://www.gnu.org/licenses/>.
*/
package edu.stanford.hivdb.drugresistance.mutationpattern;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;

import com.google.gson.reflect.TypeToken;

import edu.stanford.hivdb.drugresistance.GeneDR;
import edu.stanford.hivdb.drugresistance.algorithm.ASIDrugSusc;
import edu.stanford.hivdb.drugresistance.algorithm.DrugResistanceAlgorithm;
import edu.stanford.hivdb.drugs.Drug;
import edu.stanford.hivdb.drugs.DrugClass;
import edu.stanford.hivdb.hivfacts.HIV;
import edu.stanford.hivdb.mutations.MutationSet;
import edu.stanford.hivdb.testutils.TestUtils;
import edu.stanford.hivdb.utilities.Json;
import edu.stanford.hivdb.viruses.Gene;
import edu.stanford.hivdb.viruses.Strain;


public class MutationPatterns {

private static HIV hiv = HIV.getInstance();

public static class MutationPattern {
public final Gene<HIV> gene;
public final DrugClass<HIV> drugClass;
public final MutationSet<HIV> mutations;
public final Integer count;
private transient GeneDR<HIV> geneDR = null;

public MutationPattern(
Gene<HIV> gene,
DrugClass<HIV> drugClass,
MutationSet<HIV> mutations,
Integer count
) {
this.gene = gene;
this.drugClass = drugClass;
this.mutations = mutations;
this.count = count;
}

public GeneDR<HIV> getGeneDR(DrugResistanceAlgorithm<HIV> algorithm) {
if (geneDR == null) {
geneDR = new GeneDR<>(gene, mutations, algorithm);
}
return geneDR;
}

@Override
public String toString() {
return String.format(
"MutationPattern(%s, count=%d, gene=%s, drugClass=%d)",
mutations.join(), count, gene, drugClass);
}
}

private static Map<DrugClass<HIV>, List<MutationPattern>> allMutationPatterns;

public static Map<DrugClass<HIV>, List<MutationPattern>> load() throws IOException {
String resPath = "patterns_hiv1.json";
String payload = TestUtils.readTestResourceToString(resPath);
ArrayList<LinkedHashMap<String, ?>> rawPatterns = Json.loads(
payload,
new TypeToken<ArrayList<LinkedHashMap<String, ?>>>(){}
);
LinkedHashMap<DrugClass<HIV>, List<MutationPattern>> patterns = new LinkedHashMap<>();
Strain<HIV> hiv1b = hiv.getMainStrain();
for (LinkedHashMap<String, ?> rawPattern : rawPatterns) {
DrugClass<HIV> dc = hiv.getDrugClass((String) rawPattern.get("drug_class"));
Gene<HIV> gene = hiv1b.getGene(dc.getAbstractGene());
Integer count = ((Double) rawPattern.get("count")).intValue();
MutationSet<HIV> mutations = MutationSet.parseString(
gene,
(String) rawPattern.get("pattern")
);
MutationPattern pattern = new MutationPattern(gene, dc, mutations, count);
if (!patterns.containsKey(dc)) {
patterns.put(dc, new ArrayList<>());
}
patterns.get(dc).add(pattern);
}
return patterns;
}

static {
try {
allMutationPatterns = load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private DrugClass<HIV> drugClass;

/**
* The tblPatterns<DrugClass> in HIVDB_Scores has the following fields:
* Pattern, Count, Version, Drug, Level, TotalScore, MutScore, ComboScore, MutScoreText, ComboScoreText
* The Version should always be the same as the active version used by the program
*
* @param drugClass
*/
public MutationPatterns(DrugClass<HIV> drugClass) {
this.drugClass = drugClass;
}

public DrugClass<HIV> getDrugClass() { return drugClass; }

public List<MutationPattern> getPatterns() {
return allMutationPatterns.getOrDefault(drugClass, Collections.emptyList());
}

public String dumps(DrugResistanceAlgorithm<HIV> algorithm) {
List<Map<String, ?>> results = new ArrayList<>();
for (MutationPattern pattern : getPatterns()) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("gene", pattern.gene.getAbstractGene());
result.put("drugClass", pattern.drugClass.getName());
result.put("pattern", pattern.mutations.join());
result.put("count", pattern.count);
GeneDR<HIV> geneDR = pattern.getGeneDR(algorithm);
for(Drug<HIV> drug : pattern.drugClass.getDrugs()) {
ASIDrugSusc<HIV> susc = geneDR.getDrugSusc(drug);
result.put(String.format("%s Level", drug.getDisplayAbbr()), susc.getLevel());
result.put(String.format("%s Score", drug.getDisplayAbbr()), susc.getScore());
}
results.add(result);
}
return Json.dumps(results);
}

}
Loading

0 comments on commit cf9ab78

Please sign in to comment.