Fixes for bugs uncovered by unit tests
This commit is contained in:
parent
b07106b3a7
commit
0a2dded093
|
|
@ -102,17 +102,24 @@ public class AlleleBiasedDownsamplingUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int scoreAlleleCounts(final int[] alleleCounts) {
|
private static int scoreAlleleCounts(final int[] alleleCounts) {
|
||||||
final int maxIndex = MathUtils.maxElementIndex(alleleCounts);
|
if ( alleleCounts.length < 2 )
|
||||||
final int maxCount = alleleCounts[maxIndex];
|
return 0;
|
||||||
|
|
||||||
int nonMaxCount = 0;
|
// sort the counts (in ascending order)
|
||||||
for ( int i = 0; i < 4; i++ ) {
|
final int[] alleleCountsCopy = alleleCounts.clone();
|
||||||
if ( i != maxIndex )
|
Arrays.sort(alleleCountsCopy);
|
||||||
nonMaxCount += alleleCounts[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to get the best score: in the het case the counts should be equal and in the hom case the non-max should be zero
|
final int maxCount = alleleCountsCopy[alleleCounts.length - 1];
|
||||||
return Math.min(Math.abs(maxCount - nonMaxCount), Math.abs(nonMaxCount));
|
final int nextBestCount = alleleCountsCopy[alleleCounts.length - 2];
|
||||||
|
|
||||||
|
int remainderCount = 0;
|
||||||
|
for ( int i = 0; i < alleleCounts.length - 2; i++ )
|
||||||
|
remainderCount += alleleCountsCopy[i];
|
||||||
|
|
||||||
|
// try to get the best score:
|
||||||
|
// - in the het case the counts should be equal with nothing else
|
||||||
|
// - in the hom case the non-max should be zero
|
||||||
|
return Math.min(maxCount - nextBestCount + remainderCount, Math.abs(nextBestCount + remainderCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ public class AlleleBiasedDownsamplingUtilsUnitTest extends BaseTest {
|
||||||
actualCounts[3] += addT;
|
actualCounts[3] += addT;
|
||||||
|
|
||||||
final int[] results = AlleleBiasedDownsamplingUtils.runSmartDownsampling(actualCounts, (int)(pileupSize * contaminationFraction));
|
final int[] results = AlleleBiasedDownsamplingUtils.runSmartDownsampling(actualCounts, (int)(pileupSize * contaminationFraction));
|
||||||
Assert.assertTrue(countsAreEqual(actualCounts, targetCounts));
|
Assert.assertTrue(countsAreEqual(results, targetCounts));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean countsAreEqual(final int[] counts1, final int[] counts2) {
|
private static boolean countsAreEqual(final int[] counts1, final int[] counts2) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue