use of org.broadinstitute.hellbender.utils.IndexRange in project gatk by broadinstitute.
the class HashedListTargetCollectionUnitTest method testIndexRange.
@Test(dependsOnMethods = { "testCorrectInitialization" }, dataProvider = "indexRangeData")
public void testIndexRange(final SimpleInterval region, final IndexRange expected) {
final IndexRange range = targetDB.indexRange(region);
Assert.assertEquals(range, expected);
Assert.assertEquals(targetDB.targetCount(region), range.size());
}
use of org.broadinstitute.hellbender.utils.IndexRange in project gatk-protected by broadinstitute.
the class CoverageModelEMWorkspace method getViterbiAsNDArray.
/**
* Fetches the Viterbi copy ratio (or copy number) states as a target-sample matrix
*
* @return an {@link INDArray}
*/
protected INDArray getViterbiAsNDArray(final List<List<HiddenStateSegmentRecord<STATE, Target>>> segments) {
final INDArray res = Nd4j.create(numSamples, numTargets);
final TargetCollection<Target> targetCollection = new HashedListTargetCollection<>(processedTargetList);
for (int si = 0; si < numSamples; si++) {
final SexGenotypeData sampleSexGenotype = processedSampleSexGenotypeData.get(si);
/* start with all ref */
final double[] calls = IntStream.range(0, numTargets).mapToDouble(ti -> referenceStateFactory.apply(sampleSexGenotype, processedTargetList.get(ti)).getScalar()).toArray();
/* go through segments and mutate ref calls as necessary */
segments.get(si).forEach(seg -> {
final IndexRange range = targetCollection.indexRange(seg.getSegment());
final double copyRatio = seg.getSegment().getCall().getScalar();
for (int ti = range.from; ti < range.to; ti++) {
calls[ti] = copyRatio;
}
});
res.get(NDArrayIndex.point(si), NDArrayIndex.all()).assign(Nd4j.create(calls, new int[] { 1, numTargets }));
}
return res.transpose();
}
use of org.broadinstitute.hellbender.utils.IndexRange in project gatk by broadinstitute.
the class AlleleFrequencyCalculator method effectiveAlleleCounts.
// effectiveAlleleCounts[allele a] = SUM_{genotypes g} (posterior_probability(g) * num_copies of a in g), which we denote as SUM [n_g p_g]
// for numerical stability we will do this in log space:
// count = SUM 10^(log (n_g p_g)) = SUM 10^(log n_g + log p_g)
// thanks to the log-sum-exp trick this lets us work with log posteriors alone
private double[] effectiveAlleleCounts(final VariantContext vc, final double[] log10AlleleFrequencies) {
final int numAlleles = vc.getNAlleles();
Utils.validateArg(numAlleles == log10AlleleFrequencies.length, "number of alleles inconsistent");
final double[] log10Result = new double[numAlleles];
Arrays.fill(log10Result, Double.NEGATIVE_INFINITY);
for (final Genotype g : vc.getGenotypes()) {
if (!g.hasLikelihoods()) {
continue;
}
final GenotypeLikelihoodCalculator glCalc = GL_CALCS.getInstance(g.getPloidy(), numAlleles);
final double[] log10GenotypePosteriors = log10NormalizedGenotypePosteriors(g, glCalc, log10AlleleFrequencies);
new IndexRange(0, glCalc.genotypeCount()).forEach(genotypeIndex -> glCalc.genotypeAlleleCountsAt(genotypeIndex).forEachAlleleIndexAndCount((alleleIndex, count) -> log10Result[alleleIndex] = MathUtils.log10SumLog10(log10Result[alleleIndex], log10GenotypePosteriors[genotypeIndex] + MathUtils.log10(count))));
}
return MathUtils.applyToArrayInPlace(log10Result, x -> Math.pow(10.0, x));
}
use of org.broadinstitute.hellbender.utils.IndexRange in project gatk-protected by broadinstitute.
the class MixingFractionUnitTest method testIO.
@Test
public void testIO() throws IOException {
Utils.resetRandomGenerator();
final File file = File.createTempFile("mixing_fractions", ".table");
final String sample1 = "SAMPLE1";
final double fraction1 = 0.15;
final String sample2 = "SAMPLE2";
final double fraction2 = 0.17;
final List<MixingFraction> original = Arrays.asList(new MixingFraction(sample1, fraction1), new MixingFraction(sample2, fraction2));
MixingFraction.writeMixingFractions(original, file);
final List<MixingFraction> copy = MixingFraction.readMixingFractions(file);
Assert.assertEquals(original.size(), copy.size());
new IndexRange(0, original.size()).forEach(n -> {
Assert.assertEquals(original.get(n).getSample(), copy.get(n).getSample());
Assert.assertEquals(original.get(n).getMixingFraction(), copy.get(n).getMixingFraction());
});
}
use of org.broadinstitute.hellbender.utils.IndexRange in project gatk by broadinstitute.
the class RecalDatum method bayesianEstimateOfEmpiricalQuality.
public static double bayesianEstimateOfEmpiricalQuality(final long nObservations, final long nErrors, final double QReported) {
final int numBins = (QualityUtils.MAX_REASONABLE_Q_SCORE + 1) * (int) RESOLUTION_BINS_PER_QUAL;
final double[] log10Posteriors = new IndexRange(0, numBins).mapToDouble(bin -> {
final double QEmpOfBin = bin / RESOLUTION_BINS_PER_QUAL;
return log10QempPrior(QEmpOfBin, QReported) + log10QempLikelihood(QEmpOfBin, nObservations, nErrors);
});
final int MLEbin = MathUtils.maxElementIndex(log10Posteriors);
return MLEbin / RESOLUTION_BINS_PER_QUAL;
}
Aggregations