fdf2ec4ea4ebc757f152a0ecada2e21e785b093e
[jalview.git] / forester / java / src / org / forester / evoinference / matrix / distance / BasicSymmetricalDistanceMatrix.java
1 // $Id:
2 // Exp $
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.evoinference.matrix.distance;
28
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;
34
35 import org.forester.util.ForesterUtil;
36 import org.forester.util.IllegalFormatUseException;
37
38 public final class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
39
40     // NumberFormat                      nf1              = NumberFormat.getInstance();
41     private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat( "0.000000" );
42     final double[][]                  _values;
43     final String[]                    _identifiers;
44
45     public BasicSymmetricalDistanceMatrix( final int size ) {
46         _values = new double[ size ][ size ];
47         _identifiers = new String[ size ];
48     }
49
50     public final double[][] getValues() {
51         return _values;
52     }
53
54     @Override
55     public final String getIdentifier( final int i ) {
56         return _identifiers[ i ];
57     }
58
59     @Override
60     public final int getIndex( final String identifier ) {
61         for( int i = 0; i < _identifiers.length; i++ ) {
62             if ( getIdentifier( i ).equals( identifier ) ) {
63                 return i;
64             }
65         }
66         throw new IllegalArgumentException( "identifier [" + identifier + "] not found in distance matrix" );
67     }
68
69     @Override
70     public final int getSize() {
71         return _values.length;
72     }
73
74     @Override
75     public final double getValue( final int col, final int row ) {
76         if ( col == row ) {
77             if ( col >= _values.length ) {
78                 throw new IndexOutOfBoundsException( "" );
79             }
80             return 0.0;
81         }
82         else if ( col > row ) {
83             return _values[ row ][ col ];
84         }
85         return _values[ col ][ row ];
86     }
87
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() );
93             }
94         }
95     }
96
97     @Override
98     public final void setIdentifier( final int i, final String identifier ) {
99         _identifiers[ i ] = identifier;
100     }
101
102     public final void setRow( final String s, final int row ) {
103         final StringTokenizer tk = new StringTokenizer( s );
104         int i = 0;
105         while ( tk.hasMoreElements() ) {
106             setValue( i, row, new Double( tk.nextToken() ).doubleValue() );
107             i++;
108         }
109     }
110
111     @Override
112     public final void setValue( final int col, final int row, final double d ) {
113         if ( d < 0 ) {
114             throw new IllegalArgumentException( "negative distance value" );
115         }
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" );
118         }
119         else if ( col > row ) {
120             _values[ row ][ col ] = d;
121         }
122         _values[ col ][ row ] = d;
123     }
124
125     public final void write( final Writer w ) throws IOException {
126         w.write( "    " );
127         w.write( getSize() + "" );
128         w.write( ForesterUtil.LINE_SEPARATOR );
129         for( int row = 0; row < getSize(); ++row ) {
130             if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
131                 w.write( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ).toString() );
132                 w.write( ' ' );
133                 w.write( ' ' );
134             }
135             else {
136                 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
137             }
138             for( int col = 0; col < getSize(); ++col ) {
139                 w.write( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
140                 if ( col < ( getSize() - 1 ) ) {
141                     w.write( ' ' );
142                     w.write( ' ' );
143                 }
144             }
145             if ( row < ( getSize() - 1 ) ) {
146                 w.write( ForesterUtil.LINE_SEPARATOR );
147             }
148         }
149     }
150
151     private final StringBuffer toPhylip() {
152         final StringBuffer sb = new StringBuffer();
153         sb.append( ' ' );
154         sb.append( ' ' );
155         sb.append( ' ' );
156         sb.append( ' ' );
157         sb.append( getSize() );
158         sb.append( ForesterUtil.LINE_SEPARATOR );
159         for( int row = 0; row < getSize(); ++row ) {
160             if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
161                 sb.append( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ) );
162                 sb.append( ' ' );
163                 sb.append( ' ' );
164             }
165             else {
166                 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
167             }
168             //sb.append( "" );
169             for( int col = 0; col < getSize(); ++col ) {
170                 sb.append( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
171                 if ( col < ( getSize() - 1 ) ) {
172                     sb.append( ' ' );
173                     sb.append( ' ' );
174                 }
175             }
176             if ( row < ( getSize() - 1 ) ) {
177                 sb.append( ForesterUtil.LINE_SEPARATOR );
178             }
179         }
180         return sb;
181     }
182
183     @Override
184     public final String toString() {
185         return toPhylip().toString();
186     }
187
188     @Override
189     public final StringBuffer toStringBuffer( final Format format ) {
190         switch ( format ) {
191             case PHYLIP:
192                 return toPhylip();
193             default:
194                 throw new IllegalArgumentException( "Unknown format:" + format );
195         }
196     }
197 }