Search in sources :

Example 1 with Resource

use of org.broadinstitute.hellbender.utils.io.Resource in project gatk by broadinstitute.

the class InsertSizeMetricsCollector method writeHistogramPDF.

/**
     * Calls R script to plot histogram(s) in PDF.
     */
private void writeHistogramPDF(final String inputName) throws RScriptExecutorException {
    // path to Picard R script for producing histograms in PDF files.
    final String R_SCRIPT = "insertSizeHistogram.R";
    File histFile = new File(inputArgs.histogramPlotFile);
    IOUtil.assertFileIsWritable(histFile);
    final RScriptExecutor executor = new RScriptExecutor();
    executor.addScript(new Resource(R_SCRIPT, InsertSizeMetricsCollector.class));
    executor.addArgs(// text-based metrics file
    inputArgs.output, // PDF graphics file
    histFile.getAbsolutePath(), // input bam file
    inputName);
    if (inputArgs.histogramWidth != null) {
        executor.addArgs(String.valueOf(inputArgs.histogramWidth));
    }
    executor.exec();
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File)

Example 2 with Resource

use of org.broadinstitute.hellbender.utils.io.Resource in project gatk by broadinstitute.

the class RCBSSegmenter method writeSegmentFile.

/**
     * Create a segmentation file using CBS in R.
     *
     * <p>https://www.bioconductor.org/packages/release/bioc/manuals/DNAcopy/man/DNAcopy.pdf</p>
     *
     * <p>Please see the above documentation for a more detailed description of the parameters.</p>
     *
     * <p>IMPORTANT:  There is no check that the weights have the same number of entries as the tnFile.</p>
     *
     * Wraps the call:
     * segment(x, weights = NULL, alpha = 0.01, nperm = 10000, p.method =
     *  c("hybrid", "perm"), min.width=2, kmax=25, nmin=200,
     *  eta=0.05, sbdry=NULL, trim = 0.025, undo.splits =
     *  c("none", "prune", "sdundo"), undo.prune=0.05,
     *  undo.SD=3, verbose=1)
     *
     * <p> Note that sbdry and verbose are unavailable through this interface. </p>
     *
     * @param sampleName Name of the sample being run through the segmenter.  Never {@code null}
     * @param tnFile Tangent-normalized targets file.  Never {@code null}
     * @param outputFile Full path to the outputted segment file.  Never {@code null}
     * @param log whether the tnFile input has already been put into log2CR.  Never {@code null}
     * @param minWidth minimum length for a segment
     * @param weightFile File containing weights for each target (doubles; one per line).  Must be the same length as what is in the tnFile (note that this is not
     *                enforced here).  Typically, 1/var(target) is what is used.  All values must be greater than 0.
     *                Use {@code null} if weighting is not desired.  These values should not be log space.
     */
public static void writeSegmentFile(final String sampleName, final String tnFile, final String outputFile, final Boolean log, final File weightFile, final double alpha, final int nperm, final PMethod pmethod, final int minWidth, final int kmax, final int nmin, final double eta, final double trim, final UndoSplits undoSplits, final double undoPrune, final int undoSD) {
    String logArg = log ? "TRUE" : "FALSE";
    final RScriptExecutor executor = new RScriptExecutor();
    executor.addScript(new Resource(R_SCRIPT, RCBSSegmenter.class));
    /*--args is needed for Rscript to recognize other arguments properly*/
    executor.addArgs("--args", "--sample_name=" + sampleName, "--targets_file=" + tnFile, "--output_file=" + outputFile, "--log2_input=" + logArg, "--min_width=" + String.valueOf(minWidth), "--alpha=" + String.valueOf(alpha), "--nperm=" + String.valueOf(nperm), "--pmethod=" + pmethod.toString(), "--kmax=" + String.valueOf(kmax), "--nmin=" + String.valueOf(nmin), "--eta=" + String.valueOf(eta), "--trim=" + String.valueOf(trim), "--undosplits=" + undoSplits.toString(), "--undoprune=" + String.valueOf(undoPrune), "--undoSD=" + String.valueOf(undoSD));
    if (weightFile != null) {
        final double[] weights = ParamUtils.readValuesFromFile(weightFile);
        // Check to make sure that no weights are zero.
        if (!DoubleStream.of(weights).allMatch(d -> d > 0 && !Double.isNaN(d) && Double.isFinite(d))) {
            throw new GATKException("A weight for a target was zero or less, which is not allowed.  If you truly want zero, you must remove the target from consideration.");
        }
        // Add the argument to specify weights
        executor.addArgs("--weights_file=" + weightFile.getAbsolutePath());
    }
    executor.exec();
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) ParamUtils(org.broadinstitute.hellbender.utils.param.ParamUtils) File(java.io.File) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) DoubleStream(java.util.stream.DoubleStream) Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 3 with Resource

use of org.broadinstitute.hellbender.utils.io.Resource in project gatk by broadinstitute.

the class PlotSegmentedCopyRatio method writeSegmentedCopyRatioPlots.

/**
     * @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 writeSegmentedCopyRatioPlots(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));
    //names separated by delimiter
    final String contigLengthsArg = contigLengths.stream().map(Object::toString).collect(Collectors.joining(CONTIG_DELIMITER));
    final String outputDirArg = addTrailingSlashIfNecessary(outputDir);
    final String isLog2InputArg = isLog2Input ? "TRUE" : "FALSE";
    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(COPY_RATIO_PLOTTING_R_SCRIPT, PlotSegmentedCopyRatio.class));
    //--args is needed for Rscript to recognize other arguments properly
    executor.addArgs("--args", "--sample_name=" + sampleName, "--tangent_file=" + tangentFile, "--pre_tangent_file=" + preTangentFile, "--segments_file=" + segmentsFile, "--contig_names=" + contigNamesArg, "--contig_lengths=" + contigLengthsArg, "--output_dir=" + outputDirArg, "--output_prefix=" + outputPrefix, "--is_log2_input=" + isLog2InputArg);
    executor.exec();
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor)

Example 4 with Resource

use of org.broadinstitute.hellbender.utils.io.Resource in project gatk by broadinstitute.

the class NativeUtils method loadLibraryFromClasspath.

/**
     * Loads a native library stored on our classpath by extracting it to a temporary location and invoking
     * {@link System#load} on it
     *
     * @param libraryPathInJar absolute path to the library file on the classpath
     * @return true if the library was extracted and loaded successfully, otherwise false
     */
public static boolean loadLibraryFromClasspath(final String libraryPathInJar) {
    Utils.nonNull(libraryPathInJar);
    Utils.validateArg(libraryPathInJar.startsWith("/"), "library path in jar must be absolute");
    try {
        final File extractedLibrary = IOUtils.writeTempResource(new Resource(libraryPathInJar, NativeUtils.class));
        extractedLibrary.deleteOnExit();
        System.load(extractedLibrary.getAbsolutePath());
    } catch (UnsatisfiedLinkError | UserException e) {
        return false;
    }
    return true;
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) UserException(org.broadinstitute.hellbender.exceptions.UserException) File(java.io.File)

Example 5 with Resource

use of org.broadinstitute.hellbender.utils.io.Resource in project gatk-protected by broadinstitute.

the class RCBSSegmenter method writeSegmentFile.

/**
     * Create a segmentation file using CBS in R.
     *
     * <p>https://www.bioconductor.org/packages/release/bioc/manuals/DNAcopy/man/DNAcopy.pdf</p>
     *
     * <p>Please see the above documentation for a more detailed description of the parameters.</p>
     *
     * <p>IMPORTANT:  There is no check that the weights have the same number of entries as the tnFile.</p>
     *
     * Wraps the call:
     * segment(x, weights = NULL, alpha = 0.01, nperm = 10000, p.method =
     *  c("hybrid", "perm"), min.width=2, kmax=25, nmin=200,
     *  eta=0.05, sbdry=NULL, trim = 0.025, undo.splits =
     *  c("none", "prune", "sdundo"), undo.prune=0.05,
     *  undo.SD=3, verbose=1)
     *
     * <p> Note that sbdry and verbose are unavailable through this interface. </p>
     *
     * @param sampleName Name of the sample being run through the segmenter.  Never {@code null}
     * @param tnFile Tangent-normalized targets file.  Never {@code null}
     * @param outputFile Full path to the outputted segment file.  Never {@code null}
     * @param log whether the tnFile input has already been put into log2CR.  Never {@code null}
     * @param minWidth minimum length for a segment
     * @param weightFile File containing weights for each target (doubles; one per line).  Must be the same length as what is in the tnFile (note that this is not
     *                enforced here).  Typically, 1/var(target) is what is used.  All values must be greater than 0.
     *                Use {@code null} if weighting is not desired.  These values should not be log space.
     */
public static void writeSegmentFile(final String sampleName, final String tnFile, final String outputFile, final Boolean log, final File weightFile, final double alpha, final int nperm, final PMethod pmethod, final int minWidth, final int kmax, final int nmin, final double eta, final double trim, final UndoSplits undoSplits, final double undoPrune, final int undoSD) {
    String logArg = log ? "TRUE" : "FALSE";
    final RScriptExecutor executor = new RScriptExecutor();
    executor.addScript(new Resource(R_SCRIPT, RCBSSegmenter.class));
    /*--args is needed for Rscript to recognize other arguments properly*/
    executor.addArgs("--args", "--sample_name=" + sampleName, "--targets_file=" + tnFile, "--output_file=" + outputFile, "--log2_input=" + logArg, "--min_width=" + String.valueOf(minWidth), "--alpha=" + String.valueOf(alpha), "--nperm=" + String.valueOf(nperm), "--pmethod=" + pmethod.toString(), "--kmax=" + String.valueOf(kmax), "--nmin=" + String.valueOf(nmin), "--eta=" + String.valueOf(eta), "--trim=" + String.valueOf(trim), "--undosplits=" + undoSplits.toString(), "--undoprune=" + String.valueOf(undoPrune), "--undoSD=" + String.valueOf(undoSD));
    if (weightFile != null) {
        final double[] weights = ParamUtils.readValuesFromFile(weightFile);
        // Check to make sure that no weights are zero.
        if (!DoubleStream.of(weights).allMatch(d -> d > 0 && !Double.isNaN(d) && Double.isFinite(d))) {
            throw new GATKException("A weight for a target was zero or less, which is not allowed.  If you truly want zero, you must remove the target from consideration.");
        }
        // Add the argument to specify weights
        executor.addArgs("--weights_file=" + weightFile.getAbsolutePath());
    }
    executor.exec();
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) ParamUtils(org.broadinstitute.hellbender.utils.param.ParamUtils) File(java.io.File) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) DoubleStream(java.util.stream.DoubleStream) Resource(org.broadinstitute.hellbender.utils.io.Resource) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Aggregations

Resource (org.broadinstitute.hellbender.utils.io.Resource)16 RScriptExecutor (org.broadinstitute.hellbender.utils.R.RScriptExecutor)14 File (java.io.File)7 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)3 UserException (org.broadinstitute.hellbender.exceptions.UserException)3 MetricsFile (htsjdk.samtools.metrics.MetricsFile)2 DoubleStream (java.util.stream.DoubleStream)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 FileNotFoundException (java.io.FileNotFoundException)1 PrintStream (java.io.PrintStream)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 CommandLineException (org.broadinstitute.barclay.argparser.CommandLineException)1 GATKReport (org.broadinstitute.hellbender.utils.report.GATKReport)1 ProcessController (org.broadinstitute.hellbender.utils.runtime.ProcessController)1