in progress
authorcmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Sat, 18 Feb 2012 23:54:47 +0000 (23:54 +0000)
committercmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Sat, 18 Feb 2012 23:54:47 +0000 (23:54 +0000)
forester/java/src/org/forester/application/phyloxml_converter.java
forester/java/src/org/forester/archaeopteryx/ControlPanel.java
forester/java/src/org/forester/archaeopteryx/TreePanel.java
forester/java/src/org/forester/io/parsers/util/ParserUtils.java
forester/java/src/org/forester/phylogeny/Phylogeny.java
forester/java/src/org/forester/phylogeny/PhylogenyMethods.java
forester/java/src/org/forester/phylogeny/PhylogenyNode.java
forester/java/src/org/forester/test/Test.java
forester/java/src/org/forester/tools/SupportCount.java

index afa7dd7..bee52a0 100644 (file)
@@ -38,6 +38,7 @@ import org.forester.io.parsers.util.ParserUtils;
 import org.forester.io.writers.PhylogenyWriter;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
@@ -253,7 +254,7 @@ public class phyloxml_converter {
         }
         if ( order_subtrees ) {
             for( final Phylogeny phy : phys ) {
-                phy.orderAppearance( true );
+                PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.TAXONOMY );
             }
         }
         try {
index 814e199..21c4891 100644 (file)
@@ -61,6 +61,7 @@ import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.SequenceRelation;
@@ -240,7 +241,16 @@ final class ControlPanel extends JPanel implements ActionListener {
                     showWhole();
                 }
                 else if ( e.getSource() == _order ) {
-                    tp.getPhylogeny().orderAppearance( _order_of_appearance );
+                    DESCENDANT_SORT_PRIORITY pri = DESCENDANT_SORT_PRIORITY.TAXONOMY;
+                    if ( ( !isShowTaxonomyScientificNames() && !isShowTaxonomyCode() && !isShowTaxonomyCommonNames() ) ) {
+                        if ( ( isShowSequenceAcc() || isShowGeneNames() || isShowGeneSymbols() ) ) {
+                            pri = DESCENDANT_SORT_PRIORITY.SEQUENCE;
+                        }
+                        else if ( isShowNodeNames() ) {
+                            pri = DESCENDANT_SORT_PRIORITY.NODE_NAME;
+                        }
+                    }
+                    PhylogenyMethods.orderAppearance( tp.getPhylogeny().getRoot(), _order_of_appearance, true, pri );
                     _order_of_appearance = !_order_of_appearance;
                     displayedPhylogenyMightHaveChanged( false );
                 }
index 85c3d5e..a9c154b 100644 (file)
@@ -1786,7 +1786,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 _node_popup_menu_items[ i ].setEnabled( isCanReroot() );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.collapse_uncollapse ][ 0 ] ) ) {
-                _node_popup_menu_items[ i ].setEnabled( isCanCollapse() );
+                _node_popup_menu_items[ i ].setEnabled( ( isCanCollapse() && !node.isExternal() ) );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.color_subtree ][ 0 ] ) ) {
                 _node_popup_menu_items[ i ].setEnabled( isCanColorSubtree() );
@@ -1794,6 +1794,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             else if ( title.equals( Configuration.clickto_options[ Configuration.subtree ][ 0 ] ) ) {
                 _node_popup_menu_items[ i ].setEnabled( isCanSubtree( node ) );
             }
+            else if ( title.equals( Configuration.clickto_options[ Configuration.swap ][ 0 ] ) ) {
+                _node_popup_menu_items[ i ].setEnabled( node.getNumberOfDescendants() == 2 );
+            }
+            else if ( title.equals( Configuration.clickto_options[ Configuration.sort_descendents ][ 0 ] ) ) {
+                _node_popup_menu_items[ i ].setEnabled( node.getNumberOfDescendants() > 1 );
+            }
             _node_popup_menu_items[ i ].addActionListener( this );
             _node_popup_menu.add( _node_popup_menu_items[ i ] );
         }
@@ -4912,8 +4918,18 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final void swap( final PhylogenyNode node ) {
+        if ( node.isExternal() || ( node.getNumberOfDescendants() < 2 ) ) {
+            return;
+        }
+        if ( node.getNumberOfDescendants() > 2 ) {
+            JOptionPane.showMessageDialog( this,
+                                           "Cannot swap descendants of nodes with more than 2 descendants",
+                                           "Cannot swap descendants",
+                                           JOptionPane.ERROR_MESSAGE );
+            return;
+        }
         if ( !node.isExternal() ) {
-            _phylogeny.swapChildren( node );
+            node.swapChildren();
             setNodeInPreorderToNull();
         }
         repaint();
index b6191ee..8d65e24 100644 (file)
@@ -224,8 +224,8 @@ public final class ParserUtils {
                                                           final boolean limit_to_five,
                                                           final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
         if ( ( name.indexOf( "_" ) > 0 )
-                && ( name.length() < 25 )
-                && ( name.lastIndexOf( "_" ) == name.indexOf( "_" ) )
+                && ( name.length() < 31 )
+                //  && ( name.lastIndexOf( "_" ) == name.indexOf( "_" ) )
                 && ( name.indexOf( "|" ) < 0 )
                 && ( name.indexOf( "." ) < 0 )
                 && ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name
@@ -234,23 +234,20 @@ public final class ParserUtils {
             final String[] s = name.split( "[_/]" );
             if ( s.length > 1 ) {
                 String str = s[ 1 ];
-                if ( limit_to_five ) {
-                    if ( str.length() > 5 ) {
-                        str = str.substring( 0, 5 );
-                    }
-                    else if ( ( str.length() < 5 ) && ( str.startsWith( "RAT" ) || str.startsWith( "PIG" ) ) ) {
+                if ( !limit_to_five || ( str.length() < 6 ) ) {
+                    if ( ( str.length() < 5 ) && ( str.startsWith( "RAT" ) || str.startsWith( "PIG" ) ) ) {
                         str = str.substring( 0, 3 );
                     }
+                    final Matcher uc_letters_and_numbers = NHXParser.UC_LETTERS_NUMBERS_PATTERN.matcher( str );
+                    if ( !uc_letters_and_numbers.matches() ) {
+                        return null;
+                    }
+                    final Matcher numbers_only = NHXParser.NUMBERS_ONLY_PATTERN.matcher( str );
+                    if ( numbers_only.matches() ) {
+                        return null;
+                    }
+                    return str;
                 }
-                final Matcher letters_and_numbers = NHXParser.UC_LETTERS_NUMBERS_PATTERN.matcher( str );
-                if ( !letters_and_numbers.matches() ) {
-                    return null;
-                }
-                final Matcher numbers_only = NHXParser.NUMBERS_ONLY_PATTERN.matcher( str );
-                if ( numbers_only.matches() ) {
-                    return null;
-                }
-                return str;
             }
         }
         return null;
index f61a79a..f3b6c5f 100644 (file)
@@ -867,46 +867,6 @@ public class Phylogeny {
         PhylogenyNode.setNodeCount( max + 1 );
     }
 
-    /**
-     * Arranges the order of childern for each node of this Phylogeny in such a
-     * way that either the branch with more children is on top (right) or on
-     * bottom (left), dependent on the value of boolean order.
-     * 
-     * @param order
-     *            decides in which direction to order
-     */
-    public void orderAppearance( final boolean order ) throws RuntimeException {
-        if ( !isTree() ) {
-            throw new FailedConditionCheckException( "Attempt to order appearance on phylogeny which is not tree-like." );
-        }
-        if ( isEmpty() ) {
-            return;
-        }
-        orderAppearanceHelper( getRoot(), order );
-    }
-
-    // Helper method for "orderAppearance(boolean)".
-    // Traverses this Phylogeny recusively.
-    private void orderAppearanceHelper( final PhylogenyNode n, final boolean order ) {
-        if ( n.isExternal() ) {
-            return;
-        }
-        else {
-            PhylogenyNode temp = null;
-            // FIXME
-            if ( ( n.getNumberOfDescendants() == 2 )
-                    && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
-                    && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
-                temp = n.getChildNode1();
-                n.setChild1( n.getChildNode2() );
-                n.setChild2( temp );
-            }
-            for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
-                orderAppearanceHelper( n.getChildNode( i ), order );
-            }
-        }
-    }
-
     public void preOrderReId() {
         if ( isEmpty() ) {
             return;
@@ -1256,28 +1216,6 @@ public class Phylogeny {
         _type = type;
     }
 
-    /**
-     * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
-     * <p>
-     * (Last modified: 06/13/01)
-     * 
-     * @param node
-     *            a PhylogenyNode of this Phylogeny
-     */
-    public void swapChildren( final PhylogenyNode node ) throws RuntimeException {
-        if ( !isTree() ) {
-            throw new FailedConditionCheckException( "Attempt to swap children on phylogeny which is not tree-like." );
-        }
-        if ( isEmpty() || node.isExternal() || ( node.getNumberOfDescendants() < 2 ) ) {
-            return;
-        }
-        final PhylogenyNode first = node.getFirstChildNode();
-        for( int i = 1; i < node.getNumberOfDescendants(); ++i ) {
-            node.setChildNode( i - 1, node.getChildNode( i ) );
-        }
-        node.setChildNode( node.getNumberOfDescendants() - 1, first );
-    } // swapChildren( PhylogenyNode )
-
     public String toNewHampshire() {
         return toNewHampshire( false, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
     }
index a042cee..c5d676d 100644 (file)
@@ -1560,6 +1560,49 @@ public class PhylogenyMethods {
         return nodes_to_delete.size();
     }
 
+    /**
+     * Arranges the order of childern for each node of this Phylogeny in such a
+     * way that either the branch with more children is on top (right) or on
+     * bottom (left), dependent on the value of boolean order.
+     * 
+     * @param order
+     *            decides in which direction to order
+     * @param pri 
+     */
+    public static void orderAppearance( final PhylogenyNode n,
+                                        final boolean order,
+                                        final boolean order_ext_alphabetically,
+                                        final DESCENDANT_SORT_PRIORITY pri ) {
+        if ( n.isExternal() ) {
+            return;
+        }
+        else {
+            PhylogenyNode temp = null;
+            if ( ( n.getNumberOfDescendants() == 2 )
+                    && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
+                    && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
+                temp = n.getChildNode1();
+                n.setChild1( n.getChildNode2() );
+                n.setChild2( temp );
+            }
+            else if ( order_ext_alphabetically ) {
+                //                boolean all_ext = true;
+                //                for( PhylogenyNode i : n.getDescendants() ) {
+                //                    if ( !i.isExternal() ) {
+                //                        all_ext = false;
+                //                        break;
+                //                    }
+                //                }
+                //                if ( all_ext ) {
+                PhylogenyMethods.sortNodeDescendents( n, pri );
+                //                }
+            }
+            for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
+                orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
+            }
+        }
+    }
+
     public static enum PhylogenyNodeField {
         CLADE_NAME,
         TAXONOMY_CODE,
index f0f2a53..c7a1caf 100644 (file)
@@ -990,6 +990,23 @@ public class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode>
     }
 
     /**
+     * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
+     */
+    public void swapChildren() throws RuntimeException {
+        if ( isExternal() ) {
+            throw new RuntimeException( "attempt to swap descendants of external node" );
+        }
+        if ( getNumberOfDescendants() != 2 ) {
+            throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
+                    + " descendants" );
+        }
+        final PhylogenyNode a = getChildNode( 0 );
+        final PhylogenyNode b = getChildNode( 1 );
+        setChildNode( 0, b );
+        setChildNode( 1, a );
+    }
+
+    /**
      * Converts this PhylogenyNode to a New Hampshire X (NHX) String
      * representation.
      */
index da2c06c..9241552 100644 (file)
@@ -4715,7 +4715,7 @@ public final class Test {
                 if ( !b.getName().equals( "n10_ECOLI1/1-2" ) ) {
                     return false;
                 }
-                if ( !PhylogenyMethods.getSpecies( b ).equals( "ECOLI" ) ) {
+                if ( !PhylogenyMethods.getSpecies( b ).equals( "" ) ) {
                     return false;
                 }
                 final PhylogenyNode c = PhylogenyNode
@@ -4724,7 +4724,25 @@ public final class Test {
                 if ( !c.getName().equals( "n10_RATAF12/1000-2000" ) ) {
                     return false;
                 }
-                if ( !PhylogenyMethods.getSpecies( c ).equals( "RATAF" ) ) {
+                if ( !PhylogenyMethods.getSpecies( c ).equals( "" ) ) {
+                    return false;
+                }
+                final PhylogenyNode c1 = PhylogenyNode
+                        .createInstanceFromNhxString( "n10_BOVIN_1/1000-2000",
+                                                      PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
+                if ( !c1.getName().equals( "n10_BOVIN_1/1000-2000" ) ) {
+                    return false;
+                }
+                if ( !PhylogenyMethods.getSpecies( c1 ).equals( "BOVIN" ) ) {
+                    return false;
+                }
+                final PhylogenyNode c2 = PhylogenyNode
+                        .createInstanceFromNhxString( "n10_Bovin_1/1000-2000",
+                                                      PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
+                if ( !c2.getName().equals( "n10_Bovin_1/1000-2000" ) ) {
+                    return false;
+                }
+                if ( !PhylogenyMethods.getSpecies( c2 ).equals( "" ) ) {
                     return false;
                 }
                 final PhylogenyNode d = PhylogenyNode
index 0014915..e554d4e 100644 (file)
@@ -33,6 +33,7 @@ import java.util.NoSuchElementException;
 
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 
@@ -195,7 +196,10 @@ public final class SupportCount {
             if ( strip_evaluator_phylogenies ) {
                 unstripped_evaluator_phylogeny = evaluator_phylogeny.copy();
                 SupportCount.strip( seq_names_to_keep, evaluator_phylogeny );
-                evaluator_phylogeny.orderAppearance( true ); // This is for
+                PhylogenyMethods.orderAppearance( evaluator_phylogeny.getRoot(),
+                                                  true,
+                                                  true,
+                                                  DESCENDANT_SORT_PRIORITY.TAXONOMY ); // This is for
                 // easer
                 // comparison if
                 // phylos are saved
@@ -207,7 +211,10 @@ public final class SupportCount {
                                                    true,
                                                    similarity_threshold );
             if ( ( similarity_threshold < 0.0 ) || ( s >= similarity_threshold ) ) {
-                unstripped_evaluator_phylogeny.orderAppearance( true );
+                PhylogenyMethods.orderAppearance( unstripped_evaluator_phylogeny.getRoot(),
+                                                  true,
+                                                  true,
+                                                  DESCENDANT_SORT_PRIORITY.TAXONOMY );
                 evaluator_phylogenies_above_threshold.add( unstripped_evaluator_phylogeny );
             }
             if ( verbose ) {