use of org.apache.commons.math3.random.RandomGenerator in project gatk-protected by broadinstitute.
the class CoverageDropoutDetectorTest method getUnivariateGaussianTargetsWithDropout.
private Object[][] getUnivariateGaussianTargetsWithDropout(final double sigma, final double dropoutRate) {
Random rng = new Random(337);
final RandomGenerator randomGenerator = RandomGeneratorFactory.createRandomGenerator(rng);
NormalDistribution n = new NormalDistribution(randomGenerator, 1, sigma);
final int numDataPoints = 10000;
final int numEventPoints = 2000;
// Randomly select dropoutRate of targets and reduce by 25%-75% (uniformly distributed)
UniformRealDistribution uniformRealDistribution = new UniformRealDistribution(randomGenerator, 0, 1.0);
final List<ReadCountRecord.SingleSampleRecord> targetList = new ArrayList<>();
for (int i = 0; i < numDataPoints; i++) {
double coverage = n.sample() + (i < (numDataPoints - numEventPoints) ? 0.0 : 0.5);
if (uniformRealDistribution.sample() < dropoutRate) {
double multiplier = .25 + uniformRealDistribution.sample() / 2;
coverage = coverage * multiplier;
}
targetList.add(new ReadCountRecord.SingleSampleRecord(new Target("arbitrary_name", new SimpleInterval("chr1", 100 + 2 * i, 101 + 2 * i)), coverage));
}
HashedListTargetCollection<ReadCountRecord.SingleSampleRecord> targets = new HashedListTargetCollection<>(targetList);
List<ModeledSegment> segments = new ArrayList<>();
segments.add(new ModeledSegment(new SimpleInterval("chr1", 100, 16050), 8000, 1));
segments.add(new ModeledSegment(new SimpleInterval("chr1", 16100, 20200), 2000, 1.5));
return new Object[][] { { targets, segments } };
}
use of org.apache.commons.math3.random.RandomGenerator in project gatk by broadinstitute.
the class CoverageDropoutDetectorTest method getUnivariateGaussianTargetsWithDropout.
private Object[][] getUnivariateGaussianTargetsWithDropout(final double sigma, final double dropoutRate) {
Random rng = new Random(337);
final RandomGenerator randomGenerator = RandomGeneratorFactory.createRandomGenerator(rng);
NormalDistribution n = new NormalDistribution(randomGenerator, 1, sigma);
final int numDataPoints = 10000;
final int numEventPoints = 2000;
// Randomly select dropoutRate of targets and reduce by 25%-75% (uniformly distributed)
UniformRealDistribution uniformRealDistribution = new UniformRealDistribution(randomGenerator, 0, 1.0);
final List<ReadCountRecord.SingleSampleRecord> targetList = new ArrayList<>();
for (int i = 0; i < numDataPoints; i++) {
double coverage = n.sample() + (i < (numDataPoints - numEventPoints) ? 0.0 : 0.5);
if (uniformRealDistribution.sample() < dropoutRate) {
double multiplier = .25 + uniformRealDistribution.sample() / 2;
coverage = coverage * multiplier;
}
targetList.add(new ReadCountRecord.SingleSampleRecord(new Target("arbitrary_name", new SimpleInterval("chr1", 100 + 2 * i, 101 + 2 * i)), coverage));
}
HashedListTargetCollection<ReadCountRecord.SingleSampleRecord> targets = new HashedListTargetCollection<>(targetList);
List<ModeledSegment> segments = new ArrayList<>();
segments.add(new ModeledSegment(new SimpleInterval("chr1", 100, 16050), 8000, 1));
segments.add(new ModeledSegment(new SimpleInterval("chr1", 16100, 20200), 2000, 1.5));
return new Object[][] { { targets, segments } };
}
use of org.apache.commons.math3.random.RandomGenerator in project gatk by broadinstitute.
the class AlleleFractionSegmenterUnitTest method generateCounts.
//visible for testing joint segmentation
protected static AllelicCountCollection generateCounts(final List<Double> minorAlleleFractionSequence, final List<SimpleInterval> positions, final RandomGenerator rng, final AlleleFractionGlobalParameters trueParams) {
//translate to ApacheCommons' parametrization of the gamma distribution
final GammaDistribution biasGenerator = getGammaDistribution(trueParams, rng);
final double outlierProbability = trueParams.getOutlierProbability();
final AllelicCountCollection counts = new AllelicCountCollection();
for (int n = 0; n < minorAlleleFractionSequence.size(); n++) {
counts.add(generateAllelicCount(minorAlleleFractionSequence.get(n), positions.get(n), rng, biasGenerator, outlierProbability));
}
return counts;
}
use of org.apache.commons.math3.random.RandomGenerator in project gatk by broadinstitute.
the class AlleleFractionSegmenterUnitTest method generateAllelicCount.
protected static AllelicCount generateAllelicCount(final double minorFraction, final SimpleInterval position, final RandomGenerator rng, final GammaDistribution biasGenerator, final double outlierProbability) {
final int numReads = 100;
final double bias = biasGenerator.sample();
//flip a coin to decide alt minor (alt fraction = minor fraction) or ref minor (alt fraction = 1 - minor fraction)
final double altFraction = rng.nextDouble() < 0.5 ? minorFraction : 1 - minorFraction;
//the probability of an alt read is the alt fraction modified by the bias or, in the case of an outlier, random
final double pAlt = rng.nextDouble() < outlierProbability ? rng.nextDouble() : altFraction / (altFraction + (1 - altFraction) * bias);
final int numAltReads = new BinomialDistribution(rng, numReads, pAlt).sample();
final int numRefReads = numReads - numAltReads;
return new AllelicCount(position, numAltReads, numRefReads);
}
use of org.apache.commons.math3.random.RandomGenerator in project gatk by broadinstitute.
the class CopyRatioSegmenterUnitTest method testSegmentation.
@Test
public void testSegmentation() {
final RandomGenerator rng = RandomGeneratorFactory.createRandomGenerator(new Random(563));
final List<Double> trueWeights = Arrays.asList(0.2, 0.5, 0.3);
final List<Double> trueLog2CopyRatios = Arrays.asList(-2.0, 0.0, 1.4);
final double trueMemoryLength = 1e5;
final double trueStandardDeviation = 0.2;
final CopyRatioHMM trueModel = new CopyRatioHMM(trueLog2CopyRatios, trueWeights, trueMemoryLength, trueStandardDeviation);
final int chainLength = 10000;
final List<SimpleInterval> positions = randomPositions("chr1", chainLength, rng, trueMemoryLength / 4);
final List<Integer> trueStates = trueModel.generateHiddenStateChain(positions);
final List<Double> trueLog2CopyRatioSequence = trueStates.stream().map(n -> trueLog2CopyRatios.get(n)).collect(Collectors.toList());
final List<Double> data = trueLog2CopyRatioSequence.stream().map(cr -> generateData(trueStandardDeviation, cr, rng)).collect(Collectors.toList());
final List<Target> targets = positions.stream().map(Target::new).collect(Collectors.toList());
final ReadCountCollection rcc = new ReadCountCollection(targets, Arrays.asList("SAMPLE"), new Array2DRowRealMatrix(data.stream().mapToDouble(x -> x).toArray()));
final CopyRatioSegmenter segmenter = new CopyRatioSegmenter(10, rcc);
final List<ModeledSegment> segments = segmenter.getModeledSegments();
final double[] segmentCopyRatios = segments.stream().flatMap(s -> Collections.nCopies((int) s.getTargetCount(), s.getSegmentMeanInLog2CRSpace()).stream()).mapToDouble(x -> x).toArray();
final double averageCopyRatioError = IntStream.range(0, trueLog2CopyRatioSequence.size()).mapToDouble(n -> Math.abs(segmentCopyRatios[n] - trueLog2CopyRatioSequence.get(n))).average().getAsDouble();
Assert.assertEquals(averageCopyRatioError, 0, 0.025);
}
Aggregations