JAL-2805 loads of file reorganizing, interfaces expanded
[jalview.git] / src / jalview / ext / forester / ForesterMatrix.java
index 4ce3d12..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,20 +22,16 @@ public class ForesterMatrix implements DistanceMatrix
 
   private final MatrixI jalviewMatrix;
 
-  private Sequence[] sequences;
-
   private final String[] identifiers;
 
   public ForesterMatrix(final MatrixI jalviewInputMatrix,
-          final Sequence[] matrixSequences)
+          final SequenceI[] matrixSequences)
   {
     this.jalviewMatrix = jalviewInputMatrix;
-    this.sequences = matrixSequences;
-    this.identifiers = new String[sequences.length];
+    this.identifiers = new String[matrixSequences.length];
 
     int i = 0;
-
-    for (Sequence sequence : sequences)
+    for (SequenceI sequence : matrixSequences)
     {
       identifiers[i] = sequence.getName();
       i++;
@@ -52,13 +50,25 @@ public class ForesterMatrix implements DistanceMatrix
   @Override
   public String getIdentifier(final int i)
   {
-    return identifiers[i];
+    return identifiers[i]; // add handling if index is out of bounds
   }
 
+
   @Override
   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 }));
+    }
   }
 
   /**
@@ -152,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