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