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 final String[] identifiers;
27 public ForesterMatrix(final MatrixI jalviewInputMatrix,
28 final Sequence[] matrixSequences)
30 this.jalviewMatrix = jalviewInputMatrix;
31 this.sequences = matrixSequences;
32 this.identifiers = new String[sequences.length];
36 for (Sequence sequence : sequences)
38 identifiers[i] = sequence.getName();
44 public ForesterMatrix(final MatrixI jalviewInputMatrix,
45 final String[] matrixIdentifiers)
47 this.jalviewMatrix = jalviewInputMatrix;
48 this.identifiers = matrixIdentifiers;
53 public String getIdentifier(final int i)
55 return identifiers[i];
59 public int getIndex(final String identifier)
61 return Arrays.asList(identifiers).indexOf(identifier);
65 * Returns the length of whichever is longest, columns or rows
70 return jalviewMatrix.getValues().length;
74 * See {@link MatrixI#getValue(int,int)} except that the order of column, row
75 * in the parameters is inverted here (as that is how forester demands it)
78 public double getValue(final int col, final int row)
80 return jalviewMatrix.getValue(row, col);
84 public void setIdentifier(final int i, final String identifier)
86 identifiers[i] = identifier;
91 * See {@link MatrixI#setValue()} except that the order of column, row in the
92 * parameters is inverted here (as that is how forester demands it)
95 public void setValue(final int col, final int row, final double distance)
97 jalviewMatrix.setValue(row, col, distance);
102 public StringBuffer toStringBuffer(Format format)
104 // TODO Auto-generated method stub
109 * See {@link MatrixI#getValues()}
112 public double[][] getValues()
114 return jalviewMatrix.getValues();
118 public void write(final Writer w) throws IOException // directly copied from
122 w.write(getSize() + "");
123 w.write(ForesterUtil.LINE_SEPARATOR);
124 for (int row = 0; row < getSize(); ++row)
126 if (!ForesterUtil.isEmpty(getIdentifier(row)))
128 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
135 throw new IllegalFormatUseException(
136 "Phylip format does not allow empty identifiers");
138 for (int col = 0; col < getSize(); ++col)
140 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
141 if (col < (getSize() - 1))
147 if (row < (getSize() - 1))
149 w.write(ForesterUtil.LINE_SEPARATOR);