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;
8 import java.util.Arrays;
11 import org.junit.Before;
12 import org.junit.Test;
14 public class SequenceTest
21 seq = new Sequence("FER1", "AKPNGVL");
24 public void testGetAnnotation()
26 // initial state returns null not an empty array
27 assertNull(seq.getAnnotation());
28 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
30 AlignmentAnnotation[] anns = seq.getAnnotation();
31 assertEquals(1, anns.length);
32 assertSame(ann, anns[0]);
34 // removing all annotations reverts array to null
35 seq.removeAlignmentAnnotation(ann);
36 assertNull(seq.getAnnotation());
40 public void testGetAnnotation_forLabel()
42 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
43 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
44 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
45 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
46 assertEquals(2, anns.length);
47 assertSame(ann1, anns[0]);
48 assertSame(ann3, anns[1]);
51 private AlignmentAnnotation addAnnotation(String label,
52 String description, String calcId,
55 final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
57 annotation.setCalcId(calcId);
58 seq.addAlignmentAnnotation(annotation);
63 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
65 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
67 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
69 AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
71 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
73 AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
75 AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
77 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
79 assertEquals(2, anns.size());
80 assertSame(ann2, anns.get(0));
81 assertSame(ann4, anns.get(1));
83 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
84 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
85 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
86 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
87 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
91 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
92 * setting the sequenceRef on the annotation. Adding the same annotation twice
96 public void testAddAlignmentAnnotation()
98 assertNull(seq.annotation);
99 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
101 assertNull(annotation.sequenceRef);
102 seq.addAlignmentAnnotation(annotation);
103 assertSame(seq, annotation.sequenceRef);
104 AlignmentAnnotation[] anns = seq.getAnnotation();
105 assertEquals(1, anns.length);
106 assertSame(annotation, anns[0]);
108 // re-adding does nothing
109 seq.addAlignmentAnnotation(annotation);
110 anns = seq.getAnnotation();
111 assertEquals(1, anns.length);
112 assertSame(annotation, anns[0]);
114 // an identical but different annotation can be added
115 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
117 seq.addAlignmentAnnotation(annotation2);
118 anns = seq.getAnnotation();
119 assertEquals(2, anns.length);
120 assertSame(annotation, anns[0]);
121 assertSame(annotation2, anns[1]);
126 public void testGetStartGetEnd()
128 SequenceI seq = new Sequence("test", "ABCDEF");
129 assertEquals(1, seq.getStart());
130 assertEquals(6, seq.getEnd());
132 seq = new Sequence("test", "--AB-C-DEF--");
133 assertEquals(1, seq.getStart());
134 assertEquals(6, seq.getEnd());
136 seq = new Sequence("test", "----");
137 assertEquals(1, seq.getStart());
138 assertEquals(0, seq.getEnd()); // ??
142 * Tests for the method that returns an alignment column position (base 1) for
143 * a given sequence position (base 1).
146 public void testFindIndex()
148 SequenceI seq = new Sequence("test", "ABCDEF");
149 assertEquals(0, seq.findIndex(0));
150 assertEquals(1, seq.findIndex(1));
151 assertEquals(5, seq.findIndex(5));
152 assertEquals(6, seq.findIndex(6));
153 assertEquals(6, seq.findIndex(9));
155 seq = new Sequence("test", "-A--B-C-D-E-F--");
156 assertEquals(2, seq.findIndex(1));
157 assertEquals(5, seq.findIndex(2));
158 assertEquals(7, seq.findIndex(3));
160 // before start returns 0
161 assertEquals(0, seq.findIndex(0));
162 assertEquals(0, seq.findIndex(-1));
164 // beyond end returns last residue column
165 assertEquals(13, seq.findIndex(99));
170 * Tests for the method that returns a dataset sequence position (base 1) for
171 * an aligned column position (base 0).
174 public void testFindPosition()
176 SequenceI seq = new Sequence("test", "ABCDEF");
177 assertEquals(1, seq.findPosition(0));
178 assertEquals(6, seq.findPosition(5));
179 // assertEquals(-1, seq.findPosition(6)); // fails
181 seq = new Sequence("test", "AB-C-D--");
182 assertEquals(1, seq.findPosition(0));
183 assertEquals(2, seq.findPosition(1));
184 // gap position 'finds' residue to the right (not the left as per javadoc)
185 assertEquals(3, seq.findPosition(2));
186 assertEquals(3, seq.findPosition(3));
187 assertEquals(4, seq.findPosition(4));
188 assertEquals(4, seq.findPosition(5));
189 // returns 1 more than sequence length if off the end ?!?
190 assertEquals(5, seq.findPosition(6));
191 assertEquals(5, seq.findPosition(7));
193 seq = new Sequence("test", "--AB-C-DEF--");
194 assertEquals(1, seq.findPosition(0));
195 assertEquals(1, seq.findPosition(1));
196 assertEquals(1, seq.findPosition(2));
197 assertEquals(2, seq.findPosition(3));
198 assertEquals(3, seq.findPosition(4));
199 assertEquals(3, seq.findPosition(5));
200 assertEquals(4, seq.findPosition(6));
201 assertEquals(4, seq.findPosition(7));
202 assertEquals(5, seq.findPosition(8));
203 assertEquals(6, seq.findPosition(9));
204 assertEquals(7, seq.findPosition(10));
205 assertEquals(7, seq.findPosition(11));
209 public void testDeleteChars()
211 SequenceI seq = new Sequence("test", "ABCDEF");
212 assertEquals(1, seq.getStart());
213 assertEquals(6, seq.getEnd());
214 seq.deleteChars(2, 3);
215 assertEquals("ABDEF", seq.getSequenceAsString());
216 assertEquals(1, seq.getStart());
217 assertEquals(5, seq.getEnd());
219 seq = new Sequence("test", "ABCDEF");
220 seq.deleteChars(0, 2);
221 assertEquals("CDEF", seq.getSequenceAsString());
222 assertEquals(3, seq.getStart());
223 assertEquals(6, seq.getEnd());
227 public void testInsertCharAt()
229 // non-static methods:
230 SequenceI seq = new Sequence("test", "ABCDEF");
231 seq.insertCharAt(0, 'z');
232 assertEquals("zABCDEF", seq.getSequenceAsString());
233 seq.insertCharAt(2, 2, 'x');
234 assertEquals("zAxxBCDEF", seq.getSequenceAsString());
236 // for static method see StringUtilsTest
240 * Test the method that returns an array of aligned sequence positions where
241 * the array index is the data sequence position (both base 0).
244 public void testGapMap()
246 SequenceI seq = new Sequence("test", "-A--B-CD-E--F-");
247 seq.createDatasetSequence();
248 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(seq.gapMap()));