JAL-2416 support roundtrip print/parse of ScoreMatrix
[jalview.git] / src / jalview / io / ScoreMatrixFile.java
index 97fe46b..eee7d68 100644 (file)
@@ -137,6 +137,14 @@ public class ScoreMatrixFile extends AlignFile implements
       }
 
       /*
+       * permit an uncommented line with delimited residue headings
+       */
+      if (isHeaderLine(data, alphabet))
+      {
+        continue;
+      }
+
+      /*
        * subsequent lines should be the symbol scores
        * optionally with the symbol as the first column for readability
        */
@@ -202,6 +210,37 @@ public class ScoreMatrixFile extends AlignFile implements
     return sm;
   }
 
+  /**
+   * Answers true if the data line consists of the alphabet characters,
+   * delimited (as to provide a heading row). Otherwise returns false (e.g. if
+   * the data is a row of score values).
+   * 
+   * @param data
+   * @param alphabet
+   * @return
+   */
+  private boolean isHeaderLine(String data, String alphabet)
+  {
+    StringTokenizer scoreLine = new StringTokenizer(data, DELIMITERS);
+    int i = 0;
+    while (scoreLine.hasMoreElements())
+    {
+      /*
+       * skip over characters in the alphabet that are 
+       * also a delimiter (e.g. space)
+       */
+      char symbol = alphabet.charAt(i++);
+      if (!DELIMITERS.contains(String.valueOf(symbol)))
+      {
+        if (!String.valueOf(symbol).equals(scoreLine.nextToken()))
+        {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
   public String getMatrixName()
   {
     return matrixName;