JAL-1499 output !Label lines for "MEGA Label" alignment annotation
[jalview.git] / test / jalview / io / MegaFileTest.java
index f7d83c0..bdae11a 100644 (file)
@@ -279,9 +279,9 @@ public class MegaFileTest
     System.out.println(printed);
     // normally output should match input
     // we cheated here with a number of short input lines
-    String expected = "#MEGA\n\n"
- + "#U455\n" + "ABCFEDHIJM\nNOPQR\n\n"
-            + "#CPZANT\n" + "KLMNOPWXYZ\nCGATC\n";
+    String expected = "#MEGA\n\n" + "#U455\n"
+            + "ABCFEDHIJM [10]\nNOPQR [15]\n\n" + "#CPZANT\n"
+            + "KLMNOPWXYZ [10]\nCGATC [15]\n";
     assertEquals("Print format wrong", expected, printed);
   }
 
@@ -304,7 +304,6 @@ public class MegaFileTest
     String printed = testee.print();
     System.out.println(printed);
     //@formatter:off
-    //0123456789klmnopqrstABCDEFGHIJ9876543210abcdefghij
     String expected = 
             "#MEGA\n\n" + 
             "#U455   0123456789 klmnopqrst [20]\n" + // first 20
@@ -333,10 +332,14 @@ public class MegaFileTest
     assertEquals(30, testee.getPositionsPerLine());
     testee.setPositionsPerLine(25);
     String printed = testee.print();
-    // 60 character sequence should be output as 50 on first line then 10 more
+
+    /*
+     * 25 positions per line is rounded down to 20 (two blocks of 10)
+     */
     String expected = "#MEGA\n\n" + "#SIXTY\n"
-            + "0123456789klmnopqrstABCDE\n" + "FGHIJ9876543210abcdefghij\n"
-            + "9993332221\n";
+            + "0123456789 klmnopqrst [20]\n"
+            + "ABCDEFGHIJ 9876543210 [40]\n"
+            + "abcdefghij 9993332221 [60]\n";
     assertEquals("Print format wrong", expected, printed);
   }
 
@@ -718,24 +721,31 @@ public class MegaFileTest
     "TITLE: Interleaved sequence data\n\n" + 
     "#U455   ABC DEF\n" + 
     "#CPZANT MNO PQR\n" +
-    "!Label  +-_ 23_\n" +
+    "!Label  +-_ 23_\n\n" +
+    // a row with no labels = null annotation
+    "#U455   abc def\n" + 
+    "#CPZANT mno pqr\n\n" +
     "#U455   KLM NOP\n" + 
     "#CPZANT WXY ZGC\n" +
     "!label  __3 +X_\n", AppletFormatAdapter.PASTE);
     //@formatter:on
     Vector<SequenceI> seqs = testee.getSeqs();
     assertEquals("Expected two sequences", 2, seqs.size());
-    assertEquals("First sequence data wrong", "ABCDEFKLMNOP", seqs.get(0)
+    assertEquals("First sequence data wrong", "ABCDEFabcdefKLMNOP", seqs
+            .get(0)
             .getSequenceAsString());
-    assertEquals("Second sequence data wrong", "MNOPQRWXYZGC", seqs.get(1)
+    assertEquals("Second sequence data wrong", "MNOPQRmnopqrWXYZGC", seqs
+            .get(1)
             .getSequenceAsString());
 
     // check AlignmentAnnotation added with expected values
     assertEquals(1, testee.annotations.size());
     AlignmentAnnotation aa = testee.annotations.get(0);
     assertNull(aa.sequenceRef);
-    assertEquals(12, aa.annotations.length);
-    assertEquals("+, -, , 2, 3, , , , 3, +, X, , ", aa.toString());
+    assertEquals("MEGA Label", aa.label);
+    assertEquals(18, aa.annotations.length);
+    assertEquals("+, -, , 2, 3, , , , , , , , , , 3, +, X, , ",
+            aa.toString());
   }
 
   //@formatter:on
@@ -809,4 +819,50 @@ public class MegaFileTest
             .getSequenceAsString());
     assertEquals('-', al.getGapCharacter());
   }
+
+  /**
+   * Test reading a MEGA file to an alignment then writing it out in MEGA
+   * format. Includes !Label statements which should be converted to
+   * AlignmentAnnotation and back again.
+   * 
+   * @throws IOException
+   */
+  @Test(groups = "Functional")
+  public void testRoundTrip_withLabels() throws IOException
+  {
+    AppletFormatAdapter fa = new AppletFormatAdapter();
+
+    //@formatter:off
+    String data = "#MEGA\n"
+    + "#U455   C-- GTA\n" 
+    + "#CPZANT ATC -G-\n"
+    + "!Label F__E_H\n\n"
+    + "#U455   CGA --T\n" 
+    + "#CPZANT CA- -GC\n"
+    + "!Label FFH__E\n";
+    AlignmentI al = fa.readFile(data,
+            AppletFormatAdapter.PASTE, "MEGA");
+    AlignmentAnnotation aa = al.getAlignmentAnnotation()[0];
+    assertEquals("MEGA Label", aa.label);
+    assertEquals("F, , , E, , H, F, F, H, , , E, ",
+            aa.toString());
+
+    MegaFile output = new MegaFile();
+    String formatted = output.print(al);
+    String expected = 
+        "#MEGA\n" +
+        "!Format\n" +
+        "    DataType=Nucleotide CodeTable=Standard\n" +
+        "    NSeqs=2 NSites=12\n" +
+        "    Indel=-;\n\n" +
+        "#U455   C-- GTA [6]\n" +
+        "#CPZANT ATC -G- [6]\n" +
+        "!Label F__ E_H;\n\n" +  
+        "#U455   CGA --T [12]\n" +
+        "#CPZANT CA- -GC [12]\n" +
+        "!Label FFH __E;\n";
+    //@formatter:on
+    assertEquals("Roundtrip didn't match", expected,
+            formatted);
+  }
 }