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 final double[][] values;
26 private String[] identifiers;
28 public ForesterMatrix(MatrixI jalviewInputMatrix,
29 Sequence[] matrixSequences)
31 this.jalviewMatrix = jalviewInputMatrix;
32 this.sequences = matrixSequences;
35 if (jalviewMatrix.width() != jalviewMatrix.height())
37 // some kind of warning?
40 values = new double[jalviewMatrix.width()][jalviewMatrix.height()];
44 public ForesterMatrix(MatrixI jalviewInputMatrix,
45 String[] matrixIdentifiers)
47 this.jalviewMatrix = jalviewInputMatrix;
48 this.identifiers = matrixIdentifiers;
51 if (jalviewMatrix.width() != jalviewMatrix.height())
53 // some kind of warning?
56 values = new double[jalviewMatrix.width()][jalviewMatrix.height()];
61 public String getIdentifier(int i)
63 // TODO Auto-generated method stub
68 public int getIndex(String identifier)
76 return jalviewMatrix.width();
80 public double getValue(int col, int row)
82 return jalviewMatrix.getValue(row, col);
86 public void setIdentifier(int i, String identifier)
88 // TODO Auto-generated method stub
93 public void setValue(int col, int row, double distance)
95 jalviewMatrix.setValue(row, col, distance);
100 public StringBuffer toStringBuffer(Format format)
102 // TODO Auto-generated method stub
107 public double[][] getValues()
114 public void write(Writer w) throws IOException // directly copied from
118 w.write(getSize() + "");
119 w.write(ForesterUtil.LINE_SEPARATOR);
120 for (int row = 0; row < getSize(); ++row)
122 if (!ForesterUtil.isEmpty(getIdentifier(row)))
124 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
131 throw new IllegalFormatUseException(
132 "Phylip format does not allow empty identifiers");
134 for (int col = 0; col < getSize(); ++col)
136 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
137 if (col < (getSize() - 1))
143 if (row < (getSize() - 1))
145 w.write(ForesterUtil.LINE_SEPARATOR);