2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.msa;
28 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.forester.io.parsers.FastaParser;
36 import org.forester.io.writers.SequenceWriter;
37 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
38 import org.forester.sequence.Sequence;
39 import org.forester.util.SystemCommandExecutor;
41 public final class Mafft extends MsaInferrer {
43 private final static String DEFAULT_PARAMETERS = "--maxiterate 1000 --localpair";
44 private String _error;
45 private int _exit_code;
46 private final String _path_to_prg;
48 public static MsaInferrer createInstance( final String path_to_prg ) throws IOException {
49 return new Mafft( path_to_prg );
52 private Mafft( final String path_to_prg ) throws IOException {
53 if ( !isInstalled( path_to_prg ) ) {
54 throw new IOException( "cannot execute MAFFT with \"" + path_to_prg + "\"" );
56 _path_to_prg = new String( path_to_prg );
60 public static String getDefaultParameters() {
61 return DEFAULT_PARAMETERS;
65 public String getErrorDescription() {
70 public int getExitCode() {
75 public Msa infer( final List<Sequence> seqs, final List<String> opts ) throws IOException, InterruptedException {
76 final File file = File.createTempFile( "__mafft_input_", ".fasta" );
78 final BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );
79 SequenceWriter.writeSeqs( seqs, writer, SEQ_FORMAT.FASTA, 100 );
81 final Msa msa = infer( file, opts );
87 public Msa infer( final File path_to_input_seqs, final List<String> opts ) throws IOException, InterruptedException {
89 final List<String> my_opts = new ArrayList<String>();
90 my_opts.add( _path_to_prg );
91 for( int i = 0; i < opts.size(); i++ ) {
92 my_opts.add( opts.get( i ) );
94 my_opts.add( path_to_input_seqs.getAbsolutePath() );
95 final SystemCommandExecutor command_executor = new SystemCommandExecutor( my_opts );
96 final int _exit_code = command_executor.executeCommand();
97 final StringBuilder stderr = command_executor.getStandardErrorFromCommand();
98 _error = stderr.toString();
99 if ( _exit_code != 0 ) {
100 throw new IOException( "MAFFT program failed, exit code: " + _exit_code + "\nCommand:\n" + my_opts
101 + "\nError:\n" + stderr );
103 final StringBuilder stdout = command_executor.getStandardOutputFromCommand();
104 if ( ( stdout == null ) || ( stdout.length() < 2 ) ) {
105 throw new IOException( "MAFFT program did not produce any output\nCommand:\n" + my_opts + "\nError:\n"
108 final Msa msa = FastaParser.parseMsa( stdout.toString() );
112 private void init() {