in progress
[jalview.git] / forester / java / src / org / forester / evoinference / TestPhylogenyReconstruction.java
index 3f989a0..0e554f8 100644 (file)
@@ -32,13 +32,14 @@ import java.io.FileInputStream;
 import java.io.StringWriter;
 import java.util.Date;
 import java.util.List;
-import java.util.SortedSet;
+import java.util.Set;
 
 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.distance.Sarray;
+import org.forester.evoinference.distance.Sset;
 import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
@@ -83,6 +84,13 @@ public class TestPhylogenyReconstruction {
         else {
             System.out.println( "  failed." );
         }
+        System.out.println( "Sarray" );
+        if ( testSarray() ) {
+            System.out.println( "  OK." );
+        }
+        else {
+            System.out.println( "  failed." );
+        }
         System.out.println( "NJR" );
         if ( testNeighborJoiningR() ) {
             System.out.println( "  OK." );
@@ -90,7 +98,7 @@ public class TestPhylogenyReconstruction {
         else {
             System.out.println( "  failed." );
         }
-        //timeNeighborJoining();
+        timeNeighborJoining();
     }
 
     public static boolean test( final File test_dir ) {
@@ -429,7 +437,7 @@ public class TestPhylogenyReconstruction {
     private static boolean testDistanceCalculationMethods( final File test_dir ) {
         try {
             final Msa msa0 = GeneralMsaParser.parse( new FileInputStream( test_dir + ForesterUtil.FILE_SEPARATOR
-                    + "bcl.aln" ) );
+                                                                          + "bcl.aln" ) );
             final BasicSymmetricalDistanceMatrix pwd0 = PairwiseDistanceCalculator.calcKimuraDistances( msa0 );
             if ( pwd0.getSize() != 120 ) {
                 return false;
@@ -457,7 +465,7 @@ public class TestPhylogenyReconstruction {
             final String p1_str = "((((((a,b)ab,c)ac,d)ad,(e,f)ef)af,(g,h)gh)ah,i)r";
             final Phylogeny p1 = factory1.create( p1_str, new NHXParser() )[ 0 ];
             CharacterStateMatrix<CharacterStateMatrix.BinaryStates> m1 = new BasicCharacterStateMatrix<CharacterStateMatrix.BinaryStates>( 9,
-                                                                                                                                           1 );
+                    1 );
             m1.setIdentifier( 0, "a" );
             m1.setIdentifier( 1, "b" );
             m1.setIdentifier( 2, "c" );
@@ -530,7 +538,7 @@ public class TestPhylogenyReconstruction {
             final String p2_str = "((((((a,b)ab,c)ac,d)ad,(e,f)ef)af,(g,h,i)gi)ai,((j,k,l)jl,(m,n,o)mo,(p,q,r)pr)jr)root";
             final Phylogeny p2 = factory2.create( p2_str, new NHXParser() )[ 0 ];
             final CharacterStateMatrix<CharacterStateMatrix.BinaryStates> m2 = new BasicCharacterStateMatrix<CharacterStateMatrix.BinaryStates>( 18,
-                                                                                                                                                 4 );
+                    4 );
             m2.setIdentifier( 0, "a" );
             m2.setIdentifier( 1, "b" );
             m2.setIdentifier( 2, "c" );
@@ -839,7 +847,7 @@ public class TestPhylogenyReconstruction {
             final String p10_str = "((((((a,b)ab,c)ac,d)ad,(e,f)ef)af,(g,h)gh)ah,i)r";
             final Phylogeny p10 = factory10.create( p10_str, new NHXParser() )[ 0 ];
             final CharacterStateMatrix<CharacterStateMatrix.BinaryStates> m10 = new BasicCharacterStateMatrix<CharacterStateMatrix.BinaryStates>( 9,
-                                                                                                                                                  1 );
+                    1 );
             m10.setIdentifier( 0, "a" );
             m10.setIdentifier( 1, "b" );
             m10.setIdentifier( 2, "c" );
@@ -882,7 +890,7 @@ public class TestPhylogenyReconstruction {
             final String p1_str = "((((((a,b,y)aby,c)ac,d)ad,(e,f)ef)af,(g,h)gh)ah,i)r";
             final Phylogeny p1 = factory1.create( p1_str, new NHXParser() )[ 0 ];
             final CharacterStateMatrix<CharacterStateMatrix.BinaryStates> m1 = new BasicCharacterStateMatrix<CharacterStateMatrix.BinaryStates>( 10,
-                                                                                                                                                 1 );
+                    1 );
             m1.setIdentifier( 0, "a" );
             m1.setIdentifier( 1, "b" );
             m1.setIdentifier( 2, "y" );
@@ -953,7 +961,7 @@ public class TestPhylogenyReconstruction {
             final String p2_str = "((((((a,b,y)aby,c,d)cad,e,f)af,(g,h)gh)ah,i))r";
             final Phylogeny p2 = factory2.create( p2_str, new NHXParser() )[ 0 ];
             final CharacterStateMatrix<CharacterStateMatrix.BinaryStates> m2 = new BasicCharacterStateMatrix<CharacterStateMatrix.BinaryStates>( 10,
-                                                                                                                                                 1 );
+                    1 );
             m2.setIdentifier( 0, "a" );
             m2.setIdentifier( 1, "b" );
             m2.setIdentifier( 2, "y" );
@@ -2073,7 +2081,7 @@ public class TestPhylogenyReconstruction {
             //NeighborJoiningR njr = NeighborJoiningR.createInstance( true, 6 );
             nj = NeighborJoining.createInstance( true, 6 );
             final Phylogeny p2 = nj.execute( m );
-            //  Archaeopteryx.createApplication( p2 );
+            //Archaeopteryx.createApplication( p2 );
             p2.reRoot( p2.getNode( "Bovine" ) );
             if ( isUnequal( p2.getNode( "Chimp" ).getDistanceToParent(), 0.151675 ) ) {
                 return false;
@@ -2113,7 +2121,7 @@ public class TestPhylogenyReconstruction {
                 return false;
             }
             if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getParent().getParent().getParent()
-                    .getDistanceToParent(), 0.458845 ) ) {
+                            .getDistanceToParent(), 0.458845 ) ) {
                 return false;
             }
             m = new BasicSymmetricalDistanceMatrix( 4 );
@@ -2228,11 +2236,11 @@ public class TestPhylogenyReconstruction {
                 return false;
             }
             if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getParent().getParent().getParent()
-                    .getDistanceToParent(), 0.42027 ) ) {
+                            .getDistanceToParent(), 0.42027 ) ) {
                 return false;
             }
             if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getParent().getParent().getParent().getParent()
-                    .getDistanceToParent(), 0.458845 ) ) {
+                            .getDistanceToParent(), 0.458845 ) ) {
                 return false;
             }
         }
@@ -2245,7 +2253,7 @@ public class TestPhylogenyReconstruction {
 
     private static boolean testS() {
         try {
-            final S s0 = new S();
+            final Sset s0 = new Sset();
             s0.initialize( 1 );
             s0.addPairing( 0, 1, 0 );
             s0.addPairing( 7, 8, 0 );
@@ -2350,7 +2358,7 @@ public class TestPhylogenyReconstruction {
             }
             s0.addPairing( 2, 33, 0 );
             s0.addPairing( 2, 333, 0 );
-            final SortedSet<Integer>[] a = s0.toArray( 0 );
+            final Set<Integer>[] a = s0.toArray( 0 );
             if ( !a[ 0 ].contains( 1 ) ) {
                 return false;
             }
@@ -2380,6 +2388,147 @@ public class TestPhylogenyReconstruction {
         return true;
     }
 
+    private static boolean testSarray() {
+        try {
+            final Sarray s0 = new Sarray();
+            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 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 1, 0 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 2, 0 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 3, 0 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 4, 0 ).length != 2 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 ).length != 4 ) {
+                return false;
+            }
+            if ( s0.getValues( 6, 0 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 7, 0 ).length != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 0, 0 )[ 0 ] != 1 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 )[ 3 ] != 6 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 )[ 2 ] != 66 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 )[ 1 ] != 666 ) {
+                return false;
+            }
+            if ( s0.getValues( 5, 0 )[ 0 ] != 6666 ) {
+                return false;
+            }
+            s0.removePairing( 5, 6666, 0 );
+            if ( s0.getValues( 5, 0 ).length != 3 ) {
+                System.out.println( s0.getValues( 5, 0 ).length );
+                return false;
+            }
+            //            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 Set<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();
@@ -2479,8 +2628,10 @@ public class TestPhylogenyReconstruction {
             m2.setRow( "1.59050 1.46290 0.55830 0.47100 0.30830 0.26920 0.00000", 6 );
             final NeighborJoiningR nj2 = NeighborJoiningR.createInstance( true, 6 );
             final Phylogeny p2 = nj2.execute( m2 );
+            // Archaeopteryx.createApplication( p2 );
             p2.reRoot( p2.getNode( "Bovine" ) );
             if ( isUnequal( p2.getNode( "Chimp" ).getDistanceToParent(), 0.151675 ) ) {
+                System.out.println( p2.getNode( "Chimp" ).getDistanceToParent() );
                 return false;
             }
             if ( isUnequal( p2.getNode( "Human" ).getDistanceToParent(), 0.117525 ) ) {
@@ -2518,10 +2669,11 @@ public class TestPhylogenyReconstruction {
                 return false;
             }
             if ( isUnequal( p2.getNode( "Chimp" ).getParent().getParent().getParent().getParent().getParent()
-                    .getDistanceToParent(), 0.458845 ) ) {
+                            .getDistanceToParent(), 0.458845 ) ) {
                 return false;
             }
             //
+            // System.exit( 1 );
             final BasicSymmetricalDistanceMatrix m3 = new BasicSymmetricalDistanceMatrix( 20 );
             m3.setIdentifier( 0, "F_MOUSE" );
             m3.setIdentifier( 1, "11_RAT" );
@@ -2583,8 +2735,19 @@ public class TestPhylogenyReconstruction {
                        18 );
             m3.setRow( "0.010376  0.010376  0.031503  0.000010  0.000010  0.000010  0.000010  0.010375  0.010376  0.010376  0.010376  0.010376  0.010376  0.098678  0.741282  0.000010  0.000010  0.000010  0.000010  0.000000",
                        19 );
-            final NeighborJoiningR nj3 = NeighborJoiningR.createInstance( true, 6 );
+            final NeighborJoiningR nj3 = NeighborJoiningR.createInstance( false, 6 );
             final Phylogeny p3 = nj3.execute( m3 );
+            //Archaeopteryx.createApplication( p3 );
+            ////
+            final int size = 10;
+            for( int n = 0; n <= 100; ++n ) {
+                final NeighborJoiningR njn = NeighborJoiningR.createInstance( false, 6 );
+                final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( size );
+                mt.randomize( new Date().getTime() );
+                final long start_time = new Date().getTime();
+                njn.execute( mt );
+                System.out.println( "Size: " + size + " -> " + ( new Date().getTime() - start_time ) + "ms" );
+            }
         }
         catch ( final Exception e ) {
             e.printStackTrace( System.out );
@@ -2955,7 +3118,7 @@ public class TestPhylogenyReconstruction {
 
     private static void timeNeighborJoining() {
         final NeighborJoiningR njr = NeighborJoiningR.createInstance();
-        for( int n = 3; n <= 9; ++n ) {
+        for( int n = 3; n <= 10; ++n ) {
             final int x = ( int ) Math.pow( 2, n );
             final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
             mt.randomize( new Date().getTime() );
@@ -2964,7 +3127,7 @@ public class TestPhylogenyReconstruction {
             System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
         }
         final NeighborJoiningF njf = NeighborJoiningF.createInstance();
-        for( int n = 3; n <= 9; ++n ) {
+        for( int n = 3; n <= 10; ++n ) {
             final int x = ( int ) Math.pow( 2, n );
             final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
             mt.randomize( new Date().getTime() );
@@ -2973,7 +3136,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 <= 9; ++n ) {
+        for( int n = 3; n <= 10; ++n ) {
             final int x = ( int ) Math.pow( 2, n );
             final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
             mt.randomize( new Date().getTime() );