use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testReadTargetNameOnlyFormattedFile.
@Test
public void testReadTargetNameOnlyFormattedFile() throws IOException {
final File testFile = createTempFile();
final PrintWriter writer = new PrintWriter(testFile);
writer.println("## comment 1");
writer.println("## comment 2");
writer.println("SAMPLE2\tSAMPLE1\t" + TargetTableColumn.NAME.toString());
writer.println("1.1\t2.2\ttgt_0");
writer.println("-1.1E-7\t-2.2E-8\ttgt_1");
writer.close();
final ReadCountCollection subject = ReadCountCollectionUtils.parse(testFile);
Assert.assertNotNull(subject);
Assert.assertEquals(subject.columnNames(), Arrays.asList("SAMPLE2", "SAMPLE1"));
Assert.assertEquals(subject.targets().stream().map(Target::getName).collect(Collectors.toList()), Arrays.asList("tgt_0", "tgt_1"));
Assert.assertEquals(subject.targets().stream().map(Target::getInterval).collect(Collectors.toList()), Arrays.asList(null, null));
Assert.assertEquals(subject.targets().size(), 2);
final RealMatrix counts = subject.counts();
Assert.assertEquals(counts.getRowDimension(), 2);
Assert.assertEquals(counts.getColumnDimension(), 2);
Assert.assertEquals(counts.getEntry(0, 0), 1.1, 0.0001);
Assert.assertEquals(counts.getEntry(0, 1), 2.2, 0.0001);
Assert.assertEquals(counts.getEntry(1, 0), -1.1E-7, 0.000000001);
Assert.assertEquals(counts.getEntry(1, 1), -2.2E-8, 0.000000001);
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testReadFullFormattedFile.
@Test
public void testReadFullFormattedFile() throws IOException {
final File testFile = createTempFile();
final PrintWriter writer = new PrintWriter(testFile);
writer.println("## comment 1");
writer.println("## comment 2");
writer.println(CONTIG_START_END_NAME + "\tSAMPLE1\tSAMPLE2");
writer.println("1\t100\t200\ttgt_0\t1.1\t2.2");
writer.println("2\t200\t300\ttgt_1\t-1.1E-7\t-2.2E-8");
writer.close();
final ReadCountCollection subject = ReadCountCollectionUtils.parse(testFile);
Assert.assertNotNull(subject);
Assert.assertEquals(subject.columnNames(), Arrays.asList("SAMPLE1", "SAMPLE2"));
Assert.assertEquals(subject.targets().stream().map(Target::getName).collect(Collectors.toList()), Arrays.asList("tgt_0", "tgt_1"));
Assert.assertEquals(subject.targets().stream().map(Target::getInterval).collect(Collectors.toList()), Arrays.asList(new SimpleInterval("1", 100, 200), new SimpleInterval("2", 200, 300)));
Assert.assertEquals(subject.targets().size(), 2);
final RealMatrix counts = subject.counts();
Assert.assertEquals(counts.getRowDimension(), 2);
Assert.assertEquals(counts.getColumnDimension(), 2);
Assert.assertEquals(counts.getEntry(0, 0), 1.1, 0.0001);
Assert.assertEquals(counts.getEntry(0, 1), 2.2, 0.0001);
Assert.assertEquals(counts.getEntry(1, 0), -1.1E-7, 0.000000001);
Assert.assertEquals(counts.getEntry(1, 1), -2.2E-8, 0.000000001);
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testExtremeMedianColumnsData.
@Test(dataProvider = "readCountAndPercentileData")
public void testExtremeMedianColumnsData(final ReadCountCollection readCount, final double percentile) {
final Median median = new Median();
final RealMatrix counts = readCount.counts();
final double[] columnMedians = IntStream.range(0, counts.getColumnDimension()).mapToDouble(i -> median.evaluate(counts.getColumn(i))).toArray();
final double top = new Percentile(100 - percentile).evaluate(columnMedians);
final double bottom = new Percentile(percentile).evaluate(columnMedians);
final Boolean[] toBeKept = DoubleStream.of(columnMedians).mapToObj(d -> d <= top && d >= bottom).toArray(Boolean[]::new);
final int toBeKeptCount = (int) Stream.of(toBeKept).filter(b -> b).count();
final ReadCountCollection result = ReadCountCollectionUtils.removeColumnsWithExtremeMedianCounts(readCount, percentile, NULL_LOGGER);
Assert.assertEquals(result.columnNames().size(), toBeKeptCount);
int nextIndex = 0;
for (int i = 0; i < toBeKept.length; i++) {
if (toBeKept[i]) {
int index = result.columnNames().indexOf(readCount.columnNames().get(i));
Assert.assertEquals(index, nextIndex++);
Assert.assertEquals(counts.getColumn(i), result.counts().getColumn(index));
} else {
Assert.assertEquals(result.columnNames().indexOf(readCount.columnNames().get(i)), -1);
}
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class GCCorrector method correctCoverage.
/**
*
* @param inputCounts raw coverage before GC correction
* @param gcContentByTarget array of gc contents, one per target of the input
* @return GC-corrected coverage
*/
public static ReadCountCollection correctCoverage(final ReadCountCollection inputCounts, final double[] gcContentByTarget) {
// each column (sample) has its own GC bias curve, hence its own GC corrector
final List<GCCorrector> gcCorrectors = IntStream.range(0, inputCounts.columnNames().size()).mapToObj(n -> new GCCorrector(gcContentByTarget, inputCounts.counts().getColumnVector(n))).collect(Collectors.toList());
// gc correct a copy of the input counts in-place
final RealMatrix correctedCounts = inputCounts.counts().copy();
correctedCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int target, int column, double coverage) {
return gcCorrectors.get(column).correctedCoverage(coverage, gcContentByTarget[target]);
}
});
// we would like the average correction factor to be 1.0 in the sense that average coverage before and after
// correction should be equal
final double[] columnNormalizationFactors = IntStream.range(0, inputCounts.columnNames().size()).mapToDouble(c -> inputCounts.counts().getColumnVector(c).getL1Norm() / correctedCounts.getColumnVector(c).getL1Norm()).toArray();
correctedCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int target, int column, double coverage) {
return coverage * columnNormalizationFactors[column];
}
});
return new ReadCountCollection(inputCounts.targets(), inputCounts.columnNames(), correctedCounts);
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class CNLOHCaller method sumOverSegments.
private double[] sumOverSegments(final JavaRDD<double[]> eZskBySeg) {
// S x K
final double[][] eZskBySeg2D = eZskBySeg.collect().stream().toArray(double[][]::new);
final RealMatrix eZskBySegMatrix = MatrixUtils.createRealMatrix(eZskBySeg2D);
return GATKProtectedMathUtils.columnSums(eZskBySegMatrix);
}
Aggregations