JAL-2907 test method renamed to match method name change
[jalview.git] / test / jalview / io / StockholmFileTest.java
index 4273e6c..14fba70 100644 (file)
@@ -22,17 +22,21 @@ package jalview.io;
 
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertNotNull;
+import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertTrue;
 import static org.testng.AssertJUnit.fail;
 
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.JvOptionPane;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.HashMap;
@@ -230,8 +234,8 @@ public class StockholmFileTest
     // we might want to revise this in future
     int aa_new_size = (aa_new == null ? 0 : aa_new.length);
     int aa_original_size = (aa_original == null ? 0 : aa_original.length);
-    Map<Integer, BitSet> orig_groups = new HashMap<Integer, BitSet>();
-    Map<Integer, BitSet> new_groups = new HashMap<Integer, BitSet>();
+    Map<Integer, BitSet> orig_groups = new HashMap<>();
+    Map<Integer, BitSet> new_groups = new HashMap<>();
 
     if (aa_new != null && aa_original != null)
     {
@@ -654,4 +658,112 @@ public class StockholmFileTest
     testAlignmentEquivalence(al, newAl, true, true, true);
 
   }
+
+  @Test(groups = "Functional")
+  public void testType2id()
+  {
+    assertEquals("OS", StockholmFile.type2id("organism"));
+    // not case-sensitive:
+    assertEquals("OS", StockholmFile.type2id("Organism"));
+    // is space-sensitive:
+    assertNull(StockholmFile.type2id("Organism "));
+    assertNull(StockholmFile.type2id("orgasm"));
+  }
+
+  @Test(groups = "Functional")
+  public void testGetAnnotationCharacter()
+  {
+    SequenceI seq = new Sequence("seq", "abc--def-");
+
+    Annotation[] ann = new Annotation[8];
+    ann[1] = new Annotation("Z", "desc", 'E', 1f);
+    ann[2] = new Annotation("Q", "desc", ' ', 1f);
+    ann[4] = new Annotation("", "desc", 'E', 1f);
+    ann[6] = new Annotation("ZH", "desc", 'E', 1f);
+
+    /*
+     * null annotation in column (not Secondary Structure annotation)
+     * should answer sequence character, or '-' if null sequence
+     */
+    assertEquals('-', StockholmFile.outputCharacter("RF", 0, ann, null));
+    assertEquals('d', StockholmFile.outputCharacter("RF", 5, ann, seq));
+    assertEquals('-', StockholmFile.outputCharacter("RF", 8, ann, seq));
+
+    /*
+     * null annotation in column (SS annotation) should answer underscore
+     */
+    assertEquals('_', StockholmFile.outputCharacter("SS", 0, ann, seq));
+
+    /*
+     * SS secondary structure symbol
+     */
+    assertEquals('E', StockholmFile.outputCharacter("SS", 1, ann, seq));
+
+    /*
+     * no SS symbol, use label instead 
+     */
+    assertEquals('Q', StockholmFile.outputCharacter("SS", 2, ann, seq));
+
+    /*
+     * SS with 2 character label - second character overrides SS symbol 
+     */
+    assertEquals('H', StockholmFile.outputCharacter("SS", 6, ann, seq));
+
+    /*
+     * empty display character, not SS - answers '.'
+     */
+    assertEquals('.', StockholmFile.outputCharacter("RF", 4, ann, seq));
+  }
+
+  /**
+   * Test to verify that gaps are input/output as underscore in STO annotation
+   * 
+   * @throws IOException
+   */
+  @Test(groups = "Functional")
+  public void testRoundtripWithGaps() throws IOException
+  {
+    /*
+     * small extract from RF00031_folded.stk
+     */
+    // @formatter:off
+    String stoData = 
+            "# STOCKHOLM 1.0\n" +
+            "#=GR B.taurus.4 SS .._((.))_\n" +
+            "B.taurus.4         AC.UGCGU.\n" +
+            "#=GR B.taurus.5 SS ..((_._))\n" +
+            "B.taurus.5         ACUU.G.CG\n" +
+        "//\n";
+    // @formatter:on
+    StockholmFile parser = new StockholmFile(stoData, DataSourceType.PASTE);
+    SequenceI[] seqs = parser.getSeqsAsArray();
+    assertEquals(2, seqs.length);
+
+    /*
+     * B.taurus.4 has a trailing gap
+     * rendered as underscore in Stockholm annotation
+     */
+    assertEquals("AC.UGCGU.", seqs[0].getSequenceAsString());
+    AlignmentAnnotation[] anns = seqs[0].getAnnotation();
+    assertEquals(1, anns.length);
+    AlignmentAnnotation taurus4SS = anns[0];
+    assertEquals(9, taurus4SS.annotations.length);
+    assertEquals(" .", taurus4SS.annotations[0].displayCharacter);
+    assertNull(taurus4SS.annotations[2]); // gapped position
+    assertNull(taurus4SS.annotations[8]); // gapped position
+    assertEquals('(', taurus4SS.annotations[3].secondaryStructure);
+    assertEquals("(", taurus4SS.annotations[3].displayCharacter);
+    assertEquals(')', taurus4SS.annotations[7].secondaryStructure);
+    
+    /*
+     * output as Stockholm and verify it matches the original input
+     * (gaps output as underscore in annotation lines)
+     * note: roundtrip test works with the input lines ordered as above;
+     * can also parse in other orders, but then input doesn't match output
+     */
+    AlignmentFileWriterI afile = FileFormat.Stockholm
+            .getWriter(new Alignment(seqs));
+    String output = afile.print(seqs, false);
+    assertEquals(stoData, output);
+  }
 }