Major optimizations to graph construction through better use of built in graph.containsVertex and vertex.equals methods. Minor optimizations to MathUtils.approximateLog10SumLog10 method

This commit is contained in:
Ryan Poplin 2012-03-26 11:32:44 -04:00
parent 1fa66f76c9
commit 019145175b
2 changed files with 30 additions and 26 deletions

View File

@ -72,6 +72,11 @@ public class Haplotype {
public boolean equals( Object h ) {
return h instanceof Haplotype && Arrays.equals(bases, ((Haplotype) h).bases);
}
@Override
public int hashCode() {
return Arrays.hashCode(bases);
}
public void addReadLikelihoods( final String sample, final double[] readLikelihoods ) {
if( readLikelihoodsPerSample == null ) {

View File

@ -50,7 +50,8 @@ public class MathUtils {
public static final double[] log10Cache;
private static final double[] jacobianLogTable;
private static final double JACOBIAN_LOG_TABLE_STEP = 0.001;
private static final double MAX_JACOBIAN_TOLERANCE = 10.0;
private static final double JACOBIAN_LOG_TABLE_INV_STEP = 1.0 / 0.001;
private static final double MAX_JACOBIAN_TOLERANCE = 8.0;
private static final int JACOBIAN_LOG_TABLE_SIZE = (int) (MAX_JACOBIAN_TOLERANCE / JACOBIAN_LOG_TABLE_STEP) + 1;
private static final int MAXN = 11000;
private static final int LOG10_CACHE_SIZE = 4 * MAXN; // we need to be able to go up to 2*(2N) when calculating some of the coefficients
@ -73,7 +74,7 @@ public class MathUtils {
// under/overflow checking, so this shouldn't be used in the general case (but is fine
// if one is already make those checks before calling in to the rounding).
public static int fastRound(double d) {
return (d > 0) ? (int) (d + 0.5d) : (int) (d - 0.5d);
return (d > 0.0) ? (int) (d + 0.5d) : (int) (d - 0.5d);
}
public static double approximateLog10SumLog10(final double[] vals) {
@ -84,8 +85,6 @@ public class MathUtils {
final int maxElementIndex = MathUtils.maxElementIndex(vals, endIndex);
double approxSum = vals[maxElementIndex];
if (approxSum == Double.NEGATIVE_INFINITY)
return approxSum;
for (int i = 0; i < endIndex; i++) {
if (i == maxElementIndex || vals[i] == Double.NEGATIVE_INFINITY)
@ -94,7 +93,7 @@ public class MathUtils {
final double diff = approxSum - vals[i];
if (diff < MathUtils.MAX_JACOBIAN_TOLERANCE) {
// See notes from the 2-inout implementation below
final int ind = fastRound(diff / MathUtils.JACOBIAN_LOG_TABLE_STEP); // hard rounding
final int ind = fastRound(diff * MathUtils.JACOBIAN_LOG_TABLE_INV_STEP); // hard rounding
approxSum += MathUtils.jacobianLogTable[ind];
}
}
@ -123,7 +122,7 @@ public class MathUtils {
// max(x,y) + log10(1+10^-abs(x-y))
// we compute the second term as a table lookup with integer quantization
// we have pre-stored correction for 0,0.1,0.2,... 10.0
final int ind = fastRound(diff / MathUtils.JACOBIAN_LOG_TABLE_STEP); // hard rounding
final int ind = fastRound(diff * MathUtils.JACOBIAN_LOG_TABLE_INV_STEP); // hard rounding
return big + MathUtils.jacobianLogTable[ind];
}
@ -591,12 +590,12 @@ public class MathUtils {
}
public static int maxElementIndex(final double[] array, final int endIndex) {
if (array == null)
if (array == null || array.length == 0)
throw new IllegalArgumentException("Array cannot be null!");
int maxI = -1;
for (int i = 0; i < endIndex; i++) {
if (maxI == -1 || array[i] > array[maxI])
int maxI = 0;
for (int i = 1; i < endIndex; i++) {
if (array[i] > array[maxI])
maxI = i;
}
@ -608,12 +607,12 @@ public class MathUtils {
}
public static int maxElementIndex(final int[] array, int endIndex) {
if (array == null)
if (array == null || array.length == 0)
throw new IllegalArgumentException("Array cannot be null!");
int maxI = -1;
for (int i = 0; i < endIndex; i++) {
if (maxI == -1 || array[i] > array[maxI])
int maxI = 0;
for (int i = 1; i < endIndex; i++) {
if (array[i] > array[maxI])
maxI = i;
}
@ -637,12 +636,12 @@ public class MathUtils {
}
public static int minElementIndex(double[] array) {
if (array == null)
if (array == null || array.length == 0)
throw new IllegalArgumentException("Array cannot be null!");
int minI = -1;
for (int i = 0; i < array.length; i++) {
if (minI == -1 || array[i] < array[minI])
int minI = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < array[minI])
minI = i;
}
@ -650,12 +649,12 @@ public class MathUtils {
}
public static int minElementIndex(byte[] array) {
if (array == null)
if (array == null || array.length == 0)
throw new IllegalArgumentException("Array cannot be null!");
int minI = -1;
for (int i = 0; i < array.length; i++) {
if (minI == -1 || array[i] < array[minI])
int minI = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < array[minI])
minI = i;
}
@ -663,12 +662,12 @@ public class MathUtils {
}
public static int minElementIndex(int[] array) {
if (array == null)
if (array == null || array.length == 0)
throw new IllegalArgumentException("Array cannot be null!");
int minI = -1;
for (int i = 0; i < array.length; i++) {
if (minI == -1 || array[i] < array[minI])
int minI = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < array[minI])
minI = i;
}