removed unneeded import
[jalview.git] / forester / java / src / org / forester / phylogeny / PhylogenyMethods.java
index 97eba29..42fde76 100644 (file)
@@ -42,7 +42,6 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
-import org.forester.archaeopteryx.TreePanelUtil;
 import org.forester.io.parsers.FastaParser;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
@@ -66,7 +65,6 @@ import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PreorderTreeIterator;
 import org.forester.util.BasicDescriptiveStatistics;
 import org.forester.util.DescriptiveStatistics;
-import org.forester.util.FailedConditionCheckException;
 import org.forester.util.ForesterUtil;
 import org.forester.util.TaxonomyUtil;
 
@@ -153,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.
      *
      *
@@ -239,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(); ) {
@@ -273,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(); ) {
@@ -286,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;
@@ -547,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
@@ -1004,7 +1027,7 @@ public class PhylogenyMethods {
     private static enum NDF {
                              NodeName( "NN" ),
                              TaxonomyCode( "TC" ),
-                             TaxonomyCommonName( "CN" ),
+                             TaxonomyCommonName( "TN" ),
                              TaxonomyScientificName( "TS" ),
                              TaxonomyIdentifier( "TI" ),
                              TaxonomySynonym( "SY" ),
@@ -1545,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();
@@ -1559,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( "" );
                 }
             }
@@ -1698,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 );
                     }
@@ -2057,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 );
@@ -2085,8 +2112,6 @@ public class PhylogenyMethods {
         }
     }
 
-   
-    
     public final static void collapseToRank( final Phylogeny phy, final int rank ) {
         if ( phy.getNumberOfExternalNodes() < 3 ) {
             return;
@@ -2111,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 );
@@ -2126,7 +2150,7 @@ public class PhylogenyMethods {
             collapseToRankHelper( desc, target_rank );
         }
     }
-    
+
     public final static PhylogenyNode getFirstExternalNode( final PhylogenyNode node ) {
         PhylogenyNode n = node;
         while ( n.isInternal() ) {
@@ -2134,7 +2158,7 @@ public class PhylogenyMethods {
         }
         return n;
     }
-    
+
     public final static PhylogenyNode getLastExternalNode( final PhylogenyNode node ) {
         PhylogenyNode n = node;
         while ( n.isInternal() ) {
@@ -2142,5 +2166,14 @@ public class PhylogenyMethods {
         }
         return n;
     }
-    
+
+    public final static boolean isHasCollapsedNodes( final Phylogeny phy ) {
+        for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
+            final PhylogenyNode n = iter.next();
+            if ( !n.isExternal() && ( n.isCollapse() ) ) {
+                return true;
+            }
+        }
+        return false;
+    }
 }