From 8a5da5060358c86632b0ed74bdc38a7449768cb6 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Mon, 10 Mar 2014 23:10:10 +0000 Subject: [PATCH] inprogress --- .../evoinference/TestPhylogenyReconstruction.java | 60 +++----------------- .../evoinference/distance/NeighborJoiningR.java | 21 ++++--- .../src/org/forester/evoinference/distance/S.java | 51 ++++++++++------- 3 files changed, 54 insertions(+), 78 deletions(-) diff --git a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java index 534fe21..b9d2bb1 100644 --- a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java +++ b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java @@ -2248,10 +2248,7 @@ public class TestPhylogenyReconstruction { 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( 7, 8, 0 ); s0.addPairing( 4, 55, 0 ); s0.addPairing( 2, 3, 0 ); s0.addPairing( 4, 5, 0 ); @@ -2265,7 +2262,7 @@ public class TestPhylogenyReconstruction { if ( s0.size() != 1 ) { return false; } - if ( s0.getS( 0 ).size() != 10 ) { + if ( s0.getS( 0 ).size() != 8 ) { return false; } if ( s0.getValues( 0, 0 ).size() != 1 ) { @@ -2289,13 +2286,7 @@ public class TestPhylogenyReconstruction { 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 ) { + if ( s0.getValues( 7, 0 ).size() != 1 ) { return false; } if ( !s0.getValues( 0, 0 ).contains( 1 ) ) { @@ -2328,15 +2319,15 @@ public class TestPhylogenyReconstruction { if ( s0.getValues( 5, 0 ).size() != 1 ) { return false; } - if ( s0.getS( 0 ).size() != 10 ) { + if ( s0.getS( 0 ).size() != 8 ) { return false; } s0.removePairing( 5, 6, 0 ); - if ( s0.getS( 0 ).size() != 9 ) { + if ( s0.getS( 0 ).size() != 7 ) { return false; } s0.addPairing( 5, 6, 0 ); - if ( s0.getS( 0 ).size() != 10 ) { + if ( s0.getS( 0 ).size() != 8 ) { return false; } if ( s0.getValues( 5, 0 ).size() != 1 ) { @@ -2352,9 +2343,9 @@ public class TestPhylogenyReconstruction { 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 ) { + s0.addPairing( 693, 100, 0 ); + s0.addPairing( 693, 101, 0 ); + if ( s0.getValues( 693, 0 ).size() != 2 ) { return false; } s0.addPairing( 2, 33, 0 ); @@ -2381,39 +2372,6 @@ public class TestPhylogenyReconstruction { 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 ); diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java index 2a851b5..c71ad7d 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java @@ -81,6 +81,9 @@ public final class NeighborJoiningR { final int otu2 = _min_j; System.out.println( _min_i + " " + _min_j + " => " + DF.format( m ) + " (" + DF.format( _d_min ) + ")" ); // It is a condition that otu1 < otu2. + //for( int j = 0; j < _s.size(); ++j ) { + _s.removePairing( _d_min, _min_i, 1 ); + // } final PhylogenyNode node = new PhylogenyNode(); final double d = getDvalue( otu1, otu2 ); final double d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) ); @@ -145,7 +148,9 @@ public final class NeighborJoiningR { } private final void updateDvalue( final int otu1, final int otu2, final int i, final double d ) { - setDvalue( otu1, i, ( getDvalue( otu1, i ) + getDvalue( i, otu2 ) - d ) / 2 ); + final double new_d = ( getDvalue( otu1, i ) + getDvalue( i, otu2 ) - d ) / 2; + _s.addPairing( new_d, otu1, i ); + setDvalue( otu1, i, new_d ); } private void setDvalue( final int i, final int j, final double d ) { @@ -247,10 +252,10 @@ public final class NeighborJoiningR { System.out.print( " " ); } System.out.print( " " ); - for( final Entry> entry : _s.getSentrySet( _mappings[ j ] ) ) { + for( final Entry> entry : _s.getSentrySet( _mappings[ j ] ) ) { final double key = entry.getKey(); final SortedSet value = entry.getValue(); - System.out.print( DF.format( key ) + "=" ); + System.out.print( DF.format( key / S.FACTOR ) + "=" ); boolean first = true; for( final Integer v : value ) { if ( !first ) { @@ -277,17 +282,17 @@ public final class NeighborJoiningR { final int m_j = _mappings[ j ]; int counter = 0; int counter_all = 0; - X: for( final Entry> entry : _s.getSentrySet( m_j ) ) { + for( final Entry> entry : _s.getSentrySet( m_j ) ) { for( final int sorted_i : entry.getValue() ) { //if ( counter_all >= j ) { // break X; //} if ( _mappings[ counter ] == counter_all ) { System.out.print( sorted_i + " " ); - System.out.print( "(" + DF.format( getDvalueUnmapped( sorted_i, m_j ) ) + ") " ); - final double m = getDvalueUnmapped( sorted_i, m_j ) - ( ( _r[ sorted_i ] + r_j ) / n_minus_2 ); - if ( m < min ) { - _d_min = getDvalueUnmapped( sorted_i, m_j ); + System.out.print( "(" + DF.format( getDvalue( sorted_i, j ) ) + ") " ); + final double m = getDvalue( sorted_i, j ) - ( ( _r[ sorted_i ] + r_j ) / n_minus_2 ); + if ( ( m < min ) && ( sorted_i != j ) ) { + _d_min = getDvalue( sorted_i, j ); min = m; _min_i = sorted_i; _min_j = j; diff --git a/forester/java/src/org/forester/evoinference/distance/S.java b/forester/java/src/org/forester/evoinference/distance/S.java index 8f3e994..842f59a 100644 --- a/forester/java/src/org/forester/evoinference/distance/S.java +++ b/forester/java/src/org/forester/evoinference/distance/S.java @@ -15,31 +15,36 @@ import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix; public final class S { - private final static boolean DEBUG = true; - private final List>> _data; + public final static int FACTOR = 1000000; + private final static boolean DEBUG = true; + private final List>> _data; public S() { - _data = new ArrayList>>(); + _data = new ArrayList>>(); } final public void addPairing( final double key, final int value, final int j ) { + addPairing( ( int ) ( FACTOR * key ), value, getS( j ) ); + } + + final public void addPairing( final int key, final int value, final int j ) { addPairing( key, value, getS( j ) ); } - final public SortedMap> getS( final int j ) { + final public SortedMap> getS( final int j ) { return _data.get( j ); } - final public SortedSet getValues( final double key, final int j ) { + final public SortedSet getValues( final int key, final int j ) { return getS( j ).get( key ); } final public void initialize( final BasicSymmetricalDistanceMatrix d ) { for( int j = 0; j < d.getSize(); ++j ) { - final TreeMap> map = new TreeMap>(); + final TreeMap> map = new TreeMap>(); _data.add( map ); for( int i = 0; i < j; ++i ) { - addPairing( d.getValues()[ i ][ j ], i, map ); + addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map ); } } System.out.println( toString() ); @@ -47,18 +52,28 @@ public final class S { final public void initialize( final int size ) { for( int j = 0; j < size; ++j ) { - final TreeMap> map = new TreeMap>(); + 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 ); + removePairing( ( int ) ( key * FACTOR ), value, j ); + } + + final public void removePairing( final int key, final int value, final int j ) { + final SortedMap> m = _data.get( j ); final SortedSet x = m.get( key ); + if ( DEBUG ) { + if ( x == null ) { + System.out.println( toString() ); + throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j ); + } + } if ( x.size() == 1 ) { if ( DEBUG ) { if ( !x.contains( value ) ) { - throw new IllegalArgumentException( "pairing " + key + " -> " + value + " does not exist for row " + throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row " + j ); } } @@ -67,7 +82,7 @@ public final class S { else if ( x.size() > 1 ) { if ( DEBUG ) { if ( !x.remove( value ) ) { - throw new IllegalArgumentException( "pairing " + key + " -> " + value + throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist (could not be removed) for row " + j ); } } @@ -95,12 +110,12 @@ public final class S { 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 ) ) { + for( final Entry> entry : getSentrySet( j ) ) { final double key = entry.getKey(); final SortedSet values = entry.getValue(); - sb.append( df.format( key ) + "->" ); + sb.append( df.format( key / FACTOR ) + "->" ); boolean first = true; - for( final Integer v : values ) { + for( final int v : values ) { if ( !first ) { sb.append( "," ); } @@ -114,13 +129,11 @@ public final class S { return sb.toString(); } - final Set>> getSentrySet( final int j ) { + final Set>> getSentrySet( final int j ) { return getS( j ).entrySet(); } - final private static void addPairing( final double key, - final int value, - final SortedMap> m ) { + final private static void addPairing( final int key, final int value, final SortedMap> m ) { if ( !m.containsKey( key ) ) { final TreeSet x = new TreeSet(); x.add( value ); @@ -129,7 +142,7 @@ public final class S { else { if ( DEBUG ) { if ( m.get( key ).contains( value ) ) { - throw new IllegalArgumentException( "pairing " + key + " -> " + value + " already exists" ); + throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" ); } } m.get( key ).add( value ); -- 1.7.10.2