From 88af7c059e5f19aae3b5a43003961b8689788a4b Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Thu, 6 Mar 2014 22:48:51 +0000 Subject: [PATCH] inprogress --- .../evoinference/TestPhylogenyReconstruction.java | 56 ++++++++++++++++ .../evoinference/distance/NeighborJoiningR.java | 68 +++++++++++--------- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java index 4e53280..281861c 100644 --- a/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java +++ b/forester/java/src/org/forester/evoinference/TestPhylogenyReconstruction.java @@ -2238,6 +2238,25 @@ public class TestPhylogenyReconstruction { m0.setRow( "3 6 ", 2 ); m0.setRow( "7.5 10.5 5.5", 3 ); final Phylogeny p0 = nj.execute( m0 ); + p0.reRoot( p0.getNode( "D" ) ); + if ( isUnequal( p0.getNode( "A" ).getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p0.getNode( "B" ).getDistanceToParent(), 4 ) ) { + return false; + } + if ( isUnequal( p0.getNode( "C" ).getDistanceToParent(), 0.5 ) ) { + return false; + } + if ( isUnequal( p0.getNode( "D" ).getDistanceToParent(), 2.5 ) ) { + return false; + } + if ( isUnequal( p0.getNode( "A" ).getParent().getDistanceToParent(), 1.5 ) ) { + return false; + } + if ( isUnequal( p0.getNode( "A" ).getParent().getParent().getDistanceToParent(), 2.5 ) ) { + return false; + } BasicSymmetricalDistanceMatrix m = new BasicSymmetricalDistanceMatrix( 6 ); m.setRow( "5", 1 ); m.setRow( "4 7", 2 ); @@ -2252,6 +2271,43 @@ public class TestPhylogenyReconstruction { m.setIdentifier( 5, "F" ); nj = NeighborJoiningR.createInstance(); final Phylogeny p1 = nj.execute( m ); + p1.reRoot( p1.getNode( "F" ) ); + if ( isUnequal( p1.getNode( "A" ).getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "B" ).getDistanceToParent(), 4 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "C" ).getDistanceToParent(), 2 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "D" ).getDistanceToParent(), 3 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "E" ).getDistanceToParent(), 2 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "F" ).getDistanceToParent(), 2.5 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "A" ).getParent().getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "A" ).getParent().getParent().getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "A" ).getParent().getParent().getParent().getDistanceToParent(), 2.5 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "B" ).getParent().getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "D" ).getParent().getDistanceToParent(), 1 ) ) { + return false; + } + if ( isUnequal( p1.getNode( "E" ).getParent().getDistanceToParent(), 1 ) ) { + 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 ee3b014..cfcd024 100644 --- a/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java +++ b/forester/java/src/org/forester/evoinference/distance/NeighborJoiningR.java @@ -80,7 +80,7 @@ public final class NeighborJoiningR { 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 ] ]; + final double d = getDvalue( otu1, otu2 ); final double d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) ); final double d2 = d - d1; if ( _df == null ) { @@ -102,7 +102,7 @@ public final class NeighborJoiningR { updateMappings( otu2 ); --_n; } - final double d = _d_values[ _mappings[ 0 ] ][ _mappings[ 1 ] ] / 2; + final double d = getDvalue( 0, 1 ) / 2; if ( _df == null ) { getExternalPhylogenyNode( 0 ).setDistanceToParent( d ); getExternalPhylogenyNode( 1 ).setDistanceToParent( d ); @@ -132,32 +132,32 @@ public final class NeighborJoiningR { } private final void calculateDistancesFromNewNode( final int otu1, final int otu2, final double d ) { - final int m_otu1 = _mappings[ otu1 ]; - final int m_otu2 = _mappings[ otu2 ]; for( int i = 0; i < _n; ++i ) { if ( ( i == otu1 ) || ( i == otu2 ) ) { continue; } - final int m_i = _mappings[ i ]; - if ( otu1 < i ) { - if ( otu2 > i ) { - _d_values[ m_otu1 ][ m_i ] = ( _d_values[ m_otu1 ][ m_i ] + _d_values[ m_i ][ m_otu2 ] - d ) / 2; - } - else { - _d_values[ m_otu1 ][ m_i ] = ( _d_values[ m_otu1 ][ m_i ] + _d_values[ m_otu2 ][ m_i ] - d ) / 2; - } - } - else { - if ( otu2 > i ) { - _d_values[ m_i ][ m_otu1 ] = ( _d_values[ m_i ][ m_otu1 ] + _d_values[ m_i ][ m_otu2 ] - d ) / 2; - } - else { - _d_values[ m_i ][ m_otu1 ] = ( _d_values[ m_i ][ m_otu1 ] + _d_values[ m_otu2 ][ m_i ] - d ) / 2; - } - } + updateDvalue( otu1, otu2, i, d ); } } + 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 ); + } + + private void setDvalue( final int i, final int j, final double d ) { + if ( i < j ) { + _d_values[ _mappings[ i ] ][ _mappings[ j ] ] = d; + } + _d_values[ _mappings[ j ] ][ _mappings[ i ] ] = d; + } + + private double getDvalue( final int i, final int j ) { + if ( i < j ) { + return _d_values[ _mappings[ i ] ][ _mappings[ j ] ]; + } + return _d_values[ _mappings[ j ] ][ _mappings[ i ] ]; + } + private final void calculateNetDivergences() { for( int i = 0; i < _n; ++i ) { _r[ i ] = calculateNetDivergence( i ); @@ -166,15 +166,9 @@ public final class NeighborJoiningR { private double calculateNetDivergence( final int i ) { double d = 0; - final int m_i = _mappings[ i ]; for( int n = 0; n < _n; ++n ) { if ( i != n ) { - if ( i > n ) { - d += _d_values[ _mappings[ n ] ][ m_i ]; - } - else { - d += _d_values[ m_i ][ _mappings[ n ] ]; - } + d += getDvalue( n, i ); } } return d; @@ -264,8 +258,24 @@ public final class NeighborJoiningR { for( int j = 1; j < _n; ++j ) { final double r_j = _r[ j ]; final int m_j = _mappings[ j ]; + final SortedMap> s_j = _s.get( m_j ); + for( final Entry> entry : s_j.entrySet() ) { + //Double key = entry.getKey(); + final SortedSet value = entry.getValue(); + for( final Integer sorted_i : value ) { + System.out.print( sorted_i + " " ); + // final double m = _d_values[ _mappings[ sorted_i ] ][ m_j ] + // - ( ( _r[ sorted_i ] + r_j ) / n_minus_2 ); + // if ( m < min ) { + // min = m; + // _min_i = sorted_i; + // _min_j = j; + // } + } + } + System.out.println(); for( int i = 0; i < j; ++i ) { - final double m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 ); + final double m = getDvalue( i, j ) - ( ( _r[ i ] + r_j ) / n_minus_2 ); if ( m < min ) { min = m; _min_i = i; -- 1.7.10.2