Skip to content

Commit

Permalink
Fix issue in merging data and json files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Oct 25, 2023
1 parent a220ac7 commit bfc5450
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/genepi/riskscore/io/OutputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class OutputFile {
Expand All @@ -13,11 +15,18 @@ public class OutputFile {

private List<String> scores;

private Map<String, Integer> scoresIndex = new HashMap<String, Integer>();

public OutputFile(String filename) throws IOException {

OutputFileReader outputFile = new OutputFileReader(filename);

scores = outputFile.getScores();
int index = 0;
for (String score: scores) {
scoresIndex.put(score, index);
index++;
}

samples = new Vector<String>();
data = new Vector<double[]>();
Expand Down Expand Up @@ -45,15 +54,16 @@ public int getCountScores() {
public List<String> getScores() {
return scores;
}

public double getValue(int score, int sample) {
return data.get(sample)[score];
}

public double[] getValuesByScore(int score) {
public double[] getValuesByScore(String score) {
int scoreIndex = scoresIndex.get(score);
double[] values = new double[samples.size()];
for (int i = 0; i < values.length; i++) {
values[i] = getValue(score, i);
values[i] = getValue(scoreIndex, i);
}
return values;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/genepi/riskscore/io/ReportFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void mergeWithData(OutputFile data) {
// ignore empty scores
if (getSummaries().get(i).getVariantsUsed() > 0) {
if (data != null) {
getSummaries().get(i).setData(data.getValuesByScore(i));
getSummaries().get(i).setData(data.getValuesByScore(getSummaries().get(i).getName()));
}
}
getSummaries().get(i).updateStatistics();
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/genepi/riskscore/tasks/MergeScoreTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ public void assertEqualsScoreFiles(String filename1, String filename2, double de
assertEquals(file1.getSamples().size(), file2.getSamples().size());
assertEquals(file1.getScores().size(), file2.getScores().size());
for (int i = 0; i < file1.getScores().size(); i++) {
assertEquals(file1.getScores().get(i), file2.getScores().get(i));
double[] values1 = file1.getValuesByScore(i);
double[] values2 = file2.getValuesByScore(i);
String name1 = file1.getScores().get(i);
String name2 = file2.getScores().get(i);
assertEquals(name1, name2);
double[] values1 = file1.getValuesByScore(name1);
double[] values2 = file2.getValuesByScore(name2);
assertEquals(values1.length, values2.length);
assertEquals(values1.length, file1.getSamples().size());
for (int j = 0; j < values1.length; j++) {
Expand Down

0 comments on commit bfc5450

Please sign in to comment.