use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class DecomposeSingularValuesIntegrationTest method assertOutputFile.
private void assertOutputFile(final File outputFile, final int gtNumRows, final int gtNumCols) {
Assert.assertTrue(outputFile.exists(), "Output file does not exist.");
Assert.assertTrue(outputFile.length() > 0, "Output file is empty: " + outputFile.getAbsolutePath());
final RealMatrix actualResults = PoNTestUtils.readTsvIntoMatrix(outputFile);
Assert.assertEquals(actualResults.getRowDimension(), gtNumRows);
Assert.assertEquals(actualResults.getColumnDimension(), gtNumCols);
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class NormalizeSomaticReadCountsIntegrationTest method assertTangentNormalized.
private void assertTangentNormalized(final ReadCountCollection actualReadCounts, final ReadCountCollection preTangentNormalized, final RealMatrix betaHats, final File ponFile) {
try (final HDF5File ponReader = new HDF5File(ponFile)) {
final PCACoveragePoN pon = new HDF5PCACoveragePoN(ponReader);
final RealMatrix inCounts = reorderTargetsToPoNOrder(preTangentNormalized, pon.getPanelTargetNames());
final RealMatrix actual = reorderTargetsToPoNOrder(actualReadCounts, pon.getPanelTargetNames());
final RealMatrix ponMat = pon.getReducedPanelCounts();
final RealMatrix projection = ponMat.multiply(betaHats);
final RealMatrix expected = inCounts.subtract(projection);
Assert.assertEquals(actual.getRowDimension(), expected.getRowDimension());
Assert.assertEquals(actual.getColumnDimension(), expected.getColumnDimension());
for (int i = 0; i < actual.getRowDimension(); i++) {
Assert.assertEquals(actual.getRow(i), expected.getRow(i));
}
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class NormalizeSomaticReadCountsIntegrationTest method assertBetaHats.
/**
* Asserts that a collection of beta-hats corresponds to the expected value given
* the input pre-tangent normalization matrix and the PoN file.
*/
private void assertBetaHats(final ReadCountCollection preTangentNormalized, final RealMatrix actual, final File ponFile) {
Assert.assertEquals(actual.getColumnDimension(), preTangentNormalized.columnNames().size());
final double epsilon = PCATangentNormalizationUtils.EPSILON;
try (final HDF5File ponReader = new HDF5File(ponFile)) {
final PCACoveragePoN pon = new HDF5PCACoveragePoN(ponReader);
final List<String> ponTargets = pon.getPanelTargetNames();
final RealMatrix inCounts = reorderTargetsToPoNOrder(preTangentNormalized, ponTargets);
// obtain subset of relevant targets to calculate the beta-hats;
final int[][] betaHatTargets = new int[inCounts.getColumnDimension()][];
for (int i = 0; i < inCounts.getColumnDimension(); i++) {
final List<Integer> relevantTargets = new ArrayList<>();
for (int j = 0; j < inCounts.getRowDimension(); j++) {
if (inCounts.getEntry(j, i) > 1 + (Math.log(epsilon) / Math.log(2))) {
relevantTargets.add(j);
}
}
betaHatTargets[i] = relevantTargets.stream().mapToInt(Integer::intValue).toArray();
}
// calculate beta-hats per column and check with actual values.
final RealMatrix normalsInv = pon.getReducedPanelPInverseCounts();
Assert.assertEquals(actual.getRowDimension(), normalsInv.getRowDimension());
final RealMatrix normalsInvT = normalsInv.transpose();
for (int i = 0; i < inCounts.getColumnDimension(); i++) {
final RealMatrix inValues = inCounts.getColumnMatrix(i).transpose().getSubMatrix(new int[] { 0 }, betaHatTargets[i]);
final RealMatrix normalValues = normalsInvT.getSubMatrix(betaHatTargets[i], IntStream.range(0, normalsInvT.getColumnDimension()).toArray());
final RealMatrix betaHats = inValues.multiply(normalValues);
for (int j = 0; j < actual.getRowDimension(); j++) {
Assert.assertEquals(actual.getEntry(j, i), betaHats.getEntry(0, j), 0.000001, "Col " + i + " row " + j);
}
}
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class HDF5PCACoveragePoNCreationUtilsUnitTest method testSubtractMedianOfMedians.
@Test(dataProvider = "readCountOnlyData")
public void testSubtractMedianOfMedians(final ReadCountCollection readCounts) {
final RealMatrix counts = readCounts.counts();
final Median median = new Median();
final double[] columnMedians = IntStream.range(0, counts.getColumnDimension()).mapToDouble(i -> median.evaluate(counts.getColumn(i))).toArray();
final double center = median.evaluate(columnMedians);
final double[][] expected = new double[counts.getRowDimension()][];
for (int i = 0; i < expected.length; i++) {
expected[i] = counts.getRow(i).clone();
for (int j = 0; j < expected[i].length; j++) {
expected[i][j] -= center;
}
}
HDF5PCACoveragePoNCreationUtils.subtractMedianOfMedians(readCounts, NULL_LOGGER);
final RealMatrix newCounts = readCounts.counts();
Assert.assertEquals(newCounts.getColumnDimension(), expected[0].length);
Assert.assertEquals(newCounts.getRowDimension(), expected.length);
for (int i = 0; i < expected.length; i++) {
for (int j = 0; j < expected[i].length; j++) {
Assert.assertEquals(newCounts.getEntry(i, j), expected[i][j], 0.000001);
}
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class HDF5PCACoveragePoNUnitTest method testReducedPoNMatrixReading.
@Test(dependsOnMethods = { "testTargetNameReading", "testLogNormalizedSampleNameReading" })
public void testReducedPoNMatrixReading() throws IOException {
final HDF5File reader = new HDF5File(TEST_PON);
final PCACoveragePoN pon = new HDF5PCACoveragePoN(reader);
final List<String> targets = pon.getTargetNames();
final List<String> samples = pon.getPanelSampleNames();
final RealMatrix actual = pon.getReducedPanelCounts();
Assert.assertNotNull(actual);
Assert.assertEquals(actual.getRowDimension(), targets.size());
Assert.assertTrue(actual.getColumnDimension() <= samples.size());
final RealMatrix expected = readDoubleMatrix(TEST_PON_REDUCED_PON);
MathObjectAsserts.assertRealMatrixEquals(actual, expected);
}
Aggregations