Search in sources :

Example 16 with RScriptExecutor

use of org.broadinstitute.hellbender.utils.R.RScriptExecutor in project gatk by broadinstitute.

the class CollectGcBiasMetrics method finish.

/////////////////////////////////////////////////////////////////////////////
// Synthesize the normalized coverage metrics and write it all out to a file
/////////////////////////////////////////////////////////////////////////////
@Override
protected void finish() {
    final MetricsFile<GcBiasDetailMetrics, ?> metricsFile = getMetricsFile();
    final double totalWindows = sum(windowsByGc);
    final double totalReads = sum(readsByGc);
    final double meanReadsPerWindow = totalReads / totalWindows;
    final double minimumWindowsToCountInSummary = totalWindows * this.MINIMUM_GENOME_FRACTION;
    for (int i = 0; i < windowsByGc.length; ++i) {
        if (windowsByGc[i] == 0)
            continue;
        final GcBiasDetailMetrics m = new GcBiasDetailMetrics();
        m.GC = i;
        m.WINDOWS = windowsByGc[i];
        m.READ_STARTS = readsByGc[i];
        if (errorsByGc[i] > 0)
            m.MEAN_BASE_QUALITY = QualityUtil.getPhredScoreFromObsAndErrors(basesByGc[i], errorsByGc[i]);
        m.NORMALIZED_COVERAGE = (m.READ_STARTS / (double) m.WINDOWS) / meanReadsPerWindow;
        m.ERROR_BAR_WIDTH = (Math.sqrt(m.READ_STARTS) / (double) m.WINDOWS) / meanReadsPerWindow;
        metricsFile.addMetric(m);
    }
    metricsFile.write(OUTPUT);
    // Synthesize the high level metrics
    if (SUMMARY_OUTPUT != null) {
        final MetricsFile<GcBiasSummaryMetrics, ?> summaryMetricsFile = getMetricsFile();
        final GcBiasSummaryMetrics summary = new GcBiasSummaryMetrics();
        summary.WINDOW_SIZE = this.WINDOW_SIZE;
        summary.TOTAL_CLUSTERS = this.totalClusters;
        summary.ALIGNED_READS = this.totalAlignedReads;
        calculateDropoutMetrics(metricsFile.getMetrics(), summary);
        summaryMetricsFile.addMetric(summary);
        summaryMetricsFile.write(SUMMARY_OUTPUT);
    }
    // Plot the results
    final NumberFormat fmt = NumberFormat.getIntegerInstance();
    fmt.setGroupingUsed(true);
    final String subtitle = "Total clusters: " + fmt.format(this.totalClusters) + ", Aligned reads: " + fmt.format(this.totalAlignedReads);
    String title = INPUT.getName().replace(".duplicates_marked", "").replace(".aligned.bam", "");
    title += "." + saveHeader;
    if (PRODUCE_PLOT) {
        final RScriptExecutor executor = new RScriptExecutor();
        executor.addScript(new Resource(R_SCRIPT, CollectGcBiasMetrics.class));
        executor.addArgs(OUTPUT.getAbsolutePath(), CHART_OUTPUT.getAbsolutePath(), title, subtitle, String.valueOf(WINDOW_SIZE));
        executor.exec();
    }
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) NumberFormat(java.text.NumberFormat)

Example 17 with RScriptExecutor

use of org.broadinstitute.hellbender.utils.R.RScriptExecutor in project gatk by broadinstitute.

the class CollectBaseDistributionByCycle method finish.

@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 if (PRODUCE_PLOT) {
        final RScriptExecutor executor = new RScriptExecutor();
        executor.addScript(new Resource(R_SCRIPT, CollectBaseDistributionByCycle.class));
        executor.addArgs(OUTPUT.getAbsolutePath(), CHART_OUTPUT.getAbsolutePath(), INPUT.getName(), plotSubtitle);
        executor.exec();
    }
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor)

Example 18 with RScriptExecutor

use of org.broadinstitute.hellbender.utils.R.RScriptExecutor in project gatk by broadinstitute.

the class PlotACNVResults method writeSegmentedAlleleFractionPlot.

/**
     * @param sampleName Sample name derived from input files
     * @param contigNames List containing contig names
     * @param contigLengths List containing contig lengths (same order as contigNames)
     */
private void writeSegmentedAlleleFractionPlot(final String sampleName, final List<String> contigNames, final List<Integer> contigLengths) {
    //names separated by delimiter
    final String contigNamesArg = contigNames.stream().collect(Collectors.joining(CONTIG_DELIMITER));
    //lengths separated by delimiter
    final String contigLengthsArg = contigLengths.stream().map(Object::toString).collect(Collectors.joining(CONTIG_DELIMITER));
    final String outputDirArg = addTrailingSlashIfNecessary(outputDir);
    final RScriptExecutor executor = new RScriptExecutor();
    //this runs the R statement "source("CNVPlottingLibrary.R")" before the main script runs
    executor.addScript(new Resource(CNV_PLOTTING_R_LIBRARY, PlotSegmentedCopyRatio.class));
    executor.addScript(new Resource(ACNV_PLOTTING_R_SCRIPT, PlotACNVResults.class));
    //--args is needed for Rscript to recognize other arguments properly
    executor.addArgs("--args", "--sample_name=" + sampleName, "--snp_counts_file=" + snpCountsFile, "--tangent_file=" + tangentFile, "--segments_file=" + segmentsFile, "--contig_names=" + contigNamesArg, "--contig_lengths=" + contigLengthsArg, "--output_dir=" + outputDirArg, "--output_prefix=" + outputPrefix);
    executor.exec();
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor)

Aggregations

RScriptExecutor (org.broadinstitute.hellbender.utils.R.RScriptExecutor)18 Resource (org.broadinstitute.hellbender.utils.io.Resource)14 File (java.io.File)8 UserException (org.broadinstitute.hellbender.exceptions.UserException)3 MetricsFile (htsjdk.samtools.metrics.MetricsFile)2 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 PrintStream (java.io.PrintStream)2 PrintWriter (java.io.PrintWriter)2 DoubleStream (java.util.stream.DoubleStream)2 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)2 ParamUtils (org.broadinstitute.hellbender.utils.param.ParamUtils)2 SAMRecord (htsjdk.samtools.SAMRecord)1 SamReader (htsjdk.samtools.SamReader)1 ReferenceSequence (htsjdk.samtools.reference.ReferenceSequence)1 ReferenceSequenceFileWalker (htsjdk.samtools.reference.ReferenceSequenceFileWalker)1 Histogram (htsjdk.samtools.util.Histogram)1 NumberFormat (java.text.NumberFormat)1 CommandLineException (org.broadinstitute.barclay.argparser.CommandLineException)1 ExpandingArrayList (org.broadinstitute.hellbender.utils.collections.ExpandingArrayList)1