use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class ReadCountCollectionUtils method removeTargetsWithTooManyZeros.
/**
* Remove targets that have too many counts equal to 0.
* <p>
* It will return a copy of the input read-count collection with such targets dropped.
* </p>
*
* @param readCounts the input read counts.
* @param maximumTargetZeros maximum number of counts equal to 0 per target tolerated.
* @return never {@code null}. It might be a reference to the input read-counts if there is
* is no target to be dropped.
*/
public static ReadCountCollection removeTargetsWithTooManyZeros(final ReadCountCollection readCounts, final int maximumTargetZeros, final boolean roundToInteger, final Logger logger) {
final RealMatrix counts = readCounts.counts();
final Set<Target> targetsToKeep = IntStream.range(0, counts.getRowDimension()).boxed().filter(i -> countZeroes(counts.getRow(i), roundToInteger) <= maximumTargetZeros).map(i -> readCounts.targets().get(i)).collect(Collectors.toCollection(LinkedHashSet::new));
final int targetsToDropCount = readCounts.targets().size() - targetsToKeep.size();
if (targetsToDropCount == 0) {
logger.info(String.format("There are no targets with large number of columns with zero counts (<= %d of %d) to drop", maximumTargetZeros, readCounts.columnNames().size()));
return readCounts;
} else if (targetsToDropCount == readCounts.targets().size()) {
throw new UserException.BadInput("the number of zeros per target in the input is too large resulting " + "in all targets being dropped");
} else {
final double droppedPercentage = ((double) (targetsToDropCount) / readCounts.targets().size()) * 100;
logger.info(String.format("Some targets dropped (%d out of %d, %.2f%%) as they had too many zeros (> %d of %d).", targetsToDropCount, readCounts.targets().size(), droppedPercentage, maximumTargetZeros, readCounts.columnNames().size()));
return readCounts.subsetTargets(targetsToKeep);
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class ReadCountCollectionUtils method truncateExtremeCounts.
/**
* Truncates the extreme count values in the input read-count collection.
* Values are forced to be bound by the percentile indicated with the input {@code percentile} which must be
* in the range [0 .. 50.0]. Values under that percentile and the complementary (1 - percentile) are set to the
* corresponding threshold value.
*
* <p>The imputation is done in-place, thus the input matrix is modified as a result of this call.</p>
*
* @param readCounts the input and output read-count matrix.
*/
public static void truncateExtremeCounts(final ReadCountCollection readCounts, final double percentile, final Logger logger) {
final RealMatrix counts = readCounts.counts();
final int targetCount = counts.getRowDimension();
final int columnCount = counts.getColumnDimension();
// Create a row major array of the counts.
final double[] values = Doubles.concat(counts.getData());
final Percentile bottomPercentileEvaluator = new Percentile(percentile);
final Percentile topPercentileEvaluator = new Percentile(100.0 - percentile);
final double bottomPercentileThreshold = bottomPercentileEvaluator.evaluate(values);
final double topPercentileThreshold = topPercentileEvaluator.evaluate(values);
long totalCounts = 0;
long bottomTruncatedCounts = 0;
long topTruncatedCounts = 0;
for (int i = 0; i < targetCount; i++) {
final double[] rowCounts = counts.getRow(i);
for (int j = 0; j < columnCount; j++) {
final double count = rowCounts[j];
totalCounts++;
if (count < bottomPercentileThreshold) {
counts.setEntry(i, j, bottomPercentileThreshold);
bottomTruncatedCounts++;
} else if (count > topPercentileThreshold) {
counts.setEntry(i, j, topPercentileThreshold);
topTruncatedCounts++;
}
}
}
if (topTruncatedCounts == 0 && bottomTruncatedCounts == 0) {
logger.info(String.format("None of the %d counts were truncated as they all fall in the non-extreme range " + "[%.2f, %.2f]", totalCounts, bottomPercentileThreshold, topPercentileThreshold));
} else {
final double truncatedPercentage = ((double) (topTruncatedCounts + bottomTruncatedCounts) / totalCounts) * 100;
logger.info(String.format("Some counts (%d out of %d, %.2f%%) were truncated as they fall out of the " + "non-extreme range [%.2f, %.2f]", topTruncatedCounts + bottomTruncatedCounts, totalCounts, truncatedPercentage, bottomPercentileThreshold, topPercentileThreshold));
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class ReadCountCollectionUtils method performWriting.
private static void performWriting(ReadCountCollection collection, TableWriter<ReadCountRecord> tableWriter, String[] headerComments) throws IOException {
// print the header comments
for (final String comment : headerComments) {
tableWriter.writeComment(comment);
}
final List<Target> targets = collection.targets();
final RealMatrix counts = collection.counts();
for (int i = 0; i < targets.size(); i++) {
tableWriter.writeRecord(new ReadCountRecord(targets.get(i), counts.getRow(i)));
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class Nd4jApacheAdapterUtils method convertINDArrayToApacheMatrix.
/**
* INDArray to Apache
*
* @param matrix rank-2 INDArray
* @return Apache matrix
*/
public static RealMatrix convertINDArrayToApacheMatrix(@Nonnull final INDArray matrix) {
Utils.validateArg(matrix.rank() == 2, "Input rank is not 2 (not matrix)");
final int[] shape = matrix.shape();
final INDArray concreteMatrix = matrix.isView() ? matrix.dup() : matrix;
final double[] data = concreteMatrix.data().asDouble();
final char ordering = concreteMatrix.ordering();
if (ordering == 'c') {
return new BlockRealMatrix(monoToBiDiArrayRowMajor(data, shape[0], shape[1]));
} else {
/* ordering == 'f' */
return new BlockRealMatrix(monoToBiDiArrayColumnMajor(data, shape[0], shape[1]));
}
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class ReadCountCollectionUtils method removeColumnsWithTooManyZeros.
/**
* Remove columns that have too many counts equal to 0.
* <p>
* It will return a copy of the input read-count collection with such columns dropped.
* </p>
*
* @param readCounts the input read counts.
* @param maximumColumnZeros maximum number of counts equal to 0 per column tolerated.
* @return never {@code null}. It might be a reference to the input read-counts if there is
* is no column to be dropped.
*/
@VisibleForTesting
public static ReadCountCollection removeColumnsWithTooManyZeros(final ReadCountCollection readCounts, final int maximumColumnZeros, final boolean roundToInteger, final Logger logger) {
final RealMatrix counts = readCounts.counts();
final Set<String> columnsToKeep = IntStream.range(0, counts.getColumnDimension()).boxed().filter(i -> countZeroes(counts.getColumn(i), roundToInteger) <= maximumColumnZeros).map(i -> readCounts.columnNames().get(i)).collect(Collectors.toCollection(LinkedHashSet::new));
final int columnsToDropCount = readCounts.columnNames().size() - columnsToKeep.size();
if (columnsToDropCount == 0) {
logger.info(String.format("There were no columns with a large number of targets with zero counts " + "(<= %d of %d) to drop", maximumColumnZeros, readCounts.targets().size()));
return readCounts;
} else if (columnsToDropCount == readCounts.columnNames().size()) {
throw new UserException.BadInput("The number of zeros per count column is too large resulting in all count " + "columns to be dropped");
} else {
final double droppedPercentage = ((double) (columnsToDropCount) / readCounts.columnNames().size()) * 100;
logger.info(String.format("Some counts columns dropped (%d out of %d, %.2f%%) as they had too many targets with zeros (> %d of %d)", columnsToDropCount, readCounts.columnNames().size(), droppedPercentage, maximumColumnZeros, readCounts.targets().size()));
return readCounts.subsetColumns(columnsToKeep);
}
}
Aggregations