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;
10 import java.util.Arrays;
12 import org.forester.evoinference.matrix.distance.DistanceMatrix;
13 import org.forester.util.ForesterUtil;
14 import org.forester.util.IllegalFormatUseException;
16 public class ForesterMatrix implements DistanceMatrix
18 private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
19 "0.000000"); // straight from forester
21 private final MatrixI jalviewMatrix;
23 private Sequence[] sequences;
25 private String[] identifiers;
27 public ForesterMatrix(MatrixI jalviewInputMatrix,
28 Sequence[] matrixSequences)
30 this.jalviewMatrix = jalviewInputMatrix;
31 this.sequences = matrixSequences;
33 if (jalviewMatrix.width() != jalviewMatrix.height())
35 // some kind of warning?
39 for (Sequence sequence : sequences)
41 identifiers[i] = sequence.getName();
47 public ForesterMatrix(MatrixI jalviewInputMatrix,
48 String[] matrixIdentifiers)
50 this.jalviewMatrix = jalviewInputMatrix;
51 this.identifiers = matrixIdentifiers;
54 if (jalviewMatrix.width() != jalviewMatrix.height())
56 // some kind of warning?
60 for (String identifier : matrixIdentifiers)
62 identifiers[i] = identifier;
70 public String getIdentifier(int i)
72 return identifiers[i];
76 public int getIndex(String identifier)
78 return Arrays.asList(identifiers).indexOf(identifier);
82 * Returns the length of whichever is longest, columns or rows
87 return jalviewMatrix.getValues().length;
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)
95 public double getValue(int col, int row)
97 return jalviewMatrix.getValue(row, col);
101 public void setIdentifier(int i, String identifier)
103 identifiers[i] = identifier;
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)
112 public void setValue(int col, int row, double distance)
114 jalviewMatrix.setValue(row, col, distance);
119 public StringBuffer toStringBuffer(Format format)
121 // TODO Auto-generated method stub
126 * See {@link MatrixI#getValues()}
129 public double[][] getValues()
131 return jalviewMatrix.getValues();
135 public void write(Writer w) throws IOException // directly copied from
139 w.write(getSize() + "");
140 w.write(ForesterUtil.LINE_SEPARATOR);
141 for (int row = 0; row < getSize(); ++row)
143 if (!ForesterUtil.isEmpty(getIdentifier(row)))
145 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
152 throw new IllegalFormatUseException(
153 "Phylip format does not allow empty identifiers");
155 for (int col = 0; col < getSize(); ++col)
157 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
158 if (col < (getSize() - 1))
164 if (row < (getSize() - 1))
166 w.write(ForesterUtil.LINE_SEPARATOR);