JAL-2795 foresterMatrix now stores distance values itself
[jalview.git] / src / jalview / ext / forester / ForesterMatrix.java
1 package jalview.ext.forester;
2
3 import jalview.datamodel.Sequence;
4 import jalview.math.MatrixI;
5
6 import java.io.IOException;
7 import java.io.Writer;
8 import java.text.DecimalFormat;
9 import java.text.NumberFormat;
10
11 import org.forester.evoinference.matrix.distance.DistanceMatrix;
12 import org.forester.util.ForesterUtil;
13 import org.forester.util.IllegalFormatUseException;
14
15 public class ForesterMatrix implements DistanceMatrix
16 {
17   private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
18           "0.000000"); // straight from forester
19
20
21   private Sequence[] sequences;
22
23   private int width;
24
25   private int height;
26
27   private final double[][] values;
28
29   private String[] identifiers;
30
31   public ForesterMatrix(MatrixI jalviewInputMatrix,
32           Sequence[] matrixSequences)
33   {
34     width = jalviewInputMatrix.width();
35     height = jalviewInputMatrix.height();
36     values = new double[width][height];
37
38     this.sequences = matrixSequences;
39
40
41     if (width != height)
42     {
43       // some kind of warning?
44     }
45
46
47
48   }
49
50
51   @Override
52   public String getIdentifier(int i)
53   {
54     // TODO Auto-generated method stub
55     return null;
56   }
57
58   @Override
59   public int getIndex(String identifier)
60   {
61     return 0;
62   }
63
64   @Override
65   public int getSize()
66   {
67     return values.length;
68   }
69
70   @Override
71   public double getValue(int col, int row)
72   {
73     return values[row][col];
74   }
75
76   @Override
77   public void setIdentifier(int i, String identifier)
78   {
79     // TODO Auto-generated method stub
80
81   }
82
83   @Override
84   public void setValue(int col, int row, double distance)
85   {
86     values[row][col] = distance;
87
88   }
89
90   @Override
91   public StringBuffer toStringBuffer(Format format)
92   {
93     // TODO Auto-generated method stub
94     return null;
95   }
96
97   @Override
98   public double[][] getValues()
99   {
100
101     return values;
102   }
103
104   @Override
105   public void write(Writer w) throws IOException // directly copied from
106                                                  // forester
107   {
108     w.write("    ");
109     w.write(getSize() + "");
110     w.write(ForesterUtil.LINE_SEPARATOR);
111     for (int row = 0; row < getSize(); ++row)
112     {
113       if (!ForesterUtil.isEmpty(getIdentifier(row)))
114       {
115         w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
116                 .toString());
117         w.write(' ');
118         w.write(' ');
119       }
120       else
121       {
122         throw new IllegalFormatUseException(
123                 "Phylip format does not allow empty identifiers");
124       }
125       for (int col = 0; col < getSize(); ++col)
126       {
127         w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
128         if (col < (getSize() - 1))
129         {
130           w.write(' ');
131           w.write(' ');
132         }
133       }
134       if (row < (getSize() - 1))
135       {
136         w.write(ForesterUtil.LINE_SEPARATOR);
137       }
138     }
139
140   }
141
142
143 }