JAL-2795 rebuilt MatrixConverter to be purely static
[jalview.git] / src / jalview / ext / forester / ForesterMatrix.java
index f8b68d0..4ce3d12 100644 (file)
@@ -7,6 +7,7 @@ import java.io.IOException;
 import java.io.Writer;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
+import java.util.Arrays;
 
 import org.forester.evoinference.matrix.distance.DistanceMatrix;
 import org.forester.util.ForesterUtil;
@@ -21,72 +22,77 @@ public class ForesterMatrix implements DistanceMatrix
 
   private Sequence[] sequences;
 
+  private final String[] identifiers;
 
-  private String[] identifiers;
-
-  public ForesterMatrix(MatrixI jalviewInputMatrix,
-          Sequence[] matrixSequences)
+  public ForesterMatrix(final MatrixI jalviewInputMatrix,
+          final Sequence[] matrixSequences)
   {
     this.jalviewMatrix = jalviewInputMatrix;
     this.sequences = matrixSequences;
+    this.identifiers = new String[sequences.length];
 
+    int i = 0;
 
-    if (jalviewMatrix.width() != jalviewMatrix.height())
+    for (Sequence sequence : sequences)
     {
-      // some kind of warning?
+      identifiers[i] = sequence.getName();
+      i++;
     }
 
   }
 
-  public ForesterMatrix(MatrixI jalviewInputMatrix,
-          String[] matrixIdentifiers)
+  public ForesterMatrix(final MatrixI jalviewInputMatrix,
+          final String[] matrixIdentifiers)
   {
     this.jalviewMatrix = jalviewInputMatrix;
     this.identifiers = matrixIdentifiers;
 
-
-    if (jalviewMatrix.width() != jalviewMatrix.height())
-    {
-      // some kind of warning?
-    }
-
-
   }
 
   @Override
-  public String getIdentifier(int i)
+  public String getIdentifier(final int i)
   {
-    // TODO Auto-generated method stub
-    return null;
+    return identifiers[i];
   }
 
   @Override
-  public int getIndex(String identifier)
+  public int getIndex(final String identifier)
   {
-    return 0;
+    return Arrays.asList(identifiers).indexOf(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);
 
@@ -99,6 +105,9 @@ public class ForesterMatrix implements DistanceMatrix
     return null;
   }
 
+  /**
+   * See {@link MatrixI#getValues()}
+   */
   @Override
   public double[][] getValues()
   {
@@ -106,7 +115,7 @@ public class ForesterMatrix implements DistanceMatrix
   }
 
   @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("    ");