2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2014 Christian M. Zmasek
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 // Contact: phylosoft @ gmail . com
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 package org.forester.evoinference.distance;
27 import java.math.RoundingMode;
28 import java.text.DecimalFormat;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
33 import org.forester.evoinference.matrix.distance.DistanceMatrix;
34 import org.forester.phylogeny.Phylogeny;
35 import org.forester.phylogeny.PhylogenyNode;
36 import org.forester.util.ForesterUtil;
38 public final class NeighborJoiningF {
40 private DistanceMatrix _d;
41 private float[][] _d_values;
42 private final DecimalFormat _df;
43 private PhylogenyNode[] _external_nodes;
44 private int[] _mappings;
47 private final boolean _verbose;
51 private NeighborJoiningF() {
56 private NeighborJoiningF( final boolean verbose, final int maximum_fraction_digits_for_distances ) {
57 if ( ( maximum_fraction_digits_for_distances < 1 ) || ( maximum_fraction_digits_for_distances > 9 ) ) {
58 throw new IllegalArgumentException( "maximum fraction digits for distances is out of range: "
59 + maximum_fraction_digits_for_distances );
62 _df = new DecimalFormat();
63 _df.setMaximumFractionDigits( maximum_fraction_digits_for_distances );
64 _df.setRoundingMode( RoundingMode.HALF_UP );
67 public final Phylogeny execute( final DistanceMatrix distance ) {
69 final Phylogeny phylogeny = new Phylogeny();
71 // Calculates the minimal distance.
72 // If more than one minimal distances, always the first found is used
74 final int otu1 = _min_i;
75 final int otu2 = _min_j;
76 // It is a condition that otu1 < otu2.
77 final PhylogenyNode node = new PhylogenyNode();
78 final float d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ];
79 final float d1 = ( d / 2 ) + ( ( _r[ otu1 ] - _r[ otu2 ] ) / ( 2 * ( _n - 2 ) ) );
80 final float d2 = d - d1;
82 getExternalPhylogenyNode( otu1 ).setDistanceToParent( d1 );
83 getExternalPhylogenyNode( otu2 ).setDistanceToParent( d2 );
86 // yes, yes, slow but only grows with n (and not n^2 or worse)...
87 getExternalPhylogenyNode( otu1 ).setDistanceToParent( Double.parseDouble( _df.format( d1 ) ) );
88 getExternalPhylogenyNode( otu2 ).setDistanceToParent( Double.parseDouble( _df.format( d2 ) ) );
90 node.addAsChild( getExternalPhylogenyNode( otu1 ) );
91 node.addAsChild( getExternalPhylogenyNode( otu2 ) );
93 printProgress( otu1, otu2 );
95 calculateDistancesFromNewNode( otu1, otu2, d );
96 _external_nodes[ _mappings[ otu1 ] ] = node;
97 updateMappings( otu2 );
100 final double d = _d_values[ _mappings[ 0 ] ][ _mappings[ 1 ] ] / 2;
102 getExternalPhylogenyNode( 0 ).setDistanceToParent( d );
103 getExternalPhylogenyNode( 1 ).setDistanceToParent( d );
106 final double dd = Double.parseDouble( _df.format( d ) );
107 getExternalPhylogenyNode( 0 ).setDistanceToParent( dd );
108 getExternalPhylogenyNode( 1 ).setDistanceToParent( dd );
110 final PhylogenyNode root = new PhylogenyNode();
111 root.addAsChild( getExternalPhylogenyNode( 0 ) );
112 root.addAsChild( getExternalPhylogenyNode( 1 ) );
114 printProgress( 0, 1 );
116 phylogeny.setRoot( root );
117 phylogeny.setRooted( false );
121 public final List<Phylogeny> execute( final List<BasicSymmetricalDistanceMatrix> distances_list ) {
122 final List<Phylogeny> pl = new ArrayList<Phylogeny>();
123 for( final BasicSymmetricalDistanceMatrix distances : distances_list ) {
124 pl.add( execute( distances ) );
129 private final void calculateDistancesFromNewNode( final int otu1, final int otu2, final float d ) {
130 final int m_otu1 = _mappings[ otu1 ];
131 final int m_otu2 = _mappings[ otu2 ];
132 for( int i = 0; i < _n; ++i ) {
133 if ( ( i == otu1 ) || ( i == otu2 ) ) {
136 final int m_i = _mappings[ i ];
139 _d_values[ m_otu1 ][ m_i ] = ( ( _d_values[ m_otu1 ][ m_i ] + _d_values[ m_i ][ m_otu2 ] ) - d ) / 2;
142 _d_values[ m_otu1 ][ m_i ] = ( ( _d_values[ m_otu1 ][ m_i ] + _d_values[ m_otu2 ][ m_i ] ) - d ) / 2;
147 _d_values[ m_i ][ m_otu1 ] = ( ( _d_values[ m_i ][ m_otu1 ] + _d_values[ m_i ][ m_otu2 ] ) - d ) / 2;
150 _d_values[ m_i ][ m_otu1 ] = ( ( _d_values[ m_i ][ m_otu1 ] + _d_values[ m_otu2 ][ m_i ] ) - d ) / 2;
156 private final void calculateNetDivergences() {
158 for( int i = 0; i < _n; ++i ) {
160 final int m_i = _mappings[ i ];
161 for( int n = 0; n < _n; ++n ) {
164 d += _d_values[ _mappings[ n ] ][ m_i ];
167 d += _d_values[ m_i ][ _mappings[ n ] ];
175 private final PhylogenyNode getExternalPhylogenyNode( final int i ) {
176 return _external_nodes[ _mappings[ i ] ];
179 private final void initExternalNodes() {
180 _external_nodes = new PhylogenyNode[ _n ];
182 for( int i = 0; i < _n; ++i ) {
183 _external_nodes[ i ] = new PhylogenyNode();
184 id = _d.getIdentifier( i );
186 _external_nodes[ i ].setName( id );
189 _external_nodes[ i ].setName( Integer.toString( i ) );
195 private final void printProgress( final int otu1, final int otu2 ) {
196 System.out.println( "Node " + printProgressNodeToString( getExternalPhylogenyNode( otu1 ) ) + " joins "
197 + ( printProgressNodeToString( getExternalPhylogenyNode( otu2 ) ) ) );
200 private final String printProgressNodeToString( final PhylogenyNode n ) {
201 if ( n.isExternal() ) {
202 if ( ForesterUtil.isEmpty( n.getName() ) ) {
203 return Long.toString( n.getId() );
209 + ( ForesterUtil.isEmpty( n.getChildNode1().getName() ) ? n.getChildNode1().getId() : n.getChildNode1()
212 + ( ForesterUtil.isEmpty( n.getChildNode2().getName() ) ? n.getChildNode2().getId() : n.getChildNode2()
216 // only the values in the lower triangle are used.
217 private final void reset( final DistanceMatrix distances ) {
218 _n = distances.getSize();
220 _r = new float[ _n ];
221 _mappings = new int[ _n ];
222 _d_values = new float[ distances.getSize() ][ distances.getSize() ];
223 for( int i = 0; i < distances.getSize(); ++i ) {
224 for( int j = 0; j < distances.getSize(); ++j ) {
225 _d_values[ i ][ j ] = ( float ) distances.getValue( i, j );
231 private final void updateM() {
232 calculateNetDivergences();
233 final int n_minus_2 = _n - 2;
234 float min = Float.MAX_VALUE;
237 for( int j = 1; j < _n; ++j ) {
238 final float r_j = _r[ j ];
239 final int m_j = _mappings[ j ];
240 for( int i = 0; i < j; ++i ) {
241 final float m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 );
251 // otu2 will, in effect, be "deleted" from the matrix.
252 private final void updateMappings( final int otu2 ) {
253 for( int i = otu2; i < ( _mappings.length - 1 ); ++i ) {
254 _mappings[ i ] = _mappings[ i + 1 ];
258 public final static NeighborJoiningF createInstance() {
259 return new NeighborJoiningF();
262 public final static NeighborJoiningF createInstance( final boolean verbose,
263 final int maximum_fraction_digits_for_distances ) {
264 return new NeighborJoiningF( verbose, maximum_fraction_digits_for_distances );