From c6db1dfeb009e379b3b53425b65cc4d11f49c1e8 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Wed, 18 Jan 2012 01:28:30 +0000 Subject: [PATCH] in progress --- .../src/org/forester/archaeopteryx/Constants.java | 4 +- .../archaeopteryx/tools/PhylogeneticInferrer.java | 29 +++++---- forester/java/src/org/forester/msa/Mafft.java | 28 +++++---- forester/java/src/org/forester/test/Test.java | 2 +- .../org/forester/util/SystemCommandExecutor.java | 63 ++++++++++---------- 5 files changed, 64 insertions(+), 62 deletions(-) diff --git a/forester/java/src/org/forester/archaeopteryx/Constants.java b/forester/java/src/org/forester/archaeopteryx/Constants.java index 7edf5b0..338d2f3 100644 --- a/forester/java/src/org/forester/archaeopteryx/Constants.java +++ b/forester/java/src/org/forester/archaeopteryx/Constants.java @@ -36,8 +36,8 @@ import org.forester.util.ForesterConstants; public final class Constants { - final static boolean __ALLOW_PHYLOGENETIC_INFERENCE = false; - public final static boolean __RELEASE = true; // TODO remove me + final static boolean __ALLOW_PHYLOGENETIC_INFERENCE = true; + public final static boolean __RELEASE = false; // TODO remove me public final static boolean __SNAPSHOT_RELEASE = false; // TODO remove me public final static boolean __SYNTH_LF = false; // TODO remove me public final static String PRG_NAME = "Archaeopteryx"; diff --git a/forester/java/src/org/forester/archaeopteryx/tools/PhylogeneticInferrer.java b/forester/java/src/org/forester/archaeopteryx/tools/PhylogeneticInferrer.java index a44fe96..1d8309f 100644 --- a/forester/java/src/org/forester/archaeopteryx/tools/PhylogeneticInferrer.java +++ b/forester/java/src/org/forester/archaeopteryx/tools/PhylogeneticInferrer.java @@ -85,25 +85,17 @@ public class PhylogeneticInferrer implements Runnable { _options = options; } - private Msa inferMsa() throws IOException { - final File temp_seqs_file = File.createTempFile( "aptx", ".fasta" ); + private Msa inferMsa() throws IOException, InterruptedException { + final File temp_seqs_file = File.createTempFile( "__msa__temp__", ".fasta" ); + System.out.println(); System.out.println( "temp file: " + temp_seqs_file ); + System.out.println(); //final File temp_seqs_file = new File( _options.getTempDir() + ForesterUtil.FILE_SEPARATOR + "s.fasta" ); final BufferedWriter writer = new BufferedWriter( new FileWriter( temp_seqs_file ) ); SequenceWriter.writeSeqs( _seqs, writer, SEQ_FORMAT.FASTA, 100 ); writer.close(); final List opts = processMafftOptions(); - Msa msa = null; - try { - msa = runMAFFT( temp_seqs_file, opts ); - } - catch ( final InterruptedException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // copy aln file to intermediate dir file - // delete temp seqs file - return msa; + return runMAFFT( temp_seqs_file, opts ); } private List processMafftOptions() { @@ -156,7 +148,7 @@ public class PhylogeneticInferrer implements Runnable { return phy; } - private void infer() { + private void infer() throws InterruptedException { //_mf.getMainPanel().getCurrentTreePanel().setWaitCursor(); if ( ( _msa == null ) && ( _seqs == null ) ) { throw new IllegalArgumentException( "cannot run phylogenetic analysis with null msa and seq array" ); @@ -231,7 +223,14 @@ public class PhylogeneticInferrer implements Runnable { @Override public void run() { - infer(); + try { + infer(); + } + catch ( InterruptedException e ) { + // TODO need to handle this exception SOMEHOW! + // TODO Auto-generated catch block + e.printStackTrace(); + } } private Msa runMAFFT( final File input_seqs, final List opts ) throws IOException, InterruptedException { diff --git a/forester/java/src/org/forester/msa/Mafft.java b/forester/java/src/org/forester/msa/Mafft.java index b78cb32..81e43b2 100644 --- a/forester/java/src/org/forester/msa/Mafft.java +++ b/forester/java/src/org/forester/msa/Mafft.java @@ -41,12 +41,18 @@ public final class Mafft implements MsaInferrer { private int _exit_code; private final String _path_to_prg; + public static MsaInferrer createInstance() { + return createInstance( getPathToCmd() ); + } + public static MsaInferrer createInstance( final String path_to_prg ) { return new Mafft( path_to_prg ); } private static String getPathToCmd() { //TODO this needs to come from env variable, etc. + //FIXME .. + //should not be in this class! String path = ""; final String os = ForesterUtil.OS_NAME.toLowerCase(); if ( ( os.indexOf( "mac" ) >= 0 ) && ( os.indexOf( "os" ) > 0 ) ) { @@ -56,7 +62,7 @@ public final class Mafft implements MsaInferrer { path = "C:\\Program Files\\mafft-win\\mafft.bat"; } else { - path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-6.832-without-extensions/scripts/mafft"; + path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-6.864-without-extensions/scripts/mafft"; } return path; } @@ -65,10 +71,6 @@ public final class Mafft implements MsaInferrer { return SystemCommandExecutor.isExecuteableFile( new File( getPathToCmd() ) ); } - public static MsaInferrer createInstance() { - return createInstance( getPathToCmd() ); - } - private Mafft( final String path_to_prg ) { if ( !SystemCommandExecutor.isExecuteableFile( new File( path_to_prg ) ) ) { throw new IllegalArgumentException( "cannot execute MAFFT via [" + path_to_prg + "]" ); @@ -105,16 +107,16 @@ public final class Mafft implements MsaInferrer { my_opts.add( opts.get( i ) ); } my_opts.add( path_to_input_seqs.getAbsolutePath() ); - final SystemCommandExecutor commandExecutor = new SystemCommandExecutor( my_opts ); - final int _exit_code = commandExecutor.executeCommand(); + final SystemCommandExecutor command_executor = new SystemCommandExecutor( my_opts ); + final int _exit_code = command_executor.executeCommand(); if ( _exit_code != 0 ) { - throw new IOException( "MAFFT failed, exit code: " + _exit_code ); + throw new IOException( "MAFFT program failed, exit code: " + _exit_code + ", command: " + my_opts ); + } + final StringBuilder stdout = command_executor.getStandardOutputFromCommand(); + final StringBuilder stderr = command_executor.getStandardErrorFromCommand(); + if ( stdout == null || stdout.length() < 2 ) { + throw new IOException( "MAFFT program did not produce any output, command: " + my_opts ); } - final StringBuilder stdout = commandExecutor.getStandardOutputFromCommand(); - final StringBuilder stderr = commandExecutor.getStandardErrorFromCommand(); - System.out.println( stdout ); - System.out.println(); - System.out.println( stderr ); _error = stderr.toString(); final Msa msa = FastaParser.parseMsa( stdout.toString() ); return msa; diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index f380239..da2c06c 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -8229,7 +8229,7 @@ public final class Test { opts.add( "--quiet" ); Msa msa = null; final MsaInferrer mafft = Mafft.createInstance(); - msa = mafft.infer( new File( PATH_TO_TEST_DATA + "ncbi.fasta" ), opts ); + msa = mafft.infer( new File( PATH_TO_TEST_DATA + "ncbi_sn.fasta" ), opts ); if ( ( msa == null ) || ( msa.getLength() < 10 ) || ( msa.getNumberOfSequences() != 19 ) ) { return false; } diff --git a/forester/java/src/org/forester/util/SystemCommandExecutor.java b/forester/java/src/org/forester/util/SystemCommandExecutor.java index 5b32dc4..bb87989 100644 --- a/forester/java/src/org/forester/util/SystemCommandExecutor.java +++ b/forester/java/src/org/forester/util/SystemCommandExecutor.java @@ -8,7 +8,6 @@ * * http://devdaily.com/java/java-processbuilder-process-system-exec * - * * Copyright 2010 alvin j. alexander, devdaily.com. * * This program is free software: you can redistribute it and/or modify it under @@ -23,7 +22,7 @@ * You should have received a copy of the GNU Lesser Public License along with * this program. If not, see . * - * Please ee the following page for the LGPL license: + * Please see the following page for the LGPL license: * http://www.gnu.org/licenses/lgpl.txt * */ @@ -38,10 +37,11 @@ import java.util.List; public class SystemCommandExecutor { - private final List commandInformation; - private final String adminPassword; - private ThreadedStreamHandler inputStreamHandler; - private ThreadedStreamHandler errorStreamHandler; + private final List _command_information; + private final String _admin_password; + private ThreadedStreamHandler _input_stream_handler; + private ThreadedStreamHandler _error_stream_handler; + private final static boolean DEBUG = false; /** * Pass in the system command you want to run as a List of Strings, as shown here: @@ -59,15 +59,15 @@ public class SystemCommandExecutor { * working to the point where it won't hang when the given password is * wrong. * - * @param commandInformation The command you want to run. + * @param command_information The command you want to run. */ - public SystemCommandExecutor( final List commandInformation ) { - if ( ( commandInformation == null ) || commandInformation.isEmpty() ) { + public SystemCommandExecutor( final List command_information ) { + if ( ( command_information == null ) || command_information.isEmpty() ) { throw new IllegalArgumentException( "The commandInformation is required." ); } - checkCmdFile( new File( commandInformation.get( 0 ) ) ); - this.commandInformation = commandInformation; - adminPassword = null; + checkCmdFile( new File( command_information.get( 0 ) ) ); + _command_information = command_information; + _admin_password = null; } public static boolean isExecuteableFile( final File path_to_cmd_f ) { @@ -83,7 +83,7 @@ public class SystemCommandExecutor { return true; } - private void checkCmdFile( final File path_to_cmd_f ) { + private static void checkCmdFile( final File path_to_cmd_f ) { if ( !path_to_cmd_f.exists() ) { throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] does not exist" ); } @@ -96,9 +96,12 @@ public class SystemCommandExecutor { } public int executeCommand() throws IOException, InterruptedException { - int exitValue = -99; + int exit_value = -99; try { - final ProcessBuilder pb = new ProcessBuilder( commandInformation ); + final ProcessBuilder pb = new ProcessBuilder( _command_information ); + if ( DEBUG ) { + System.out.println( "command_information=" + _command_information ); + } final Process process = pb.start(); // you need this if you're going to write something to the command's input stream // (such as when invoking the 'sudo' command, and it prompts you for a password). @@ -111,44 +114,42 @@ public class SystemCommandExecutor { // these need to run as java threads to get the standard output and error from the command. // the inputstream handler gets a reference to our stdOutput in case we need to write // something to it, such as with the sudo command - inputStreamHandler = new ThreadedStreamHandler( inputStream, stdOutput, adminPassword ); - errorStreamHandler = new ThreadedStreamHandler( errorStream ); + _input_stream_handler = new ThreadedStreamHandler( inputStream, stdOutput, _admin_password ); + _error_stream_handler = new ThreadedStreamHandler( errorStream ); // TODO the inputStreamHandler has a nasty side-effect of hanging if the given password is wrong; fix it - inputStreamHandler.start(); - errorStreamHandler.start(); + _input_stream_handler.start(); + _error_stream_handler.start(); // TODO a better way to do this? - exitValue = process.waitFor(); + exit_value = process.waitFor(); // TODO a better way to do this? - inputStreamHandler.interrupt(); - errorStreamHandler.interrupt(); - inputStreamHandler.join(); - errorStreamHandler.join(); + _input_stream_handler.interrupt(); + _error_stream_handler.interrupt(); + _input_stream_handler.join(); + _error_stream_handler.join(); } catch ( final IOException e ) { - // TODO deal with this here, or just throw it? throw e; } catch ( final InterruptedException e ) { // generated by process.waitFor() call - // TODO deal with this here, or just throw it? throw e; } - finally { - return exitValue; - } + // finally { + return exit_value; + // } } /** * Get the standard error (stderr) from the command you just exec'd. */ public StringBuilder getStandardErrorFromCommand() { - return errorStreamHandler.getOutputBuffer(); + return _error_stream_handler.getOutputBuffer(); } /** * Get the standard output (stdout) from the command you just exec'd. */ public StringBuilder getStandardOutputFromCommand() { - return inputStreamHandler.getOutputBuffer(); + return _input_stream_handler.getOutputBuffer(); } } -- 1.7.10.2