in progress
[jalview.git] / forester / java / src / org / forester / evoinference / TestPhylogenyReconstruction.java
index 39e1a20..f6d3975 100644 (file)
@@ -32,10 +32,14 @@ import java.io.FileInputStream;
 import java.io.StringWriter;
 import java.util.Date;
 import java.util.List;
+import java.util.SortedSet;
 
+import org.forester.archaeopteryx.Archaeopteryx;
 import org.forester.evoinference.distance.NeighborJoining;
 import org.forester.evoinference.distance.NeighborJoiningF;
+import org.forester.evoinference.distance.NeighborJoiningR;
 import org.forester.evoinference.distance.PairwiseDistanceCalculator;
+import org.forester.evoinference.distance.S;
 import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
@@ -66,13 +70,28 @@ public class TestPhylogenyReconstruction {
     }
 
     public static void main( final String[] args ) {
-        if ( testNeighborJoining() ) {
-            System.out.println( "OK." );
+        //        System.out.println( "NJ" );
+        //        if ( testNeighborJoining() ) {
+        //            System.out.println( "  OK." );
+        //        }
+        //        else {
+        //            System.out.println( "  failed." );
+        //        }
+        System.out.println( "S" );
+        if ( testS() ) {
+            System.out.println( "  OK." );
         }
         else {
-            System.out.println( "failed." );
+            System.out.println( "  failed." );
+        }
+        System.out.println( "NJR" );
+        if ( testNeighborJoiningR() ) {
+            System.out.println( "  OK." );
+        }
+        else {
+            System.out.println( "  failed." );
         }
-        timeNeighborJoining();
+       // timeNeighborJoining();
     }
 
     public static boolean test( final File test_dir ) {
@@ -100,6 +119,12 @@ public class TestPhylogenyReconstruction {
             return false;
         }
         System.out.println( "OK." );
+        System.out.print( "  Datastructure S: " );
+        if ( !testS() ) {
+            System.out.println( "failed." );
+            return false;
+        }
+        System.out.println( "OK." );
         System.out.print( "  Neighbor Joining: " );
         if ( !testNeighborJoining() ) {
             System.out.println( "failed." );
@@ -2046,8 +2071,10 @@ public class TestPhylogenyReconstruction {
             m.setRow( "1.52430 1.44650 0.59580 0.46310 0.00000 0.34840 0.30830", 4 );
             m.setRow( "1.60430 1.43890 0.61790 0.50610 0.34840 0.00000 0.26920", 5 );
             m.setRow( "1.59050 1.46290 0.55830 0.47100 0.30830 0.26920 0.00000", 6 );
-            nj = NeighborJoining.createInstance( false, 6 );
+            //NeighborJoiningR njr = NeighborJoiningR.createInstance( true, 6 );
+            nj = NeighborJoining.createInstance( true, 6 );
             final Phylogeny p2 = nj.execute( m );
+            //  Archaeopteryx.createApplication( p2 );
             p2.reRoot( p2.getNode( "Bovine" ) );
             if ( isUnequal( p2.getNode( "Chimp" ).getDistanceToParent(), 0.151675 ) ) {
                 return false;
@@ -2217,6 +2244,317 @@ public class TestPhylogenyReconstruction {
         return true;
     }
 
+    private static boolean testS() {
+        try {
+            final S s0 = new S();
+            s0.initialize( 1 );
+            s0.addPairing( 0, 1, 0 );
+            s0.addPairing( 7, 8, 0 );
+            s0.addPairing( 4, 55, 0 );
+            s0.addPairing( 2, 3, 0 );
+            s0.addPairing( 4, 5, 0 );
+            s0.addPairing( 5, 6666, 0 );
+            s0.addPairing( 5, 666, 0 );
+            s0.addPairing( 5, 66, 0 );
+            s0.addPairing( 5, 6, 0 );
+            s0.addPairing( 6, 7, 0 );
+            s0.addPairing( 3, 4, 0 );
+            s0.addPairing( 1, 2, 0 );
+            if ( s0.size() != 1 ) {
+                return false;
+            }
+            if ( s0.getS( 0 ).size() != 8 ) {
+                return false;
+            }
+            if ( s0.getValues( 0, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 1, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 2, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 3, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 4, 0 ).size() != 2 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 ).size() != 4 ) {
+                return false;
+            }
+            if ( s0.getValues( 6, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 7, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( !s0.getValues( 0, 0 ).contains( 1 ) ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 6 ) ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 66 ) ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 666 ) ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 6666 ) ) {
+                return false;
+            }
+            s0.removePairing( 5, 6666, 0 );
+            if ( s0.getValues( 5, 0 ).contains( 6666 ) ) {
+                return false;
+            }
+            s0.removePairing( 5, 666, 0 );
+            if ( s0.getValues( 5, 0 ).contains( 666 ) ) {
+                return false;
+            }
+            s0.removePairing( 5, 66, 0 );
+            if ( s0.getValues( 5, 0 ).contains( 66 ) ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( s0.getS( 0 ).size() != 8 ) {
+                return false;
+            }
+            s0.removePairing( 5, 6, 0 );
+            if ( s0.getS( 0 ).size() != 7 ) {
+                return false;
+            }
+            s0.addPairing( 5, 6, 0 );
+            if ( s0.getS( 0 ).size() != 8 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 ).size() != 1 ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 6 ) ) {
+                return false;
+            }
+            s0.addPairing( 5, 403, 0 );
+            if ( s0.getValues( 5, 0 ).size() != 2 ) {
+                return false;
+            }
+            if ( !s0.getValues( 5, 0 ).contains( 403 ) ) {
+                return false;
+            }
+            s0.addPairing( 693, 100, 0 );
+            s0.addPairing( 693, 101, 0 );
+            if ( s0.getValues( 693, 0 ).size() != 2 ) {
+                return false;
+            }
+            s0.addPairing( 2, 33, 0 );
+            s0.addPairing( 2, 333, 0 );
+            final SortedSet<Integer>[] a = s0.toArray( 0 );
+            if ( !a[ 0 ].contains( 1 ) ) {
+                return false;
+            }
+            if ( a[ 0 ].size() != 1 ) {
+                return false;
+            }
+            if ( !a[ 1 ].contains( 2 ) ) {
+                return false;
+            }
+            if ( a[ 1 ].size() != 1 ) {
+                return false;
+            }
+            if ( !a[ 2 ].contains( 3 ) ) {
+                return false;
+            }
+            if ( !a[ 2 ].contains( 33 ) ) {
+                return false;
+            }
+            if ( !a[ 2 ].contains( 333 ) ) {
+                return false;
+            }
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
+
+    private static boolean testNeighborJoiningR() {
+        try {
+            //            final NeighborJoiningR nj0 = NeighborJoiningR.createInstance();
+            //            final BasicSymmetricalDistanceMatrix m0 = new BasicSymmetricalDistanceMatrix( 4 );
+            //            m0.setIdentifier( 0, "A" );
+            //            m0.setIdentifier( 1, "B" );
+            //            m0.setIdentifier( 2, "C" );
+            //            m0.setIdentifier( 3, "D" );
+            //            m0.setRow( "5 ", 1 );
+            //            m0.setRow( "3 6 ", 2 );
+            //            m0.setRow( "7.5 10.5 5.5", 3 );
+            //            final Phylogeny p0 = nj0.execute( m0 );
+            //            p0.reRoot( p0.getNode( "D" ) );
+            //            //  Archaeopteryx.createApplication( p0 );
+            //            if ( isUnequal( p0.getNode( "A" ).getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p0.getNode( "B" ).getDistanceToParent(), 4 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p0.getNode( "C" ).getDistanceToParent(), 0.5 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p0.getNode( "D" ).getDistanceToParent(), 2.5 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p0.getNode( "A" ).getParent().getDistanceToParent(), 1.5 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p0.getNode( "A" ).getParent().getParent().getDistanceToParent(), 2.5 ) ) {
+            //                return false;
+            //            }
+            BasicSymmetricalDistanceMatrix m = new BasicSymmetricalDistanceMatrix( 6 );
+            //            m.setRow( "5", 1 );
+            //            m.setRow( "4 7", 2 );
+            //            m.setRow( "7 10 7", 3 );
+            //            m.setRow( "6 9 6 5", 4 );
+            //            m.setRow( "8 11 8 9 8", 5 );
+            //            m.setIdentifier( 0, "A" );
+            //            m.setIdentifier( 1, "B" );
+            //            m.setIdentifier( 2, "C" );
+            //            m.setIdentifier( 3, "D" );
+            //            m.setIdentifier( 4, "E" );
+            //            m.setIdentifier( 5, "F" );
+            //            final NeighborJoiningR nj1 = NeighborJoiningR.createInstance();
+            //            final Phylogeny p1 = nj1.execute( m );
+            //            p1.reRoot( p1.getNode( "F" ) );
+            //            Archaeopteryx.createApplication( p1 );
+            //            if ( isUnequal( p1.getNode( "A" ).getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "B" ).getDistanceToParent(), 4 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "C" ).getDistanceToParent(), 2 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "D" ).getDistanceToParent(), 3 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "E" ).getDistanceToParent(), 2 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "F" ).getDistanceToParent(), 2.5 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "A" ).getParent().getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "A" ).getParent().getParent().getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "A" ).getParent().getParent().getParent().getDistanceToParent(), 2.5 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "B" ).getParent().getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "D" ).getParent().getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            //            if ( isUnequal( p1.getNode( "E" ).getParent().getDistanceToParent(), 1 ) ) {
+            //                return false;
+            //            }
+            ///////////////
+            m = new BasicSymmetricalDistanceMatrix( 7 );
+            m.setIdentifier( 0, "Bovine" );
+            m.setIdentifier( 1, "Mouse" );
+            m.setIdentifier( 2, "Gibbon" );
+            m.setIdentifier( 3, "Orang" );
+            m.setIdentifier( 4, "Gorilla" );
+            m.setIdentifier( 5, "Chimp" );
+            m.setIdentifier( 6, "Human" );
+            m.setRow( "0.00000 1.68660 1.71980 1.66060 1.52430 1.60430 1.59050", 0 );
+            m.setRow( "1.68660 0.00000 1.52320 1.48410 1.44650 1.43890 1.46290", 1 );
+            m.setRow( "1.71980 1.52320 0.00000 0.71150 0.59580 0.61790 0.55830", 2 );
+            m.setRow( "1.66060 1.48410 0.71150 0.00000 0.46310 0.50610 0.47100", 3 );
+            m.setRow( "1.52430 1.44650 0.59580 0.46310 0.00000 0.34840 0.30830", 4 );
+            m.setRow( "1.60430 1.43890 0.61790 0.50610 0.34840 0.00000 0.26920", 5 );
+            m.setRow( "1.59050 1.46290 0.55830 0.47100 0.30830 0.26920 0.00000", 6 );
+            final NeighborJoining nj = NeighborJoining.createInstance( true, 6 );
+            //nj = NeighborJoining.createInstance( true, 6 );
+            final Phylogeny pnj = nj.execute( m );
+            Archaeopteryx.createApplication( pnj );
+            //
+            m = new BasicSymmetricalDistanceMatrix( 7 );
+            m.setIdentifier( 0, "Bovine" );
+            m.setIdentifier( 1, "Mouse" );
+            m.setIdentifier( 2, "Gibbon" );
+            m.setIdentifier( 3, "Orang" );
+            m.setIdentifier( 4, "Gorilla" );
+            m.setIdentifier( 5, "Chimp" );
+            m.setIdentifier( 6, "Human" );
+            m.setRow( "0.00000 1.68660 1.71980 1.66060 1.52430 1.60430 1.59050", 0 );
+            m.setRow( "1.68660 0.00000 1.52320 1.48410 1.44650 1.43890 1.46290", 1 );
+            m.setRow( "1.71980 1.52320 0.00000 0.71150 0.59580 0.61790 0.55830", 2 );
+            m.setRow( "1.66060 1.48410 0.71150 0.00000 0.46310 0.50610 0.47100", 3 );
+            m.setRow( "1.52430 1.44650 0.59580 0.46310 0.00000 0.34840 0.30830", 4 );
+            m.setRow( "1.60430 1.43890 0.61790 0.50610 0.34840 0.00000 0.26920", 5 );
+            m.setRow( "1.59050 1.46290 0.55830 0.47100 0.30830 0.26920 0.00000", 6 );
+            final NeighborJoiningR nj2 = NeighborJoiningR.createInstance( true, 6 );
+            //nj = NeighborJoining.createInstance( true, 6 );
+            final Phylogeny p2 = nj2.execute( m );
+              Archaeopteryx.createApplication( p2 );
+            p2.reRoot( p2.getNode( "Bovine" ) );
+            if ( isUnequal( p2.getNode( "Chimp" ).getDistanceToParent(), 0.151675 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Human" ).getDistanceToParent(), 0.117525 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Gorilla" ).getDistanceToParent(), 0.153932 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Orang" ).getDistanceToParent(), 0.284694 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Gibbon" ).getDistanceToParent(), 0.357931 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Mouse" ).getDistanceToParent(), 0.76891 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Bovine" ).getDistanceToParent(), 0.458845 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Chimp" ).getParent().getDistanceToParent(), 0.039819 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Human" ).getParent().getDistanceToParent(), 0.039819 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getDistanceToParent(), 0.026956 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getParent().getDistanceToParent(), 0.046481 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getParent().getParent().getDistanceToParent(),
+                            0.420269 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getParent().getParent().getParent()
+                    .getDistanceToParent(), 0.458845 ) ) {
+                return false;
+            }
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
+
     private static boolean testSymmetricalDistanceMatrixParser() {
         try {
             final String l = ForesterUtil.getLineSeparator();
@@ -2578,8 +2916,17 @@ public class TestPhylogenyReconstruction {
     }
 
     private static void timeNeighborJoining() {
+        final NeighborJoiningR njr = NeighborJoiningR.createInstance();
+        for( int n = 3; n <= 9; ++n ) {
+            final int x = ( int ) Math.pow( 2, n );
+            final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
+            mt.randomize( new Date().getTime() );
+            final long start_time = new Date().getTime();
+            njr.execute( mt );
+            System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
+        }
         final NeighborJoiningF njf = NeighborJoiningF.createInstance();
-        for( int n = 3; n <= 8; ++n ) {
+        for( int n = 3; n <= 9; ++n ) {
             final int x = ( int ) Math.pow( 2, n );
             final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
             mt.randomize( new Date().getTime() );
@@ -2588,7 +2935,7 @@ public class TestPhylogenyReconstruction {
             System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
         }
         final NeighborJoining nj = NeighborJoining.createInstance();
-        for( int n = 3; n <= 8; ++n ) {
+        for( int n = 3; n <= 9; ++n ) {
             final int x = ( int ) Math.pow( 2, n );
             final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
             mt.randomize( new Date().getTime() );