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