JAL-2416 support roundtrip print/parse of ScoreMatrix
[jalview.git] / src / jalview / analysis / scoremodels / ScoreMatrix.java
index 22c81f1..13ce6a8 100644 (file)
@@ -231,7 +231,12 @@ public class ScoreMatrix implements PairwiseScoreModelI
   }
 
   /**
-   * Print the score matrix, optionally formatted as html, with the alphabet symbols as column headings and at the start of each row
+   * Print the score matrix, optionally formatted as html, with the alphabet
+   * symbols as column headings and at the start of each row.
+   * <p>
+   * The non-html format should give an output which can be parsed as a score
+   * matrix file
+   * 
    * @param html
    * @return
    */
@@ -247,6 +252,11 @@ public class ScoreMatrix implements PairwiseScoreModelI
       sb.append("<table border=\"1\">");
       sb.append(html ? "<tr><th></th>" : "");
     }
+    else
+    {
+      sb.append("ScoreMatrix ").append(getName()).append("\n");
+      sb.append(symbols).append("\n");
+    }
     for (char sym : symbols)
     {
       if (html)
@@ -338,4 +348,39 @@ public class ScoreMatrix implements PairwiseScoreModelI
     }
     return new Matrix(values);
   }
+
+  /**
+   * Answers a hashcode computed from the symbol alphabet and the matrix score
+   * values
+   */
+  @Override
+  public int hashCode()
+  {
+    int hs = Arrays.hashCode(symbols);
+    for (float[] row : matrix)
+    {
+      hs = hs * 31 + Arrays.hashCode(row);
+    }
+    return hs;
+  }
+
+  /**
+   * Answers true if the argument is a ScoreMatrix with the same symbol alphabet
+   * and score values, else false
+   */
+  @Override
+  public boolean equals(Object obj)
+  {
+    if (!(obj instanceof ScoreMatrix))
+    {
+      return false;
+    }
+    ScoreMatrix sm = (ScoreMatrix) obj;
+    if (Arrays.equals(symbols, sm.symbols)
+            && Arrays.deepEquals(matrix, sm.matrix))
+    {
+      return true;
+    }
+    return false;
+  }
 }