use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class ClassUtils method makeInstancesOfSubclasses.
/**
* Finds and creates objects of all concrete subclasses of the given class in the package.
* The public no-arg constructor is called to create the objects.
*
* GATKException is thrown if creation of any object fails.
* @param clazz class to be instantiated
* @param pack package in which the class will be searched for
*/
@SuppressWarnings("unchecked")
public static <T> List<T> makeInstancesOfSubclasses(final Class<? extends T> clazz, final Package pack) {
Utils.nonNull(clazz, "class");
Utils.nonNull(pack, "package");
final ClassFinder finder = new ClassFinder();
finder.find(pack.getName(), clazz);
final Set<Class<?>> classes = finder.getClasses();
final List<T> results = new ArrayList<>(classes.size());
for (final Class<?> found : classes) {
if (canMakeInstances(found)) {
try {
results.add((T) found.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new GATKException("Problem making an instance of " + found + " Do check that the class has a non-arg constructor", e);
}
}
}
return results;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class TextFormattingUtils method getWordStarts.
/**
* Returns the word starting positions within line, excluding the first position 0.
* The returned list is compatible with splitFixedWidth.
* @param line Text to parse.
* @return the word starting positions within line, excluding the first position 0.
*/
public static List<Integer> getWordStarts(String line) {
if (line == null)
throw new GATKException("line is null");
List<Integer> starts = new ArrayList<>();
int stop = line.length();
for (int i = 1; i < stop; i++) if (Character.isWhitespace(line.charAt(i - 1)))
if (!Character.isWhitespace(line.charAt(i)))
starts.add(i);
return starts;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class ProcessController method exec.
/**
* Executes a command line program with the settings and waits for it to return,
* processing the output on a background thread.
*
* @param settings Settings to be run.
* @return The output of the command.
*/
public ProcessOutput exec(ProcessSettings settings) {
Utils.validate(!destroyed, "This controller was destroyed");
ProcessBuilder builder = new ProcessBuilder(settings.getCommand());
builder.directory(settings.getDirectory());
Map<String, String> settingsEnvironment = settings.getEnvironment();
if (settingsEnvironment != null) {
Map<String, String> builderEnvironment = builder.environment();
builderEnvironment.clear();
builderEnvironment.putAll(settingsEnvironment);
}
builder.redirectErrorStream(settings.isRedirectErrorStream());
StreamOutput stdout = null;
StreamOutput stderr = null;
try {
synchronized (toCapture) {
process = builder.start();
}
running.add(this);
} catch (IOException e) {
String message = String.format("Unable to start command: %s\nReason: %s", StringUtils.join(builder.command(), " "), e.getMessage());
throw new GATKException(message);
}
int exitCode;
try {
// Notify the background threads to start capturing.
synchronized (toCapture) {
toCapture.put(ProcessStream.Stdout, new CapturedStreamOutput(settings.getStdoutSettings(), process.getInputStream(), System.out));
toCapture.put(ProcessStream.Stderr, new CapturedStreamOutput(settings.getStderrSettings(), process.getErrorStream(), System.err));
toCapture.notifyAll();
}
// Write stdin content
InputStreamSettings stdinSettings = settings.getStdinSettings();
Set<StreamLocation> streamLocations = stdinSettings.getStreamLocations();
if (!streamLocations.isEmpty()) {
try {
OutputStream stdinStream = process.getOutputStream();
for (StreamLocation location : streamLocations) {
InputStream inputStream;
switch(location) {
case Buffer:
inputStream = new ByteArrayInputStream(stdinSettings.getInputBuffer());
break;
case File:
try {
inputStream = FileUtils.openInputStream(stdinSettings.getInputFile());
} catch (IOException e) {
throw new UserException.BadInput(e.getMessage());
}
break;
case Standard:
inputStream = System.in;
break;
default:
throw new GATKException("Unexpected stream location: " + location);
}
try {
IOUtils.copy(inputStream, stdinStream);
} finally {
if (location != StreamLocation.Standard)
IOUtils.closeQuietly(inputStream);
}
}
stdinStream.flush();
} catch (IOException e) {
throw new GATKException("Error writing to stdin on command: " + StringUtils.join(builder.command(), " "), e);
}
}
// Wait for the process to complete.
try {
process.getOutputStream().close();
process.waitFor();
} catch (IOException e) {
throw new GATKException("Unable to close stdin on command: " + StringUtils.join(builder.command(), " "), e);
} catch (InterruptedException e) {
throw new GATKException("Process interrupted", e);
} finally {
while (!destroyed && stdout == null || stderr == null) {
synchronized (fromCapture) {
if (fromCapture.containsKey(ProcessStream.Stdout))
stdout = fromCapture.remove(ProcessStream.Stdout);
if (fromCapture.containsKey(ProcessStream.Stderr))
stderr = fromCapture.remove(ProcessStream.Stderr);
try {
if (stdout == null || stderr == null)
fromCapture.wait();
} catch (InterruptedException e) {
// Log the error, ignore the interrupt and wait patiently
// for the OutputCaptures to (via finally) return their
// stdout and stderr.
logger.error(e);
}
}
}
if (destroyed) {
if (stdout == null)
stdout = StreamOutput.EMPTY;
if (stderr == null)
stderr = StreamOutput.EMPTY;
}
}
} finally {
synchronized (toCapture) {
exitCode = process.exitValue();
process = null;
}
running.remove(this);
}
return new ProcessOutput(exitCode, stdout, stderr);
}
use of org.broadinstitute.hellbender.exceptions.GATKException 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();
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class RScriptExecutor method exec.
public boolean exec() {
if (!RSCRIPT_EXISTS) {
if (!ignoreExceptions) {
throw new UserException.CannotExecuteRScript(RSCRIPT_MISSING_MESSAGE);
} else {
logger.warn("Skipping: " + getApproximateCommandLine());
return false;
}
}
List<File> tempFiles = new ArrayList<>();
try {
File tempLibSourceDir = IOUtils.tempDir("RlibSources.", "");
File tempLibInstallationDir = IOUtils.tempDir("Rlib.", "");
tempFiles.add(tempLibSourceDir);
tempFiles.add(tempLibInstallationDir);
StringBuilder expression = new StringBuilder("tempLibDir = '").append(tempLibInstallationDir).append("';");
if (!this.libraries.isEmpty()) {
List<String> tempLibraryPaths = new ArrayList<>();
for (RScriptLibrary library : this.libraries) {
File tempLibrary = library.writeLibrary(tempLibSourceDir);
tempFiles.add(tempLibrary);
tempLibraryPaths.add(tempLibrary.getAbsolutePath());
}
expression.append("install.packages(");
expression.append("pkgs=c('").append(StringUtils.join(tempLibraryPaths, "', '")).append("'), lib=tempLibDir, repos=NULL, type='source', ");
// Install faster by eliminating cruft.
expression.append("INSTALL_opts=c('--no-libs', '--no-data', '--no-help', '--no-demo', '--no-exec')");
expression.append(");");
for (RScriptLibrary library : this.libraries) {
expression.append("library('").append(library.getLibraryName()).append("', lib.loc=tempLibDir);");
}
}
for (Resource script : this.scriptResources) {
File tempScript = IOUtils.writeTempResource(script);
tempFiles.add(tempScript);
expression.append("source('").append(tempScript.getAbsolutePath()).append("');");
}
for (File script : this.scriptFiles) {
expression.append("source('").append(script.getAbsolutePath()).append("');");
}
String[] cmd = new String[this.args.size() + 3];
int i = 0;
cmd[i++] = RSCRIPT_BINARY;
cmd[i++] = "-e";
cmd[i++] = expression.toString();
for (String arg : this.args) cmd[i++] = arg;
ProcessSettings processSettings = new ProcessSettings(cmd);
//if debug is enabled, output the stdout and stdder, otherwise capture it to a buffer
if (logger.isDebugEnabled()) {
processSettings.getStdoutSettings().printStandard(true);
processSettings.getStderrSettings().printStandard(true);
} else {
processSettings.getStdoutSettings().setBufferSize(8192);
processSettings.getStderrSettings().setBufferSize(8192);
}
ProcessController controller = ProcessController.getThreadLocal();
if (logger.isDebugEnabled()) {
logger.debug("Executing:");
for (String arg : cmd) logger.debug(" " + arg);
}
ProcessOutput po = controller.exec(processSettings);
int exitValue = po.getExitValue();
logger.debug("Result: " + exitValue);
if (exitValue != 0) {
StringBuilder message = new StringBuilder();
message.append(String.format("\nRscript exited with %d\nCommand Line: %s", exitValue, String.join(" ", cmd)));
//if debug was enabled the stdout/error were already output somewhere
if (!logger.isDebugEnabled()) {
message.append(String.format("\nStdout: %s\nStderr: %s", po.getStdout().getBufferString(), po.getStderr().getBufferString()));
}
throw new RScriptExecutorException(message.toString());
}
return true;
} catch (GATKException e) {
if (!ignoreExceptions) {
throw e;
} else {
logger.warn(e.getMessage());
return false;
}
} finally {
for (File temp : tempFiles) FileUtils.deleteQuietly(temp);
}
}
Aggregations