JAL-2795 removed unneeded distancematrix length/width check
[jalview.git] / src / jalview / ext / forester / ForesterMatrix.java
index fa67e4e..a12dc1d 100644 (file)
@@ -1,13 +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.Arrays;
+import java.util.NoSuchElementException;
+import java.util.stream.IntStream;
 
 import org.forester.evoinference.matrix.distance.DistanceMatrix;
 import org.forester.util.ForesterUtil;
@@ -20,24 +22,16 @@ public class ForesterMatrix implements DistanceMatrix
 
   private final MatrixI jalviewMatrix;
 
-  private Sequence[] sequences;
+  private final String[] identifiers;
 
-  private String[] identifiers;
-
-  public ForesterMatrix(MatrixI jalviewInputMatrix,
-          Sequence[] matrixSequences)
+  public ForesterMatrix(final MatrixI jalviewInputMatrix,
+          final SequenceI[] matrixSequences)
   {
     this.jalviewMatrix = jalviewInputMatrix;
-    this.sequences = matrixSequences;
-    this.identifiers = new String[sequences.length];
+    this.identifiers = new String[matrixSequences.length];
 
-    if (jalviewMatrix.width() != jalviewMatrix.height())
-    {
-      // some kind of warning?
-    }
     int i = 0;
-
-    for (Sequence sequence : sequences)
+    for (SequenceI sequence : matrixSequences)
     {
       identifiers[i] = sequence.getName();
       i++;
@@ -45,32 +39,36 @@ public class ForesterMatrix implements DistanceMatrix
 
   }
 
-  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)
   {
-    return identifiers[i];
+    return identifiers[i]; // add handling if index is out of bounds
   }
 
+
   @Override
-  public int getIndex(String identifier)
+  public int getIndex(final String identifier)
   {
-    return Arrays.asList(identifiers).indexOf(identifier);
+    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 }));
+    }
   }
 
   /**
@@ -87,13 +85,13 @@ public class ForesterMatrix implements DistanceMatrix
    * 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)
   {
     identifiers[i] = identifier;
 
@@ -104,7 +102,7 @@ public class ForesterMatrix implements DistanceMatrix
    * 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);
 
@@ -127,7 +125,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("    ");
@@ -164,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