Skip to content

Commit

Permalink
simplified the iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbenjamin committed Jan 27, 2022
1 parent e166b50 commit e9d52f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ <EVIDENCE, A extends Allele> double[] getReadRawReadLikelihoodsByGenotypeIndex(f
}

final double[] result = new double[genotypeCount];

Utils.stream(iterator()).forEach(alleleCounts -> {
final int componentCount = alleleCounts.distinctAlleleCount();
final int genotypeIndex = alleleCounts.index();
Expand Down Expand Up @@ -217,6 +217,7 @@ private void ensureReadCapacity(final int requestedCapacity) {
}


// note that if the input has a high index that is not cached, it will be mutated in order to form the output
private GenotypeAlleleCounts nextGenotypeAlleleCounts(final GenotypeAlleleCounts alleleCounts) {
final int index = alleleCounts.index();
if (index < (GenotypeLikelihoodCalculators.MAXIMUM_CACHED_GENOTYPES_PER_CALCULATOR - 1)) {
Expand Down Expand Up @@ -290,23 +291,18 @@ public int[] newToOldGenotypeMap(final int[] newToOldAlleleMap) {
@Override
public Iterator<GenotypeAlleleCounts> iterator() {
return new Iterator<GenotypeAlleleCounts>() {
private int genotypeIndex = 0;
private final GenotypeAlleleCounts increasingAlleleCounts = genotypeCount < GenotypeLikelihoodCalculators.MAXIMUM_CACHED_GENOTYPES_PER_CALCULATOR ?
null : genotypeAlleleCounts[GenotypeLikelihoodCalculators.MAXIMUM_CACHED_GENOTYPES_PER_CALCULATOR - 1].copy();
private int index = 0;
private GenotypeAlleleCounts alleleCounts = genotypeAlleleCounts[0];

@Override
public boolean hasNext() {
return genotypeIndex < genotypeCount;
return index < genotypeCount;
}

@Override
public GenotypeAlleleCounts next() {
if (genotypeIndex < GenotypeLikelihoodCalculators.MAXIMUM_CACHED_GENOTYPES_PER_CALCULATOR) {
return genotypeAlleleCounts[genotypeIndex++];
} else {
genotypeIndex++;
return increasingAlleleCounts.increase();
}
alleleCounts = index++ == 0 ? genotypeAlleleCounts[0] : nextGenotypeAlleleCounts(alleleCounts);
return alleleCounts;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testPloidyAndMaximumAllele(final int ploidy, final int alleleCount)
Assert.assertEquals(calculator.alleleCountsToIndex(alleleCountArray), i);
}
}

@Test(dataProvider = "ploidyAndMaximumAlleleAndReadCountsData", dependsOnMethods = "testPloidyAndMaximumAllele")
public void testLikelihoodCalculation(final int ploidy, final int alleleCount, final int[] readCount) {
final AlleleLikelihoods<GATKRead, Allele> readLikelihoods = ReadLikelihoodsUnitTester.readLikelihoods(alleleCount, readCount);
Expand Down

0 comments on commit e9d52f1

Please sign in to comment.