45ae79d52a8eb0df31cc98745200311090981cc3
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplication.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.archaeopteryx;
28
29 import java.awt.BorderLayout;
30 import java.awt.Font;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ComponentAdapter;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.net.MalformedURLException;
41 import java.net.URL;
42 import java.util.ArrayList;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Set;
46
47 import javax.swing.ButtonGroup;
48 import javax.swing.JCheckBoxMenuItem;
49 import javax.swing.JFileChooser;
50 import javax.swing.JMenu;
51 import javax.swing.JMenuBar;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JRadioButtonMenuItem;
55 import javax.swing.UIManager;
56 import javax.swing.UnsupportedLookAndFeelException;
57 import javax.swing.WindowConstants;
58 import javax.swing.event.ChangeEvent;
59 import javax.swing.event.ChangeListener;
60 import javax.swing.event.InternalFrameAdapter;
61 import javax.swing.event.InternalFrameEvent;
62
63 import org.forester.analysis.TaxonomyDataManager;
64 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
65 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
66 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
67 import org.forester.archaeopteryx.tools.InferenceManager;
68 import org.forester.archaeopteryx.tools.PhyloInferenceDialog;
69 import org.forester.archaeopteryx.tools.PhylogeneticInferenceOptions;
70 import org.forester.archaeopteryx.tools.PhylogeneticInferrer;
71 import org.forester.archaeopteryx.tools.SequenceDataRetriver;
72 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
73 import org.forester.archaeopteryx.webservices.WebservicesManager;
74 import org.forester.io.parsers.FastaParser;
75 import org.forester.io.parsers.GeneralMsaParser;
76 import org.forester.io.parsers.PhylogenyParser;
77 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
78 import org.forester.io.parsers.nhx.NHXParser;
79 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
80 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
81 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
82 import org.forester.io.parsers.tol.TolParser;
83 import org.forester.io.parsers.util.ParserUtils;
84 import org.forester.io.writers.SequenceWriter;
85 import org.forester.msa.Msa;
86 import org.forester.msa.MsaFormatException;
87 import org.forester.phylogeny.Phylogeny;
88 import org.forester.phylogeny.PhylogenyMethods;
89 import org.forester.phylogeny.PhylogenyNode;
90 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
91 import org.forester.phylogeny.data.Confidence;
92 import org.forester.phylogeny.data.PhylogenyDataUtil;
93 import org.forester.phylogeny.data.Sequence;
94 import org.forester.phylogeny.data.Taxonomy;
95 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
96 import org.forester.phylogeny.factories.PhylogenyFactory;
97 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
98 import org.forester.sequence.MolecularSequence;
99 import org.forester.util.BasicDescriptiveStatistics;
100 import org.forester.util.BasicTable;
101 import org.forester.util.BasicTableParser;
102 import org.forester.util.DescriptiveStatistics;
103 import org.forester.util.ForesterUtil;
104
105 public final class MainFrameApplication extends MainFrame {
106
107     private final static int             FRAME_X_SIZE                    = 900;
108     private final static int             FRAME_Y_SIZE                    = 900;
109     // Filters for the file-open dialog (classes defined in this file)
110     private static final long            serialVersionUID                = -799735726778865234L;
111     private static final boolean         PREPROCESS_TREES                = false;
112     private final JFileChooser           _values_filechooser;
113     private final JFileChooser           _sequences_filechooser;
114     private final JFileChooser           _open_filechooser;
115     private final JFileChooser           _msa_filechooser;
116     private final JFileChooser           _seqs_pi_filechooser;
117     private final JFileChooser           _open_filechooser_for_species_tree;
118     // Application-only print menu items
119     private JMenuItem                    _collapse_below_threshold;
120     private JMenuItem                    _collapse_below_branch_length;
121     private ButtonGroup                  _radio_group_1;
122     private ButtonGroup                  _radio_group_2;
123     // Others:
124     double                               _min_not_collapse               = AptxConstants.MIN_NOT_COLLAPSE_DEFAULT;
125     double                               _min_not_collapse_bl            = 0.001;
126     // Phylogeny Inference menu
127     private JMenu                        _inference_menu;
128     private JMenuItem                    _inference_from_msa_item;
129     private JMenuItem                    _inference_from_seqs_item;
130     // Phylogeny Inference
131     private PhylogeneticInferenceOptions _phylogenetic_inference_options = null;
132     private Msa                          _msa                            = null;
133     private File                         _msa_file                       = null;
134     private List<MolecularSequence>      _seqs                           = null;
135     private File                         _seqs_file                      = null;
136     JMenuItem                            _read_values_jmi;
137     JMenuItem                            _read_seqs_jmi;
138
139     private MainFrameApplication( final Phylogeny[] phys, final Configuration config ) {
140         _configuration = config;
141         if ( _configuration == null ) {
142             throw new IllegalArgumentException( "configuration is null" );
143         }
144         setVisible( false );
145         setOptions( Options.createInstance( _configuration ) );
146         _mainpanel = new MainPanel( _configuration, this );
147         _open_filechooser = null;
148         _open_filechooser_for_species_tree = null;
149         _save_filechooser = null;
150         _writetopdf_filechooser = null;
151         _writetographics_filechooser = null;
152         _msa_filechooser = null;
153         _seqs_pi_filechooser = null;
154         _values_filechooser = null;
155         _sequences_filechooser = null;
156         _jmenubar = new JMenuBar();
157         buildFileMenu();
158         buildTypeMenu();
159         _contentpane = getContentPane();
160         _contentpane.setLayout( new BorderLayout() );
161         _contentpane.add( _mainpanel, BorderLayout.CENTER );
162         // App is this big
163         setSize( MainFrameApplication.FRAME_X_SIZE, MainFrameApplication.FRAME_Y_SIZE );
164         // The window listener
165         setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
166         addFrameListener( new FrameAdapter() {
167
168             @Override
169             public void FrameClosing () {
170              
171                 exit();
172             }
173         } );
174         //   setVisible( true );
175         if ( ( phys != null ) && ( phys.length > 0 ) ) {
176             AptxUtil.addPhylogeniesToTabs( phys, "", null, _configuration, _mainpanel );
177             validate();
178             getMainPanel().getControlPanel().showWholeAll();
179             getMainPanel().getControlPanel().showWhole();
180         }
181         //activateSaveAllIfNeeded();
182         // ...and its children
183         _contentpane.repaint();
184     }
185
186  
187
188     private MainFrameApplication( final Phylogeny[] phys, final Configuration config, final String title ) {
189         this( phys, config, title, null );
190     }
191
192     private MainFrameApplication( final Phylogeny[] phys,
193                                   final Configuration config,
194                                   final String title,
195                                   final File current_dir ) {
196         super();
197         _configuration = config;
198         if ( _configuration == null ) {
199             throw new IllegalArgumentException( "configuration is null" );
200         }
201         try {
202             if ( _configuration.isUseNativeUI() ) {
203                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
204             }
205             else {
206                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
207             }
208         }
209         catch ( final UnsupportedLookAndFeelException e ) {
210             AptxUtil.dieWithSystemError( "unsupported look and feel: " + e.toString() );
211         }
212         catch ( final ClassNotFoundException e ) {
213             AptxUtil.dieWithSystemError( "class not found exception: " + e.toString() );
214         }
215         catch ( final InstantiationException e ) {
216             AptxUtil.dieWithSystemError( "instantiation exception: " + e.toString() );
217         }
218         catch ( final IllegalAccessException e ) {
219             AptxUtil.dieWithSystemError( "illegal access exception: " + e.toString() );
220         }
221         if ( ( current_dir != null ) && current_dir.canRead() && current_dir.isDirectory() ) {
222             setCurrentDir( current_dir );
223         }
224         // hide until everything is ready
225         setVisible( false );
226         setOptions( Options.createInstance( _configuration ) );
227         setInferenceManager( InferenceManager.createInstance( _configuration ) );
228         setPhylogeneticInferenceOptions( PhylogeneticInferenceOptions.createInstance( _configuration ) );
229         // set title
230         setTitle( AptxConstants.PRG_NAME + " " + AptxConstants.VERSION + " (" + AptxConstants.PRG_DATE + ")" );
231         _mainpanel = new MainPanel( _configuration, this );
232         // The file dialogs
233         _open_filechooser = new JFileChooser();
234         _open_filechooser.setMultiSelectionEnabled( true );
235         _open_filechooser.addChoosableFileFilter( MainFrame.xmlfilter );
236         _open_filechooser.addChoosableFileFilter( MainFrame.nhxfilter );
237         _open_filechooser.addChoosableFileFilter( MainFrame.nhfilter );
238         _open_filechooser.addChoosableFileFilter( MainFrame.nexusfilter );
239         _open_filechooser.addChoosableFileFilter( MainFrame.tolfilter );
240         _open_filechooser.addChoosableFileFilter( _open_filechooser.getAcceptAllFileFilter() );
241         _open_filechooser.setFileFilter( MainFrame.defaultfilter );
242         _open_filechooser_for_species_tree = new JFileChooser();
243         _open_filechooser_for_species_tree.setMultiSelectionEnabled( false );
244         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrame.xmlfilter );
245         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrame.tolfilter );
246         _open_filechooser_for_species_tree.setFileFilter( MainFrame.xmlfilter );
247         // Msa:
248         _msa_filechooser = new JFileChooser();
249         _msa_filechooser.setName( "Read Multiple Sequence Alignment File" );
250         _msa_filechooser.setMultiSelectionEnabled( false );
251         _msa_filechooser.addChoosableFileFilter( _msa_filechooser.getAcceptAllFileFilter() );
252         _msa_filechooser.addChoosableFileFilter( MainFrame.msafilter );
253         // Seqs:
254         _seqs_pi_filechooser = new JFileChooser();
255         _seqs_pi_filechooser.setName( "Read Sequences File" );
256         _seqs_pi_filechooser.setMultiSelectionEnabled( false );
257         _seqs_pi_filechooser.addChoosableFileFilter( _seqs_pi_filechooser.getAcceptAllFileFilter() );
258         _seqs_pi_filechooser.addChoosableFileFilter( MainFrame.seqsfilter );
259         // Expression
260         _values_filechooser = new JFileChooser();
261         _values_filechooser.setMultiSelectionEnabled( false );
262         // Sequences
263         _sequences_filechooser = new JFileChooser();
264         _sequences_filechooser.setMultiSelectionEnabled( false );
265         try {
266             final String home_dir = System.getProperty( "user.home" );
267             _open_filechooser.setCurrentDirectory( new File( home_dir ) );
268             _open_filechooser_for_species_tree.setCurrentDirectory( new File( home_dir ) );
269             _msa_filechooser.setCurrentDirectory( new File( home_dir ) );
270             _seqs_pi_filechooser.setCurrentDirectory( new File( home_dir ) );
271             _values_filechooser.setCurrentDirectory( new File( home_dir ) );
272             _sequences_filechooser.setCurrentDirectory( new File( home_dir ) );
273         }
274         catch ( final Exception e ) {
275             e.printStackTrace();
276             // Do nothing. Not important.
277         }
278         // build the menu bar
279         _jmenubar = new JMenuBar();
280         if ( !_configuration.isUseNativeUI() ) {
281             _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
282         }
283         buildFileMenu();
284         if ( AptxConstants.__ALLOW_PHYLOGENETIC_INFERENCE ) {
285             buildPhylogeneticInferenceMenu();
286         }
287         buildAnalysisMenu();
288         buildToolsMenu();
289         buildViewMenu();
290         buildFontSizeMenu();
291         buildOptionsMenu();
292         buildTypeMenu();
293         buildHelpMenu();
294         setJMenuBar( _jmenubar );
295         _jmenubar.add( _help_jmenu );
296         _contentpane = getContentPane();
297         _contentpane.setLayout( new BorderLayout() );
298         _contentpane.add( _mainpanel, BorderLayout.CENTER );
299         // App is this big
300         setSize( MainFrameApplication.FRAME_X_SIZE, MainFrameApplication.FRAME_Y_SIZE );
301         //        addWindowFocusListener( new WindowAdapter() {
302         //
303         //            @Override
304         //            public void windowGainedFocus( WindowEvent e ) {
305         //                requestFocusInWindow();
306         //            }
307         //        } );
308         // The window listener
309         setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
310  
311         addFrameListener( new FrameAdapter() {
312
313             @Override
314             public void FrameClosing(  ) {
315                 if (MainFrameApplication.this.getParent() == null) {
316                 if ( isUnsavedDataPresent() ) {
317                     final int r = JOptionPane.showConfirmDialog( _mainpanel,
318                                                                  "Close Archaeopteryx despite potentially unsaved changes?",
319                                                                  "Close viewer?",
320                                                                  JOptionPane.YES_NO_OPTION );
321                     if ( r != JOptionPane.YES_OPTION ) {
322                         return;
323                     }
324                 }
325                 else {
326                     final int r = JOptionPane
327                             .showConfirmDialog( null, "Exit Archaeopteryx?", "Exit?", JOptionPane.YES_NO_OPTION );
328                     if ( r != JOptionPane.YES_OPTION ) {
329                         return;
330                     }
331                 }}
332                 exit();
333             
334         } });
335         // The component listener
336         addComponentListener( new ComponentAdapter() {
337
338             @Override
339             public void componentResized( final ComponentEvent e ) {
340                 if ( _mainpanel.getCurrentTreePanel() != null ) {
341                     _mainpanel.getCurrentTreePanel()
342                             .calcParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(),
343                                                         _mainpanel.getCurrentTreePanel().getHeight() );
344                 }
345             }
346         } );
347         requestFocusInWindow();
348         // addKeyListener( this );
349         setVisible( true );
350         if ( ( phys != null ) && ( phys.length > 0 ) ) {
351             AptxUtil.addPhylogeniesToTabs( phys, title, null, _configuration, _mainpanel );
352             validate();
353             getMainPanel().getControlPanel().showWholeAll();
354             getMainPanel().getControlPanel().showWhole();
355         }
356         activateSaveAllIfNeeded();
357         // ...and its children
358         _contentpane.repaint();
359         System.gc();
360     }
361
362
363
364  
365
366
367
368     private MainFrameApplication( final Phylogeny[] phys, final String config_file, final String title ) {
369         // Reads the config file (false, false => not url, not applet):
370         this( phys, new Configuration( config_file, false, false, true ), title );
371     }
372
373     @Override
374     public void actionPerformed( final ActionEvent e ) {
375         try {
376             super.actionPerformed( e );
377             final Object o = e.getSource();
378             // Handle app-specific actions here:
379             if ( o == _open_item ) {
380                 readPhylogeniesFromFile();
381             }
382             if ( o == _open_url_item ) {
383                 readPhylogeniesFromURL();
384             }
385             else if ( o == _new_item ) {
386                 newTree();
387             }
388             else if ( o == _close_item ) {
389                 closeCurrentPane();
390             }
391             else if ( o == _load_species_tree_item ) {
392                 readSpeciesTreeFromFile();
393             }
394             else if ( o == _obtain_detailed_taxonomic_information_jmi ) {
395                 if ( isSubtreeDisplayed() ) {
396                     return;
397                 }
398                 obtainDetailedTaxonomicInformation();
399             }
400             else if ( o == _obtain_detailed_taxonomic_information_deleting_jmi ) {
401                 if ( isSubtreeDisplayed() ) {
402                     return;
403                 }
404                 obtainDetailedTaxonomicInformationDelete();
405             }
406             else if ( o == _obtain_seq_information_jmi ) {
407                 obtainSequenceInformation();
408             }
409             else if ( o == _read_values_jmi ) {
410                 if ( isSubtreeDisplayed() ) {
411                     return;
412                 }
413                 addExpressionValuesFromFile();
414             }
415             else if ( o == _read_seqs_jmi ) {
416                 if ( isSubtreeDisplayed() ) {
417                     return;
418                 }
419                 addSequencesFromFile();
420             }
421             else if ( o == _move_node_names_to_tax_sn_jmi ) {
422                 moveNodeNamesToTaxSn();
423             }
424             else if ( o == _move_node_names_to_seq_names_jmi ) {
425                 moveNodeNamesToSeqNames();
426             }
427             else if ( o == _extract_tax_code_from_node_names_jmi ) {
428                 extractTaxDataFromNodeNames();
429             }
430             else if ( o == _internal_number_are_confidence_for_nh_parsing_cbmi ) {
431                 updateOptions( getOptions() );
432             }
433             else if ( o == _replace_underscores_cbmi ) {
434                 if ( ( _extract_taxonomy_no_rbmi != null ) && !_extract_taxonomy_no_rbmi.isSelected() ) {
435                     _extract_taxonomy_no_rbmi.setSelected( true );
436                 }
437                 updateOptions( getOptions() );
438             }
439             else if ( o == _allow_errors_in_distance_to_parent_cbmi ) {
440                 updateOptions( getOptions() );
441             }
442             else if ( o == _collapse_below_threshold ) {
443                 if ( isSubtreeDisplayed() ) {
444                     return;
445                 }
446                 collapseBelowThreshold();
447             }
448            
449             else if ( o == _collapse_below_branch_length ) {
450                 if ( isSubtreeDisplayed() ) {
451                     return;
452                 }
453                 collapseBelowBranchLengthThreshold();
454             }
455             else if ( ( o == _extract_taxonomy_pfam_strict_rbmi ) || ( o == _extract_taxonomy_pfam_relaxed_rbmi )
456                     || ( o == _extract_taxonomy_agressive_rbmi ) ) {
457                 if ( _replace_underscores_cbmi != null ) {
458                     _replace_underscores_cbmi.setSelected( false );
459                 }
460                 updateOptions( getOptions() );
461             }
462             else if ( o == _extract_taxonomy_no_rbmi ) {
463                 updateOptions( getOptions() );
464             }
465             else if ( o == _inference_from_msa_item ) {
466                 executePhyleneticInference( false );
467             }
468             else if ( o == _inference_from_seqs_item ) {
469                 executePhyleneticInference( true );
470             }
471             _contentpane.repaint();
472         }
473         catch ( final Exception ex ) {
474             AptxUtil.unexpectedException( ex );
475         }
476         catch ( final Error err ) {
477             AptxUtil.unexpectedError( err );
478         }
479     }
480
481     public void end() {
482         _mainpanel.terminate();
483         _contentpane.removeAll();
484         setVisible( false );
485         dispose();
486     }
487
488     @Override
489     public MainPanel getMainPanel() {
490         return _mainpanel;
491     }
492
493     public Msa getMsa() {
494         return _msa;
495     }
496
497     public File getMsaFile() {
498         return _msa_file;
499     }
500
501     public List<MolecularSequence> getSeqs() {
502         return _seqs;
503     }
504
505     public File getSeqsFile() {
506         return _seqs_file;
507     }
508
509     public void readMsaFromFile() {
510         // Set an initial directory if none set yet
511         final File my_dir = getCurrentDir();
512         _msa_filechooser.setMultiSelectionEnabled( false );
513         // Open file-open dialog and set current directory
514         if ( my_dir != null ) {
515             _msa_filechooser.setCurrentDirectory( my_dir );
516         }
517         final int result = _msa_filechooser.showOpenDialog( _contentpane );
518         // All done: get the msa
519         final File file = _msa_filechooser.getSelectedFile();
520         setCurrentDir( _msa_filechooser.getCurrentDirectory() );
521         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
522             setMsaFile( null );
523             setMsa( null );
524             Msa msa = null;
525             try {
526                 final InputStream is = new FileInputStream( file );
527                 if ( FastaParser.isLikelyFasta( file ) ) {
528                     msa = FastaParser.parseMsa( is );
529                 }
530                 else {
531                     msa = GeneralMsaParser.parseMsa( is );
532                 }
533             }
534             catch ( final MsaFormatException e ) {
535                 setArrowCursor();
536                 JOptionPane.showMessageDialog( getThisFrame(),
537                                                e.getLocalizedMessage(),
538                                                "Multiple sequence alignment format error",
539                                                JOptionPane.ERROR_MESSAGE );
540                 return;
541             }
542             catch ( final IOException e ) {
543                 setArrowCursor();
544                 JOptionPane.showMessageDialog( getThisFrame(),
545                                                e.getLocalizedMessage(),
546                                                "Failed to read multiple sequence alignment",
547                                                JOptionPane.ERROR_MESSAGE );
548                 return;
549             }
550             catch ( final IllegalArgumentException e ) {
551                 setArrowCursor();
552                 JOptionPane.showMessageDialog( getThisFrame(),
553                                                e.getLocalizedMessage(),
554                                                "Unexpected error during reading of multiple sequence alignment",
555                                                JOptionPane.ERROR_MESSAGE );
556                 return;
557             }
558             catch ( final Exception e ) {
559                 setArrowCursor();
560                 e.printStackTrace();
561                 JOptionPane.showMessageDialog( getThisFrame(),
562                                                e.getLocalizedMessage(),
563                                                "Unexpected error during reading of multiple sequence alignment",
564                                                JOptionPane.ERROR_MESSAGE );
565                 return;
566             }
567             if ( ( msa == null ) || ( msa.getNumberOfSequences() < 1 ) ) {
568                 JOptionPane.showMessageDialog( getThisFrame(),
569                                                "Multiple sequence alignment is empty",
570                                                "Illegal Multiple Sequence Alignment",
571                                                JOptionPane.ERROR_MESSAGE );
572                 return;
573             }
574             if ( msa.getNumberOfSequences() < 4 ) {
575                 JOptionPane.showMessageDialog( getThisFrame(),
576                                                "Multiple sequence alignment needs to contain at least 3 sequences",
577                                                "Illegal multiple sequence alignment",
578                                                JOptionPane.ERROR_MESSAGE );
579                 return;
580             }
581             if ( msa.getLength() < 2 ) {
582                 JOptionPane.showMessageDialog( getThisFrame(),
583                                                "Multiple sequence alignment needs to contain at least 2 residues",
584                                                "Illegal multiple sequence alignment",
585                                                JOptionPane.ERROR_MESSAGE );
586                 return;
587             }
588             System.gc();
589             setMsaFile( _msa_filechooser.getSelectedFile() );
590             setMsa( msa );
591         }
592     }
593
594     public void readSeqsFromFileforPI() {
595         // Set an initial directory if none set yet
596         final File my_dir = getCurrentDir();
597         _seqs_pi_filechooser.setMultiSelectionEnabled( false );
598         // Open file-open dialog and set current directory
599         if ( my_dir != null ) {
600             _seqs_pi_filechooser.setCurrentDirectory( my_dir );
601         }
602         final int result = _seqs_pi_filechooser.showOpenDialog( _contentpane );
603         // All done: get the seqs
604         final File file = _seqs_pi_filechooser.getSelectedFile();
605         setCurrentDir( _seqs_pi_filechooser.getCurrentDirectory() );
606         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
607             setSeqsFile( null );
608             setSeqs( null );
609             List<MolecularSequence> seqs = null;
610             try {
611                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
612                     seqs = FastaParser.parse( new FileInputStream( file ) );
613                     for( final MolecularSequence seq : seqs ) {
614                         System.out.println( SequenceWriter.toFasta( seq, 60 ) );
615                     }
616                 }
617                 else {
618                     //TODO error
619                 }
620             }
621             catch ( final MsaFormatException e ) {
622                 setArrowCursor();
623                 JOptionPane.showMessageDialog( getThisFrame(),
624                                                e.getLocalizedMessage(),
625                                                "Multiple sequence file format error",
626                                                JOptionPane.ERROR_MESSAGE );
627                 return;
628             }
629             catch ( final IOException e ) {
630                 setArrowCursor();
631                 JOptionPane.showMessageDialog( getThisFrame(),
632                                                e.getLocalizedMessage(),
633                                                "Failed to read multiple sequence file",
634                                                JOptionPane.ERROR_MESSAGE );
635                 return;
636             }
637             catch ( final IllegalArgumentException e ) {
638                 setArrowCursor();
639                 JOptionPane.showMessageDialog( getThisFrame(),
640                                                e.getLocalizedMessage(),
641                                                "Unexpected error during reading of multiple sequence file",
642                                                JOptionPane.ERROR_MESSAGE );
643                 return;
644             }
645             catch ( final Exception e ) {
646                 setArrowCursor();
647                 e.printStackTrace();
648                 JOptionPane.showMessageDialog( getThisFrame(),
649                                                e.getLocalizedMessage(),
650                                                "Unexpected error during reading of multiple sequence file",
651                                                JOptionPane.ERROR_MESSAGE );
652                 return;
653             }
654             if ( ( seqs == null ) || ( seqs.size() < 1 ) ) {
655                 JOptionPane.showMessageDialog( getThisFrame(),
656                                                "Multiple sequence file is empty",
657                                                "Illegal multiple sequence file",
658                                                JOptionPane.ERROR_MESSAGE );
659                 return;
660             }
661             if ( seqs.size() < 4 ) {
662                 JOptionPane.showMessageDialog( getThisFrame(),
663                                                "Multiple sequence file needs to contain at least 3 sequences",
664                                                "Illegal multiple sequence file",
665                                                JOptionPane.ERROR_MESSAGE );
666                 return;
667             }
668             //  if ( msa.getLength() < 2 ) {
669             //       JOptionPane.showMessageDialog( this,
670             //                                      "Multiple sequence alignment needs to contain at least 2 residues",
671             //                                      "Illegal multiple sequence file",
672             //                                      JOptionPane.ERROR_MESSAGE );
673             //       return;
674             //   }
675             System.gc();
676             setSeqsFile( _seqs_pi_filechooser.getSelectedFile() );
677             setSeqs( seqs );
678         }
679     }
680
681     private void addExpressionValuesFromFile() {
682         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) {
683             JOptionPane.showMessageDialog( getThisFrame(),
684                                            "Need to load evolutionary tree first",
685                                            "Can Not Read Expression Values",
686                                            JOptionPane.WARNING_MESSAGE );
687             return;
688         }
689         final File my_dir = getCurrentDir();
690         if ( my_dir != null ) {
691             _values_filechooser.setCurrentDirectory( my_dir );
692         }
693         final int result = _values_filechooser.showOpenDialog( _contentpane );
694         final File file = _values_filechooser.getSelectedFile();
695         if ( ( file != null ) && ( file.length() > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
696             BasicTable<String> t = null;
697             try {
698                 t = BasicTableParser.parse( file, '\t' );
699                 if ( t.getNumberOfColumns() < 2 ) {
700                     t = BasicTableParser.parse( file, ',' );
701                 }
702                 if ( t.getNumberOfColumns() < 2 ) {
703                     t = BasicTableParser.parse( file, ' ' );
704                 }
705             }
706             catch ( final IOException e ) {
707                 JOptionPane.showMessageDialog( getThisFrame(),
708                                                e.getMessage(),
709                                                "Could Not Read Expression Value Table",
710                                                JOptionPane.ERROR_MESSAGE );
711                 return;
712             }
713             if ( t.getNumberOfColumns() < 2 ) {
714                 JOptionPane.showMessageDialog( getThisFrame(),
715                                                "Table contains " + t.getNumberOfColumns() + " column(s)",
716                                                "Problem with Expression Value Table",
717                                                JOptionPane.ERROR_MESSAGE );
718                 return;
719             }
720             if ( t.getNumberOfRows() < 1 ) {
721                 JOptionPane.showMessageDialog( getThisFrame(),
722                                                "Table contains zero rows",
723                                                "Problem with Expression Value Table",
724                                                JOptionPane.ERROR_MESSAGE );
725                 return;
726             }
727             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
728             if ( t.getNumberOfRows() != phy.getNumberOfExternalNodes() ) {
729                 JOptionPane.showMessageDialog( getThisFrame(),
730                                                "Table contains " + t.getNumberOfRows() + " rows, but tree contains "
731                                                        + phy.getNumberOfExternalNodes() + " external nodes",
732                                                "Warning",
733                                                JOptionPane.WARNING_MESSAGE );
734             }
735             final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
736             int not_found = 0;
737             for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
738                 final PhylogenyNode node = iter.next();
739                 final String node_name = node.getName();
740                 if ( !ForesterUtil.isEmpty( node_name ) ) {
741                     int row = -1;
742                     try {
743                         row = t.findRow( node_name );
744                     }
745                     catch ( final IllegalArgumentException e ) {
746                         JOptionPane.showMessageDialog( getThisFrame(),
747                                                        e.getMessage(),
748                                                        "Error Mapping Node Identifiers to Expression Value Identifiers",
749                                                        JOptionPane.ERROR_MESSAGE );
750                         return;
751                     }
752                     if ( row < 0 ) {
753                         if ( node.isExternal() ) {
754                             not_found++;
755                         }
756                         continue;
757                     }
758                     final List<Double> l = new ArrayList<Double>();
759                     for( int col = 1; col < t.getNumberOfColumns(); ++col ) {
760                         double d = -100;
761                         try {
762                             d = Double.parseDouble( t.getValueAsString( col, row ) );
763                         }
764                         catch ( final NumberFormatException e ) {
765                             JOptionPane.showMessageDialog( getThisFrame(),
766                                                            "Could not parse \"" + t.getValueAsString( col, row )
767                                                                    + "\" into a decimal value",
768                                                            "Issue with Expression Value Table",
769                                                            JOptionPane.ERROR_MESSAGE );
770                             return;
771                         }
772                         stats.addValue( d );
773                         l.add( d );
774                     }
775                     if ( !l.isEmpty() ) {
776                        
777                         node.getNodeData().setVector( l );
778                     }
779                 }
780             }
781             if ( not_found > 0 ) {
782                 JOptionPane
783                         .showMessageDialog( getThisFrame(),
784                                             "Could not fine expression values for " + not_found + " external node(s)",
785                                             "Warning",
786                                             JOptionPane.WARNING_MESSAGE );
787             }
788             getCurrentTreePanel().setStatisticsForExpressionValues( stats );
789         }
790     }
791
792     private void addSequencesFromFile() {
793         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) {
794             JOptionPane.showMessageDialog( getThisFrame(),
795                                            "Need to load evolutionary tree first",
796                                            "Can Not Read Sequences",
797                                            JOptionPane.WARNING_MESSAGE );
798             return;
799         }
800         final File my_dir = getCurrentDir();
801         if ( my_dir != null ) {
802             _sequences_filechooser.setCurrentDirectory( my_dir );
803         }
804         final int result = _sequences_filechooser.showOpenDialog( _contentpane );
805         final File file = _sequences_filechooser.getSelectedFile();
806         List<MolecularSequence> seqs = null;
807         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
808             try {
809                 final FileInputStream fis1 = new FileInputStream( file );
810                 if ( FastaParser.isLikelyFasta( fis1 ) ) {
811                     final FileInputStream fis2 = new FileInputStream( file );
812                     seqs = FastaParser.parse( fis2 );
813                     try {
814                         fis2.close();
815                     }
816                     catch ( final Exception e ) {
817                         // Ignore.
818                     }
819                 }
820                 else {
821                     JOptionPane.showMessageDialog( getThisFrame(),
822                                                    "Format does not appear to be Fasta",
823                                                    "Multiple sequence file format error",
824                                                    JOptionPane.ERROR_MESSAGE );
825                     return;
826                 }
827                 try {
828                     fis1.close();
829                 }
830                 catch ( final Exception e ) {
831                     // Ignore.
832                 }
833             }
834             catch ( final MsaFormatException e ) {
835                 setArrowCursor();
836                 JOptionPane.showMessageDialog( getThisFrame(),
837                                                e.getLocalizedMessage(),
838                                                "Multiple sequence file format error",
839                                                JOptionPane.ERROR_MESSAGE );
840                 return;
841             }
842             catch ( final IOException e ) {
843                 setArrowCursor();
844                 JOptionPane.showMessageDialog( getThisFrame(),
845                                                e.getLocalizedMessage(),
846                                                "Failed to read multiple sequence file",
847                                                JOptionPane.ERROR_MESSAGE );
848                 return;
849             }
850             catch ( final Exception e ) {
851                 setArrowCursor();
852                 e.printStackTrace();
853                 JOptionPane.showMessageDialog( getThisFrame(),
854                                                e.getLocalizedMessage(),
855                                                "Unexpected error during reading of multiple sequence file",
856                                                JOptionPane.ERROR_MESSAGE );
857                 return;
858             }
859             if ( ( seqs == null ) || ( seqs.size() < 1 ) ) {
860                 JOptionPane.showMessageDialog( getThisFrame(),
861                                                "Multiple sequence file is empty",
862                                                "Empty multiple sequence file",
863                                                JOptionPane.ERROR_MESSAGE );
864                 setArrowCursor();
865                 return;
866             }
867         }
868         if ( seqs != null ) {
869             for( final MolecularSequence seq : seqs ) {
870                 System.out.println( seq.getIdentifier() );
871             }
872             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
873             int total_counter = 0;
874             int attached_counter = 0;
875             for( final MolecularSequence seq : seqs ) {
876                 ++total_counter;
877                 final String seq_name = seq.getIdentifier();
878                 if ( !ForesterUtil.isEmpty( seq_name ) ) {
879                     List<PhylogenyNode> nodes = phy.getNodesViaSequenceName( seq_name );
880                     if ( nodes.isEmpty() ) {
881                         nodes = phy.getNodesViaSequenceSymbol( seq_name );
882                     }
883                     if ( nodes.isEmpty() ) {
884                         nodes = phy.getNodesViaGeneName( seq_name );
885                     }
886                     if ( nodes.isEmpty() ) {
887                         nodes = phy.getNodes( seq_name );
888                     }
889                     if ( nodes.size() > 1 ) {
890                         JOptionPane.showMessageDialog( getThisFrame(),
891                                                        "Sequence name \"" + seq_name + "\" is not unique",
892                                                        "Sequence name not unique",
893                                                        JOptionPane.ERROR_MESSAGE );
894                         setArrowCursor();
895                         return;
896                     }
897                     final String[] a = seq_name.split( "\\s" );
898                     if ( nodes.isEmpty() && ( a.length > 1 ) ) {
899                         final String seq_name_split = a[ 0 ];
900                         nodes = phy.getNodesViaSequenceName( seq_name_split );
901                         if ( nodes.isEmpty() ) {
902                             nodes = phy.getNodesViaSequenceSymbol( seq_name_split );
903                         }
904                         if ( nodes.isEmpty() ) {
905                             nodes = phy.getNodes( seq_name_split );
906                         }
907                         if ( nodes.size() > 1 ) {
908                             JOptionPane.showMessageDialog( getThisFrame(),
909                                                            "Split sequence name \"" + seq_name_split
910                                                                    + "\" is not unique",
911                                                            "Sequence name not unique",
912                                                            JOptionPane.ERROR_MESSAGE );
913                             setArrowCursor();
914                             return;
915                         }
916                     }
917                     if ( nodes.size() == 1 ) {
918                         ++attached_counter;
919                         final PhylogenyNode n = nodes.get( 0 );
920                         if ( !n.getNodeData().isHasSequence() ) {
921                             n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() );
922                         }
923                         n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() );
924                         if ( ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
925                             n.getNodeData().getSequence().setName( seq_name );
926                         }
927                     }
928                 }
929             }
930             if ( attached_counter > 0 ) {
931                 int ext_nodes = 0;
932                 int ext_nodes_with_seq = 0;
933                 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
934                     ++ext_nodes;
935                     final PhylogenyNode n = iter.next();
936                     if ( n.getNodeData().isHasSequence()
937                             && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getMolecularSequence() ) ) {
938                         ++ext_nodes_with_seq;
939                     }
940                 }
941                 final String s;
942                 if ( ext_nodes == ext_nodes_with_seq ) {
943                     s = "All " + ext_nodes_with_seq + " external nodes now have a molecular sequence attached to them.";
944                 }
945                 else {
946                     s = ext_nodes_with_seq + " out of " + ext_nodes
947                             + " external nodes now have a molecular sequence attached to them.";
948                 }
949                 if ( ( attached_counter == total_counter ) && ( ext_nodes == ext_nodes_with_seq ) ) {
950                     JOptionPane.showMessageDialog( getThisFrame(),
951                                                    "Attached all " + total_counter + " sequences to tree nodes.\n" + s,
952                                                    "All sequences attached",
953                                                    JOptionPane.INFORMATION_MESSAGE );
954                 }
955                 else {
956                     JOptionPane.showMessageDialog( getThisFrame(),
957                                                    "Attached " + attached_counter + " sequences out of a total of "
958                                                            + total_counter + " sequences.\n" + s,
959                                                    attached_counter + " sequences attached",
960                                                    JOptionPane.WARNING_MESSAGE );
961                 }
962             }
963             else {
964                 JOptionPane.showMessageDialog( getThisFrame(),
965                                                "No maching tree node for any of the " + total_counter + " sequences",
966                                                "Could not attach any sequences",
967                                                JOptionPane.ERROR_MESSAGE );
968             }
969         }
970     }
971
972     private void closeCurrentPane() {
973         if ( getMainPanel().getCurrentTreePanel() != null ) {
974             if ( getMainPanel().getCurrentTreePanel().isEdited() ) {
975                 final int r = JOptionPane.showConfirmDialog( getThisFrame(),
976                                                              "Close tab despite potentially unsaved changes?",
977                                                              "Close Tab?",
978                                                              JOptionPane.YES_NO_OPTION );
979                 if ( r != JOptionPane.YES_OPTION ) {
980                     return;
981                 }
982             }
983             getMainPanel().closeCurrentPane();
984             activateSaveAllIfNeeded();
985         }
986     }
987
988     private void collapseBelowThreshold( final Phylogeny phy ) {
989         final PhylogenyNodeIterator it = phy.iteratorPostorder();
990         final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();
991         double min_support = Double.MAX_VALUE;
992         boolean conf_present = false;
993         while ( it.hasNext() ) {
994             final PhylogenyNode n = it.next();
995             if ( !n.isExternal() && !n.isRoot() ) {
996                 final List<Confidence> c = n.getBranchData().getConfidences();
997                 if ( ( c != null ) && ( c.size() > 0 ) ) {
998                     conf_present = true;
999                     double max = 0;
1000                     for( final Confidence confidence : c ) {
1001                         if ( confidence.getValue() > max ) {
1002                             max = confidence.getValue();
1003                         }
1004                     }
1005                     if ( max < getMinNotCollapseConfidenceValue() ) {
1006                         to_be_removed.add( n );
1007                     }
1008                     if ( max < min_support ) {
1009                         min_support = max;
1010                     }
1011                 }
1012             }
1013         }
1014         if ( conf_present ) {
1015             for( final PhylogenyNode node : to_be_removed ) {
1016                 PhylogenyMethods.removeNode( node, phy );
1017             }
1018             if ( to_be_removed.size() > 0 ) {
1019                 phy.externalNodesHaveChanged();
1020                 phy.clearHashIdToNodeMap();
1021                 phy.recalculateNumberOfExternalDescendants( true );
1022                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
1023                 getCurrentTreePanel().updateSetOfCollapsedExternalNodes();
1024                 getCurrentTreePanel().calculateLongestExtNodeInfo();
1025                 getCurrentTreePanel().setNodeInPreorderToNull();
1026                 getCurrentTreePanel().recalculateMaxDistanceToRoot();
1027                 getCurrentTreePanel().resetPreferredSize();
1028                 getCurrentTreePanel().setEdited( true );
1029                 getCurrentTreePanel().repaint();
1030                 repaint();
1031             }
1032             if ( to_be_removed.size() > 0 ) {
1033                 JOptionPane.showMessageDialog( getThisFrame(),
1034                                                "Collapsed " + to_be_removed.size()
1035                                                        + " branches with\nconfidence values below "
1036                                                        + getMinNotCollapseConfidenceValue(),
1037                                                "Collapsed " + to_be_removed.size() + " branches",
1038                                                JOptionPane.INFORMATION_MESSAGE );
1039             }
1040             else {
1041                 JOptionPane.showMessageDialog( getThisFrame(),
1042                                                "No branch collapsed,\nminimum confidence value per branch is "
1043                                                        + min_support,
1044                                                "No branch collapsed",
1045                                                JOptionPane.INFORMATION_MESSAGE );
1046             }
1047         }
1048         else {
1049             JOptionPane.showMessageDialog( getThisFrame(),
1050                                            "No branch collapsed because no confidence values present",
1051                                            "No confidence values present",
1052                                            JOptionPane.INFORMATION_MESSAGE );
1053         }
1054     }
1055
1056     private void collapseBelowBranchLengthThreshold() {
1057         if ( getCurrentTreePanel() != null ) {
1058             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1059             if ( ( phy != null ) && !phy.isEmpty() ) {
1060                 final String s = ( String ) JOptionPane.showInputDialog( getThisFrame(),
1061                                                                          "Please enter the minimum branch length value\n",
1062                                                                          "Minimal Branch Length Value",
1063                                                                          JOptionPane.QUESTION_MESSAGE,
1064                                                                          null,
1065                                                                          null,
1066                                                                          getMinNotCollapseBlValue() );
1067                 if ( !ForesterUtil.isEmpty( s ) ) {
1068                     boolean success = true;
1069                     double m = 0.0;
1070                     final String m_str = s.trim();
1071                     if ( !ForesterUtil.isEmpty( m_str ) ) {
1072                         try {
1073                             m = Double.parseDouble( m_str );
1074                         }
1075                         catch ( final Exception ex ) {
1076                             success = false;
1077                         }
1078                     }
1079                     else {
1080                         success = false;
1081                     }
1082                     if ( success && ( m >= 0.0 ) ) {
1083                         setMinNotCollapseBlValue( m );
1084                         collapseBl( phy );
1085                     }
1086                 }
1087             }
1088         }
1089     }
1090
1091     private void collapseBelowThreshold() {
1092         if ( getCurrentTreePanel() != null ) {
1093             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1094             if ( ( phy != null ) && !phy.isEmpty() ) {
1095                 final String s = ( String ) JOptionPane.showInputDialog( getThisFrame(),
1096                                                                          "Please enter the minimum confidence value\n",
1097                                                                          "Minimal Confidence Value",
1098                                                                          JOptionPane.QUESTION_MESSAGE,
1099                                                                          null,
1100                                                                          null,
1101                                                                          getMinNotCollapseConfidenceValue() );
1102                 if ( !ForesterUtil.isEmpty( s ) ) {
1103                     boolean success = true;
1104                     double m = 0.0;
1105                     final String m_str = s.trim();
1106                     if ( !ForesterUtil.isEmpty( m_str ) ) {
1107                         try {
1108                             m = Double.parseDouble( m_str );
1109                         }
1110                         catch ( final Exception ex ) {
1111                             success = false;
1112                         }
1113                     }
1114                     else {
1115                         success = false;
1116                     }
1117                     if ( success && ( m >= 0.0 ) ) {
1118                         setMinNotCollapseConfidenceValue( m );
1119                         collapseBelowThreshold( phy );
1120                     }
1121                 }
1122             }
1123         }
1124     }
1125
1126     private void collapseBl( final Phylogeny phy ) {
1127         final PhylogenyNodeIterator it = phy.iteratorPostorder();
1128         final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();
1129         double min_bl = Double.MAX_VALUE;
1130         boolean bl_present = false;
1131         while ( it.hasNext() ) {
1132             final PhylogenyNode n = it.next();
1133             if ( !n.isExternal() && !n.isRoot() ) {
1134                 final double bl = n.getDistanceToParent();
1135                 if ( bl != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1136                     bl_present = true;
1137                     if ( bl < getMinNotCollapseBlValue() ) {
1138                         to_be_removed.add( n );
1139                     }
1140                     if ( bl < min_bl ) {
1141                         min_bl = bl;
1142                     }
1143                 }
1144             }
1145         }
1146         if ( bl_present ) {
1147             for( final PhylogenyNode node : to_be_removed ) {
1148                 PhylogenyMethods.removeNode( node, phy );
1149             }
1150             if ( to_be_removed.size() > 0 ) {
1151                 phy.externalNodesHaveChanged();
1152                 phy.clearHashIdToNodeMap();
1153                 phy.recalculateNumberOfExternalDescendants( true );
1154                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
1155                 getCurrentTreePanel().updateSetOfCollapsedExternalNodes();
1156                 getCurrentTreePanel().calculateLongestExtNodeInfo();
1157                 getCurrentTreePanel().setNodeInPreorderToNull();
1158                 getCurrentTreePanel().recalculateMaxDistanceToRoot();
1159                 getCurrentTreePanel().resetPreferredSize();
1160                 getCurrentTreePanel().setEdited( true );
1161                 getCurrentTreePanel().repaint();
1162                 repaint();
1163             }
1164             if ( to_be_removed.size() > 0 ) {
1165                 JOptionPane.showMessageDialog( getThisFrame(),
1166                                                "Collapsed " + to_be_removed.size()
1167                                                        + " branches with\nbranch length values below "
1168                                                        + getMinNotCollapseBlValue(),
1169                                                "Collapsed " + to_be_removed.size() + " branches",
1170                                                JOptionPane.INFORMATION_MESSAGE );
1171             }
1172             else {
1173                 JOptionPane.showMessageDialog( getThisFrame(),
1174                                                "No branch collapsed,\nminimum branch length is " + min_bl,
1175                                                "No branch collapsed",
1176                                                JOptionPane.INFORMATION_MESSAGE );
1177             }
1178         }
1179         else {
1180             JOptionPane.showMessageDialog( getThisFrame(),
1181                                            "No branch collapsed because no branch length values present",
1182                                            "No branch length values present",
1183                                            JOptionPane.INFORMATION_MESSAGE );
1184         }
1185     }
1186
1187     private PhyloXmlParser createPhyloXmlParser() {
1188         PhyloXmlParser xml_parser = null;
1189         if ( getConfiguration().isValidatePhyloXmlAgainstSchema() ) {
1190             try {
1191                 xml_parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
1192             }
1193             catch ( final Exception e ) {
1194                 JOptionPane.showMessageDialog( getThisFrame(),
1195                                                e.getLocalizedMessage(),
1196                                                "failed to create validating XML parser",
1197                                                JOptionPane.WARNING_MESSAGE );
1198             }
1199         }
1200         if ( xml_parser == null ) {
1201             xml_parser = PhyloXmlParser.createPhyloXmlParser();
1202         }
1203         return xml_parser;
1204     }
1205
1206     private void executePhyleneticInference( final boolean from_unaligned_seqs ) {
1207         final PhyloInferenceDialog dialog = new PhyloInferenceDialog( this,
1208                                                                       getPhylogeneticInferenceOptions(),
1209                                                                       from_unaligned_seqs );
1210         dialog.activate();
1211         if ( dialog.getValue() == JOptionPane.OK_OPTION ) {
1212             if ( !from_unaligned_seqs ) {
1213                 if ( getMsa() != null ) {
1214                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getMsa(),
1215                                                                                     getPhylogeneticInferenceOptions()
1216                                                                                             .copy(),
1217                                                                                     this );
1218                     new Thread( inferrer ).start();
1219                 }
1220                 else {
1221                     JOptionPane.showMessageDialog( getThisFrame(),
1222                                                    "No multiple sequence alignment selected",
1223                                                    "Phylogenetic Inference Not Launched",
1224                                                    JOptionPane.WARNING_MESSAGE );
1225                 }
1226             }
1227             else {
1228                 if ( getSeqs() != null ) {
1229                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getSeqs(),
1230                                                                                     getPhylogeneticInferenceOptions()
1231                                                                                             .copy(),
1232                                                                                     this );
1233                     new Thread( inferrer ).start();
1234                 }
1235                 else {
1236                     JOptionPane.showMessageDialog( getThisFrame(),
1237                                                    "No input sequences selected",
1238                                                    "Phylogenetic Inference Not Launched",
1239                                                    JOptionPane.WARNING_MESSAGE );
1240                 }
1241             }
1242         }
1243     }
1244
1245     private void extractTaxDataFromNodeNames() throws PhyloXmlDataFormatException {
1246         final StringBuilder sb = new StringBuilder();
1247         final StringBuilder sb_failed = new StringBuilder();
1248         int counter = 0;
1249         int counter_failed = 0;
1250         if ( getCurrentTreePanel() != null ) {
1251             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1252             if ( ( phy != null ) && !phy.isEmpty() ) {
1253                 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
1254                 while ( it.hasNext() ) {
1255                     final PhylogenyNode n = it.next();
1256                     final String name = n.getName().trim();
1257                     if ( !ForesterUtil.isEmpty( name ) ) {
1258                         final String nt = ParserUtils.extractTaxonomyDataFromNodeName( n,
1259                                                                                        TAXONOMY_EXTRACTION.AGGRESSIVE );
1260                         if ( !ForesterUtil.isEmpty( nt ) ) {
1261                             if ( counter < 15 ) {
1262                                 sb.append( name + ": " + nt + "\n" );
1263                             }
1264                             else if ( counter == 15 ) {
1265                                 sb.append( "...\n" );
1266                             }
1267                             counter++;
1268                         }
1269                         else {
1270                             if ( counter_failed < 15 ) {
1271                                 sb_failed.append( name + "\n" );
1272                             }
1273                             else if ( counter_failed == 15 ) {
1274                                 sb_failed.append( "...\n" );
1275                             }
1276                             counter_failed++;
1277                         }
1278                     }
1279                 }
1280                 if ( counter > 0 ) {
1281                     String failed = "";
1282                     String all = "all ";
1283                     if ( counter_failed > 0 ) {
1284                         all = "";
1285                         failed = "\nCould not extract taxonomic data for " + counter_failed + " named external nodes:\n"
1286                                 + sb_failed;
1287                     }
1288                     JOptionPane.showMessageDialog( getThisFrame(),
1289                                                    "Extracted taxonomic data from " + all + counter
1290                                                            + " named external nodes:\n" + sb.toString() + failed,
1291                                                    "Taxonomic Data Extraction Completed",
1292                                                    counter_failed > 0 ? JOptionPane.WARNING_MESSAGE
1293                                                            : JOptionPane.INFORMATION_MESSAGE );
1294                 }
1295                 else {
1296                     JOptionPane.showMessageDialog( getThisFrame(),
1297                                                    "Could not extract any taxonomic data.\nMaybe node names are empty\n"
1298                                                            + "or not in the forms \"XYZ_CAEEL\", \"XYZ_6239\", or \"XYZ_Caenorhabditis_elegans\"\n"
1299                                                            + "or nodes already have taxonomic data?\n",
1300                                                    "No Taxonomic Data Extracted",
1301                                                    JOptionPane.ERROR_MESSAGE );
1302                 }
1303             }
1304         }
1305     }
1306
1307     private double getMinNotCollapseBlValue() {
1308         return _min_not_collapse_bl;
1309     }
1310
1311     private double getMinNotCollapseConfidenceValue() {
1312         return _min_not_collapse;
1313     }
1314
1315     private PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {
1316         if ( _phylogenetic_inference_options == null ) {
1317             _phylogenetic_inference_options = new PhylogeneticInferenceOptions();
1318         }
1319         return _phylogenetic_inference_options;
1320     }
1321
1322     private boolean isUnsavedDataPresent() {
1323         final List<TreePanel> tps = getMainPanel().getTreePanels();
1324         for( final TreePanel tp : tps ) {
1325             if ( tp.isEdited() ) {
1326                 return true;
1327             }
1328         }
1329         return false;
1330     }
1331
1332     private void moveNodeNamesToSeqNames() throws PhyloXmlDataFormatException {
1333         if ( getCurrentTreePanel() != null ) {
1334             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1335             if ( ( phy != null ) && !phy.isEmpty() ) {
1336                 PhylogenyMethods.transferNodeNameToField( phy,
1337                                                           PhylogenyMethods.PhylogenyNodeField.SEQUENCE_NAME,
1338                                                           false );
1339             }
1340         }
1341     }
1342
1343     private void moveNodeNamesToTaxSn() throws PhyloXmlDataFormatException {
1344         if ( getCurrentTreePanel() != null ) {
1345             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1346             if ( ( phy != null ) && !phy.isEmpty() ) {
1347                 PhylogenyMethods.transferNodeNameToField( phy,
1348                                                           PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
1349                                                           false );
1350             }
1351         }
1352     }
1353
1354     private void newTree() {
1355         final Phylogeny[] phys = new Phylogeny[ 1 ];
1356         final Phylogeny phy = new Phylogeny();
1357         final PhylogenyNode node = new PhylogenyNode();
1358         phy.setRoot( node );
1359         phy.setRooted( true );
1360         phys[ 0 ] = phy;
1361         AptxUtil.addPhylogeniesToTabs( phys, "", "", getConfiguration(), getMainPanel() );
1362         _mainpanel.getControlPanel().showWhole();
1363         _mainpanel.getCurrentTreePanel().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1364         _mainpanel.getOptions().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1365        
1366         getMainPanel().getMainFrame().setSelectedTypeInTypeMenu( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1367        
1368         activateSaveAllIfNeeded();
1369         System.gc();
1370     }
1371
1372     private void obtainDetailedTaxonomicInformation() {
1373         if ( getCurrentTreePanel() != null ) {
1374             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1375             if ( ( phy != null ) && !phy.isEmpty() ) {
1376                 final TaxonomyDataManager t = new TaxonomyDataManager( this,
1377                                                                        _mainpanel.getCurrentTreePanel(),
1378                                                                        phy.copy(),
1379                                                                        false,
1380                                                                        true );
1381                 new Thread( t ).start();
1382             }
1383         }
1384     }
1385
1386     private void obtainDetailedTaxonomicInformationDelete() {
1387         if ( getCurrentTreePanel() != null ) {
1388             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1389             if ( ( phy != null ) && !phy.isEmpty() ) {
1390                 final TaxonomyDataManager t = new TaxonomyDataManager( this,
1391                                                                        _mainpanel.getCurrentTreePanel(),
1392                                                                        phy.copy(),
1393                                                                        true,
1394                                                                        true );
1395                 new Thread( t ).start();
1396             }
1397         }
1398     }
1399
1400     private void obtainSequenceInformation() {
1401         if ( getCurrentTreePanel() != null ) {
1402             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1403             if ( ( phy != null ) && !phy.isEmpty() ) {
1404                 final SequenceDataRetriver u = new SequenceDataRetriver( this,
1405                                                                          _mainpanel.getCurrentTreePanel(),
1406                                                                          phy.copy() );
1407                 new Thread( u ).start();
1408             }
1409         }
1410     }
1411
1412     private void preProcessTreesUponReading( final Phylogeny[] phys ) {
1413         for( final Phylogeny phy : phys ) {
1414             if ( ( phy != null ) && !phy.isEmpty() ) {
1415                 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
1416                     final PhylogenyNode n = it.next();
1417                     if ( n.isExternal() ) {
1418                         if ( n.getNodeData().isHasSequence() ) {
1419                             final Sequence s = n.getNodeData().getSequence();
1420                             if ( ForesterUtil.isEmpty( s.getGeneName() ) || s.getGeneName().startsWith( "LOC" ) ) {
1421                                 if ( ( s.getAccession() != null )
1422                                         && !ForesterUtil.isEmpty( s.getAccession().getValue() ) ) {
1423                                     s.setGeneName( s.getAccession().getValue() );
1424                                 }
1425                                 else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
1426                                     s.setGeneName( n.getName() );
1427                                 }
1428                             }
1429                         }
1430                     }
1431                 }
1432             }
1433         }
1434     }
1435
1436     private void readPhylogeniesFromFile() {
1437         boolean exception = false;
1438         Phylogeny[] phys = null;
1439         // Set an initial directory if none set yet
1440         final File my_dir = getCurrentDir();
1441         // Open file-open dialog and set current directory
1442         if ( my_dir != null ) {
1443             _open_filechooser.setCurrentDirectory( my_dir );
1444         }
1445         final int result = _open_filechooser.showOpenDialog( _contentpane );
1446         // All done: get the file
1447         final File[] files = _open_filechooser.getSelectedFiles();
1448         setCurrentDir( _open_filechooser.getCurrentDirectory() );
1449         boolean nhx_or_nexus = false;
1450         if ( ( files != null ) && ( files.length > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1451             for( final File file : files ) {
1452                 if ( ( file != null ) && !file.isDirectory() ) {
1453                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1454                         _mainpanel.getCurrentTreePanel().setWaitCursor();
1455                     }
1456                     else {
1457                         _mainpanel.setWaitCursor();
1458                     }
1459                     if ( ( _open_filechooser.getFileFilter() == MainFrame.nhfilter )
1460                             || ( _open_filechooser.getFileFilter() == MainFrame.nhxfilter ) ) {
1461                         try {
1462                             final NHXParser nhx = new NHXParser();
1463                             setSpecialOptionsForNhxParser( nhx );
1464                             phys = PhylogenyMethods.readPhylogenies( nhx, file );
1465                             nhx_or_nexus = true;
1466                         }
1467                         catch ( final Exception e ) {
1468                             exception = true;
1469                             exceptionOccuredDuringOpenFile( e );
1470                         }
1471                     }
1472                     else if ( _open_filechooser.getFileFilter() == MainFrame.xmlfilter ) {
1473                         warnIfNotPhyloXmlValidation( getConfiguration() );
1474                         try {
1475                             final PhyloXmlParser xml_parser = createPhyloXmlParser();
1476                             phys = PhylogenyMethods.readPhylogenies( xml_parser, file );
1477                         }
1478                         catch ( final Exception e ) {
1479                             exception = true;
1480                             exceptionOccuredDuringOpenFile( e );
1481                         }
1482                     }
1483                     else if ( _open_filechooser.getFileFilter() == MainFrame.tolfilter ) {
1484                         try {
1485                             phys = PhylogenyMethods.readPhylogenies( new TolParser(), file );
1486                         }
1487                         catch ( final Exception e ) {
1488                             exception = true;
1489                             exceptionOccuredDuringOpenFile( e );
1490                         }
1491                     }
1492                     else if ( _open_filechooser.getFileFilter() == MainFrame.nexusfilter ) {
1493                         try {
1494                             final NexusPhylogeniesParser nex = new NexusPhylogeniesParser();
1495                             setSpecialOptionsForNexParser( nex );
1496                             phys = PhylogenyMethods.readPhylogenies( nex, file );
1497                             nhx_or_nexus = true;
1498                         }
1499                         catch ( final Exception e ) {
1500                             exception = true;
1501                             exceptionOccuredDuringOpenFile( e );
1502                         }
1503                     }
1504                     // "*.*":
1505                     else {
1506                         try {
1507                             final PhylogenyParser parser = ParserUtils
1508                                     .createParserDependingOnFileType( file,
1509                                                                       getConfiguration()
1510                                                                               .isValidatePhyloXmlAgainstSchema() );
1511                             if ( parser instanceof NexusPhylogeniesParser ) {
1512                                 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) parser;
1513                                 setSpecialOptionsForNexParser( nex );
1514                                 nhx_or_nexus = true;
1515                             }
1516                             else if ( parser instanceof NHXParser ) {
1517                                 final NHXParser nhx = ( NHXParser ) parser;
1518                                 setSpecialOptionsForNhxParser( nhx );
1519                                 nhx_or_nexus = true;
1520                             }
1521                             else if ( parser instanceof PhyloXmlParser ) {
1522                                 warnIfNotPhyloXmlValidation( getConfiguration() );
1523                             }
1524                             phys = PhylogenyMethods.readPhylogenies( parser, file );
1525                         }
1526                         catch ( final Exception e ) {
1527                             exception = true;
1528                             exceptionOccuredDuringOpenFile( e );
1529                         }
1530                     }
1531                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1532                         _mainpanel.getCurrentTreePanel().setArrowCursor();
1533                     }
1534                     else {
1535                         _mainpanel.setArrowCursor();
1536                     }
1537                     if ( !exception && ( phys != null ) && ( phys.length > 0 ) ) {
1538                         boolean one_desc = false;
1539                         if ( nhx_or_nexus ) {
1540                             for( final Phylogeny phy : phys ) {
1541                                 if ( getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
1542                                     PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
1543                                 }
1544                                 if ( PhylogenyMethods.getMinimumDescendentsPerInternalNodes( phy ) == 1 ) {
1545                                     one_desc = true;
1546                                     break;
1547                                 }
1548                             }
1549                         }
1550                         if ( PREPROCESS_TREES ) {
1551                             preProcessTreesUponReading( phys );
1552                         }
1553                         AptxUtil.addPhylogeniesToTabs( phys,
1554                                                        file.getName(),
1555                                                        file.getAbsolutePath(),
1556                                                        getConfiguration(),
1557                                                        getMainPanel() );
1558                         _mainpanel.getControlPanel().showWhole();
1559                         if ( nhx_or_nexus && one_desc ) {
1560                             JOptionPane.showMessageDialog( getThisFrame(),
1561                                                            "One or more trees contain (a) node(s) with one descendant, "
1562                                                                    + ForesterUtil.LINE_SEPARATOR
1563                                                                    + "possibly indicating illegal parentheses within node names.",
1564                                                            "Warning: Possible Error in New Hampshire Formatted Data",
1565                                                            JOptionPane.WARNING_MESSAGE );
1566                         }
1567                     }
1568                 }
1569             }
1570         }
1571         activateSaveAllIfNeeded();
1572         System.gc();
1573     }
1574
1575     private void readSpeciesTreeFromFile() {
1576         Phylogeny t = null;
1577         boolean exception = false;
1578         final File my_dir = getCurrentDir();
1579         _open_filechooser_for_species_tree.setSelectedFile( new File( "" ) );
1580         if ( my_dir != null ) {
1581             _open_filechooser_for_species_tree.setCurrentDirectory( my_dir );
1582         }
1583         final int result = _open_filechooser_for_species_tree.showOpenDialog( _contentpane );
1584         final File file = _open_filechooser_for_species_tree.getSelectedFile();
1585         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1586             if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrame.xmlfilter ) {
1587                 try {
1588                     final Phylogeny[] trees = PhylogenyMethods
1589                             .readPhylogenies( PhyloXmlParser.createPhyloXmlParserXsdValidating(), file );
1590                     t = trees[ 0 ];
1591                 }
1592                 catch ( final Exception e ) {
1593                     exception = true;
1594                     exceptionOccuredDuringOpenFile( e );
1595                 }
1596             }
1597             else if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrame.tolfilter ) {
1598                 try {
1599                     final Phylogeny[] trees = PhylogenyMethods.readPhylogenies( new TolParser(), file );
1600                     t = trees[ 0 ];
1601                 }
1602                 catch ( final Exception e ) {
1603                     exception = true;
1604                     exceptionOccuredDuringOpenFile( e );
1605                 }
1606             }
1607             // "*.*":
1608             else {
1609                 try {
1610                     final Phylogeny[] trees = PhylogenyMethods
1611                             .readPhylogenies( PhyloXmlParser.createPhyloXmlParserXsdValidating(), file );
1612                     t = trees[ 0 ];
1613                 }
1614                 catch ( final Exception e ) {
1615                     exception = true;
1616                     exceptionOccuredDuringOpenFile( e );
1617                 }
1618             }
1619             if ( !exception && ( t != null ) && !t.isRooted() ) {
1620                 exception = true;
1621                 t = null;
1622                 JOptionPane.showMessageDialog( getThisFrame(),
1623                                                "Species tree is not rooted",
1624                                                "Species tree not loaded",
1625                                                JOptionPane.ERROR_MESSAGE );
1626             }
1627             if ( !exception && ( t != null ) ) {
1628                 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
1629                 for( final PhylogenyNodeIterator it = t.iteratorExternalForward(); it.hasNext(); ) {
1630                     final PhylogenyNode node = it.next();
1631                     if ( !node.getNodeData().isHasTaxonomy() ) {
1632                         exception = true;
1633                         t = null;
1634                         JOptionPane.showMessageDialog( getThisFrame(),
1635                                                        "Species tree contains external node(s) without taxonomy information",
1636                                                        "Species tree not loaded",
1637                                                        JOptionPane.ERROR_MESSAGE );
1638                         break;
1639                     }
1640                     else {
1641                         if ( tax_set.contains( node.getNodeData().getTaxonomy() ) ) {
1642                             exception = true;
1643                             t = null;
1644                             JOptionPane
1645                                     .showMessageDialog( getThisFrame(),
1646                                                         "Taxonomy [" + node.getNodeData().getTaxonomy().asSimpleText()
1647                                                                 + "] is not unique in species tree",
1648                                                         "Species tree not loaded",
1649                                                         JOptionPane.ERROR_MESSAGE );
1650                             break;
1651                         }
1652                         else {
1653                             tax_set.add( node.getNodeData().getTaxonomy() );
1654                         }
1655                     }
1656                 }
1657             }
1658             if ( !exception && ( t != null ) ) {
1659                 setSpeciesTree( t );
1660                 JOptionPane.showMessageDialog( getThisFrame(),
1661                                                "Species tree successfully loaded",
1662                                                "Species tree loaded",
1663                                                JOptionPane.INFORMATION_MESSAGE );
1664             }
1665             _contentpane.repaint();
1666             System.gc();
1667         }
1668     }
1669
1670     private void setArrowCursor() {
1671         try {
1672             _mainpanel.getCurrentTreePanel().setArrowCursor();
1673         }
1674         catch ( final Exception ex ) {
1675             // Do nothing.
1676         }
1677     }
1678
1679     private void setMinNotCollapseBlValue( final double min_not_collapse_bl ) {
1680         _min_not_collapse_bl = min_not_collapse_bl;
1681     }
1682
1683     private void setMinNotCollapseConfidenceValue( final double min_not_collapse ) {
1684         _min_not_collapse = min_not_collapse;
1685     }
1686
1687     private void setPhylogeneticInferenceOptions( final PhylogeneticInferenceOptions phylogenetic_inference_options ) {
1688         _phylogenetic_inference_options = phylogenetic_inference_options;
1689     }
1690
1691     private void setSpecialOptionsForNexParser( final NexusPhylogeniesParser nex ) {
1692         nex.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
1693         nex.setTaxonomyExtraction( getOptions().getTaxonomyExtraction() );
1694         nex.setParseBeastStyleExtendedTags( getOptions().isParseBeastStyleExtendedNexusTags() );
1695     }
1696
1697     private void setSpecialOptionsForNhxParser( final NHXParser nhx ) {
1698         nhx.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
1699         nhx.setTaxonomyExtraction( getOptions().getTaxonomyExtraction() );
1700         nhx.setAllowErrorsInDistanceToParent( getOptions().isAllowErrorsInDistanceToParent() );
1701         nhx.setParseBeastStyleExtendedTags( getOptions().isParseBeastStyleExtendedNexusTags() );
1702     }
1703
1704     void buildAnalysisMenu() {
1705         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
1706         _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
1707         _analysis_menu.add( _gsdir_item = new JMenuItem( "GSDIR (GSDI with re-rooting)" ) );
1708         _analysis_menu.add( _load_species_tree_item = new JMenuItem( "Load Species Tree..." ) );
1709         customizeJMenuItem( _gsdi_item );
1710         customizeJMenuItem( _gsdir_item );
1711         customizeJMenuItem( _load_species_tree_item );
1712         _analysis_menu.addSeparator();
1713         _analysis_menu.add( _lineage_inference = new JMenuItem( INFER_ANCESTOR_TAXONOMIES ) );
1714         customizeJMenuItem( _lineage_inference );
1715         _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
1716         _jmenubar.add( _analysis_menu );
1717     }
1718
1719     @Override
1720     void buildFileMenu() {
1721         _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
1722         _file_jmenu.add( _open_item = new JMenuItem( "Read Tree from File..." ) );
1723         _file_jmenu.addSeparator();
1724         _file_jmenu.add( _open_url_item = new JMenuItem( "Read Tree from URL/Webservice..." ) );
1725         _file_jmenu.addSeparator();
1726         final WebservicesManager webservices_manager = WebservicesManager.getInstance();
1727         _load_phylogeny_from_webservice_menu_items = new JMenuItem[ webservices_manager
1728                 .getAvailablePhylogeniesWebserviceClients().size() ];
1729         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
1730             final PhylogeniesWebserviceClient client = webservices_manager.getAvailablePhylogeniesWebserviceClient( i );
1731             _load_phylogeny_from_webservice_menu_items[ i ] = new JMenuItem( client.getMenuName() );
1732             _file_jmenu.add( _load_phylogeny_from_webservice_menu_items[ i ] );
1733         }
1734         if ( getConfiguration().isEditable() ) {
1735             _file_jmenu.addSeparator();
1736             _file_jmenu.add( _new_item = new JMenuItem( "New" ) );
1737             _new_item.setToolTipText( "to create a new tree with one node, as source for manual tree construction" );
1738         }
1739         _file_jmenu.addSeparator();
1740         _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
1741         _file_jmenu.add( _save_all_item = new JMenuItem( "Save All Trees As..." ) );
1742         _save_all_item.setToolTipText( "Write all phylogenies to one file." );
1743         _save_all_item.setEnabled( false );
1744         _file_jmenu.addSeparator();
1745         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
1746         if ( AptxUtil.canWriteFormat( "tif" ) || AptxUtil.canWriteFormat( "tiff" )
1747                 || AptxUtil.canWriteFormat( "TIF" ) ) {
1748             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
1749         }
1750         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
1751         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
1752         if ( AptxUtil.canWriteFormat( "gif" ) ) {
1753             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
1754         }
1755         if ( AptxUtil.canWriteFormat( "bmp" ) ) {
1756             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
1757         }
1758         _file_jmenu.addSeparator();
1759         _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
1760         _file_jmenu.addSeparator();
1761         _file_jmenu.add( _close_item = new JMenuItem( "Close Tab" ) );
1762         _close_item.setToolTipText( "To close the current pane." );
1763         _close_item.setEnabled( true );
1764         _file_jmenu.addSeparator();
1765         _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
1766         customizeJMenuItem( _open_item );
1767         _open_item.setFont( new Font( _open_item.getFont().getFontName(),
1768                                       Font.BOLD,
1769                                       _open_item.getFont().getSize() + 4 ) );
1770         customizeJMenuItem( _open_url_item );
1771         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
1772             customizeJMenuItem( _load_phylogeny_from_webservice_menu_items[ i ] );
1773         }
1774         customizeJMenuItem( _save_item );
1775         if ( getConfiguration().isEditable() ) {
1776             customizeJMenuItem( _new_item );
1777         }
1778         customizeJMenuItem( _close_item );
1779         customizeJMenuItem( _save_all_item );
1780         customizeJMenuItem( _write_to_pdf_item );
1781         customizeJMenuItem( _write_to_png_item );
1782         customizeJMenuItem( _write_to_jpg_item );
1783         customizeJMenuItem( _write_to_gif_item );
1784         customizeJMenuItem( _write_to_tif_item );
1785         customizeJMenuItem( _write_to_bmp_item );
1786         customizeJMenuItem( _print_item );
1787         customizeJMenuItem( _exit_item );
1788         _jmenubar.add( _file_jmenu );
1789     }
1790
1791     void buildOptionsMenu() {
1792         _options_jmenu = MainFrame.createMenu( OPTIONS_HEADER, getConfiguration() );
1793         _options_jmenu.addChangeListener( new ChangeListener() {
1794
1795             @Override
1796             public void stateChanged( final ChangeEvent e ) {
1797                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
1798                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
1799                 MainFrame.setTextMinSupportMenuItem( _choose_minimal_confidence_mi,
1800                                                      getOptions(),
1801                                                      getCurrentTreePanel() );
1802                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi,
1803                                                          MainFrame.createCurrentFontDesc( getMainPanel()
1804                                                                  .getTreeFontSet() ) );
1805                 //  MainFrame.setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );
1806                 MainFrame.setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );
1807                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
1808                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
1809                 MainFrame.setCycleDataReturnMenuItem( _cycle_data_return, getOptions() );
1810                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
1811                 try {
1812                     getMainPanel().getControlPanel().setVisibilityOfDomainStrucureCB();
1813                     getMainPanel().getControlPanel().setVisibilityOfX();
1814                 }
1815                 catch ( final Exception ignore ) {
1816                     // do nothing, not important.
1817                 }
1818             }
1819         } );
1820         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( DISPLAY_SUBHEADER ), getConfiguration() ) );
1821         _options_jmenu
1822                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
1823         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
1824         _radio_group_1 = new ButtonGroup();
1825         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
1826          _radio_group_1.add( _non_lined_up_cladograms_rbmi );
1827         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( SHOW_OVERVIEW_LABEL ) );
1828         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( DISPLAY_SCALE_LABEL ) );
1829         _options_jmenu
1830                 .add( _show_default_node_shapes_internal_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_INT ) );
1831         _options_jmenu
1832                 .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_EXT ) );
1833         _options_jmenu
1834                 .add( _show_default_node_shapes_for_marked_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_MARKED ) );
1835       
1836         _options_jmenu
1837         .add( _collapsed_with_average_height_cbmi = new JCheckBoxMenuItem( "Proportional Height of Collapsed Subtrees" ) );
1838
1839         
1840         _options_jmenu
1841         .add( _show_abbreviated_labels_for_collapsed_nodes_cbmi = new JCheckBoxMenuItem( "Add Abbreviated Labels to Collapsed Subtrees" ) );
1842
1843      
1844         
1845         _options_jmenu
1846                 .add( _line_up_renderable_data_cbmi = new JCheckBoxMenuItem( MainFrame.LINE_UP_RENDERABLE_DATA ) );
1847         
1848        
1849         
1850         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
1851             _options_jmenu
1852                     .add( _right_line_up_domains_cbmi = new JCheckBoxMenuItem( MainFrame.RIGHT_LINE_UP_DOMAINS ) );
1853             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( MainFrame.SHOW_DOMAIN_LABELS_LABEL ) );
1854         }
1855         _options_jmenu.add( _show_annotation_ref_source = new JCheckBoxMenuItem( SHOW_ANN_REF_SOURCE_LABEL ) );
1856         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( SHOW_CONF_STDDEV_LABEL ) );
1857         _options_jmenu.add( _color_by_taxonomic_group_cbmi = new JCheckBoxMenuItem( COLOR_BY_TAXONOMIC_GROUP ) );
1858         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
1859         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
1860         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( ABBREV_SN_LABEL ) );
1861         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
1862         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
1863         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( SCREEN_ANTIALIAS_LABEL ) );
1864         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( BG_GRAD_LABEL ) );
1865         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
1866         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
1867         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
1868         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
1869         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
1870         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
1871         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
1872         _options_jmenu.addSeparator();
1873         _options_jmenu.add( _cycle_data_return = new JMenuItem( "Cycle Data Return" ) );
1874         _options_jmenu.addSeparator();
1875         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( SEARCH_SUBHEADER ), getConfiguration() ) );
1876         _options_jmenu.add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( SEARCH_CASE_SENSITIVE_LABEL ) );
1877         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( SEARCH_TERMS_ONLY_LABEL ) );
1878         _options_jmenu.add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) );
1879         _search_with_regex_cbmi.setToolTipText( MainFrame.SEARCH_WITH_REGEX_TIP );
1880         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
1881         _options_jmenu
1882                 .add( _color_all_found_nodes_when_coloring_subtree_cbmi = new JCheckBoxMenuItem( "Colorize All Found Nodes When Colorizing Subtree(s)" ) );
1883         _options_jmenu.addSeparator();
1884         _options_jmenu
1885                 .add( customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ), getConfiguration() ) );
1886         _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) );
1887         _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) );
1888         _options_jmenu
1889                 .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) );
1890         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
1891         _options_jmenu.addSeparator();
1892         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Newick/NHX/Nexus Read:" ), getConfiguration() ) );
1893         _options_jmenu
1894                 .add( _internal_number_are_confidence_for_nh_parsing_cbmi = new JCheckBoxMenuItem( "Internal Node Names are Confidence Values" ) );
1895         _options_jmenu.add( _replace_underscores_cbmi = new JCheckBoxMenuItem( "Replace Underscores with Spaces" ) );
1896         _options_jmenu
1897                 .add( _parse_beast_style_extended_nexus_tags_cbmi = new JCheckBoxMenuItem( "Parse BEAST-style extended Newick/Nexus tags" ) );
1898         _parse_beast_style_extended_nexus_tags_cbmi
1899                 .setToolTipText( "to parse elements in the form of \"[&!color=#800080]\" in Newick/Nexus formatted trees" );
1900         _options_jmenu
1901                 .add( _allow_errors_in_distance_to_parent_cbmi = new JCheckBoxMenuItem( "Ignore Distance Values Format Errors" ) );
1902         _options_jmenu.add( _extract_taxonomy_no_rbmi = new JRadioButtonMenuItem( "No Taxonomy Extraction" ) );
1903         _options_jmenu
1904                 .add( _extract_taxonomy_pfam_strict_rbmi = new JRadioButtonMenuItem( "Extract Taxonomy Codes/Ids from Pfam-style Node Names" ) );
1905         _options_jmenu
1906                 .add( _extract_taxonomy_pfam_relaxed_rbmi = new JRadioButtonMenuItem( "Extract Taxonomy Codes/Ids from Pfam-style like Node Names" ) );
1907         _options_jmenu
1908                 .add( _extract_taxonomy_agressive_rbmi = new JRadioButtonMenuItem( "Extract Taxonomy Codes/Ids/Scientific Names from Node Names" ) );
1909         _extract_taxonomy_pfam_strict_rbmi
1910                 .setToolTipText( "To extract taxonomy codes/ids from node names in the form of e.g. \"BCL2_MOUSE/123-304\" or \"BCL2_10090/123-304\"" );
1911         _extract_taxonomy_pfam_relaxed_rbmi
1912                 .setToolTipText( "To extract taxonomy codes/ids from node names in the form of e.g. \"bax_MOUSE\" or \"bax_10090\"" );
1913         _extract_taxonomy_agressive_rbmi
1914                 .setToolTipText( "To extract taxonomy codes/ids or scientific names from node names in the form of e.g. \"MOUSE\" or \"10090\" or \"xyz_Nematostella_vectensis\"" );
1915         _radio_group_2 = new ButtonGroup();
1916         _radio_group_2.add( _extract_taxonomy_no_rbmi );
1917         _radio_group_2.add( _extract_taxonomy_pfam_strict_rbmi );
1918         _radio_group_2.add( _extract_taxonomy_pfam_relaxed_rbmi );
1919         _radio_group_2.add( _extract_taxonomy_agressive_rbmi );
1920         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Newick/Nexus Save:" ), getConfiguration() ) );
1921         _options_jmenu
1922                 .add( _use_brackets_for_conf_in_nh_export_cbmi = new JCheckBoxMenuItem( USE_BRACKETS_FOR_CONF_IN_NH_LABEL ) );
1923         _use_brackets_for_conf_in_nh_export_cbmi
1924                 .setToolTipText( "e.g. \"0.1[90]\" for a branch with support 90 and a length of 0.1" );
1925         _options_jmenu
1926                 .add( _use_internal_names_for_conf_in_nh_export_cbmi = new JCheckBoxMenuItem( USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL ) );
1927         customizeJMenuItem( _choose_font_mi );
1928         customizeJMenuItem( _choose_minimal_confidence_mi );
1929         customizeJMenuItem( _switch_colors_mi );
1930         customizeJMenuItem( _choose_pdf_width_mi );
1931         customizeJMenuItem( _overview_placment_mi );
1932         customizeCheckBoxMenuItem( _show_default_node_shapes_external_cbmi,
1933                                    getOptions().isShowDefaultNodeShapesExternal() );
1934         customizeCheckBoxMenuItem( _show_default_node_shapes_internal_cbmi,
1935                                    getOptions().isShowDefaultNodeShapesInternal() );
1936         customizeCheckBoxMenuItem( _show_default_node_shapes_for_marked_cbmi,
1937                                    getOptions().isShowDefaultNodeShapesForMarkedNodes() );
1938         customizeJMenuItem( _cycle_node_shape_mi );
1939         customizeJMenuItem( _cycle_node_fill_mi );
1940         customizeJMenuItem( _choose_node_size_mi );
1941         customizeJMenuItem( _cycle_data_return );
1942         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch,
1943                                    getOptions().isColorLabelsSameAsParentBranch() );
1944         customizeCheckBoxMenuItem( _color_by_taxonomic_group_cbmi, getOptions().isColorByTaxonomicGroup() );
1945         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
1946         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
1947         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
1948         customizeCheckBoxMenuItem( _show_annotation_ref_source, getOptions().isShowAnnotationRefSource() );
1949         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
1950         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
1951         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
1952         customizeCheckBoxMenuItem( _collapsed_with_average_height_cbmi, getOptions().isCollapsedWithAverageHeigh() );
1953         customizeCheckBoxMenuItem( _show_abbreviated_labels_for_collapsed_nodes_cbmi, getOptions().isShowAbbreviatedLabelsForCollapsedNodes() );
1954         
1955         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
1956                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
1957         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
1958                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.LINED_UP );
1959         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
1960         customizeCheckBoxMenuItem( _label_direction_cbmi,
1961                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
1962         customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() );
1963         customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() );
1964         customizeCheckBoxMenuItem( _internal_number_are_confidence_for_nh_parsing_cbmi,
1965                                    getOptions().isInternalNumberAreConfidenceForNhParsing() );
1966         customizeRadioButtonMenuItem( _extract_taxonomy_no_rbmi,
1967                                       getOptions().getTaxonomyExtraction() == TAXONOMY_EXTRACTION.NO );
1968         customizeRadioButtonMenuItem( _extract_taxonomy_pfam_strict_rbmi,
1969                                       getOptions().getTaxonomyExtraction() == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
1970         customizeRadioButtonMenuItem( _extract_taxonomy_pfam_relaxed_rbmi,
1971                                       getOptions().getTaxonomyExtraction() == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED );
1972         customizeRadioButtonMenuItem( _extract_taxonomy_agressive_rbmi,
1973                                       getOptions().getTaxonomyExtraction() == TAXONOMY_EXTRACTION.AGGRESSIVE );
1974         customizeCheckBoxMenuItem( _replace_underscores_cbmi, getOptions().isReplaceUnderscoresInNhParsing() );
1975         customizeCheckBoxMenuItem( _allow_errors_in_distance_to_parent_cbmi,
1976                                    getOptions().isReplaceUnderscoresInNhParsing() );
1977         customizeCheckBoxMenuItem( _search_with_regex_cbmi, getOptions().isSearchWithRegex() );
1978         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
1979         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
1980         customizeCheckBoxMenuItem( _color_all_found_nodes_when_coloring_subtree_cbmi,
1981                                    getOptions().isColorAllFoundNodesWhenColoringSubtree() );
1982         customizeCheckBoxMenuItem( _parse_beast_style_extended_nexus_tags_cbmi,
1983                                    getOptions().isParseBeastStyleExtendedNexusTags() );
1984         customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() );
1985         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
1986         customizeCheckBoxMenuItem( _use_brackets_for_conf_in_nh_export_cbmi,
1987                                    getOptions()
1988                                            .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS );
1989         customizeCheckBoxMenuItem( _use_internal_names_for_conf_in_nh_export_cbmi,
1990                                    getOptions()
1991                                            .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES );
1992         customizeCheckBoxMenuItem( _line_up_renderable_data_cbmi, getOptions().isLineUpRendarableNodeData() );
1993         customizeCheckBoxMenuItem( _right_line_up_domains_cbmi, getOptions().isRightLineUpDomains() );
1994         _jmenubar.add( _options_jmenu );
1995     }
1996
1997     void buildPhylogeneticInferenceMenu() {
1998         final InferenceManager im = getInferenceManager();
1999         _inference_menu = MainFrame.createMenu( "Inference", getConfiguration() );
2000         _inference_menu.add( _inference_from_msa_item = new JMenuItem( "From Multiple Sequence Alignment..." ) );
2001         customizeJMenuItem( _inference_from_msa_item );
2002         _inference_from_msa_item.setToolTipText( "Basic phylogenetic inference from MSA" );
2003         if ( im.canDoMsa() ) {
2004             _inference_menu.add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences..." ) );
2005             customizeJMenuItem( _inference_from_seqs_item );
2006             _inference_from_seqs_item
2007                     .setToolTipText( "Basic phylogenetic inference including multiple sequence alignment" );
2008         }
2009         else {
2010             _inference_menu
2011                     .add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences (no program found)" ) );
2012             customizeJMenuItem( _inference_from_seqs_item );
2013             _inference_from_seqs_item.setEnabled( false );
2014         }
2015         _jmenubar.add( _inference_menu );
2016     }
2017
2018     void buildToolsMenu() {
2019         _tools_menu = createMenu( "Tools", getConfiguration() );
2020         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
2021         customizeJMenuItem( _confcolor_item );
2022         _tools_menu.add( _color_rank_jmi = new JMenuItem( "Colorize Subtrees via Taxonomic Rank" ) );
2023         customizeJMenuItem( _color_rank_jmi );
2024         _color_rank_jmi.setToolTipText( "for example, at \"Class\" level, colorize mammal specific subtree red" );
2025         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
2026         customizeJMenuItem( _taxcolor_item );
2027         _tools_menu.addSeparator();
2028         _tools_menu.add( _remove_visual_styles_item = new JMenuItem( "Delete All Visual Styles From Nodes" ) );
2029         _remove_visual_styles_item
2030                 .setToolTipText( "To remove all node visual styles (fonts, colors) from the current phylogeny" );
2031         customizeJMenuItem( _remove_visual_styles_item );
2032         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete All Colors From Branches" ) );
2033         _remove_branch_color_item.setToolTipText( "To remove all branch color values from the current phylogeny" );
2034         customizeJMenuItem( _remove_branch_color_item );
2035         _tools_menu.addSeparator();
2036         _tools_menu.add( _annotate_item = new JMenuItem( "Annotate Sequences of Selected Nodes" ) );
2037         customizeJMenuItem( _annotate_item );
2038         _tools_menu.addSeparator();
2039         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
2040         customizeJMenuItem( _midpoint_root_item );
2041         _tools_menu.addSeparator();
2042         _tools_menu.add( _delete_selected_nodes_item = new JMenuItem( "Delete Selected Nodes" ) );
2043         _delete_selected_nodes_item.setToolTipText( "To delete all selected external nodes" );
2044         customizeJMenuItem( _delete_selected_nodes_item );
2045         _tools_menu.add( _delete_not_selected_nodes_item = new JMenuItem( "Retain Selected Nodes" ) );
2046         _delete_not_selected_nodes_item.setToolTipText( "To delete all not selected external nodes" );
2047         customizeJMenuItem( _delete_not_selected_nodes_item );
2048         _tools_menu.addSeparator();
2049         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Single Taxonomy-Subtrees" ) );
2050         customizeJMenuItem( _collapse_species_specific_subtrees );
2051         _collapse_species_specific_subtrees.setToolTipText( "To (reversibly) collapse subtrees associated with only one taxonomy (such as species specific subtrees)" );
2052         _tools_menu
2053                 .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold into Multifurcations" ) );
2054         customizeJMenuItem( _collapse_below_threshold );
2055         _collapse_below_threshold
2056                 .setToolTipText( "To (permanently) collapse branches with confidence values below a threshold into multifurcations (in the case of multiple confidences per branch: without at least one confidence value above a threshold)" );
2057         //
2058         _tools_menu
2059                 .add( _collapse_below_branch_length = new JMenuItem( "Collapse Branches with Branch Lengths Below Threshold into Multifurcations" ) );
2060         customizeJMenuItem( _collapse_below_branch_length );
2061         _collapse_below_branch_length
2062                 .setToolTipText( "To (permanently) collapse branches with branches with branch lengths below a threshold into multifurcations" );
2063         //
2064         _tools_menu.addSeparator();
2065         _tools_menu
2066                 .add( _extract_tax_code_from_node_names_jmi = new JMenuItem( "Extract Taxonomic Data from Node Names" ) );
2067         customizeJMenuItem( _extract_tax_code_from_node_names_jmi );
2068         _extract_tax_code_from_node_names_jmi
2069                 .setToolTipText( "To extract SwissProt/Uniprot taxonomic codes (mnemonics) from nodes names in the form of 'xyz_CAEEL', Uniprot/NCBI identifiers form of 'xyz_6239', or scientific names form of 'xyz_Caenorhabditis_elegans'" );
2070         _tools_menu
2071                 .add( _move_node_names_to_tax_sn_jmi = new JMenuItem( "Transfer Node Names to Taxonomic Scientific Names" ) );
2072         customizeJMenuItem( _move_node_names_to_tax_sn_jmi );
2073         _move_node_names_to_tax_sn_jmi.setToolTipText( "To interpret node names as taxonomic scientific names" );
2074         _tools_menu.add( _move_node_names_to_seq_names_jmi = new JMenuItem( "Transfer Node Names to Sequence Names" ) );
2075         customizeJMenuItem( _move_node_names_to_seq_names_jmi );
2076         _move_node_names_to_seq_names_jmi.setToolTipText( "To interpret node names as sequence (protein, gene) names" );
2077         _tools_menu.addSeparator();
2078         _tools_menu.add( _obtain_seq_information_jmi = new JMenuItem( "Obtain Sequence Information" ) );
2079         customizeJMenuItem( _obtain_seq_information_jmi );
2080         _obtain_seq_information_jmi.setToolTipText( "To add additional sequence information" );
2081         _tools_menu
2082                 .add( _obtain_detailed_taxonomic_information_jmi = new JMenuItem( OBTAIN_DETAILED_TAXONOMIC_INFORMATION ) );
2083         customizeJMenuItem( _obtain_detailed_taxonomic_information_jmi );
2084         _obtain_detailed_taxonomic_information_jmi
2085                 .setToolTipText( "To add additional taxonomic information (from UniProt Taxonomy)" );
2086         _tools_menu
2087                 .add( _obtain_detailed_taxonomic_information_deleting_jmi = new JMenuItem( "Obtain Detailed Taxonomic Information (deletes nodes!)" ) );
2088         customizeJMenuItem( _obtain_detailed_taxonomic_information_deleting_jmi );
2089         _obtain_detailed_taxonomic_information_deleting_jmi
2090                 .setToolTipText( "To add additional taxonomic information, deletes nodes for which taxonomy cannot found (from UniProt Taxonomy)" );
2091         _tools_menu.addSeparator();
2092         _tools_menu.add( _read_values_jmi = new JMenuItem( "Attach Vector/Expression Values" ) );
2093         customizeJMenuItem( _read_values_jmi );
2094         _read_values_jmi.setToolTipText( "To attach vector (e.g. gene expression) values to tree nodes (beta)" );
2095         _jmenubar.add( _tools_menu );
2096         _tools_menu.add( _read_seqs_jmi = new JMenuItem( "Attach Molecular Sequences" ) );
2097         customizeJMenuItem( _read_seqs_jmi );
2098         _read_seqs_jmi
2099                 .setToolTipText( "To attach molecular sequences to tree nodes (from Fasta-formatted file) (beta)" );
2100         _jmenubar.add( _tools_menu );
2101     }
2102
2103     @Override
2104     void close() {
2105         if ( isUnsavedDataPresent() ) {
2106             final int r = JOptionPane.showConfirmDialog( getThisFrame(),
2107                                                          "Exit despite potentially unsaved changes?",
2108                                                          "Exit?",
2109                                                          JOptionPane.YES_NO_OPTION );
2110             if ( r != JOptionPane.YES_OPTION ) {
2111                 return;
2112             }
2113         }
2114         exit();
2115     }
2116
2117     void exit() {
2118         removeAllTextFrames();
2119         _mainpanel.terminate();
2120         _contentpane.removeAll();
2121         setVisible( false );
2122         dispose();
2123         // System.exit( 0 ); //TODO reconfirm that this is OK, then remove.
2124     }
2125
2126     void readPhylogeniesFromURL() {
2127         URL url = null;
2128         Phylogeny[] phys = null;
2129         final String message = "Please enter a complete URL, for example \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\"";
2130         final String url_string = JOptionPane
2131                 .showInputDialog( getThisFrame(),
2132                                   message,
2133                                   "Use URL/webservice to obtain a phylogeny",
2134                                   JOptionPane.QUESTION_MESSAGE );
2135         boolean nhx_or_nexus = false;
2136         if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
2137             try {
2138                 url = new URL( url_string );
2139                 PhylogenyParser parser = null;
2140                 if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
2141                     parser = new TolParser();
2142                 }
2143                 else {
2144                     parser = ParserUtils
2145                             .createParserDependingOnUrlContents( url,
2146                                                                  getConfiguration().isValidatePhyloXmlAgainstSchema() );
2147                 }
2148                 if ( parser instanceof NexusPhylogeniesParser ) {
2149                     nhx_or_nexus = true;
2150                 }
2151                 else if ( parser instanceof NHXParser ) {
2152                     nhx_or_nexus = true;
2153                 }
2154                 if ( _mainpanel.getCurrentTreePanel() != null ) {
2155                     _mainpanel.getCurrentTreePanel().setWaitCursor();
2156                 }
2157                 else {
2158                     _mainpanel.setWaitCursor();
2159                 }
2160                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
2161                 phys = factory.create( url.openStream(), parser );
2162             }
2163             catch ( final MalformedURLException e ) {
2164                 JOptionPane.showMessageDialog( getThisFrame(),
2165                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
2166                                                "Malformed URL",
2167                                                JOptionPane.ERROR_MESSAGE );
2168             }
2169             catch ( final IOException e ) {
2170                 JOptionPane.showMessageDialog( getThisFrame(),
2171                                                "Could not read from " + url + "\n"
2172                                                        + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2173                                                "Failed to read URL",
2174                                                JOptionPane.ERROR_MESSAGE );
2175             }
2176             catch ( final Exception e ) {
2177                 JOptionPane.showMessageDialog( getThisFrame(),
2178                                                ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2179                                                "Unexpected Exception",
2180                                                JOptionPane.ERROR_MESSAGE );
2181             }
2182             finally {
2183                 if ( _mainpanel.getCurrentTreePanel() != null ) {
2184                     _mainpanel.getCurrentTreePanel().setArrowCursor();
2185                 }
2186                 else {
2187                     _mainpanel.setArrowCursor();
2188                 }
2189             }
2190             if ( ( phys != null ) && ( phys.length > 0 ) ) {
2191                 if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
2192                     for( final Phylogeny phy : phys ) {
2193                         PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
2194                     }
2195                 }
2196                 AptxUtil.addPhylogeniesToTabs( phys,
2197                                                new File( url.getFile() ).getName(),
2198                                                new File( url.getFile() ).toString(),
2199                                                getConfiguration(),
2200                                                getMainPanel() );
2201                 _mainpanel.getControlPanel().showWhole();
2202             }
2203         }
2204         activateSaveAllIfNeeded();
2205         System.gc();
2206     }
2207
2208     void setMsa( final Msa msa ) {
2209         _msa = msa;
2210     }
2211
2212     void setMsaFile( final File msa_file ) {
2213         _msa_file = msa_file;
2214     }
2215
2216     void setSeqs( final List<MolecularSequence> seqs ) {
2217         _seqs = seqs;
2218     }
2219
2220     void setSeqsFile( final File seqs_file ) {
2221         _seqs_file = seqs_file;
2222     }
2223
2224     public static MainFrameApplication createInstance( final Phylogeny[] phys, final Configuration config ) {
2225         return new MainFrameApplication( phys, config );
2226     }
2227
2228     public static MainFrame createInstance( final Phylogeny[] phys,
2229                                             final Configuration config,
2230                                             final String title,
2231                                             final File current_dir ) {
2232         return new MainFrameApplication( phys, config, title, current_dir );
2233     }
2234
2235     static MainFrame createInstance( final Phylogeny[] phys, final Configuration config, final String title ) {
2236         return new MainFrameApplication( phys, config, title );
2237     }
2238
2239     static MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) {
2240         return new MainFrameApplication( phys, config_file_name, title );
2241     }
2242
2243     static void warnIfNotPhyloXmlValidation( final Configuration c ) {
2244         if ( !c.isValidatePhyloXmlAgainstSchema() ) {
2245             JOptionPane.showMessageDialog( null,
2246                                            ForesterUtil.wordWrap(
2247                                                                   "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]",
2248                                                                   80 ),
2249                                            "Warning",
2250                                            JOptionPane.WARNING_MESSAGE );
2251         }
2252     }
2253 } // MainFrameApplication.