initial commit
[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( final String path_to_prg ) {
45         return new Mafft( path_to_prg );
46     }
47
48     private static String getPathToCmd() {
49         //TODO this needs to come from env variable, etc.
50         String path = "";
51         final String os = ForesterUtil.OS_NAME.toLowerCase();
52         if ( ( os.indexOf( "mac" ) >= 0 ) && ( os.indexOf( "os" ) > 0 ) ) {
53             path = "/usr/local/bin/mafft";
54         }
55         else if ( os.indexOf( "win" ) >= 0 ) {
56             path = "C:\\Program Files\\mafft-win\\mafft.bat";
57         }
58         else {
59             path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-6.832-without-extensions/scripts/mafft";
60         }
61         return path;
62     }
63
64     public static boolean isInstalled() {
65         return SystemCommandExecutor.isExecuteableFile( new File( getPathToCmd() ) );
66     }
67
68     public static MsaInferrer createInstance() {
69         return createInstance( getPathToCmd() );
70     }
71
72     private Mafft( final String path_to_prg ) {
73         if ( !SystemCommandExecutor.isExecuteableFile( new File( path_to_prg ) ) ) {
74             throw new IllegalArgumentException( "cannot execute MAFFT via [" + path_to_prg + "]" );
75         }
76         _path_to_prg = new String( path_to_prg );
77         init();
78     }
79
80     public static String getDefaultParameters() {
81         return DEFAULT_PARAMETERS;
82     }
83
84     @Override
85     public Object clone() {
86         throw new NoSuchMethodError();
87     }
88
89     public String getErrorDescription() {
90         return _error;
91     }
92
93     public int getExitCode() {
94         return _exit_code;
95     }
96
97     public Msa infer( final File path_to_input_seqs, final List<String> opts ) throws IOException, InterruptedException {
98         init();
99         final List<String> my_opts = new ArrayList<String>();
100         my_opts.add( _path_to_prg );
101         for( int i = 0; i < opts.size(); i++ ) {
102             my_opts.add( opts.get( i ) );
103         }
104         my_opts.add( path_to_input_seqs.getAbsolutePath() );
105         final SystemCommandExecutor commandExecutor = new SystemCommandExecutor( my_opts );
106         final int _exit_code = commandExecutor.executeCommand();
107         if ( _exit_code != 0 ) {
108             throw new IOException( "MAFFT failed, exit code: " + _exit_code );
109         }
110         final StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
111         final StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();
112         System.out.println( stdout );
113         System.out.println();
114         System.out.println( stderr );
115         _error = stderr.toString();
116         final Msa msa = FastaParser.parseMsa( stdout.toString() );
117         return msa;
118     }
119
120     private void init() {
121         _error = null;
122         _exit_code = -100;
123     }
124 }