import java.io.StringWriter;
import java.util.Date;
import java.util.List;
+import java.util.SortedSet;
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;
// else {
// System.out.println( " failed." );
// }
+ System.out.println( "S" );
+ if ( testS() ) {
+ System.out.println( " OK." );
+ }
+ else {
+ System.out.println( " failed." );
+ }
System.out.println( "NJR" );
if ( testNeighborJoiningR() ) {
System.out.println( " OK." );
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." );
return true;
}
+ private static boolean testS() {
+ try {
+ final S s0 = new S();
+ s0.initialize( 1 );
+ s0.addPairing( 0, 1, 0 );
+ s0.addPairing( 7.0000001, 8, 0 );
+ s0.addPairing( 7.00000011, 9, 0 );
+ s0.addPairing( 7.000000111, 10, 0 );
+ s0.addPairing( 7.000000111, 101, 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() != 10 ) {
+ 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.0000001, 0 ).size() != 1 ) {
+ return false;
+ }
+ if ( s0.getValues( 7.00000011, 0 ).size() != 1 ) {
+ return false;
+ }
+ if ( s0.getValues( 7.000000111, 0 ).size() != 2 ) {
+ 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() != 10 ) {
+ return false;
+ }
+ s0.removePairing( 5, 6, 0 );
+ if ( s0.getS( 0 ).size() != 9 ) {
+ return false;
+ }
+ s0.addPairing( 5, 6, 0 );
+ if ( s0.getS( 0 ).size() != 10 ) {
+ 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.539324, 100, 0 );
+ s0.addPairing( 693.539324, 101, 0 );
+ if ( s0.getValues( 693.539324, 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;
+ }
+ if ( a[ 2 ].size() != 3 ) {
+ return false;
+ }
+ if ( a[ 8 ].size() != 1 ) {
+ return false;
+ }
+ if ( !a[ 8 ].contains( 9 ) ) {
+ return false;
+ }
+ if ( a[ 9 ].size() != 2 ) {
+ return false;
+ }
+ if ( !a[ 9 ].contains( 10 ) ) {
+ return false;
+ }
+ if ( !a[ 9 ].contains( 101 ) ) {
+ return false;
+ }
+ if ( !a[ 10 ].contains( 100 ) ) {
+ return false;
+ }
+ if ( !a[ 10 ].contains( 101 ) ) {
+ return false;
+ }
+ if ( a[ 4 ].size() != 2 ) {
+ return false;
+ }
+ if ( !a[ 4 ].contains( 5 ) ) {
+ return false;
+ }
+ if ( !a[ 4 ].contains( 55 ) ) {
+ return false;
+ }
+ }
+ catch ( final Exception e ) {
+ e.printStackTrace( System.out );
+ return false;
+ }
+ return true;
+ }
+
private static boolean testNeighborJoiningR() {
try {
// final NeighborJoiningR nj0 = NeighborJoiningR.createInstance();
import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
-public class S {
+public final class S {
+ private final static boolean DEBUG = true;
private final List<SortedMap<Double, SortedSet<Integer>>> _data;
- S() {
+ public S() {
_data = new ArrayList<SortedMap<Double, SortedSet<Integer>>>();
}
- @Override
- public String toString() {
- final DecimalFormat df = new DecimalFormat( "0.00" );
- final StringBuilder sb = new StringBuilder();
- for( int j = 1; j < size(); ++j ) {
- for( final Entry<Double, SortedSet<Integer>> entry : getSentrySet( j ) ) {
- final double key = entry.getKey();
- final SortedSet<Integer> values = entry.getValue();
- sb.append( df.format( key ) + "->" );
- boolean first = true;
- for( final Integer v : values ) {
- if ( !first ) {
- sb.append( "," );
- }
- first = false;
- sb.append( v );
- }
- sb.append( " " );
- }
- sb.append( "\n" );
- }
- return sb.toString();
- }
-
- void addPairing( final double key, final int value, final int j ) {
- final SortedMap<Double, SortedSet<Integer>> m = getS( j );
- addPairing( key, value, m );
+ final public void addPairing( final double key, final int value, final int j ) {
+ addPairing( key, value, getS( j ) );
}
- SortedMap<Double, SortedSet<Integer>> getS( final int j ) {
+ final public SortedMap<Double, SortedSet<Integer>> getS( final int j ) {
return _data.get( j );
}
- Set<Entry<Double, SortedSet<Integer>>> getSentrySet( final int j ) {
- return getS( j ).entrySet();
+ final public SortedSet<Integer> getValues( final double key, final int j ) {
+ return getS( j ).get( key );
}
- void initialize( final BasicSymmetricalDistanceMatrix d ) {
+ final public void initialize( final BasicSymmetricalDistanceMatrix d ) {
for( int j = 0; j < d.getSize(); ++j ) {
final TreeMap<Double, SortedSet<Integer>> map = new TreeMap<Double, SortedSet<Integer>>();
_data.add( map );
System.out.println( toString() );
}
- void removePairing( final double key, final int value, final int j ) {
+ final public void initialize( final int size ) {
+ for( int j = 0; j < size; ++j ) {
+ final TreeMap<Double, SortedSet<Integer>> map = new TreeMap<Double, SortedSet<Integer>>();
+ _data.add( map );
+ }
+ }
+
+ final public void removePairing( final double key, final int value, final int j ) {
final SortedMap<Double, SortedSet<Integer>> m = _data.get( j );
final SortedSet<Integer> x = m.get( key );
if ( x.size() == 1 ) {
- if ( !x.contains( value ) ) {
- //TODO remove me later
- throw new IllegalStateException( "pairing " + key + " -> " + value + " does not exist" );
+ if ( DEBUG ) {
+ if ( !x.contains( value ) ) {
+ throw new IllegalArgumentException( "pairing " + key + " -> " + value + " does not exist for row "
+ + j );
+ }
}
m.remove( key );
}
else if ( x.size() > 1 ) {
- final boolean removed = x.remove( value );
- if ( !removed ) {
- //TODO remove me later
- throw new IllegalStateException( "pairing " + key + " -> " + value + " does not exist/was not removed" );
+ if ( DEBUG ) {
+ if ( !x.remove( value ) ) {
+ throw new IllegalArgumentException( "pairing " + key + " -> " + value
+ + " does not exist (could not be removed) for row " + j );
+ }
+ }
+ else {
+ x.remove( value );
}
}
- else {
- //TODO remove me later
- throw new IllegalStateException( "empty" );
+ else if ( DEBUG ) {
+ throw new IllegalStateException();
}
}
- int size() {
+ final public int size() {
return _data.size();
}
- private static void addPairing( final double key, final int value, final SortedMap<Double, SortedSet<Integer>> m ) {
+ // Slow, only for testing
+ @SuppressWarnings( "unchecked")
+ final public SortedSet<Integer>[] toArray( final int j ) {
+ return _data.get( j ).values().toArray( new SortedSet[ _data.get( j ).size() ] );
+ }
+
+ @Override
+ final public String toString() {
+ final DecimalFormat df = new DecimalFormat( "0.00" );
+ final StringBuilder sb = new StringBuilder();
+ for( int j = 0; j < size(); ++j ) {
+ for( final Entry<Double, SortedSet<Integer>> entry : getSentrySet( j ) ) {
+ final double key = entry.getKey();
+ final SortedSet<Integer> values = entry.getValue();
+ sb.append( df.format( key ) + "->" );
+ boolean first = true;
+ for( final Integer v : values ) {
+ if ( !first ) {
+ sb.append( "," );
+ }
+ first = false;
+ sb.append( v );
+ }
+ sb.append( " " );
+ }
+ sb.append( "\n" );
+ }
+ return sb.toString();
+ }
+
+ final Set<Entry<Double, SortedSet<Integer>>> getSentrySet( final int j ) {
+ return getS( j ).entrySet();
+ }
+
+ final private static void addPairing( final double key,
+ final int value,
+ final SortedMap<Double, SortedSet<Integer>> m ) {
if ( !m.containsKey( key ) ) {
final TreeSet<Integer> x = new TreeSet<Integer>();
x.add( value );
m.put( key, x );
}
else {
- if ( m.get( key ).contains( value ) ) {
- //TODO remove me later
- throw new IllegalStateException( "pairing " + key + " -> " + value + " already exists" );
+ if ( DEBUG ) {
+ if ( m.get( key ).contains( value ) ) {
+ throw new IllegalArgumentException( "pairing " + key + " -> " + value + " already exists" );
+ }
}
m.get( key ).add( value );
}