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;
25 private String[] identifiers;
27 public ForesterMatrix(MatrixI jalviewInputMatrix,
28 Sequence[] matrixSequences)
30 this.jalviewMatrix = jalviewInputMatrix;
31 this.sequences = matrixSequences;
34 if (jalviewMatrix.width() != jalviewMatrix.height())
36 // some kind of warning?
41 public ForesterMatrix(MatrixI jalviewInputMatrix,
42 String[] matrixIdentifiers)
44 this.jalviewMatrix = jalviewInputMatrix;
45 this.identifiers = matrixIdentifiers;
48 if (jalviewMatrix.width() != jalviewMatrix.height())
50 // some kind of warning?
57 public String getIdentifier(int i)
59 // TODO Auto-generated method stub
64 public int getIndex(String identifier)
72 return jalviewMatrix.width();
76 public double getValue(int col, int row)
78 return jalviewMatrix.getValue(row, col);
82 public void setIdentifier(int i, String identifier)
84 // TODO Auto-generated method stub
89 public void setValue(int col, int row, double distance)
91 jalviewMatrix.setValue(row, col, distance);
96 public StringBuffer toStringBuffer(Format format)
98 // TODO Auto-generated method stub
103 public double[][] getValues()
105 return jalviewMatrix.getValues();
109 public void write(Writer w) throws IOException // directly copied from
113 w.write(getSize() + "");
114 w.write(ForesterUtil.LINE_SEPARATOR);
115 for (int row = 0; row < getSize(); ++row)
117 if (!ForesterUtil.isEmpty(getIdentifier(row)))
119 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
126 throw new IllegalFormatUseException(
127 "Phylip format does not allow empty identifiers");
129 for (int col = 0; col < getSize(); ++col)
131 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
132 if (col < (getSize() - 1))
138 if (row < (getSize() - 1))
140 w.write(ForesterUtil.LINE_SEPARATOR);