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 testInsertGapsAndGapmaps()
26 SequenceI aseq = seq.deriveSequence();
27 aseq.insertCharAt(2, 3, '-');
28 aseq.insertCharAt(6, 3, '-');
29 assertEquals("Gap insertions not correct", "AK---P---NGVL",
30 aseq.getSequenceAsString());
31 List<int[]> gapInt = aseq.getInsertions();
32 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
33 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
34 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
35 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
39 public void testGetAnnotation()
41 // initial state returns null not an empty array
42 assertNull(seq.getAnnotation());
43 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
45 AlignmentAnnotation[] anns = seq.getAnnotation();
46 assertEquals(1, anns.length);
47 assertSame(ann, anns[0]);
49 // removing all annotations reverts array to null
50 seq.removeAlignmentAnnotation(ann);
51 assertNull(seq.getAnnotation());
55 public void testGetAnnotation_forLabel()
57 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
58 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
59 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
60 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
61 assertEquals(2, anns.length);
62 assertSame(ann1, anns[0]);
63 assertSame(ann3, anns[1]);
66 private AlignmentAnnotation addAnnotation(String label,
67 String description, String calcId,
70 final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
72 annotation.setCalcId(calcId);
73 seq.addAlignmentAnnotation(annotation);
78 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
80 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
82 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
84 AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
86 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
88 AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
90 AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
92 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
94 assertEquals(2, anns.size());
95 assertSame(ann2, anns.get(0));
96 assertSame(ann4, anns.get(1));
98 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
99 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
100 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
101 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
102 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
106 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
107 * setting the sequenceRef on the annotation. Adding the same annotation twice
111 public void testAddAlignmentAnnotation()
113 assertNull(seq.getAnnotation());
114 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
116 assertNull(annotation.sequenceRef);
117 seq.addAlignmentAnnotation(annotation);
118 assertSame(seq, annotation.sequenceRef);
119 AlignmentAnnotation[] anns = seq.getAnnotation();
120 assertEquals(1, anns.length);
121 assertSame(annotation, anns[0]);
123 // re-adding does nothing
124 seq.addAlignmentAnnotation(annotation);
125 anns = seq.getAnnotation();
126 assertEquals(1, anns.length);
127 assertSame(annotation, anns[0]);
129 // an identical but different annotation can be added
130 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
132 seq.addAlignmentAnnotation(annotation2);
133 anns = seq.getAnnotation();
134 assertEquals(2, anns.length);
135 assertSame(annotation, anns[0]);
136 assertSame(annotation2, anns[1]);
141 public void testGetStartGetEnd()
143 SequenceI seq = new Sequence("test", "ABCDEF");
144 assertEquals(1, seq.getStart());
145 assertEquals(6, seq.getEnd());
147 seq = new Sequence("test", "--AB-C-DEF--");
148 assertEquals(1, seq.getStart());
149 assertEquals(6, seq.getEnd());
151 seq = new Sequence("test", "----");
152 assertEquals(1, seq.getStart());
153 assertEquals(0, seq.getEnd()); // ??
157 * Tests for the method that returns an alignment column position (base 1) for
158 * a given sequence position (base 1).
161 public void testFindIndex()
163 SequenceI seq = new Sequence("test", "ABCDEF");
164 assertEquals(0, seq.findIndex(0));
165 assertEquals(1, seq.findIndex(1));
166 assertEquals(5, seq.findIndex(5));
167 assertEquals(6, seq.findIndex(6));
168 assertEquals(6, seq.findIndex(9));
170 seq = new Sequence("test", "-A--B-C-D-E-F--");
171 assertEquals(2, seq.findIndex(1));
172 assertEquals(5, seq.findIndex(2));
173 assertEquals(7, seq.findIndex(3));
175 // before start returns 0
176 assertEquals(0, seq.findIndex(0));
177 assertEquals(0, seq.findIndex(-1));
179 // beyond end returns last residue column
180 assertEquals(13, seq.findIndex(99));
185 * Tests for the method that returns a dataset sequence position (base 1) for
186 * an aligned column position (base 0).
189 public void testFindPosition()
191 SequenceI seq = new Sequence("test", "ABCDEF");
192 assertEquals(1, seq.findPosition(0));
193 assertEquals(6, seq.findPosition(5));
194 // assertEquals(-1, seq.findPosition(6)); // fails
196 seq = new Sequence("test", "AB-C-D--");
197 assertEquals(1, seq.findPosition(0));
198 assertEquals(2, seq.findPosition(1));
199 // gap position 'finds' residue to the right (not the left as per javadoc)
200 assertEquals(3, seq.findPosition(2));
201 assertEquals(3, seq.findPosition(3));
202 assertEquals(4, seq.findPosition(4));
203 assertEquals(4, seq.findPosition(5));
204 // returns 1 more than sequence length if off the end ?!?
205 assertEquals(5, seq.findPosition(6));
206 assertEquals(5, seq.findPosition(7));
208 seq = new Sequence("test", "--AB-C-DEF--");
209 assertEquals(1, seq.findPosition(0));
210 assertEquals(1, seq.findPosition(1));
211 assertEquals(1, seq.findPosition(2));
212 assertEquals(2, seq.findPosition(3));
213 assertEquals(3, seq.findPosition(4));
214 assertEquals(3, seq.findPosition(5));
215 assertEquals(4, seq.findPosition(6));
216 assertEquals(4, seq.findPosition(7));
217 assertEquals(5, seq.findPosition(8));
218 assertEquals(6, seq.findPosition(9));
219 assertEquals(7, seq.findPosition(10));
220 assertEquals(7, seq.findPosition(11));
224 public void testDeleteChars()
226 SequenceI seq = new Sequence("test", "ABCDEF");
227 assertEquals(1, seq.getStart());
228 assertEquals(6, seq.getEnd());
229 seq.deleteChars(2, 3);
230 assertEquals("ABDEF", seq.getSequenceAsString());
231 assertEquals(1, seq.getStart());
232 assertEquals(5, seq.getEnd());
234 seq = new Sequence("test", "ABCDEF");
235 seq.deleteChars(0, 2);
236 assertEquals("CDEF", seq.getSequenceAsString());
237 assertEquals(3, seq.getStart());
238 assertEquals(6, seq.getEnd());
242 public void testInsertCharAt()
244 // non-static methods:
245 SequenceI seq = new Sequence("test", "ABCDEF");
246 seq.insertCharAt(0, 'z');
247 assertEquals("zABCDEF", seq.getSequenceAsString());
248 seq.insertCharAt(2, 2, 'x');
249 assertEquals("zAxxBCDEF", seq.getSequenceAsString());
251 // for static method see StringUtilsTest
255 * Test the method that returns an array of aligned sequence positions where
256 * the array index is the data sequence position (both base 0).
259 public void testGapMap()
261 SequenceI seq = new Sequence("test", "-A--B-CD-E--F-");
262 seq.createDatasetSequence();
263 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(seq.gapMap()));
267 * Test the method that gets sequence features, either from the sequence or
271 public void testGetSequenceFeatures()
273 SequenceI seq = new Sequence("test", "GATCAT");
274 seq.createDatasetSequence();
276 assertNull(seq.getSequenceFeatures());
279 * SequenceFeature on sequence
281 SequenceFeature sf = new SequenceFeature();
282 seq.addSequenceFeature(sf);
283 SequenceFeature[] sfs = seq.getSequenceFeatures();
284 assertEquals(1, sfs.length);
285 assertSame(sf, sfs[0]);
288 * SequenceFeature on sequence and dataset sequence; returns that on
291 SequenceFeature sf2 = new SequenceFeature();
292 seq.getDatasetSequence().addSequenceFeature(sf2);
293 sfs = seq.getSequenceFeatures();
294 assertEquals(1, sfs.length);
295 assertSame(sf, sfs[0]);
298 * SequenceFeature on dataset sequence only
300 seq.setSequenceFeatures(null);
301 sfs = seq.getSequenceFeatures();
302 assertEquals(1, sfs.length);
303 assertSame(sf2, sfs[0]);
306 * Corrupt case - no SequenceFeature, dataset's dataset is the original
307 * sequence. Test shows no infinite loop results.
309 seq.getDatasetSequence().setSequenceFeatures(null);
310 seq.getDatasetSequence().setDatasetSequence(seq); // loop!
311 assertNull(seq.getSequenceFeatures());
315 * Test the method that returns an array, indexed by sequence position, whose
316 * entries are the residue positions at the sequence position (or to the right
320 public void testFindPositionMap()
323 * Note: Javadoc for findPosition says it returns the residue position to
324 * the left of a gapped position; in fact it returns the position to the
325 * right. Also it returns a non-existent residue position for a gap beyond
328 Sequence seq = new Sequence("TestSeq", "AB.C-D E.");
329 int[] map = seq.findPositionMap();
330 assertEquals(Arrays.toString(new int[]
331 { 1, 2, 3, 3, 4, 4, 5, 5, 6 }), Arrays.toString(map));
335 * Test for getSubsequence
338 public void testGetSubsequence()
340 SequenceI seq = new Sequence("TestSeq", "ABCDEFG");
341 seq.createDatasetSequence();
343 // positions are base 0, end position is exclusive
344 SequenceI subseq = seq.getSubSequence(2, 4);
346 assertEquals("CD", subseq.getSequenceAsString());
347 // start/end are base 1 positions
348 assertEquals(3, subseq.getStart());
349 assertEquals(4, subseq.getEnd());
350 // subsequence shares the full dataset sequence
351 assertSame(seq.getDatasetSequence(), subseq.getDatasetSequence());
355 * Test for deriveSequence applied to a sequence with a dataset
358 public void testDeriveSequence_existingDataset()
360 SequenceI seq = new Sequence("Seq1", "CD");
361 seq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
364 SequenceI derived = seq.deriveSequence();
365 assertEquals("CD", derived.getSequenceAsString());
366 assertSame(seq.getDatasetSequence(), derived.getDatasetSequence());
370 * Test for deriveSequence applied to an ungapped sequence with no dataset
373 public void testDeriveSequence_noDatasetUngapped()
375 SequenceI seq = new Sequence("Seq1", "ABCDEF");
376 assertEquals(1, seq.getStart());
377 assertEquals(6, seq.getEnd());
378 SequenceI derived = seq.deriveSequence();
379 assertEquals("ABCDEF", derived.getSequenceAsString());
380 assertEquals("ABCDEF", derived.getDatasetSequence()
381 .getSequenceAsString());
385 * Test for deriveSequence applied to a gapped sequence with no dataset
388 public void testDeriveSequence_noDatasetGapped()
390 SequenceI seq = new Sequence("Seq1", "AB-C.D EF");
391 assertEquals(1, seq.getStart());
392 assertEquals(6, seq.getEnd());
393 assertNull(seq.getDatasetSequence());
394 SequenceI derived = seq.deriveSequence();
395 assertEquals("AB-C.D EF", derived.getSequenceAsString());
396 assertEquals("ABCDEF", derived.getDatasetSequence()
397 .getSequenceAsString());