3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.evoinference.matrix.distance;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.text.DecimalFormat;
32 import java.text.NumberFormat;
33 import java.util.StringTokenizer;
35 import org.forester.util.ForesterUtil;
36 import org.forester.util.IllegalFormatUseException;
38 public final class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
40 // NumberFormat nf1 = NumberFormat.getInstance();
41 private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat( "0.000000" );
42 final String[] _identifiers;
43 final double[][] _values;
45 public BasicSymmetricalDistanceMatrix( final int size ) {
46 _values = new double[ size ][ size ];
47 _identifiers = new String[ size ];
51 public final String getIdentifier( final int i ) {
52 return _identifiers[ i ];
56 public final int getIndex( final String identifier ) {
57 for( int i = 0; i < _identifiers.length; i++ ) {
58 if ( getIdentifier( i ).equals( identifier ) ) {
62 throw new IllegalArgumentException( "identifier [" + identifier + "] not found in distance matrix" );
66 public final int getSize() {
67 return _values.length;
71 public final double getValue( final int col, final int row ) {
73 if ( col >= _values.length ) {
74 throw new IndexOutOfBoundsException( "" );
78 else if ( col > row ) {
79 return _values[ row ][ col ];
81 return _values[ col ][ row ];
84 public final double[][] getValues() {
88 public final void randomize( final long seed ) {
89 final java.util.Random r = new java.util.Random( seed );
90 for( int j = 0; j < getSize(); ++j ) {
91 for( int i = 0; i < j; ++i ) {
92 setValue( i, j, r.nextDouble() );
98 public final void setIdentifier( final int i, final String identifier ) {
99 _identifiers[ i ] = identifier;
102 public final void setRow( final String s, final int row ) {
103 final StringTokenizer tk = new StringTokenizer( s );
105 while ( tk.hasMoreElements() ) {
106 setValue( i, row, new Double( tk.nextToken() ).doubleValue() );
112 public final void setValue( final int col, final int row, final double d ) {
114 throw new IllegalArgumentException( "negative distance value" );
116 if ( ( col == row ) && ( d != 0.0 ) ) {
117 throw new IllegalArgumentException( "attempt to set a non-zero value on the diagonal of a symmetrical distance matrix" );
119 else if ( col > row ) {
120 _values[ row ][ col ] = d;
122 _values[ col ][ row ] = d;
126 public final String toString() {
127 return toPhylip().toString();
131 public final StringBuffer toStringBuffer( final Format format ) {
136 throw new IllegalArgumentException( "Unknown format:" + format );
140 public final void write( final Writer w ) throws IOException {
142 w.write( getSize() + "" );
143 w.write( ForesterUtil.LINE_SEPARATOR );
144 for( int row = 0; row < getSize(); ++row ) {
145 if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
146 w.write( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ).toString() );
151 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
153 for( int col = 0; col < getSize(); ++col ) {
154 w.write( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
155 if ( col < ( getSize() - 1 ) ) {
160 if ( row < ( getSize() - 1 ) ) {
161 w.write( ForesterUtil.LINE_SEPARATOR );
166 private final StringBuffer toPhylip() {
167 final StringBuffer sb = new StringBuffer();
172 sb.append( getSize() );
173 sb.append( ForesterUtil.LINE_SEPARATOR );
174 for( int row = 0; row < getSize(); ++row ) {
175 if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
176 sb.append( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ) );
181 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
184 for( int col = 0; col < getSize(); ++col ) {
185 sb.append( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
186 if ( col < ( getSize() - 1 ) ) {
191 if ( row < ( getSize() - 1 ) ) {
192 sb.append( ForesterUtil.LINE_SEPARATOR );