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