2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
30 import jalview.datamodel.PDBEntry.Type;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Vector;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
39 public class SequenceTest
43 @BeforeMethod(alwaysRun = true)
46 seq = new Sequence("FER1", "AKPNGVL");
49 @Test(groups = { "Functional" })
50 public void testInsertGapsAndGapmaps()
52 SequenceI aseq = seq.deriveSequence();
53 aseq.insertCharAt(2, 3, '-');
54 aseq.insertCharAt(6, 3, '-');
55 assertEquals("Gap insertions not correct", "AK---P---NGVL",
56 aseq.getSequenceAsString());
57 List<int[]> gapInt = aseq.getInsertions();
58 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
59 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
60 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
61 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
64 @Test(groups = { "Functional" })
65 public void testGetAnnotation()
67 // initial state returns null not an empty array
68 assertNull(seq.getAnnotation());
69 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
71 AlignmentAnnotation[] anns = seq.getAnnotation();
72 assertEquals(1, anns.length);
73 assertSame(ann, anns[0]);
75 // removing all annotations reverts array to null
76 seq.removeAlignmentAnnotation(ann);
77 assertNull(seq.getAnnotation());
80 @Test(groups = { "Functional" })
81 public void testGetAnnotation_forLabel()
83 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
85 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
87 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
89 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
90 assertEquals(2, anns.length);
91 assertSame(ann1, anns[0]);
92 assertSame(ann3, anns[1]);
95 private AlignmentAnnotation addAnnotation(String label,
96 String description, String calcId, float value)
98 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
100 annotation.setCalcId(calcId);
101 seq.addAlignmentAnnotation(annotation);
105 @Test(groups = { "Functional" })
106 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
108 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
110 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
112 AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
114 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
116 AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null, 1f);
117 AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3", 1f);
118 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
120 assertEquals(2, anns.size());
121 assertSame(ann2, anns.get(0));
122 assertSame(ann4, anns.get(1));
124 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
125 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
126 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
127 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
128 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
132 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
133 * setting the sequenceRef on the annotation. Adding the same annotation twice
136 @Test(groups = { "Functional" })
137 public void testAddAlignmentAnnotation()
139 assertNull(seq.getAnnotation());
140 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
142 assertNull(annotation.sequenceRef);
143 seq.addAlignmentAnnotation(annotation);
144 assertSame(seq, annotation.sequenceRef);
145 AlignmentAnnotation[] anns = seq.getAnnotation();
146 assertEquals(1, anns.length);
147 assertSame(annotation, anns[0]);
149 // re-adding does nothing
150 seq.addAlignmentAnnotation(annotation);
151 anns = seq.getAnnotation();
152 assertEquals(1, anns.length);
153 assertSame(annotation, anns[0]);
155 // an identical but different annotation can be added
156 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
158 seq.addAlignmentAnnotation(annotation2);
159 anns = seq.getAnnotation();
160 assertEquals(2, anns.length);
161 assertSame(annotation, anns[0]);
162 assertSame(annotation2, anns[1]);
165 @Test(groups = { "Functional" })
166 public void testGetStartGetEnd()
168 SequenceI sq = new Sequence("test", "ABCDEF");
169 assertEquals(1, sq.getStart());
170 assertEquals(6, sq.getEnd());
172 sq = new Sequence("test", "--AB-C-DEF--");
173 assertEquals(1, sq.getStart());
174 assertEquals(6, sq.getEnd());
176 sq = new Sequence("test", "----");
177 assertEquals(1, sq.getStart());
178 assertEquals(0, sq.getEnd()); // ??
182 * Tests for the method that returns an alignment column position (base 1) for
183 * a given sequence position (base 1).
185 @Test(groups = { "Functional" })
186 public void testFindIndex()
188 SequenceI sq = new Sequence("test", "ABCDEF");
189 assertEquals(0, sq.findIndex(0));
190 assertEquals(1, sq.findIndex(1));
191 assertEquals(5, sq.findIndex(5));
192 assertEquals(6, sq.findIndex(6));
193 assertEquals(6, sq.findIndex(9));
195 sq = new Sequence("test", "-A--B-C-D-E-F--");
196 assertEquals(2, sq.findIndex(1));
197 assertEquals(5, sq.findIndex(2));
198 assertEquals(7, sq.findIndex(3));
200 // before start returns 0
201 assertEquals(0, sq.findIndex(0));
202 assertEquals(0, sq.findIndex(-1));
204 // beyond end returns last residue column
205 assertEquals(13, sq.findIndex(99));
210 * Tests for the method that returns a dataset sequence position (base 1) for
211 * an aligned column position (base 0).
213 @Test(groups = { "Functional" })
214 public void testFindPosition()
216 SequenceI sq = new Sequence("test", "ABCDEF");
217 assertEquals(1, sq.findPosition(0));
218 assertEquals(6, sq.findPosition(5));
219 // assertEquals(-1, seq.findPosition(6)); // fails
221 sq = new Sequence("test", "AB-C-D--");
222 assertEquals(1, sq.findPosition(0));
223 assertEquals(2, sq.findPosition(1));
224 // gap position 'finds' residue to the right (not the left as per javadoc)
225 assertEquals(3, sq.findPosition(2));
226 assertEquals(3, sq.findPosition(3));
227 assertEquals(4, sq.findPosition(4));
228 assertEquals(4, sq.findPosition(5));
229 // returns 1 more than sequence length if off the end ?!?
230 assertEquals(5, sq.findPosition(6));
231 assertEquals(5, sq.findPosition(7));
233 sq = new Sequence("test", "--AB-C-DEF--");
234 assertEquals(1, sq.findPosition(0));
235 assertEquals(1, sq.findPosition(1));
236 assertEquals(1, sq.findPosition(2));
237 assertEquals(2, sq.findPosition(3));
238 assertEquals(3, sq.findPosition(4));
239 assertEquals(3, sq.findPosition(5));
240 assertEquals(4, sq.findPosition(6));
241 assertEquals(4, sq.findPosition(7));
242 assertEquals(5, sq.findPosition(8));
243 assertEquals(6, sq.findPosition(9));
244 assertEquals(7, sq.findPosition(10));
245 assertEquals(7, sq.findPosition(11));
248 @Test(groups = { "Functional" })
249 public void testDeleteChars()
251 SequenceI sq = new Sequence("test", "ABCDEF");
252 assertEquals(1, sq.getStart());
253 assertEquals(6, sq.getEnd());
254 sq.deleteChars(2, 3);
255 assertEquals("ABDEF", sq.getSequenceAsString());
256 assertEquals(1, sq.getStart());
257 assertEquals(5, sq.getEnd());
259 sq = new Sequence("test", "ABCDEF");
260 sq.deleteChars(0, 2);
261 assertEquals("CDEF", sq.getSequenceAsString());
262 assertEquals(3, sq.getStart());
263 assertEquals(6, sq.getEnd());
266 @Test(groups = { "Functional" })
267 public void testInsertCharAt()
269 // non-static methods:
270 SequenceI sq = new Sequence("test", "ABCDEF");
271 sq.insertCharAt(0, 'z');
272 assertEquals("zABCDEF", sq.getSequenceAsString());
273 sq.insertCharAt(2, 2, 'x');
274 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
276 // for static method see StringUtilsTest
280 * Test the method that returns an array of aligned sequence positions where
281 * the array index is the data sequence position (both base 0).
283 @Test(groups = { "Functional" })
284 public void testGapMap()
286 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
287 sq.createDatasetSequence();
288 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
292 * Test the method that gets sequence features, either from the sequence or
295 @Test(groups = { "Functional" })
296 public void testGetSequenceFeatures()
298 SequenceI sq = new Sequence("test", "GATCAT");
299 sq.createDatasetSequence();
301 assertNull(sq.getSequenceFeatures());
304 * SequenceFeature on sequence
306 SequenceFeature sf = new SequenceFeature();
307 sq.addSequenceFeature(sf);
308 SequenceFeature[] sfs = sq.getSequenceFeatures();
309 assertEquals(1, sfs.length);
310 assertSame(sf, sfs[0]);
313 * SequenceFeature on sequence and dataset sequence; returns that on
316 SequenceFeature sf2 = new SequenceFeature();
317 sq.getDatasetSequence().addSequenceFeature(sf2);
318 sfs = sq.getSequenceFeatures();
319 assertEquals(1, sfs.length);
320 assertSame(sf, sfs[0]);
323 * SequenceFeature on dataset sequence only
325 sq.setSequenceFeatures(null);
326 sfs = sq.getSequenceFeatures();
327 assertEquals(1, sfs.length);
328 assertSame(sf2, sfs[0]);
331 * Corrupt case - no SequenceFeature, dataset's dataset is the original
332 * sequence. Test shows no infinite loop results.
334 sq.getDatasetSequence().setSequenceFeatures(null);
335 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
336 assertNull(sq.getSequenceFeatures());
340 * Test the method that returns an array, indexed by sequence position, whose
341 * entries are the residue positions at the sequence position (or to the right
344 @Test(groups = { "Functional" })
345 public void testFindPositionMap()
348 * Note: Javadoc for findPosition says it returns the residue position to
349 * the left of a gapped position; in fact it returns the position to the
350 * right. Also it returns a non-existent residue position for a gap beyond
353 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
354 int[] map = sq.findPositionMap();
355 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
356 Arrays.toString(map));
360 * Test for getSubsequence
362 @Test(groups = { "Functional" })
363 public void testGetSubsequence()
365 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
366 sq.createDatasetSequence();
368 // positions are base 0, end position is exclusive
369 SequenceI subseq = sq.getSubSequence(2, 4);
371 assertEquals("CD", subseq.getSequenceAsString());
372 // start/end are base 1 positions
373 assertEquals(3, subseq.getStart());
374 assertEquals(4, subseq.getEnd());
375 // subsequence shares the full dataset sequence
376 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
380 * Test for deriveSequence applied to a sequence with a dataset
382 @Test(groups = { "Functional" })
383 public void testDeriveSequence_existingDataset()
385 SequenceI sq = new Sequence("Seq1", "CD");
386 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
389 SequenceI derived = sq.deriveSequence();
390 assertEquals("CD", derived.getSequenceAsString());
391 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
395 * Test for deriveSequence applied to an ungapped sequence with no dataset
397 @Test(groups = { "Functional" })
398 public void testDeriveSequence_noDatasetUngapped()
400 SequenceI sq = new Sequence("Seq1", "ABCDEF");
401 assertEquals(1, sq.getStart());
402 assertEquals(6, sq.getEnd());
403 SequenceI derived = sq.deriveSequence();
404 assertEquals("ABCDEF", derived.getSequenceAsString());
405 assertEquals("ABCDEF", derived.getDatasetSequence()
406 .getSequenceAsString());
410 * Test for deriveSequence applied to a gapped sequence with no dataset
412 @Test(groups = { "Functional" })
413 public void testDeriveSequence_noDatasetGapped()
415 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
416 assertEquals(1, sq.getStart());
417 assertEquals(6, sq.getEnd());
418 assertNull(sq.getDatasetSequence());
419 SequenceI derived = sq.deriveSequence();
420 assertEquals("AB-C.D EF", derived.getSequenceAsString());
421 assertEquals("ABCDEF", derived.getDatasetSequence()
422 .getSequenceAsString());
425 @Test(groups = { "Functional" })
426 public void testCopyConstructor_noDataset()
428 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
429 seq1.setDescription("description");
430 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
432 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
434 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
435 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
437 SequenceI copy = new Sequence(seq1);
439 assertNull(copy.getDatasetSequence());
441 verifyCopiedSequence(seq1, copy);
443 // copy has a copy of the DBRefEntry
444 // this is murky - DBrefs are only copied for dataset sequences
445 // where the test for 'dataset sequence' is 'dataset is null'
446 // but that doesn't distinguish it from an aligned sequence
447 // which has not yet generated a dataset sequence
448 // NB getDBRef looks inside dataset sequence if not null
449 DBRefEntry[] dbrefs = copy.getDBRef();
450 assertEquals(1, dbrefs.length);
451 assertFalse(dbrefs[0] == seq1.getDBRef()[0]);
452 assertTrue(dbrefs[0].equals(seq1.getDBRef()[0]));
455 @Test(groups = { "Functional" })
456 public void testCopyConstructor_withDataset()
458 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
459 seq1.createDatasetSequence();
460 seq1.setDescription("description");
461 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
463 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
465 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
466 // here we add DBRef to the dataset sequence:
467 seq1.getDatasetSequence().addDBRef(
468 new DBRefEntry("EMBL", "1.2", "AZ12345"));
470 SequenceI copy = new Sequence(seq1);
472 assertNotNull(copy.getDatasetSequence());
473 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
475 verifyCopiedSequence(seq1, copy);
477 // getDBRef looks inside dataset sequence and this is shared,
478 // so holds the same dbref objects
479 DBRefEntry[] dbrefs = copy.getDBRef();
480 assertEquals(1, dbrefs.length);
481 assertSame(dbrefs[0], seq1.getDBRef()[0]);
485 * Helper to make assertions about a copied sequence
490 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
492 // verify basic properties:
493 assertEquals(copy.getName(), seq1.getName());
494 assertEquals(copy.getDescription(), seq1.getDescription());
495 assertEquals(copy.getStart(), seq1.getStart());
496 assertEquals(copy.getEnd(), seq1.getEnd());
497 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
499 // copy has a copy of the annotation:
500 AlignmentAnnotation[] anns = copy.getAnnotation();
501 assertEquals(1, anns.length);
502 assertFalse(anns[0] == seq1.getAnnotation()[0]);
503 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
504 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
505 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
507 // copy has a copy of the sequence feature:
508 SequenceFeature[] sfs = copy.getSequenceFeatures();
509 assertEquals(1, sfs.length);
510 assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
511 assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
513 // copy has a copy of the PDB entry
514 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
515 assertEquals(1, pdbs.size());
516 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
517 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));