JAL-1499 tweak output format for Description
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 1 Oct 2015 07:57:52 +0000 (08:57 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 1 Oct 2015 07:57:52 +0000 (08:57 +0100)
src/jalview/io/MegaFile.java
test/jalview/io/MegaFileTest.java

index 238061a..af9889b 100644 (file)
@@ -955,8 +955,21 @@ public class MegaFile extends AlignFile
   {
     StringBuilder sb = new StringBuilder(128);
     sb.append(MEGA_ID).append(newline);
-    printProperty(al, sb, PROP_TITLE, TITLE);
-    printProperty(al, sb, PROP_DESCRIPTION, DESCRIPTION);
+    String propertyValue = (String) al.getProperty(PROP_TITLE);
+    if (propertyValue != null)
+    {
+      sb.append(BANG).append(TITLE).append(SPACE)
+.append(propertyValue)
+              .append(SEMICOLON)
+              .append(newline);
+    }
+    propertyValue = (String) al.getProperty(PROP_DESCRIPTION);
+    if (propertyValue != null)
+    {
+      sb.append(BANG).append(DESCRIPTION).append(newline)
+              .append(propertyValue).append(SEMICOLON)
+              .append(newline);
+    }
 
     /*
      * !Format DataType CodeTable
@@ -1166,27 +1179,6 @@ public class MegaFile extends AlignFile
   }
 
   /**
-   * Helper method to append a property e.g. !Title to the output buffer, if the
-   * property is set on the alignment.
-   * 
-   * @param al
-   * @param headers
-   * @param propertyName
-   * @param propertyKeyword
-   */
-  protected void printProperty(AlignmentI al, StringBuilder headers,
-          String propertyName, String propertyKeyword)
-  {
-    String propertyValue = (String) al.getProperty(propertyName);
-    if (propertyValue != null)
-    {
-      headers.append(BANG).append(propertyKeyword).append(SPACE)
-              .append(propertyValue).append(SEMICOLON)
-              .append(newline);
-    }
-  }
-
-  /**
    * Returns the number of sequence positions output per line
    * 
    * @return
index 5caa50e..fef8337 100644 (file)
@@ -419,7 +419,58 @@ public class MegaFileTest
     //@formatter:off
     String expected = 
          "#MEGA\n!Title Data with description;\n" +
-         "!Description     Line one of description\n" +
+         "!Description\n" +
+         "    Line one of description\n" +
+         "    Line two of description;\n" +
+         "!Format\n" +
+         "    DataType=DNA CodeTable=Standard\n" +
+         "    NSeqs=2 NSites=12\n" +
+         "    Indel=- Identical=. Missing=?;\n\n" +
+         "#U455   CGC GTA\n" +
+         "#CPZANT ATC GGG\n\n" +
+         "#U455   CGA TTT\n" +
+         "#CPZANT CAA TGC\n";
+    //@formatter:on
+    assertEquals("Roundtrip didn't match", expected,
+            formatted);
+  }
+
+  /**
+   * Test reading a MEGA file to an alignment then writing it out in MEGA
+   * format. Verify the output is (functionally) the same as the input.
+   * 
+   * @throws IOException
+   */
+  @Test(groups = "Functional")
+  public void testRoundTrip_multilineFormatWithComments()
+          throws IOException
+  {
+    AppletFormatAdapter fa = new AppletFormatAdapter();
+    //@formatter:off
+    AlignmentI al = fa.readFile("#MEGA\n"
+    + "!Title Data with description;\n"
+    + "[ this comment should be ignored\n"
+    + "including [this nested comment]\n"
+    + "]\n"
+    + "!Format \n"
+    + "DataType=DNA CodeTable=Standard\n"
+    + "indel=- Missing=? MatchChar=.;\n\n"
+    + "!Description\n" 
+    + "    Line one of description\n"
+    + "    Line two of description;\n\n"
+    + "#U455   CGC GTA\n" 
+    + "#CPZANT ATC GGG\n\n"
+    + "#U455   CGA TTT\n" 
+    + "#CPZANT CAA TGC\n",
+            AppletFormatAdapter.PASTE, "MEGA");
+    //@formatter:on
+    MegaFile output = new MegaFile();
+    String formatted = output.print(al);
+    //@formatter:off
+    String expected = 
+         "#MEGA\n!Title Data with description;\n" +
+         "!Description\n" +
+         "    Line one of description\n" +
          "    Line two of description;\n" +
          "!Format\n" +
          "    DataType=DNA CodeTable=Standard\n" +