Skip to content

Instantly share code, notes, and snippets.

@jctoledo
Created December 18, 2013 02:40
Show Gist options
  • Save jctoledo/8016481 to your computer and use it in GitHub Desktop.
Save jctoledo/8016481 to your computer and use it in GitHub Desktop.
protected String execute(Sequence aSequence, String[] commands)
throws InvalidSequenceException, IOException {
String returnMe = "";
if (aSequence.getLength() == 0) {
throw new InvalidSequenceException(
"The sequence provided to execute RNAFold was empty!");
}
String[] cmdArr = new String[commands.length + 1];
System.arraycopy(new String[] { "RNAfold" }, 0, cmdArr, 0, 1);
System.arraycopy(commands, 0, cmdArr, 1, commands.length);
Process p = Runtime.getRuntime().exec(cmdArr);
InputStream output_stream = p.getInputStream();
InputStream seq_stream = PdbHelper.makeInputStreamFromString(aSequence
.getSequenceString());
OutputStream out_stream = p.getOutputStream();
InputStream error_stream = p.getErrorStream();
ByteArrayOutputStream out_baos = new ByteArrayOutputStream();
ByteArrayOutputStream error_baos = new ByteArrayOutputStream();
try {
// A thread for handling the input to RNAFold
Thread stdInThread = ProcessRunner.bindStreams(seq_stream,
out_stream, seq_stream + " feed", true);
// A thread for handling the output of RNAFold
Thread stdOutThread = ProcessRunner.bindStreams(output_stream,
out_baos, "writer", false);
try {
int status = p.waitFor();
if (status != 0) {
log.error("Something bad happened!\n!");
System.exit(-1);
}
} catch (InterruptedException e) {
log.error("wating problem\n", e);
System.exit(-1);
}
returnMe = out_baos.toString();
if (returnMe.length() > 0) {
return returnMe;
}
stdInThread.join(10*1000);
stdOutThread.join(10*1000);
} catch (InterruptedException e) {
log.error("problem\n", e);
} finally {
try {
output_stream.close();
seq_stream.close();
out_stream.close();
out_baos.close();
} catch (IOException e) {
log.error("io problem\n", e);
}
p.destroy();
}
return returnMe;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment