removed unneeded import
[jalview.git] / forester / java / src / org / forester / phylogeny / Phylogeny.java
index 876510c..86aae06 100644 (file)
@@ -37,6 +37,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Vector;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.forester.io.parsers.nhx.NHXParser;
 import org.forester.io.writers.PhylogenyWriter;
@@ -116,8 +118,8 @@ public class Phylogeny {
         new_node.setParent( sibling_parent );
         sibling.setParent( new_node );
         sibling_parent.setChildNode( sibling_index, new_node );
-        final double new_dist = sibling.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ? PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT
-                : sibling.getDistanceToParent() / 2;
+        final double new_dist = sibling.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT
+                ? PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT : sibling.getDistanceToParent() / 2;
         new_node.setDistanceToParent( new_dist );
         sibling.setDistanceToParent( new_dist );
         externalNodesHaveChanged();
@@ -138,12 +140,12 @@ public class Phylogeny {
         else {
             double max = -Double.MAX_VALUE;
             for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
-                final double l = calculateSubtreeHeight( n.getChildNode( i ),  take_collapse_into_account );
+                final double l = calculateSubtreeHeight( n.getChildNode( i ), take_collapse_into_account );
                 if ( l > max ) {
                     max = l;
                 }
             }
-            return max + ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0);
+            return max + ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0 );
         }
     }
 
@@ -260,13 +262,17 @@ public class Phylogeny {
                 if ( p.getNumberOfDescendants() == 2 ) {
                     final int pi = p.getChildNodeIndex();
                     if ( removed_node.isFirstChildNode() ) {
-                        p.getChildNode( 1 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
-                                                                                                         .getDistanceToParent(), p.getChildNode( 1 ).getDistanceToParent() ) );
+                        p.getChildNode( 1 )
+                                .setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p.getDistanceToParent(),
+                                                                                              p.getChildNode( 1 )
+                                                                                                      .getDistanceToParent() ) );
                         pp.setChildNode( pi, p.getChildNode( 1 ) );
                     }
                     else {
-                        p.getChildNode( 0 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
-                                                                                                         .getDistanceToParent(), p.getChildNode( 0 ).getDistanceToParent() ) );
+                        p.getChildNode( 0 )
+                                .setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p.getDistanceToParent(),
+                                                                                              p.getChildNode( 0 )
+                                                                                                      .getDistanceToParent() ) );
                         pp.setChildNode( pi, p.getChildNode( 0 ) );
                     }
                 }
@@ -332,7 +338,6 @@ public class Phylogeny {
         return _external_nodes_set;
     }
 
     /**
      * Returns the first external PhylogenyNode.
      */
@@ -353,7 +358,7 @@ public class Phylogeny {
      *
      * @return the height for rooted, tree-shaped phylogenies
      */
-    public double calculateHeight(final boolean take_collapse_into_account) {
+    public double calculateHeight( final boolean take_collapse_into_account ) {
         if ( isEmpty() ) {
             return 0.0;
         }
@@ -446,6 +451,23 @@ public class Phylogeny {
         return nodes;
     }
 
+    public List<PhylogenyNode> getNodes( final Pattern p ) {
+        if ( isEmpty() ) {
+            return null;
+        }
+        final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
+        for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
+            final PhylogenyNode n = iter.next();
+            if ( n.getName() != null ) {
+                final Matcher m = p.matcher( n.getName() );
+                if ( m.find() ) {
+                    nodes.add( n );
+                }
+            }
+        }
+        return nodes;
+    }
+
     public List<PhylogenyNode> getNodesViaSequenceName( final String seq_name ) {
         if ( isEmpty() ) {
             return null;
@@ -634,12 +656,12 @@ public class Phylogeny {
             taxIdList = map.get( node );
             if ( node.isDuplication() && isContains( taxIdList, taxonomyCodeRangeList ) ) {
                 if ( node.getChildNode1() == prev ) {
-                    v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode2()
-                                                   .getAllExternalDescendants() ) );
+                    v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId,
+                                                   node.getChildNode2().getAllExternalDescendants() ) );
                 }
                 else {
-                    v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode1()
-                                                   .getAllExternalDescendants() ) );
+                    v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId,
+                                                   node.getChildNode1().getAllExternalDescendants() ) );
                 }
             }
         }
@@ -648,7 +670,7 @@ public class Phylogeny {
 
     public Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> getRelevantSequenceRelationTypes() {
         if ( _relevant_sequence_relation_types == null ) {
-            _relevant_sequence_relation_types = new Vector<SEQUENCE_RELATION_TYPE>();
+            _relevant_sequence_relation_types = new Vector<SequenceRelation.SEQUENCE_RELATION_TYPE>();
         }
         return _relevant_sequence_relation_types;
     }
@@ -703,6 +725,27 @@ public class Phylogeny {
         return true;
     }
 
+    public boolean isCompletelyBinaryAllow3ChildrenAtRoot() {
+        if ( isEmpty() ) {
+            return false;
+        }
+        for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
+            final PhylogenyNode node = iter.next();
+            if ( node.isRoot() ) {
+                if ( node.isInternal()
+                        && ( ( node.getNumberOfDescendants() != 2 ) && ( node.getNumberOfDescendants() != 3 ) ) ) {
+                    return false;
+                }
+            }
+            else {
+                if ( node.isInternal() && ( node.getNumberOfDescendants() != 2 ) ) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
     /**
      * Checks whether a Phylogeny object is deleted (or empty).
      *
@@ -972,7 +1015,7 @@ public class Phylogeny {
                 }
                 else {
                     node.setDistanceToParent( ( c.getDistanceToParent() >= 0.0 ? c.getDistanceToParent() : 0.0 )
-                                              + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) );
+                            + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) );
                 }
                 if ( c.getBranchDataDirectly() != null ) {
                     node.setBranchData( ( BranchData ) c.getBranchDataDirectly().copy() );