1 package jalview.ext.forester;
3 import jalview.datamodel.Sequence;
4 import jalview.math.MatrixI;
5 import jalview.util.MessageManager;
7 import java.io.IOException;
9 import java.text.DecimalFormat;
10 import java.text.NumberFormat;
11 import java.util.NoSuchElementException;
12 import java.util.stream.IntStream;
14 import org.forester.evoinference.matrix.distance.DistanceMatrix;
15 import org.forester.util.ForesterUtil;
16 import org.forester.util.IllegalFormatUseException;
18 public class ForesterMatrix implements DistanceMatrix
20 private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
21 "0.000000"); // straight from forester
23 private final MatrixI jalviewMatrix;
25 private Sequence[] sequences;
27 private final String[] identifiers;
29 public ForesterMatrix(final MatrixI jalviewInputMatrix,
30 final Sequence[] matrixSequences)
32 this.jalviewMatrix = jalviewInputMatrix;
33 this.sequences = matrixSequences;
34 this.identifiers = new String[sequences.length];
38 for (Sequence sequence : sequences)
40 identifiers[i] = sequence.getName();
46 public ForesterMatrix(final MatrixI jalviewInputMatrix,
47 final String[] matrixIdentifiers)
49 this.jalviewMatrix = jalviewInputMatrix;
50 this.identifiers = matrixIdentifiers;
55 public String getIdentifier(final int i)
57 return identifiers[i];
62 public int getIndex(final String identifier)
65 return IntStream.range(0, identifiers.length)
66 .filter(x -> identifier.equals(identifiers[x])).findAny()
69 catch (NoSuchElementException ex) {
70 throw new Error(MessageManager.formatMessage(
71 "exception.invalid_matrix_identifier", new String[]
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(final int col, final int row)
92 return jalviewMatrix.getValue(row, col);
96 public void setIdentifier(final int i, final 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(final int col, final int row, final 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(final 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);