made PhyloInferenceDialog constructor refer to null instead of mainframe
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / PhyloInferenceDialog.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.awt.Color;
29 import java.awt.FlowLayout;
30 import java.awt.Frame;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.util.List;
34
35 import javax.swing.BoxLayout;
36 import javax.swing.ButtonGroup;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JDialog;
40 import javax.swing.JFormattedTextField;
41 import javax.swing.JLabel;
42 import javax.swing.JOptionPane;
43 import javax.swing.JPanel;
44 import javax.swing.JRadioButton;
45 import javax.swing.JTextField;
46 import javax.swing.border.Border;
47 import javax.swing.border.LineBorder;
48
49 import org.forester.archaeopteryx.AptxUtil;
50 import org.forester.archaeopteryx.MainFrameApplication;
51 import org.forester.evoinference.distance.PairwiseDistanceCalculator.PWD_DISTANCE_METHOD;
52 import org.forester.sequence.MolecularSequence;
53 import org.forester.util.BasicDescriptiveStatistics;
54 import org.forester.util.DescriptiveStatistics;
55
56 public class PhyloInferenceDialog extends JDialog implements ActionListener {
57
58     private static final long                  serialVersionUID = 8337543508238133614L;
59     private final JPanel                       _pnl;
60     private final JButton                      _launch_btn;
61     private final JButton                      _cancel_btn;
62     private final JFormattedTextField          _bootstrap_tf;
63     private final JCheckBox                    _bootstrap_cb;
64     private final PhylogeneticInferenceOptions _opts;
65     private JTextField                         _input_msa_file_tf;
66     private JButton                            _select_input_msa_btn;
67     private final MainFrameApplication         _parent_frame;
68     private JTextField                         _msa_length_tf;
69     private JTextField                         _msa_size_tf;
70     private JTextField                         _msa_type_tf;
71     private final JRadioButton                 _distance_calc_kimura_rb;
72     private final JRadioButton                 _distance_calc_poisson_rb;
73     private final JRadioButton                 _distance_calc_fract_dissimilarity_rb;
74     private int                                _value           = JOptionPane.CANCEL_OPTION;
75     private JTextField                         _input_seqs_tf;
76     private JButton                            _select_input_seqs_btn;
77     private JTextField                         _input_seqs_number_tf;
78     private JTextField                         _input_seqs_median_length_tf;
79     private JTextField                         _input_seqs_min_length_tf;
80     private JTextField                         _input_seqs_max_length_tf;
81     private JTextField                         _input_seqs_type_tf;
82     private JTextField                         _mafft_paramenters_tf;
83     private JTextField                         _msa_processing_max_allowed_gap_ratio_tf;
84     private JTextField                         _msa_processing_min_allowed_length_tf;
85     private JTextField                         _random_seed_tf;
86     private JCheckBox                          _execute_msa_processing_cb;
87     private JCheckBox                          _msa_processing_remove_all_gap_columns_cb;
88     private JCheckBox                          _mafft_cb;
89     private JCheckBox                          _save_pwd_file_cb;
90     private JCheckBox                          _save_processed_msa_cb;
91     private JCheckBox                          _save_original_msa_cb;
92     private JTextField                         _pwd_outfile_tf;
93     private JTextField                         _processed_msa_outfile_tf;
94     private JTextField                         _original_msa_outfile_tf;
95
96     public PhyloInferenceDialog( final MainFrameApplication frame,
97                                  final PhylogeneticInferenceOptions options,
98                                  final boolean from_unaligned_seqs ) {
99         super( (Frame) null, true );
100         setVisible( false );
101         _parent_frame = frame;
102         _opts = options;
103         _pnl = new JPanel();
104         getContentPane().add( _pnl );
105         final BoxLayout box_layout = new BoxLayout( _pnl, BoxLayout.PAGE_AXIS );
106         _pnl.setLayout( box_layout );
107         if ( from_unaligned_seqs ) {
108             setTitle( "Phylogenetic Inference (including multiple sequence alignment)" );
109             final JPanel inputfile_pnl_1 = new JPanel();
110             final JPanel inputfile_pnl_2 = new JPanel();
111             final JPanel inputfile_pnl_3 = new JPanel();
112             final JPanel inputfile_pnl_4 = new JPanel();
113             inputfile_pnl_1.setLayout( new FlowLayout() );
114             inputfile_pnl_2.setLayout( new FlowLayout() );
115             inputfile_pnl_3.setLayout( new FlowLayout() );
116             inputfile_pnl_4.setLayout( new FlowLayout() );
117             inputfile_pnl_1.add( new JLabel( "Input Sequence File:" ) );
118             inputfile_pnl_1.add( _input_seqs_tf = new JTextField() );
119             inputfile_pnl_1.add( _select_input_seqs_btn = new JButton( "Select Input File" ) );
120             inputfile_pnl_2.add( new JLabel( "Sequences: " ) );
121             inputfile_pnl_2.add( new JLabel( "Number of Sequences:" ) );
122             inputfile_pnl_2.add( _input_seqs_number_tf = new JTextField() );
123             inputfile_pnl_2.add( new JLabel( "Length: median:" ) );
124             inputfile_pnl_2.add( _input_seqs_median_length_tf = new JTextField() );
125             inputfile_pnl_2.add( new JLabel( "min:" ) );
126             inputfile_pnl_2.add( _input_seqs_min_length_tf = new JTextField() );
127             inputfile_pnl_2.add( new JLabel( "max:" ) );
128             inputfile_pnl_2.add( _input_seqs_max_length_tf = new JTextField() );
129             inputfile_pnl_2.add( new JLabel( "Type:" ) );
130             inputfile_pnl_2.add( _input_seqs_type_tf = new JTextField() );
131             inputfile_pnl_3.add( _mafft_cb = new JCheckBox( "MAFFT" ) );
132             inputfile_pnl_3.add( new JLabel( "Parameters: " ) );
133             inputfile_pnl_3.add( _mafft_paramenters_tf = new JTextField() );
134             _input_seqs_median_length_tf.setColumns( 4 );
135             _input_seqs_min_length_tf.setColumns( 4 );
136             _input_seqs_max_length_tf.setColumns( 4 );
137             _input_seqs_number_tf.setColumns( 4 );
138             _input_seqs_type_tf.setColumns( 2 );
139             _input_seqs_tf.setColumns( 20 );
140             _input_seqs_tf.setEditable( false );
141             _input_seqs_median_length_tf.setEditable( false );
142             _input_seqs_min_length_tf.setEditable( false );
143             _input_seqs_max_length_tf.setEditable( false );
144             _input_seqs_number_tf.setEditable( false );
145             _input_seqs_type_tf.setEditable( false );
146             _mafft_paramenters_tf.setColumns( 26 );
147             _mafft_paramenters_tf.setText( "--maxiterate 1000 --localpair" );
148             _select_input_seqs_btn.addActionListener( this );
149             _pnl.add( inputfile_pnl_1 );
150             _pnl.add( inputfile_pnl_2 );
151             _pnl.add( inputfile_pnl_3 );
152             _pnl.add( inputfile_pnl_4 );
153         }
154         else {
155             setTitle( "Phylogenetic Inference (from already aligned sequences) " );
156             // Inputfile (MSA):
157             final JPanel inputfile_pnl_1 = new JPanel();
158             final JPanel inputfile_pnl_2 = new JPanel();
159             inputfile_pnl_1.setLayout( new FlowLayout() );
160             inputfile_pnl_2.setLayout( new FlowLayout() );
161             inputfile_pnl_1.add( new JLabel( "Input MSA File:" ) );
162             inputfile_pnl_1.add( _input_msa_file_tf = new JTextField() );
163             inputfile_pnl_1.add( _select_input_msa_btn = new JButton( "Select Input File" ) );
164             inputfile_pnl_2.add( new JLabel( "MSA: " ) );
165             inputfile_pnl_2.add( new JLabel( "Number of Sequences:" ) );
166             inputfile_pnl_2.add( _msa_size_tf = new JTextField() );
167             inputfile_pnl_2.add( new JLabel( "Length:" ) );
168             inputfile_pnl_2.add( _msa_length_tf = new JTextField() );
169             inputfile_pnl_2.add( new JLabel( "Type:" ) );
170             inputfile_pnl_2.add( _msa_type_tf = new JTextField() );
171             _msa_length_tf.setColumns( 4 );
172             _msa_size_tf.setColumns( 4 );
173             _msa_type_tf.setColumns( 2 );
174             _input_msa_file_tf.setColumns( 20 );
175             _input_msa_file_tf.setEditable( false );
176             _msa_length_tf.setEditable( false );
177             _msa_size_tf.setEditable( false );
178             _msa_type_tf.setEditable( false );
179             _select_input_msa_btn.addActionListener( this );
180             _pnl.add( inputfile_pnl_1 );
181             _pnl.add( inputfile_pnl_2 );
182         }
183         //
184         final JPanel inputfile_pnl_4 = new JPanel();
185         inputfile_pnl_4.setLayout( new FlowLayout() );
186         inputfile_pnl_4.add( new JLabel( "MSA Processing: " ) );
187         inputfile_pnl_4.add( _execute_msa_processing_cb = new JCheckBox( "Process MSA" ) );
188         inputfile_pnl_4.add( _msa_processing_remove_all_gap_columns_cb = new JCheckBox( "Remove all gap columns" ) );
189         inputfile_pnl_4.add( new JLabel( "Max allowed gap ratio: " ) );
190         inputfile_pnl_4.add( _msa_processing_max_allowed_gap_ratio_tf = new JTextField() );
191         inputfile_pnl_4.add( new JLabel( "Min allowed non-gap sequence length: " ) );
192         inputfile_pnl_4.add( _msa_processing_min_allowed_length_tf = new JTextField() );
193         _msa_processing_max_allowed_gap_ratio_tf.setColumns( 4 );
194         _msa_processing_min_allowed_length_tf.setColumns( 4 );
195         final Border b = new LineBorder( Color.DARK_GRAY );
196         inputfile_pnl_4.setBorder( b );
197         _pnl.add( inputfile_pnl_4 );
198         //
199         // Distance calculation:
200         // TODO if type==AA...
201         final JPanel distance_calc_pnl_1 = new JPanel();
202         distance_calc_pnl_1.setLayout( new FlowLayout() );
203         distance_calc_pnl_1.add( new JLabel( "Distance calculation:" ) );
204         distance_calc_pnl_1.add( _distance_calc_kimura_rb = new JRadioButton( "Kimura correction" ) );
205         distance_calc_pnl_1.add( _distance_calc_poisson_rb = new JRadioButton( "Poisson" ) );
206         distance_calc_pnl_1
207         .add( _distance_calc_fract_dissimilarity_rb = new JRadioButton( "Fractional dissimilarity" ) );
208         final ButtonGroup distance_calc_group_1 = new ButtonGroup();
209         distance_calc_group_1.add( _distance_calc_kimura_rb );
210         distance_calc_group_1.add( _distance_calc_poisson_rb );
211         distance_calc_group_1.add( _distance_calc_fract_dissimilarity_rb );
212         _pnl.add( distance_calc_pnl_1 );
213         // Bootstrap resampling:
214         final JPanel bootstrap_pnl = new JPanel();
215         bootstrap_pnl.setLayout( new FlowLayout() );
216         bootstrap_pnl.add( _bootstrap_cb = new JCheckBox( "Perform Bootstrap Resampling" ) );
217         bootstrap_pnl.add( new JLabel( "Number of Bootstrap Samples:" ) );
218         bootstrap_pnl.add( _bootstrap_tf = new JFormattedTextField( AptxUtil.createMaskFormatter( "###" ) ) );
219         _bootstrap_tf.setColumns( 4 );
220         // TODO see
221         // http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
222         // _bootstrap_tf.setColumns( 4 );
223         bootstrap_pnl.add( new JLabel( "Random Seed:" ) );
224         bootstrap_pnl.add( _random_seed_tf = new JTextField() );
225         _random_seed_tf.setColumns( 4 );
226         _pnl.add( bootstrap_pnl );
227         final JPanel launch_pnl = new JPanel();
228         launch_pnl.setLayout( new FlowLayout() );
229         _launch_btn = new JButton( "Go!" );
230         _launch_btn.addActionListener( this );
231         launch_pnl.add( _launch_btn );
232         _cancel_btn = new JButton( "Cancel" );
233         _cancel_btn.addActionListener( this );
234         launch_pnl.add( _cancel_btn );
235         _pnl.add( launch_pnl );
236         initializeValues( from_unaligned_seqs );
237         pack();
238         setLocationRelativeTo( getParentFrame() );
239         setResizable( false );
240     }
241
242     @Override
243     public void actionPerformed( final ActionEvent e ) {
244         if ( e.getSource() == _select_input_msa_btn ) {
245             readInputFile();
246         }
247         else if ( e.getSource() == _select_input_seqs_btn ) {
248             readInputSeqsFile();
249         }
250         else if ( e.getSource() == _launch_btn ) {
251             launch();
252         }
253         else if ( e.getSource() == _cancel_btn ) {
254             cancel();
255         }
256     }
257
258     public void activate() {
259         setVisible( true );
260     }
261
262     private MainFrameApplication getParentFrame() {
263         return _parent_frame;
264     }
265
266     public PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {
267         return _opts;
268     }
269
270     public int getValue() {
271         return _value;
272     }
273
274     private void initializeValues( final boolean from_unaligned_seqs ) {
275         _value = JOptionPane.CANCEL_OPTION;
276         if ( from_unaligned_seqs ) {
277             updateSeqsItems();
278         }
279         else {
280             updateMsaItems();
281         }
282         updateMsaProcessingItem();
283         updateDistanceCalcMethod();
284         _bootstrap_tf.setText( getPhylogeneticInferenceOptions().getBootstrapSamples() + "" );
285         _random_seed_tf.setText( getPhylogeneticInferenceOptions().getRandomNumberGeneratorSeed() + "" );
286     }
287
288     private void launch() {
289         processPerformBootstrapResampling();
290         if ( _bootstrap_cb.isSelected() ) {
291             processBootstrapSamplesNumber();
292             processRandomNumberGeneratorSeed();
293         }
294         if ( true ) {
295             //TODO
296             processMsaProcessing();
297         }
298         processDistanceCalcMethod();
299         processMsaPrgParameters();
300         setVisible( false );
301         _value = JOptionPane.OK_OPTION;
302     }
303
304     private void cancel() {
305         setVisible( false );
306         _value = JOptionPane.CANCEL_OPTION;
307     }
308
309     private void processBootstrapSamplesNumber() {
310         int bootstrap_samples = 0;
311         try {
312             bootstrap_samples = Integer.parseInt( _bootstrap_tf.getText().trim() );
313         }
314         catch ( final NumberFormatException e ) {
315             // JOptionPane.showMessageDialog( this, "Could not parse number of bootstrap resamplings from: " +  _bootstrap_tf.getText().trim(), "User Error", JOptionPane.ERROR_MESSAGE );
316             return;
317         }
318         if ( bootstrap_samples >= 0 ) {
319             getPhylogeneticInferenceOptions().setBootstrapSamples( bootstrap_samples );
320         }
321     }
322
323     private void processRandomNumberGeneratorSeed() {
324         long seed = PhylogeneticInferenceOptions.RANDOM_NUMBER_SEED_DEFAULT;
325         try {
326             seed = Long.parseLong( _random_seed_tf.getText().trim() );
327         }
328         catch ( final NumberFormatException e ) {
329             return;
330         }
331         getPhylogeneticInferenceOptions().setRandomNumberGeneratorSeed( seed );
332     }
333
334     private void processMsaProcessing() {
335         getPhylogeneticInferenceOptions().setExecuteMsaProcessing( _execute_msa_processing_cb.isSelected() );
336         getPhylogeneticInferenceOptions()
337         .setMsaProcessingRemoveAllGapColumns( _msa_processing_remove_all_gap_columns_cb.isSelected() );
338         int min_length = -1;
339         try {
340             min_length = Integer.parseInt( _msa_processing_min_allowed_length_tf.getText().trim() );
341         }
342         catch ( final NumberFormatException e ) {
343             min_length = -1;
344         }
345         if ( min_length > 0 ) {
346             getPhylogeneticInferenceOptions().setMsaProcessingMinAllowedLength( min_length );
347         }
348         double msa_processing_max_allowed_gap_ratio = -1.0;
349         try {
350             msa_processing_max_allowed_gap_ratio = Double.parseDouble( _msa_processing_max_allowed_gap_ratio_tf
351                                                                        .getText().trim() );
352         }
353         catch ( final NumberFormatException e ) {
354             msa_processing_max_allowed_gap_ratio = -1.0;
355         }
356         if ( ( msa_processing_max_allowed_gap_ratio >= 0.0 ) && ( msa_processing_max_allowed_gap_ratio <= 1.0 ) ) {
357             getPhylogeneticInferenceOptions().setMsaProcessingMaxAllowedGapRatio( msa_processing_max_allowed_gap_ratio );
358         }
359     }
360
361     private void processDistanceCalcMethod() {
362         if ( ( _distance_calc_kimura_rb != null ) && _distance_calc_kimura_rb.isSelected() ) {
363             getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.KIMURA_DISTANCE );
364         }
365         else if ( ( _distance_calc_poisson_rb != null ) && _distance_calc_poisson_rb.isSelected() ) {
366             getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.POISSON_DISTANCE );
367         }
368         else if ( ( _distance_calc_fract_dissimilarity_rb != null )
369                 && _distance_calc_fract_dissimilarity_rb.isSelected() ) {
370             getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.FRACTIONAL_DISSIMILARITY );
371         }
372     }
373
374     private void processPerformBootstrapResampling() {
375         getPhylogeneticInferenceOptions().setPerformBootstrapResampling( _bootstrap_cb.isSelected() );
376     }
377
378     private void processMsaPrgParameters() {
379         if ( _mafft_paramenters_tf != null ) {
380             getPhylogeneticInferenceOptions().setMsaPrgParameters( _mafft_paramenters_tf.getText() );
381         }
382     }
383
384     private void readInputFile() {
385         getParentFrame().readMsaFromFile();
386         updateMsaItems();
387     }
388
389     private void readInputSeqsFile() {
390         getParentFrame().readSeqsFromFileforPI();
391         updateSeqsItems();
392     }
393
394     private void updateDistanceCalcMethod() {
395         switch ( getPhylogeneticInferenceOptions().getPwdDistanceMethod() ) {
396             case KIMURA_DISTANCE:
397                 _distance_calc_kimura_rb.setSelected( true );
398                 break;
399             case POISSON_DISTANCE:
400                 _distance_calc_poisson_rb.setSelected( true );
401                 break;
402             case FRACTIONAL_DISSIMILARITY:
403                 _distance_calc_fract_dissimilarity_rb.setSelected( true );
404                 break;
405             default:
406                 throw new RuntimeException( "invalid distance calc method" );
407         }
408     }
409
410     private void updateMsaProcessingItem() {
411         _execute_msa_processing_cb.setSelected( getPhylogeneticInferenceOptions().isExecuteMsaProcessing() );
412         _msa_processing_remove_all_gap_columns_cb.setSelected( getPhylogeneticInferenceOptions()
413                                                                .isMsaProcessingRemoveAllGapColumns() );
414         if ( _opts.getMsaProcessingMaxAllowedGapRatio() > 0 ) {
415             _msa_processing_max_allowed_gap_ratio_tf.setText( _opts.getMsaProcessingMaxAllowedGapRatio() + "" );
416         }
417         if ( _opts.getMsaProcessingMinAllowedLength() > 0 ) {
418             _msa_processing_min_allowed_length_tf.setText( _opts.getMsaProcessingMinAllowedLength() + "" );
419         }
420     }
421
422     private void updateMsaItems() {
423         if ( getParentFrame().getMsa() != null ) {
424             _input_msa_file_tf.setText( getParentFrame().getMsaFile().toString() );
425             _msa_length_tf.setText( getParentFrame().getMsa().getLength() + "" );
426             _msa_size_tf.setText( getParentFrame().getMsa().getNumberOfSequences() + "" );
427             _msa_type_tf.setText( getParentFrame().getMsa().getType() + "" );
428             _input_msa_file_tf.setEnabled( true );
429             _msa_length_tf.setEnabled( true );
430             _msa_size_tf.setEnabled( true );
431             _msa_type_tf.setEnabled( true );
432             _launch_btn.setEnabled( true );
433         }
434         else {
435             _input_msa_file_tf.setText( "" );
436             _msa_length_tf.setText( "" );
437             _msa_size_tf.setText( "" );
438             _msa_type_tf.setText( "" );
439             _input_msa_file_tf.setEnabled( false );
440             _msa_length_tf.setEnabled( false );
441             _msa_size_tf.setEnabled( false );
442             _msa_type_tf.setEnabled( false );
443             _launch_btn.setEnabled( false );
444         }
445     }
446
447     private void updateSeqsItems() {
448         if ( getParentFrame().getSeqs() != null ) {
449             final DescriptiveStatistics stats = calcSequenceStats( getParentFrame().getSeqs() );
450             _input_seqs_tf.setText( getParentFrame().getSeqsFile().toString() );
451             _input_seqs_median_length_tf.setText( ( int ) stats.median() + "" );
452             _input_seqs_min_length_tf.setText( ( int ) stats.getMin() + "" );
453             _input_seqs_max_length_tf.setText( ( int ) stats.getMax() + "" );
454             _input_seqs_number_tf.setText( getParentFrame().getSeqs().size() + "" );
455             _input_seqs_type_tf.setText( getParentFrame().getSeqs().get( 0 ).getType() + "" );
456             _input_seqs_tf.setEnabled( true );
457             _input_seqs_median_length_tf.setEnabled( true );
458             _input_seqs_min_length_tf.setEnabled( true );
459             _input_seqs_max_length_tf.setEnabled( true );
460             _input_seqs_number_tf.setEnabled( true );
461             _input_seqs_type_tf.setEnabled( true );
462             _launch_btn.setEnabled( true );
463         }
464         else {
465             _input_seqs_tf.setText( "" );
466             _input_seqs_median_length_tf.setText( "" );
467             _input_seqs_min_length_tf.setText( "" );
468             _input_seqs_max_length_tf.setText( "" );
469             _input_seqs_number_tf.setText( "" );
470             _input_seqs_type_tf.setText( "" );
471             _input_seqs_tf.setEnabled( false );
472             _input_seqs_median_length_tf.setEnabled( false );
473             _input_seqs_min_length_tf.setEnabled( false );
474             _input_seqs_max_length_tf.setEnabled( false );
475             _input_seqs_number_tf.setEnabled( false );
476             _input_seqs_type_tf.setEnabled( false );
477             _launch_btn.setEnabled( false );
478         }
479     }
480
481     DescriptiveStatistics calcSequenceStats( final List<MolecularSequence> seqs ) {
482         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
483         for( final MolecularSequence s : seqs ) {
484             stats.addValue( s.getLength() );
485         }
486         return stats;
487     }
488 }