in progress
[jalview.git] / forester / java / src / org / forester / msa / ClustalOmega.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.sequence.Sequence;
35 import org.forester.util.SystemCommandExecutor;
36
37 public final class ClustalOmega extends MsaInferrer {
38
39     private final static String DEFAULT_PARAMETERS = "";
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 ) throws IOException {
45         return new ClustalOmega( path_to_prg );
46     }
47
48     private ClustalOmega( final String path_to_prg ) throws IOException {
49         if ( !isInstalled( path_to_prg ) ) {
50             throw new IOException( "cannot execute Clustal Omega with \"" + path_to_prg + "\"" );
51         }
52         _path_to_prg = new String( path_to_prg );
53         init();
54     }
55
56     public static String getDefaultParameters() {
57         return DEFAULT_PARAMETERS;
58     }
59
60     @Override
61     public String getErrorDescription() {
62         return _error;
63     }
64
65     @Override
66     public int getExitCode() {
67         return _exit_code;
68     }
69
70     @Override
71     public Msa infer( final File path_to_input_seqs, final List<String> opts ) throws IOException, InterruptedException {
72         init();
73         final List<String> my_opts = new ArrayList<String>();
74         my_opts.add( _path_to_prg );
75         for( int i = 0; i < opts.size(); i++ ) {
76             my_opts.add( opts.get( i ) );
77         }
78         my_opts.add( path_to_input_seqs.getAbsolutePath() );
79         final SystemCommandExecutor command_executor = new SystemCommandExecutor( my_opts );
80         final int _exit_code = command_executor.executeCommand();
81         final StringBuilder stderr = command_executor.getStandardErrorFromCommand();
82         _error = stderr.toString();
83         if ( _exit_code != 0 ) {
84             throw new IOException( "Clustal Omega program failed, exit code: " + _exit_code + "\nCommand:\n" + my_opts
85                     + "\nError:\n" + stderr );
86         }
87         final StringBuilder stdout = command_executor.getStandardOutputFromCommand();
88         if ( ( stdout == null ) || ( stdout.length() < 2 ) ) {
89             throw new IOException( "Clustal Omega program did not produce any output\nCommand:\n" + my_opts
90                     + "\nError:\n" + stderr );
91         }
92         final Msa msa = FastaParser.parseMsa( stdout.toString() );
93         return msa;
94     }
95
96     private void init() {
97         _error = null;
98         _exit_code = -100;
99     }
100
101     @Override
102     public Msa infer( final List<Sequence> seqs, final List<String> opts ) throws IOException, InterruptedException {
103         // TODO Auto-generated method stub
104         return null;
105     }
106 }