special domain coloring
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplication.java
index b688722..125471d 100644 (file)
@@ -93,11 +93,13 @@ import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;\r
 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;\r
 import org.forester.phylogeny.data.Confidence;\r
+import org.forester.phylogeny.data.PhylogenyDataUtil;\r
+import org.forester.phylogeny.data.Sequence;\r
 import org.forester.phylogeny.data.Taxonomy;\r
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;\r
 import org.forester.phylogeny.factories.PhylogenyFactory;\r
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;\r
-import org.forester.sequence.Sequence;\r
+import org.forester.sequence.MolecularSequence;\r
 import org.forester.util.BasicDescriptiveStatistics;\r
 import org.forester.util.BasicTable;\r
 import org.forester.util.BasicTableParser;\r
@@ -123,6 +125,7 @@ public final class MainFrameApplication extends MainFrame {
     private final static SequencesFileFilter seqsfilter                            = new SequencesFileFilter();\r
     private final static DefaultFilter       defaultfilter                         = new DefaultFilter();\r
     private static final long                serialVersionUID                      = -799735726778865234L;\r
+    private static final boolean             PREPROCESS_TREES                      = false;\r
     private final JFileChooser               _values_filechooser;\r
     private final JFileChooser               _sequences_filechooser;\r
     private final JFileChooser               _open_filechooser;\r
@@ -140,11 +143,14 @@ public final class MainFrameApplication extends MainFrame {
     private JMenuItem                        _write_to_tif_item;\r
     private JMenuItem                        _write_to_png_item;\r
     private JMenuItem                        _write_to_bmp_item;\r
+    private JMenuItem                        _collapse_below_threshold;\r
+    private JMenuItem                        _collapse_below_branch_length;\r
     private File                             _current_dir;\r
     private ButtonGroup                      _radio_group_1;\r
     private ButtonGroup                      _radio_group_2;\r
     // Others:\r
     double                                   _min_not_collapse                     = Constants.MIN_NOT_COLLAPSE_DEFAULT;\r
+    double                                   _min_not_collapse_bl                  = 0.001;\r
     // Phylogeny Inference menu\r
     private JMenu                            _inference_menu;\r
     private JMenuItem                        _inference_from_msa_item;\r
@@ -153,7 +159,7 @@ public final class MainFrameApplication extends MainFrame {
     private PhylogeneticInferenceOptions     _phylogenetic_inference_options       = null;\r
     private Msa                              _msa                                  = null;\r
     private File                             _msa_file                             = null;\r
-    private List<Sequence>                   _seqs                                 = null;\r
+    private List<MolecularSequence>          _seqs                                 = null;\r
     private File                             _seqs_file                            = null;\r
     JMenuItem                                _read_values_jmi;\r
     JMenuItem                                _read_seqs_jmi;\r
@@ -543,6 +549,12 @@ public final class MainFrameApplication extends MainFrame {
                 }\r
                 collapseBelowThreshold();\r
             }\r
+            else if ( o == _collapse_below_branch_length ) {\r
+                if ( isSubtreeDisplayed() ) {\r
+                    return;\r
+                }\r
+                collapseBelowBranchLengthThreshold();\r
+            }\r
             else if ( ( o == _extract_taxonomy_pfam_strict_rbmi ) || ( o == _extract_taxonomy_pfam_relaxed_rbmi )\r
                     || ( o == _extract_taxonomy_agressive_rbmi ) ) {\r
                 if ( _replace_underscores_cbmi != null ) {\r
@@ -589,7 +601,7 @@ public final class MainFrameApplication extends MainFrame {
         return _msa_file;\r
     }\r
 \r
-    public List<Sequence> getSeqs() {\r
+    public List<MolecularSequence> getSeqs() {\r
         return _seqs;\r
     }\r
 \r
@@ -697,11 +709,11 @@ public final class MainFrameApplication extends MainFrame {
         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {\r
             setSeqsFile( null );\r
             setSeqs( null );\r
-            List<Sequence> seqs = null;\r
+            List<MolecularSequence> seqs = null;\r
             try {\r
                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {\r
                     seqs = FastaParser.parse( new FileInputStream( file ) );\r
-                    for( final Sequence seq : seqs ) {\r
+                    for( final MolecularSequence seq : seqs ) {\r
                         System.out.println( SequenceWriter.toFasta( seq, 60 ) );\r
                     }\r
                 }\r
@@ -868,16 +880,16 @@ public final class MainFrameApplication extends MainFrame {
                         .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) );\r
                 setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );\r
                 setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );\r
-                MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),\r
-                                                                     _show_scale_cbmi,\r
-                                                                     _show_branch_length_values_cbmi,\r
-                                                                     _non_lined_up_cladograms_rbmi,\r
-                                                                     _uniform_cladograms_rbmi,\r
-                                                                     _ext_node_dependent_cladogram_rbmi,\r
-                                                                     _label_direction_cbmi );\r
                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );\r
                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );\r
                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );\r
+                try {\r
+                    getMainPanel().getControlPanel().setVisibilityOfDomainStrucureCB();\r
+                    getMainPanel().getControlPanel().setVisibilityOfX();\r
+                }\r
+                catch ( final Exception ignore ) {\r
+                    // do nothing, not important.\r
+                }\r
             }\r
         } );\r
         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( DISPLAY_SUBHEADER ), getConfiguration() ) );\r
@@ -893,13 +905,13 @@ public final class MainFrameApplication extends MainFrame {
         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( SHOW_OVERVIEW_LABEL ) );\r
         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( DISPLAY_SCALE_LABEL ) );\r
         _options_jmenu\r
-                .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );\r
-        _options_jmenu\r
                 .add( _show_default_node_shapes_internal_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_INT ) );\r
         _options_jmenu\r
                 .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_EXT ) );\r
+        _options_jmenu.add( _line_up_renderable_data_cbmi = new JCheckBoxMenuItem( MainFrame.LINE_UP_RENDERABLE_DATA ) );\r
         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {\r
-            _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( SHOW_DOMAIN_LABELS_LABEL ) );\r
+            _options_jmenu.add( _right_line_up_domains_cbmi = new JCheckBoxMenuItem( MainFrame.RIGHT_LINE_UP_DOMAINS ) );\r
+            _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( MainFrame.SHOW_DOMAIN_LABELS_LABEL ) );\r
         }\r
         _options_jmenu.add( _show_annotation_ref_source = new JCheckBoxMenuItem( SHOW_ANN_REF_SOURCE_LABEL ) );\r
         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( SHOW_CONF_STDDEV_LABEL ) );\r
@@ -999,7 +1011,6 @@ public final class MainFrameApplication extends MainFrame {
                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );\r
         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,\r
                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );\r
-        customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );\r
         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );\r
         customizeCheckBoxMenuItem( _label_direction_cbmi,\r
                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );\r
@@ -1029,6 +1040,8 @@ public final class MainFrameApplication extends MainFrame {
                 .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS );\r
         customizeCheckBoxMenuItem( _use_internal_names_for_conf_in_nh_export_cbmi, getOptions()\r
                 .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES );\r
+        customizeCheckBoxMenuItem( _line_up_renderable_data_cbmi, getOptions().isLineUpRendarableNodeData() );\r
+        customizeCheckBoxMenuItem( _right_line_up_domains_cbmi, getOptions().isRightLineUpDomains() );\r
         _jmenubar.add( _options_jmenu );\r
     }\r
 \r
@@ -1065,10 +1078,10 @@ public final class MainFrameApplication extends MainFrame {
         _tools_menu.addSeparator();\r
         _tools_menu.add( _remove_visual_styles_item = new JMenuItem( "Delete All Visual Styles From Nodes" ) );\r
         _remove_visual_styles_item\r
-                .setToolTipText( "To remove all node visual styles (fonts, colors) from the current phylogeny." );\r
+                .setToolTipText( "To remove all node visual styles (fonts, colors) from the current phylogeny" );\r
         customizeJMenuItem( _remove_visual_styles_item );\r
         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete All Colors From Branches" ) );\r
-        _remove_branch_color_item.setToolTipText( "To remove all branch color values from the current phylogeny." );\r
+        _remove_branch_color_item.setToolTipText( "To remove all branch color values from the current phylogeny" );\r
         customizeJMenuItem( _remove_branch_color_item );\r
         _tools_menu.addSeparator();\r
         _tools_menu.add( _annotate_item = new JMenuItem( "Annotate Sequences of Selected Nodes" ) );\r
@@ -1077,18 +1090,28 @@ public final class MainFrameApplication extends MainFrame {
         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );\r
         customizeJMenuItem( _midpoint_root_item );\r
         _tools_menu.addSeparator();\r
-        _tools_menu.add( _deleted_selected_nodes_item = new JMenuItem( "Delete Selected Nodes" ) );\r
-        customizeJMenuItem( _deleted_selected_nodes_item );\r
-        _tools_menu.add( _deleted_not_selected_nodes_item = new JMenuItem( "Retain Selected Nodes" ) );\r
-        customizeJMenuItem( _deleted_not_selected_nodes_item );\r
+        _tools_menu.add( _delete_selected_nodes_item = new JMenuItem( "Delete Selected Nodes" ) );\r
+        _delete_selected_nodes_item.setToolTipText( "To delete all selected external nodes" );\r
+        customizeJMenuItem( _delete_selected_nodes_item );\r
+        _tools_menu.add( _delete_not_selected_nodes_item = new JMenuItem( "Retain Selected Nodes" ) );\r
+        _delete_not_selected_nodes_item.setToolTipText( "To delete all not selected external nodes" );\r
+        customizeJMenuItem( _delete_not_selected_nodes_item );\r
         _tools_menu.addSeparator();\r
         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );\r
         customizeJMenuItem( _collapse_species_specific_subtrees );\r
+        _collapse_species_specific_subtrees.setToolTipText( "To (reversibly) collapse species-specific subtrees" );\r
         _tools_menu\r
                 .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold into Multifurcations" ) );\r
         customizeJMenuItem( _collapse_below_threshold );\r
         _collapse_below_threshold\r
-                .setToolTipText( "To collapse branches with confidence values below a threshold into multifurcations (in the case of multiple confidences per branch: without at least one confidence value above a threshold)" );\r
+                .setToolTipText( "To (permanently) collapse branches with confidence values below a threshold into multifurcations (in the case of multiple confidences per branch: without at least one confidence value above a threshold)" );\r
+        //\r
+        _tools_menu\r
+                .add( _collapse_below_branch_length = new JMenuItem( "Collapse Branches with Branch Lengths Below Threshold into Multifurcations" ) );\r
+        customizeJMenuItem( _collapse_below_branch_length );\r
+        _collapse_below_branch_length\r
+                .setToolTipText( "To (permanently) collapse branches with branches with branch lengths below a threshold into multifurcations" );\r
+        //\r
         _tools_menu.addSeparator();\r
         _tools_menu\r
                 .add( _extract_tax_code_from_node_names_jmi = new JMenuItem( "Extract Taxonomic Data from Node Names" ) );\r
@@ -1234,7 +1257,6 @@ public final class MainFrameApplication extends MainFrame {
             if ( ( phys != null ) && ( phys.length > 0 ) ) {\r
                 if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) {\r
                     for( final Phylogeny phy : phys ) {\r
-                        \r
                         PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );\r
                     }\r
                 }\r
@@ -1258,7 +1280,7 @@ public final class MainFrameApplication extends MainFrame {
         _msa_file = msa_file;\r
     }\r
 \r
-    void setSeqs( final List<Sequence> seqs ) {\r
+    void setSeqs( final List<MolecularSequence> seqs ) {\r
         _seqs = seqs;\r
     }\r
 \r
@@ -1429,7 +1451,7 @@ public final class MainFrameApplication extends MainFrame {
         }\r
         final int result = _sequences_filechooser.showOpenDialog( _contentpane );\r
         final File file = _sequences_filechooser.getSelectedFile();\r
-        List<Sequence> seqs = null;\r
+        List<MolecularSequence> seqs = null;\r
         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {\r
             try {\r
                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {\r
@@ -1478,13 +1500,13 @@ public final class MainFrameApplication extends MainFrame {
             }\r
         }\r
         if ( seqs != null ) {\r
-            for( final Sequence seq : seqs ) {\r
+            for( final MolecularSequence seq : seqs ) {\r
                 System.out.println( seq.getIdentifier() );\r
             }\r
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();\r
             int total_counter = 0;\r
             int attached_counter = 0;\r
-            for( final Sequence seq : seqs ) {\r
+            for( final MolecularSequence seq : seqs ) {\r
                 ++total_counter;\r
                 final String seq_name = seq.getIdentifier();\r
                 if ( !ForesterUtil.isEmpty( seq_name ) ) {\r
@@ -1668,7 +1690,65 @@ public final class MainFrameApplication extends MainFrame {
         }\r
     }\r
 \r
-    private void collapse( final Phylogeny phy, final double m ) {\r
+    private void collapseBl( final Phylogeny phy ) {\r
+        final PhylogenyNodeIterator it = phy.iteratorPostorder();\r
+        final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();\r
+        double min_bl = Double.MAX_VALUE;\r
+        boolean bl_present = false;\r
+        while ( it.hasNext() ) {\r
+            final PhylogenyNode n = it.next();\r
+            if ( !n.isExternal() && !n.isRoot() ) {\r
+                final double bl = n.getDistanceToParent();\r
+                if ( bl != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {\r
+                    bl_present = true;\r
+                    if ( bl < getMinNotCollapseBlValue() ) {\r
+                        to_be_removed.add( n );\r
+                    }\r
+                    if ( bl < min_bl ) {\r
+                        min_bl = bl;\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        if ( bl_present ) {\r
+            for( final PhylogenyNode node : to_be_removed ) {\r
+                PhylogenyMethods.removeNode( node, phy );\r
+            }\r
+            if ( to_be_removed.size() > 0 ) {\r
+                phy.externalNodesHaveChanged();\r
+                phy.clearHashIdToNodeMap();\r
+                phy.recalculateNumberOfExternalDescendants( true );\r
+                getCurrentTreePanel().resetNodeIdToDistToLeafMap();\r
+                getCurrentTreePanel().updateSetOfCollapsedExternalNodes();\r
+                getCurrentTreePanel().calculateLongestExtNodeInfo();\r
+                getCurrentTreePanel().setNodeInPreorderToNull();\r
+                getCurrentTreePanel().recalculateMaxDistanceToRoot();\r
+                getCurrentTreePanel().resetPreferredSize();\r
+                getCurrentTreePanel().setEdited( true );\r
+                getCurrentTreePanel().repaint();\r
+                repaint();\r
+            }\r
+            if ( to_be_removed.size() > 0 ) {\r
+                JOptionPane.showMessageDialog( this, "Collapsed " + to_be_removed.size()\r
+                        + " branches with\nbranch length values below " + getMinNotCollapseBlValue(), "Collapsed "\r
+                        + to_be_removed.size() + " branches", JOptionPane.INFORMATION_MESSAGE );\r
+            }\r
+            else {\r
+                JOptionPane.showMessageDialog( this,\r
+                                               "No branch collapsed,\nminimum branch length is " + min_bl,\r
+                                               "No branch collapsed",\r
+                                               JOptionPane.INFORMATION_MESSAGE );\r
+            }\r
+        }\r
+        else {\r
+            JOptionPane.showMessageDialog( this,\r
+                                           "No branch collapsed because no branch length values present",\r
+                                           "No branch length values present",\r
+                                           JOptionPane.INFORMATION_MESSAGE );\r
+        }\r
+    }\r
+\r
+    private void collapse( final Phylogeny phy ) {\r
         final PhylogenyNodeIterator it = phy.iteratorPostorder();\r
         final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();\r
         double min_support = Double.MAX_VALUE;\r
@@ -1758,7 +1838,43 @@ public final class MainFrameApplication extends MainFrame {
                     }\r
                     if ( success && ( m >= 0.0 ) ) {\r
                         setMinNotCollapseConfidenceValue( m );\r
-                        collapse( phy, m );\r
+                        collapse( phy );\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private void collapseBelowBranchLengthThreshold() {\r
+        if ( getCurrentTreePanel() != null ) {\r
+            final Phylogeny phy = getCurrentTreePanel().getPhylogeny();\r
+            if ( ( phy != null ) && !phy.isEmpty() ) {\r
+                final String s = ( String ) JOptionPane\r
+                        .showInputDialog( this,\r
+                                          "Please enter the minimum branch length value\n",\r
+                                          "Minimal Branch Length Value",\r
+                                          JOptionPane.QUESTION_MESSAGE,\r
+                                          null,\r
+                                          null,\r
+                                          getMinNotCollapseBlValue() );\r
+                if ( !ForesterUtil.isEmpty( s ) ) {\r
+                    boolean success = true;\r
+                    double m = 0.0;\r
+                    final String m_str = s.trim();\r
+                    if ( !ForesterUtil.isEmpty( m_str ) ) {\r
+                        try {\r
+                            m = Double.parseDouble( m_str );\r
+                        }\r
+                        catch ( final Exception ex ) {\r
+                            success = false;\r
+                        }\r
+                    }\r
+                    else {\r
+                        success = false;\r
+                    }\r
+                    if ( success && ( m >= 0.0 ) ) {\r
+                        setMinNotCollapseBlValue( m );\r
+                        collapseBl( phy );\r
                     }\r
                 }\r
             }\r
@@ -1914,6 +2030,10 @@ public final class MainFrameApplication extends MainFrame {
         return _min_not_collapse;\r
     }\r
 \r
+    private double getMinNotCollapseBlValue() {\r
+        return _min_not_collapse_bl;\r
+    }\r
+\r
     private PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {\r
         if ( _phylogenetic_inference_options == null ) {\r
             _phylogenetic_inference_options = new PhylogeneticInferenceOptions();\r
@@ -2209,6 +2329,9 @@ public final class MainFrameApplication extends MainFrame {
                                 }\r
                             }\r
                         }\r
+                        if ( PREPROCESS_TREES ) {\r
+                            preProcessTreesUponReading( phys );\r
+                        }\r
                         AptxUtil.addPhylogeniesToTabs( phys,\r
                                                        file.getName(),\r
                                                        file.getAbsolutePath(),\r
@@ -2232,6 +2355,30 @@ public final class MainFrameApplication extends MainFrame {
         System.gc();\r
     }\r
 \r
+    private void preProcessTreesUponReading( final Phylogeny[] phys ) {\r
+        for( final Phylogeny phy : phys ) {\r
+            if ( ( phy != null ) && !phy.isEmpty() ) {\r
+                for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {\r
+                    final PhylogenyNode n = it.next();\r
+                    if ( n.isExternal() ) {\r
+                        if ( n.getNodeData().isHasSequence() ) {\r
+                            final Sequence s = n.getNodeData().getSequence();\r
+                            if ( ForesterUtil.isEmpty( s.getGeneName() ) || s.getGeneName().startsWith( "LOC" ) ) {\r
+                                if ( ( s.getAccession() != null )\r
+                                        && !ForesterUtil.isEmpty( s.getAccession().getValue() ) ) {\r
+                                    s.setGeneName( s.getAccession().getValue() );\r
+                                }\r
+                                else if ( !ForesterUtil.isEmpty( n.getName() ) ) {\r
+                                    s.setGeneName( n.getName() );\r
+                                }\r
+                            }\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
     private void readSpeciesTreeFromFile() {\r
         Phylogeny t = null;\r
         boolean exception = false;\r
@@ -2345,6 +2492,10 @@ public final class MainFrameApplication extends MainFrame {
         _min_not_collapse = min_not_collapse;\r
     }\r
 \r
+    private void setMinNotCollapseBlValue( final double min_not_collapse_bl ) {\r
+        _min_not_collapse_bl = min_not_collapse_bl;\r
+    }\r
+\r
     private void setPhylogeneticInferenceOptions( final PhylogeneticInferenceOptions phylogenetic_inference_options ) {\r
         _phylogenetic_inference_options = phylogenetic_inference_options;\r
     }\r