JAL-2843 add isByAttribute to interface for convenience
[jalview.git] / test / jalview / datamodel / AlignmentAnnotationTests.java
index fd292d3..e47e9d6 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
- * Copyright (C) 2015 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -24,12 +24,24 @@ import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertNull;
 
 import jalview.analysis.AlignSeq;
+import jalview.gui.JvOptionPane;
 import jalview.io.AppletFormatAdapter;
+import jalview.io.FileFormat;
 
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class AlignmentAnnotationTests
 {
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   @Test(groups = { "Functional" })
   public void testCopyConstructor()
   {
@@ -119,7 +131,8 @@ public class AlignmentAnnotationTests
     alSeq2.setEnd(sqTo.getStart() + align.getSeq2End() - 1);
     alSeq2.setDatasetSequence(sqTo);
     System.out.println(new AppletFormatAdapter()
-            .formatSequences("STH", new Alignment(new SequenceI[] { sqFrom,
+.formatSequences(
+            FileFormat.Stockholm, new Alignment(new SequenceI[] { sqFrom,
                 alSeq1, sqTo, alSeq2 }), true));
 
     Mapping mp = align.getMappingFromS1(false);
@@ -143,7 +156,8 @@ public class AlignmentAnnotationTests
     AlignmentI all = new Alignment(new SequenceI[] { alSeq1, alSeq2 });
     all.addAnnotation(almap1);
     all.addAnnotation(almap2);
-    System.out.println(new AppletFormatAdapter().formatSequences("STH",
+    System.out.println(new AppletFormatAdapter().formatSequences(
+            FileFormat.Stockholm,
             all, true));
 
     for (int p = 0; p < alSeq1.getLength(); p++)
@@ -219,4 +233,106 @@ public class AlignmentAnnotationTests
     assertEquals(1, ann.annotations[1].value, 0.001);
     assertEquals(2, ann.annotations[2].value, 0.001);
   }
+
+  /**
+   * Test the method that defaults rna symbol to the one matching the preceding
+   * unmatched opening bracket (if any)
+   */
+  @Test(groups = { "Functional" })
+  public void testGetDefaultRnaHelixSymbol()
+  {
+    AlignmentAnnotation ann = new AlignmentAnnotation("SS",
+            "secondary structure", null);
+    assertEquals("(", ann.getDefaultRnaHelixSymbol(4));
+
+    Annotation[] anns = new Annotation[20];
+    ann.annotations = anns;
+    assertEquals("(", ann.getDefaultRnaHelixSymbol(4));
+
+    anns[1] = new Annotation("(", "S", '(', 0f);
+    assertEquals("(", ann.getDefaultRnaHelixSymbol(0));
+    assertEquals("(", ann.getDefaultRnaHelixSymbol(1));
+    assertEquals(")", ann.getDefaultRnaHelixSymbol(2));
+    assertEquals(")", ann.getDefaultRnaHelixSymbol(3));
+
+    /*
+     * .(.[.{.<.}.>.).].
+     */
+    anns[1] = new Annotation("(", "S", '(', 0f);
+    anns[3] = new Annotation("[", "S", '[', 0f);
+    anns[5] = new Annotation("{", "S", '{', 0f);
+    anns[7] = new Annotation("<", "S", '<', 0f);
+    anns[9] = new Annotation("}", "S", '}', 0f);
+    anns[11] = new Annotation(">", "S", '>', 0f);
+    anns[13] = new Annotation(")", "S", ')', 0f);
+    anns[15] = new Annotation("]", "S", ']', 0f);
+
+    String expected = "(())]]}}>>>>]]]](";
+    for (int i = 0; i < expected.length(); i++)
+    {
+      assertEquals("column " + i, String.valueOf(expected.charAt(i)),
+              ann.getDefaultRnaHelixSymbol(i));
+    }
+
+    /*
+     * .(.[.(.).{.}.<.].D.
+     */
+    anns[1] = new Annotation("(", "S", '(', 0f);
+    anns[3] = new Annotation("[", "S", '[', 0f);
+    anns[5] = new Annotation("(", "S", '(', 0f);
+    anns[7] = new Annotation(")", "S", ')', 0f);
+    anns[9] = new Annotation("{", "S", '{', 0f);
+    anns[11] = new Annotation("}", "S", '}', 0f);
+    anns[13] = new Annotation("<", "S", '>', 0f);
+    anns[15] = new Annotation("]", "S", ']', 0f);
+    anns[17] = new Annotation("D", "S", 'D', 0f);
+
+    expected = "(())]]))]]}}]]>>>>dd";
+    for (int i = 0; i < expected.length(); i++)
+    {
+      assertEquals("column " + i, String.valueOf(expected.charAt(i)),
+              ann.getDefaultRnaHelixSymbol(i));
+    }
+  }
+
+  public static Annotation newAnnotation(String ann)
+  {
+    float val = 0f;
+    try
+    {
+      val = Float.parseFloat(ann);
+    } catch (NumberFormatException q)
+    {
+    }
+    ;
+    return new Annotation(ann, ann, '\0', val);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testIsQuantitative()
+  {
+    AlignmentAnnotation ann = null;
+
+    ann = new AlignmentAnnotation("an", "some an", null);
+    Assert.assertFalse(ann.isQuantitative(),
+            "Empty annotation set should not be quantitative.");
+
+    ann = new AlignmentAnnotation("an", "some an", new Annotation[] {
+        newAnnotation("4"), newAnnotation("1"), newAnnotation("1"),
+        newAnnotation("0.1"), newAnnotation("0.3") });
+    Assert.assertTrue(ann.isQuantitative(),
+            "All numbers annotation set should be quantitative.");
+
+    ann = new AlignmentAnnotation("an", "some an", new Annotation[] {
+        newAnnotation("E"), newAnnotation("E"), newAnnotation("E"),
+        newAnnotation("E"), newAnnotation("E") });
+    Assert.assertFalse(ann.isQuantitative(),
+            "All 'E' annotation set should not be quantitative.");
+
+    ann = new AlignmentAnnotation("an", "some an", new Annotation[] {
+        newAnnotation("E"), newAnnotation("1"), newAnnotation("2"),
+        newAnnotation("3"), newAnnotation("E") });
+    Assert.assertTrue(ann.isQuantitative(),
+            "Mixed 'E' annotation set should be quantitative.");
+  }
 }