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;
32 this.identifiers = new String[sequences.length];
34 if (jalviewMatrix.width() != jalviewMatrix.height())
36 // some kind of warning?
40 for (Sequence sequence : sequences)
42 identifiers[i] = sequence.getName();
48 public ForesterMatrix(MatrixI jalviewInputMatrix,
49 String[] matrixIdentifiers)
51 this.jalviewMatrix = jalviewInputMatrix;
52 this.identifiers = matrixIdentifiers;
55 if (jalviewMatrix.width() != jalviewMatrix.height())
57 // some kind of warning?
65 public String getIdentifier(int i)
67 return identifiers[i];
71 public int getIndex(String identifier)
73 return Arrays.asList(identifiers).indexOf(identifier);
77 * Returns the length of whichever is longest, columns or rows
82 return jalviewMatrix.getValues().length;
86 * See {@link MatrixI#getValue(int,int)} except that the order of column, row
87 * in the parameters is inverted here (as that is how forester demands it)
90 public double getValue(int col, int row)
92 return jalviewMatrix.getValue(row, col);
96 public void setIdentifier(int i, String identifier)
98 identifiers[i] = identifier;
103 * See {@link MatrixI#setValue()} except that the order of column, row in the
104 * parameters is inverted here (as that is how forester demands it)
107 public void setValue(int col, int row, double distance)
109 jalviewMatrix.setValue(row, col, distance);
114 public StringBuffer toStringBuffer(Format format)
116 // TODO Auto-generated method stub
121 * See {@link MatrixI#getValues()}
124 public double[][] getValues()
126 return jalviewMatrix.getValues();
130 public void write(Writer w) throws IOException // directly copied from
134 w.write(getSize() + "");
135 w.write(ForesterUtil.LINE_SEPARATOR);
136 for (int row = 0; row < getSize(); ++row)
138 if (!ForesterUtil.isEmpty(getIdentifier(row)))
140 w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
147 throw new IllegalFormatUseException(
148 "Phylip format does not allow empty identifiers");
150 for (int col = 0; col < getSize(); ++col)
152 w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
153 if (col < (getSize() - 1))
159 if (row < (getSize() - 1))
161 w.write(ForesterUtil.LINE_SEPARATOR);