removed unneeded import
[jalview.git] / forester / java / src / org / forester / phylogeny / PhylogenyMethods.java
index f31c657..42fde76 100644 (file)
@@ -151,6 +151,30 @@ public class PhylogenyMethods {
     }
 
     /**
+     * For external nodes the level is 0.
+     * 
+     * @param node
+     * @return
+     */
+    public static int calculateLevel( final PhylogenyNode node ) {
+        if ( node.isExternal() ) {
+            return 0;
+        }
+        int level = 0;
+        for( PhylogenyNode ext : node.getAllExternalDescendants() ) {
+            int counter = 0;
+            while ( ext != node ) {
+                ext = ext.getParent();
+                ++counter;
+            }
+            if ( counter > level ) {
+                level = counter;
+            }
+        }
+        return level;
+    }
+
+    /**
      * Calculates the distance between PhylogenyNodes node1 and node2.
      *
      *
@@ -237,29 +261,6 @@ public class PhylogenyMethods {
         return node1;
     }
 
-    public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
-        if ( node.isExternal() ) {
-            return 0;
-        }
-        short max = 0;
-        for( PhylogenyNode d : node.getAllExternalDescendants() ) {
-            short steps = 0;
-            while ( d != node ) {
-                if ( d.isCollapse() ) {
-                    steps = 0;
-                }
-                else {
-                    steps++;
-                }
-                d = d.getParent();
-            }
-            if ( max < steps ) {
-                max = steps;
-            }
-        }
-        return max;
-    }
-
     public static int calculateMaxDepth( final Phylogeny phy ) {
         int max = 0;
         for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
@@ -271,7 +272,7 @@ public class PhylogenyMethods {
         }
         return max;
     }
-    
+
     public static String[] obtainPresentRanksSorted( final Phylogeny phy ) {
         final Set<String> present_ranks = new HashSet<String>();
         for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
@@ -284,12 +285,12 @@ public class PhylogenyMethods {
                 }
             }
         }
-        final String ordered_ranks[] = new String[present_ranks.size() + 1];
+        final String ordered_ranks[] = new String[ present_ranks.size() + 1 ];
         int c = 0;
         for( final String rank : TaxonomyUtil.RANKS ) {
-             if ( present_ranks.contains( rank ) ) {
-                 ordered_ranks[ c++ ] = rank;
-             }
+            if ( present_ranks.contains( rank ) ) {
+                ordered_ranks[ c++ ] = rank;
+            }
         }
         ordered_ranks[ c ] = "off";
         return ordered_ranks;
@@ -545,6 +546,30 @@ public class PhylogenyMethods {
         return descs;
     }
 
+    public static List<PhylogenyNode> getAllDescendantsOfGivenLevel( final PhylogenyNode node, final int level ) {
+        final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
+        final Set<Long> encountered = new HashSet<Long>();
+        if ( !node.isExternal() ) {
+            final List<PhylogenyNode> exts = node.getAllExternalDescendants();
+            for( PhylogenyNode current : exts ) {
+                if ( calculateLevel( current ) == level ) {
+                    descs.add( current );
+                }
+                while ( current != node ) {
+                    current = current.getParent();
+                    if ( encountered.contains( current.getId() ) ) {
+                        continue;
+                    }
+                    if ( calculateLevel( current ) == level ) {
+                        descs.add( current );
+                    }
+                    encountered.add( current.getId() );
+                }
+            }
+        }
+        return descs;
+    }
+
     /**
      *
      * Convenience method
@@ -1543,7 +1568,8 @@ public class PhylogenyMethods {
         return nodes_to_delete;
     }
 
-    final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
+    final static public void transferInternalNamesToConfidenceValues( final Phylogeny phy,
+                                                                      final String confidence_type ) {
         final PhylogenyNodeIterator it = phy.iteratorPostorder();
         while ( it.hasNext() ) {
             final PhylogenyNode n = it.next();
@@ -1557,7 +1583,7 @@ public class PhylogenyMethods {
                             + e.getLocalizedMessage() );
                 }
                 if ( value >= 0.0 ) {
-                    n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
+                    n.getBranchData().addConfidence( new Confidence( value, confidence_type ) );
                     n.setName( "" );
                 }
             }
@@ -1696,6 +1722,9 @@ public class PhylogenyMethods {
                         n.getNodeData().getTaxonomy().setIdentifier( new Identifier( name ) );
                         break;
                     }
+                    case CLADE_NAME:
+                        n.setName( name );
+                        break;
                     default: {
                         throw new IllegalArgumentException( "don't know what to do with " + field );
                     }
@@ -2055,7 +2084,7 @@ public class PhylogenyMethods {
     }
 
     public final static void collapseToDepth( final Phylogeny phy, final int depth ) {
-         if ( phy.getNumberOfExternalNodes() < 3 ) {
+        if ( phy.getNumberOfExternalNodes() < 3 ) {
             return;
         }
         collapseToDepthHelper( phy.getRoot(), 0, depth );
@@ -2083,8 +2112,6 @@ public class PhylogenyMethods {
         }
     }
 
-   
-    
     public final static void collapseToRank( final Phylogeny phy, final int rank ) {
         if ( phy.getNumberOfExternalNodes() < 3 ) {
             return;
@@ -2109,7 +2136,6 @@ public class PhylogenyMethods {
             else {
                 if ( TaxonomyUtil.RANK_TO_INT.get( current_rank ) >= target_rank ) {
                     n.setCollapse( true );
-                    
                     final PhylogenyNodeIterator it = new PreorderTreeIterator( n );
                     while ( it.hasNext() ) {
                         it.next().setCollapse( true );
@@ -2124,7 +2150,7 @@ public class PhylogenyMethods {
             collapseToRankHelper( desc, target_rank );
         }
     }
-    
+
     public final static PhylogenyNode getFirstExternalNode( final PhylogenyNode node ) {
         PhylogenyNode n = node;
         while ( n.isInternal() ) {
@@ -2132,7 +2158,7 @@ public class PhylogenyMethods {
         }
         return n;
     }
-    
+
     public final static PhylogenyNode getLastExternalNode( final PhylogenyNode node ) {
         PhylogenyNode n = node;
         while ( n.isInternal() ) {
@@ -2150,5 +2176,4 @@ public class PhylogenyMethods {
         }
         return false;
     }
-    
 }