JAL-845 first working linked edit protein -> cDNA
[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   Sequence seq;
16
17   @Before
18   public void setUp()
19   {
20     seq = new Sequence("FER1", "AKPNGVL");
21   }
22   @Test
23   public void testGetAnnotation()
24   {
25     // initial state returns null not an empty array
26     assertNull(seq.getAnnotation());
27     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
28             1f);
29     AlignmentAnnotation[] anns = seq.getAnnotation();
30     assertEquals(1, anns.length);
31     assertSame(ann, anns[0]);
32
33     // removing all annotations reverts array to null
34     seq.removeAlignmentAnnotation(ann);
35     assertNull(seq.getAnnotation());
36   }
37
38   @Test
39   public void testGetAnnotation_forLabel()
40   {
41     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
42     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
43     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
44     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
45     assertEquals(2, anns.length);
46     assertSame(ann1, anns[0]);
47     assertSame(ann3, anns[1]);
48   }
49
50   private AlignmentAnnotation addAnnotation(String label,
51           String description, String calcId,
52           float value)
53   {
54     final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
55             value);
56     annotation.setCalcId(calcId);
57     seq.addAlignmentAnnotation(annotation);
58     return annotation;
59   }
60
61   @Test
62   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
63   {
64     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
65             1f);
66     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
67             1f);
68     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
69             1f);
70     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
71             1f);
72     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
73             1f);
74     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
75             1f);
76     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
77             "label2");
78     assertEquals(2, anns.size());
79     assertSame(ann2, anns.get(0));
80     assertSame(ann4, anns.get(1));
81     
82     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
83     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
84     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
85     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
86     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
87   }
88
89   /**
90    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
91    * setting the sequenceRef on the annotation. Adding the same annotation twice
92    * should be ignored.
93    */
94   @Test
95   public void testAddAlignmentAnnotation()
96   {
97     assertNull(seq.annotation);
98     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
99             "b", 2d);
100     assertNull(annotation.sequenceRef);
101     seq.addAlignmentAnnotation(annotation);
102     assertSame(seq, annotation.sequenceRef);
103     AlignmentAnnotation[] anns = seq.getAnnotation();
104     assertEquals(1, anns.length);
105     assertSame(annotation, anns[0]);
106
107     // re-adding does nothing
108     seq.addAlignmentAnnotation(annotation);
109     anns = seq.getAnnotation();
110     assertEquals(1, anns.length);
111     assertSame(annotation, anns[0]);
112
113     // an identical but different annotation can be added
114     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
115             "b", 2d);
116     seq.addAlignmentAnnotation(annotation2);
117     anns = seq.getAnnotation();
118     assertEquals(2, anns.length);
119     assertSame(annotation, anns[0]);
120     assertSame(annotation2, anns[1]);
121
122   }
123
124   @Test
125   public void testGetStartGetEnd()
126   {
127     SequenceI seq = new Sequence("test", "ABCDEF");
128     assertEquals(1, seq.getStart());
129     assertEquals(6, seq.getEnd());
130
131     seq = new Sequence("test", "--AB-C-DEF--");
132     assertEquals(1, seq.getStart());
133     assertEquals(6, seq.getEnd());
134
135     seq = new Sequence("test", "----");
136     assertEquals(1, seq.getStart());
137     assertEquals(0, seq.getEnd()); // ??
138   }
139
140   @Test
141   public void testFindPosition()
142   {
143     SequenceI seq = new Sequence("test", "ABCDEF");
144     assertEquals(1, seq.findPosition(0));
145     assertEquals(6, seq.findPosition(5));
146     // assertEquals(-1, seq.findPosition(6)); // fails
147
148     seq = new Sequence("test", "AB-C-D--");
149     assertEquals(1, seq.findPosition(0));
150     assertEquals(2, seq.findPosition(1));
151     // gap position 'finds' residue to the right (not the left as per javadoc)
152     assertEquals(3, seq.findPosition(2));
153     assertEquals(3, seq.findPosition(3));
154     assertEquals(4, seq.findPosition(4));
155     assertEquals(4, seq.findPosition(5));
156     // returns 1 more than sequence length if off the end ?!?
157     assertEquals(5, seq.findPosition(6));
158     assertEquals(5, seq.findPosition(7));
159
160     seq = new Sequence("test", "--AB-C-DEF--");
161     assertEquals(1, seq.findPosition(0));
162     assertEquals(1, seq.findPosition(1));
163     assertEquals(1, seq.findPosition(2));
164     assertEquals(2, seq.findPosition(3));
165     assertEquals(3, seq.findPosition(4));
166     assertEquals(3, seq.findPosition(5));
167     assertEquals(4, seq.findPosition(6));
168     assertEquals(4, seq.findPosition(7));
169     assertEquals(5, seq.findPosition(8));
170     assertEquals(6, seq.findPosition(9));
171     assertEquals(7, seq.findPosition(10));
172     assertEquals(7, seq.findPosition(11));
173   }
174
175   @Test
176   public void testDeleteChars()
177   {
178     SequenceI seq = new Sequence("test", "ABCDEF");
179     assertEquals(1, seq.getStart());
180     assertEquals(6, seq.getEnd());
181     seq.deleteChars(2, 3);
182     assertEquals("ABDEF", seq.getSequenceAsString());
183     assertEquals(1, seq.getStart());
184     assertEquals(5, seq.getEnd());
185
186     seq = new Sequence("test", "ABCDEF");
187     seq.deleteChars(0, 2);
188     assertEquals("CDEF", seq.getSequenceAsString());
189     assertEquals(3, seq.getStart());
190     assertEquals(6, seq.getEnd());
191   }
192
193   @Test
194   public void testInsertCharAt()
195   {
196     // non-static methods:
197     SequenceI seq = new Sequence("test", "ABCDEF");
198     seq.insertCharAt(0, 'z');
199     assertEquals("zABCDEF", seq.getSequenceAsString());
200     seq.insertCharAt(2, 2, 'x');
201     assertEquals("zAxxBCDEF", seq.getSequenceAsString());
202     
203     // for static method see StringUtilsTest
204   }
205 }