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.assertNotSame;
27 import static org.testng.AssertJUnit.assertNull;
28 import static org.testng.AssertJUnit.assertSame;
29 import static org.testng.AssertJUnit.assertTrue;
30 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
32 import jalview.datamodel.PDBEntry.Type;
33 import jalview.gui.JvOptionPane;
34 import jalview.util.MapList;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Vector;
42 import junit.extensions.PA;
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
49 public class SequenceTest
52 @BeforeClass(alwaysRun = true)
53 public void setUpJvOptionPane()
55 JvOptionPane.setInteractiveMode(false);
56 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
61 @BeforeMethod(alwaysRun = true)
64 seq = new Sequence("FER1", "AKPNGVL");
67 @Test(groups = { "Functional" })
68 public void testInsertGapsAndGapmaps()
70 SequenceI aseq = seq.deriveSequence();
71 aseq.insertCharAt(2, 3, '-');
72 aseq.insertCharAt(6, 3, '-');
73 assertEquals("Gap insertions not correct", "AK---P---NGVL",
74 aseq.getSequenceAsString());
75 List<int[]> gapInt = aseq.getInsertions();
76 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
77 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
78 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
79 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
82 @Test(groups = ("Functional"))
83 public void testIsProtein()
86 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
88 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
90 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
91 assertFalse(sq.isProtein());
92 // change sequence, should trigger an update of cached result
93 sq.setSequence("ASDFASDFADSF");
94 assertTrue(sq.isProtein());
96 * in situ change of sequence doesn't change hashcode :-O
97 * (sequence should not expose internal implementation)
99 for (int i = 0; i < sq.getSequence().length; i++)
101 sq.getSequence()[i] = "acgtu".charAt(i % 5);
103 assertTrue(sq.isProtein()); // but it isn't
106 @Test(groups = { "Functional" })
107 public void testGetAnnotation()
109 // initial state returns null not an empty array
110 assertNull(seq.getAnnotation());
111 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
113 AlignmentAnnotation[] anns = seq.getAnnotation();
114 assertEquals(1, anns.length);
115 assertSame(ann, anns[0]);
117 // removing all annotations reverts array to null
118 seq.removeAlignmentAnnotation(ann);
119 assertNull(seq.getAnnotation());
122 @Test(groups = { "Functional" })
123 public void testGetAnnotation_forLabel()
125 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
127 addAnnotation("label2", "desc2", "calcId2", 1f);
128 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
130 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
131 assertEquals(2, anns.length);
132 assertSame(ann1, anns[0]);
133 assertSame(ann3, anns[1]);
136 private AlignmentAnnotation addAnnotation(String label,
137 String description, String calcId, float value)
139 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
141 annotation.setCalcId(calcId);
142 seq.addAlignmentAnnotation(annotation);
146 @Test(groups = { "Functional" })
147 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
149 addAnnotation("label1", "desc1", "calcId1", 1f);
150 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
152 addAnnotation("label2", "desc3", "calcId3", 1f);
153 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
155 addAnnotation("label5", "desc3", null, 1f);
156 addAnnotation(null, "desc3", "calcId3", 1f);
158 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
160 assertEquals(2, anns.size());
161 assertSame(ann2, anns.get(0));
162 assertSame(ann4, anns.get(1));
164 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
165 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
166 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
167 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
168 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
172 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
173 * setting the sequenceRef on the annotation. Adding the same annotation twice
176 @Test(groups = { "Functional" })
177 public void testAddAlignmentAnnotation()
179 assertNull(seq.getAnnotation());
180 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
182 assertNull(annotation.sequenceRef);
183 seq.addAlignmentAnnotation(annotation);
184 assertSame(seq, annotation.sequenceRef);
185 AlignmentAnnotation[] anns = seq.getAnnotation();
186 assertEquals(1, anns.length);
187 assertSame(annotation, anns[0]);
189 // re-adding does nothing
190 seq.addAlignmentAnnotation(annotation);
191 anns = seq.getAnnotation();
192 assertEquals(1, anns.length);
193 assertSame(annotation, anns[0]);
195 // an identical but different annotation can be added
196 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
198 seq.addAlignmentAnnotation(annotation2);
199 anns = seq.getAnnotation();
200 assertEquals(2, anns.length);
201 assertSame(annotation, anns[0]);
202 assertSame(annotation2, anns[1]);
205 @Test(groups = { "Functional" })
206 public void testGetStartGetEnd()
208 SequenceI sq = new Sequence("test", "ABCDEF");
209 assertEquals(1, sq.getStart());
210 assertEquals(6, sq.getEnd());
212 sq = new Sequence("test", "--AB-C-DEF--");
213 assertEquals(1, sq.getStart());
214 assertEquals(6, sq.getEnd());
216 sq = new Sequence("test", "----");
217 assertEquals(1, sq.getStart());
218 assertEquals(0, sq.getEnd()); // ??
222 * Tests for the method that returns an alignment column position (base 1) for
223 * a given sequence position (base 1).
225 @Test(groups = { "Functional" })
226 public void testFindIndex()
228 SequenceI sq = new Sequence("test", "ABCDEF");
229 assertEquals(0, sq.findIndex(0));
230 assertEquals(1, sq.findIndex(1));
231 assertEquals(5, sq.findIndex(5));
232 assertEquals(6, sq.findIndex(6));
233 assertEquals(6, sq.findIndex(9));
235 sq = new Sequence("test", "-A--B-C-D-E-F--");
236 assertEquals(2, sq.findIndex(1));
237 assertEquals(5, sq.findIndex(2));
238 assertEquals(7, sq.findIndex(3));
240 // before start returns 0
241 assertEquals(0, sq.findIndex(0));
242 assertEquals(0, sq.findIndex(-1));
244 // beyond end returns last residue column
245 assertEquals(13, sq.findIndex(99));
250 * Tests for the method that returns a dataset sequence position (base 1) for
251 * an aligned column position (base 0).
253 @Test(groups = { "Functional" })
254 public void testFindPosition()
256 SequenceI sq = new Sequence("test", "ABCDEF");
257 assertEquals(1, sq.findPosition(0));
258 assertEquals(6, sq.findPosition(5));
259 // assertEquals(-1, seq.findPosition(6)); // fails
261 sq = new Sequence("test", "AB-C-D--");
262 assertEquals(1, sq.findPosition(0));
263 assertEquals(2, sq.findPosition(1));
264 // gap position 'finds' residue to the right (not the left as per javadoc)
265 assertEquals(3, sq.findPosition(2));
266 assertEquals(3, sq.findPosition(3));
267 assertEquals(4, sq.findPosition(4));
268 assertEquals(4, sq.findPosition(5));
269 // returns 1 more than sequence length if off the end ?!?
270 assertEquals(5, sq.findPosition(6));
271 assertEquals(5, sq.findPosition(7));
273 sq = new Sequence("test", "--AB-C-DEF--");
274 assertEquals(1, sq.findPosition(0));
275 assertEquals(1, sq.findPosition(1));
276 assertEquals(1, sq.findPosition(2));
277 assertEquals(2, sq.findPosition(3));
278 assertEquals(3, sq.findPosition(4));
279 assertEquals(3, sq.findPosition(5));
280 assertEquals(4, sq.findPosition(6));
281 assertEquals(4, sq.findPosition(7));
282 assertEquals(5, sq.findPosition(8));
283 assertEquals(6, sq.findPosition(9));
284 assertEquals(7, sq.findPosition(10));
285 assertEquals(7, sq.findPosition(11));
288 @Test(groups = { "Functional" })
289 public void testDeleteChars()
294 SequenceI sq = new Sequence("test", "ABCDEF");
295 assertNull(PA.getValue(sq, "datasetSequence"));
296 assertEquals(1, sq.getStart());
297 assertEquals(6, sq.getEnd());
298 sq.deleteChars(2, 3);
299 assertEquals("ABDEF", sq.getSequenceAsString());
300 assertEquals(1, sq.getStart());
301 assertEquals(5, sq.getEnd());
302 assertNull(PA.getValue(sq, "datasetSequence"));
307 sq = new Sequence("test", "ABCDEF");
308 sq.deleteChars(0, 2);
309 assertEquals("CDEF", sq.getSequenceAsString());
310 assertEquals(3, sq.getStart());
311 assertEquals(6, sq.getEnd());
312 assertNull(PA.getValue(sq, "datasetSequence"));
317 sq = new Sequence("test", "ABCDEF");
318 sq.deleteChars(4, 6);
319 assertEquals("ABCD", sq.getSequenceAsString());
320 assertEquals(1, sq.getStart());
321 assertEquals(4, sq.getEnd());
322 assertNull(PA.getValue(sq, "datasetSequence"));
325 @Test(groups = { "Functional" })
326 public void testDeleteChars_withDbRefsAndFeatures()
329 * internal delete - new dataset sequence created
330 * gets a copy of any dbrefs
332 SequenceI sq = new Sequence("test", "ABCDEF");
333 sq.createDatasetSequence();
334 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
336 Object ds = PA.getValue(sq, "datasetSequence");
338 assertEquals(1, sq.getStart());
339 assertEquals(6, sq.getEnd());
340 sq.deleteChars(2, 3);
341 assertEquals("ABDEF", sq.getSequenceAsString());
342 assertEquals(1, sq.getStart());
343 assertEquals(5, sq.getEnd());
344 Object newDs = PA.getValue(sq, "datasetSequence");
345 assertNotNull(newDs);
346 assertNotSame(ds, newDs);
347 assertNotNull(sq.getDBRefs());
348 assertEquals(1, sq.getDBRefs().length);
349 assertNotSame(dbr1, sq.getDBRefs()[0]);
350 assertEquals(dbr1, sq.getDBRefs()[0]);
353 * internal delete with sequence features
354 * (failure case for JAL-2541)
356 sq = new Sequence("test", "ABCDEF");
357 sq.createDatasetSequence();
358 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
360 sq.addSequenceFeature(sf1);
361 ds = PA.getValue(sq, "datasetSequence");
363 assertEquals(1, sq.getStart());
364 assertEquals(6, sq.getEnd());
365 sq.deleteChars(2, 4);
366 assertEquals("ABEF", sq.getSequenceAsString());
367 assertEquals(1, sq.getStart());
368 assertEquals(4, sq.getEnd());
369 newDs = PA.getValue(sq, "datasetSequence");
370 assertNotNull(newDs);
371 assertNotSame(ds, newDs);
372 SequenceFeature[] sfs = sq.getSequenceFeatures();
374 assertEquals(1, sfs.length);
375 assertNotSame(sf1, sfs[0]);
376 assertEquals(sf1, sfs[0]);
379 * delete at start - no new dataset sequence created
380 * any sequence features remain as before
382 sq = new Sequence("test", "ABCDEF");
383 sq.createDatasetSequence();
384 ds = PA.getValue(sq, "datasetSequence");
385 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
386 sq.addSequenceFeature(sf1);
387 sq.deleteChars(0, 2);
388 assertEquals("CDEF", sq.getSequenceAsString());
389 assertEquals(3, sq.getStart());
390 assertEquals(6, sq.getEnd());
391 assertSame(ds, PA.getValue(sq, "datasetSequence"));
392 sfs = sq.getSequenceFeatures();
394 assertEquals(1, sfs.length);
395 assertSame(sf1, sfs[0]);
398 * delete at end - no new dataset sequence created
399 * any dbrefs remain as before
401 sq = new Sequence("test", "ABCDEF");
402 sq.createDatasetSequence();
403 ds = PA.getValue(sq, "datasetSequence");
404 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
406 sq.deleteChars(4, 6);
407 assertEquals("ABCD", sq.getSequenceAsString());
408 assertEquals(1, sq.getStart());
409 assertEquals(4, sq.getEnd());
410 assertSame(ds, PA.getValue(sq, "datasetSequence"));
411 assertNotNull(sq.getDBRefs());
412 assertEquals(1, sq.getDBRefs().length);
413 assertSame(dbr1, sq.getDBRefs()[0]);
416 @Test(groups = { "Functional" })
417 public void testInsertCharAt()
419 // non-static methods:
420 SequenceI sq = new Sequence("test", "ABCDEF");
421 sq.insertCharAt(0, 'z');
422 assertEquals("zABCDEF", sq.getSequenceAsString());
423 sq.insertCharAt(2, 2, 'x');
424 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
426 // for static method see StringUtilsTest
430 * Test the method that returns an array of aligned sequence positions where
431 * the array index is the data sequence position (both base 0).
433 @Test(groups = { "Functional" })
434 public void testGapMap()
436 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
437 sq.createDatasetSequence();
438 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
442 * Test the method that gets sequence features, either from the sequence or
445 @Test(groups = { "Functional" })
446 public void testGetSequenceFeatures()
448 SequenceI sq = new Sequence("test", "GATCAT");
449 sq.createDatasetSequence();
451 assertNull(sq.getSequenceFeatures());
454 * SequenceFeature on sequence
456 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
457 sq.addSequenceFeature(sf);
458 SequenceFeature[] sfs = sq.getSequenceFeatures();
459 assertEquals(1, sfs.length);
460 assertSame(sf, sfs[0]);
463 * SequenceFeature on sequence and dataset sequence; returns that on
466 * Note JAL-2046: spurious: we have no use case for this at the moment.
467 * This test also buggy - as sf2.equals(sf), no new feature is added
469 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
471 sq.getDatasetSequence().addSequenceFeature(sf2);
472 sfs = sq.getSequenceFeatures();
473 assertEquals(1, sfs.length);
474 assertSame(sf, sfs[0]);
477 * SequenceFeature on dataset sequence only
478 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
480 sq.setSequenceFeatures(null);
481 assertNull(sq.getDatasetSequence().getSequenceFeatures());
484 * Corrupt case - no SequenceFeature, dataset's dataset is the original
485 * sequence. Test shows no infinite loop results.
487 sq.getDatasetSequence().setSequenceFeatures(null);
489 * is there a usecase for this ? setDatasetSequence should throw an error if
490 * this actually occurs.
494 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
495 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
496 } catch (IllegalArgumentException e)
498 // TODO Jalview error/exception class for raising implementation errors
499 assertTrue(e.getMessage().toLowerCase()
500 .contains("implementation error"));
502 assertNull(sq.getSequenceFeatures());
506 * Test the method that returns an array, indexed by sequence position, whose
507 * entries are the residue positions at the sequence position (or to the right
510 @Test(groups = { "Functional" })
511 public void testFindPositionMap()
514 * Note: Javadoc for findPosition says it returns the residue position to
515 * the left of a gapped position; in fact it returns the position to the
516 * right. Also it returns a non-existent residue position for a gap beyond
519 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
520 int[] map = sq.findPositionMap();
521 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
522 Arrays.toString(map));
526 * Test for getSubsequence
528 @Test(groups = { "Functional" })
529 public void testGetSubsequence()
531 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
532 sq.createDatasetSequence();
534 // positions are base 0, end position is exclusive
535 SequenceI subseq = sq.getSubSequence(2, 4);
537 assertEquals("CD", subseq.getSequenceAsString());
538 // start/end are base 1 positions
539 assertEquals(3, subseq.getStart());
540 assertEquals(4, subseq.getEnd());
541 // subsequence shares the full dataset sequence
542 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
546 * test createDatasetSequence behaves to doc
548 @Test(groups = { "Functional" })
549 public void testCreateDatasetSequence()
551 SequenceI sq = new Sequence("my", "ASDASD");
552 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
554 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
555 assertNull(sq.getDatasetSequence());
556 assertNotNull(PA.getValue(sq, "sequenceFeatures")); // to be removed!
557 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
558 assertNotNull(PA.getValue(sq, "dbrefs"));
560 SequenceI rds = sq.createDatasetSequence();
562 assertNull(rds.getDatasetSequence());
563 assertSame(sq.getDatasetSequence(), rds);
565 // sequence features and dbrefs transferred to dataset sequence
566 assertNull(PA.getValue(sq, "sequenceFeatures"));
567 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
568 assertNull(PA.getValue(sq, "dbrefs"));
569 assertNotNull(PA.getValue(rds, "sequenceFeatures"));
570 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
571 assertNotNull(PA.getValue(rds, "dbrefs"));
575 * Test for deriveSequence applied to a sequence with a dataset
577 @Test(groups = { "Functional" })
578 public void testDeriveSequence_existingDataset()
580 Sequence sq = new Sequence("Seq1", "CD");
581 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
582 sq.getDatasetSequence().addSequenceFeature(
583 new SequenceFeature("", "", 1, 2, 0f, null));
587 sq.setDescription("Test sequence description..");
588 sq.setVamsasId("TestVamsasId");
589 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
591 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
592 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
593 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
594 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
596 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
597 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
598 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
599 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
601 // these are the same as ones already added
602 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
603 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
605 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
608 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
609 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
610 sq.getDatasetSequence().addDBRef(
611 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
612 sq.getDatasetSequence().addDBRef(
613 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
615 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
616 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
617 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
619 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
621 sq.getDatasetSequence().addPDBId(pdbe1a);
622 sq.getDatasetSequence().addPDBId(pdbe1b);
623 sq.getDatasetSequence().addPDBId(pdbe2a);
624 sq.getDatasetSequence().addPDBId(pdbe2b);
627 * test we added pdb entries to the dataset sequence
629 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
630 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
631 "PDB Entries were not found on dataset sequence.");
634 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
636 Assert.assertEquals(pdbe1a,
637 sq.getDatasetSequence().getPDBEntry("1PDB"),
638 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
639 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
640 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
641 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
642 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
643 Annotation[] annots = annotsList.toArray(new Annotation[0]);
644 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
645 "Test annot description", annots));
646 sq.getDatasetSequence().addAlignmentAnnotation(
647 new AlignmentAnnotation("Test annot", "Test annot description",
649 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
650 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
652 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
653 Assert.assertNotNull(sq.getAnnotation());
654 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
655 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
658 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
660 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
662 Sequence derived = (Sequence) sq.deriveSequence();
664 Assert.assertEquals(derived.getDescription(),
665 "Test sequence description..");
666 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
667 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
668 Assert.assertNotNull(derived.getAnnotation());
669 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
670 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
671 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
673 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
675 assertEquals("CD", derived.getSequenceAsString());
676 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
678 assertNull(sq.sequenceFeatures);
679 assertNull(derived.sequenceFeatures);
680 // derived sequence should access dataset sequence features
681 assertNotNull(sq.getSequenceFeatures());
682 assertArrayEquals(sq.getSequenceFeatures(),
683 derived.getSequenceFeatures());
686 * verify we have primary db refs *just* for PDB IDs with associated
690 assertEquals(primRefs, sq.getPrimaryDBRefs());
691 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
693 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
698 * Test for deriveSequence applied to an ungapped sequence with no dataset
700 @Test(groups = { "Functional" })
701 public void testDeriveSequence_noDatasetUngapped()
703 SequenceI sq = new Sequence("Seq1", "ABCDEF");
704 assertEquals(1, sq.getStart());
705 assertEquals(6, sq.getEnd());
706 SequenceI derived = sq.deriveSequence();
707 assertEquals("ABCDEF", derived.getSequenceAsString());
708 assertEquals("ABCDEF", derived.getDatasetSequence()
709 .getSequenceAsString());
713 * Test for deriveSequence applied to a gapped sequence with no dataset
715 @Test(groups = { "Functional" })
716 public void testDeriveSequence_noDatasetGapped()
718 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
719 assertEquals(1, sq.getStart());
720 assertEquals(6, sq.getEnd());
721 assertNull(sq.getDatasetSequence());
722 SequenceI derived = sq.deriveSequence();
723 assertEquals("AB-C.D EF", derived.getSequenceAsString());
724 assertEquals("ABCDEF", derived.getDatasetSequence()
725 .getSequenceAsString());
728 @Test(groups = { "Functional" })
729 public void testCopyConstructor_noDataset()
731 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
732 seq1.setDescription("description");
733 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
735 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
737 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
738 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
740 SequenceI copy = new Sequence(seq1);
742 assertNull(copy.getDatasetSequence());
744 verifyCopiedSequence(seq1, copy);
746 // copy has a copy of the DBRefEntry
747 // this is murky - DBrefs are only copied for dataset sequences
748 // where the test for 'dataset sequence' is 'dataset is null'
749 // but that doesn't distinguish it from an aligned sequence
750 // which has not yet generated a dataset sequence
751 // NB getDBRef looks inside dataset sequence if not null
752 DBRefEntry[] dbrefs = copy.getDBRefs();
753 assertEquals(1, dbrefs.length);
754 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
755 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
758 @Test(groups = { "Functional" })
759 public void testCopyConstructor_withDataset()
761 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
762 seq1.createDatasetSequence();
763 seq1.setDescription("description");
764 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
766 // JAL-2046 - what is the contract for using a derived sequence's
767 // addSequenceFeature ?
768 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
770 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
771 // here we add DBRef to the dataset sequence:
772 seq1.getDatasetSequence().addDBRef(
773 new DBRefEntry("EMBL", "1.2", "AZ12345"));
775 SequenceI copy = new Sequence(seq1);
777 assertNotNull(copy.getDatasetSequence());
778 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
780 verifyCopiedSequence(seq1, copy);
782 // getDBRef looks inside dataset sequence and this is shared,
783 // so holds the same dbref objects
784 DBRefEntry[] dbrefs = copy.getDBRefs();
785 assertEquals(1, dbrefs.length);
786 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
790 * Helper to make assertions about a copied sequence
795 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
797 // verify basic properties:
798 assertEquals(copy.getName(), seq1.getName());
799 assertEquals(copy.getDescription(), seq1.getDescription());
800 assertEquals(copy.getStart(), seq1.getStart());
801 assertEquals(copy.getEnd(), seq1.getEnd());
802 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
804 // copy has a copy of the annotation:
805 AlignmentAnnotation[] anns = copy.getAnnotation();
806 assertEquals(1, anns.length);
807 assertFalse(anns[0] == seq1.getAnnotation()[0]);
808 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
809 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
810 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
812 // copy has a copy of the sequence feature:
813 SequenceFeature[] sfs = copy.getSequenceFeatures();
814 assertEquals(1, sfs.length);
815 if (seq1.getDatasetSequence() != null
816 && copy.getDatasetSequence() == seq1.getDatasetSequence())
818 assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
822 assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
824 assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
826 // copy has a copy of the PDB entry
827 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
828 assertEquals(1, pdbs.size());
829 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
830 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
833 @Test(groups = "Functional")
834 public void testGetCharAt()
836 SequenceI sq = new Sequence("", "abcde");
837 assertEquals('a', sq.getCharAt(0));
838 assertEquals('e', sq.getCharAt(4));
839 assertEquals(' ', sq.getCharAt(5));
840 assertEquals(' ', sq.getCharAt(-1));
843 @Test(groups = { "Functional" })
844 public void testAddSequenceFeatures()
846 SequenceI sq = new Sequence("", "abcde");
847 // type may not be null
848 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
850 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
852 // can't add a duplicate feature
853 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
855 // can add a different feature
856 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
857 8, 0f, null))); // different type
858 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
859 "description", 4, 8, 0f, null)));// different description
860 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
861 8, 0f, null))); // different start position
862 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
863 9, 0f, null))); // different end position
864 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
865 8, 1f, null))); // different score
866 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
867 8, Float.NaN, null))); // score NaN
868 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
869 8, 0f, "Metal"))); // different group
870 assertEquals(8, sq.getFeatures().getAllFeatures().size());
874 * Tests for adding (or updating) dbrefs
876 * @see DBRefEntry#updateFrom(DBRefEntry)
878 @Test(groups = { "Functional" })
879 public void testAddDBRef()
881 SequenceI sq = new Sequence("", "abcde");
882 assertNull(sq.getDBRefs());
883 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
885 assertEquals(1, sq.getDBRefs().length);
886 assertSame(dbref, sq.getDBRefs()[0]);
889 * change of version - new entry
891 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
893 assertEquals(2, sq.getDBRefs().length);
894 assertSame(dbref, sq.getDBRefs()[0]);
895 assertSame(dbref2, sq.getDBRefs()[1]);
898 * matches existing entry - not added
900 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
901 assertEquals(2, sq.getDBRefs().length);
904 * different source = new entry
906 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
908 assertEquals(3, sq.getDBRefs().length);
909 assertSame(dbref3, sq.getDBRefs()[2]);
912 * different ref = new entry
914 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
916 assertEquals(4, sq.getDBRefs().length);
917 assertSame(dbref4, sq.getDBRefs()[3]);
920 * matching ref with a mapping - map updated
922 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
923 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
927 assertEquals(4, sq.getDBRefs().length);
928 assertSame(dbref4, sq.getDBRefs()[3]);
929 assertSame(map, dbref4.getMap());
932 * 'real' version replaces "0" version
934 dbref2.setVersion("0");
935 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
936 dbref2.getAccessionId());
938 assertEquals(4, sq.getDBRefs().length);
939 assertSame(dbref2, sq.getDBRefs()[1]);
940 assertEquals("3", dbref2.getVersion());
943 * 'real' version replaces "source:0" version
945 dbref3.setVersion("Uniprot:0");
946 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
947 dbref3.getAccessionId());
949 assertEquals(4, sq.getDBRefs().length);
950 assertSame(dbref3, sq.getDBRefs()[2]);
951 assertEquals("3", dbref2.getVersion());
954 @Test(groups = { "Functional" })
955 public void testGetPrimaryDBRefs_peptide()
957 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
960 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
961 assertTrue(primaryDBRefs.isEmpty());
964 sq.setDBRefs(new DBRefEntry[] {});
965 primaryDBRefs = sq.getPrimaryDBRefs();
966 assertTrue(primaryDBRefs.isEmpty());
969 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
970 sq.addDBRef(upentry1);
972 // primary - uniprot with congruent map
973 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
974 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
975 new int[] { 10, 22 }, 1, 1)));
976 sq.addDBRef(upentry2);
978 // primary - uniprot with map of enclosing sequence
979 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
980 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
981 new int[] { 8, 24 }, 1, 1)));
982 sq.addDBRef(upentry3);
984 // not primary - uniprot with map of sub-sequence (5')
985 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
986 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
987 new int[] { 10, 18 }, 1, 1)));
988 sq.addDBRef(upentry4);
990 // not primary - uniprot with map that overlaps 3'
991 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
992 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
993 new int[] { 12, 22 }, 1, 1)));
994 sq.addDBRef(upentry5);
996 // not primary - uniprot with map to different coordinates frame
997 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
998 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
999 new int[] { 112, 118 }, 1, 1)));
1000 sq.addDBRef(upentry6);
1002 // not primary - dbref to 'non-core' database
1003 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1004 sq.addDBRef(upentry7);
1006 // primary - type is PDB
1007 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1008 sq.addDBRef(pdbentry);
1010 // not primary - PDBEntry has no file
1011 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1013 // not primary - no PDBEntry
1014 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1016 // add corroborating PDB entry for primary DBref -
1017 // needs to have a file as well as matching ID
1018 // note PDB ID is not treated as case sensitive
1019 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1022 // not valid DBRef - no file..
1023 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1025 primaryDBRefs = sq.getPrimaryDBRefs();
1026 assertEquals(4, primaryDBRefs.size());
1027 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1028 primaryDBRefs.contains(upentry1));
1029 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1030 primaryDBRefs.contains(upentry2));
1031 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1032 primaryDBRefs.contains(upentry3));
1033 assertTrue("Couldn't find expected PDB primary reference",
1034 primaryDBRefs.contains(pdbentry));
1037 @Test(groups = { "Functional" })
1038 public void testGetPrimaryDBRefs_nucleotide()
1040 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1042 // primary - Ensembl
1043 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1046 // not primary - Ensembl 'transcript' mapping of sub-sequence
1047 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1048 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1049 new int[] { 1, 11 }, 1, 1)));
1052 // primary - EMBL with congruent map
1053 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1054 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1055 new int[] { 10, 34 }, 1, 1)));
1058 // not primary - to non-core database
1059 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1062 // not primary - to protein
1063 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1066 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1067 assertEquals(2, primaryDBRefs.size());
1068 assertTrue(primaryDBRefs.contains(dbr1));
1069 assertTrue(primaryDBRefs.contains(dbr3));
1073 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1076 @Test(groups = { "Functional" })
1077 public void testUpdatePDBIds()
1079 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1080 seq.addPDBId(pdbe1);
1081 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1082 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1083 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1084 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1085 // 7 is not a valid chain code:
1086 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1089 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1090 assertEquals(4, pdbIds.size());
1091 assertSame(pdbe1, pdbIds.get(0));
1092 // chain code got added to 3A6S:
1093 assertEquals("B", pdbe1.getChainCode());
1094 assertEquals("1A70", pdbIds.get(1).getId());
1095 // 4BQGA is parsed into id + chain
1096 assertEquals("4BQG", pdbIds.get(2).getId());
1097 assertEquals("a", pdbIds.get(2).getChainCode());
1098 assertEquals("2GIS7", pdbIds.get(3).getId());
1099 assertNull(pdbIds.get(3).getChainCode());
1103 * Test the method that either adds a pdbid or updates an existing one
1105 @Test(groups = { "Functional" })
1106 public void testAddPDBId()
1108 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1110 assertEquals(1, seq.getAllPDBEntries().size());
1111 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1112 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1114 // add the same entry
1116 assertEquals(1, seq.getAllPDBEntries().size());
1117 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1119 // add an identical entry
1120 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1121 assertEquals(1, seq.getAllPDBEntries().size());
1122 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1124 // add a different entry
1125 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1126 seq.addPDBId(pdbe2);
1127 assertEquals(2, seq.getAllPDBEntries().size());
1128 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1129 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1131 // update pdbe with chain code, file, type
1132 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1133 seq.addPDBId(pdbe3);
1134 assertEquals(2, seq.getAllPDBEntries().size());
1135 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1136 assertEquals("3A6S", pdbe.getId()); // unchanged
1137 assertEquals("A", pdbe.getChainCode()); // updated
1138 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1139 assertEquals("filepath", pdbe.getFile()); // updated
1140 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1142 // add with a different file path
1143 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1144 seq.addPDBId(pdbe4);
1145 assertEquals(3, seq.getAllPDBEntries().size());
1146 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1148 // add with a different chain code
1149 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1150 seq.addPDBId(pdbe5);
1151 assertEquals(4, seq.getAllPDBEntries().size());
1152 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1156 groups = { "Functional" },
1157 expectedExceptions = { IllegalArgumentException.class })
1158 public void testSetDatasetSequence_toSelf()
1160 seq.setDatasetSequence(seq);
1164 groups = { "Functional" },
1165 expectedExceptions = { IllegalArgumentException.class })
1166 public void testSetDatasetSequence_cascading()
1168 SequenceI seq2 = new Sequence("Seq2", "xyz");
1169 seq2.createDatasetSequence();
1170 seq.setDatasetSequence(seq2);