in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / PhylogeneticInferrer.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.io.BufferedWriter;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.swing.JOptionPane;
35
36 import org.forester.archaeopteryx.MainFrameApplication;
37 import org.forester.evoinference.distance.NeighborJoiningF;
38 import org.forester.evoinference.distance.PairwiseDistanceCalculator;
39 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
40 import org.forester.evoinference.tools.BootstrapResampler;
41 import org.forester.msa.BasicMsa;
42 import org.forester.msa.Mafft;
43 import org.forester.msa.Msa;
44 import org.forester.msa.Msa.MSA_FORMAT;
45 import org.forester.msa.MsaInferrer;
46 import org.forester.msa.MsaMethods;
47 import org.forester.msa.ResampleableMsa;
48 import org.forester.phylogeny.Phylogeny;
49 import org.forester.phylogeny.PhylogenyMethods;
50 import org.forester.sequence.MolecularSequence;
51 import org.forester.tools.ConfidenceAssessor;
52 import org.forester.util.ForesterUtil;
53
54 public class PhylogeneticInferrer extends RunnableProcess {
55
56     private Msa                                _msa;
57     private final MainFrameApplication         _mf;
58     private final PhylogeneticInferenceOptions _options;
59     private final List<MolecularSequence>      _seqs;
60     private final boolean                      DEBUG           = true;
61     public final static String                 MSA_FILE_SUFFIX = ".aln";
62     public final static String                 PWD_FILE_SUFFIX = ".pwd";
63
64     public PhylogeneticInferrer( final List<MolecularSequence> seqs,
65                                  final PhylogeneticInferenceOptions options,
66                                  final MainFrameApplication mf ) {
67         _msa = null;
68         _seqs = seqs;
69         _mf = mf;
70         _options = options;
71     }
72
73     public PhylogeneticInferrer( final Msa msa,
74                                  final PhylogeneticInferenceOptions options,
75                                  final MainFrameApplication mf ) {
76         _msa = msa;
77         _seqs = null;
78         _mf = mf;
79         _options = options;
80     }
81
82     private Msa inferMsa( final MSA_PRG msa_prg ) throws IOException, InterruptedException {
83         //        final File temp_seqs_file = File.createTempFile( "__msa__temp__", ".fasta" );
84         //        if ( DEBUG ) {
85         //            System.out.println();
86         //            System.out.println( "temp file: " + temp_seqs_file );
87         //            System.out.println();
88         //        }
89         //        //final File temp_seqs_file = new File( _options.getTempDir() + ForesterUtil.FILE_SEPARATOR + "s.fasta" );
90         //        final BufferedWriter writer = new BufferedWriter( new FileWriter( temp_seqs_file ) );
91         //        SequenceWriter.writeSeqs( _seqs, writer, SEQ_FORMAT.FASTA, 100 );
92         //        writer.close();
93         switch ( msa_prg ) {
94             case MAFFT:
95                 return runMAFFT( _seqs, processMafftOptions() );
96             default:
97                 return null;
98         }
99     }
100
101     private List<String> processMafftOptions() {
102         final String opts_str = _options.getMsaPrgParameters().trim().toLowerCase();
103         final String[] opts_ary = opts_str.split( " " );
104         final List<String> opts = new ArrayList<String>();
105         boolean saw_quiet = false;
106         for( final String opt : opts_ary ) {
107             opts.add( opt );
108             if ( opt.equals( "--quiet" ) ) {
109                 saw_quiet = true;
110             }
111         }
112         if ( !saw_quiet ) {
113             opts.add( "--quiet" );
114         }
115         return opts;
116     }
117
118     private Phylogeny inferPhylogeny( final Msa msa ) {
119         BasicSymmetricalDistanceMatrix m = null;
120         switch ( _options.getPwdDistanceMethod() ) {
121             case KIMURA_DISTANCE:
122                 m = PairwiseDistanceCalculator.calcKimuraDistances( msa );
123                 break;
124             case POISSON_DISTANCE:
125                 m = PairwiseDistanceCalculator.calcPoissonDistances( msa );
126                 break;
127             case FRACTIONAL_DISSIMILARITY:
128                 m = PairwiseDistanceCalculator.calcFractionalDissimilarities( msa );
129                 break;
130             default:
131                 throw new RuntimeException( "invalid pwd method" );
132         }
133         if ( !ForesterUtil.isEmpty( _options.getIntermediateFilesBase() ) ) {
134             BufferedWriter pwd_writer;
135             try {
136                 pwd_writer = new BufferedWriter( new FileWriter( _options.getIntermediateFilesBase() + PWD_FILE_SUFFIX ) );
137                 m.write( pwd_writer );
138                 pwd_writer.close();
139             }
140             catch ( final IOException e ) {
141                 // TODO Auto-generated catch block
142                 e.printStackTrace();
143             }
144         }
145         final NeighborJoiningF nj = NeighborJoiningF.createInstance( false, 5 );
146         final Phylogeny phy = nj.execute( m );
147         PhylogenyMethods.addMolecularSeqsToTree( phy, msa );
148         PhylogenyMethods.extractFastaInformation( phy );
149         return phy;
150     }
151
152     private void infer() throws InterruptedException {
153         //_mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
154         if ( ( _msa == null ) && ( _seqs == null ) ) {
155             throw new IllegalArgumentException( "cannot run phylogenetic analysis with null msa and seq array" );
156         }
157         start( _mf, "phylogenetic inference" );
158         if ( _msa == null ) {
159             Msa msa = null;
160             try {
161                 msa = inferMsa( MSA_PRG.MAFFT );
162             }
163             catch ( final IOException e ) {
164                 end( _mf );
165                 JOptionPane.showMessageDialog( _mf,
166                                                "Could not create multiple sequence alignment with \""
167                                                        + _options.getMsaPrg() + "\" and the following parameters:\n\""
168                                                        + _options.getMsaPrgParameters() + "\"\nError: "
169                                                        + e.getLocalizedMessage(),
170                                                "Failed to Calculate MSA",
171                                                JOptionPane.ERROR_MESSAGE );
172                 if ( DEBUG ) {
173                     e.printStackTrace();
174                 }
175                 return;
176             }
177             catch ( final Exception e ) {
178                 end( _mf );
179                 JOptionPane.showMessageDialog( _mf,
180                                                "Could not create multiple sequence alignment with \""
181                                                        + _options.getMsaPrg() + "\" and the following parameters:\n\""
182                                                        + _options.getMsaPrgParameters() + "\"\nError: "
183                                                        + e.getLocalizedMessage(),
184                                                "Unexpected Exception During MSA Calculation",
185                                                JOptionPane.ERROR_MESSAGE );
186                 if ( DEBUG ) {
187                     e.printStackTrace();
188                 }
189                 return;
190             }
191             if ( msa == null ) {
192                 end( _mf );
193                 JOptionPane.showMessageDialog( _mf,
194                                                "Could not create multiple sequence alignment with "
195                                                        + _options.getMsaPrg() + "\nand the following parameters:\n\""
196                                                        + _options.getMsaPrgParameters() + "\"",
197                                                "Failed to Calculate MSA",
198                                                JOptionPane.ERROR_MESSAGE );
199                 return;
200             }
201             if ( DEBUG ) {
202                 System.out.println( msa.toString() );
203                 System.out.println( MsaMethods.calcGapRatio( msa ) );
204             }
205             final MsaMethods msa_tools = MsaMethods.createInstance();
206             if ( _options.isExecuteMsaProcessing() ) {
207                 msa = msa_tools.deleteGapColumns( _options.getMsaProcessingMaxAllowedGapRatio(),
208                                                   _options.getMsaProcessingMinAllowedLength(),
209                                                   msa );
210                 if ( msa == null ) {
211                     end( _mf );
212                     JOptionPane.showMessageDialog( _mf,
213                                                    "Less than two sequences longer than "
214                                                            + _options.getMsaProcessingMinAllowedLength()
215                                                            + " residues left after MSA processing",
216                                                    "MSA Processing Settings Too Stringent",
217                                                    JOptionPane.ERROR_MESSAGE );
218                     return;
219                 }
220             }
221             if ( DEBUG ) {
222                 System.out.println( msa_tools.getIgnoredSequenceIds() );
223                 System.out.println( msa.toString() );
224                 System.out.println( MsaMethods.calcGapRatio( msa ) );
225             }
226             _msa = msa;
227         }
228         final int n = _options.getBootstrapSamples();
229         final long seed = _options.getRandomNumberGeneratorSeed();
230         final Phylogeny master_phy = inferPhylogeny( _msa );
231         if ( _options.isPerformBootstrapResampling() && ( n > 0 ) ) {
232             final ResampleableMsa resampleable_msa = new ResampleableMsa( ( BasicMsa ) _msa );
233             final int[][] resampled_column_positions = BootstrapResampler.createResampledColumnPositions( _msa
234                     .getLength(), n, seed );
235             final Phylogeny[] eval_phys = new Phylogeny[ n ];
236             for( int i = 0; i < n; ++i ) {
237                 resampleable_msa.resample( resampled_column_positions[ i ] );
238                 eval_phys[ i ] = inferPhylogeny( resampleable_msa );
239             }
240             ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 );
241         }
242         _mf.getMainPanel().addPhylogenyInNewTab( master_phy, _mf.getConfiguration(), "nj", "njpath" );
243         //  _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
244         end( _mf );
245         JOptionPane.showMessageDialog( _mf,
246                                        "Inference successfully completed",
247                                        "Inference Completed",
248                                        JOptionPane.INFORMATION_MESSAGE );
249     }
250
251     @Override
252     public void run() {
253         try {
254             infer();
255         }
256         catch ( final InterruptedException e ) {
257             // TODO need to handle this exception SOMEHOW!
258             // TODO Auto-generated catch block
259             e.printStackTrace();
260         }
261     }
262
263     private Msa runMAFFT( final List<MolecularSequence> seqs, final List<String> opts ) throws IOException,
264             InterruptedException {
265         Msa msa = null;
266         final MsaInferrer mafft = Mafft.createInstance( _mf.getInferenceManager().getPathToLocalMafft()
267                 .getCanonicalPath() );
268         try {
269             msa = mafft.infer( seqs, opts );
270         }
271         catch ( final IOException e ) {
272             System.out.println( mafft.getErrorDescription() );
273         }
274         return msa;
275     }
276
277     private void writeToFiles( final BasicSymmetricalDistanceMatrix m ) {
278         if ( !ForesterUtil.isEmpty( _options.getIntermediateFilesBase() ) ) {
279             try {
280                 final BufferedWriter msa_writer = new BufferedWriter( new FileWriter( _options.getIntermediateFilesBase()
281                         + MSA_FILE_SUFFIX ) );
282                 _msa.write( msa_writer, MSA_FORMAT.PHYLIP );
283                 msa_writer.close();
284                 final BufferedWriter pwd_writer = new BufferedWriter( new FileWriter( _options.getIntermediateFilesBase()
285                         + PWD_FILE_SUFFIX ) );
286                 m.write( pwd_writer );
287                 pwd_writer.close();
288             }
289             catch ( final Exception e ) {
290                 System.out.println( "Error: " + e.getMessage() );
291             }
292         }
293     }
294
295     public enum MSA_PRG {
296         MAFFT;
297     }
298 }