X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fmsa%2FMafft.java;h=ec72c0d01e5a068a7951ab6dd65f46f877976483;hb=ea402c168ecf73c1824d023a91331494939f2641;hp=81e43b28c11aa34557eb5a27990c359502a0aa86;hpb=c6db1dfeb009e379b3b53425b65cc4d11f49c1e8;p=jalview.git diff --git a/forester/java/src/org/forester/msa/Mafft.java b/forester/java/src/org/forester/msa/Mafft.java index 81e43b2..ec72c0d 100644 --- a/forester/java/src/org/forester/msa/Mafft.java +++ b/forester/java/src/org/forester/msa/Mafft.java @@ -25,55 +25,33 @@ package org.forester.msa; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.forester.io.parsers.FastaParser; -import org.forester.util.ForesterUtil; +import org.forester.io.writers.SequenceWriter; +import org.forester.io.writers.SequenceWriter.SEQ_FORMAT; +import org.forester.sequence.Sequence; import org.forester.util.SystemCommandExecutor; -public final class Mafft implements MsaInferrer { +public final class Mafft extends MsaInferrer { private final static String DEFAULT_PARAMETERS = "--maxiterate 1000 --localpair"; private String _error; 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 ) { + public static MsaInferrer createInstance( final String path_to_prg ) throws IOException { 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 ) ) { - path = "/usr/local/bin/mafft"; - } - else if ( os.indexOf( "win" ) >= 0 ) { - path = "C:\\Program Files\\mafft-win\\mafft.bat"; - } - else { - path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-6.864-without-extensions/scripts/mafft"; - } - return path; - } - - public static boolean isInstalled() { - return SystemCommandExecutor.isExecuteableFile( new File( 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 + "]" ); + private Mafft( final String path_to_prg ) throws IOException { + if ( !isInstalled( path_to_prg ) ) { + throw new IOException( "cannot execute MAFFT with \"" + path_to_prg + "\"" ); } _path_to_prg = new String( path_to_prg ); init(); @@ -84,11 +62,6 @@ public final class Mafft implements MsaInferrer { } @Override - public Object clone() { - throw new NoSuchMethodError(); - } - - @Override public String getErrorDescription() { return _error; } @@ -99,6 +72,18 @@ public final class Mafft implements MsaInferrer { } @Override + public Msa infer( final List seqs, final List opts ) throws IOException, InterruptedException { + final File file = File.createTempFile( "__mafft_input_", ".fasta" ); + file.deleteOnExit(); + final BufferedWriter writer = new BufferedWriter( new FileWriter( file ) ); + SequenceWriter.writeSeqs( seqs, writer, SEQ_FORMAT.FASTA, 100 ); + writer.close(); + final Msa msa = infer( file, opts ); + file.delete(); + return msa; + } + + @Override public Msa infer( final File path_to_input_seqs, final List opts ) throws IOException, InterruptedException { init(); final List my_opts = new ArrayList(); @@ -109,15 +94,17 @@ public final class Mafft implements MsaInferrer { my_opts.add( path_to_input_seqs.getAbsolutePath() ); final SystemCommandExecutor command_executor = new SystemCommandExecutor( my_opts ); final int _exit_code = command_executor.executeCommand(); + final StringBuilder stderr = command_executor.getStandardErrorFromCommand(); + _error = stderr.toString(); if ( _exit_code != 0 ) { - throw new IOException( "MAFFT program failed, exit code: " + _exit_code + ", command: " + my_opts ); + throw new IOException( "MAFFT program failed, exit code: " + _exit_code + "\nCommand:\n" + my_opts + + "\nError:\n" + stderr ); } 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 ); + if ( ( stdout == null ) || ( stdout.length() < 2 ) ) { + throw new IOException( "MAFFT program did not produce any output\nCommand:\n" + my_opts + "\nError:\n" + + stderr ); } - _error = stderr.toString(); final Msa msa = FastaParser.parseMsa( stdout.toString() ); return msa; }