in progress
[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: www.phylosoft.org/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 public 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     @Override
51     public final String getIdentifier( final int i ) {
52         return _identifiers[ i ];
53     }
54
55     @Override
56     public final int getIndex( final String identifier ) {
57         for( int i = 0; i < _identifiers.length; i++ ) {
58             if ( getIdentifier( i ).equals( identifier ) ) {
59                 return i;
60             }
61         }
62         throw new IllegalArgumentException( "identifier [" + identifier + "] not found in distance matrix" );
63     }
64
65     @Override
66     public final int getSize() {
67         return _values.length;
68     }
69
70     @Override
71     public final double getValue( final int col, final int row ) {
72         if ( col == row ) {
73             if ( col >= _values.length ) {
74                 throw new IndexOutOfBoundsException( "" );
75             }
76             return 0.0;
77         }
78         else if ( col > row ) {
79             return _values[ row ][ col ];
80         }
81         return _values[ col ][ row ];
82     }
83
84     public final void randomize( final long seed ) {
85         final java.util.Random r = new java.util.Random( seed );
86         for( int j = 0; j < getSize(); ++j ) {
87             for( int i = 0; i < j; ++i ) {
88                 setValue( i, j, r.nextDouble() );
89             }
90         }
91     }
92
93     @Override
94     public final void setIdentifier( final int i, final String identifier ) {
95         _identifiers[ i ] = identifier;
96     }
97
98     public final void setRow( final String s, final int row ) {
99         final StringTokenizer tk = new StringTokenizer( s );
100         int i = 0;
101         while ( tk.hasMoreElements() ) {
102             setValue( i, row, new Double( tk.nextToken() ).doubleValue() );
103             i++;
104         }
105     }
106
107     @Override
108     public final void setValue( final int col, final int row, final double d ) {
109         if ( ( col == row ) && ( d != 0.0 ) ) {
110             throw new IllegalArgumentException( "attempt to set a non-zero value on the diagonal of a symmetrical distance matrix" );
111         }
112         else if ( col > row ) {
113             _values[ row ][ col ] = d;
114         }
115         _values[ col ][ row ] = d;
116     }
117
118     public final void write( final Writer w ) throws IOException {
119         w.write( "    " );
120         w.write( getSize() + "" );
121         w.write( ForesterUtil.LINE_SEPARATOR );
122         for( int row = 0; row < getSize(); ++row ) {
123             if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
124                 w.write( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ).toString() );
125                 w.write( ' ' );
126                 w.write( ' ' );
127             }
128             else {
129                 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
130             }
131             for( int col = 0; col < getSize(); ++col ) {
132                 w.write( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
133                 if ( col < getSize() - 1 ) {
134                     w.write( ' ' );
135                     w.write( ' ' );
136                 }
137             }
138             if ( row < getSize() - 1 ) {
139                 w.write( ForesterUtil.LINE_SEPARATOR );
140             }
141         }
142     }
143
144     private final StringBuffer toPhylip() {
145         final StringBuffer sb = new StringBuffer();
146         sb.append( ' ' );
147         sb.append( ' ' );
148         sb.append( ' ' );
149         sb.append( ' ' );
150         sb.append( getSize() );
151         sb.append( ForesterUtil.LINE_SEPARATOR );
152         for( int row = 0; row < getSize(); ++row ) {
153             if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) {
154                 sb.append( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ) );
155                 sb.append( ' ' );
156                 sb.append( ' ' );
157             }
158             else {
159                 throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" );
160             }
161             //sb.append( "" );
162             for( int col = 0; col < getSize(); ++col ) {
163                 sb.append( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
164                 if ( col < getSize() - 1 ) {
165                     sb.append( ' ' );
166                     sb.append( ' ' );
167                 }
168             }
169             if ( row < getSize() - 1 ) {
170                 sb.append( ForesterUtil.LINE_SEPARATOR );
171             }
172         }
173         return sb;
174     }
175
176     @Override
177     public final String toString() {
178         return toPhylip().toString();
179     }
180
181     @Override
182     public final StringBuffer toStringBuffer( final Format format ) {
183         switch ( format ) {
184             case PHYLIP:
185                 return toPhylip();
186             default:
187                 throw new IllegalArgumentException( "Unknown format:" + format );
188         }
189     }
190 }