From 057e99cdcd1535dd6f7833048cbe1bebf554f0a4 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Thu, 13 Mar 2014 01:58:50 +0000 Subject: [PATCH] inprogress --- .../evoinference/TestPhylogenyReconstruction.java | 28 ++++++++++++++++++++ .../evoinference/distance/NeighborJoining.java | 17 ++++++++++++ .../evoinference/distance/NeighborJoiningR.java | 21 +++++++++++---- .../src/org/forester/evoinference/distance/S.java | 28 ++++++++++---------- 4 files changed, 75 insertions(+), 19 deletions(-) diff --git a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java index b9d2bb1..2fe3a4c 100644 --- a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java +++ b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java @@ -2480,6 +2480,25 @@ 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 ); + final NeighborJoining nj = NeighborJoining.createInstance( true, 6 ); + //nj = NeighborJoining.createInstance( true, 6 ); + final Phylogeny pnj = nj.execute( m ); + // + 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 ); final NeighborJoiningR nj2 = NeighborJoiningR.createInstance( true, 6 ); //nj = NeighborJoining.createInstance( true, 6 ); final Phylogeny p2 = nj2.execute( m ); @@ -2895,6 +2914,15 @@ public class TestPhylogenyReconstruction { } private static void timeNeighborJoining() { + final NeighborJoiningR njr = NeighborJoiningR.createInstance(); + for( int n = 3; n <= 9; ++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 <= 9; ++n ) { final int x = ( int ) Math.pow( 2, n ); diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java index 1bce7bc..14553ce 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java @@ -36,6 +36,7 @@ import org.forester.util.ForesterUtil; public final class NeighborJoining { + private final static DecimalFormat DF = new DecimalFormat( "0.00000" ); private BasicSymmetricalDistanceMatrix _d; private double[][] _d_values; private final DecimalFormat _df; @@ -242,12 +243,28 @@ public final class NeighborJoining { } } } + for( int j = 1; j < _n; ++j ) { + final double r_j = _r[ j ]; + final int m_j = _mappings[ j ]; + for( int i = 0; i < j; ++i ) { + System.out.print( i ); + System.out.print( "->" ); + System.out.print( DF.format( _r[ i ] ) ); + System.out.print( " " ); + } + System.out.println(); + } } // otu2 will, in effect, be "deleted" from the matrix. private final void updateMappings( final int otu2 ) { for( int i = otu2; i < ( _mappings.length - 1 ); ++i ) { + System.out.print( _mappings[ i ] ); _mappings[ i ] = _mappings[ i + 1 ]; + System.out.println( "----->" + _mappings[ i ] ); + } + for( int i = 0; i < _mappings.length; ++i ) { + System.out.println( i + "-->" + _mappings[ i ] ); } } diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java index bdc80be..0db013a 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java @@ -81,10 +81,11 @@ 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. - System.out.println( "mapped 1 " + _mappings[ otu1 ] ); - System.out.println( "mapped 2 " + _mappings[ otu2 ] ); + //System.out.println( "mapped 1 " + _mappings[ otu1 ] ); + System.out.println( "mapped otu 2 " + _mappings[ otu2 ] ); final PhylogenyNode node = new PhylogenyNode(); - final double d = getDvalueUnmapped( otu1, _mappings[ otu2 ] ); + //final double d = getDvalueUnmapped( otu1, _mappings[ otu2 ] ); + final double d = _d_values[ otu1 ][ _mappings[ otu2 ] ]; final double d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) ); final double d2 = d - d1; if ( _df == null ) { @@ -104,7 +105,8 @@ public final class NeighborJoiningR { System.out.println( "otu1=" + otu1 ); System.out.println( "otu2=" + otu2 ); calculateDistancesFromNewNode( otu1, otu2, d ); - _external_nodes[ _mappings[ otu1 ] ] = node; + // _external_nodes[ _mappings[ otu1 ] ] = node; + _external_nodes[ otu1 ] = node; updateMappings( otu2 ); --_n; System.out.println( "" ); @@ -313,7 +315,7 @@ public final class NeighborJoiningR { 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_m ) && ( sorted_i != j ) ) { + if ( ( m < min_m ) ) { _d_min = getDvalueUnmapped( sorted_i, m_j ); min_m = m; _min_i = sorted_i; @@ -322,6 +324,15 @@ public final class NeighborJoiningR { } } System.out.println(); + for( final Entry> entry : _s.getSentrySet( m_j ) ) { + for( final int sorted_i : entry.getValue() ) { + System.out.print( sorted_i ); + System.out.print( "->" ); + System.out.print( DF.format( _r[ sorted_i ] ) ); + System.out.print( " " ); + } + } + System.out.println(); /* for( int i = 0; i < j; ++i ) { final double m = getDvalue( i, j ) - ( ( _r[ i ] + r_j ) / n_minus_2 ); diff --git a/forester/java/src/org/forester/evoinference/distance/S.java b/forester/java/src/org/forester/evoinference/distance/S.java index f1d94db..fe70cd6 100644 --- a/forester/java/src/org/forester/evoinference/distance/S.java +++ b/forester/java/src/org/forester/evoinference/distance/S.java @@ -73,20 +73,20 @@ public final class S { throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j ); } } - // if ( x.size() == 1 ) { - // if ( DEBUG ) { - // if ( !x.contains( value ) ) { - // System.out.println(); - // System.out - // .println( "________________________________________________________________________________________" ); - // System.out.println( toString() ); - // throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row " - // + j ); - // } - // } - // m.remove( key ); - // } - else if ( x.size() >= 1 ) { + if ( x.size() == 1 ) { + if ( DEBUG ) { + if ( !x.contains( value ) ) { + System.out.println(); + System.out + .println( "________________________________________________________________________________________" ); + System.out.println( toString() ); + throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row " + + j ); + } + } + m.remove( key ); + } + else if ( x.size() > 1 ) { if ( DEBUG ) { if ( !x.remove( value ) ) { throw new IllegalArgumentException( "pairing " + key + "->" + value -- 1.7.10.2