package jalview.ext.forester; import jalview.datamodel.Sequence; import jalview.math.MatrixI; import java.io.IOException; import java.io.Writer; import java.text.DecimalFormat; import java.text.NumberFormat; import org.forester.evoinference.matrix.distance.DistanceMatrix; import org.forester.util.ForesterUtil; import org.forester.util.IllegalFormatUseException; public class ForesterMatrix implements DistanceMatrix { private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat( "0.000000"); // straight from forester private final MatrixI jalviewMatrix; private Sequence[] sequences; private final double[][] values; private String[] identifiers; public ForesterMatrix(MatrixI jalviewInputMatrix, Sequence[] matrixSequences) { this.jalviewMatrix = jalviewInputMatrix; this.sequences = matrixSequences; if (jalviewMatrix.width() != jalviewMatrix.height()) { // some kind of warning? } values = new double[jalviewMatrix.width()][jalviewMatrix.height()]; } public ForesterMatrix(MatrixI jalviewInputMatrix, String[] matrixIdentifiers) { this.jalviewMatrix = jalviewInputMatrix; this.identifiers = matrixIdentifiers; if (jalviewMatrix.width() != jalviewMatrix.height()) { // some kind of warning? } values = new double[jalviewMatrix.width()][jalviewMatrix.height()]; } @Override public String getIdentifier(int i) { // TODO Auto-generated method stub return null; } @Override public int getIndex(String identifier) { return 0; } @Override public int getSize() { return jalviewMatrix.width(); } @Override public double getValue(int col, int row) { return jalviewMatrix.getValue(row, col); } @Override public void setIdentifier(int i, String identifier) { // TODO Auto-generated method stub } @Override public void setValue(int col, int row, double distance) { jalviewMatrix.setValue(row, col, distance); } @Override public StringBuffer toStringBuffer(Format format) { // TODO Auto-generated method stub return null; } @Override public double[][] getValues() { return null; } @Override public void write(Writer w) throws IOException // directly copied from // forester { w.write(" "); w.write(getSize() + ""); w.write(ForesterUtil.LINE_SEPARATOR); for (int row = 0; row < getSize(); ++row) { if (!ForesterUtil.isEmpty(getIdentifier(row))) { w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false) .toString()); w.write(' '); w.write(' '); } else { throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers"); } for (int col = 0; col < getSize(); ++col) { w.write(PHYLIP_FORMATTER.format(getValue(col, row))); if (col < (getSize() - 1)) { w.write(' '); w.write(' '); } } if (row < (getSize() - 1)) { w.write(ForesterUtil.LINE_SEPARATOR); } } } }