SomaticPeakFactory { @NotNull static List<SomaticPeak> findPeaks(@NotNull final List<Double> sample) { final KernelEstimator estimator = new KernelEstimator(0.001, KERNEL_BANDWIDTH); sample.forEach(x -> estimator.addValue(x, 1.0D)); final double[] vafs = IntStream.rangeClosed(0, 51).mapToDouble(x -> x / 100d).toArray(); final double[] densities = DoubleStream.of(vafs).map(estimator::getProbability).toArray(); final List<SomaticPeak> results = Lists.newArrayList(); for (int i = 1; i < densities.length - 1; i++) { double density = densities[i]; if (Doubles.greaterThan(density, densities[i - 1]) && Doubles.greaterThan(density, densities[i + 1])) { final double alleleFrequency = vafs[i]; final int peakCount = count(alleleFrequency, sample); final SomaticPeak peak = ImmutableSomaticPeak.builder().alleleFrequency(alleleFrequency).count(peakCount).build(); LOGGER.info("Discovered peak {}", peak); results.add(peak); } } return results; }    }
@Test public void testFindPeaks() { List<Double> sample = Lists.newArrayList(); for (int i = 0; i < 100; i++) { sample.add(i / 100d); } for (int i = 0; i < 10; i++) { sample.add(0.2); sample.add(0.4); sample.add(0.6); } final List<SomaticPeak> peaks = SomaticPeakFactory.findPeaks(sample); assertEquals(2, peaks.size()); assertPeak(peaks.get(0), 0.2, 13); assertPeak(peaks.get(1), 0.4, 13); }