993712453b729a119c9d6dec49bc28752d19f664
[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: www.phylosoft.org/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.net.MalformedURLException;
40 import java.net.URL;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Set;
45
46 import javax.swing.ButtonGroup;
47 import javax.swing.JCheckBoxMenuItem;
48 import javax.swing.JFileChooser;
49 import javax.swing.JMenu;
50 import javax.swing.JMenuBar;
51 import javax.swing.JMenuItem;
52 import javax.swing.JOptionPane;
53 import javax.swing.JRadioButtonMenuItem;
54 import javax.swing.UIManager;
55 import javax.swing.UnsupportedLookAndFeelException;
56 import javax.swing.WindowConstants;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.filechooser.FileFilter;
60 import javax.swing.plaf.synth.SynthLookAndFeel;
61
62 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
63 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
64 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
65 import org.forester.archaeopteryx.Util.GraphicsExportType;
66 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
67 import org.forester.archaeopteryx.tools.GoAnnotation;
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.tools.TaxonomyDataObtainer;
73 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
74 import org.forester.archaeopteryx.webservices.WebservicesManager;
75 import org.forester.io.parsers.FastaParser;
76 import org.forester.io.parsers.GeneralMsaParser;
77 import org.forester.io.parsers.PhylogenyParser;
78 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
79 import org.forester.io.parsers.nhx.NHXParser;
80 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
81 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
82 import org.forester.io.parsers.tol.TolParser;
83 import org.forester.io.writers.PhylogenyWriter;
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.data.Confidence;
91 import org.forester.phylogeny.data.Taxonomy;
92 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
93 import org.forester.phylogeny.factories.PhylogenyFactory;
94 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
95 import org.forester.sdi.GSDI;
96 import org.forester.sdi.SDI;
97 import org.forester.sdi.SDIR;
98 import org.forester.sdi.SDIse;
99 import org.forester.sequence.Sequence;
100 import org.forester.util.BasicDescriptiveStatistics;
101 import org.forester.util.BasicTable;
102 import org.forester.util.BasicTableParser;
103 import org.forester.util.DescriptiveStatistics;
104 import org.forester.util.ForesterUtil;
105 import org.forester.util.ForesterUtil.PhylogenyNodeField;
106 import org.forester.util.ForesterUtil.TAXONOMY_EXTRACTION;
107 import org.forester.util.WindowsUtils;
108
109 class DefaultFilter extends FileFilter {
110
111     @Override
112     public boolean accept( final File f ) {
113         final String file_name = f.getName().trim().toLowerCase();
114         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
115                 || file_name.endsWith( ".nwk" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".ph" )
116                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".dnd" ) || file_name.endsWith( ".tree" )
117                 || file_name.endsWith( ".nhx" ) || file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" )
118                 || file_name.endsWith( "phylo.xml" ) || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".nexus" )
119                 || file_name.endsWith( ".nx" ) || file_name.endsWith( ".nex" ) || file_name.endsWith( ".tre" )
120                 || file_name.endsWith( ".zip" ) || file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" )
121                 || file_name.endsWith( ".con" ) || f.isDirectory();
122     }
123
124     @Override
125     public String getDescription() {
126         return "All supported files (*.xml, *.phyloxml, *phylo.xml, *.nhx, *.nh, *.newick, *.nex, *.nexus, *.phy, *.tre, *.tree, *.tol, ...)";
127     }
128 }
129
130 class GraphicsFileFilter extends FileFilter {
131
132     @Override
133     public boolean accept( final File f ) {
134         final String file_name = f.getName().trim().toLowerCase();
135         return file_name.endsWith( ".jpg" ) || file_name.endsWith( ".jpeg" ) || file_name.endsWith( ".png" )
136                 || file_name.endsWith( ".gif" ) || file_name.endsWith( ".bmp" ) || f.isDirectory();
137     }
138
139     @Override
140     public String getDescription() {
141         return "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)";
142     }
143 }
144
145 class MsaFileFilter extends FileFilter {
146
147     @Override
148     public boolean accept( final File f ) {
149         final String file_name = f.getName().trim().toLowerCase();
150         return file_name.endsWith( ".msa" ) || file_name.endsWith( ".aln" ) || file_name.endsWith( ".fasta" )
151                 || file_name.endsWith( ".fas" ) || file_name.endsWith( ".fa" ) || f.isDirectory();
152     }
153
154     @Override
155     public String getDescription() {
156         return "Multiple sequence alignment files (*.msa, *.aln, *.fasta, *.fa, *.fas)";
157     }
158 }
159
160 class SequencesFileFilter extends FileFilter {
161
162     @Override
163     public boolean accept( final File f ) {
164         final String file_name = f.getName().trim().toLowerCase();
165         return file_name.endsWith( ".fasta" ) || file_name.endsWith( ".fa" ) || file_name.endsWith( ".fas" )
166                 || file_name.endsWith( ".seqs" ) || f.isDirectory();
167     }
168
169     @Override
170     public String getDescription() {
171         return "Sequences files (*.fasta, *.fa, *.fas, *.seqs )";
172     }
173 }
174
175 public final class MainFrameApplication extends MainFrame {
176
177     private final static int                 FRAME_X_SIZE                    = 800;
178     private final static int                 FRAME_Y_SIZE                    = 800;
179     // Filters for the file-open dialog (classes defined in this file)
180     private final static NHFilter            nhfilter                        = new NHFilter();
181     private final static NHXFilter           nhxfilter                       = new NHXFilter();
182     private final static XMLFilter           xmlfilter                       = new XMLFilter();
183     private final static TolFilter           tolfilter                       = new TolFilter();
184     private final static NexusFilter         nexusfilter                     = new NexusFilter();
185     private final static PdfFilter           pdffilter                       = new PdfFilter();
186     private final static GraphicsFileFilter  graphicsfilefilter              = new GraphicsFileFilter();
187     private final static MsaFileFilter       msafilter                       = new MsaFileFilter();
188     private final static SequencesFileFilter seqsfilter                      = new SequencesFileFilter();
189     private final static DefaultFilter       defaultfilter                   = new DefaultFilter();
190     private static final long                serialVersionUID                = -799735726778865234L;
191     private final JFileChooser               _values_filechooser;
192     private final JFileChooser               _open_filechooser;
193     private final JFileChooser               _msa_filechooser;
194     private final JFileChooser               _seqs_filechooser;
195     private final JFileChooser               _open_filechooser_for_species_tree;
196     private final JFileChooser               _save_filechooser;
197     private final JFileChooser               _writetopdf_filechooser;
198     private final JFileChooser               _writetographics_filechooser;
199     // Analysis menu
200     private JMenu                            _analysis_menu;
201     private JMenuItem                        _load_species_tree_item;
202     private JMenuItem                        _sdi_item;
203     private JMenuItem                        _gsdi_item;
204     private JMenuItem                        _root_min_dups_item;
205     private JMenuItem                        _root_min_cost_l_item;
206     private JMenuItem                        _lineage_inference;
207     private JMenuItem                        _function_analysis;
208     // Application-only print menu items
209     private JMenuItem                        _print_item;
210     private JMenuItem                        _write_to_pdf_item;
211     private JMenuItem                        _write_to_jpg_item;
212     private JMenuItem                        _write_to_gif_item;
213     private JMenuItem                        _write_to_tif_item;
214     private JMenuItem                        _write_to_png_item;
215     private JMenuItem                        _write_to_bmp_item;
216     private Phylogeny                        _species_tree;
217     private File                             _current_dir;
218     private ButtonGroup                      _radio_group_1;
219     // Others:
220     double                                   _min_not_collapse               = Constants.MIN_NOT_COLLAPSE_DEFAULT;
221     // Phylogeny Inference menu
222     private JMenu                            _inference_menu;
223     private JMenuItem                        _inference_from_msa_item;
224     private JMenuItem                        _inference_from_seqs_item;
225     // Phylogeny Inference
226     private PhylogeneticInferenceOptions     _phylogenetic_inference_options = null;
227     private Msa                              _msa                            = null;
228     private File                             _msa_file                       = null;
229     private List<Sequence>                   _seqs                           = null;
230     private File                             _seqs_file                      = null;
231     // expression values menu:
232     JMenuItem                                _read_values_jmi;
233
234     private MainFrameApplication( final Phylogeny[] phys, final Configuration config, final String title ) {
235         _configuration = config;
236         if ( _configuration == null ) {
237             throw new IllegalArgumentException( "configuration is null" );
238         }
239         try {
240             boolean synth_exception = false;
241             if ( Constants.__SYNTH_LF ) {
242                 try {
243                     final SynthLookAndFeel synth = new SynthLookAndFeel();
244                     synth.load( MainFrameApplication.class.getResourceAsStream( "synth_look_and_feel_1.xml" ),
245                                 MainFrameApplication.class );
246                     UIManager.setLookAndFeel( synth );
247                 }
248                 catch ( Exception ex ) {
249                     synth_exception = true;
250                     ForesterUtil.printWarningMessage( Constants.PRG_NAME,
251                                                       "could not create synth look and feel: "
252                                                               + ex.getLocalizedMessage() );
253                 }
254             }
255             if ( !Constants.__SYNTH_LF || synth_exception ) {
256                 if ( _configuration.isUseNativeUI() ) {
257                     UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
258                 }
259                 else {
260                     UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
261                 }
262             }
263             //UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
264         }
265         catch ( final UnsupportedLookAndFeelException e ) {
266             Util.dieWithSystemError( "unsupported look and feel: " + e.toString() );
267         }
268         catch ( final ClassNotFoundException e ) {
269             Util.dieWithSystemError( "class not found exception: " + e.toString() );
270         }
271         catch ( final InstantiationException e ) {
272             Util.dieWithSystemError( "instantiation exception: " + e.toString() );
273         }
274         catch ( final IllegalAccessException e ) {
275             Util.dieWithSystemError( "illegal access exception: " + e.toString() );
276         }
277         catch ( final Exception e ) {
278             Util.dieWithSystemError( e.toString() );
279         }
280         // hide until everything is ready
281         setVisible( false );
282         setOptions( Options.createInstance( _configuration ) );
283         setPhylogeneticInferenceOptions( PhylogeneticInferenceOptions.createInstance( _configuration ) );
284         _textframe = null;
285         _species_tree = null;
286         // set title
287         setTitle( Constants.PRG_NAME + " " + Constants.VERSION + " (" + Constants.PRG_DATE + ")" );
288         _mainpanel = new MainPanel( _configuration, this );
289         // The file dialogs
290         _open_filechooser = new JFileChooser();
291         _open_filechooser.setCurrentDirectory( new File( "." ) );
292         _open_filechooser.setMultiSelectionEnabled( false );
293         _open_filechooser.addChoosableFileFilter( MainFrameApplication.xmlfilter );
294         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhxfilter );
295         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter );
296         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter );
297         _open_filechooser.addChoosableFileFilter( MainFrameApplication.tolfilter );
298         _open_filechooser.addChoosableFileFilter( _open_filechooser.getAcceptAllFileFilter() );
299         _open_filechooser.setFileFilter( MainFrameApplication.defaultfilter );
300         _open_filechooser_for_species_tree = new JFileChooser();
301         _open_filechooser_for_species_tree.setCurrentDirectory( new File( "." ) );
302         _open_filechooser_for_species_tree.setMultiSelectionEnabled( false );
303         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.xmlfilter );
304         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.tolfilter );
305         _open_filechooser_for_species_tree.setFileFilter( MainFrameApplication.xmlfilter );
306         _save_filechooser = new JFileChooser();
307         _save_filechooser.setCurrentDirectory( new File( "." ) );
308         _save_filechooser.setMultiSelectionEnabled( false );
309         _save_filechooser.setFileFilter( MainFrameApplication.xmlfilter );
310         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nhxfilter );
311         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter );
312         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter );
313         _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() );
314         _writetopdf_filechooser = new JFileChooser();
315         _writetopdf_filechooser.addChoosableFileFilter( MainFrameApplication.pdffilter );
316         _writetographics_filechooser = new JFileChooser();
317         _writetographics_filechooser.addChoosableFileFilter( MainFrameApplication.graphicsfilefilter );
318         // Msa:
319         _msa_filechooser = new JFileChooser();
320         _msa_filechooser.setName( "Read Multiple Sequence Alignment File" );
321         _msa_filechooser.setCurrentDirectory( new File( "." ) );
322         _msa_filechooser.setMultiSelectionEnabled( false );
323         _msa_filechooser.addChoosableFileFilter( _msa_filechooser.getAcceptAllFileFilter() );
324         _msa_filechooser.addChoosableFileFilter( MainFrameApplication.msafilter );
325         // Seqs:
326         _seqs_filechooser = new JFileChooser();
327         _seqs_filechooser.setName( "Read Sequences File" );
328         _seqs_filechooser.setCurrentDirectory( new File( "." ) );
329         _seqs_filechooser.setMultiSelectionEnabled( false );
330         _seqs_filechooser.addChoosableFileFilter( _seqs_filechooser.getAcceptAllFileFilter() );
331         _seqs_filechooser.addChoosableFileFilter( MainFrameApplication.seqsfilter );
332         // Expression
333         _values_filechooser = new JFileChooser();
334         _values_filechooser.setCurrentDirectory( new File( "." ) );
335         _values_filechooser.setMultiSelectionEnabled( false );
336         // build the menu bar
337         _jmenubar = new JMenuBar();
338         if ( !_configuration.isUseNativeUI() ) {
339             _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
340         }
341         buildFileMenu();
342         if ( Constants.__ALLOW_PHYLOGENETIC_INFERENCE ) {
343             buildPhylogeneticInferenceMenu();
344         }
345         buildAnalysisMenu();
346         buildToolsMenu();
347         buildViewMenu();
348         buildFontSizeMenu();
349         buildOptionsMenu();
350         buildTypeMenu();
351         buildHelpMenu();
352         setJMenuBar( _jmenubar );
353         _jmenubar.add( _help_jmenu );
354         _contentpane = getContentPane();
355         _contentpane.setLayout( new BorderLayout() );
356         _contentpane.add( _mainpanel, BorderLayout.CENTER );
357         // App is this big
358         setSize( MainFrameApplication.FRAME_X_SIZE, MainFrameApplication.FRAME_Y_SIZE );
359         //        addWindowFocusListener( new WindowAdapter() {
360         //
361         //            @Override
362         //            public void windowGainedFocus( WindowEvent e ) {
363         //                requestFocusInWindow();
364         //            }
365         //        } );
366         // The window listener
367         setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
368         addWindowListener( new WindowAdapter() {
369
370             @Override
371             public void windowClosing( final WindowEvent e ) {
372                 if ( isUnsavedDataPresent() ) {
373                     final int r = JOptionPane.showConfirmDialog( null,
374                                                                  "Exit despite potentially unsaved changes?",
375                                                                  "Exit?",
376                                                                  JOptionPane.YES_NO_OPTION );
377                     if ( r != JOptionPane.YES_OPTION ) {
378                         return;
379                     }
380                 }
381                 else {
382                     final int r = JOptionPane.showConfirmDialog( null,
383                                                                  "Exit Archaeopteryx?",
384                                                                  "Exit?",
385                                                                  JOptionPane.YES_NO_OPTION );
386                     if ( r != JOptionPane.YES_OPTION ) {
387                         return;
388                     }
389                 }
390                 exit();
391             }
392         } );
393         // The component listener
394         addComponentListener( new ComponentAdapter() {
395
396             @Override
397             public void componentResized( final ComponentEvent e ) {
398                 if ( _mainpanel.getCurrentTreePanel() != null ) {
399                     _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel()
400                                                                                        .getWidth(),
401                                                                                _mainpanel.getCurrentTreePanel()
402                                                                                        .getHeight(),
403                                                                                false );
404                 }
405             }
406         } );
407         requestFocusInWindow();
408         // addKeyListener( this );
409         setVisible( true );
410         if ( ( phys != null ) && ( phys.length > 0 ) ) {
411             Util.addPhylogeniesToTabs( phys, title, null, _configuration, _mainpanel );
412             validate();
413             getMainPanel().getControlPanel().showWholeAll();
414             getMainPanel().getControlPanel().showWhole();
415         }
416         activateSaveAllIfNeeded();
417         // ...and its children
418         _contentpane.repaint();
419         System.gc();
420     }
421
422     private MainFrameApplication( final Phylogeny[] phys, final String config_file, final String title ) {
423         // Reads the config file (false, false => not url, not applet):
424         this( phys, new Configuration( config_file, false, false ), title );
425     }
426
427     @Override
428     public void actionPerformed( final ActionEvent e ) {
429         try {
430             super.actionPerformed( e );
431             final Object o = e.getSource();
432             // Handle app-specific actions here:
433             if ( o == _open_item ) {
434                 readPhylogeniesFromFile();
435             }
436             else if ( o == _save_item ) {
437                 writeToFile( _mainpanel.getCurrentPhylogeny() );
438                 // If subtree currently displayed, save it, instead of complete
439                 // tree.
440             }
441             else if ( o == _new_item ) {
442                 newTree();
443             }
444             else if ( o == _save_all_item ) {
445                 writeAllToFile();
446             }
447             else if ( o == _close_item ) {
448                 closeCurrentPane();
449             }
450             else if ( o == _write_to_pdf_item ) {
451                 writeToPdf( _mainpanel.getCurrentPhylogeny() );
452             }
453             else if ( o == _write_to_jpg_item ) {
454                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.JPG );
455             }
456             else if ( o == _write_to_png_item ) {
457                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.PNG );
458             }
459             else if ( o == _write_to_gif_item ) {
460                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.GIF );
461             }
462             else if ( o == _write_to_tif_item ) {
463                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.TIFF );
464             }
465             else if ( o == _write_to_bmp_item ) {
466                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.BMP );
467             }
468             else if ( o == _print_item ) {
469                 print();
470             }
471             else if ( o == _load_species_tree_item ) {
472                 readSpeciesTreeFromFile();
473             }
474             else if ( o == _sdi_item ) {
475                 if ( isSubtreeDisplayed() ) {
476                     return;
477                 }
478                 executeSDI();
479             }
480             else if ( o == _lineage_inference ) {
481                 if ( isSubtreeDisplayed() ) {
482                     JOptionPane.showMessageDialog( this,
483                                                    "Subtree is shown.",
484                                                    "Cannot infer ancestral taxonomies",
485                                                    JOptionPane.ERROR_MESSAGE );
486                     return;
487                 }
488                 executeLineageInference();
489             }
490             else if ( o == _function_analysis ) {
491                 executeFunctionAnalysis();
492             }
493             else if ( o == _obtain_detailed_taxonomic_information_jmi ) {
494                 if ( isSubtreeDisplayed() ) {
495                     return;
496                 }
497                 obtainDetailedTaxonomicInformation();
498             }
499             else if ( o == _obtain_uniprot_seq_information_jmi ) {
500                 obtainUniProtSequenceInformation();
501             }
502             else if ( o == _read_values_jmi ) {
503                 if ( isSubtreeDisplayed() ) {
504                     return;
505                 }
506                 addExpressionValuesFromFile();
507             }
508             else if ( o == _move_node_names_to_tax_sn_jmi ) {
509                 moveNodeNamesToTaxSn();
510             }
511             else if ( o == _move_node_names_to_seq_names_jmi ) {
512                 moveNodeNamesToSeqNames();
513             }
514             else if ( o == _extract_tax_code_from_node_names_jmi ) {
515                 extractTaxCodeFromNodeNames();
516             }
517             else if ( o == _gsdi_item ) {
518                 if ( isSubtreeDisplayed() ) {
519                     return;
520                 }
521                 executeGSDI();
522             }
523             else if ( o == _root_min_dups_item ) {
524                 if ( isSubtreeDisplayed() ) {
525                     return;
526                 }
527                 executeSDIR( false );
528             }
529             else if ( o == _root_min_cost_l_item ) {
530                 if ( isSubtreeDisplayed() ) {
531                     return;
532                 }
533                 executeSDIR( true );
534             }
535             else if ( o == _graphics_export_visible_only_cbmi ) {
536                 updateOptions( getOptions() );
537             }
538             else if ( o == _antialias_print_cbmi ) {
539                 updateOptions( getOptions() );
540             }
541             else if ( o == _print_black_and_white_cbmi ) {
542                 updateOptions( getOptions() );
543             }
544             else if ( o == _print_using_actual_size_cbmi ) {
545                 updateOptions( getOptions() );
546             }
547             else if ( o == _graphics_export_using_actual_size_cbmi ) {
548                 updateOptions( getOptions() );
549             }
550             else if ( o == _print_size_mi ) {
551                 choosePrintSize();
552             }
553             else if ( o == _choose_pdf_width_mi ) {
554                 choosePdfWidth();
555             }
556             else if ( o == _internal_number_are_confidence_for_nh_parsing_cbmi ) {
557                 updateOptions( getOptions() );
558             }
559             else if ( o == _replace_underscores_cbmi ) {
560                 if ( ( _extract_pfam_style_tax_codes_cbmi != null ) && _replace_underscores_cbmi.isSelected() ) {
561                     _extract_pfam_style_tax_codes_cbmi.setSelected( false );
562                 }
563                 updateOptions( getOptions() );
564             }
565             else if ( o == _collapse_below_threshold ) {
566                 if ( isSubtreeDisplayed() ) {
567                     return;
568                 }
569                 collapseBelowThreshold();
570             }
571             else if ( o == _extract_pfam_style_tax_codes_cbmi ) {
572                 if ( ( _replace_underscores_cbmi != null ) && _extract_pfam_style_tax_codes_cbmi.isSelected() ) {
573                     _replace_underscores_cbmi.setSelected( false );
574                 }
575                 updateOptions( getOptions() );
576             }
577             else if ( o == _inference_from_msa_item ) {
578                 executePhyleneticInference( false );
579             }
580             else if ( o == _inference_from_seqs_item ) {
581                 executePhyleneticInference( true );
582             }
583             _contentpane.repaint();
584         }
585         catch ( final Exception ex ) {
586             Util.unexpectedException( ex );
587         }
588         catch ( final Error err ) {
589             Util.unexpectedError( err );
590         }
591     }
592
593     void buildAnalysisMenu() {
594         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
595         _analysis_menu.add( _sdi_item = new JMenuItem( "SDI (Speciation Duplication Inference)" ) );
596         if ( !Constants.__RELEASE && !Constants.__SNAPSHOT_RELEASE ) {
597             _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
598         }
599         _analysis_menu.addSeparator();
600         _analysis_menu.add( _root_min_dups_item = new JMenuItem( "Root by Minimizing Duplications | Height (SDI)" ) );
601         _analysis_menu.add( _root_min_cost_l_item = new JMenuItem( "Root by Minimizing Cost L | Height (SDI)" ) );
602         _analysis_menu.addSeparator();
603         _analysis_menu.add( _load_species_tree_item = new JMenuItem( "Load Species Tree..." ) );
604         customizeJMenuItem( _sdi_item );
605         customizeJMenuItem( _gsdi_item );
606         customizeJMenuItem( _root_min_dups_item );
607         customizeJMenuItem( _root_min_cost_l_item );
608         customizeJMenuItem( _load_species_tree_item );
609         _analysis_menu.addSeparator();
610         _analysis_menu.add( _lineage_inference = new JMenuItem( "Infer Ancestor Taxonomies" ) );
611         customizeJMenuItem( _lineage_inference );
612         _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
613         _jmenubar.add( _analysis_menu );
614     }
615
616     void buildPhylogeneticInferenceMenu() {
617         _inference_menu = MainFrame.createMenu( "Inference", getConfiguration() );
618         _inference_menu
619                 .add( _inference_from_msa_item = new JMenuItem( "From Multiple Sequence Alignment...(EXPERIMENTAL - DO NOT USE!!) " ) );
620         customizeJMenuItem( _inference_from_msa_item );
621         _inference_from_msa_item.setToolTipText( "Basic phylogenetic inference from MSA" );
622         _inference_menu
623                 .add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences...(EXPERIMENTAL - DO NOT USE!!) " ) );
624         customizeJMenuItem( _inference_from_seqs_item );
625         _inference_from_seqs_item.setToolTipText( "Basic phylogenetic inference including multiple sequence alignment" );
626         _jmenubar.add( _inference_menu );
627     }
628
629     @Override
630     void buildFileMenu() {
631         _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
632         _file_jmenu.add( _open_item = new JMenuItem( "Read Tree from File..." ) );
633         _file_jmenu.addSeparator();
634         _file_jmenu.add( _open_url_item = new JMenuItem( "Read Tree from URL/Webservice..." ) );
635         _file_jmenu.addSeparator();
636         final WebservicesManager webservices_manager = WebservicesManager.getInstance();
637         _load_phylogeny_from_webservice_menu_items = new JMenuItem[ webservices_manager
638                 .getAvailablePhylogeniesWebserviceClients().size() ];
639         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
640             final PhylogeniesWebserviceClient client = webservices_manager.getAvailablePhylogeniesWebserviceClient( i );
641             _load_phylogeny_from_webservice_menu_items[ i ] = new JMenuItem( client.getMenuName() );
642             _file_jmenu.add( _load_phylogeny_from_webservice_menu_items[ i ] );
643         }
644         if ( getConfiguration().isEditable() ) {
645             _file_jmenu.addSeparator();
646             _file_jmenu.add( _new_item = new JMenuItem( "New" ) );
647             _new_item.setToolTipText( "to create a new tree with one node, as source for manual tree construction" );
648         }
649         _file_jmenu.addSeparator();
650         _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
651         _file_jmenu.add( _save_all_item = new JMenuItem( "Save All Trees As..." ) );
652         _save_all_item.setToolTipText( "Write all phylogenies to one file." );
653         _save_all_item.setEnabled( false );
654         _file_jmenu.addSeparator();
655         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
656         if ( Util.canWriteFormat( "tif" ) || Util.canWriteFormat( "tiff" ) || Util.canWriteFormat( "TIF" ) ) {
657             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
658         }
659         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
660         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
661         if ( Util.canWriteFormat( "gif" ) ) {
662             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
663         }
664         if ( Util.canWriteFormat( "bmp" ) ) {
665             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
666         }
667         _file_jmenu.addSeparator();
668         _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
669         _file_jmenu.addSeparator();
670         _file_jmenu.add( _close_item = new JMenuItem( "Close Tab" ) );
671         _close_item.setToolTipText( "To close the current pane." );
672         _close_item.setEnabled( true );
673         _file_jmenu.addSeparator();
674         _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
675         // For print in color option item
676         customizeJMenuItem( _open_item );
677         _open_item
678                 .setFont( new Font( _open_item.getFont().getFontName(), Font.BOLD, _open_item.getFont().getSize() + 4 ) );
679         customizeJMenuItem( _open_url_item );
680         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
681             customizeJMenuItem( _load_phylogeny_from_webservice_menu_items[ i ] );
682         }
683         customizeJMenuItem( _save_item );
684         if ( getConfiguration().isEditable() ) {
685             customizeJMenuItem( _new_item );
686         }
687         customizeJMenuItem( _close_item );
688         customizeJMenuItem( _save_all_item );
689         customizeJMenuItem( _write_to_pdf_item );
690         customizeJMenuItem( _write_to_png_item );
691         customizeJMenuItem( _write_to_jpg_item );
692         customizeJMenuItem( _write_to_gif_item );
693         customizeJMenuItem( _write_to_tif_item );
694         customizeJMenuItem( _write_to_bmp_item );
695         customizeJMenuItem( _print_item );
696         customizeJMenuItem( _exit_item );
697         _jmenubar.add( _file_jmenu );
698     }
699
700     void buildOptionsMenu() {
701         _options_jmenu = MainFrame.createMenu( OPTIONS_HEADER, getConfiguration() );
702         _options_jmenu.addChangeListener( new ChangeListener() {
703
704             @Override
705             public void stateChanged( final ChangeEvent e ) {
706                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
707                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
708                 MainFrame
709                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
710                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, MainFrame
711                         .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) );
712                 setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );
713                 setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );
714                 MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),
715                                                                      _show_scale_cbmi,
716                                                                      _show_branch_length_values_cbmi,
717                                                                      _non_lined_up_cladograms_rbmi,
718                                                                      _uniform_cladograms_rbmi,
719                                                                      _ext_node_dependent_cladogram_rbmi,
720                                                                      _label_direction_cbmi );
721                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
722                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
723                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
724             }
725         } );
726         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( DISPLAY_SUBHEADER ), getConfiguration() ) );
727         _options_jmenu
728                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
729         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
730         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
731         _radio_group_1 = new ButtonGroup();
732         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
733         _radio_group_1.add( _uniform_cladograms_rbmi );
734         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
735         _options_jmenu.add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL ) );
736         _options_jmenu
737                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
738         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
739         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
740         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
741         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( DISPLAY_SCALE_LABEL ) );
742         _options_jmenu
743                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
744         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( SHOW_OVERVIEW_LABEL ) );
745         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
746         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
747         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
748         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
749         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( ABBREV_SN_LABEL ) );
750         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( SCREEN_ANTIALIAS_LABEL ) );
751         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( BG_GRAD_LABEL ) );
752         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
753             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( SHOW_DOMAIN_LABELS_LABEL ) );
754         }
755         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
756         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
757         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
758         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
759         _options_jmenu.addSeparator();
760         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( SEARCH_SUBHEADER ), getConfiguration() ) );
761         _options_jmenu.add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( SEARCH_CASE_SENSITIVE_LABEL ) );
762         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( SEARCH_TERMS_ONLY_LABEL ) );
763         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
764         _options_jmenu.addSeparator();
765         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ),
766                                                       getConfiguration() ) );
767         _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) );
768         _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) );
769         _options_jmenu
770                 .add( _print_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PDF export and Printing" ) );
771         _options_jmenu
772                 .add( _graphics_export_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PNG, JPG, and GIF export" ) );
773         _options_jmenu
774                 .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) );
775         _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) );
776         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
777         _options_jmenu.addSeparator();
778         _options_jmenu
779                 .add( customizeMenuItemAsLabel( new JMenuItem( "Newick/NHX/Nexus Parsing:" ), getConfiguration() ) );
780         _options_jmenu
781                 .add( _internal_number_are_confidence_for_nh_parsing_cbmi = new JCheckBoxMenuItem( "Internal Numbers Are Confidence Values" ) );
782         _options_jmenu.add( _replace_underscores_cbmi = new JCheckBoxMenuItem( "Replace Underscores with Spaces" ) );
783         _options_jmenu
784                 .add( _extract_pfam_style_tax_codes_cbmi = new JCheckBoxMenuItem( "Extract Taxonomy Codes from Pfam-style Labels" ) );
785         customizeJMenuItem( _choose_font_mi );
786         customizeJMenuItem( _choose_minimal_confidence_mi );
787         customizeJMenuItem( _switch_colors_mi );
788         customizeJMenuItem( _print_size_mi );
789         customizeJMenuItem( _choose_pdf_width_mi );
790         customizeJMenuItem( _overview_placment_mi );
791         customizeCheckBoxMenuItem( _show_default_node_shapes_cbmi, getOptions().isShowDefaultNodeShapes() );
792         customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
793         customizeJMenuItem( _cycle_node_shape_mi );
794         customizeJMenuItem( _cycle_node_fill_mi );
795         customizeJMenuItem( _choose_node_size_mi );
796         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
797         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
798         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
799         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
800         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
801         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
802         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
803         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
804                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
805         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
806                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
807         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
808                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
809         customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );
810         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
811         customizeCheckBoxMenuItem( _label_direction_cbmi,
812                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
813         customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() );
814         customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() );
815         customizeCheckBoxMenuItem( _internal_number_are_confidence_for_nh_parsing_cbmi, getOptions()
816                 .isInternalNumberAreConfidenceForNhParsing() );
817         customizeCheckBoxMenuItem( _extract_pfam_style_tax_codes_cbmi, getOptions()
818                 .isExtractPfamTaxonomyCodesInNhParsing() );
819         customizeCheckBoxMenuItem( _replace_underscores_cbmi, getOptions().isReplaceUnderscoresInNhParsing() );
820         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
821         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
822         customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() );
823         customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() );
824         customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions()
825                 .isGraphicsExportUsingActualSize() );
826         _jmenubar.add( _options_jmenu );
827     }
828
829     void buildToolsMenu() {
830         _tools_menu = createMenu( "Tools", getConfiguration() );
831         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
832         customizeJMenuItem( _confcolor_item );
833         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
834         customizeJMenuItem( _taxcolor_item );
835         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) );
836         _remove_branch_color_item.setToolTipText( "To delete branch color values from the current phylogeny" );
837         customizeJMenuItem( _remove_branch_color_item );
838         _tools_menu.addSeparator();
839         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
840         customizeJMenuItem( _midpoint_root_item );
841         _tools_menu.addSeparator();
842         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
843         customizeJMenuItem( _collapse_species_specific_subtrees );
844         _tools_menu
845                 .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold" ) );
846         customizeJMenuItem( _collapse_below_threshold );
847         _collapse_below_threshold
848                 .setToolTipText( "To permanently collapse branches without at least one support value above a given threshold" );
849         _tools_menu.addSeparator();
850         _tools_menu
851                 .add( _move_node_names_to_tax_sn_jmi = new JMenuItem( "Transfer Node Names to Taxonomic Scientific Names" ) );
852         customizeJMenuItem( _move_node_names_to_tax_sn_jmi );
853         _move_node_names_to_tax_sn_jmi.setToolTipText( "To interpret node names as taxonomic scientific names" );
854         _tools_menu.add( _move_node_names_to_seq_names_jmi = new JMenuItem( "Transfer Node Names to Sequence Names" ) );
855         customizeJMenuItem( _move_node_names_to_seq_names_jmi );
856         _move_node_names_to_seq_names_jmi.setToolTipText( "To interpret node names as sequence (protein, gene) names" );
857         _tools_menu
858                 .add( _extract_tax_code_from_node_names_jmi = new JMenuItem( "Extract Taxonomic Codes from Node Names" ) );
859         customizeJMenuItem( _extract_tax_code_from_node_names_jmi );
860         _extract_tax_code_from_node_names_jmi
861                 .setToolTipText( "To extract taxonomic codes (mnemonics) from nodes names in the form of 'xyz_ECOLI'" );
862         _tools_menu.addSeparator();
863         _tools_menu
864                 .add( _infer_common_sn_names_item = new JMenuItem( "Infer Common Parts of Internal Scientific Names" ) );
865         customizeJMenuItem( _infer_common_sn_names_item );
866         _tools_menu.addSeparator();
867         _tools_menu
868                 .add( _obtain_detailed_taxonomic_information_jmi = new JMenuItem( "Obtain Detailed Taxonomic Information" ) );
869         customizeJMenuItem( _obtain_detailed_taxonomic_information_jmi );
870         _obtain_detailed_taxonomic_information_jmi
871                 .setToolTipText( "To add additional taxonomic information (from UniProt Taxonomy)" );
872         _tools_menu
873                 .add( _obtain_uniprot_seq_information_jmi = new JMenuItem( "Obtain Sequence Information (from UniProt)" ) );
874         customizeJMenuItem( _obtain_uniprot_seq_information_jmi );
875         _obtain_uniprot_seq_information_jmi.setToolTipText( "To add additional sequence information (from UniProt)" );
876         _tools_menu.addSeparator();
877         if ( !Constants.__RELEASE ) {
878             _tools_menu.add( _function_analysis = new JMenuItem( "Add UniProtKB Annotations" ) );
879             customizeJMenuItem( _function_analysis );
880             _function_analysis
881                     .setToolTipText( "To add UniProtKB annotations for sequences with appropriate identifiers" );
882             _tools_menu.addSeparator();
883         }
884         _tools_menu.add( _read_values_jmi = new JMenuItem( "Read Vector/Expression Values" ) );
885         customizeJMenuItem( _read_values_jmi );
886         _read_values_jmi.setToolTipText( "To add vector (e.g. gene expression) values (beta)" );
887         _jmenubar.add( _tools_menu );
888     }
889
890     private void choosePdfWidth() {
891         final String s = ( String ) JOptionPane.showInputDialog( this,
892                                                                  "Please enter the default line width for PDF export.\n"
893                                                                          + "[current value: "
894                                                                          + getOptions().getPrintLineWidth() + "]\n",
895                                                                  "Line Width for PDF Export",
896                                                                  JOptionPane.QUESTION_MESSAGE,
897                                                                  null,
898                                                                  null,
899                                                                  getOptions().getPrintLineWidth() );
900         if ( !ForesterUtil.isEmpty( s ) ) {
901             boolean success = true;
902             float f = 0.0f;
903             final String m_str = s.trim();
904             if ( !ForesterUtil.isEmpty( m_str ) ) {
905                 try {
906                     f = Float.parseFloat( m_str );
907                 }
908                 catch ( final Exception ex ) {
909                     success = false;
910                 }
911             }
912             else {
913                 success = false;
914             }
915             if ( success && ( f > 0.0 ) ) {
916                 getOptions().setPrintLineWidth( f );
917             }
918         }
919     }
920
921     private void choosePrintSize() {
922         final String s = ( String ) JOptionPane.showInputDialog( this,
923                                                                  "Please enter values for width and height,\nseparated by a comma.\n"
924                                                                          + "[current values: "
925                                                                          + getOptions().getPrintSizeX() + ", "
926                                                                          + getOptions().getPrintSizeY() + "]\n"
927                                                                          + "[A4: " + Constants.A4_SIZE_X + ", "
928                                                                          + Constants.A4_SIZE_Y + "]\n" + "[US Letter: "
929                                                                          + Constants.US_LETTER_SIZE_X + ", "
930                                                                          + Constants.US_LETTER_SIZE_Y + "]",
931                                                                  "Default Size for Graphics Export",
932                                                                  JOptionPane.QUESTION_MESSAGE,
933                                                                  null,
934                                                                  null,
935                                                                  getOptions().getPrintSizeX() + ", "
936                                                                          + getOptions().getPrintSizeY() );
937         if ( !ForesterUtil.isEmpty( s ) && ( s.indexOf( ',' ) > 0 ) ) {
938             boolean success = true;
939             int x = 0;
940             int y = 0;
941             final String[] str_ary = s.split( "," );
942             if ( str_ary.length == 2 ) {
943                 final String x_str = str_ary[ 0 ].trim();
944                 final String y_str = str_ary[ 1 ].trim();
945                 if ( !ForesterUtil.isEmpty( x_str ) && !ForesterUtil.isEmpty( y_str ) ) {
946                     try {
947                         x = Integer.parseInt( x_str );
948                         y = Integer.parseInt( y_str );
949                     }
950                     catch ( final Exception ex ) {
951                         success = false;
952                     }
953                 }
954                 else {
955                     success = false;
956                 }
957             }
958             else {
959                 success = false;
960             }
961             if ( success && ( x > 1 ) && ( y > 1 ) ) {
962                 getOptions().setPrintSizeX( x );
963                 getOptions().setPrintSizeY( y );
964             }
965         }
966     }
967
968     @Override
969     void close() {
970         if ( isUnsavedDataPresent() ) {
971             final int r = JOptionPane.showConfirmDialog( this,
972                                                          "Exit despite potentially unsaved changes?",
973                                                          "Exit?",
974                                                          JOptionPane.YES_NO_OPTION );
975             if ( r != JOptionPane.YES_OPTION ) {
976                 return;
977             }
978         }
979         exit();
980     }
981
982     private void closeCurrentPane() {
983         if ( getMainPanel().getCurrentTreePanel() != null ) {
984             if ( getMainPanel().getCurrentTreePanel().isEdited() ) {
985                 final int r = JOptionPane.showConfirmDialog( this,
986                                                              "Close tab despite potentially unsaved changes?",
987                                                              "Close Tab?",
988                                                              JOptionPane.YES_NO_OPTION );
989                 if ( r != JOptionPane.YES_OPTION ) {
990                     return;
991                 }
992             }
993             getMainPanel().closeCurrentPane();
994             activateSaveAllIfNeeded();
995         }
996     }
997
998     private void collapse( final Phylogeny phy, final double m ) {
999         final PhylogenyNodeIterator it = phy.iteratorPostorder();
1000         final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();
1001         double min_support = Double.MAX_VALUE;
1002         boolean conf_present = false;
1003         while ( it.hasNext() ) {
1004             final PhylogenyNode n = it.next();
1005             if ( !n.isExternal() && !n.isRoot() ) {
1006                 final List<Confidence> c = n.getBranchData().getConfidences();
1007                 if ( ( c != null ) && ( c.size() > 0 ) ) {
1008                     conf_present = true;
1009                     double max = 0;
1010                     for( final Confidence confidence : c ) {
1011                         if ( confidence.getValue() > max ) {
1012                             max = confidence.getValue();
1013                         }
1014                     }
1015                     if ( max < getMinNotCollapseConfidenceValue() ) {
1016                         to_be_removed.add( n );
1017                     }
1018                     if ( max < min_support ) {
1019                         min_support = max;
1020                     }
1021                 }
1022             }
1023         }
1024         if ( conf_present ) {
1025             for( final PhylogenyNode node : to_be_removed ) {
1026                 PhylogenyMethods.removeNode( node, phy );
1027             }
1028             if ( to_be_removed.size() > 0 ) {
1029                 phy.externalNodesHaveChanged();
1030                 phy.hashIDs();
1031                 phy.recalculateNumberOfExternalDescendants( true );
1032                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
1033                 getCurrentTreePanel().setEdited( true );
1034                 getCurrentTreePanel().repaint();
1035             }
1036             if ( to_be_removed.size() > 0 ) {
1037                 JOptionPane.showMessageDialog( this, "Collapsed " + to_be_removed.size()
1038                         + " branches with\nconfidence values below " + getMinNotCollapseConfidenceValue(), "Collapsed "
1039                         + to_be_removed.size() + " branches", JOptionPane.INFORMATION_MESSAGE );
1040             }
1041             else {
1042                 JOptionPane.showMessageDialog( this, "No branch collapsed,\nminimum confidence value per branch is "
1043                         + min_support, "No branch collapsed", JOptionPane.INFORMATION_MESSAGE );
1044             }
1045         }
1046         else {
1047             JOptionPane.showMessageDialog( this,
1048                                            "No branch collapsed because no confidence values present",
1049                                            "No confidence values present",
1050                                            JOptionPane.INFORMATION_MESSAGE );
1051         }
1052     }
1053
1054     private void collapseBelowThreshold() {
1055         if ( getCurrentTreePanel() != null ) {
1056             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1057             if ( ( phy != null ) && !phy.isEmpty() ) {
1058                 final String s = ( String ) JOptionPane.showInputDialog( this,
1059                                                                          "Please enter the minimum confidence value\n",
1060                                                                          "Minimal Confidence Value",
1061                                                                          JOptionPane.QUESTION_MESSAGE,
1062                                                                          null,
1063                                                                          null,
1064                                                                          getMinNotCollapseConfidenceValue() );
1065                 if ( !ForesterUtil.isEmpty( s ) ) {
1066                     boolean success = true;
1067                     double m = 0.0;
1068                     final String m_str = s.trim();
1069                     if ( !ForesterUtil.isEmpty( m_str ) ) {
1070                         try {
1071                             m = Double.parseDouble( m_str );
1072                         }
1073                         catch ( final Exception ex ) {
1074                             success = false;
1075                         }
1076                     }
1077                     else {
1078                         success = false;
1079                     }
1080                     if ( success && ( m >= 0.0 ) ) {
1081                         setMinNotCollapseConfidenceValue( m );
1082                         collapse( phy, m );
1083                     }
1084                 }
1085             }
1086         }
1087     }
1088
1089     private PhyloXmlParser createPhyloXmlParser() {
1090         PhyloXmlParser xml_parser = null;
1091         if ( getConfiguration().isValidatePhyloXmlAgainstSchema() ) {
1092             try {
1093                 xml_parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
1094             }
1095             catch ( final Exception e ) {
1096                 JOptionPane.showMessageDialog( this,
1097                                                e.getLocalizedMessage(),
1098                                                "failed to create validating XML parser",
1099                                                JOptionPane.WARNING_MESSAGE );
1100             }
1101         }
1102         if ( xml_parser == null ) {
1103             xml_parser = new PhyloXmlParser();
1104         }
1105         return xml_parser;
1106     }
1107
1108     void executeGSDI() {
1109         if ( !isOKforSDI( false, true ) ) {
1110             return;
1111         }
1112         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1113             JOptionPane.showMessageDialog( this,
1114                                            "Gene tree is not rooted.",
1115                                            "Cannot execute GSDI",
1116                                            JOptionPane.ERROR_MESSAGE );
1117             return;
1118         }
1119         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1120         gene_tree.setAllNodesToNotCollapse();
1121         gene_tree.recalculateNumberOfExternalDescendants( false );
1122         GSDI gsdi = null;
1123         int duplications = -1;
1124         try {
1125             gsdi = new GSDI( gene_tree, _species_tree.copy(), true );
1126             duplications = gsdi.getDuplicationsSum();
1127         }
1128         catch ( final Exception e ) {
1129             JOptionPane.showMessageDialog( this, e.toString(), "Error during GSDI", JOptionPane.ERROR_MESSAGE );
1130         }
1131         gene_tree.setRerootable( false );
1132         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1133         getControlPanel().setShowEvents( true );
1134         showWhole();
1135         _mainpanel.getCurrentTreePanel().setEdited( true );
1136         JOptionPane.showMessageDialog( this,
1137                                        "Number of duplications: " + duplications,
1138                                        "GSDI successfully completed",
1139                                        JOptionPane.INFORMATION_MESSAGE );
1140     }
1141
1142     void executeFunctionAnalysis() {
1143         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) {
1144             return;
1145         }
1146         final GoAnnotation a = new GoAnnotation( this,
1147                                                  _mainpanel.getCurrentTreePanel(),
1148                                                  _mainpanel.getCurrentPhylogeny() );
1149         new Thread( a ).start();
1150     }
1151
1152     void executeLineageInference() {
1153         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) {
1154             return;
1155         }
1156         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1157             JOptionPane.showMessageDialog( this,
1158                                            "Phylogeny is not rooted.",
1159                                            "Cannot infer ancestral taxonomies",
1160                                            JOptionPane.ERROR_MESSAGE );
1161             return;
1162         }
1163         final Phylogeny phy = _mainpanel.getCurrentPhylogeny().copy();
1164         final AncestralTaxonomyInferrer inferrer = new AncestralTaxonomyInferrer( this,
1165                                                                                   _mainpanel.getCurrentTreePanel(),
1166                                                                                   phy );
1167         new Thread( inferrer ).start();
1168     }
1169
1170     private void executePhyleneticInference( final boolean from_unaligned_seqs ) {
1171         final PhyloInferenceDialog dialog = new PhyloInferenceDialog( this,
1172                                                                       getPhylogeneticInferenceOptions(),
1173                                                                       from_unaligned_seqs );
1174         dialog.activate();
1175         if ( dialog.getValue() == JOptionPane.OK_OPTION ) {
1176             if ( !from_unaligned_seqs ) {
1177                 if ( getMsa() != null ) {
1178                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getMsa(),
1179                                                                                     getPhylogeneticInferenceOptions()
1180                                                                                             .copy(), this );
1181                     new Thread( inferrer ).start();
1182                 }
1183                 else {
1184                     JOptionPane.showMessageDialog( this,
1185                                                    "No multiple sequence alignment selected",
1186                                                    "Phylogenetic Inference Not Launched",
1187                                                    JOptionPane.WARNING_MESSAGE );
1188                 }
1189             }
1190             else {
1191                 if ( getSeqs() != null ) {
1192                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getSeqs(),
1193                                                                                     getPhylogeneticInferenceOptions()
1194                                                                                             .copy(), this );
1195                     new Thread( inferrer ).start();
1196                 }
1197                 else {
1198                     JOptionPane.showMessageDialog( this,
1199                                                    "No input sequences selected",
1200                                                    "Phylogenetic Inference Not Launched",
1201                                                    JOptionPane.WARNING_MESSAGE );
1202                 }
1203             }
1204         }
1205     }
1206
1207     void executeSDI() {
1208         if ( !isOKforSDI( true, true ) ) {
1209             return;
1210         }
1211         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1212             JOptionPane.showMessageDialog( this,
1213                                            "Gene tree is not rooted",
1214                                            "Cannot execute SDI",
1215                                            JOptionPane.ERROR_MESSAGE );
1216             return;
1217         }
1218         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1219         gene_tree.setAllNodesToNotCollapse();
1220         gene_tree.recalculateNumberOfExternalDescendants( false );
1221         SDI sdi = null;
1222         int duplications = -1;
1223         try {
1224             sdi = new SDIse( gene_tree, _species_tree.copy() );
1225             duplications = sdi.getDuplicationsSum();
1226         }
1227         catch ( final Exception e ) {
1228             JOptionPane.showMessageDialog( this, e.toString(), "Error during SDI", JOptionPane.ERROR_MESSAGE );
1229         }
1230         gene_tree.setRerootable( false );
1231         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1232         getControlPanel().setShowEvents( true );
1233         showWhole();
1234         _mainpanel.getCurrentTreePanel().setEdited( true );
1235         JOptionPane.showMessageDialog( this,
1236                                        "Number of duplications: " + duplications,
1237                                        "SDI successfully completed",
1238                                        JOptionPane.INFORMATION_MESSAGE );
1239     }
1240
1241     void executeSDIR( final boolean minimize_cost ) {
1242         if ( !isOKforSDI( true, true ) ) {
1243             return;
1244         }
1245         Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1246         final SDIR sdiunrooted = new SDIR();
1247         gene_tree.setAllNodesToNotCollapse();
1248         gene_tree.recalculateNumberOfExternalDescendants( false );
1249         try {
1250             gene_tree = sdiunrooted.infer( gene_tree, _species_tree, minimize_cost, // minimize cost
1251                                            !minimize_cost, // minimize sum of dups
1252                                            true, // minimize height
1253                                            true, // return tree(s)
1254                                            1 )[ 0 ]; // # of trees to return
1255         }
1256         catch ( final Exception e ) {
1257             JOptionPane.showMessageDialog( this, e.toString(), "Error during SDIR", JOptionPane.ERROR_MESSAGE );
1258             return;
1259         }
1260         final int duplications = sdiunrooted.getMinimalDuplications();
1261         gene_tree.setRerootable( false );
1262         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1263         getControlPanel().setShowEvents( true );
1264         showWhole();
1265         _mainpanel.getCurrentTreePanel().setEdited( true );
1266         JOptionPane.showMessageDialog( this,
1267                                        "Number of duplications: " + duplications,
1268                                        "SDIR successfully completed",
1269                                        JOptionPane.INFORMATION_MESSAGE );
1270     }
1271
1272     void exit() {
1273         removeTextFrame();
1274         _mainpanel.terminate();
1275         _contentpane.removeAll();
1276         setVisible( false );
1277         dispose();
1278         System.exit( 0 );
1279     }
1280
1281     private void extractTaxCodeFromNodeNames() {
1282         if ( getCurrentTreePanel() != null ) {
1283             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1284             if ( ( phy != null ) && !phy.isEmpty() ) {
1285                 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1286                 while ( it.hasNext() ) {
1287                     final PhylogenyNode n = it.next();
1288                     final String name = n.getName().trim();
1289                     if ( !ForesterUtil.isEmpty( name ) ) {
1290                         final String code = ForesterUtil.extractTaxonomyCodeFromNodeName( name,
1291                                                                                           false,
1292                                                                                           TAXONOMY_EXTRACTION.YES );
1293                         if ( !ForesterUtil.isEmpty( code ) ) {
1294                             PhylogenyMethods.setTaxonomyCode( n, code );
1295                         }
1296                     }
1297                 }
1298             }
1299         }
1300     }
1301
1302     private ControlPanel getControlPanel() {
1303         return getMainPanel().getControlPanel();
1304     }
1305
1306     private File getCurrentDir() {
1307         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1308             if ( ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1 ) {
1309                 try {
1310                     _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() );
1311                 }
1312                 catch ( final Exception e ) {
1313                     _current_dir = null;
1314                 }
1315             }
1316         }
1317         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1318             if ( System.getProperty( "user.home" ) != null ) {
1319                 _current_dir = new File( System.getProperty( "user.home" ) );
1320             }
1321             else if ( System.getProperty( "user.dir" ) != null ) {
1322                 _current_dir = new File( System.getProperty( "user.dir" ) );
1323             }
1324         }
1325         return _current_dir;
1326     }
1327
1328     @Override
1329     public MainPanel getMainPanel() {
1330         return _mainpanel;
1331     }
1332
1333     private double getMinNotCollapseConfidenceValue() {
1334         return _min_not_collapse;
1335     }
1336
1337     boolean isOKforSDI( final boolean species_tree_has_to_binary, final boolean gene_tree_has_to_binary ) {
1338         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || _mainpanel.getCurrentPhylogeny().isEmpty() ) {
1339             return false;
1340         }
1341         else if ( ( _species_tree == null ) || _species_tree.isEmpty() ) {
1342             JOptionPane.showMessageDialog( this,
1343                                            "No species tree loaded",
1344                                            "Cannot execute SDI",
1345                                            JOptionPane.ERROR_MESSAGE );
1346             return false;
1347         }
1348         else if ( species_tree_has_to_binary && !_species_tree.isCompletelyBinary() ) {
1349             JOptionPane.showMessageDialog( this,
1350                                            "Species tree is not completely binary",
1351                                            "Cannot execute SDI",
1352                                            JOptionPane.ERROR_MESSAGE );
1353             return false;
1354         }
1355         else if ( gene_tree_has_to_binary && !_mainpanel.getCurrentPhylogeny().isCompletelyBinary() ) {
1356             JOptionPane.showMessageDialog( this,
1357                                            "Gene tree is not completely binary",
1358                                            "Cannot execute SDI",
1359                                            JOptionPane.ERROR_MESSAGE );
1360             return false;
1361         }
1362         else {
1363             return true;
1364         }
1365     }
1366
1367     private boolean isUnsavedDataPresent() {
1368         final List<TreePanel> tps = getMainPanel().getTreePanels();
1369         for( final TreePanel tp : tps ) {
1370             if ( tp.isEdited() ) {
1371                 return true;
1372             }
1373         }
1374         return false;
1375     }
1376
1377     private void moveNodeNamesToSeqNames() {
1378         if ( getCurrentTreePanel() != null ) {
1379             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1380             if ( ( phy != null ) && !phy.isEmpty() ) {
1381                 ForesterUtil.transferNodeNameToField( phy, PhylogenyNodeField.SEQUENCE_NAME );
1382             }
1383         }
1384     }
1385
1386     private void moveNodeNamesToTaxSn() {
1387         if ( getCurrentTreePanel() != null ) {
1388             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1389             if ( ( phy != null ) && !phy.isEmpty() ) {
1390                 ForesterUtil.transferNodeNameToField( phy, PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME );
1391             }
1392         }
1393     }
1394
1395     private void newTree() {
1396         final Phylogeny[] phys = new Phylogeny[ 1 ];
1397         final Phylogeny phy = new Phylogeny();
1398         final PhylogenyNode node = new PhylogenyNode();
1399         phy.setRoot( node );
1400         phy.setRooted( true );
1401         phys[ 0 ] = phy;
1402         Util.addPhylogeniesToTabs( phys, "", "", getConfiguration(), getMainPanel() );
1403         _mainpanel.getControlPanel().showWhole();
1404         _mainpanel.getCurrentTreePanel().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1405         _mainpanel.getOptions().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1406         if ( getMainPanel().getMainFrame() == null ) {
1407             // Must be "E" applet version.
1408             ( ( ArchaeopteryxE ) ( ( MainPanelApplets ) getMainPanel() ).getApplet() )
1409                     .setSelectedTypeInTypeMenu( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1410         }
1411         else {
1412             getMainPanel().getMainFrame().setSelectedTypeInTypeMenu( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1413         }
1414         activateSaveAllIfNeeded();
1415         System.gc();
1416     }
1417
1418     private void obtainDetailedTaxonomicInformation() {
1419         if ( getCurrentTreePanel() != null ) {
1420             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1421             if ( ( phy != null ) && !phy.isEmpty() ) {
1422                 final TaxonomyDataObtainer t = new TaxonomyDataObtainer( this,
1423                                                                          _mainpanel.getCurrentTreePanel(),
1424                                                                          phy.copy() );
1425                 new Thread( t ).start();
1426             }
1427         }
1428     }
1429
1430     private void obtainUniProtSequenceInformation() {
1431         if ( getCurrentTreePanel() != null ) {
1432             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1433             if ( ( phy != null ) && !phy.isEmpty() ) {
1434                 final SequenceDataRetriver u = new SequenceDataRetriver( this,
1435                                                                          _mainpanel.getCurrentTreePanel(),
1436                                                                          phy.copy() );
1437                 new Thread( u ).start();
1438             }
1439         }
1440     }
1441
1442     private void print() {
1443         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null )
1444                 || getCurrentTreePanel().getPhylogeny().isEmpty() ) {
1445             return;
1446         }
1447         if ( !getOptions().isPrintUsingActualSize() ) {
1448             getCurrentTreePanel().setParametersForPainting( getOptions().getPrintSizeX() - 80,
1449                                                             getOptions().getPrintSizeY() - 140,
1450                                                             true );
1451             getCurrentTreePanel().resetPreferredSize();
1452             getCurrentTreePanel().repaint();
1453         }
1454         final String job_name = Constants.PRG_NAME;
1455         boolean error = false;
1456         String printer_name = null;
1457         try {
1458             printer_name = Printer.print( getCurrentTreePanel(), job_name );
1459         }
1460         catch ( final Exception e ) {
1461             error = true;
1462             JOptionPane.showMessageDialog( this, e.getMessage(), "Printing Error", JOptionPane.ERROR_MESSAGE );
1463         }
1464         if ( !error && ( printer_name != null ) ) {
1465             String msg = "Printing data sent to printer";
1466             if ( printer_name.length() > 1 ) {
1467                 msg += " [" + printer_name + "]";
1468             }
1469             JOptionPane.showMessageDialog( this, msg, "Printing...", JOptionPane.INFORMATION_MESSAGE );
1470         }
1471         if ( !getOptions().isPrintUsingActualSize() ) {
1472             getControlPanel().showWhole();
1473         }
1474     }
1475
1476     private void printPhylogenyToPdf( final String file_name ) {
1477         if ( !getOptions().isPrintUsingActualSize() ) {
1478             getCurrentTreePanel().setParametersForPainting( getOptions().getPrintSizeX(),
1479                                                             getOptions().getPrintSizeY(),
1480                                                             true );
1481             getCurrentTreePanel().resetPreferredSize();
1482             getCurrentTreePanel().repaint();
1483         }
1484         String pdf_written_to = "";
1485         boolean error = false;
1486         try {
1487             if ( getOptions().isPrintUsingActualSize() ) {
1488                 pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name,
1489                                                                   getCurrentTreePanel(),
1490                                                                   getCurrentTreePanel().getWidth(),
1491                                                                   getCurrentTreePanel().getHeight() );
1492             }
1493             else {
1494                 pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, getCurrentTreePanel(), getOptions()
1495                         .getPrintSizeX(), getOptions().getPrintSizeY() );
1496             }
1497         }
1498         catch ( final IOException e ) {
1499             error = true;
1500             JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
1501         }
1502         if ( !error ) {
1503             if ( !ForesterUtil.isEmpty( pdf_written_to ) ) {
1504                 JOptionPane.showMessageDialog( this,
1505                                                "Wrote PDF to: " + pdf_written_to,
1506                                                "Information",
1507                                                JOptionPane.INFORMATION_MESSAGE );
1508             }
1509             else {
1510                 JOptionPane.showMessageDialog( this,
1511                                                "There was an unknown problem when attempting to write to PDF file: \""
1512                                                        + file_name + "\"",
1513                                                "Error",
1514                                                JOptionPane.ERROR_MESSAGE );
1515             }
1516         }
1517         if ( !getOptions().isPrintUsingActualSize() ) {
1518             getControlPanel().showWhole();
1519         }
1520     }
1521
1522     private void addExpressionValuesFromFile() {
1523         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) {
1524             JOptionPane.showMessageDialog( this,
1525                                            "Need to load evolutionary tree first",
1526                                            "Can Not Read Expression Values",
1527                                            JOptionPane.WARNING_MESSAGE );
1528             return;
1529         }
1530         final File my_dir = getCurrentDir();
1531         if ( my_dir != null ) {
1532             _values_filechooser.setCurrentDirectory( my_dir );
1533         }
1534         final int result = _values_filechooser.showOpenDialog( _contentpane );
1535         final File file = _values_filechooser.getSelectedFile();
1536         if ( ( file != null ) && ( file.length() > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1537             BasicTable<String> t = null;
1538             try {
1539                 t = BasicTableParser.parse( file, "\t" );
1540                 if ( t.getNumberOfColumns() < 2 ) {
1541                     t = BasicTableParser.parse( file, "," );
1542                 }
1543                 if ( t.getNumberOfColumns() < 2 ) {
1544                     t = BasicTableParser.parse( file, " " );
1545                 }
1546             }
1547             catch ( final IOException e ) {
1548                 JOptionPane.showMessageDialog( this,
1549                                                e.getMessage(),
1550                                                "Could Not Read Expression Value Table",
1551                                                JOptionPane.ERROR_MESSAGE );
1552                 return;
1553             }
1554             if ( t.getNumberOfColumns() < 2 ) {
1555                 JOptionPane.showMessageDialog( this,
1556                                                "Table contains " + t.getNumberOfColumns() + " column(s)",
1557                                                "Problem with Expression Value Table",
1558                                                JOptionPane.ERROR_MESSAGE );
1559                 return;
1560             }
1561             if ( t.getNumberOfRows() < 1 ) {
1562                 JOptionPane.showMessageDialog( this,
1563                                                "Table contains zero rows",
1564                                                "Problem with Expression Value Table",
1565                                                JOptionPane.ERROR_MESSAGE );
1566                 return;
1567             }
1568             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1569             if ( t.getNumberOfRows() != phy.getNumberOfExternalNodes() ) {
1570                 JOptionPane.showMessageDialog( this,
1571                                                "Table contains " + t.getNumberOfRows() + " rows, but tree contains "
1572                                                        + phy.getNumberOfExternalNodes() + " external nodes",
1573                                                "Warning",
1574                                                JOptionPane.WARNING_MESSAGE );
1575             }
1576             final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
1577             int not_found = 0;
1578             for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1579                 final PhylogenyNode node = iter.next();
1580                 final String node_name = node.getName();
1581                 if ( !ForesterUtil.isEmpty( node_name ) ) {
1582                     int row = -1;
1583                     try {
1584                         row = t.findRow( node_name );
1585                     }
1586                     catch ( final IllegalArgumentException e ) {
1587                         JOptionPane
1588                                 .showMessageDialog( this,
1589                                                     e.getMessage(),
1590                                                     "Error Mapping Node Identifiers to Expression Value Identifiers",
1591                                                     JOptionPane.ERROR_MESSAGE );
1592                         return;
1593                     }
1594                     if ( row < 0 ) {
1595                         if ( node.isExternal() ) {
1596                             not_found++;
1597                         }
1598                         continue;
1599                     }
1600                     final List<Double> l = new ArrayList<Double>();
1601                     for( int col = 1; col < t.getNumberOfColumns(); ++col ) {
1602                         double d = -100;
1603                         try {
1604                             d = Double.parseDouble( t.getValueAsString( col, row ) );
1605                         }
1606                         catch ( final NumberFormatException e ) {
1607                             JOptionPane.showMessageDialog( this,
1608                                                            "Could not parse \"" + t.getValueAsString( col, row )
1609                                                                    + "\" into a decimal value",
1610                                                            "Issue with Expression Value Table",
1611                                                            JOptionPane.ERROR_MESSAGE );
1612                             return;
1613                         }
1614                         stats.addValue( d );
1615                         l.add( d );
1616                     }
1617                     if ( !l.isEmpty() ) {
1618                         if ( node.getNodeData().getProperties() != null ) {
1619                             node.getNodeData().getProperties()
1620                                     .removePropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF );
1621                         }
1622                         node.getNodeData().setVector( l );
1623                     }
1624                 }
1625             }
1626             if ( not_found > 0 ) {
1627                 JOptionPane.showMessageDialog( this, "Could not fine expression values for " + not_found
1628                         + " external node(s)", "Warning", JOptionPane.WARNING_MESSAGE );
1629             }
1630             getCurrentTreePanel().setStatisticsForExpressionValues( stats );
1631         }
1632     }
1633
1634     private void readPhylogeniesFromFile() {
1635         boolean exception = false;
1636         Phylogeny[] phys = null;
1637         // Set an initial directory if none set yet
1638         final File my_dir = getCurrentDir();
1639         _open_filechooser.setMultiSelectionEnabled( true );
1640         // Open file-open dialog and set current directory
1641         if ( my_dir != null ) {
1642             _open_filechooser.setCurrentDirectory( my_dir );
1643         }
1644         final int result = _open_filechooser.showOpenDialog( _contentpane );
1645         // All done: get the file
1646         final File[] files = _open_filechooser.getSelectedFiles();
1647         setCurrentDir( _open_filechooser.getCurrentDirectory() );
1648         boolean nhx_or_nexus = false;
1649         if ( ( files != null ) && ( files.length > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1650             for( final File file : files ) {
1651                 if ( ( file != null ) && !file.isDirectory() ) {
1652                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1653                         _mainpanel.getCurrentTreePanel().setWaitCursor();
1654                     }
1655                     else {
1656                         _mainpanel.setWaitCursor();
1657                     }
1658                     if ( ( _open_filechooser.getFileFilter() == MainFrameApplication.nhfilter )
1659                             || ( _open_filechooser.getFileFilter() == MainFrameApplication.nhxfilter ) ) {
1660                         try {
1661                             final NHXParser nhx = new NHXParser();
1662                             setSpecialOptionsForNhxParser( nhx );
1663                             phys = ForesterUtil.readPhylogenies( nhx, file );
1664                             nhx_or_nexus = true;
1665                         }
1666                         catch ( final Exception e ) {
1667                             exception = true;
1668                             exceptionOccuredDuringOpenFile( e );
1669                         }
1670                     }
1671                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) {
1672                         warnIfNotPhyloXmlValidation( getConfiguration() );
1673                         try {
1674                             final PhyloXmlParser xml_parser = createPhyloXmlParser();
1675                             phys = ForesterUtil.readPhylogenies( xml_parser, file );
1676                         }
1677                         catch ( final Exception e ) {
1678                             exception = true;
1679                             exceptionOccuredDuringOpenFile( e );
1680                         }
1681                     }
1682                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.tolfilter ) {
1683                         try {
1684                             phys = ForesterUtil.readPhylogenies( new TolParser(), file );
1685                         }
1686                         catch ( final Exception e ) {
1687                             exception = true;
1688                             exceptionOccuredDuringOpenFile( e );
1689                         }
1690                     }
1691                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) {
1692                         try {
1693                             final NexusPhylogeniesParser nex = new NexusPhylogeniesParser();
1694                             setSpecialOptionsForNexParser( nex );
1695                             phys = ForesterUtil.readPhylogenies( nex, file );
1696                             nhx_or_nexus = true;
1697                         }
1698                         catch ( final Exception e ) {
1699                             exception = true;
1700                             exceptionOccuredDuringOpenFile( e );
1701                         }
1702                     }
1703                     // "*.*":
1704                     else {
1705                         try {
1706                             final PhylogenyParser parser = ForesterUtil
1707                                     .createParserDependingOnFileType( file, getConfiguration()
1708                                             .isValidatePhyloXmlAgainstSchema() );
1709                             if ( parser instanceof NexusPhylogeniesParser ) {
1710                                 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) parser;
1711                                 setSpecialOptionsForNexParser( nex );
1712                                 nhx_or_nexus = true;
1713                             }
1714                             else if ( parser instanceof NHXParser ) {
1715                                 final NHXParser nhx = ( NHXParser ) parser;
1716                                 setSpecialOptionsForNhxParser( nhx );
1717                                 nhx_or_nexus = true;
1718                             }
1719                             else if ( parser instanceof PhyloXmlParser ) {
1720                                 warnIfNotPhyloXmlValidation( getConfiguration() );
1721                             }
1722                             phys = ForesterUtil.readPhylogenies( parser, file );
1723                         }
1724                         catch ( final Exception e ) {
1725                             exception = true;
1726                             exceptionOccuredDuringOpenFile( e );
1727                         }
1728                     }
1729                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1730                         _mainpanel.getCurrentTreePanel().setArrowCursor();
1731                     }
1732                     else {
1733                         _mainpanel.setArrowCursor();
1734                     }
1735                     if ( !exception && ( phys != null ) && ( phys.length > 0 ) ) {
1736                         boolean one_desc = false;
1737                         if ( nhx_or_nexus ) {
1738                             for( final Phylogeny phy : phys ) {
1739                                 if ( getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
1740                                     ForesterUtil.transferInternalNodeNamesToConfidence( phy );
1741                                 }
1742                                 if ( PhylogenyMethods.getMinimumDescendentsPerInternalNodes( phy ) == 1 ) {
1743                                     one_desc = true;
1744                                     break;
1745                                 }
1746                             }
1747                         }
1748                         Util.addPhylogeniesToTabs( phys,
1749                                                    file.getName(),
1750                                                    file.getAbsolutePath(),
1751                                                    getConfiguration(),
1752                                                    getMainPanel() );
1753                         _mainpanel.getControlPanel().showWhole();
1754                         if ( nhx_or_nexus && one_desc ) {
1755                             JOptionPane
1756                                     .showMessageDialog( this,
1757                                                         "One or more trees contain (a) node(s) with one descendant, "
1758                                                                 + ForesterUtil.LINE_SEPARATOR
1759                                                                 + "possibly indicating illegal parentheses within node names.",
1760                                                         "Warning: Possible Error in New Hampshire Formatted Data",
1761                                                         JOptionPane.WARNING_MESSAGE );
1762                         }
1763                     }
1764                 }
1765             }
1766         }
1767         activateSaveAllIfNeeded();
1768         System.gc();
1769     }
1770
1771     public void readSeqsFromFile() {
1772         // Set an initial directory if none set yet
1773         final File my_dir = getCurrentDir();
1774         _seqs_filechooser.setMultiSelectionEnabled( false );
1775         // Open file-open dialog and set current directory
1776         if ( my_dir != null ) {
1777             _seqs_filechooser.setCurrentDirectory( my_dir );
1778         }
1779         final int result = _seqs_filechooser.showOpenDialog( _contentpane );
1780         // All done: get the seqs
1781         final File file = _seqs_filechooser.getSelectedFile();
1782         setCurrentDir( _seqs_filechooser.getCurrentDirectory() );
1783         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
1784             setSeqsFile( null );
1785             setSeqs( null );
1786             List<Sequence> seqs = null;
1787             try {
1788                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
1789                     seqs = FastaParser.parse( new FileInputStream( file ) );
1790                     for( final Sequence seq : seqs ) {
1791                         System.out.println( SequenceWriter.toFasta( seq, 60 ) );
1792                     }
1793                 }
1794                 else {
1795                     //TODO error
1796                 }
1797             }
1798             catch ( final MsaFormatException e ) {
1799                 try {
1800                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1801                 }
1802                 catch ( final Exception ex ) {
1803                     // Do nothing.
1804                 }
1805                 JOptionPane.showMessageDialog( this,
1806                                                e.getLocalizedMessage(),
1807                                                "Multiple sequence file format error",
1808                                                JOptionPane.ERROR_MESSAGE );
1809                 return;
1810             }
1811             catch ( final IOException e ) {
1812                 try {
1813                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1814                 }
1815                 catch ( final Exception ex ) {
1816                     // Do nothing.
1817                 }
1818                 JOptionPane.showMessageDialog( this,
1819                                                e.getLocalizedMessage(),
1820                                                "Failed to read multiple sequence file",
1821                                                JOptionPane.ERROR_MESSAGE );
1822                 return;
1823             }
1824             catch ( final IllegalArgumentException e ) {
1825                 try {
1826                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1827                 }
1828                 catch ( final Exception ex ) {
1829                     // Do nothing.
1830                 }
1831                 JOptionPane.showMessageDialog( this,
1832                                                e.getLocalizedMessage(),
1833                                                "Unexpected error during reading of multiple sequence file",
1834                                                JOptionPane.ERROR_MESSAGE );
1835                 return;
1836             }
1837             catch ( final Exception e ) {
1838                 try {
1839                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1840                 }
1841                 catch ( final Exception ex ) {
1842                     // Do nothing.
1843                 }
1844                 e.printStackTrace();
1845                 JOptionPane.showMessageDialog( this,
1846                                                e.getLocalizedMessage(),
1847                                                "Unexpected error during reading of multiple sequence file",
1848                                                JOptionPane.ERROR_MESSAGE );
1849                 return;
1850             }
1851             if ( ( seqs == null ) || ( seqs.size() < 1 ) ) {
1852                 JOptionPane.showMessageDialog( this,
1853                                                "Multiple sequence file is empty",
1854                                                "Illegal multiple sequence file",
1855                                                JOptionPane.ERROR_MESSAGE );
1856                 return;
1857             }
1858             if ( seqs.size() < 4 ) {
1859                 JOptionPane.showMessageDialog( this,
1860                                                "Multiple sequence file needs to contain at least 3 sequences",
1861                                                "Illegal multiple sequence file",
1862                                                JOptionPane.ERROR_MESSAGE );
1863                 return;
1864             }
1865             //  if ( msa.getLength() < 2 ) {
1866             //       JOptionPane.showMessageDialog( this,
1867             //                                      "Multiple sequence alignment needs to contain at least 2 residues",
1868             //                                      "Illegal multiple sequence file",
1869             //                                      JOptionPane.ERROR_MESSAGE );
1870             //       return;
1871             //   }
1872             System.gc();
1873             setSeqsFile( _seqs_filechooser.getSelectedFile() );
1874             setSeqs( seqs );
1875         }
1876     }
1877
1878     public void readMsaFromFile() {
1879         // Set an initial directory if none set yet
1880         final File my_dir = getCurrentDir();
1881         _msa_filechooser.setMultiSelectionEnabled( false );
1882         // Open file-open dialog and set current directory
1883         if ( my_dir != null ) {
1884             _msa_filechooser.setCurrentDirectory( my_dir );
1885         }
1886         final int result = _msa_filechooser.showOpenDialog( _contentpane );
1887         // All done: get the msa
1888         final File file = _msa_filechooser.getSelectedFile();
1889         setCurrentDir( _msa_filechooser.getCurrentDirectory() );
1890         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
1891             setMsaFile( null );
1892             setMsa( null );
1893             Msa msa = null;
1894             try {
1895                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
1896                     msa = FastaParser.parseMsa( new FileInputStream( file ) );
1897                     System.out.println( msa.toString() );
1898                 }
1899                 else {
1900                     msa = GeneralMsaParser.parse( new FileInputStream( file ) );
1901                 }
1902             }
1903             catch ( final MsaFormatException e ) {
1904                 try {
1905                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1906                 }
1907                 catch ( final Exception ex ) {
1908                     // Do nothing.
1909                 }
1910                 JOptionPane.showMessageDialog( this,
1911                                                e.getLocalizedMessage(),
1912                                                "Multiple sequence alignment format error",
1913                                                JOptionPane.ERROR_MESSAGE );
1914                 return;
1915             }
1916             catch ( final IOException e ) {
1917                 try {
1918                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1919                 }
1920                 catch ( final Exception ex ) {
1921                     // Do nothing.
1922                 }
1923                 JOptionPane.showMessageDialog( this,
1924                                                e.getLocalizedMessage(),
1925                                                "Failed to read multiple sequence alignment",
1926                                                JOptionPane.ERROR_MESSAGE );
1927                 return;
1928             }
1929             catch ( final IllegalArgumentException e ) {
1930                 try {
1931                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1932                 }
1933                 catch ( final Exception ex ) {
1934                     // Do nothing.
1935                 }
1936                 JOptionPane.showMessageDialog( this,
1937                                                e.getLocalizedMessage(),
1938                                                "Unexpected error during reading of multiple sequence alignment",
1939                                                JOptionPane.ERROR_MESSAGE );
1940                 return;
1941             }
1942             catch ( final Exception e ) {
1943                 try {
1944                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1945                 }
1946                 catch ( final Exception ex ) {
1947                     // Do nothing.
1948                 }
1949                 e.printStackTrace();
1950                 JOptionPane.showMessageDialog( this,
1951                                                e.getLocalizedMessage(),
1952                                                "Unexpected error during reading of multiple sequence alignment",
1953                                                JOptionPane.ERROR_MESSAGE );
1954                 return;
1955             }
1956             if ( ( msa == null ) || ( msa.getNumberOfSequences() < 1 ) ) {
1957                 JOptionPane.showMessageDialog( this,
1958                                                "Multiple sequence alignment is empty",
1959                                                "Illegal Multiple Sequence Alignment",
1960                                                JOptionPane.ERROR_MESSAGE );
1961                 return;
1962             }
1963             if ( msa.getNumberOfSequences() < 4 ) {
1964                 JOptionPane.showMessageDialog( this,
1965                                                "Multiple sequence alignment needs to contain at least 3 sequences",
1966                                                "Illegal multiple sequence alignment",
1967                                                JOptionPane.ERROR_MESSAGE );
1968                 return;
1969             }
1970             if ( msa.getLength() < 2 ) {
1971                 JOptionPane.showMessageDialog( this,
1972                                                "Multiple sequence alignment needs to contain at least 2 residues",
1973                                                "Illegal multiple sequence alignment",
1974                                                JOptionPane.ERROR_MESSAGE );
1975                 return;
1976             }
1977             System.gc();
1978             setMsaFile( _msa_filechooser.getSelectedFile() );
1979             setMsa( msa );
1980         }
1981     }
1982
1983     @Override
1984     void readPhylogeniesFromURL() {
1985         URL url = null;
1986         Phylogeny[] phys = null;
1987         final String message = "Please enter a complete URL, for example \"http://www.phyloxml.org/examples/apaf.xml\"";
1988         final String url_string = JOptionPane.showInputDialog( this,
1989                                                                message,
1990                                                                "Use URL/webservice to obtain a phylogeny",
1991                                                                JOptionPane.QUESTION_MESSAGE );
1992         boolean nhx_or_nexus = false;
1993         if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
1994             try {
1995                 url = new URL( url_string );
1996                 PhylogenyParser parser = null;
1997                 if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
1998                     parser = new TolParser();
1999                 }
2000                 else {
2001                     parser = ForesterUtil.createParserDependingOnUrlContents( url, getConfiguration()
2002                             .isValidatePhyloXmlAgainstSchema() );
2003                 }
2004                 if ( parser instanceof NexusPhylogeniesParser ) {
2005                     nhx_or_nexus = true;
2006                 }
2007                 else if ( parser instanceof NHXParser ) {
2008                     nhx_or_nexus = true;
2009                 }
2010                 if ( _mainpanel.getCurrentTreePanel() != null ) {
2011                     _mainpanel.getCurrentTreePanel().setWaitCursor();
2012                 }
2013                 else {
2014                     _mainpanel.setWaitCursor();
2015                 }
2016                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
2017                 phys = factory.create( url.openStream(), parser );
2018             }
2019             catch ( final MalformedURLException e ) {
2020                 JOptionPane.showMessageDialog( this,
2021                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
2022                                                "Malformed URL",
2023                                                JOptionPane.ERROR_MESSAGE );
2024             }
2025             catch ( final IOException e ) {
2026                 JOptionPane.showMessageDialog( this,
2027                                                "Could not read from " + url + "\n"
2028                                                        + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2029                                                "Failed to read URL",
2030                                                JOptionPane.ERROR_MESSAGE );
2031             }
2032             catch ( final Exception e ) {
2033                 JOptionPane.showMessageDialog( this,
2034                                                ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2035                                                "Unexpected Exception",
2036                                                JOptionPane.ERROR_MESSAGE );
2037             }
2038             finally {
2039                 if ( _mainpanel.getCurrentTreePanel() != null ) {
2040                     _mainpanel.getCurrentTreePanel().setArrowCursor();
2041                 }
2042                 else {
2043                     _mainpanel.setArrowCursor();
2044                 }
2045             }
2046             if ( ( phys != null ) && ( phys.length > 0 ) ) {
2047                 if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
2048                     for( final Phylogeny phy : phys ) {
2049                         ForesterUtil.transferInternalNodeNamesToConfidence( phy );
2050                     }
2051                 }
2052                 Util.addPhylogeniesToTabs( phys,
2053                                            new File( url.getFile() ).getName(),
2054                                            new File( url.getFile() ).toString(),
2055                                            getConfiguration(),
2056                                            getMainPanel() );
2057                 _mainpanel.getControlPanel().showWhole();
2058             }
2059         }
2060         activateSaveAllIfNeeded();
2061         System.gc();
2062     }
2063
2064     private void readSpeciesTreeFromFile() {
2065         Phylogeny t = null;
2066         boolean exception = false;
2067         final File my_dir = getCurrentDir();
2068         _open_filechooser_for_species_tree.setSelectedFile( new File( "" ) );
2069         if ( my_dir != null ) {
2070             _open_filechooser_for_species_tree.setCurrentDirectory( my_dir );
2071         }
2072         final int result = _open_filechooser_for_species_tree.showOpenDialog( _contentpane );
2073         final File file = _open_filechooser_for_species_tree.getSelectedFile();
2074         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2075             if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.xmlfilter ) {
2076                 try {
2077                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new PhyloXmlParser(), file );
2078                     t = trees[ 0 ];
2079                 }
2080                 catch ( final Exception e ) {
2081                     exception = true;
2082                     exceptionOccuredDuringOpenFile( e );
2083                 }
2084             }
2085             else if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.tolfilter ) {
2086                 try {
2087                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new TolParser(), file );
2088                     t = trees[ 0 ];
2089                 }
2090                 catch ( final Exception e ) {
2091                     exception = true;
2092                     exceptionOccuredDuringOpenFile( e );
2093                 }
2094             }
2095             // "*.*":
2096             else {
2097                 try {
2098                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new PhyloXmlParser(), file );
2099                     t = trees[ 0 ];
2100                 }
2101                 catch ( final Exception e ) {
2102                     exception = true;
2103                     exceptionOccuredDuringOpenFile( e );
2104                 }
2105             }
2106             if ( !exception && ( t != null ) && !t.isRooted() ) {
2107                 exception = true;
2108                 t = null;
2109                 JOptionPane.showMessageDialog( this,
2110                                                "Species tree is not rooted",
2111                                                "Species tree not loaded",
2112                                                JOptionPane.ERROR_MESSAGE );
2113             }
2114             if ( !exception && ( t != null ) ) {
2115                 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
2116                 for( final PhylogenyNodeIterator it = t.iteratorExternalForward(); it.hasNext(); ) {
2117                     final PhylogenyNode node = it.next();
2118                     if ( !node.getNodeData().isHasTaxonomy() ) {
2119                         exception = true;
2120                         t = null;
2121                         JOptionPane
2122                                 .showMessageDialog( this,
2123                                                     "Species tree contains external node(s) without taxonomy information",
2124                                                     "Species tree not loaded",
2125                                                     JOptionPane.ERROR_MESSAGE );
2126                         break;
2127                     }
2128                     else {
2129                         if ( tax_set.contains( node.getNodeData().getTaxonomy() ) ) {
2130                             exception = true;
2131                             t = null;
2132                             JOptionPane.showMessageDialog( this,
2133                                                            "Taxonomy ["
2134                                                                    + node.getNodeData().getTaxonomy().asSimpleText()
2135                                                                    + "] is not unique in species tree",
2136                                                            "Species tree not loaded",
2137                                                            JOptionPane.ERROR_MESSAGE );
2138                             break;
2139                         }
2140                         else {
2141                             tax_set.add( node.getNodeData().getTaxonomy() );
2142                         }
2143                     }
2144                 }
2145             }
2146             if ( !exception && ( t != null ) ) {
2147                 _species_tree = t;
2148                 JOptionPane.showMessageDialog( this,
2149                                                "Species tree successfully loaded",
2150                                                "Species tree loaded",
2151                                                JOptionPane.INFORMATION_MESSAGE );
2152             }
2153             _contentpane.repaint();
2154             System.gc();
2155         }
2156     }
2157
2158     private void setCurrentDir( final File current_dir ) {
2159         _current_dir = current_dir;
2160     }
2161
2162     private void setMinNotCollapseConfidenceValue( final double min_not_collapse ) {
2163         _min_not_collapse = min_not_collapse;
2164     }
2165
2166     private void setSpecialOptionsForNexParser( final NexusPhylogeniesParser nex ) {
2167         nex.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
2168     }
2169
2170     private void setSpecialOptionsForNhxParser( final NHXParser nhx ) {
2171         nhx.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
2172         ForesterUtil.TAXONOMY_EXTRACTION te = ForesterUtil.TAXONOMY_EXTRACTION.NO;
2173         if ( getOptions().isExtractPfamTaxonomyCodesInNhParsing() ) {
2174             te = ForesterUtil.TAXONOMY_EXTRACTION.YES;
2175         }
2176         nhx.setTaxonomyExtraction( te );
2177     }
2178
2179     private void writeAllToFile() {
2180         if ( ( getMainPanel().getTabbedPane() == null ) || ( getMainPanel().getTabbedPane().getTabCount() < 1 ) ) {
2181             return;
2182         }
2183         final File my_dir = getCurrentDir();
2184         if ( my_dir != null ) {
2185             _save_filechooser.setCurrentDirectory( my_dir );
2186         }
2187         _save_filechooser.setSelectedFile( new File( "" ) );
2188         final int result = _save_filechooser.showSaveDialog( _contentpane );
2189         final File file = _save_filechooser.getSelectedFile();
2190         setCurrentDir( _save_filechooser.getCurrentDirectory() );
2191         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2192             if ( file.exists() ) {
2193                 final int i = JOptionPane.showConfirmDialog( this,
2194                                                              file + " already exists. Overwrite?",
2195                                                              "Warning",
2196                                                              JOptionPane.OK_CANCEL_OPTION,
2197                                                              JOptionPane.WARNING_MESSAGE );
2198                 if ( i != JOptionPane.OK_OPTION ) {
2199                     return;
2200                 }
2201                 else {
2202                     try {
2203                         file.delete();
2204                     }
2205                     catch ( final Exception e ) {
2206                         JOptionPane.showMessageDialog( this,
2207                                                        "Failed to delete: " + file,
2208                                                        "Error",
2209                                                        JOptionPane.WARNING_MESSAGE );
2210                     }
2211                 }
2212             }
2213             final int count = getMainPanel().getTabbedPane().getTabCount();
2214             final List<Phylogeny> trees = new ArrayList<Phylogeny>();
2215             for( int i = 0; i < count; ++i ) {
2216                 trees.add( getMainPanel().getPhylogeny( i ) );
2217                 getMainPanel().getTreePanels().get( i ).setEdited( false );
2218             }
2219             final PhylogenyWriter writer = new PhylogenyWriter();
2220             try {
2221                 writer.toPhyloXML( file, trees, 0, ForesterUtil.LINE_SEPARATOR );
2222             }
2223             catch ( final IOException e ) {
2224                 JOptionPane.showMessageDialog( this,
2225                                                "Failed to write to: " + file,
2226                                                "Error",
2227                                                JOptionPane.WARNING_MESSAGE );
2228             }
2229         }
2230     }
2231
2232     private boolean writeAsNewHampshire( final Phylogeny t, boolean exception, final File file ) {
2233         try {
2234             final PhylogenyWriter writer = new PhylogenyWriter();
2235             writer.toNewHampshire( t, false, true, file );
2236         }
2237         catch ( final Exception e ) {
2238             exception = true;
2239             exceptionOccuredDuringSaveAs( e );
2240         }
2241         return exception;
2242     }
2243
2244     private boolean writeAsNexus( final Phylogeny t, boolean exception, final File file ) {
2245         try {
2246             final PhylogenyWriter writer = new PhylogenyWriter();
2247             writer.toNexus( file, t );
2248         }
2249         catch ( final Exception e ) {
2250             exception = true;
2251             exceptionOccuredDuringSaveAs( e );
2252         }
2253         return exception;
2254     }
2255
2256     private boolean writeAsNHX( final Phylogeny t, boolean exception, final File file ) {
2257         try {
2258             final PhylogenyWriter writer = new PhylogenyWriter();
2259             writer.toNewHampshireX( t, file );
2260         }
2261         catch ( final Exception e ) {
2262             exception = true;
2263             exceptionOccuredDuringSaveAs( e );
2264         }
2265         return exception;
2266     }
2267
2268     private boolean writeAsPhyloXml( final Phylogeny t, boolean exception, final File file ) {
2269         try {
2270             final PhylogenyWriter writer = new PhylogenyWriter();
2271             writer.toPhyloXML( file, t, 0 );
2272         }
2273         catch ( final Exception e ) {
2274             exception = true;
2275             exceptionOccuredDuringSaveAs( e );
2276         }
2277         return exception;
2278     }
2279
2280     private void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) {
2281         _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(),
2282                                                                    _mainpanel.getCurrentTreePanel().getHeight(),
2283                                                                    true );
2284         String file_written_to = "";
2285         boolean error = false;
2286         try {
2287             file_written_to = Util.writePhylogenyToGraphicsFile( file_name,
2288                                                                  _mainpanel.getCurrentTreePanel().getWidth(),
2289                                                                  _mainpanel.getCurrentTreePanel().getHeight(),
2290                                                                  _mainpanel.getCurrentTreePanel(),
2291                                                                  _mainpanel.getControlPanel(),
2292                                                                  type,
2293                                                                  getOptions() );
2294         }
2295         catch ( final IOException e ) {
2296             error = true;
2297             JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
2298         }
2299         if ( !error ) {
2300             if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) {
2301                 JOptionPane.showMessageDialog( this,
2302                                                "Wrote image to: " + file_written_to,
2303                                                "Graphics Export",
2304                                                JOptionPane.INFORMATION_MESSAGE );
2305             }
2306             else {
2307                 JOptionPane.showMessageDialog( this,
2308                                                "There was an unknown problem when attempting to write to an image file: \""
2309                                                        + file_name + "\"",
2310                                                "Error",
2311                                                JOptionPane.ERROR_MESSAGE );
2312             }
2313         }
2314         _contentpane.repaint();
2315     }
2316
2317     private void writeToFile( final Phylogeny t ) {
2318         if ( t == null ) {
2319             return;
2320         }
2321         String initial_filename = null;
2322         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2323             try {
2324                 initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().getCanonicalPath();
2325             }
2326             catch ( final IOException e ) {
2327                 initial_filename = null;
2328             }
2329         }
2330         if ( !ForesterUtil.isEmpty( initial_filename ) ) {
2331             _save_filechooser.setSelectedFile( new File( initial_filename ) );
2332         }
2333         else {
2334             _save_filechooser.setSelectedFile( new File( "" ) );
2335         }
2336         final File my_dir = getCurrentDir();
2337         if ( my_dir != null ) {
2338             _save_filechooser.setCurrentDirectory( my_dir );
2339         }
2340         final int result = _save_filechooser.showSaveDialog( _contentpane );
2341         final File file = _save_filechooser.getSelectedFile();
2342         setCurrentDir( _save_filechooser.getCurrentDirectory() );
2343         boolean exception = false;
2344         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2345             if ( file.exists() ) {
2346                 final int i = JOptionPane.showConfirmDialog( this,
2347                                                              file + " already exists.\nOverwrite?",
2348                                                              "Overwrite?",
2349                                                              JOptionPane.OK_CANCEL_OPTION,
2350                                                              JOptionPane.QUESTION_MESSAGE );
2351                 if ( i != JOptionPane.OK_OPTION ) {
2352                     return;
2353                 }
2354                 else {
2355                     final File to = new File( file.getAbsoluteFile().toString() + Constants.BACKUP_FILE_SUFFIX );
2356                     try {
2357                         ForesterUtil.copyFile( file, to );
2358                     }
2359                     catch ( final Exception e ) {
2360                         JOptionPane.showMessageDialog( this,
2361                                                        "Failed to create backup copy " + to,
2362                                                        "Failed to Create Backup Copy",
2363                                                        JOptionPane.WARNING_MESSAGE );
2364                     }
2365                     try {
2366                         file.delete();
2367                     }
2368                     catch ( final Exception e ) {
2369                         JOptionPane.showMessageDialog( this,
2370                                                        "Failed to delete: " + file,
2371                                                        "Failed to Delete",
2372                                                        JOptionPane.WARNING_MESSAGE );
2373                     }
2374                 }
2375             }
2376             if ( _save_filechooser.getFileFilter() == MainFrameApplication.nhfilter ) {
2377                 exception = writeAsNewHampshire( t, exception, file );
2378             }
2379             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.nhxfilter ) {
2380                 exception = writeAsNHX( t, exception, file );
2381             }
2382             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) {
2383                 exception = writeAsPhyloXml( t, exception, file );
2384             }
2385             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) {
2386                 exception = writeAsNexus( t, exception, file );
2387             }
2388             // "*.*":
2389             else {
2390                 final String file_name = file.getName().trim().toLowerCase();
2391                 if ( file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2392                         || file_name.endsWith( ".tree" ) ) {
2393                     exception = writeAsNewHampshire( t, exception, file );
2394                 }
2395                 else if ( file_name.endsWith( ".nhx" ) ) {
2396                     exception = writeAsNHX( t, exception, file );
2397                 }
2398                 else if ( file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) ) {
2399                     exception = writeAsNexus( t, exception, file );
2400                 }
2401                 // XML is default:
2402                 else {
2403                     exception = writeAsPhyloXml( t, exception, file );
2404                 }
2405             }
2406             if ( !exception ) {
2407                 getMainPanel().setTitleOfSelectedTab( file.getName() );
2408                 getMainPanel().getCurrentTreePanel().setTreeFile( file );
2409                 getMainPanel().getCurrentTreePanel().setEdited( false );
2410             }
2411         }
2412     }
2413
2414     private void writeToGraphicsFile( final Phylogeny t, final GraphicsExportType type ) {
2415         if ( ( t == null ) || t.isEmpty() ) {
2416             return;
2417         }
2418         String initial_filename = "";
2419         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2420             initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString();
2421         }
2422         if ( initial_filename.indexOf( '.' ) > 0 ) {
2423             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2424         }
2425         initial_filename = initial_filename + "." + type;
2426         _writetographics_filechooser.setSelectedFile( new File( initial_filename ) );
2427         final File my_dir = getCurrentDir();
2428         if ( my_dir != null ) {
2429             _writetographics_filechooser.setCurrentDirectory( my_dir );
2430         }
2431         final int result = _writetographics_filechooser.showSaveDialog( _contentpane );
2432         File file = _writetographics_filechooser.getSelectedFile();
2433         setCurrentDir( _writetographics_filechooser.getCurrentDirectory() );
2434         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2435             if ( !file.toString().toLowerCase().endsWith( type.toString() ) ) {
2436                 file = new File( file.toString() + "." + type );
2437             }
2438             if ( file.exists() ) {
2439                 final int i = JOptionPane.showConfirmDialog( this,
2440                                                              file + " already exists. Overwrite?",
2441                                                              "Warning",
2442                                                              JOptionPane.OK_CANCEL_OPTION,
2443                                                              JOptionPane.WARNING_MESSAGE );
2444                 if ( i != JOptionPane.OK_OPTION ) {
2445                     return;
2446                 }
2447                 else {
2448                     try {
2449                         file.delete();
2450                     }
2451                     catch ( final Exception e ) {
2452                         JOptionPane.showMessageDialog( this,
2453                                                        "Failed to delete: " + file,
2454                                                        "Error",
2455                                                        JOptionPane.WARNING_MESSAGE );
2456                     }
2457                 }
2458             }
2459             writePhylogenyToGraphicsFile( file.toString(), type );
2460         }
2461     }
2462
2463     private void writeToPdf( final Phylogeny t ) {
2464         if ( ( t == null ) || t.isEmpty() ) {
2465             return;
2466         }
2467         String initial_filename = "";
2468         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2469             initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString();
2470         }
2471         if ( initial_filename.indexOf( '.' ) > 0 ) {
2472             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2473         }
2474         initial_filename = initial_filename + ".pdf";
2475         _writetopdf_filechooser.setSelectedFile( new File( initial_filename ) );
2476         final File my_dir = getCurrentDir();
2477         if ( my_dir != null ) {
2478             _writetopdf_filechooser.setCurrentDirectory( my_dir );
2479         }
2480         final int result = _writetopdf_filechooser.showSaveDialog( _contentpane );
2481         File file = _writetopdf_filechooser.getSelectedFile();
2482         setCurrentDir( _writetopdf_filechooser.getCurrentDirectory() );
2483         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2484             if ( !file.toString().toLowerCase().endsWith( ".pdf" ) ) {
2485                 file = new File( file.toString() + ".pdf" );
2486             }
2487             if ( file.exists() ) {
2488                 final int i = JOptionPane.showConfirmDialog( this,
2489                                                              file + " already exists. Overwrite?",
2490                                                              "WARNING",
2491                                                              JOptionPane.OK_CANCEL_OPTION,
2492                                                              JOptionPane.WARNING_MESSAGE );
2493                 if ( i != JOptionPane.OK_OPTION ) {
2494                     return;
2495                 }
2496             }
2497             printPhylogenyToPdf( file.toString() );
2498         }
2499     }
2500
2501     static MainFrame createInstance( final Phylogeny[] phys, final Configuration config, final String title ) {
2502         return new MainFrameApplication( phys, config, title );
2503     }
2504
2505     static MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) {
2506         return new MainFrameApplication( phys, config_file_name, title );
2507     }
2508
2509     static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) {
2510         mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", "
2511                 + o.getPrintSizeY() + ")" );
2512     }
2513
2514     static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) {
2515         mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" );
2516     }
2517
2518     static void warnIfNotPhyloXmlValidation( final Configuration c ) {
2519         if ( !c.isValidatePhyloXmlAgainstSchema() ) {
2520             JOptionPane
2521                     .showMessageDialog( null,
2522                                         ForesterUtil
2523                                                 .wordWrap( "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]",
2524                                                            80 ),
2525                                         "Warning",
2526                                         JOptionPane.WARNING_MESSAGE );
2527         }
2528     }
2529
2530     private void setPhylogeneticInferenceOptions( final PhylogeneticInferenceOptions phylogenetic_inference_options ) {
2531         _phylogenetic_inference_options = phylogenetic_inference_options;
2532     }
2533
2534     private PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {
2535         if ( _phylogenetic_inference_options == null ) {
2536             _phylogenetic_inference_options = new PhylogeneticInferenceOptions();
2537         }
2538         return _phylogenetic_inference_options;
2539     }
2540
2541     public Msa getMsa() {
2542         return _msa;
2543     }
2544
2545     void setMsa( final Msa msa ) {
2546         _msa = msa;
2547     }
2548
2549     void setMsaFile( final File msa_file ) {
2550         _msa_file = msa_file;
2551     }
2552
2553     public File getMsaFile() {
2554         return _msa_file;
2555     }
2556
2557     public List<Sequence> getSeqs() {
2558         return _seqs;
2559     }
2560
2561     void setSeqs( final List<Sequence> seqs ) {
2562         _seqs = seqs;
2563     }
2564
2565     void setSeqsFile( final File seqs_file ) {
2566         _seqs_file = seqs_file;
2567     }
2568
2569     public File getSeqsFile() {
2570         return _seqs_file;
2571     }
2572 } // MainFrameApplication.
2573
2574 class NexusFilter extends FileFilter {
2575
2576     @Override
2577     public boolean accept( final File f ) {
2578         final String file_name = f.getName().trim().toLowerCase();
2579         return file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) || file_name.endsWith( ".nx" )
2580                 || file_name.endsWith( ".tre" ) || f.isDirectory();
2581     }
2582
2583     @Override
2584     public String getDescription() {
2585         return "Nexus files (*.nex, *.nexus, *.nx, *.tre)";
2586     }
2587 } // NexusFilter
2588
2589 class NHFilter extends FileFilter {
2590
2591     @Override
2592     public boolean accept( final File f ) {
2593         final String file_name = f.getName().trim().toLowerCase();
2594         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2595                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".tree" ) || file_name.endsWith( ".dnd" )
2596                 || file_name.endsWith( ".ph" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".nwk" )
2597                 || f.isDirectory();
2598     }
2599
2600     @Override
2601     public String getDescription() {
2602         return "New Hampshire - Newick files (*.nh, *.newick, *.phy, *.tree, *.dnd, *.tr, *.ph, *.phb, *.nwk)";
2603     }
2604 } // NHFilter
2605
2606 class NHXFilter extends FileFilter {
2607
2608     @Override
2609     public boolean accept( final File f ) {
2610         final String file_name = f.getName().trim().toLowerCase();
2611         return file_name.endsWith( ".nhx" ) || f.isDirectory();
2612     }
2613
2614     @Override
2615     public String getDescription() {
2616         return "NHX files (*.nhx)";
2617     }
2618 }
2619
2620 class PdfFilter extends FileFilter {
2621
2622     @Override
2623     public boolean accept( final File f ) {
2624         return f.getName().trim().toLowerCase().endsWith( ".pdf" ) || f.isDirectory();
2625     }
2626
2627     @Override
2628     public String getDescription() {
2629         return "PDF files (*.pdf)";
2630     }
2631 } // PdfFilter
2632
2633 class TolFilter extends FileFilter {
2634
2635     @Override
2636     public boolean accept( final File f ) {
2637         final String file_name = f.getName().trim().toLowerCase();
2638         return ( file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) || file_name.endsWith( ".zip" ) || f
2639                 .isDirectory() ) && ( !file_name.endsWith( ".xml.zip" ) );
2640     }
2641
2642     @Override
2643     public String getDescription() {
2644         return "Tree of Life files (*.tol, *.tolxml)";
2645     }
2646 } // TolFilter
2647
2648 class XMLFilter extends FileFilter {
2649
2650     @Override
2651     public boolean accept( final File f ) {
2652         final String file_name = f.getName().trim().toLowerCase();
2653         return file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) || file_name.endsWith( "phylo.xml" )
2654                 || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".zip" ) || f.isDirectory();
2655     }
2656
2657     @Override
2658     public String getDescription() {
2659         return "phyloXML files (*.xml, *.phyloxml, *phylo.xml, *.pxml, *.zip)";
2660     }
2661 } // XMLFilter