use of org.testng.annotations.DataProvider in project gatk by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method tooManyZerosData.
@DataProvider(name = "tooManyZerosData")
public Object[][] tooManyZerosData() {
final double[] zeroProbabilities = new double[] { .001, .01, .02, 0.1 };
final List<Object[]> result = new ArrayList<>();
final Random rdn = new Random(13);
final int columnCount = 100;
final int targetCount = 100;
final List<String> columnNames = IntStream.range(0, columnCount).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
final List<Target> targets = IntStream.range(0, targetCount).mapToObj(i -> new Target("target_" + (i + 1))).collect(Collectors.toList());
for (final double zeroProbability : zeroProbabilities) {
final double[][] counts = new double[columnCount][targetCount];
for (int i = 0; i < counts.length; i++) {
for (int j = 0; j < counts[0].length; j++) {
counts[i][j] = rdn.nextDouble() <= zeroProbability ? 0.0 : rdn.nextDouble();
}
}
final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames, new Array2DRowRealMatrix(counts, false));
result.add(new Object[] { readCounts });
}
return result.toArray(new Object[result.size()][]);
}
use of org.testng.annotations.DataProvider in project gatk by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testRemoveTargetsWithTooManyZeros.
@Test(dataProvider = "tooManyZerosData")
public void testRemoveTargetsWithTooManyZeros(final ReadCountCollection readCount) {
final RealMatrix counts = readCount.counts();
final int[] numberOfZeros = IntStream.range(0, counts.getRowDimension()).map(i -> (int) DoubleStream.of(counts.getRow(i)).filter(d -> d == 0.0).count()).toArray();
final int maximumNumberOfZeros = IntStream.of(numberOfZeros).max().getAsInt();
for (int maxZeros = 0; maxZeros < maximumNumberOfZeros; maxZeros++) {
final int maxZerosThres = maxZeros;
final int expectedRemainingCount = (int) IntStream.of(numberOfZeros).filter(i -> i <= maxZerosThres).count();
if (expectedRemainingCount == 0) {
try {
ReadCountCollectionUtils.removeTargetsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
} catch (final UserException.BadInput ex) {
// expected.
continue;
}
Assert.fail("expects an exception");
}
final ReadCountCollection rc = ReadCountCollectionUtils.removeTargetsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
Assert.assertEquals(rc.targets().size(), expectedRemainingCount);
int nextIndex = 0;
for (int i = 0; i < readCount.targets().size(); i++) {
final Target target = readCount.targets().get(i);
final int newIndex = rc.targets().indexOf(target);
if (numberOfZeros[i] <= maxZeros) {
Assert.assertTrue(newIndex >= 0, " " + numberOfZeros[i] + " " + maxZeros);
Assert.assertEquals(newIndex, nextIndex++);
} else {
Assert.assertEquals(newIndex, -1);
}
}
Assert.assertEquals(nextIndex, expectedRemainingCount);
}
}
use of org.testng.annotations.DataProvider in project gatk by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testRemoveColumnsWithTooManyZeros.
@Test(dataProvider = "tooManyZerosData")
public void testRemoveColumnsWithTooManyZeros(final ReadCountCollection readCount) {
final RealMatrix counts = readCount.counts();
final int[] numberOfZeros = IntStream.range(0, counts.getColumnDimension()).map(i -> (int) DoubleStream.of(counts.getColumn(i)).filter(d -> d == 0.0).count()).toArray();
final int maximumNumberOfZeros = IntStream.of(numberOfZeros).max().getAsInt();
for (int maxZeros = 0; maxZeros < maximumNumberOfZeros; maxZeros++) {
final int maxZerosThres = maxZeros;
final int expectedRemainingCount = (int) IntStream.of(numberOfZeros).filter(i -> i <= maxZerosThres).count();
if (expectedRemainingCount == 0) {
try {
ReadCountCollectionUtils.removeColumnsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
} catch (final UserException.BadInput ex) {
// expected.
continue;
}
Assert.fail("expects an exception");
}
final ReadCountCollection rc = ReadCountCollectionUtils.removeColumnsWithTooManyZeros(readCount, maxZeros, false, NULL_LOGGER);
Assert.assertEquals(rc.columnNames().size(), expectedRemainingCount);
final int[] newIndices = new int[expectedRemainingCount];
int nextIndex = 0;
for (int i = 0; i < readCount.columnNames().size(); i++) {
final String name = readCount.columnNames().get(i);
final int newIndex = rc.columnNames().indexOf(name);
if (numberOfZeros[i] <= maxZeros) {
Assert.assertTrue(newIndex >= 0);
newIndices[nextIndex++] = i;
} else {
Assert.assertEquals(newIndex, -1);
}
}
Assert.assertEquals(nextIndex, expectedRemainingCount);
for (int i = 1; i < newIndices.length; i++) {
Assert.assertTrue(newIndices[i - 1] < newIndices[i]);
}
}
}
use of org.testng.annotations.DataProvider in project gatk by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testImputeZeroCounts.
@Test(dataProvider = "tooManyZerosData")
public void testImputeZeroCounts(final ReadCountCollection readCounts) {
final Median median = new Median();
final RealMatrix counts = readCounts.counts();
final double[] targetNonZeroMedians = IntStream.range(0, counts.getRowDimension()).mapToDouble(i -> median.evaluate(DoubleStream.of(counts.getRow(i)).filter(d -> d != 0.0).toArray())).toArray();
final double[][] expected = new double[counts.getRowDimension()][];
final double[][] original = counts.getData();
for (int i = 0; i < expected.length; i++) {
final double[] rowCounts = counts.getRow(i).clone();
expected[i] = rowCounts;
for (int j = 0; j < expected[i].length; j++) {
if (expected[i][j] == 0.0) {
expected[i][j] = targetNonZeroMedians[i];
}
}
}
ReadCountCollectionUtils.imputeZeroCountsAsTargetMedians(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], "i,j == " + i + "," + j + " " + original[i][j]);
}
}
}
use of org.testng.annotations.DataProvider in project gatk 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);
}
}
}
Aggregations