From 5ffc54fe497b9b11663f629c8ed924de32d62925 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Wed, 5 Mar 2014 20:49:14 +0000 Subject: [PATCH] inprogress --- .../evoinference/TestPhylogenyReconstruction.java | 4 +-- .../evoinference/distance/NeighborJoining.java | 31 +++++++++----------- .../evoinference/distance/NeighborJoiningF.java | 31 +++++++++----------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java index 39e1a20..e7eb3b1 100644 --- a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java +++ b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java @@ -2579,7 +2579,7 @@ public class TestPhylogenyReconstruction { private static void timeNeighborJoining() { final NeighborJoiningF njf = NeighborJoiningF.createInstance(); - for( int n = 3; n <= 8; ++n ) { + 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() ); @@ -2588,7 +2588,7 @@ public class TestPhylogenyReconstruction { System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" ); } final NeighborJoining nj = NeighborJoining.createInstance(); - for( int n = 3; n <= 8; ++n ) { + 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() ); diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java index c0792a4..3d28ab7 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java @@ -40,11 +40,12 @@ public final class NeighborJoining { private double[][] _d_values; private final DecimalFormat _df; private PhylogenyNode[] _external_nodes; - private double[][] _m_values; private int[] _mappings; private int _n; private double[] _r; private final boolean _verbose; + private int _min_i; + private int _min_j; private NeighborJoining() { _verbose = false; @@ -66,22 +67,11 @@ public final class NeighborJoining { reset( distance ); final Phylogeny phylogeny = new Phylogeny(); while ( _n > 2 ) { - updateM(); // Calculates the minimal distance. // If more than one minimal distances, always the first found is used - // could randomize this, so that any would be returned in a randomized fashion... - double minimum = _m_values[ 0 ][ 1 ]; - int otu1 = 0; - int otu2 = 1; - for( int j = 1; j < _n; ++j ) { - for( int i = 0; i < j; ++i ) { - if ( _m_values[ i ][ j ] < minimum ) { - minimum = _m_values[ i ][ j ]; - otu1 = i; - otu2 = j; - } - } - } + updateM(); + final int otu1 = _min_i; + final int otu2 = _min_j; // It is a condition that otu1 < otu2. final PhylogenyNode node = new PhylogenyNode(); final double d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ]; @@ -216,18 +206,25 @@ public final class NeighborJoining { _r = new double[ _n ]; _mappings = new int[ _n ]; _d_values = _d.getValues(); - _m_values = new double[ _n ][ _n ]; initExternalNodes(); } private final void updateM() { calculateNetDivergences(); + Double min = Double.MAX_VALUE; + _min_i = -1; + _min_j = -1; final int n_minus_2 = _n - 2; 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 ) { - _m_values[ i ][ j ] = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 ); + final double m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 ); + if ( m < min ) { + min = m; + _min_i = i; + _min_j = j; + } } } } diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningF.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningF.java index dd984ae..fc39735 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningF.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningF.java @@ -40,11 +40,12 @@ public final class NeighborJoiningF { private float[][] _d_values; private final DecimalFormat _df; private PhylogenyNode[] _external_nodes; - private float[][] _m_values; private int[] _mappings; private int _n; private float[] _r; private final boolean _verbose; + private int _min_i; + private int _min_j; private NeighborJoiningF() { _verbose = false; @@ -66,22 +67,11 @@ public final class NeighborJoiningF { reset( distance ); final Phylogeny phylogeny = new Phylogeny(); while ( _n > 2 ) { - updateM(); // Calculates the minimal distance. // If more than one minimal distances, always the first found is used - // could randomize this, so that any would be returned in a randomized fashion... - double minimum = _m_values[ 0 ][ 1 ]; - int otu1 = 0; - int otu2 = 1; - for( int j = 1; j < _n; ++j ) { - for( int i = 0; i < j; ++i ) { - if ( _m_values[ i ][ j ] < minimum ) { - minimum = _m_values[ i ][ j ]; - otu1 = i; - otu2 = j; - } - } - } + updateM(); + final int otu1 = _min_i; + final int otu2 = _min_j; // It is a condition that otu1 < otu2. final PhylogenyNode node = new PhylogenyNode(); final float d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ]; @@ -220,18 +210,25 @@ public final class NeighborJoiningF { _d_values[ i ][ j ] = ( float ) distances.getValue( i, j ); } } - _m_values = new float[ _n ][ _n ]; initExternalNodes(); } private final void updateM() { calculateNetDivergences(); final int n_minus_2 = _n - 2; + float min = Float.MAX_VALUE; + _min_i = -1; + _min_j = -1; for( int j = 1; j < _n; ++j ) { final float r_j = _r[ j ]; final int m_j = _mappings[ j ]; for( int i = 0; i < j; ++i ) { - _m_values[ i ][ j ] = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 ); + final float m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 ); + if ( m < min ) { + min = m; + _min_i = i; + _min_j = j; + } } } } -- 1.7.10.2