X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fforester%2FForesterMatrix.java;h=a12dc1dbae0f2826e8c45c715d5f1964e96fa739;hb=99d91ca9589546151d5c1f3843c7de9c8f4d5990;hp=d4864cff1eba4fe1c441c54512a3c6ab732faefe;hpb=b1e2aea95b3839a1a53f809c579666075cd3d348;p=jalview.git diff --git a/src/jalview/ext/forester/ForesterMatrix.java b/src/jalview/ext/forester/ForesterMatrix.java index d4864cf..a12dc1d 100644 --- a/src/jalview/ext/forester/ForesterMatrix.java +++ b/src/jalview/ext/forester/ForesterMatrix.java @@ -1,12 +1,15 @@ package jalview.ext.forester; -import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; import jalview.math.MatrixI; +import jalview.util.MessageManager; import java.io.IOException; import java.io.Writer; import java.text.DecimalFormat; import java.text.NumberFormat; +import java.util.NoSuchElementException; +import java.util.stream.IntStream; import org.forester.evoinference.matrix.distance.DistanceMatrix; import org.forester.util.ForesterUtil; @@ -19,59 +22,87 @@ public class ForesterMatrix implements DistanceMatrix private final MatrixI jalviewMatrix; - private final Sequence[] sequences; + private final String[] identifiers; - private final double[][] values; - - public ForesterMatrix(MatrixI jalviewInputMatrix, - Sequence[] matrixSequences) + public ForesterMatrix(final MatrixI jalviewInputMatrix, + final SequenceI[] matrixSequences) { this.jalviewMatrix = jalviewInputMatrix; - this.sequences = matrixSequences; + this.identifiers = new String[matrixSequences.length]; - if (jalviewMatrix.width() != jalviewMatrix.height()) + int i = 0; + for (SequenceI sequence : matrixSequences) { - // some kind of warning? + identifiers[i] = sequence.getName(); + i++; } - values = new double[jalviewMatrix.width()][jalviewMatrix.height()]; + } + + public ForesterMatrix(final MatrixI jalviewInputMatrix, + final String[] matrixIdentifiers) + { + this.jalviewMatrix = jalviewInputMatrix; + this.identifiers = matrixIdentifiers; } @Override - public String getIdentifier(int i) + public String getIdentifier(final int i) { - // TODO Auto-generated method stub - return null; + return identifiers[i]; // add handling if index is out of bounds } + @Override - public int getIndex(String identifier) + public int getIndex(final String identifier) { - return 0; + try { + return IntStream.range(0, identifiers.length) + .filter(x -> identifier.equals(identifiers[x])).findAny() + .getAsInt(); // stream to bypass otherwise having to duplicate the + // list + // with Arrays.aslist + } + catch (NoSuchElementException ex) { + throw new Error(MessageManager.formatMessage( + "exception.invalid_matrix_identifier", new String[] + { identifier })); + } } + /** + * Returns the length of whichever is longest, columns or rows + */ @Override public int getSize() { - return jalviewMatrix.width(); + return jalviewMatrix.getValues().length; } + /** + * See {@link MatrixI#getValue(int,int)} except that the order of column, row + * in the parameters is inverted here (as that is how forester demands it) + */ @Override - public double getValue(int col, int row) + public double getValue(final int col, final int row) { return jalviewMatrix.getValue(row, col); } @Override - public void setIdentifier(int i, String identifier) + public void setIdentifier(final int i, final String identifier) { - // TODO Auto-generated method stub + identifiers[i] = identifier; } + /** + * See {@link MatrixI#setValue()} except that the order of column, row in the + * parameters is inverted here (as that is how forester demands it) + */ @Override - public void setValue(int col, int row, double distance) + public void setValue(final int col, final int row, final double distance) { jalviewMatrix.setValue(row, col, distance); @@ -84,15 +115,17 @@ public class ForesterMatrix implements DistanceMatrix return null; } + /** + * See {@link MatrixI#getValues()} + */ @Override public double[][] getValues() { - - return null; + return jalviewMatrix.getValues(); } @Override - public void write(Writer w) throws IOException // directly copied from + public void write(final Writer w) throws IOException // directly copied from // forester { w.write(" "); @@ -129,5 +162,23 @@ public class ForesterMatrix implements DistanceMatrix } + public static DistanceMatrix convertJalviewToForester( + final MatrixI jalviewInputMatrix, + final SequenceI[] matrixSequences) + { + return DataConversions.createForesterDistanceMatrix( + jalviewInputMatrix, matrixSequences); + + } + + public static DistanceMatrix convertJalviewToForester( + final MatrixI jalviewInputMatrix, + final String[] matrixIdentifiers) + { + return DataConversions.createForesterDistanceMatrix( + jalviewInputMatrix, matrixIdentifiers); + + } + } \ No newline at end of file