1 package jalview.ext.forester;
3 import jalview.datamodel.Sequence;
4 import jalview.math.MatrixI;
6 import java.io.IOException;
8 import java.text.DecimalFormat;
9 import java.text.NumberFormat;
11 import org.forester.evoinference.matrix.distance.DistanceMatrix;
12 import org.forester.util.ForesterUtil;
13 import org.forester.util.IllegalFormatUseException;
15 public class ForesterMatrix implements DistanceMatrix
17 private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
18 "0.000000"); // straight from forester
20 private final MatrixI jalviewMatrix;
22 private Sequence[] sequences;
24 private String[] identifiers;
26 public ForesterMatrix(MatrixI jalviewInputMatrix,
27 Sequence[] matrixSequences)
29 this.jalviewMatrix = jalviewInputMatrix;
30 this.sequences = matrixSequences;
33 if (jalviewMatrix.width() != jalviewMatrix.height())
35 // some kind of warning?
40 public ForesterMatrix(MatrixI jalviewInputMatrix,
41 String[] matrixIdentifiers)
43 this.jalviewMatrix = jalviewInputMatrix;
44 this.identifiers = matrixIdentifiers;
47 if (jalviewMatrix.width() != jalviewMatrix.height())
49 // some kind of warning?
56 public String getIdentifier(int i)
58 // TODO Auto-generated method stub
63 public int getIndex(String identifier)
69 * Returns the length of whichever is longest, columns or rows
74 return jalviewMatrix.getValues().length;
78 * See {@link MatrixI#getValue(int,int)} except that the order of column, row
79 * in the parameters is inverted here
82 public double getValue(int col, int row)
84 return jalviewMatrix.getValue(row, col);
88 public void setIdentifier(int i, String identifier)
90 // TODO Auto-generated method stub
95 * See {@link MatrixI#setValue()} except that the order of column, row in the
96 * parameters is inverted here
99 public void setValue(int col, int row, double distance)
101 jalviewMatrix.setValue(row, col, distance);
106 public StringBuffer toStringBuffer(Format format)
108 // TODO Auto-generated method stub
113 * See {@link MatrixI#getValues()}
116 public double[][] getValues()
118 return jalviewMatrix.getValues();
122 public void write(Writer w) throws IOException // directly copied from
126 w.write(getSize() + "");
127 w.write(ForesterUtil.LINE_SEPARATOR);
128 for (int row = 0; row < getSize(); ++row)
130 if (!ForesterUtil.isEmpty(getIdentifier(row)))
132 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
139 throw new IllegalFormatUseException(
140 "Phylip format does not allow empty identifiers");
142 for (int col = 0; col < getSize(); ++col)
144 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
145 if (col < (getSize() - 1))
151 if (row < (getSize() - 1))
153 w.write(ForesterUtil.LINE_SEPARATOR);