Source:
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(); final double[] values = new double[targetCount * columnCount]; for (int i = 0; i < targetCount; i++) { final double[] rowCounts = counts.getRow(i); System.arraycopy(rowCounts, 0, values, i * columnCount, columnCount); } 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 where 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%%), where truncated as they fall out of the non-extreme range [%.2f, %.2f]", topTruncatedCounts + bottomTruncatedCounts, totalCounts, truncatedPercentage, bottomPercentileThreshold, topPercentileThreshold)); } }

Target:
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(); 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 where 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%%), where truncated as they fall out of the non-extreme range [%.2f, %.2f]", topTruncatedCounts + bottomTruncatedCounts, totalCounts, truncatedPercentage, bottomPercentileThreshold, topPercentileThreshold)); } }