Fixes for bugs uncovered by unit tests

This commit is contained in:
Eric Banks 2012-11-06 16:07:40 -08:00
parent b07106b3a7
commit 0a2dded093
2 changed files with 17 additions and 10 deletions

View File

@ -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));
} }
/** /**

View File

@ -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) {