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