JAL-2795 more matrix implementations
[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   private final MatrixI jalviewMatrix;
21
22   private final Sequence[] sequences;
23
24   private final double[][] values;
25
26   public ForesterMatrix(MatrixI jalviewInputMatrix,
27           Sequence[] matrixSequences)
28   {
29     this.jalviewMatrix = jalviewInputMatrix;
30     this.sequences = matrixSequences;
31
32     if (jalviewMatrix.width() != jalviewMatrix.height())
33     {
34       // some kind of warning?
35     }
36
37     values = new double[jalviewMatrix.width()][jalviewMatrix.height()];
38
39   }
40
41   @Override
42   public String getIdentifier(int i)
43   {
44     // TODO Auto-generated method stub
45     return null;
46   }
47
48   @Override
49   public int getIndex(String identifier)
50   {
51     return 0;
52   }
53
54   @Override
55   public int getSize()
56   {
57     return jalviewMatrix.width();
58   }
59
60   @Override
61   public double getValue(int col, int row)
62   {
63     return jalviewMatrix.getValue(row, col);
64   }
65
66   @Override
67   public void setIdentifier(int i, String identifier)
68   {
69     // TODO Auto-generated method stub
70
71   }
72
73   @Override
74   public void setValue(int col, int row, double distance)
75   {
76     jalviewMatrix.setValue(row, col, distance);
77
78   }
79
80   @Override
81   public StringBuffer toStringBuffer(Format format)
82   {
83     // TODO Auto-generated method stub
84     return null;
85   }
86
87   @Override
88   public double[][] getValues()
89   {
90
91     return null;
92   }
93
94   @Override
95   public void write(Writer w) throws IOException // directly copied from
96                                                  // forester
97   {
98     w.write("    ");
99     w.write(getSize() + "");
100     w.write(ForesterUtil.LINE_SEPARATOR);
101     for (int row = 0; row < getSize(); ++row)
102     {
103       if (!ForesterUtil.isEmpty(getIdentifier(row)))
104       {
105         w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
106                 .toString());
107         w.write(' ');
108         w.write(' ');
109       }
110       else
111       {
112         throw new IllegalFormatUseException(
113                 "Phylip format does not allow empty identifiers");
114       }
115       for (int col = 0; col < getSize(); ++col)
116       {
117         w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
118         if (col < (getSize() - 1))
119         {
120           w.write(' ');
121           w.write(' ');
122         }
123       }
124       if (row < (getSize() - 1))
125       {
126         w.write(ForesterUtil.LINE_SEPARATOR);
127       }
128     }
129
130   }
131
132
133 }