JAL-1167 method and simple tests for printing score matrices as tab or
authorjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 6 Sep 2012 13:37:35 +0000 (14:37 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 6 Sep 2012 13:37:35 +0000 (14:37 +0100)
html formatted tables

src/jalview/schemes/ScoreMatrix.java
test/jalview/schemes/ScoreMatrixPrinter.java [new file with mode: 0644]

index f5d30a9..895ee86 100644 (file)
@@ -83,4 +83,58 @@ public class ScoreMatrix
     return pog;\r
   }\r
 \r
+  /**\r
+   * pretty print the matrix\r
+   */\r
+  public String toString()\r
+  {\r
+    return outputMatrix(false);\r
+  }\r
+  public String outputMatrix(boolean html)\r
+  {\r
+    StringBuffer sb=new StringBuffer();\r
+    int[] symbols=(type==0) ? ResidueProperties.aaIndex : ResidueProperties.nucleotideIndex;\r
+    int symMax = (type==0) ? ResidueProperties.maxProteinIndex :ResidueProperties.maxNucleotideIndex;\r
+    boolean header=true;\r
+    if (html)\r
+    {\r
+      sb.append("<table>");\r
+    }\r
+    for (char sym='A';sym<='Z';sym++)\r
+    {\r
+      if (symbols[sym]>=0 && symbols[sym]<symMax)\r
+      {\r
+        if (header) {\r
+          sb.append(html ? "<tr><td></td>" : "");\r
+        for (char sym2='A';sym2<='Z';sym2++)\r
+        {\r
+          if (symbols[sym2]>=0 && symbols[sym2]<symMax)\r
+          {\r
+            sb.append((html ? "<td>&nbsp;" : "\t")+sym2 +(html ? "&nbsp;</td>": ""));\r
+          }\r
+        }\r
+        header=false;\r
+        sb.append(html ? "</tr>\n" : "\n");\r
+        } \r
+        if (html)\r
+        {\r
+          sb.append("<tr>");\r
+        }\r
+        sb.append((html ? "<td>" : "")+sym+(html ? "</td>" : ""));\r
+        for (char sym2='A';sym2<='Z';sym2++)\r
+        {\r
+          if (symbols[sym2]>=0 && symbols[sym2]<symMax)\r
+          {\r
+            sb.append((html ? "<td>" : "\t")+matrix[symbols[sym]][symbols[sym2]]+(html ? "</td>" : ""));\r
+          }\r
+        }\r
+        sb.append(html ? "</tr>\n" : "\n");\r
+      }\r
+    }\r
+    if (html)\r
+    {\r
+      sb.append("</table>");\r
+    }\r
+    return sb.toString();\r
+  }\r
 }\r
diff --git a/test/jalview/schemes/ScoreMatrixPrinter.java b/test/jalview/schemes/ScoreMatrixPrinter.java
new file mode 100644 (file)
index 0000000..099db3c
--- /dev/null
@@ -0,0 +1,29 @@
+package jalview.schemes;
+
+import java.util.Map;
+
+import org.junit.Test;
+
+
+public class ScoreMatrixPrinter 
+{
+  @Test
+  public void printAllMatrices()
+  {
+    for (Map.Entry<String,ScoreMatrix> sm:((Map<String, ScoreMatrix>) ResidueProperties.scoreMatrices).entrySet())
+    {
+      System.out.println("Matrix "+sm.getKey());
+      System.out.println(sm.getValue().toString());
+    }
+  }
+  @Test
+  public void printHTMLMatrices()
+  {
+    for (Map.Entry<String,ScoreMatrix> sm:((Map<String, ScoreMatrix>) ResidueProperties.scoreMatrices).entrySet())
+    {
+      System.out.println("Matrix "+sm.getKey());
+      System.out.println(sm.getValue().outputMatrix(true));
+    }
+  }
+
+}