initial commit
[jalview.git] / forester / java / src / org / forester / msa / MafftOLD.java
1
2 package org.forester.msa;
3
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.util.List;
10
11 import org.forester.io.parsers.FastaParser;
12 import org.forester.util.ExternalProgram;
13 import org.forester.util.ForesterUtil;
14
15 public final class MafftOLD implements MsaInferrer {
16
17     private String       _error;
18     private int          _exit_code;
19     private final String _path_to_prg;
20
21     public static MsaInferrer createInstance( final String path_to_prg ) {
22         return new MafftOLD( path_to_prg );
23     }
24
25     private MafftOLD( final String path_to_prg ) {
26         _path_to_prg = new String( path_to_prg );
27         init();
28     }
29
30     @Override
31     public Object clone() {
32         throw new NoSuchMethodError();
33     }
34
35     public String getErrorDescription() {
36         return _error;
37     }
38
39     public int getExitCode() {
40         return _exit_code;
41     }
42
43     public Msa infer( final File path_to_input_seqs, final List<String> opts ) throws IOException, InterruptedException {
44         init();
45         final String[] my_opts = new String[ opts.size() + 1 ];
46         for( int i = 0; i < opts.size(); i++ ) {
47             my_opts[ i ] = opts.get( i );
48         }
49         my_opts[ opts.size() ] = path_to_input_seqs.getAbsolutePath();
50         final ExternalProgram mafft_prg = new ExternalProgram( _path_to_prg );
51         mafft_prg.launch( my_opts );
52         // _exit_code = mafft_prg.waitFor();
53         // if ( _exit_code != 0 ) {
54         //    throw new IOException( "MAFFT failed, exit code: " + _exit_code );
55         // }
56         final BufferedReader r = new BufferedReader( new InputStreamReader( mafft_prg.getErrorStream() ) );
57         final StringBuffer error_sb = new StringBuffer();
58         String line = null;
59         while ( ( line = r.readLine() ) != null ) {
60             error_sb.append( line );
61             error_sb.append( ForesterUtil.LINE_SEPARATOR );
62         }
63         r.close();
64         if ( error_sb.length() > 0 ) {
65             _error = error_sb.toString();
66             throw new IOException( "MAFFT failed" );
67         }
68         final InputStream is = mafft_prg.getInputStream();
69         final Msa msa = FastaParser.parseMsa( is );
70         is.close();
71         return msa;
72     }
73
74     private void init() {
75         _error = null;
76         _exit_code = -100;
77     }
78 }