remove single node root
authorcmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 24 Dec 2012 20:44:46 +0000 (20:44 +0000)
committercmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 24 Dec 2012 20:44:46 +0000 (20:44 +0000)
forester/java/src/org/forester/phylogeny/PhylogenyMethods.java
forester/java/src/org/forester/test/Test.java

index dab7523..d48db8d 100644 (file)
@@ -913,20 +913,19 @@ public class PhylogenyMethods {
     }
 
     public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
-        if ( remove_me.isRoot() && remove_me.getNumberOfDescendants() != 1 ) {
-            throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
-        }
-        
-        if ( remove_me.isRoot() && remove_me.getNumberOfDescendants() == 1 ) {
-            final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
-           
-            desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
-                                                             desc.getDistanceToParent() ) );
-            desc.setParent( null );
-            phylogeny.setRoot( desc );
-            phylogeny.clearHashIdToNodeMap();
+        if ( remove_me.isRoot() ) {
+            if ( remove_me.getNumberOfDescendants() == 1 ) {
+                final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
+                desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
+                                                                 desc.getDistanceToParent() ) );
+                desc.setParent( null );
+                phylogeny.setRoot( desc );
+                phylogeny.clearHashIdToNodeMap();
+            }
+            else {
+                throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
+            }
         }
-        
         else if ( remove_me.isExternal() ) {
             phylogeny.deleteSubtree( remove_me, false );
             phylogeny.clearHashIdToNodeMap();
index 6b726eb..04c868e 100644 (file)
@@ -459,6 +459,15 @@ public final class Test {
             System.out.println( "failed." );
             failed++;
         }
+        System.out.print( "Node removal: " );
+        if ( Test.testNodeRemoval() ) {
+            System.out.println( "OK." );
+            succeeded++;
+        }
+        else {
+            System.out.println( "failed." );
+            failed++;
+        }
         System.out.print( "Support count: " );
         if ( Test.testSupportCount() ) {
             System.out.println( "OK." );
@@ -3958,6 +3967,37 @@ public final class Test {
         return true;
     }
 
+    
+    private static boolean testNodeRemoval() {
+        try {
+            final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
+            final Phylogeny t0 = factory.create( "((a)b)", new NHXParser() )[ 0 ];
+            PhylogenyMethods.removeNode( t0.getNode( "b" ), t0 );
+            
+            if ( !t0.toNewHampshire().equals( "(a);" ) ) {
+                return false;
+            }
+            final Phylogeny t1 = factory.create( "((a:2)b:4)", new NHXParser() )[ 0 ];
+            PhylogenyMethods.removeNode( t1.getNode( "b" ), t1 );
+            
+            if ( !t1.toNewHampshire().equals( "(a:6.0);" ) ) {
+                return false;
+            }
+            final Phylogeny t2 = factory.create( "((a,b),c)", new NHXParser() )[ 0 ];
+            PhylogenyMethods.removeNode( t2.getNode( "b" ), t2 );
+            
+            if ( !t2.toNewHampshire().equals( "((a),c);" ) ) {
+                return false;
+            }
+            
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
+    
     private static boolean testMidpointrooting() {
         try {
             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();