2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.evoinference.distance;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
32 import org.forester.phylogeny.Phylogeny;
33 import org.forester.phylogeny.PhylogenyNode;
34 import org.forester.util.ForesterUtil;
36 public final class NeighborJoining {
38 private BasicSymmetricalDistanceMatrix _d;
39 private BasicSymmetricalDistanceMatrix _m;
42 private PhylogenyNode[] _external_nodes;
43 private int[] _mappings;
44 private final boolean _verbose;
45 private final static boolean DEBUG = false;
47 private NeighborJoining( final boolean verbose ) {
51 private final void calculateDistancesFromNewNode( final int otu1, final int otu2, final double d ) {
52 for( int i = 0; i < _n; ++i ) {
53 if ( ( i == otu1 ) || ( i == otu2 ) ) {
57 // setValueInD( nd, otu1, i );
58 // _d.setValue( _mappings[ otu1 ],
60 // ( getValueFromD( otu1, i ) + getValueFromD( i, otu2 ) - d ) / 2 );
61 _d._values[ _mappings[ otu1 ] ][ _mappings[ i ] ] = ( getValueFromD( otu1, i ) + getValueFromD( i, otu2 ) - d ) / 2;
65 private final void calculateNetDivergences() {
67 for( int i = 0; i < _n; ++i ) {
69 for( int n = 0; n < _n; ++n ) {
70 // d += getValueFromD( i, n );
71 d += _d._values[ _mappings[ i ] ][ _mappings[ n ] ];
77 public final Phylogeny execute( final BasicSymmetricalDistanceMatrix distance ) {
79 final Phylogeny phylogeny = new Phylogeny();
82 // Calculates the minimal distance.
83 // If more than one minimal distances, always the first found is used
84 // could randomize this, so that any would be returned in a randomized fashion...
85 double minimum = Double.MAX_VALUE;
88 for( int j = 1; j < _n; ++j ) {
89 for( int i = 0; i < j; ++i ) {
90 // if ( _m.getValue( i, j ) < minimum ) {
91 if ( _m._values[ i ][ j ] < minimum ) {
92 //minimum = _m.getValue( i, j );
93 minimum = _m._values[ i ][ j ];
99 // It is a condition that otu1 < otu2.
102 throw new RuntimeException( "NJ code is faulty: otu1 > otu2" );
105 final PhylogenyNode node = new PhylogenyNode();
106 final double d = getValueFromD( otu1, otu2 );
107 final double d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) );
108 final double d2 = d - d1;
109 getExternalPhylogenyNode( otu1 ).setDistanceToParent( d1 );
110 getExternalPhylogenyNode( otu2 ).setDistanceToParent( d2 );
111 node.addAsChild( getExternalPhylogenyNode( otu1 ) );
112 node.addAsChild( getExternalPhylogenyNode( otu2 ) );
114 printProgress( otu1, otu2 );
116 calculateDistancesFromNewNode( otu1, otu2, d );
117 //setExternalPhylogenyNode( node, otu1 );
118 _external_nodes[ _mappings[ otu1 ] ] = node;
119 updateMappings( otu2 );
122 final double d = getValueFromD( 0, 1 ) / 2;
123 getExternalPhylogenyNode( 0 ).setDistanceToParent( d );
124 getExternalPhylogenyNode( 1 ).setDistanceToParent( d );
125 final PhylogenyNode root = new PhylogenyNode();
126 root.addAsChild( getExternalPhylogenyNode( 0 ) );
127 root.addAsChild( getExternalPhylogenyNode( 1 ) );
129 printProgress( 0, 1 );
131 phylogeny.setRoot( root );
132 phylogeny.setRooted( false );
136 public final List<Phylogeny> execute( final List<BasicSymmetricalDistanceMatrix> distances_list ) {
137 final List<Phylogeny> pl = new ArrayList<Phylogeny>();
138 for( final BasicSymmetricalDistanceMatrix distances : distances_list ) {
139 pl.add( execute( distances ) );
144 // private int[] findMinimalDistance() {
145 // // if more than one minimal distances, always the first found is
147 // // i could randomize this, so that any would be returned in a randomized
149 // double minimum = Double.MAX_VALUE;
152 // for( int j = 1; j < _n; ++j ) {
153 // for( int i = 0; i < j; ++i ) {
154 // if ( _m.getValue( i, j ) < minimum ) {
155 // minimum = _m.getValue( i, j );
161 // return new int[] { otu_1, otu_2 };
163 private final PhylogenyNode getExternalPhylogenyNode( final int i ) {
164 return _external_nodes[ _mappings[ i ] ];
167 private final double getValueFromD( final int otu1, final int otu2 ) {
168 //return _d.getValue( _mappings[ otu1 ], _mappings[ otu2 ] );
169 return _d._values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ];
172 private final void initExternalNodes() {
173 _external_nodes = new PhylogenyNode[ _n ];
174 for( int i = 0; i < _n; ++i ) {
175 _external_nodes[ i ] = new PhylogenyNode();
176 final String id = _d.getIdentifier( i );
178 _external_nodes[ i ].setName( id );
181 _external_nodes[ i ].setName( "" + i );
187 private final void printProgress( final int otu1, final int otu2 ) {
188 final PhylogenyNode n1 = getExternalPhylogenyNode( otu1 );
189 final PhylogenyNode n2 = getExternalPhylogenyNode( otu2 );
190 System.out.println( "Node " + ( ForesterUtil.isEmpty( n1.getName() ) ? n1.getId() : n1.getName() ) + " joins "
191 + ( ForesterUtil.isEmpty( n2.getName() ) ? n2.getId() : n2.getName() ) );
194 // only the values in the lower triangle are used.
195 // !matrix values will be changed!
196 private final void reset( final BasicSymmetricalDistanceMatrix distances ) {
197 _n = distances.getSize();
199 _m = new BasicSymmetricalDistanceMatrix( _n );
200 _r = new double[ _n ];
201 _mappings = new int[ _n ];
205 // private final void setExternalPhylogenyNode( final PhylogenyNode node, final int i ) {
206 // _external_nodes[ _mappings[ i ] ] = node;
208 // private final void setValueInD( final double d, final int otu1, final int otu2 ) {
209 // _d.setValue( _mappings[ otu1 ], _mappings[ otu2 ], d );
211 private final void updateM() {
212 calculateNetDivergences();
213 for( int j = 1; j < _n; ++j ) {
214 for( int i = 0; i < j; ++i ) {
215 //_m.setValue( i, j, calculateM( i, j ) );
216 //_m.setValue( i, j, getValueFromD( i, j ) - ( _r[ i ] + _r[ j ] ) / ( _n - 2 ) );
217 //_m._values[ i ][ j ] = getValueFromD( i, j ) - ( _r[ i ] + _r[ j ] ) / ( _n - 2 );
218 _m._values[ i ][ j ] = _d._values[ _mappings[ i ] ][ _mappings[ j ] ] - ( _r[ i ] + _r[ j ] )
224 //private double calculateM( final int i, final int j ) {
225 // return getValueFromD( i, j ) - ( _r[ i ] + _r[ j ] ) / ( _n - 2 );
227 // otu2 will, in effect, be "deleted" from the matrix.
228 private final void updateMappings( final int otu2 ) {
229 for( int i = otu2; i < _mappings.length - 1; ++i ) {
230 _mappings[ i ] = _mappings[ i + 1 ];
234 public final static NeighborJoining createInstance() {
235 return new NeighborJoining( false );
238 public final static NeighborJoining createInstance( final boolean verbose ) {
239 return new NeighborJoining( verbose );