40476a0788240f0ddaa478914ca6eecfb8ba38f5
[jalview.git] / test / jalview / datamodel / SequenceTest.java
1 package jalview.datamodel;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
7
8 import java.util.List;
9
10 import org.junit.Before;
11 import org.junit.Test;
12
13 public class SequenceTest
14 {
15   SequenceI seq;
16
17   @Before
18   public void setUp()
19   {
20     seq = new Sequence("FER1", "AKPNGVL");
21   }
22   @Test
23   public void testInsertGapsAndGapmaps()
24   {
25     SequenceI aseq = seq.deriveSequence();
26     aseq.insertCharAt(2, 3, '-');
27     aseq.insertCharAt(6, 3, '-');
28     assertEquals("Gap insertions not correct", "AK---P---NGVL",
29             aseq.getSequenceAsString());
30     List<int[]> gapInt = aseq.getInsertions();
31     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
32     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
33     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
34     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
35   }
36
37   @Test
38   public void testGetAnnotation()
39   {
40     // initial state returns null not an empty array
41     assertNull(seq.getAnnotation());
42     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
43             1f);
44     AlignmentAnnotation[] anns = seq.getAnnotation();
45     assertEquals(1, anns.length);
46     assertSame(ann, anns[0]);
47
48     // removing all annotations reverts array to null
49     seq.removeAlignmentAnnotation(ann);
50     assertNull(seq.getAnnotation());
51   }
52
53   @Test
54   public void testGetAnnotation_forLabel()
55   {
56     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
57     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
58     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
59     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
60     assertEquals(2, anns.length);
61     assertSame(ann1, anns[0]);
62     assertSame(ann3, anns[1]);
63   }
64
65   private AlignmentAnnotation addAnnotation(String label,
66           String description, String calcId,
67           float value)
68   {
69     final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
70             value);
71     annotation.setCalcId(calcId);
72     seq.addAlignmentAnnotation(annotation);
73     return annotation;
74   }
75
76   @Test
77   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
78   {
79     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
80             1f);
81     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
82             1f);
83     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
84             1f);
85     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
86             1f);
87     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
88             1f);
89     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
90             1f);
91     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
92             "label2");
93     assertEquals(2, anns.size());
94     assertSame(ann2, anns.get(0));
95     assertSame(ann4, anns.get(1));
96     
97     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
98     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
99     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
100     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
101     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
102   }
103
104   /**
105    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
106    * setting the sequenceRef on the annotation. Adding the same annotation twice
107    * should be ignored.
108    */
109   @Test
110   public void testAddAlignmentAnnotation()
111   {
112     assertNull(seq.getAnnotation());
113     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
114             "b", 2d);
115     assertNull(annotation.sequenceRef);
116     seq.addAlignmentAnnotation(annotation);
117     assertSame(seq, annotation.sequenceRef);
118     AlignmentAnnotation[] anns = seq.getAnnotation();
119     assertEquals(1, anns.length);
120     assertSame(annotation, anns[0]);
121
122     // re-adding does nothing
123     seq.addAlignmentAnnotation(annotation);
124     anns = seq.getAnnotation();
125     assertEquals(1, anns.length);
126     assertSame(annotation, anns[0]);
127
128     // an identical but different annotation can be added
129     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
130             "b", 2d);
131     seq.addAlignmentAnnotation(annotation2);
132     anns = seq.getAnnotation();
133     assertEquals(2, anns.length);
134     assertSame(annotation, anns[0]);
135     assertSame(annotation2, anns[1]);
136
137   }
138 }