}
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();
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." );
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();