JAL-2795 more matrix implementations
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Sun, 29 Oct 2017 16:51:31 +0000 (16:51 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Sun, 29 Oct 2017 16:51:31 +0000 (16:51 +0000)
forester
src/jalview/ext/forester/ForesterMatrix.java

index d62c120..09a8c72 160000 (submodule)
--- a/forester
+++ b/forester
@@ -1 +1 @@
-Subproject commit d62c120f4c6fb10f20abaa929514d980fcea9671
+Subproject commit 09a8c723e9ab220999f25c6be635282f18186be7
index 39106a7..d4864cf 100644 (file)
@@ -1,21 +1,41 @@
 package jalview.ext.forester;
 
+import jalview.datamodel.Sequence;
 import jalview.math.MatrixI;
 
+import java.io.IOException;
+import java.io.Writer;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+
 import org.forester.evoinference.matrix.distance.DistanceMatrix;
+import org.forester.util.ForesterUtil;
+import org.forester.util.IllegalFormatUseException;
 
 public class ForesterMatrix implements DistanceMatrix
 {
-  private MatrixI jalviewMatrix;
+  private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
+          "0.000000"); // straight from forester
+
+  private final MatrixI jalviewMatrix;
+
+  private final Sequence[] sequences;
 
-  public ForesterMatrix(MatrixI jalviewInputMatrix)
+  private final double[][] values;
+
+  public ForesterMatrix(MatrixI jalviewInputMatrix,
+          Sequence[] matrixSequences)
   {
     this.jalviewMatrix = jalviewInputMatrix;
+    this.sequences = matrixSequences;
+
     if (jalviewMatrix.width() != jalviewMatrix.height())
     {
-
+      // some kind of warning?
     }
 
+    values = new double[jalviewMatrix.width()][jalviewMatrix.height()];
+
   }
 
   @Override
@@ -67,8 +87,47 @@ public class ForesterMatrix implements DistanceMatrix
   @Override
   public double[][] getValues()
   {
-    // TODO Auto-generated method stub
+
     return null;
   }
 
+  @Override
+  public void write(Writer w) throws IOException // directly copied from
+                                                 // forester
+  {
+    w.write("    ");
+    w.write(getSize() + "");
+    w.write(ForesterUtil.LINE_SEPARATOR);
+    for (int row = 0; row < getSize(); ++row)
+    {
+      if (!ForesterUtil.isEmpty(getIdentifier(row)))
+      {
+        w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
+                .toString());
+        w.write(' ');
+        w.write(' ');
+      }
+      else
+      {
+        throw new IllegalFormatUseException(
+                "Phylip format does not allow empty identifiers");
+      }
+      for (int col = 0; col < getSize(); ++col)
+      {
+        w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
+        if (col < (getSize() - 1))
+        {
+          w.write(' ');
+          w.write(' ');
+        }
+      }
+      if (row < (getSize() - 1))
+      {
+        w.write(ForesterUtil.LINE_SEPARATOR);
+      }
+    }
+
+  }
+
+
 }
\ No newline at end of file