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: https://sites.google.com/site/cmzmasek/home/software/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;
40 private double[][] _d_values;
41 private double[][] _m_values;
44 private PhylogenyNode[] _external_nodes;
45 private int[] _mappings;
46 private final boolean _verbose;
47 private final static boolean DEBUG = false;
49 private NeighborJoining( final boolean verbose ) {
53 private final void calculateDistancesFromNewNode( final int otu1, final int otu2, final double d ) {
54 final int otu1_m = _mappings[ otu1 ];
55 final int otu2_m = _mappings[ otu2 ];
57 for( int i = 0; i < _n; ++i ) {
58 if ( ( i == otu1 ) || ( i == otu2 ) ) {
61 // _d_values[ _mappings[ otu1 ] ][ _mappings[ i ] ] = ( getValueFromD( otu1, i ) + getValueFromD( i, otu2 ) - d ) / 2;
63 _d_values[ otu1_m ][ i_m ] = ( ( _d_values[ otu1_m ][ i_m ] + _d_values[ i_m ][ otu2_m ] ) - 2 ) / 2;
67 private final void calculateNetDivergences() {
70 for( int i = 0; i < _n; ++i ) {
73 for( int n = 0; n < _n; ++n ) {
74 d += _d_values[ i_m ][ _mappings[ n ] ];
75 //d += getValueFromD( i, n );
81 public final Phylogeny execute( final BasicSymmetricalDistanceMatrix distance ) {
83 final Phylogeny phylogeny = new Phylogeny();
86 // Calculates the minimal distance.
87 // If more than one minimal distances, always the first found is used
88 // could randomize this, so that any would be returned in a randomized fashion...
89 double minimum = Double.MAX_VALUE;
92 for( int j = 1; j < _n; ++j ) {
93 for( int i = 0; i < j; ++i ) {
94 if ( _m_values[ i ][ j ] < minimum ) {
95 minimum = _m_values[ i ][ j ];
101 // It is a condition that otu1 < otu2.
104 throw new RuntimeException( "NJ code is faulty: otu1 > otu2" );
107 final PhylogenyNode node = new PhylogenyNode();
108 final double d = getValueFromD( otu1, otu2 );
109 final double d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) );
110 final double d2 = d - d1;
111 getExternalPhylogenyNode( otu1 ).setDistanceToParent( d1 );
112 getExternalPhylogenyNode( otu2 ).setDistanceToParent( d2 );
113 node.addAsChild( getExternalPhylogenyNode( otu1 ) );
114 node.addAsChild( getExternalPhylogenyNode( otu2 ) );
116 printProgress( otu1, otu2 );
118 calculateDistancesFromNewNode( otu1, otu2, d );
119 _external_nodes[ _mappings[ otu1 ] ] = node;
120 updateMappings( otu2 );
123 final double d = getValueFromD( 0, 1 ) / 2;
124 getExternalPhylogenyNode( 0 ).setDistanceToParent( d );
125 getExternalPhylogenyNode( 1 ).setDistanceToParent( d );
126 final PhylogenyNode root = new PhylogenyNode();
127 root.addAsChild( getExternalPhylogenyNode( 0 ) );
128 root.addAsChild( getExternalPhylogenyNode( 1 ) );
130 printProgress( 0, 1 );
132 phylogeny.setRoot( root );
133 phylogeny.setRooted( false );
137 public final List<Phylogeny> execute( final List<BasicSymmetricalDistanceMatrix> distances_list ) {
138 final List<Phylogeny> pl = new ArrayList<Phylogeny>();
139 for( final BasicSymmetricalDistanceMatrix distances : distances_list ) {
140 pl.add( execute( distances ) );
145 private final PhylogenyNode getExternalPhylogenyNode( final int i ) {
146 return _external_nodes[ _mappings[ i ] ];
149 private final double getValueFromD( final int otu1, final int otu2 ) {
150 return _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ];
153 private final void initExternalNodes() {
154 _external_nodes = new PhylogenyNode[ _n ];
156 for( int i = 0; i < _n; ++i ) {
157 _external_nodes[ i ] = new PhylogenyNode();
158 id = _d.getIdentifier( i );
160 _external_nodes[ i ].setName( id );
163 _external_nodes[ i ].setName( "" + i );
169 private final void printProgress( final int otu1, final int otu2 ) {
170 final PhylogenyNode n1 = getExternalPhylogenyNode( otu1 );
171 final PhylogenyNode n2 = getExternalPhylogenyNode( otu2 );
172 System.out.println( "Node " + ( ForesterUtil.isEmpty( n1.getName() ) ? n1.getId() : n1.getName() ) + " joins "
173 + ( ForesterUtil.isEmpty( n2.getName() ) ? n2.getId() : n2.getName() ) );
176 // only the values in the lower triangle are used.
177 // !matrix values will be changed!
178 private final void reset( final BasicSymmetricalDistanceMatrix distances ) {
179 _n = distances.getSize();
181 _r = new double[ _n ];
182 _mappings = new int[ _n ];
183 _d_values = _d.getValues();
184 _m_values = new double[ _n ][ _n ];
188 private final void updateM() {
189 calculateNetDivergences();
192 final int _n_2 = _n - 2;
193 for( int j = 1; j < _n; ++j ) {
195 j_m = _mappings[ j ];
196 for( int i = 0; i < j; ++i ) {
197 // _m_values[ i ][ j ] = getValueFromD( i, j ) - ( _r[ i ] + r_j ) / ( _n - 2 );
198 _m_values[ i ][ j ] = _d_values[ _mappings[ i ] ][ j_m ] - ( ( _r[ i ] + r_j ) / ( _n_2 ) );
203 // otu2 will, in effect, be "deleted" from the matrix.
204 private final void updateMappings( final int otu2 ) {
205 for( int i = otu2; i < ( _mappings.length - 1 ); ++i ) {
206 _mappings[ i ] = _mappings[ i + 1 ];
210 public final static NeighborJoining createInstance() {
211 return new NeighborJoining( false );
214 public final static NeighborJoining createInstance( final boolean verbose ) {
215 return new NeighborJoining( verbose );