rio, lca, refactoring
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplication.java
index 51d1129..2c674fe 100644 (file)
@@ -36,6 +36,7 @@ import java.awt.event.WindowEvent;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -59,7 +60,7 @@ import javax.swing.event.ChangeListener;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.plaf.synth.SynthLookAndFeel;
 
-import org.forester.analysis.TaxonomyDataObtainer;
+import org.forester.analysis.TaxonomyDataManager;
 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
@@ -78,6 +79,7 @@ import org.forester.io.parsers.GeneralMsaParser;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
 import org.forester.io.parsers.tol.TolParser;
@@ -96,9 +98,7 @@ import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.sdi.GSDI;
-import org.forester.sdi.SDI;
 import org.forester.sdi.SDIR;
-import org.forester.sdi.SDIse;
 import org.forester.sequence.Sequence;
 import org.forester.util.BasicDescriptiveStatistics;
 import org.forester.util.BasicTable;
@@ -202,7 +202,6 @@ public final class MainFrameApplication extends MainFrame {
     // Analysis menu
     private JMenu                            _analysis_menu;
     private JMenuItem                        _load_species_tree_item;
-    private JMenuItem                        _sdi_item;
     private JMenuItem                        _gsdi_item;
     private JMenuItem                        _root_min_dups_item;
     private JMenuItem                        _root_min_cost_l_item;
@@ -294,8 +293,9 @@ public final class MainFrameApplication extends MainFrame {
         // hide until everything is ready
         setVisible( false );
         setOptions( Options.createInstance( _configuration ) );
+        setInferenceManager( InferenceManager.createInstance( _configuration ) );
         setPhylogeneticInferenceOptions( PhylogeneticInferenceOptions.createInstance( _configuration ) );
-        _textframe = null;
+        //     _textframe = null; #~~~~
         _species_tree = null;
         // set title
         setTitle( Constants.PRG_NAME + " " + Constants.VERSION + " (" + Constants.PRG_DATE + ")" );
@@ -530,12 +530,6 @@ public final class MainFrameApplication extends MainFrame {
             else if ( o == _load_species_tree_item ) {
                 readSpeciesTreeFromFile();
             }
-            else if ( o == _sdi_item ) {
-                if ( isSubtreeDisplayed() ) {
-                    return;
-                }
-                executeSDI();
-            }
             else if ( o == _lineage_inference ) {
                 if ( isSubtreeDisplayed() ) {
                     JOptionPane.showMessageDialog( this,
@@ -657,16 +651,12 @@ public final class MainFrameApplication extends MainFrame {
 
     void buildAnalysisMenu() {
         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
-        _analysis_menu.add( _sdi_item = new JMenuItem( "SDI (Speciation Duplication Inference)" ) );
-        if ( !Constants.__RELEASE && !Constants.__SNAPSHOT_RELEASE ) {
-            _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
-        }
+        _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
         _analysis_menu.addSeparator();
         _analysis_menu.add( _root_min_dups_item = new JMenuItem( "Root by Minimizing Duplications | Height (SDI)" ) );
         _analysis_menu.add( _root_min_cost_l_item = new JMenuItem( "Root by Minimizing Cost L | Height (SDI)" ) );
         _analysis_menu.addSeparator();
         _analysis_menu.add( _load_species_tree_item = new JMenuItem( "Load Species Tree..." ) );
-        customizeJMenuItem( _sdi_item );
         customizeJMenuItem( _gsdi_item );
         customizeJMenuItem( _root_min_dups_item );
         customizeJMenuItem( _root_min_cost_l_item );
@@ -679,14 +669,23 @@ public final class MainFrameApplication extends MainFrame {
     }
 
     void buildPhylogeneticInferenceMenu() {
-        final InferenceManager inference_manager = InferenceManager.getInstance();
+        final InferenceManager im = getInferenceManager();
         _inference_menu = MainFrame.createMenu( "Inference", getConfiguration() );
         _inference_menu.add( _inference_from_msa_item = new JMenuItem( "From Multiple Sequence Alignment..." ) );
         customizeJMenuItem( _inference_from_msa_item );
         _inference_from_msa_item.setToolTipText( "Basic phylogenetic inference from MSA" );
-        _inference_menu.add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences..." ) );
-        customizeJMenuItem( _inference_from_seqs_item );
-        _inference_from_seqs_item.setToolTipText( "Basic phylogenetic inference including multiple sequence alignment" );
+        if ( im.canDoMsa() ) {
+            _inference_menu.add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences..." ) );
+            customizeJMenuItem( _inference_from_seqs_item );
+            _inference_from_seqs_item
+                    .setToolTipText( "Basic phylogenetic inference including multiple sequence alignment" );
+        }
+        else {
+            _inference_menu
+                    .add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences (no program found)" ) );
+            customizeJMenuItem( _inference_from_seqs_item );
+            _inference_from_seqs_item.setEnabled( false );
+        }
         _jmenubar.add( _inference_menu );
     }
 
@@ -953,10 +952,9 @@ public final class MainFrameApplication extends MainFrame {
         customizeJMenuItem( _obtain_detailed_taxonomic_information_deleting_jmi );
         _obtain_detailed_taxonomic_information_deleting_jmi
                 .setToolTipText( "To add additional taxonomic information, deletes nodes for which taxonomy cannot found (from UniProt Taxonomy)" );
-        _tools_menu
-                .add( _obtain_uniprot_seq_information_jmi = new JMenuItem( "Obtain Sequence Information (from UniProt)" ) );
+        _tools_menu.add( _obtain_uniprot_seq_information_jmi = new JMenuItem( "Obtain Sequence Information" ) );
         customizeJMenuItem( _obtain_uniprot_seq_information_jmi );
-        _obtain_uniprot_seq_information_jmi.setToolTipText( "To add additional sequence information (from UniProt)" );
+        _obtain_uniprot_seq_information_jmi.setToolTipText( "To add additional sequence information" );
         _tools_menu.addSeparator();
         if ( !Constants.__RELEASE ) {
             _tools_menu.add( _function_analysis = new JMenuItem( "Add UniProtKB Annotations" ) );
@@ -1111,7 +1109,7 @@ public final class MainFrameApplication extends MainFrame {
             }
             if ( to_be_removed.size() > 0 ) {
                 phy.externalNodesHaveChanged();
-                phy.hashIDs();
+                phy.clearHashIdToNodeMap();
                 phy.recalculateNumberOfExternalDescendants( true );
                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
                 getCurrentTreePanel().updateSetOfCollapsedExternalNodes();
@@ -1210,23 +1208,29 @@ public final class MainFrameApplication extends MainFrame {
         gene_tree.setAllNodesToNotCollapse();
         gene_tree.recalculateNumberOfExternalDescendants( false );
         GSDI gsdi = null;
-        int duplications = -1;
         try {
-            gsdi = new GSDI( gene_tree, _species_tree.copy(), true );
-            duplications = gsdi.getDuplicationsSum();
+            gsdi = new GSDI( gene_tree, _species_tree.copy(), false, true, true );
         }
         catch ( final Exception e ) {
             JOptionPane.showMessageDialog( this, e.toString(), "Error during GSDI", JOptionPane.ERROR_MESSAGE );
         }
         gene_tree.setRerootable( false );
         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
+        _mainpanel.getCurrentPhylogeny().clearHashIdToNodeMap();
+        _mainpanel.getCurrentPhylogeny().recalculateNumberOfExternalDescendants( true );
+        _mainpanel.getCurrentTreePanel().resetNodeIdToDistToLeafMap();
+        _mainpanel.getCurrentTreePanel().setEdited( true );
         getControlPanel().setShowEvents( true );
         showWhole();
+        final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
+        _mainpanel.addPhylogenyInNewTab( gsdi.getSpeciesTree(), getConfiguration(), "species tree", null );
+        showWhole();
+        _mainpanel.getTabbedPane().setSelectedIndex( selected );
+        showWhole();
         _mainpanel.getCurrentTreePanel().setEdited( true );
-        JOptionPane.showMessageDialog( this,
-                                       "Number of duplications: " + duplications,
-                                       "GSDI successfully completed",
-                                       JOptionPane.INFORMATION_MESSAGE );
+        JOptionPane.showMessageDialog( this, "Duplications: " + gsdi.getDuplicationsSum() + "\n"
+                + "Potential duplications: " + gsdi.getSpeciationOrDuplicationEventsSum() + "\n" + "Speciations: "
+                + gsdi.getSpeciationsSum(), "GSDI successfully completed", JOptionPane.INFORMATION_MESSAGE );
     }
 
     void executeFunctionAnalysis() {
@@ -1294,40 +1298,6 @@ public final class MainFrameApplication extends MainFrame {
         }
     }
 
-    void executeSDI() {
-        if ( !isOKforSDI( true, true ) ) {
-            return;
-        }
-        if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
-            JOptionPane.showMessageDialog( this,
-                                           "Gene tree is not rooted",
-                                           "Cannot execute SDI",
-                                           JOptionPane.ERROR_MESSAGE );
-            return;
-        }
-        final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
-        gene_tree.setAllNodesToNotCollapse();
-        gene_tree.recalculateNumberOfExternalDescendants( false );
-        SDI sdi = null;
-        int duplications = -1;
-        try {
-            sdi = new SDIse( gene_tree, _species_tree.copy() );
-            duplications = sdi.getDuplicationsSum();
-        }
-        catch ( final Exception e ) {
-            JOptionPane.showMessageDialog( this, e.toString(), "Error during SDI", JOptionPane.ERROR_MESSAGE );
-        }
-        gene_tree.setRerootable( false );
-        _mainpanel.getCurrentTreePanel().setTree( gene_tree );
-        getControlPanel().setShowEvents( true );
-        showWhole();
-        _mainpanel.getCurrentTreePanel().setEdited( true );
-        JOptionPane.showMessageDialog( this,
-                                       "Number of duplications: " + duplications,
-                                       "SDI successfully completed",
-                                       JOptionPane.INFORMATION_MESSAGE );
-    }
-
     void executeSDIR( final boolean minimize_cost ) {
         if ( !isOKforSDI( true, true ) ) {
             return;
@@ -1360,7 +1330,7 @@ public final class MainFrameApplication extends MainFrame {
     }
 
     void exit() {
-        removeTextFrame();
+        removeAllTextFrames();
         _mainpanel.terminate();
         _contentpane.removeAll();
         setVisible( false );
@@ -1375,7 +1345,7 @@ public final class MainFrameApplication extends MainFrame {
         dispose();
     }
 
-    private void extractTaxCodeFromNodeNames() {
+    private void extractTaxCodeFromNodeNames() throws PhyloXmlDataFormatException {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
             if ( ( phy != null ) && !phy.isEmpty() ) {
@@ -1385,7 +1355,7 @@ public final class MainFrameApplication extends MainFrame {
                     final String name = n.getName().trim();
                     if ( !ForesterUtil.isEmpty( name ) ) {
                         final String code = ParserUtils
-                                .extractTaxonomyCodeFromNodeName( name, false, PhylogenyMethods.TAXONOMY_EXTRACTION.YES );
+                                .extractTaxonomyCodeFromNodeName( name, PhylogenyMethods.TAXONOMY_EXTRACTION.YES );
                         if ( !ForesterUtil.isEmpty( code ) ) {
                             PhylogenyMethods.setTaxonomyCode( n, code );
                         }
@@ -1401,7 +1371,7 @@ public final class MainFrameApplication extends MainFrame {
 
     private File getCurrentDir() {
         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
-            if ( ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1 ) {
+            if ( ForesterUtil.isWindowns() ) {
                 try {
                     _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() );
                 }
@@ -1470,21 +1440,23 @@ public final class MainFrameApplication extends MainFrame {
         return false;
     }
 
-    private void moveNodeNamesToSeqNames() {
+    private void moveNodeNamesToSeqNames() throws PhyloXmlDataFormatException {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
             if ( ( phy != null ) && !phy.isEmpty() ) {
-                PhylogenyMethods.transferNodeNameToField( phy, PhylogenyMethods.PhylogenyNodeField.SEQUENCE_NAME );
+                PhylogenyMethods
+                        .transferNodeNameToField( phy, PhylogenyMethods.PhylogenyNodeField.SEQUENCE_NAME, false );
             }
         }
     }
 
-    private void moveNodeNamesToTaxSn() {
+    private void moveNodeNamesToTaxSn() throws PhyloXmlDataFormatException {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
             if ( ( phy != null ) && !phy.isEmpty() ) {
                 PhylogenyMethods.transferNodeNameToField( phy,
-                                                          PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME );
+                                                          PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
+                                                          false );
             }
         }
     }
@@ -1516,9 +1488,11 @@ public final class MainFrameApplication extends MainFrame {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
             if ( ( phy != null ) && !phy.isEmpty() ) {
-                final TaxonomyDataObtainer t = new TaxonomyDataObtainer( this,
-                                                                         _mainpanel.getCurrentTreePanel(),
-                                                                         phy.copy() );
+                final TaxonomyDataManager t = new TaxonomyDataManager( this,
+                                                                       _mainpanel.getCurrentTreePanel(),
+                                                                       phy.copy(),
+                                                                       false,
+                                                                       true );
                 new Thread( t ).start();
             }
         }
@@ -1528,10 +1502,11 @@ public final class MainFrameApplication extends MainFrame {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
             if ( ( phy != null ) && !phy.isEmpty() ) {
-                final TaxonomyDataObtainer t = new TaxonomyDataObtainer( this,
-                                                                         _mainpanel.getCurrentTreePanel(),
-                                                                         phy.copy(),
-                                                                         true );
+                final TaxonomyDataManager t = new TaxonomyDataManager( this,
+                                                                       _mainpanel.getCurrentTreePanel(),
+                                                                       phy.copy(),
+                                                                       true,
+                                                                       true );
                 new Thread( t ).start();
             }
         }
@@ -2002,12 +1977,12 @@ public final class MainFrameApplication extends MainFrame {
             setMsa( null );
             Msa msa = null;
             try {
-                if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
-                    msa = FastaParser.parseMsa( new FileInputStream( file ) );
-                    System.out.println( msa.toString() );
+                final InputStream is = new FileInputStream( file );
+                if ( FastaParser.isLikelyFasta( file ) ) {
+                    msa = FastaParser.parseMsa( is );
                 }
                 else {
-                    msa = GeneralMsaParser.parse( new FileInputStream( file ) );
+                    msa = GeneralMsaParser.parse( is );
                 }
             }
             catch ( final MsaFormatException e ) {