in progress
[jalview.git] / forester / java / src / org / forester / msa / Mafft.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.msa;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.io.parsers.FastaParser;
34 import org.forester.util.ForesterUtil;
35 import org.forester.util.SystemCommandExecutor;
36
37 public final class Mafft implements MsaInferrer {
38
39     private final static String DEFAULT_PARAMETERS = "--maxiterate 1000 --localpair";
40     private String              _error;
41     private int                 _exit_code;
42     private final String        _path_to_prg;
43
44     public static MsaInferrer createInstance() {
45         return createInstance( getPathToCmd() );
46     }
47
48     public static MsaInferrer createInstance( final String path_to_prg ) {
49         return new Mafft( path_to_prg );
50     }
51
52     private static String getPathToCmd() {
53         //TODO this needs to come from env variable, etc.
54         //FIXME ..
55         //should not be in this class!
56         String path = "";
57         final String os = ForesterUtil.OS_NAME.toLowerCase();
58         if ( ( os.indexOf( "mac" ) >= 0 ) && ( os.indexOf( "os" ) > 0 ) ) {
59             path = "/usr/local/bin/mafft";
60         }
61         else if ( os.indexOf( "win" ) >= 0 ) {
62             path = "C:\\Program Files\\mafft-win\\mafft.bat";
63         }
64         else {
65             path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-6.864-without-extensions/scripts/mafft";
66         }
67         return path;
68     }
69
70     public static boolean isInstalled() {
71         return SystemCommandExecutor.isExecuteableFile( new File( getPathToCmd() ) );
72     }
73
74     private Mafft( final String path_to_prg ) {
75         if ( !SystemCommandExecutor.isExecuteableFile( new File( path_to_prg ) ) ) {
76             throw new IllegalArgumentException( "cannot execute MAFFT via [" + path_to_prg + "]" );
77         }
78         _path_to_prg = new String( path_to_prg );
79         init();
80     }
81
82     public static String getDefaultParameters() {
83         return DEFAULT_PARAMETERS;
84     }
85
86     @Override
87     public Object clone() {
88         throw new NoSuchMethodError();
89     }
90
91     @Override
92     public String getErrorDescription() {
93         return _error;
94     }
95
96     @Override
97     public int getExitCode() {
98         return _exit_code;
99     }
100
101     @Override
102     public Msa infer( final File path_to_input_seqs, final List<String> opts ) throws IOException, InterruptedException {
103         init();
104         final List<String> my_opts = new ArrayList<String>();
105         my_opts.add( _path_to_prg );
106         for( int i = 0; i < opts.size(); i++ ) {
107             my_opts.add( opts.get( i ) );
108         }
109         my_opts.add( path_to_input_seqs.getAbsolutePath() );
110         final SystemCommandExecutor command_executor = new SystemCommandExecutor( my_opts );
111         final int _exit_code = command_executor.executeCommand();
112         if ( _exit_code != 0 ) {
113             throw new IOException( "MAFFT program failed, exit code: " + _exit_code + ", command: " + my_opts );
114         }
115         final StringBuilder stdout = command_executor.getStandardOutputFromCommand();
116         final StringBuilder stderr = command_executor.getStandardErrorFromCommand();
117         if ( ( stdout == null ) || ( stdout.length() < 2 ) ) {
118             throw new IOException( "MAFFT program did not produce any output, command: " + my_opts );
119         }
120         _error = stderr.toString();
121         final Msa msa = FastaParser.parseMsa( stdout.toString() );
122         return msa;
123     }
124
125     private void init() {
126         _error = null;
127         _exit_code = -100;
128     }
129 }