use of org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor in project gatk-protected by broadinstitute.
the class SomaticGenotypingEngine method getAsRealMatrix.
//convert a likelihood matrix of alleles x reads into a RealMatrix
public static RealMatrix getAsRealMatrix(final LikelihoodMatrix<Allele> matrix) {
final RealMatrix result = new Array2DRowRealMatrix(matrix.numberOfAlleles(), matrix.numberOfReads());
result.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int row, int column, double value) {
return matrix.get(row, column);
}
});
return result;
}
use of org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor in project gatk by broadinstitute.
the class NormalizeSomaticReadCountsIntegrationTest method assertBetaHatsRobustToOutliers.
/**
* Asserts that the calculation of beta hats is not significantly affected by zero-coverage outlier counts
* We perform this check by randomly setting some coverages to zero in copy ratio space (-infinity in log space).
* betaHats imputes 0 in log space (1 in copy ratio space) whenever coverage is below a certain low threshold
* and should thus be robust to this type of noise.
*/
private void assertBetaHatsRobustToOutliers(final ReadCountCollection preTangentNormalized, final File ponFile) {
try (final HDF5File ponReader = new HDF5File(ponFile)) {
final PCACoveragePoN pon = new HDF5PCACoveragePoN(ponReader);
final List<String> ponTargets = pon.getPanelTargetNames();
final RealMatrix input = reorderTargetsToPoNOrder(preTangentNormalized, ponTargets);
// randomly set some entries to zero in copy-ratio space (-infinity in log space)
final Random random = new Random(13);
final double noiseProportion = 0.01;
final RealMatrix noisyInput = input.copy();
noisyInput.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(final int row, final int column, final double value) {
return random.nextDouble() < noiseProportion ? Double.NEGATIVE_INFINITY : value;
}
});
final RealMatrix betaHats = PCATangentNormalizationUtils.calculateBetaHats(pon.getReducedPanelPInverseCounts(), input, PCATangentNormalizationUtils.EPSILON);
final RealMatrix noisyBetaHats = PCATangentNormalizationUtils.calculateBetaHats(pon.getReducedPanelPInverseCounts(), noisyInput, PCATangentNormalizationUtils.EPSILON);
final RealMatrix difference = betaHats.subtract(noisyBetaHats);
difference.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
@Override
public void visit(final int row, int column, double value) {
Assert.assertEquals(value, 0, 0.01);
}
});
}
}
use of org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor in project gatk by broadinstitute.
the class HDF5LibraryUnitTest method createMatrixOfGaussianValues.
private RealMatrix createMatrixOfGaussianValues(int numRows, int numCols, final double mean, final double sigma) {
final RealMatrix bigCounts = new Array2DRowRealMatrix(numRows, numCols);
final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
randomDataGenerator.reSeed(337337337);
bigCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int row, int column, double value) {
return randomDataGenerator.nextGaussian(mean, sigma);
}
});
return bigCounts;
}
use of org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor in project gatk by broadinstitute.
the class GCBiasSimulatedData method simulatedData.
// visible for the integration test
public static Pair<ReadCountCollection, double[]> simulatedData(final int numTargets, final int numSamples) {
final List<Target> phonyTargets = SimulatedTargets.phonyTargets(numTargets);
final List<String> phonySamples = SimulatedSamples.phonySamples(numSamples);
final Random random = new Random(13);
final double[] gcContentByTarget = IntStream.range(0, numTargets).mapToDouble(n -> 0.5 + 0.2 * random.nextGaussian()).map(x -> Math.min(x, 0.95)).map(x -> Math.max(x, 0.05)).toArray();
final double[] gcBiasByTarget = Arrays.stream(gcContentByTarget).map(QUADRATIC_GC_BIAS_CURVE::apply).toArray();
// model mainly GC bias with a small random amount of non-GC bias
// thus noise after GC correction should be nearly zero
final RealMatrix counts = new Array2DRowRealMatrix(numTargets, numSamples);
counts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(final int target, final int column, final double value) {
return gcBiasByTarget[target] * (1.0 + 0.01 * random.nextDouble());
}
});
final ReadCountCollection rcc = new ReadCountCollection(phonyTargets, phonySamples, counts);
return new ImmutablePair<>(rcc, gcContentByTarget);
}
use of org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor in project gatk-protected by broadinstitute.
the class XHMMSegmentCallerBase method standardizeByTarget.
/**
* Standardize read counts (per-target).
* Note: modification is done in-place.
*
* @param counts original read counts
*/
private void standardizeByTarget(final RealMatrix counts) {
final double[] rowMeans = GATKProtectedMathUtils.rowMeans(counts);
final double[] rowStdDev = GATKProtectedMathUtils.rowStdDevs(counts);
counts.walkInColumnOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(final int row, final int column, final double value) {
return (value - rowMeans[row]) / rowStdDev[row];
}
});
}
Aggregations