JAL-2795 implemented getting/setting identifiers
[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 import java.util.Arrays;
11
12 import org.forester.evoinference.matrix.distance.DistanceMatrix;
13 import org.forester.util.ForesterUtil;
14 import org.forester.util.IllegalFormatUseException;
15
16 public class ForesterMatrix implements DistanceMatrix
17 {
18   private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
19           "0.000000"); // straight from forester
20
21   private final MatrixI jalviewMatrix;
22
23   private Sequence[] sequences;
24
25   private String[] identifiers;
26
27   public ForesterMatrix(MatrixI jalviewInputMatrix,
28           Sequence[] matrixSequences)
29   {
30     this.jalviewMatrix = jalviewInputMatrix;
31     this.sequences = matrixSequences;
32
33     if (jalviewMatrix.width() != jalviewMatrix.height())
34     {
35       // some kind of warning?
36     }
37     int i = 0;
38
39     for (Sequence sequence : sequences)
40     {
41       identifiers[i] = sequence.getName();
42       i++;
43     }
44
45   }
46
47   public ForesterMatrix(MatrixI jalviewInputMatrix,
48           String[] matrixIdentifiers)
49   {
50     this.jalviewMatrix = jalviewInputMatrix;
51     this.identifiers = matrixIdentifiers;
52
53
54     if (jalviewMatrix.width() != jalviewMatrix.height())
55     {
56       // some kind of warning?
57     }
58     int i = 0;
59
60     for (String identifier : matrixIdentifiers)
61     {
62       identifiers[i] = identifier;
63       i++;
64     }
65
66
67   }
68
69   @Override
70   public String getIdentifier(int i)
71   {
72     return identifiers[i];
73   }
74
75   @Override
76   public int getIndex(String identifier)
77   {
78     return Arrays.asList(identifiers).indexOf(identifier);
79   }
80
81   /**
82    * Returns the length of whichever is longest, columns or rows
83    */
84   @Override
85   public int getSize()
86   {
87     return jalviewMatrix.getValues().length;
88   }
89
90   /**
91    * See {@link MatrixI#getValue(int,int)} except that the order of column, row
92    * in the parameters is inverted here (as that is how forester demands it)
93    */
94   @Override
95   public double getValue(int col, int row)
96   {
97     return jalviewMatrix.getValue(row, col);
98   }
99
100   @Override
101   public void setIdentifier(int i, String identifier)
102   {
103     identifiers[i] = identifier;
104
105   }
106
107   /**
108    * See {@link MatrixI#setValue()} except that the order of column, row in the
109    * parameters is inverted here (as that is how forester demands it)
110    */
111   @Override
112   public void setValue(int col, int row, double distance)
113   {
114     jalviewMatrix.setValue(row, col, distance);
115
116   }
117
118   @Override
119   public StringBuffer toStringBuffer(Format format)
120   {
121     // TODO Auto-generated method stub
122     return null;
123   }
124
125   /**
126    * See {@link MatrixI#getValues()}
127    */
128   @Override
129   public double[][] getValues()
130   {
131     return jalviewMatrix.getValues();
132   }
133
134   @Override
135   public void write(Writer w) throws IOException // directly copied from
136                                                  // forester
137   {
138     w.write("    ");
139     w.write(getSize() + "");
140     w.write(ForesterUtil.LINE_SEPARATOR);
141     for (int row = 0; row < getSize(); ++row)
142     {
143       if (!ForesterUtil.isEmpty(getIdentifier(row)))
144       {
145         w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
146                 .toString());
147         w.write(' ');
148         w.write(' ');
149       }
150       else
151       {
152         throw new IllegalFormatUseException(
153                 "Phylip format does not allow empty identifiers");
154       }
155       for (int col = 0; col < getSize(); ++col)
156       {
157         w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
158         if (col < (getSize() - 1))
159         {
160           w.write(' ');
161           w.write(' ');
162         }
163       }
164       if (row < (getSize() - 1))
165       {
166         w.write(ForesterUtil.LINE_SEPARATOR);
167       }
168     }
169
170   }
171
172
173 }