X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fevoinference%2Fdistance%2FNeighborJoining.java;h=14553ce7906d0262f2247dae231674b5ebdcc280;hb=0ccf8b41a889edccde310d55377e1db2351f46e6;hp=c0792a4c10289ea56729b0dd09d4c686b1f29c5f;hpb=083c646bffc9c910714880519a029ef8a38e4942;p=jalview.git diff --git a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java index c0792a4..14553ce 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoining.java @@ -36,15 +36,17 @@ 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; 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 +68,12 @@ 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; + System.out.println( _min_i + " " + _min_j ); // It is a condition that otu1 < otu2. final PhylogenyNode node = new PhylogenyNode(); final double d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ]; @@ -202,10 +194,24 @@ public final class NeighborJoining { } private final void printProgress( final int otu1, final int otu2 ) { - final PhylogenyNode n1 = getExternalPhylogenyNode( otu1 ); - final PhylogenyNode n2 = getExternalPhylogenyNode( otu2 ); - System.out.println( "Node " + ( ForesterUtil.isEmpty( n1.getName() ) ? n1.getId() : n1.getName() ) + " joins " - + ( ForesterUtil.isEmpty( n2.getName() ) ? n2.getId() : n2.getName() ) ); + System.out.println( "Node " + printProgressNodeToString( getExternalPhylogenyNode( otu1 ) ) + " joins " + + ( printProgressNodeToString( getExternalPhylogenyNode( otu2 ) ) ) ); + } + + private final String printProgressNodeToString( final PhylogenyNode n ) { + if ( n.isExternal() ) { + if ( ForesterUtil.isEmpty( n.getName() ) ) { + return Long.toString( n.getId() ); + } + return n.getName(); + } + return n.getId() + + " (" + + ( ForesterUtil.isEmpty( n.getChildNode1().getName() ) ? n.getChildNode1().getId() : n.getChildNode1() + .getName() ) + + "+" + + ( ForesterUtil.isEmpty( n.getChildNode2().getName() ) ? n.getChildNode2().getId() : n.getChildNode2() + .getName() ) + ")"; } // only the values in the lower triangle are used. @@ -216,26 +222,49 @@ 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; + } + } + } + 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 ] ); } }