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;
29 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
31 import jalview.datamodel.PDBEntry.Type;
32 import jalview.gui.JvOptionPane;
33 import jalview.util.MapList;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Vector;
41 import org.testng.Assert;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.BeforeMethod;
44 import org.testng.annotations.Test;
46 public class SequenceTest
49 @BeforeClass(alwaysRun = true)
50 public void setUpJvOptionPane()
52 JvOptionPane.setInteractiveMode(false);
53 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58 @BeforeMethod(alwaysRun = true)
61 seq = new Sequence("FER1", "AKPNGVL");
64 @Test(groups = { "Functional" })
65 public void testInsertGapsAndGapmaps()
67 SequenceI aseq = seq.deriveSequence();
68 aseq.insertCharAt(2, 3, '-');
69 aseq.insertCharAt(6, 3, '-');
70 assertEquals("Gap insertions not correct", "AK---P---NGVL",
71 aseq.getSequenceAsString());
72 List<int[]> gapInt = aseq.getInsertions();
73 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
74 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
75 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
76 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
79 @Test(groups = ("Functional"))
80 public void testIsProtein()
83 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
85 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
87 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
88 assertFalse(sq.isProtein());
89 // change sequence, should trigger an update of cached result
90 sq.setSequence("ASDFASDFADSF");
91 assertTrue(sq.isProtein());
93 * in situ change of sequence doesn't change hashcode :-O
94 * (sequence should not expose internal implementation)
96 for (int i = 0; i < sq.getSequence().length; i++)
98 sq.getSequence()[i] = "acgtu".charAt(i % 5);
100 assertTrue(sq.isProtein()); // but it isn't
103 @Test(groups = { "Functional" })
104 public void testGetAnnotation()
106 // initial state returns null not an empty array
107 assertNull(seq.getAnnotation());
108 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
110 AlignmentAnnotation[] anns = seq.getAnnotation();
111 assertEquals(1, anns.length);
112 assertSame(ann, anns[0]);
114 // removing all annotations reverts array to null
115 seq.removeAlignmentAnnotation(ann);
116 assertNull(seq.getAnnotation());
119 @Test(groups = { "Functional" })
120 public void testGetAnnotation_forLabel()
122 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
124 addAnnotation("label2", "desc2", "calcId2", 1f);
125 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
127 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
128 assertEquals(2, anns.length);
129 assertSame(ann1, anns[0]);
130 assertSame(ann3, anns[1]);
133 private AlignmentAnnotation addAnnotation(String label,
134 String description, String calcId, float value)
136 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
138 annotation.setCalcId(calcId);
139 seq.addAlignmentAnnotation(annotation);
143 @Test(groups = { "Functional" })
144 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
146 addAnnotation("label1", "desc1", "calcId1", 1f);
147 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
149 addAnnotation("label2", "desc3", "calcId3", 1f);
150 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
152 addAnnotation("label5", "desc3", null, 1f);
153 addAnnotation(null, "desc3", "calcId3", 1f);
155 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
157 assertEquals(2, anns.size());
158 assertSame(ann2, anns.get(0));
159 assertSame(ann4, anns.get(1));
161 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
162 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
163 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
164 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
165 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
169 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
170 * setting the sequenceRef on the annotation. Adding the same annotation twice
173 @Test(groups = { "Functional" })
174 public void testAddAlignmentAnnotation()
176 assertNull(seq.getAnnotation());
177 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
179 assertNull(annotation.sequenceRef);
180 seq.addAlignmentAnnotation(annotation);
181 assertSame(seq, annotation.sequenceRef);
182 AlignmentAnnotation[] anns = seq.getAnnotation();
183 assertEquals(1, anns.length);
184 assertSame(annotation, anns[0]);
186 // re-adding does nothing
187 seq.addAlignmentAnnotation(annotation);
188 anns = seq.getAnnotation();
189 assertEquals(1, anns.length);
190 assertSame(annotation, anns[0]);
192 // an identical but different annotation can be added
193 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
195 seq.addAlignmentAnnotation(annotation2);
196 anns = seq.getAnnotation();
197 assertEquals(2, anns.length);
198 assertSame(annotation, anns[0]);
199 assertSame(annotation2, anns[1]);
202 @Test(groups = { "Functional" })
203 public void testGetStartGetEnd()
205 SequenceI sq = new Sequence("test", "ABCDEF");
206 assertEquals(1, sq.getStart());
207 assertEquals(6, sq.getEnd());
209 sq = new Sequence("test", "--AB-C-DEF--");
210 assertEquals(1, sq.getStart());
211 assertEquals(6, sq.getEnd());
213 sq = new Sequence("test", "----");
214 assertEquals(1, sq.getStart());
215 assertEquals(0, sq.getEnd()); // ??
219 * Tests for the method that returns an alignment column position (base 1) for
220 * a given sequence position (base 1).
222 @Test(groups = { "Functional" })
223 public void testFindIndex()
225 SequenceI sq = new Sequence("test", "ABCDEF");
226 assertEquals(0, sq.findIndex(0));
227 assertEquals(1, sq.findIndex(1));
228 assertEquals(5, sq.findIndex(5));
229 assertEquals(6, sq.findIndex(6));
230 assertEquals(6, sq.findIndex(9));
232 sq = new Sequence("test", "-A--B-C-D-E-F--");
233 assertEquals(2, sq.findIndex(1));
234 assertEquals(5, sq.findIndex(2));
235 assertEquals(7, sq.findIndex(3));
237 // before start returns 0
238 assertEquals(0, sq.findIndex(0));
239 assertEquals(0, sq.findIndex(-1));
241 // beyond end returns last residue column
242 assertEquals(13, sq.findIndex(99));
247 * Tests for the method that returns a dataset sequence position (base 1) for
248 * an aligned column position (base 0).
250 @Test(groups = { "Functional" })
251 public void testFindPosition()
253 SequenceI sq = new Sequence("test", "ABCDEF");
254 assertEquals(1, sq.findPosition(0));
255 assertEquals(6, sq.findPosition(5));
256 // assertEquals(-1, seq.findPosition(6)); // fails
258 sq = new Sequence("test", "AB-C-D--");
259 assertEquals(1, sq.findPosition(0));
260 assertEquals(2, sq.findPosition(1));
261 // gap position 'finds' residue to the right (not the left as per javadoc)
262 assertEquals(3, sq.findPosition(2));
263 assertEquals(3, sq.findPosition(3));
264 assertEquals(4, sq.findPosition(4));
265 assertEquals(4, sq.findPosition(5));
266 // returns 1 more than sequence length if off the end ?!?
267 assertEquals(5, sq.findPosition(6));
268 assertEquals(5, sq.findPosition(7));
270 sq = new Sequence("test", "--AB-C-DEF--");
271 assertEquals(1, sq.findPosition(0));
272 assertEquals(1, sq.findPosition(1));
273 assertEquals(1, sq.findPosition(2));
274 assertEquals(2, sq.findPosition(3));
275 assertEquals(3, sq.findPosition(4));
276 assertEquals(3, sq.findPosition(5));
277 assertEquals(4, sq.findPosition(6));
278 assertEquals(4, sq.findPosition(7));
279 assertEquals(5, sq.findPosition(8));
280 assertEquals(6, sq.findPosition(9));
281 assertEquals(7, sq.findPosition(10));
282 assertEquals(7, sq.findPosition(11));
285 @Test(groups = { "Functional" })
286 public void testDeleteChars()
288 SequenceI sq = new Sequence("test", "ABCDEF");
289 assertEquals(1, sq.getStart());
290 assertEquals(6, sq.getEnd());
291 sq.deleteChars(2, 3);
292 assertEquals("ABDEF", sq.getSequenceAsString());
293 assertEquals(1, sq.getStart());
294 assertEquals(5, sq.getEnd());
296 sq = new Sequence("test", "ABCDEF");
297 sq.deleteChars(0, 2);
298 assertEquals("CDEF", sq.getSequenceAsString());
299 assertEquals(3, sq.getStart());
300 assertEquals(6, sq.getEnd());
303 @Test(groups = { "Functional" })
304 public void testInsertCharAt()
306 // non-static methods:
307 SequenceI sq = new Sequence("test", "ABCDEF");
308 sq.insertCharAt(0, 'z');
309 assertEquals("zABCDEF", sq.getSequenceAsString());
310 sq.insertCharAt(2, 2, 'x');
311 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
313 // for static method see StringUtilsTest
317 * Test the method that returns an array of aligned sequence positions where
318 * the array index is the data sequence position (both base 0).
320 @Test(groups = { "Functional" })
321 public void testGapMap()
323 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
324 sq.createDatasetSequence();
325 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
329 * Test the method that gets sequence features, either from the sequence or
332 @Test(groups = { "Functional" })
333 public void testGetSequenceFeatures()
335 SequenceI sq = new Sequence("test", "GATCAT");
336 sq.createDatasetSequence();
338 assertNull(sq.getSequenceFeatures());
341 * SequenceFeature on sequence
343 SequenceFeature sf = new SequenceFeature();
344 sq.addSequenceFeature(sf);
345 SequenceFeature[] sfs = sq.getSequenceFeatures();
346 assertEquals(1, sfs.length);
347 assertSame(sf, sfs[0]);
350 * SequenceFeature on sequence and dataset sequence; returns that on
353 * Note JAL-2046: spurious: we have no use case for this at the moment.
354 * This test also buggy - as sf2.equals(sf), no new feature is added
356 SequenceFeature sf2 = new SequenceFeature();
357 sq.getDatasetSequence().addSequenceFeature(sf2);
358 sfs = sq.getSequenceFeatures();
359 assertEquals(1, sfs.length);
360 assertSame(sf, sfs[0]);
363 * SequenceFeature on dataset sequence only
364 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
366 sq.setSequenceFeatures(null);
367 assertNull(sq.getDatasetSequence().getSequenceFeatures());
370 * Corrupt case - no SequenceFeature, dataset's dataset is the original
371 * sequence. Test shows no infinite loop results.
373 sq.getDatasetSequence().setSequenceFeatures(null);
375 * is there a usecase for this ? setDatasetSequence should throw an error if
376 * this actually occurs.
380 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
381 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
382 } catch (IllegalArgumentException e)
384 // TODO Jalview error/exception class for raising implementation errors
385 assertTrue(e.getMessage().toLowerCase()
386 .contains("implementation error"));
388 assertNull(sq.getSequenceFeatures());
392 * Test the method that returns an array, indexed by sequence position, whose
393 * entries are the residue positions at the sequence position (or to the right
396 @Test(groups = { "Functional" })
397 public void testFindPositionMap()
400 * Note: Javadoc for findPosition says it returns the residue position to
401 * the left of a gapped position; in fact it returns the position to the
402 * right. Also it returns a non-existent residue position for a gap beyond
405 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
406 int[] map = sq.findPositionMap();
407 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
408 Arrays.toString(map));
412 * Test for getSubsequence
414 @Test(groups = { "Functional" })
415 public void testGetSubsequence()
417 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
418 sq.createDatasetSequence();
420 // positions are base 0, end position is exclusive
421 SequenceI subseq = sq.getSubSequence(2, 4);
423 assertEquals("CD", subseq.getSequenceAsString());
424 // start/end are base 1 positions
425 assertEquals(3, subseq.getStart());
426 assertEquals(4, subseq.getEnd());
427 // subsequence shares the full dataset sequence
428 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
432 * test createDatasetSequence behaves to doc
434 @Test(groups = { "Functional" })
435 public void testCreateDatasetSequence()
437 SequenceI sq = new Sequence("my", "ASDASD");
438 assertNull(sq.getDatasetSequence());
439 SequenceI rds = sq.createDatasetSequence();
441 assertNull(rds.getDatasetSequence());
442 assertEquals(sq.getDatasetSequence(), rds);
446 * Test for deriveSequence applied to a sequence with a dataset
448 @Test(groups = { "Functional" })
449 public void testDeriveSequence_existingDataset()
451 Sequence sq = new Sequence("Seq1", "CD");
452 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
453 sq.getDatasetSequence().addSequenceFeature(
454 new SequenceFeature("", "", 1, 2, 0f, null));
458 sq.setDescription("Test sequence description..");
459 sq.setVamsasId("TestVamsasId");
460 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
462 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
463 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
464 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
465 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
467 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
468 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
469 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
470 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
472 // these are the same as ones already added
473 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
474 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
476 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
479 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
480 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
481 sq.getDatasetSequence().addDBRef(
482 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
483 sq.getDatasetSequence().addDBRef(
484 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
486 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
487 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
488 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
490 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
492 sq.getDatasetSequence().addPDBId(pdbe1a);
493 sq.getDatasetSequence().addPDBId(pdbe1b);
494 sq.getDatasetSequence().addPDBId(pdbe2a);
495 sq.getDatasetSequence().addPDBId(pdbe2b);
498 * test we added pdb entries to the dataset sequence
500 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
501 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
502 "PDB Entries were not found on dataset sequence.");
505 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
507 Assert.assertEquals(pdbe1a,
508 sq.getDatasetSequence().getPDBEntry("1PDB"),
509 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
510 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
511 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
512 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
513 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
514 Annotation[] annots = annotsList.toArray(new Annotation[0]);
515 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
516 "Test annot description", annots));
517 sq.getDatasetSequence().addAlignmentAnnotation(
518 new AlignmentAnnotation("Test annot", "Test annot description",
520 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
521 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
523 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
524 Assert.assertNotNull(sq.getAnnotation());
525 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
526 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
529 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
531 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
533 Sequence derived = (Sequence) sq.deriveSequence();
535 Assert.assertEquals(derived.getDescription(),
536 "Test sequence description..");
537 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
538 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
539 Assert.assertNotNull(derived.getAnnotation());
540 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
541 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
542 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
544 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
546 assertEquals("CD", derived.getSequenceAsString());
547 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
549 assertNull(sq.sequenceFeatures);
550 assertNull(derived.sequenceFeatures);
551 // derived sequence should access dataset sequence features
552 assertNotNull(sq.getSequenceFeatures());
553 assertArrayEquals(sq.getSequenceFeatures(),
554 derived.getSequenceFeatures());
557 * verify we have primary db refs *just* for PDB IDs with associated
561 assertEquals(primRefs, sq.getPrimaryDBRefs());
562 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
564 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
569 * Test for deriveSequence applied to an ungapped sequence with no dataset
571 @Test(groups = { "Functional" })
572 public void testDeriveSequence_noDatasetUngapped()
574 SequenceI sq = new Sequence("Seq1", "ABCDEF");
575 assertEquals(1, sq.getStart());
576 assertEquals(6, sq.getEnd());
577 SequenceI derived = sq.deriveSequence();
578 assertEquals("ABCDEF", derived.getSequenceAsString());
579 assertEquals("ABCDEF", derived.getDatasetSequence()
580 .getSequenceAsString());
584 * Test for deriveSequence applied to a gapped sequence with no dataset
586 @Test(groups = { "Functional" })
587 public void testDeriveSequence_noDatasetGapped()
589 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
590 assertEquals(1, sq.getStart());
591 assertEquals(6, sq.getEnd());
592 assertNull(sq.getDatasetSequence());
593 SequenceI derived = sq.deriveSequence();
594 assertEquals("AB-C.D EF", derived.getSequenceAsString());
595 assertEquals("ABCDEF", derived.getDatasetSequence()
596 .getSequenceAsString());
599 @Test(groups = { "Functional" })
600 public void testCopyConstructor_noDataset()
602 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
603 seq1.setDescription("description");
604 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
606 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
608 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
609 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
611 SequenceI copy = new Sequence(seq1);
613 assertNull(copy.getDatasetSequence());
615 verifyCopiedSequence(seq1, copy);
617 // copy has a copy of the DBRefEntry
618 // this is murky - DBrefs are only copied for dataset sequences
619 // where the test for 'dataset sequence' is 'dataset is null'
620 // but that doesn't distinguish it from an aligned sequence
621 // which has not yet generated a dataset sequence
622 // NB getDBRef looks inside dataset sequence if not null
623 DBRefEntry[] dbrefs = copy.getDBRefs();
624 assertEquals(1, dbrefs.length);
625 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
626 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
629 @Test(groups = { "Functional" })
630 public void testCopyConstructor_withDataset()
632 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
633 seq1.createDatasetSequence();
634 seq1.setDescription("description");
635 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
637 // JAL-2046 - what is the contract for using a derived sequence's
638 // addSequenceFeature ?
639 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
641 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
642 // here we add DBRef to the dataset sequence:
643 seq1.getDatasetSequence().addDBRef(
644 new DBRefEntry("EMBL", "1.2", "AZ12345"));
646 SequenceI copy = new Sequence(seq1);
648 assertNotNull(copy.getDatasetSequence());
649 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
651 verifyCopiedSequence(seq1, copy);
653 // getDBRef looks inside dataset sequence and this is shared,
654 // so holds the same dbref objects
655 DBRefEntry[] dbrefs = copy.getDBRefs();
656 assertEquals(1, dbrefs.length);
657 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
661 * Helper to make assertions about a copied sequence
666 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
668 // verify basic properties:
669 assertEquals(copy.getName(), seq1.getName());
670 assertEquals(copy.getDescription(), seq1.getDescription());
671 assertEquals(copy.getStart(), seq1.getStart());
672 assertEquals(copy.getEnd(), seq1.getEnd());
673 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
675 // copy has a copy of the annotation:
676 AlignmentAnnotation[] anns = copy.getAnnotation();
677 assertEquals(1, anns.length);
678 assertFalse(anns[0] == seq1.getAnnotation()[0]);
679 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
680 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
681 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
683 // copy has a copy of the sequence feature:
684 SequenceFeature[] sfs = copy.getSequenceFeatures();
685 assertEquals(1, sfs.length);
686 if (seq1.getDatasetSequence() != null
687 && copy.getDatasetSequence() == seq1.getDatasetSequence())
689 assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
693 assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
695 assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
697 // copy has a copy of the PDB entry
698 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
699 assertEquals(1, pdbs.size());
700 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
701 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
704 @Test(groups = "Functional")
705 public void testGetCharAt()
707 SequenceI sq = new Sequence("", "abcde");
708 assertEquals('a', sq.getCharAt(0));
709 assertEquals('e', sq.getCharAt(4));
710 assertEquals(' ', sq.getCharAt(5));
711 assertEquals(' ', sq.getCharAt(-1));
715 * Tests for adding (or updating) dbrefs
717 * @see DBRefEntry#updateFrom(DBRefEntry)
719 @Test(groups = { "Functional" })
720 public void testAddDBRef()
722 SequenceI sq = new Sequence("", "abcde");
723 assertNull(sq.getDBRefs());
724 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
726 assertEquals(1, sq.getDBRefs().length);
727 assertSame(dbref, sq.getDBRefs()[0]);
730 * change of version - new entry
732 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
734 assertEquals(2, sq.getDBRefs().length);
735 assertSame(dbref, sq.getDBRefs()[0]);
736 assertSame(dbref2, sq.getDBRefs()[1]);
739 * matches existing entry - not added
741 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
742 assertEquals(2, sq.getDBRefs().length);
745 * different source = new entry
747 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
749 assertEquals(3, sq.getDBRefs().length);
750 assertSame(dbref3, sq.getDBRefs()[2]);
753 * different ref = new entry
755 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
757 assertEquals(4, sq.getDBRefs().length);
758 assertSame(dbref4, sq.getDBRefs()[3]);
761 * matching ref with a mapping - map updated
763 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
764 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
768 assertEquals(4, sq.getDBRefs().length);
769 assertSame(dbref4, sq.getDBRefs()[3]);
770 assertSame(map, dbref4.getMap());
773 * 'real' version replaces "0" version
775 dbref2.setVersion("0");
776 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
777 dbref2.getAccessionId());
779 assertEquals(4, sq.getDBRefs().length);
780 assertSame(dbref2, sq.getDBRefs()[1]);
781 assertEquals("3", dbref2.getVersion());
784 * 'real' version replaces "source:0" version
786 dbref3.setVersion("Uniprot:0");
787 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
788 dbref3.getAccessionId());
790 assertEquals(4, sq.getDBRefs().length);
791 assertSame(dbref3, sq.getDBRefs()[2]);
792 assertEquals("3", dbref2.getVersion());
795 @Test(groups = { "Functional" })
796 public void testGetPrimaryDBRefs_peptide()
798 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
801 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
802 assertTrue(primaryDBRefs.isEmpty());
805 sq.setDBRefs(new DBRefEntry[] {});
806 primaryDBRefs = sq.getPrimaryDBRefs();
807 assertTrue(primaryDBRefs.isEmpty());
810 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
811 sq.addDBRef(upentry1);
813 // primary - uniprot with congruent map
814 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
815 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
816 new int[] { 10, 22 }, 1, 1)));
817 sq.addDBRef(upentry2);
819 // primary - uniprot with map of enclosing sequence
820 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
821 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
822 new int[] { 8, 24 }, 1, 1)));
823 sq.addDBRef(upentry3);
825 // not primary - uniprot with map of sub-sequence (5')
826 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
827 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
828 new int[] { 10, 18 }, 1, 1)));
829 sq.addDBRef(upentry4);
831 // not primary - uniprot with map that overlaps 3'
832 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
833 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
834 new int[] { 12, 22 }, 1, 1)));
835 sq.addDBRef(upentry5);
837 // not primary - uniprot with map to different coordinates frame
838 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
839 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
840 new int[] { 112, 118 }, 1, 1)));
841 sq.addDBRef(upentry6);
843 // not primary - dbref to 'non-core' database
844 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
845 sq.addDBRef(upentry7);
847 // primary - type is PDB
848 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
849 sq.addDBRef(pdbentry);
851 // not primary - PDBEntry has no file
852 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
854 // not primary - no PDBEntry
855 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
857 // add corroborating PDB entry for primary DBref -
858 // needs to have a file as well as matching ID
859 // note PDB ID is not treated as case sensitive
860 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
863 // not valid DBRef - no file..
864 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
866 primaryDBRefs = sq.getPrimaryDBRefs();
867 assertEquals(4, primaryDBRefs.size());
868 assertTrue("Couldn't find simple primary reference (UNIPROT)",
869 primaryDBRefs.contains(upentry1));
870 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
871 primaryDBRefs.contains(upentry2));
872 assertTrue("Couldn't find mapped context reference (UNIPROT)",
873 primaryDBRefs.contains(upentry3));
874 assertTrue("Couldn't find expected PDB primary reference",
875 primaryDBRefs.contains(pdbentry));
878 @Test(groups = { "Functional" })
879 public void testGetPrimaryDBRefs_nucleotide()
881 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
884 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
887 // not primary - Ensembl 'transcript' mapping of sub-sequence
888 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
889 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
890 new int[] { 1, 11 }, 1, 1)));
893 // primary - EMBL with congruent map
894 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
895 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
896 new int[] { 10, 34 }, 1, 1)));
899 // not primary - to non-core database
900 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
903 // not primary - to protein
904 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
907 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
908 assertEquals(2, primaryDBRefs.size());
909 assertTrue(primaryDBRefs.contains(dbr1));
910 assertTrue(primaryDBRefs.contains(dbr3));
914 * Test the method that updates the list of PDBEntry from any new DBRefEntry
917 @Test(groups = { "Functional" })
918 public void testUpdatePDBIds()
920 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
922 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
923 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
924 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
925 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
926 // 7 is not a valid chain code:
927 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
930 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
931 assertEquals(4, pdbIds.size());
932 assertSame(pdbe1, pdbIds.get(0));
933 // chain code got added to 3A6S:
934 assertEquals("B", pdbe1.getChainCode());
935 assertEquals("1A70", pdbIds.get(1).getId());
936 // 4BQGA is parsed into id + chain
937 assertEquals("4BQG", pdbIds.get(2).getId());
938 assertEquals("a", pdbIds.get(2).getChainCode());
939 assertEquals("2GIS7", pdbIds.get(3).getId());
940 assertNull(pdbIds.get(3).getChainCode());
944 * Test the method that either adds a pdbid or updates an existing one
946 @Test(groups = { "Functional" })
947 public void testAddPDBId()
949 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
951 assertEquals(1, seq.getAllPDBEntries().size());
952 assertSame(pdbe, seq.getPDBEntry("3A6S"));
953 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
955 // add the same entry
957 assertEquals(1, seq.getAllPDBEntries().size());
958 assertSame(pdbe, seq.getPDBEntry("3A6S"));
960 // add an identical entry
961 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
962 assertEquals(1, seq.getAllPDBEntries().size());
963 assertSame(pdbe, seq.getPDBEntry("3A6S"));
965 // add a different entry
966 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
968 assertEquals(2, seq.getAllPDBEntries().size());
969 assertSame(pdbe, seq.getAllPDBEntries().get(0));
970 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
972 // update pdbe with chain code, file, type
973 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
975 assertEquals(2, seq.getAllPDBEntries().size());
976 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
977 assertEquals("3A6S", pdbe.getId()); // unchanged
978 assertEquals("A", pdbe.getChainCode()); // updated
979 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
980 assertEquals("filepath", pdbe.getFile()); // updated
981 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
983 // add with a different file path
984 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
986 assertEquals(3, seq.getAllPDBEntries().size());
987 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
989 // add with a different chain code
990 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
992 assertEquals(4, seq.getAllPDBEntries().size());
993 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
997 groups = { "Functional" },
998 expectedExceptions = { IllegalArgumentException.class })
999 public void testSetDatasetSequence_toSelf()
1001 seq.setDatasetSequence(seq);
1005 groups = { "Functional" },
1006 expectedExceptions = { IllegalArgumentException.class })
1007 public void testSetDatasetSequence_cascading()
1009 SequenceI seq2 = new Sequence("Seq2", "xyz");
1010 seq2.createDatasetSequence();
1011 seq.setDatasetSequence(seq2);