htsjdk.samtools.metrics.MetricsFile Java Examples

The following examples show how to use htsjdk.samtools.metrics.MetricsFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CollectBaseDistributionByCycleSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void saveResults(final MetricsFile<?, Integer> metrics, final SAMFileHeader readsHeader, final String inputFileName) {
    MetricsUtils.saveMetrics(metrics, out);

    if (metrics.getAllHistograms().isEmpty()) {
        logger.warn("No valid bases found in input file.");
    } else if (chartOutput != null) {
        // Now run R to generate a chart

        // If we're working with a single library, assign that library's name
        // as a suffix to the plot title
        final List<SAMReadGroupRecord> readGroups = readsHeader.getReadGroups();

        /*
         * A subtitle for the plot, usually corresponding to a library.
         */
        String plotSubtitle = "";
        if (readGroups.size() == 1) {
            plotSubtitle = StringUtil.asEmptyIfNull(readGroups.get(0).getLibrary());
        }
        final RScriptExecutor executor = new RScriptExecutor();
        executor.addScript(getBaseDistributionByCycleRScriptResource());
        executor.addArgs(out, chartOutput.getAbsolutePath(), inputFileName, plotSubtitle);
        executor.exec();
    }
}
 
Example #2
Source File: ExtractIlluminaBarcodesTest.java    From picard with MIT License 6 votes vote down vote up
/**
 * Testing the quality thresholding. Looking at a single barcode (ACAGTG) with a min quality of 25 and no mismatches
 */
@Test(dataProvider = "qualityBarcodeData")
public void testQualityBarcodes(final int quality,
                                final int maxMismatches, final int perfectMatches, final int oneMismatch,
                                final String testName) throws Exception {
    final File metricsFile = File.createTempFile("qual.", ".metrics");
    metricsFile.deleteOnExit();

    final String[] args = new String[]{
            "BASECALLS_DIR=" + qual.getPath(),
            "LANE=" + 1,
            "READ_STRUCTURE=25T8B25T",
            "METRICS_FILE=" + metricsFile.getPath(),
            "MINIMUM_BASE_QUALITY=" + quality,
            "MAX_MISMATCHES=" + maxMismatches,
            "BARCODE=CAATAGTC"
    };

    Assert.assertEquals(runPicardCommandLine(args), 0);
    final MetricsFile<ExtractIlluminaBarcodes.BarcodeMetric, Integer> result = new MetricsFile<>();
    result.read(new FileReader(metricsFile));
    Assert.assertEquals(result.getMetrics().get(0).PERFECT_MATCHES, perfectMatches, "Got wrong number of perfect matches for test: '" + testName + "'");
    Assert.assertEquals(result.getMetrics().get(0).ONE_MISMATCH_MATCHES, oneMismatch, "Got wrong number of one-mismatch matches for test: '" + testName + "'");
}
 
Example #3
Source File: GatherReadQualityMetrics.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * Do the work after command line has been parsed. RuntimeException may be
 * thrown by this method, and are reported appropriately.
 *
 * @return program exit status.
 */
@Override
protected int doWork() {
	IOUtil.assertFileIsReadable(INPUT);
	IOUtil.assertFileIsWritable(OUTPUT);
	Map<String, ReadQualityMetrics> metricsMap = gatherMetrics(INPUT);
	MetricsFile<ReadQualityMetrics, Integer> outFile = new MetricsFile<>();
	outFile.addHistogram(metricsMap.get(this.GLOBAL).getHistogram());
       // Make sure the GLOBAL metrics is added first
       outFile.addMetric(metricsMap.remove(this.GLOBAL));
	for (ReadQualityMetrics metrics: metricsMap.values())
		outFile.addMetric(metrics);
	BufferedWriter w = IOUtil.openFileForBufferedWriting(OUTPUT);
	outFile.write(w);
	// close properly.
	try {
		w.close();
	} catch (IOException io) {
		throw new TranscriptomeException("Problem writing file", io);
	}
	return 0;
}
 
Example #4
Source File: TheoreticalSensitivityTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "TheoreticalSensitivityConstantDepthDataProvider")
public void testSensitivityAtConstantDepth(final double expected, final File metricsFile, final double alleleFraction, final int depth, final int sampleSize, final double tolerance) throws Exception {
    // This tests Theoretical Sensitivity assuming a uniform depth with a distribution of base quality scores.
    // Because this only tests sensitivity at a constant depth, we use this for testing the code at high depths.
    final MetricsFile<?, Integer> metrics = new MetricsFile<>();
    try (final FileReader metricsFileReader = new FileReader(metricsFile)) {
        metrics.read(metricsFileReader);
    }

    final List<Histogram<Integer>> histograms = metrics.getAllHistograms();
    final Histogram<Integer> qualityHistogram = histograms.get(1);

    // We ensure that even using different random seeds we converge to roughly the same value.
    for (int i = 0; i < 3; i++) {
        double result = TheoreticalSensitivity.sensitivityAtConstantDepth(depth, qualityHistogram, 3, sampleSize, alleleFraction, i);
        Assert.assertEquals(result, expected, tolerance);
    }
}
 
Example #5
Source File: CollectIlluminaBasecallingMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testNovaseqIndexedRun() throws Exception {
    final MetricsFile<IlluminaBasecallingMetrics, Integer> metricsFile = runIt(1, "151T8B8B151T",
            new File("testdata/picard/illumina/151T8B8B151T_cbcl/Data/Intensities/BaseCalls"), null, true);
    final IlluminaBasecallingMetrics laneMetric = metricsFile.getMetrics().get(0);
    Assert.assertEquals(laneMetric.LANE, "1");
    Assert.assertEquals(laneMetric.MOLECULAR_BARCODE_SEQUENCE_1, "CACCTAGTACTCGAGT");
    Assert.assertEquals(laneMetric.MOLECULAR_BARCODE_NAME, "SA_CACCTAGTACTCGAGT");
    Assert.assertEquals(laneMetric.MEAN_CLUSTERS_PER_TILE, 1.0);
    Assert.assertEquals(laneMetric.SD_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.MEAN_PF_CLUSTERS_PER_TILE,1.0);
    Assert.assertEquals(laneMetric.SD_PF_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.MEAN_PCT_PF_CLUSTERS_PER_TILE, 100.0);
    Assert.assertEquals(laneMetric.SD_PCT_PF_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.TOTAL_BASES, 302);
    Assert.assertEquals(laneMetric.TOTAL_READS, 2);
    Assert.assertEquals(laneMetric.PF_BASES, 302);
    Assert.assertEquals(laneMetric.PF_READS, 2);


    Assert.assertEquals(metricsFile.getMetrics().size(),6);
}
 
Example #6
Source File: GenotypeConcordance.java    From picard with MIT License 6 votes vote down vote up
/**
* Outputs the detailed statistics tables for SNP and Indel match categories.
**/
public static void outputDetailMetricsFile(final VariantContext.Type variantType, final MetricsFile<GenotypeConcordanceDetailMetrics,?> genotypeConcordanceDetailMetricsFile,
                                           final GenotypeConcordanceCounts counter, final String truthSampleName, final String callSampleName,
                                           final boolean missingSitesHomRef, final boolean outputAllRows) {
    final GenotypeConcordanceSchemeFactory schemeFactory = new GenotypeConcordanceSchemeFactory();
    final GenotypeConcordanceScheme scheme = schemeFactory.getScheme(missingSitesHomRef);
    scheme.validateScheme();
    for (final TruthState truthState : TruthState.values()) {
        for (final CallState callState : CallState.values()) {
            final long count = counter.getCount(truthState, callState);
            final String contingencyValues = scheme.getContingencyStateString(truthState, callState);
            if (count > 0 || outputAllRows) {
                final GenotypeConcordanceDetailMetrics detailMetrics = new GenotypeConcordanceDetailMetrics();
                detailMetrics.VARIANT_TYPE = variantType;
                detailMetrics.TRUTH_SAMPLE = truthSampleName;
                detailMetrics.CALL_SAMPLE = callSampleName;
                detailMetrics.TRUTH_STATE = truthState;
                detailMetrics.CALL_STATE = callState;
                detailMetrics.COUNT = count;
                detailMetrics.CONTINGENCY_VALUES = contingencyValues;
                genotypeConcordanceDetailMetricsFile.addMetric(detailMetrics);
            }
        }
    }
}
 
Example #7
Source File: ClusterCrosscheckMetrics.java    From picard with MIT License 6 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    if(OUTPUT != null) IOUtil.assertFileIsWritable(OUTPUT);

    final MetricsFile<CrosscheckMetric, ?> metricsFile = getMetricsFile();

    try {
        metricsFile.read(new FileReader(INPUT));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return 1;
    }

    clusterMetrics(metricsFile.getMetrics()).write(OUTPUT);

    return 0;
}
 
Example #8
Source File: CollectSamErrorMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "OneCovariateErrorMetricsDataProvider")
public void testOneCovariateErrorMetrics(final String errorSubscript, final File samFile, final int priorQ, BaseErrorMetric expectedMetric) {

    ErrorMetric.setPriorError(QualityUtil.getErrorProbabilityFromPhredScore(priorQ));
    expectedMetric.calculateDerivedFields();

    // Note that soft clipped bases are not counted
    List<BaseErrorMetric> metrics = MetricsFile.readBeans(new File(errorMetrics.get(samFile).getAbsolutePath() + errorSubscript));

    BaseErrorMetric metric = metrics
            .stream()
            .filter(m -> m.COVARIATE.equals(expectedMetric.COVARIATE))
            .findAny()
            .orElseThrow(() -> new AssertionError("didn't find metric with COVARIATE==" + expectedMetric.COVARIATE));

    Assert.assertEquals(metric, expectedMetric);
}
 
Example #9
Source File: TheoreticalSensitivityTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "TheoreticalSensitivityDataProvider")
public void testSensitivity(final double expected, final File metricsFile, final double alleleFraction, final int sampleSize) throws Exception {
    // This tests Theoretical Sensitivity using distributions on both base quality scores
    // and the depth histogram.

    // We use a pretty forgiving tolerance here because for these tests
    // we are not using large enough sample sizes to converge.
    final double tolerance = 0.02;

    final MetricsFile<?, Integer> metrics = new MetricsFile<>();
    try (final FileReader metricsFileReader = new FileReader(metricsFile)) {
        metrics.read(metricsFileReader);
    }

    final List<Histogram<Integer>> histograms = metrics.getAllHistograms();
    final Histogram<Integer> depthHistogram = histograms.get(0);
    final Histogram<Integer> qualityHistogram = histograms.get(1);

    final double result = TheoreticalSensitivity.theoreticalSensitivity(depthHistogram, qualityHistogram, sampleSize, 3, alleleFraction);

    Assert.assertEquals(result, expected, tolerance);
}
 
Example #10
Source File: CollectIlluminaBasecallingMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testNonIndexedRunLane1() throws Exception {
    final MetricsFile<IlluminaBasecallingMetrics, Integer> metricsFile = runIt(1, "125T125T",
            new File(rootTestDir, "125T125T/Data/Intensities/BaseCalls"), null, false);
    final IlluminaBasecallingMetrics laneMetric = metricsFile.getMetrics().get(0);

    Assert.assertEquals(laneMetric.LANE, "1");
    Assert.assertEquals(laneMetric.MOLECULAR_BARCODE_SEQUENCE_1, null);
    Assert.assertEquals(laneMetric.MOLECULAR_BARCODE_NAME, null);
    Assert.assertEquals(laneMetric.MEAN_CLUSTERS_PER_TILE, 2000.0);
    Assert.assertEquals(laneMetric.SD_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.MEAN_PF_CLUSTERS_PER_TILE,1863.0);
    Assert.assertEquals(laneMetric.SD_PF_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.MEAN_PCT_PF_CLUSTERS_PER_TILE, 93.15);
    Assert.assertEquals(laneMetric.SD_PCT_PF_CLUSTERS_PER_TILE, 0.0);
    Assert.assertEquals(laneMetric.TOTAL_BASES, 500000);
    Assert.assertEquals(laneMetric.TOTAL_READS, 4000);
    Assert.assertEquals(laneMetric.PF_BASES, 465750);
    Assert.assertEquals(laneMetric.PF_READS, 3726);


    Assert.assertEquals(metricsFile.getMetrics().size(),1);
}
 
Example #11
Source File: CollectBaseDistributionByCycle.java    From picard with MIT License 6 votes vote down vote up
@Override
protected void finish() {
    final MetricsFile<BaseDistributionByCycleMetrics, ?> metrics = getMetricsFile();
    hist.addToMetricsFile(metrics);
    metrics.write(OUTPUT);
    if (hist.isEmpty()) {
        log.warn("No valid bases found in input file. No plot will be produced.");
    } else {
        final int rResult = RExecutor.executeFromClasspath("picard/analysis/baseDistributionByCycle.R",
                OUTPUT.getAbsolutePath(),
                CHART_OUTPUT.getAbsolutePath(),
                INPUT.getName(),
                plotSubtitle);
        if (rResult != 0) {
            throw new PicardException("R script nucleotideDistributionByCycle.R failed with return code " + rResult);
        }
    }
}
 
Example #12
Source File: CollectAlignmentSummaryMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testZeroLengthReads() throws IOException {
    final File input = new File(TEST_DATA_DIR, "summary_alignment_stats_test2.sam");
    final File outfile = File.createTempFile("alignmentMetrics", ".txt");
    outfile.deleteOnExit();
    final String[] args = new String[]{
            "INPUT=" + input.getAbsolutePath(),
            "OUTPUT=" + outfile.getAbsolutePath(),
            "COLLECT_ALIGNMENT_INFORMATION=false"
    };
    Assert.assertEquals(runPicardCommandLine(args), 0);

    final MetricsFile<AlignmentSummaryMetrics, Comparable<?>> output = new MetricsFile<>();
    try (FileReader reader = new FileReader(outfile)) {
        output.read(reader);
    }
    for (final AlignmentSummaryMetrics metrics : output.getMetrics()) {
        // test that it doesn't blow up
    }
}
 
Example #13
Source File: AlignmentSummaryMetricsCollector.java    From picard with MIT License 6 votes vote down vote up
@Override
public void addMetricsToFile(final MetricsFile<AlignmentSummaryMetrics, Comparable<?>> file) {
    if (firstOfPairCollector.getMetrics().TOTAL_READS > 0) {
        // override how bad cycle is determined for paired reads, it should be
        // the sum of first and second reads
        pairCollector.getMetrics().BAD_CYCLES = firstOfPairCollector.getMetrics().BAD_CYCLES +
                secondOfPairCollector.getMetrics().BAD_CYCLES;

        file.addMetric(firstOfPairCollector.getMetrics());
        file.addMetric(secondOfPairCollector.getMetrics());
        file.addMetric(pairCollector.getMetrics());
    }

    //if there are no reads in any category then we will returned an unpaired alignment summary metric with all zero values
    if (unpairedCollector.getMetrics().TOTAL_READS > 0 || firstOfPairCollector.getMetrics().TOTAL_READS == 0) {
        file.addMetric(unpairedCollector.getMetrics());
    }
}
 
Example #14
Source File: CollectTargetedMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "targetedIntervalDataProvider")
public void runCollectTargetedMetricsTest(final File input, final File outfile, final File perTargetOutfile, final String referenceFile,
                            final String targetIntervals, final int sampleSize) throws IOException {

    final String[] args = new String[] {
            "TARGET_INTERVALS=" + targetIntervals,
            "INPUT=" + input.getAbsolutePath(),
            "OUTPUT=" + outfile.getAbsolutePath(),
            "REFERENCE_SEQUENCE=" + referenceFile,
            "PER_TARGET_COVERAGE=" + perTargetOutfile.getAbsolutePath(),
            "LEVEL=ALL_READS",
            "AMPLICON_INTERVALS=" + targetIntervals,
            "SAMPLE_SIZE=" + sampleSize
    };

    Assert.assertEquals(runPicardCommandLine(args), 0);

    final MetricsFile<TargetedPcrMetrics, Comparable<?>> output = new MetricsFile<>();
    output.read(new FileReader(outfile));

    for (final TargetedPcrMetrics metrics : output.getMetrics()) {
        Assert.assertEquals(metrics.TOTAL_READS, numReads * 2);
        Assert.assertEquals(metrics.HET_SNP_SENSITIVITY, .997972, .02);
    }
}
 
Example #15
Source File: TheoreticalSensitivityTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "sumOfGaussiansDataProvider")
public void testDrawSumOfQScores(final File metricsFile, final int altDepth, final double tolerance) throws Exception {
    final MetricsFile<TheoreticalSensitivityMetrics, Integer> metrics = new MetricsFile<>();
    try (final FileReader metricsFileReader = new FileReader(metricsFile)) {
        metrics.read(metricsFileReader);
    }

    final List<Histogram<Integer>> histograms = metrics.getAllHistograms();

    final Histogram<Integer> qualityHistogram = histograms.get(1);
    final TheoreticalSensitivity.RouletteWheel qualityRW = new TheoreticalSensitivity.RouletteWheel(TheoreticalSensitivity.trimDistribution(TheoreticalSensitivity.normalizeHistogram(qualityHistogram)));

    final Random randomNumberGenerator = new Random(51);

    // Calculate mean and deviation of quality score distribution to enable Gaussian sampling below
    final double averageQuality = qualityHistogram.getMean();
    final double standardDeviationQuality = qualityHistogram.getStandardDeviation();

    for (int k = 0; k < 1; k++) {
        int sumOfQualitiesFull = IntStream.range(0, altDepth).map(n -> qualityRW.draw()).sum();
        int sumOfQualities = TheoreticalSensitivity.drawSumOfQScores(altDepth, averageQuality, standardDeviationQuality, randomNumberGenerator.nextGaussian());

        Assert.assertEquals(sumOfQualitiesFull, sumOfQualities, sumOfQualitiesFull * tolerance);
    }
}
 
Example #16
Source File: CollectSamErrorMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "oneCovariateIndelErrorMetricsDataProvider")
public void testOneCovariateIndelErrorMetrics(final String errorSubscript, final File samFile, final int priorQ, BaseErrorMetric expectedMetric) {

    final File errorByAll = new File(errorMetrics.get(samFile).getAbsolutePath() + errorSubscript);
    errorByAll.deleteOnExit();

    ErrorMetric.setPriorError(QualityUtil.getErrorProbabilityFromPhredScore(priorQ));
    expectedMetric.calculateDerivedFields();

    // Note that soft clipped bases are not counted
    List<BaseErrorMetric> metrics = MetricsFile.readBeans(errorByAll);

    BaseErrorMetric metric = metrics
            .stream()
            .filter(m -> m.COVARIATE.equals(expectedMetric.COVARIATE))
            .findAny()
            .orElseThrow(() -> new AssertionError("didn't find metric with COVARIATE==" + expectedMetric.COVARIATE));

    Assert.assertEquals(metric, expectedMetric);
}
 
Example #17
Source File: CheckFingerprintTest.java    From picard with MIT License 5 votes vote down vote up
@Test
public void testBaseOutput() {
    String[] args = new String[]{
            "I=" + TEST_INPUT_VCF1,
            "O=" + TEST_OUTPUT,
            "G=" + TEST_GENOTYPES_VCF1,
            "H=" + SUBSETTED_HAPLOTYPE_DATABASE_FOR_TESTING
    };

    Assert.assertEquals(runPicardCommandLine(args), 0);
    Assert.assertTrue(MetricsFile.areMetricsEqual(new File(TEST_OUTPUT + ".fingerprinting_summary_metrics"),
            RESULT_EXAMPLE_SUMMARY));
    Assert.assertTrue(MetricsFile.areMetricsEqual(new File(TEST_OUTPUT + ".fingerprinting_detail_metrics"),
            RESULT_EXAMPLE_DETAIL));
}
 
Example #18
Source File: CompareSAMsTest.java    From picard with MIT License 5 votes vote down vote up
@Test(dataProvider = "compareSams")
public void testComparisons(final String f1, final String f2, final ArrayList<String> args, final boolean areEqual) throws IOException {
    final Path tmpOutput = Files.createTempFile("compareSam", ".tsv");
    final String in1 = new File(TEST_FILES_DIR, f1).getAbsolutePath();
    final String in2 = new File(TEST_FILES_DIR, f2).getAbsolutePath();
    ArrayList<String> commandArgs = new ArrayList<>(
            Arrays.asList(
                    in1,
                    in2,
                    "O=" + tmpOutput
            )
    );
    if (args != null) {
        commandArgs.addAll(args);
    }
    Assert.assertEquals(runPicardCommandLine(commandArgs) == 0, areEqual);
    final MetricsFile<SamComparisonMetric, Comparable<?>> metricsOutput = new MetricsFile<>();
    metricsOutput.read(new FileReader(tmpOutput.toFile()));

    //swap order of input files
    commandArgs = new ArrayList<>(
            Arrays.asList(
                    in2,
                    in1,
                    "O=" + tmpOutput
            )
    );
    if (args != null) {
        commandArgs.addAll(args);
    }
    Assert.assertEquals(runPicardCommandLine(commandArgs) == 0, areEqual);
    metricsOutput.read(new FileReader(tmpOutput.toFile()));

    Assert.assertEquals(metricsOutput.getMetrics().get(0).LEFT_FILE, in1);
    Assert.assertEquals(metricsOutput.getMetrics().get(0).RIGHT_FILE, in2);

    Assert.assertEquals(metricsOutput.getMetrics().get(1).LEFT_FILE, in2);
    Assert.assertEquals(metricsOutput.getMetrics().get(1).RIGHT_FILE, in1);
}
 
Example #19
Source File: CollectWgsMetricsWithNonZeroCoverage.java    From picard with MIT License 5 votes vote down vote up
@Override
public void addToMetricsFile(final MetricsFile<WgsMetrics, Integer> file,
                             final boolean includeBQHistogram,
                             final CountingFilter dupeFilter,
                             final CountingFilter adapterFilter,
                             final CountingFilter mapqFilter,
                             final CountingPairedFilter pairFilter) {
    highQualityDepthHistogram = getDepthHistogram();
    highQualityDepthHistogramNonZero = getDepthHistogramNonZero();

    // calculate metrics the same way as in CollectWgsMetrics
    final WgsMetricsWithNonZeroCoverage metrics = (WgsMetricsWithNonZeroCoverage) getMetrics(dupeFilter, adapterFilter, mapqFilter, pairFilter);
    metrics.CATEGORY = WgsMetricsWithNonZeroCoverage.Category.WHOLE_GENOME;

    // set count of the coverage-zero bin to 0 and re-calculate metrics
    // note we don't need to update the base quality histogram; there are no bases in the depth = 0 bin
    highQualityDepthHistogramArray[0] = 0;
    unfilteredDepthHistogramArray[0] = 0;

    final WgsMetricsWithNonZeroCoverage metricsNonZero = (WgsMetricsWithNonZeroCoverage) getMetrics(dupeFilter, adapterFilter, mapqFilter, pairFilter);
    metricsNonZero.CATEGORY = WgsMetricsWithNonZeroCoverage.Category.NON_ZERO_REGIONS;

    file.addMetric(metrics);
    file.addMetric(metricsNonZero);
    file.addHistogram(highQualityDepthHistogram);
    file.addHistogram(highQualityDepthHistogramNonZero);

    if (includeBQHistogram) {
        addBaseQHistogram(file);
    }
}
 
Example #20
Source File: CommandLineProgram.java    From picard with MIT License 5 votes vote down vote up
/** Gets a MetricsFile with default headers already written into it. */
protected <A extends MetricBase,B extends Comparable<?>> MetricsFile<A,B> getMetricsFile() {
    final MetricsFile<A,B> file = new MetricsFile<>();
    for (final Header h : this.defaultHeaders) {
        file.addHeader(h);
    }

    return file;
}
 
Example #21
Source File: PSFilterFileLogger.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PSFilterFileLogger(final MetricsFile<PSFilterMetrics, Long> metricsFile, final String metricsOutputPath) {
    Utils.nonNull(metricsFile, "Filter logger parameter metricsFile cannot be null");
    Utils.nonNull(metricsOutputPath, "Filter logger parameter metricsOutputPath cannot be null");
    this.metrics = new PSFilterMetrics();
    this.metricsFile = metricsFile;
    this.metricsOutputPath = metricsOutputPath;
}
 
Example #22
Source File: ExampleSingleMetricsCollectorSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Initialize the collector with input arguments;
 */
@Override
public void initialize(
        final ExampleSingleMetricsArgumentCollection inputArgs,
        final SAMFileHeader samHeader,
        final List<Header> defaultHeaders) {
    metricsFile = new MetricsFile<ExampleSingleMetrics, Integer>();
    defaultHeaders.stream().forEach(h -> metricsFile.addHeader(h));
    this.args = inputArgs;
}
 
Example #23
Source File: DigitalExpression.java    From Drop-seq with MIT License 5 votes vote down vote up
public static void writeSummary(final Collection<DESummary> summaryCollection, final MetricsFile<DESummary, Integer> summaryMetricsFile, final File outFile) {
    List<DESummary> sc = new ArrayList<>(summaryCollection);
    Collections.sort(sc, DigitalExpression.TRANSCRIPT_ORDER_DESCENDING);
    for (DESummary z: sc)
        summaryMetricsFile.addMetric(z);
    summaryMetricsFile.write(outFile);
}
 
Example #24
Source File: MetricsUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write a {@link MetricsFile} to the given path, can be any destination supported by {@link BucketUtils#createFile(String)}
 * @param metricsFile a {@link MetricsFile} object to write to disk
 * @param metricsOutputPath the path (or uri) to write the metrics to
 */
public static void saveMetrics(final MetricsFile<?, ?> metricsFile, String metricsOutputPath) {
    try(Writer out = new BufferedWriter(new OutputStreamWriter(BucketUtils.createFile(metricsOutputPath)))) {
        metricsFile.write(out);
    } catch (IOException | SAMException e ){
        throw new UserException.CouldNotCreateOutputFile("Could not write metrics to file: " + metricsOutputPath, e);
    }
}
 
Example #25
Source File: AbstractWgsMetricsCollector.java    From picard with MIT License 5 votes vote down vote up
/**
 * Adds collected metrics and depth histogram to file
 * @param file MetricsFile for result of collector's work
 * @param dupeFilter         counting filter for duplicate reads
 * @param adapterFilter      counting filter for adapter reads
 * @param mapqFilter         counting filter for mapping quality
 * @param pairFilter         counting filter for reads without a mapped mate pair
 */
public void addToMetricsFile(final MetricsFile<WgsMetrics, Integer> file,
        final boolean includeBQHistogram,
        final CountingFilter dupeFilter,
        final CountingFilter adapterFilter,
        final CountingFilter mapqFilter,
        final CountingPairedFilter pairFilter) {
    final WgsMetrics metrics = getMetrics(dupeFilter, adapterFilter, mapqFilter, pairFilter);

    // add them to the file
    file.addMetric(metrics);
    file.addHistogram(getHighQualityDepthHistogram());
    if (includeBQHistogram) addBaseQHistogram(file);
}
 
Example #26
Source File: MeanQualityByCycleSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private MetricsFile<?,Integer> finish(final HistogramGenerator q, final HistogramGenerator oq) {
    // Generate a "Histogram" of mean quality and write it to the file
    final MetricsFile<?,Integer> metrics = getMetricsFile();
    metrics.addHistogram(q.getMeanQualityHistogram());
    if (!oq.isEmpty()) {
        metrics.addHistogram(oq.getMeanQualityHistogram());
    }
    return metrics;
}
 
Example #27
Source File: FilterBam.java    From Drop-seq with MIT License 5 votes vote down vote up
/**
 * Write the summary output file of reads accepted/rejected.
 * @param summaryOutputFile
 * @param metrics
 */
private void writeSummary (File summaryOutputFile, FilteredReadsMetric metrics) {
	if (summaryOutputFile!=null) {
		MetricsFile<FilteredReadsMetric, Integer> outSummary = getMetricsFile();
		outSummary.addMetric(metrics);
		outSummary.write(summaryOutputFile);		
	}
}
 
Example #28
Source File: PSScoreFileLogger.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PSScoreFileLogger(final MetricsFile<PSScoreMetrics, Long> metricsFile, final String metricsOutputPath) {
    Utils.nonNull(metricsFile, "Score filter parameter metricsFile cannot be null");
    Utils.nonNull(metricsOutputPath, "Score filter parameter metricsOutputPath cannot be null");
    this.metrics = new PSScoreMetrics();
    this.metricsFile = metricsFile;
    this.metricsOutputPath = metricsOutputPath;
}
 
Example #29
Source File: QualityScoreDistributionSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void runTool(final JavaSparkContext ctx) {
    final JavaRDD<GATKRead> reads = getReads();
    final MetricsReadFilter metricsFilter =
        new MetricsReadFilter(this.pfReadsOnly, this.alignedReadsOnly);
    final JavaRDD<GATKRead> filteredReads = reads.filter(read -> metricsFilter.test(read));
    final Counts result = filteredReads.aggregate(new Counts(includeNoCalls),
            (counts, read) -> counts.addRead(read),
            (counts1, counts2) -> counts1.merge(counts2));

    final MetricsFile<?, Byte> metrics = makeMetrics(result);
    saveResults(metrics, getHeaderForReads(), getReadSourceName());
}
 
Example #30
Source File: CollectQualityYieldMetricsTest.java    From picard with MIT License 5 votes vote down vote up
@Test
public void test() throws IOException {
    final File input = new File(TEST_DATA_DIR, "insert_size_metrics_test.sam");
    final File outfile   = File.createTempFile("test", ".quality_yield_metrics");
    outfile.deleteOnExit();
    final String[] args = new String[] {
            "INPUT="  + input.getAbsolutePath(),
            "OUTPUT=" + outfile.getAbsolutePath(),
    };

    Assert.assertEquals(runPicardCommandLine(args), 0);

    final MetricsFile<CollectQualityYieldMetrics.QualityYieldMetrics, Comparable<?>> output = new MetricsFile<CollectQualityYieldMetrics.QualityYieldMetrics, Comparable<?>>();
    output.read(new FileReader(outfile));

    for (final CollectQualityYieldMetrics.QualityYieldMetrics metrics : output.getMetrics()) {
            Assert.assertEquals(metrics.TOTAL_READS, 52);
            Assert.assertEquals(metrics.PF_READS, 52);
            Assert.assertEquals(metrics.READ_LENGTH, 101);
            Assert.assertEquals(metrics.TOTAL_BASES, 5252);
            Assert.assertEquals(metrics.PF_BASES, 5252);
            Assert.assertEquals(metrics.Q20_BASES, 3532);
            Assert.assertEquals(metrics.PF_Q20_BASES, 3532);
            Assert.assertEquals(metrics.Q30_BASES, 3145);
            Assert.assertEquals(metrics.PF_Q30_BASES, 3145);
            Assert.assertEquals(metrics.Q20_EQUIVALENT_YIELD, 6497);
            Assert.assertEquals(metrics.PF_Q20_EQUIVALENT_YIELD, 6497);
    }
}