Skip to content

Commit

Permalink
Fix for issue #565 ArrayIndexOutOfBoundsException in FrequencyData.ma…
Browse files Browse the repository at this point in the history
…xFrequency()
  • Loading branch information
julesjacobsen committed Sep 2, 2024
1 parent ffb2da1 commit e613e84
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public float maxFreq() {
@JsonIgnore
@Nullable
public Frequency maxFrequency() {
return this.size == 0 ? null : frequency(maxSource);
return maxSource == -1 ? null : frequency(maxSource);
}

/**
Expand Down Expand Up @@ -454,10 +454,11 @@ public Builder addFrequency(FrequencySource frequencySource, int ac, int an, int
*/
private Builder addFrequency(FrequencySource frequencySource, float frequency, int ac, int an, int homCount) {
frequencySources[frequencySource.ordinal()] = frequencySource;
frequencyData[frequencySource.ordinal() * WORD_SIZE + FREQ_OFFSET] = frequency;
frequencyData[frequencySource.ordinal() * WORD_SIZE + AC_OFFSET] = ac;
frequencyData[frequencySource.ordinal() * WORD_SIZE + AN_OFFSET] = an;
frequencyData[frequencySource.ordinal() * WORD_SIZE + HOM_OFFSET] = homCount;
int freqIndex = frequencySource.ordinal() * WORD_SIZE;
frequencyData[freqIndex + FREQ_OFFSET] = frequency;
frequencyData[freqIndex + AC_OFFSET] = ac;
frequencyData[freqIndex + AN_OFFSET] = an;
frequencyData[freqIndex + HOM_OFFSET] = homCount;
return this;
}

Expand Down Expand Up @@ -517,10 +518,12 @@ public FrequencyData build() {
FrequencySource source = values[i];
if (frequencySources[source.ordinal()] != null) {
sources[dataPos] = source;
data[dataPos * WORD_SIZE + FREQ_OFFSET] = frequencyData[source.ordinal() * WORD_SIZE + FREQ_OFFSET];
data[dataPos * WORD_SIZE + AC_OFFSET] = frequencyData[source.ordinal() * WORD_SIZE + AC_OFFSET];
data[dataPos * WORD_SIZE + AN_OFFSET] = frequencyData[source.ordinal() * WORD_SIZE + AN_OFFSET];
data[dataPos * WORD_SIZE + HOM_OFFSET] = frequencyData[source.ordinal() * WORD_SIZE + HOM_OFFSET];
int dataIndex = dataPos * WORD_SIZE;
int freqIndex = source.ordinal() * WORD_SIZE;
data[dataIndex + FREQ_OFFSET] = frequencyData[freqIndex + FREQ_OFFSET];
data[dataIndex + AC_OFFSET] = frequencyData[freqIndex + AC_OFFSET];
data[dataIndex + AN_OFFSET] = frequencyData[freqIndex + AN_OFFSET];
data[dataIndex + HOM_OFFSET] = frequencyData[freqIndex + HOM_OFFSET];
dataPos++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@

package org.monarchinitiative.exomiser.core.model.frequency;

import org.h2.mvstore.MVStore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.monarchinitiative.exomiser.core.genome.dao.serialisers.MvStoreUtil;
import org.monarchinitiative.exomiser.core.model.AlleleProtoAdaptor;
import org.monarchinitiative.exomiser.core.proto.AlleleProto;
import org.monarchinitiative.exomiser.core.proto.AlleleProtoFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -287,6 +290,17 @@ public void testGetMaxFrequencyWithData() {
assertThat(instance.maxFrequency(), equalTo(maxFrequency));
}

@Test
public void testGetMaxFrequencyWhenZeroData() {
Frequency maxFrequency = Frequency.of(GNOMAD_E_AFR, 0, 1000, 0);
Frequency minFrequency = Frequency.of(TOPMED, 0f);
FrequencyData instance = FrequencyData.of("rs545662810", minFrequency, maxFrequency);

assertThat(instance.maxFrequency(), equalTo(null));
assertThat(instance.maxFreq(), equalTo(0f));
assertThat(instance.frequencies(), equalTo(List.of(minFrequency, maxFrequency)));
}

@Test
public void testHasFrequencyOverPercentageValue() {
float maxFreq = 0.05f;
Expand Down Expand Up @@ -482,12 +496,12 @@ void floatArray() {
var an = 10000;
var hom = 10;
var af = Frequency.percentageFrequency(ac, an);
System.out.println("AF=" + af);

data[0] = (float) ac;
data[1] = (float) an;
data[2] = (float) hom;
data[3] = (float) af;
System.out.println("dataAF=" + data[3]);

assertThat(Frequency.percentageFrequency((int) data[0], (int) data[1]), equalTo((float) data[3]));
}

Expand Down

0 comments on commit e613e84

Please sign in to comment.