Changed more concrete DistanceMatrix references to reference interface
[jalview.git] / forester / java / src / org / forester / evoinference / TestPhylogenyReconstruction.java
index 8f87d78..a9c16e9 100644 (file)
@@ -23,7 +23,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.evoinference;
 
@@ -32,9 +32,14 @@ import java.io.FileInputStream;
 import java.io.StringWriter;
 import java.util.Date;
 import java.util.List;
+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.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;
@@ -55,13 +60,45 @@ import org.forester.util.ForesterUtil;
 public class TestPhylogenyReconstruction {
 
     private final static double  ZERO_DIFF = 1.0E-9;
-    private final static boolean TIME      = false;
+    private final static boolean VERBOSE   = false;
 
     public static boolean isEqual( final double a, final double b ) {
         return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
     }
 
+    public static boolean isUnequal( final double a, final double b ) {
+        return !isEqual( a, b );
+    }
+
     public static void main( final String[] args ) {
+        System.out.println( "NJ" );
+        if ( testNeighborJoining( VERBOSE ) ) {
+            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( "Sarray" );
+        if ( testSarray() ) {
+            System.out.println( "  OK." );
+        }
+        else {
+            System.out.println( "  failed." );
+        }
+        System.out.println( "NJR" );
+        if ( testNeighborJoiningR() ) {
+            System.out.println( "  OK." );
+        }
+        else {
+            System.out.println( "  failed." );
+        }
         timeNeighborJoining();
     }
 
@@ -90,8 +127,14 @@ 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() ) {
+        if ( !testNeighborJoining( VERBOSE ) ) {
             System.out.println( "failed." );
             return false;
         }
@@ -117,27 +160,6 @@ public class TestPhylogenyReconstruction {
         return true;
     }
 
-    private static boolean testDistanceCalculationMethods( final File test_dir ) {
-        try {
-            final Msa msa0 = GeneralMsaParser.parse( new FileInputStream( test_dir + ForesterUtil.FILE_SEPARATOR
-                    + "bcl.aln" ) );
-            final BasicSymmetricalDistanceMatrix pwd0 = PairwiseDistanceCalculator.calcKimuraDistances( msa0 );
-            if ( pwd0.getSize() != 120 ) {
-                return false;
-            }
-            for( int i = 0; i < pwd0.getSize(); ++i ) {
-                if ( !isEqual( pwd0.getValue( i, i ), 0.0 ) ) {
-                    return false;
-                }
-            }
-        }
-        catch ( final Exception e ) {
-            e.printStackTrace( System.out );
-            return false;
-        }
-        return true;
-    }
-
     private static boolean testBasicCharacterStateMatrix() {
         try {
             final CharacterStateMatrix<String> matrix_0 = new BasicCharacterStateMatrix<String>( 4, 8 );
@@ -413,6 +435,27 @@ public class TestPhylogenyReconstruction {
         return true;
     }
 
+    private static boolean testDistanceCalculationMethods( final File test_dir ) {
+        try {
+            final Msa msa0 = GeneralMsaParser.parseMsa( new FileInputStream( test_dir + ForesterUtil.FILE_SEPARATOR
+                    + "bcl.aln" ) );
+            final DistanceMatrix pwd0 = PairwiseDistanceCalculator.calcKimuraDistances( msa0 );
+            if ( pwd0.getSize() != 120 ) {
+                return false;
+            }
+            for( int i = 0; i < pwd0.getSize(); ++i ) {
+                if ( !isEqual( pwd0.getValue( i, i ), 0.0 ) ) {
+                    return false;
+                }
+            }
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
+
     private static boolean testDolloParsimony() {
         try {
             final BinaryStates PRESENT = BinaryStates.PRESENT;
@@ -1910,8 +1953,66 @@ public class TestPhylogenyReconstruction {
         return true;
     }
 
-    private static boolean testNeighborJoining() {
+    private static boolean testNeighborJoining( final boolean verbose ) {
         try {
+            NeighborJoining nj = NeighborJoining.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 = nj.execute( m0 );
+            p0.reRoot( p0.getNode( "D" ) );
+            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;
+            }
+            nj = NeighborJoining.createInstance();
+            final BasicSymmetricalDistanceMatrix m00 = new BasicSymmetricalDistanceMatrix( 4 );
+            m00.setIdentifier( 0, "A" );
+            m00.setIdentifier( 1, "B" );
+            m00.setIdentifier( 2, "C" );
+            m00.setIdentifier( 3, "D" );
+            m00.setRow( "2.01 ", 1 );
+            m00.setRow( "3 3.01 ", 2 );
+            m00.setRow( "3.01 3.02 1.01", 3 );
+            final Phylogeny p00 = nj.execute( m00 );
+            p00.reRoot( p00.getNode( "D" ) );
+            if ( isUnequal( p00.getNode( "A" ).getDistanceToParent(), 1 ) ) {
+                return false;
+            }
+            if ( isUnequal( p00.getNode( "B" ).getDistanceToParent(), 1.01 ) ) {
+                return false;
+            }
+            if ( isUnequal( p00.getNode( "C" ).getDistanceToParent(), 0.5 ) ) {
+                return false;
+            }
+            if ( isUnequal( p00.getNode( "D" ).getDistanceToParent(), 0.255 ) ) {
+                return false;
+            }
+            if ( isUnequal( p00.getNode( "A" ).getParent().getDistanceToParent(), 1.5 ) ) {
+                return false;
+            }
+            if ( isUnequal( p00.getNode( "A" ).getParent().getParent().getDistanceToParent(), 0.255 ) ) {
+                return false;
+            }
             BasicSymmetricalDistanceMatrix m = new BasicSymmetricalDistanceMatrix( 6 );
             m.setRow( "5", 1 );
             m.setRow( "4 7", 2 );
@@ -1924,8 +2025,45 @@ public class TestPhylogenyReconstruction {
             m.setIdentifier( 3, "D" );
             m.setIdentifier( 4, "E" );
             m.setIdentifier( 5, "F" );
-            final NeighborJoining nj = NeighborJoining.createInstance();
-            nj.execute( m );
+            nj = NeighborJoining.createInstance();
+            final Phylogeny p1 = nj.execute( m );
+            p1.reRoot( p1.getNode( "F" ) );
+            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" );
@@ -1941,7 +2079,52 @@ 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.execute( m );
+            //NeighborJoiningR njr = NeighborJoiningR.createInstance( true, 6 );
+            nj = NeighborJoining.createInstance( verbose, 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;
+            }
+            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;
+            }
             m = new BasicSymmetricalDistanceMatrix( 4 );
             m.setIdentifier( 0, "A" );
             m.setIdentifier( 1, "B" );
@@ -1952,29 +2135,619 @@ public class TestPhylogenyReconstruction {
             m.setRow( "0.17 1.02 0.00 1.01", 2 );
             m.setRow( "0.98 1.83 1.01 0.00", 3 );
             final Phylogeny p3 = nj.execute( m );
+            p3.reRoot( p3.getNode( "C" ) );
+            if ( isUnequal( p3.getNode( "A" ).getDistanceToParent(), 0.05 ) ) {
+                return false;
+            }
+            if ( isUnequal( p3.getNode( "B" ).getDistanceToParent(), 0.90 ) ) {
+                return false;
+            }
+            if ( !isEqual( p3.getNode( "C" ).getDistanceToParent(), 0.05 ) ) {
+                return false;
+            }
+            if ( !isEqual( p3.getNode( "D" ).getDistanceToParent(), 0.91 ) ) {
+                return false;
+            }
+            if ( isUnequal( p3.getNode( "A" ).getParent().getDistanceToParent(), 0.02 ) ) {
+                return false;
+            }
+            if ( isUnequal( p3.getNode( "A" ).getParent().getParent().getDistanceToParent(), 0.05 ) ) {
+                return false;
+            }
             //
-            // -- A 0.05
-            // - |0.01
-            // ----------------------- B 0.90
+            NeighborJoiningF njf = NeighborJoiningF.createInstance();
+            final BasicSymmetricalDistanceMatrix m0f = new BasicSymmetricalDistanceMatrix( 4 );
+            m0f.setIdentifier( 0, "A" );
+            m0f.setIdentifier( 1, "B" );
+            m0f.setIdentifier( 2, "C" );
+            m0f.setIdentifier( 3, "D" );
+            m0f.setRow( "5 ", 1 );
+            m0f.setRow( "3 6 ", 2 );
+            m0f.setRow( "7.5 10.5 5.5", 3 );
+            final Phylogeny p0f = njf.execute( m0f );
+            p0f.reRoot( p0f.getNode( "D" ) );
+            if ( isUnequal( p0f.getNode( "A" ).getDistanceToParent(), 1 ) ) {
+                return false;
+            }
+            if ( isUnequal( p0f.getNode( "B" ).getDistanceToParent(), 4 ) ) {
+                return false;
+            }
+            if ( isUnequal( p0f.getNode( "C" ).getDistanceToParent(), 0.5 ) ) {
+                return false;
+            }
+            if ( isUnequal( p0f.getNode( "D" ).getDistanceToParent(), 2.5 ) ) {
+                return false;
+            }
+            if ( isUnequal( p0f.getNode( "A" ).getParent().getDistanceToParent(), 1.5 ) ) {
+                return false;
+            }
+            if ( isUnequal( p0f.getNode( "A" ).getParent().getParent().getDistanceToParent(), 2.5 ) ) {
+                return false;
+            }
             //
-            // --- C 0.10
-            // - |0.01
-            // ------------------------- D 0.91
-            p3.reRoot( p3.getNode( "C" ).getParent() );
-            if ( !isEqual( p3.getNode( "A" ).getDistanceToParent(), 0.05 ) ) {
+            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 );
+            njf = NeighborJoiningF.createInstance( verbose, 5 );
+            final Phylogeny p2f = njf.execute( m );
+            p2f.reRoot( p2f.getNode( "Bovine" ) );
+            if ( isUnequal( p2f.getNode( "Chimp" ).getDistanceToParent(), 0.15168 ) ) {
                 return false;
             }
-            if ( !isEqual( p3.getNode( "B" ).getDistanceToParent(), 0.90 ) ) {
+            if ( isUnequal( p2f.getNode( "Human" ).getDistanceToParent(), 0.11752 ) ) {
                 return false;
             }
-            if ( !isEqual( p3.getNode( "C" ).getDistanceToParent(), 0.10 ) ) {
+            if ( isUnequal( p2f.getNode( "Gorilla" ).getDistanceToParent(), 0.15393 ) ) {
                 return false;
             }
-            if ( !isEqual( p3.getNode( "D" ).getDistanceToParent(), 0.91 ) ) {
+            if ( isUnequal( p2f.getNode( "Orang" ).getDistanceToParent(), 0.28469 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Gibbon" ).getDistanceToParent(), 0.35793 ) ) {
                 return false;
             }
-            if ( TIME ) {
-                timeNeighborJoining();
+            if ( isUnequal( p2f.getNode( "Mouse" ).getDistanceToParent(), 0.76891 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Bovine" ).getDistanceToParent(), 0.458845 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getDistanceToParent(), 0.03982 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Human" ).getParent().getDistanceToParent(), 0.03982 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getParent().getDistanceToParent(), 0.02696 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getParent().getParent().getDistanceToParent(), 0.04648 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.getNode( "Chimp" ).getParent().getParent().getParent().getParent()
+                    .getDistanceToParent(), 0.42027 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2f.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 testS() {
+        try {
+            final Sset s0 = new Sset();
+            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 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 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();
+            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" ) );
+            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;
+            }
+            final BasicSymmetricalDistanceMatrix m1 = new BasicSymmetricalDistanceMatrix( 6 );
+            m1.setRow( "5", 1 );
+            m1.setRow( "4 7", 2 );
+            m1.setRow( "7 10 7", 3 );
+            m1.setRow( "6 9 6 5", 4 );
+            m1.setRow( "8 11 8 9 8", 5 );
+            m1.setIdentifier( 0, "A" );
+            m1.setIdentifier( 1, "B" );
+            m1.setIdentifier( 2, "C" );
+            m1.setIdentifier( 3, "D" );
+            m1.setIdentifier( 4, "E" );
+            m1.setIdentifier( 5, "F" );
+            final NeighborJoiningR nj1 = NeighborJoiningR.createInstance();
+            final Phylogeny p1 = nj1.execute( m1 );
+            p1.reRoot( p1.getNode( "F" ) );
+            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;
+            }
+            final BasicSymmetricalDistanceMatrix m2 = new BasicSymmetricalDistanceMatrix( 7 );
+            m2.setIdentifier( 0, "Bovine" );
+            m2.setIdentifier( 1, "Mouse" );
+            m2.setIdentifier( 2, "Gibbon" );
+            m2.setIdentifier( 3, "Orang" );
+            m2.setIdentifier( 4, "Gorilla" );
+            m2.setIdentifier( 5, "Chimp" );
+            m2.setIdentifier( 6, "Human" );
+            m2.setRow( "0.00000 1.68660 1.71980 1.66060 1.52430 1.60430 1.59050", 0 );
+            m2.setRow( "1.68660 0.00000 1.52320 1.48410 1.44650 1.43890 1.46290", 1 );
+            m2.setRow( "1.71980 1.52320 0.00000 0.71150 0.59580 0.61790 0.55830", 2 );
+            m2.setRow( "1.66060 1.48410 0.71150 0.00000 0.46310 0.50610 0.47100", 3 );
+            m2.setRow( "1.52430 1.44650 0.59580 0.46310 0.00000 0.34840 0.30830", 4 );
+            m2.setRow( "1.60430 1.43890 0.61790 0.50610 0.34840 0.00000 0.26920", 5 );
+            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 ) ) {
+                return false;
+            }
+            if ( isUnequal( p2.getNode( "Gorilla" ).getDistanceToParent(), 0.153931 ) ) {
+                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;
+            }
+            //
+            // System.exit( 1 );
+            final BasicSymmetricalDistanceMatrix m3 = new BasicSymmetricalDistanceMatrix( 20 );
+            m3.setIdentifier( 0, "F_MOUSE" );
+            m3.setIdentifier( 1, "11_RAT" );
+            m3.setIdentifier( 2, "A_CAVPO" );
+            m3.setIdentifier( 3, "D_HUMAN" );
+            m3.setIdentifier( 4, "E_HUMAN" );
+            m3.setIdentifier( 5, "F_HUMAN" );
+            m3.setIdentifier( 6, "C_HUMAN" );
+            m3.setIdentifier( 7, "6_FELCA" );
+            m3.setIdentifier( 8, "D_MOUSE" );
+            m3.setIdentifier( 9, "E_MOUSE" );
+            m3.setIdentifier( 10, "E_RAT " );
+            m3.setIdentifier( 11, "C_MOUSE" );
+            m3.setIdentifier( 12, "10_RAT" );
+            m3.setIdentifier( 13, "3_TAEGU" );
+            m3.setIdentifier( 14, "2_SACKO" );
+            m3.setIdentifier( 15, "2_PANTR" );
+            m3.setIdentifier( 16, "3_CANFA" );
+            m3.setIdentifier( 17, "9_HUMAN" );
+            m3.setIdentifier( 18, "A_HUMAN" );
+            m3.setIdentifier( 19, "B_HUMAN" );
+            m3.setRow( "0.000000  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000010  0.000010  0.000010  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376 ",
+                       0 );
+            m3.setRow( "0.000010  0.000000  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000010  0.000010  0.000010  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       1 );
+            m3.setRow( "0.020875  0.020875  0.000000  0.031503  0.031503  0.031503  0.031503  0.031477  0.020875  0.020875  0.020875  0.020875  0.020875  0.096983  0.768150  0.031503  0.031503  0.031503  0.031503  0.031503",
+                       2 );
+            m3.setRow( "0.010376  0.010376  0.031503  0.000000  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.000010",
+                       3 );
+            m3.setRow( "0.010376  0.010376  0.031503  0.000010  0.000000  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.000010",
+                       4 );
+            m3.setRow( "0.010376  0.010376  0.031503  0.000010  0.000010  0.000000  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.000010",
+                       5 );
+            m3.setRow( "0.010376  0.010376  0.031503  0.000010  0.000010  0.000010  0.000000  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.000010",
+                       6 );
+            m3.setRow( "0.010368  0.010368  0.031477  0.010375  0.010375  0.010375  0.010375  0.000000  0.010368  0.010368  0.010368  0.010368  0.010368  0.098591  0.745047  0.010375  0.010375  0.010375  0.010375  0.010375",
+                       7 );
+            m3.setRow( "0.000010  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000000  0.000010  0.000010  0.000010  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       8 );
+            m3.setRow( "0.000010  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000000  0.000010  0.000010  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       9 );
+            m3.setRow( "0.000010  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000010  0.000000  0.000010  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       10 );
+            m3.setRow( "0.000010  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000010  0.000010  0.000000  0.000010  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       11 );
+            m3.setRow( "0.000010  0.000010  0.020875  0.010376  0.010376  0.010376  0.010376  0.010368  0.000010  0.000010  0.000010  0.000010  0.000000  0.087165  0.743570  0.010376  0.010376  0.010376  0.010376  0.010376",
+                       12 );
+            m3.setRow( "0.087165  0.087165  0.096983  0.098678  0.098678  0.098678  0.098678  0.098591  0.087165  0.087165  0.087165  0.087165  0.087165  0.000000  0.720387  0.098678  0.098678  0.098678  0.098678  0.098678",
+                       13 );
+            m3.setRow( "0.743570  0.743570  0.768150  0.741282  0.741282  0.741282  0.741282  0.745047  0.743570  0.743570  0.743570  0.743570  0.743570  0.720387  0.000000  0.741282  0.741282  0.741282  0.741282  0.741282",
+                       14 );
+            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.000000  0.000010  0.000010  0.000010  0.000010",
+                       15 );
+            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.000000  0.000010  0.000010  0.000010",
+                       16 );
+            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.000000  0.000010  0.000010",
+                       17 );
+            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.000000  0.000010",
+                       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( 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 ) {
@@ -2345,18 +3118,32 @@ public class TestPhylogenyReconstruction {
     }
 
     private static void timeNeighborJoining() {
+        final NeighborJoiningR njr = NeighborJoiningR.createInstance();
+        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() );
+            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 <= 10; ++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();
+            njf.execute( mt );
+            System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
+        }
         final NeighborJoining nj = NeighborJoining.createInstance();
-        for( int n = 3; n <= 14; ++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() );
-            //  for( int i = 0; i < mt.getSize(); i++ ) {
-            //      mt.setIdentifier( i, i + "i" );
-            //  }
-            //  System.out.println( mt.toStringBuffer( Format.PHYLIP ) );
             final long start_time = new Date().getTime();
             nj.execute( mt );
-            System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms." );
+            System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
         }
     }
 }