1 package jalview.datamodel;
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;
10 import org.junit.Before;
11 import org.junit.Test;
13 public class SequenceTest
20 seq = new Sequence("FER1", "AKPNGVL");
23 public void testGetAnnotation()
25 // initial state returns null not an empty array
26 assertNull(seq.getAnnotation());
27 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
29 AlignmentAnnotation[] anns = seq.getAnnotation();
30 assertEquals(1, anns.length);
31 assertSame(ann, anns[0]);
33 // removing all annotations reverts array to null
34 seq.removeAlignmentAnnotation(ann);
35 assertNull(seq.getAnnotation());
39 public void testGetAnnotation_forLabel()
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]);
50 private AlignmentAnnotation addAnnotation(String label,
51 String description, String calcId,
54 final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
56 annotation.setCalcId(calcId);
57 seq.addAlignmentAnnotation(annotation);
62 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
64 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
66 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
68 AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
70 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
72 AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
74 AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
76 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
78 assertEquals(2, anns.size());
79 assertSame(ann2, anns.get(0));
80 assertSame(ann4, anns.get(1));
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());
90 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
91 * setting the sequenceRef on the annotation. Adding the same annotation twice
95 public void testAddAlignmentAnnotation()
97 assertNull(seq.annotation);
98 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
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]);
107 // re-adding does nothing
108 seq.addAlignmentAnnotation(annotation);
109 anns = seq.getAnnotation();
110 assertEquals(1, anns.length);
111 assertSame(annotation, anns[0]);
113 // an identical but different annotation can be added
114 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
116 seq.addAlignmentAnnotation(annotation2);
117 anns = seq.getAnnotation();
118 assertEquals(2, anns.length);
119 assertSame(annotation, anns[0]);
120 assertSame(annotation2, anns[1]);
125 public void testGetStartGetEnd()
127 SequenceI seq = new Sequence("test", "ABCDEF");
128 assertEquals(1, seq.getStart());
129 assertEquals(6, seq.getEnd());
131 seq = new Sequence("test", "--AB-C-DEF--");
132 assertEquals(1, seq.getStart());
133 assertEquals(6, seq.getEnd());
135 seq = new Sequence("test", "----");
136 assertEquals(1, seq.getStart());
137 assertEquals(0, seq.getEnd()); // ??
141 public void testFindPosition()
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
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));
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));
176 public void testDeleteChars()
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());
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());
194 public void testInsertCharAt()
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());
203 // for static method see StringUtilsTest