From 64040069224344037891a9fe348ac5d3c88590fb Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Mon, 10 Mar 2014 20:31:10 +0000 Subject: [PATCH] inprogress --- .../evoinference/TestPhylogenyReconstruction.java | 194 ++++++++++++++++++++ .../src/org/forester/evoinference/distance/S.java | 123 ++++++++----- 2 files changed, 268 insertions(+), 49 deletions(-) diff --git a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java index b525c29..534fe21 100644 --- a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java +++ b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java @@ -32,11 +32,13 @@ import java.io.FileInputStream; 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; @@ -74,6 +76,13 @@ public class TestPhylogenyReconstruction { // 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." ); @@ -109,6 +118,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." ); @@ -2228,6 +2243,185 @@ 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.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[] 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(); diff --git a/forester/java/src/org/forester/evoinference/distance/S.java b/forester/java/src/org/forester/evoinference/distance/S.java index e1593f4..8f3e994 100644 --- a/forester/java/src/org/forester/evoinference/distance/S.java +++ b/forester/java/src/org/forester/evoinference/distance/S.java @@ -13,52 +13,28 @@ import java.util.TreeSet; import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix; -public class S { +public final class S { + private final static boolean DEBUG = true; private final List>> _data; - S() { + public S() { _data = new ArrayList>>(); } - @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> entry : getSentrySet( j ) ) { - final double key = entry.getKey(); - final SortedSet 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> 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> getS( final int j ) { + final public SortedMap> getS( final int j ) { return _data.get( j ); } - Set>> getSentrySet( final int j ) { - return getS( j ).entrySet(); + final public SortedSet 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> map = new TreeMap>(); _data.add( map ); @@ -69,43 +45,92 @@ public class S { 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> map = new TreeMap>(); + _data.add( map ); + } + } + + final public void removePairing( final double key, final int value, final int j ) { final SortedMap> m = _data.get( j ); final SortedSet 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> m ) { + // Slow, only for testing + @SuppressWarnings( "unchecked") + final public SortedSet[] 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> entry : getSentrySet( j ) ) { + final double key = entry.getKey(); + final SortedSet 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>> getSentrySet( final int j ) { + return getS( j ).entrySet(); + } + + final private static void addPairing( final double key, + final int value, + final SortedMap> m ) { if ( !m.containsKey( key ) ) { final TreeSet x = new TreeSet(); 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 ); } -- 1.7.10.2