use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class GermlineCNVCallerIntegrationTest method reportCopyNumberSummaryStatistics.
/* Shame on me for using {@link ReadCountCollection} to store copy numbers! */
private void reportCopyNumberSummaryStatistics(@Nonnull final File posteriorsOutputPath, @Nonnull final File truthCopyNumberFile, @Nonnull final List<Target> targets, @Nonnull final SexGenotypeDataCollection sexGenotypeDataCollection) {
final ReadCountCollection truthCopyNumberCollection = loadTruthCopyNumberTable(truthCopyNumberFile, targets);
final RealMatrix calledCopyNumberMatrix = Nd4jApacheAdapterUtils.convertINDArrayToApacheMatrix(Nd4jIOUtils.readNDArrayMatrixFromTextFile(new File(posteriorsOutputPath, CoverageModelGlobalConstants.COPY_RATIO_VITERBI_FILENAME)));
final ReadCountCollection calledCopyNumberCollection = new ReadCountCollection(targets, truthCopyNumberCollection.columnNames(), calledCopyNumberMatrix);
final int numSamples = calledCopyNumberCollection.columnNames().size();
final List<String> sampleSexGenotypes = truthCopyNumberCollection.columnNames().stream().map(sampleName -> sexGenotypeDataCollection.getSampleSexGenotypeData(sampleName).getSexGenotype()).collect(Collectors.toList());
final List<SampleCopyNumberSummaryStatistics> sampleSummaryStatisticsList = IntStream.range(0, numSamples).mapToObj(si -> calculateSampleCopyNumberConcordance(truthCopyNumberCollection, calledCopyNumberCollection, si, sampleSexGenotypes.get(si))).collect(Collectors.toList());
/* calculation various summary statistics */
final AbstractUnivariateStatistic calculator = new Mean();
final ConfusionRates homDelMedianRates = ConfusionMatrix.getConfusionRates(sampleSummaryStatisticsList.stream().map(ss -> ss.homozygousDeletionConfusionMatrix).collect(Collectors.toList()), calculator);
final ConfusionRates hetDelMedianRates = ConfusionMatrix.getConfusionRates(sampleSummaryStatisticsList.stream().map(ss -> ss.heterozygousDeletionConfusionMatrix).collect(Collectors.toList()), calculator);
final ConfusionRates dupMedianRates = ConfusionMatrix.getConfusionRates(sampleSummaryStatisticsList.stream().map(ss -> ss.duplicationConfusionMatrix).collect(Collectors.toList()), calculator);
final double absoluteConcordance = Concordance.getCollectionConcordance(sampleSummaryStatisticsList.stream().map(ss -> ss.absoluteCopyNumberConcordance).collect(Collectors.toList()), calculator);
/* log */
logger.info("Homozygous deletion statistics: " + homDelMedianRates.toString());
logger.info("Heterozygous deletion statistics: " + hetDelMedianRates.toString());
logger.info("Duplication statistics: " + dupMedianRates.toString());
logger.info(String.format("Absolute copy number calling concordance: %f", absoluteConcordance));
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk by broadinstitute.
the class GCBiasSimulatedData method simulatedData.
// visible for the integration test
public static Pair<ReadCountCollection, double[]> simulatedData(final int numTargets, final int numSamples) {
final List<Target> phonyTargets = SimulatedTargets.phonyTargets(numTargets);
final List<String> phonySamples = SimulatedSamples.phonySamples(numSamples);
final Random random = new Random(13);
final double[] gcContentByTarget = IntStream.range(0, numTargets).mapToDouble(n -> 0.5 + 0.2 * random.nextGaussian()).map(x -> Math.min(x, 0.95)).map(x -> Math.max(x, 0.05)).toArray();
final double[] gcBiasByTarget = Arrays.stream(gcContentByTarget).map(QUADRATIC_GC_BIAS_CURVE::apply).toArray();
// model mainly GC bias with a small random amount of non-GC bias
// thus noise after GC correction should be nearly zero
final RealMatrix counts = new Array2DRowRealMatrix(numTargets, numSamples);
counts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(final int target, final int column, final double value) {
return gcBiasByTarget[target] * (1.0 + 0.01 * random.nextDouble());
}
});
final ReadCountCollection rcc = new ReadCountCollection(phonyTargets, phonySamples, counts);
return new ImmutablePair<>(rcc, gcContentByTarget);
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected 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-protected 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-protected 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);
}
}
Aggregations