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) {
final int maxIndex = MathUtils.maxElementIndex(alleleCounts);
final int maxCount = alleleCounts[maxIndex];
if ( alleleCounts.length < 2 )
return 0;
int nonMaxCount = 0;
for ( int i = 0; i < 4; i++ ) {
if ( i != maxIndex )
nonMaxCount += alleleCounts[i];
}
// sort the counts (in ascending order)
final int[] alleleCountsCopy = alleleCounts.clone();
Arrays.sort(alleleCountsCopy);
// 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
return Math.min(Math.abs(maxCount - nonMaxCount), Math.abs(nonMaxCount));
final int maxCount = alleleCountsCopy[alleleCounts.length - 1];
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;
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) {