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: www.phylosoft.org/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.MainFrameApplication;
49 import org.forester.archaeopteryx.Util;
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 _input_seqs_msa_program_name_tf;
82 private JTextField _input_seqs_msa_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 _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 inputfile_pnl_1.setLayout( new FlowLayout() );
112 inputfile_pnl_2.setLayout( new FlowLayout() );
113 inputfile_pnl_3.setLayout( new FlowLayout() );
114 inputfile_pnl_1.add( new JLabel( "Input Sequence File:" ) );
115 inputfile_pnl_1.add( _input_seqs_tf = new JTextField() );
116 inputfile_pnl_1.add( _select_input_seqs_btn = new JButton( "Select Input File" ) );
117 inputfile_pnl_2.add( new JLabel( "Sequences: " ) );
118 inputfile_pnl_2.add( new JLabel( "Number of Sequences:" ) );
119 inputfile_pnl_2.add( _input_seqs_number_tf = new JTextField() );
120 inputfile_pnl_2.add( new JLabel( "Length: median:" ) );
121 inputfile_pnl_2.add( _input_seqs_median_length_tf = new JTextField() );
122 inputfile_pnl_2.add( new JLabel( "min:" ) );
123 inputfile_pnl_2.add( _input_seqs_min_length_tf = new JTextField() );
124 inputfile_pnl_2.add( new JLabel( "max:" ) );
125 inputfile_pnl_2.add( _input_seqs_max_length_tf = new JTextField() );
126 inputfile_pnl_2.add( new JLabel( "Type:" ) );
127 inputfile_pnl_2.add( _input_seqs_type_tf = new JTextField() );
128 inputfile_pnl_3.add( new JLabel( "Mutiple Sequence Alignment: " ) );
129 inputfile_pnl_3.add( new JLabel( "Program: " ) );
130 inputfile_pnl_3.add( _input_seqs_msa_program_name_tf = new JTextField() );
131 inputfile_pnl_3.add( new JLabel( "Parameters: " ) );
132 inputfile_pnl_3.add( _input_seqs_msa_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 _input_seqs_msa_program_name_tf.setEditable( false );
146 _input_seqs_msa_paramenters_tf.setColumns( 26 );
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 );
153 setTitle( "Phylogenetic Inference (from already aligned sequences) " );
155 final JPanel inputfile_pnl_1 = new JPanel();
156 final JPanel inputfile_pnl_2 = new JPanel();
157 inputfile_pnl_1.setLayout( new FlowLayout() );
158 inputfile_pnl_2.setLayout( new FlowLayout() );
159 inputfile_pnl_1.add( new JLabel( "Input MSA File:" ) );
160 inputfile_pnl_1.add( _input_msa_file_tf = new JTextField() );
161 inputfile_pnl_1.add( _select_input_msa_btn = new JButton( "Select Input File" ) );
162 inputfile_pnl_2.add( new JLabel( "MSA: " ) );
163 inputfile_pnl_2.add( new JLabel( "Number of Sequences:" ) );
164 inputfile_pnl_2.add( _msa_size_tf = new JTextField() );
165 inputfile_pnl_2.add( new JLabel( "Length:" ) );
166 inputfile_pnl_2.add( _msa_length_tf = new JTextField() );
167 inputfile_pnl_2.add( new JLabel( "Type:" ) );
168 inputfile_pnl_2.add( _msa_type_tf = new JTextField() );
169 _msa_length_tf.setColumns( 4 );
170 _msa_size_tf.setColumns( 4 );
171 _msa_type_tf.setColumns( 2 );
172 _input_msa_file_tf.setColumns( 20 );
173 _input_msa_file_tf.setEditable( false );
174 _msa_length_tf.setEditable( false );
175 _msa_size_tf.setEditable( false );
176 _msa_type_tf.setEditable( false );
177 _select_input_msa_btn.addActionListener( this );
178 _pnl.add( inputfile_pnl_1 );
179 _pnl.add( inputfile_pnl_2 );
182 final JPanel inputfile_pnl_4 = new JPanel();
183 inputfile_pnl_4.setLayout( new FlowLayout() );
184 inputfile_pnl_4.add( new JLabel( "MSA Processing: " ) );
185 inputfile_pnl_4.add( _execute_msa_processing_cb = new JCheckBox( "Process MSA" ) );
186 inputfile_pnl_4.add( _msa_processing_remove_all_gap_columns_cb = new JCheckBox( "Remove all gap columns" ) );
187 inputfile_pnl_4.add( new JLabel( "Max allowed gap ratio: " ) );
188 inputfile_pnl_4.add( _msa_processing_max_allowed_gap_ratio_tf = new JTextField() );
189 inputfile_pnl_4.add( new JLabel( "Min allowed non-gap sequence length: " ) );
190 inputfile_pnl_4.add( _msa_processing_min_allowed_length_tf = new JTextField() );
191 _msa_processing_max_allowed_gap_ratio_tf.setColumns( 4 );
192 _msa_processing_min_allowed_length_tf.setColumns( 4 );
193 final Border b = new LineBorder( Color.DARK_GRAY );
194 inputfile_pnl_4.setBorder( b );
195 _pnl.add( inputfile_pnl_4 );
197 // Distance calculation:
198 // TODO if type==AA...
199 final JPanel distance_calc_pnl_1 = new JPanel();
200 distance_calc_pnl_1.setLayout( new FlowLayout() );
201 distance_calc_pnl_1.add( new JLabel( "Distance calculation:" ) );
202 distance_calc_pnl_1.add( _distance_calc_kimura_rb = new JRadioButton( "Kimura correction" ) );
203 distance_calc_pnl_1.add( _distance_calc_poisson_rb = new JRadioButton( "Poisson" ) );
205 .add( _distance_calc_fract_dissimilarity_rb = new JRadioButton( "Fractional dissimilarity" ) );
206 final ButtonGroup distance_calc_group_1 = new ButtonGroup();
207 distance_calc_group_1.add( _distance_calc_kimura_rb );
208 distance_calc_group_1.add( _distance_calc_poisson_rb );
209 distance_calc_group_1.add( _distance_calc_fract_dissimilarity_rb );
210 _pnl.add( distance_calc_pnl_1 );
211 // Bootstrap resampling:
212 final JPanel bootstrap_pnl = new JPanel();
213 bootstrap_pnl.setLayout( new FlowLayout() );
214 bootstrap_pnl.add( _bootstrap_cb = new JCheckBox( "Perform Bootstrap Resampling" ) );
215 bootstrap_pnl.add( new JLabel( "Number of Bootstrap Samples:" ) );
216 bootstrap_pnl.add( _bootstrap_tf = new JFormattedTextField( Util.createMaskFormatter( "###" ) ) );
217 _bootstrap_tf.setColumns( 4 );
219 // http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
220 // _bootstrap_tf.setColumns( 4 );
221 bootstrap_pnl.add( new JLabel( "Random Seed:" ) );
222 bootstrap_pnl.add( _random_seed_tf = new JTextField() );
223 _random_seed_tf.setColumns( 4 );
224 _pnl.add( bootstrap_pnl );
225 final JPanel launch_pnl = new JPanel();
226 launch_pnl.setLayout( new FlowLayout() );
227 _launch_btn = new JButton( "Go!" );
228 _launch_btn.addActionListener( this );
229 launch_pnl.add( _launch_btn );
230 _cancel_btn = new JButton( "Cancel" );
231 _cancel_btn.addActionListener( this );
232 launch_pnl.add( _cancel_btn );
233 _pnl.add( launch_pnl );
234 initializeValues( from_unaligned_seqs );
236 setLocationRelativeTo( getParentFrame() );
237 setResizable( false );
241 public void actionPerformed( final ActionEvent e ) {
242 if ( e.getSource() == _select_input_msa_btn ) {
245 else if ( e.getSource() == _select_input_seqs_btn ) {
248 else if ( e.getSource() == _launch_btn ) {
251 else if ( e.getSource() == _cancel_btn ) {
256 public void activate() {
260 private MainFrameApplication getParentFrame() {
261 return _parent_frame;
264 public PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {
268 public int getValue() {
272 private void initializeValues( final boolean from_unaligned_seqs ) {
273 _value = JOptionPane.CANCEL_OPTION;
274 if ( from_unaligned_seqs ) {
276 _input_seqs_msa_program_name_tf.setText( getPhylogeneticInferenceOptions().getMsaPrg() );
277 _input_seqs_msa_paramenters_tf.setText( getPhylogeneticInferenceOptions().getMsaPrgParameters() );
282 updateMsaProcessingItem();
283 updateDistanceCalcMethod();
284 _bootstrap_tf.setText( getPhylogeneticInferenceOptions().getBootstrapSamples() + "" );
285 _random_seed_tf.setText( getPhylogeneticInferenceOptions().getRandomNumberGeneratorSeed() + "" );
288 private void launch() {
289 processPerformBootstrapResampling();
290 if ( _bootstrap_cb.isSelected() ) {
291 processBootstrapSamplesNumber();
292 processRandomNumberGeneratorSeed();
296 processMsaProcessing();
298 processDistanceCalcMethod();
299 processMsaPrgParameters();
301 _value = JOptionPane.OK_OPTION;
304 private void cancel() {
306 _value = JOptionPane.CANCEL_OPTION;
309 private void processBootstrapSamplesNumber() {
310 int bootstrap_samples = 0;
312 bootstrap_samples = Integer.parseInt( _bootstrap_tf.getText().trim() );
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 );
318 if ( bootstrap_samples >= 0 ) {
319 getPhylogeneticInferenceOptions().setBootstrapSamples( bootstrap_samples );
323 private void processRandomNumberGeneratorSeed() {
324 long seed = PhylogeneticInferenceOptions.RANDOM_NUMBER_SEED_DEFAULT;
326 seed = Long.parseLong( _random_seed_tf.getText().trim() );
328 catch ( final NumberFormatException e ) {
331 getPhylogeneticInferenceOptions().setRandomNumberGeneratorSeed( seed );
334 private void processMsaProcessing() {
335 getPhylogeneticInferenceOptions().setExecuteMsaProcessing( _execute_msa_processing_cb.isSelected() );
336 getPhylogeneticInferenceOptions()
337 .setMsaProcessingRemoveAllGapColumns( _msa_processing_remove_all_gap_columns_cb.isSelected() );
340 min_length = Integer.parseInt( _msa_processing_min_allowed_length_tf.getText().trim() );
342 catch ( final NumberFormatException e ) {
345 if ( min_length > 0 ) {
346 getPhylogeneticInferenceOptions().setMsaProcessingMinAllowedLength( min_length );
348 double msa_processing_max_allowed_gap_ratio = -1.0;
350 msa_processing_max_allowed_gap_ratio = Double.parseDouble( _msa_processing_max_allowed_gap_ratio_tf
353 catch ( final NumberFormatException e ) {
354 msa_processing_max_allowed_gap_ratio = -1.0;
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 );
361 private void processDistanceCalcMethod() {
362 if ( ( _distance_calc_kimura_rb != null ) && _distance_calc_kimura_rb.isSelected() ) {
363 getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.KIMURA_DISTANCE );
365 else if ( ( _distance_calc_poisson_rb != null ) && _distance_calc_poisson_rb.isSelected() ) {
366 getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.POISSON_DISTANCE );
368 else if ( ( _distance_calc_fract_dissimilarity_rb != null )
369 && _distance_calc_fract_dissimilarity_rb.isSelected() ) {
370 getPhylogeneticInferenceOptions().setPwdDistanceMethod( PWD_DISTANCE_METHOD.FRACTIONAL_DISSIMILARITY );
374 private void processPerformBootstrapResampling() {
375 getPhylogeneticInferenceOptions().setPerformBootstrapResampling( _bootstrap_cb.isSelected() );
378 private void processMsaPrgParameters() {
379 if ( _input_seqs_msa_paramenters_tf != null ) {
380 getPhylogeneticInferenceOptions().setMsaPrgParameters( _input_seqs_msa_paramenters_tf.getText() );
384 private void readInputFile() {
385 getParentFrame().readMsaFromFile();
389 private void readInputSeqsFile() {
390 getParentFrame().readSeqsFromFile();
394 private void updateDistanceCalcMethod() {
395 switch ( getPhylogeneticInferenceOptions().getPwdDistanceMethod() ) {
396 case KIMURA_DISTANCE:
397 _distance_calc_kimura_rb.setSelected( true );
399 case POISSON_DISTANCE:
400 _distance_calc_poisson_rb.setSelected( true );
402 case FRACTIONAL_DISSIMILARITY:
403 _distance_calc_fract_dissimilarity_rb.setSelected( true );
406 throw new RuntimeException( "invalid distance calc method" );
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() + "" );
417 if ( _opts.getMsaProcessingMinAllowedLength() > 0 ) {
418 _msa_processing_min_allowed_length_tf.setText( _opts.getMsaProcessingMinAllowedLength() + "" );
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 );
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 );
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 );
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 );
481 DescriptiveStatistics calcSequenceStats( final List<Sequence> seqs ) {
482 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
483 for( final Sequence s : seqs ) {
484 stats.addValue( s.getLength() );