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.util.MapList;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Vector;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
44 public class SequenceTest
48 @BeforeMethod(alwaysRun = true)
51 seq = new Sequence("FER1", "AKPNGVL");
54 @Test(groups = { "Functional" })
55 public void testInsertGapsAndGapmaps()
57 SequenceI aseq = seq.deriveSequence();
58 aseq.insertCharAt(2, 3, '-');
59 aseq.insertCharAt(6, 3, '-');
60 assertEquals("Gap insertions not correct", "AK---P---NGVL",
61 aseq.getSequenceAsString());
62 List<int[]> gapInt = aseq.getInsertions();
63 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
64 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
65 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
66 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
69 @Test(groups = ("Functional"))
70 public void testIsProtein()
73 assertTrue(new Sequence("prot","ASDFASDFASDF").isProtein());
75 assertFalse(new Sequence("prot","ACGTACGTACGT").isProtein());
77 SequenceI sq = new Sequence("prot","ACGUACGUACGU");
78 assertFalse(sq.isProtein());
79 // change sequence, should trigger an update of cached result
80 sq.setSequence("ASDFASDFADSF");
81 assertTrue(sq.isProtein());
83 * in situ change of sequence doesn't change hashcode :-O
84 * (sequence should not expose internal implementation)
86 for (int i = 0; i < sq.getSequence().length; i++)
88 sq.getSequence()[i] = "acgtu".charAt(i % 5);
90 assertTrue(sq.isProtein()); // but it isn't
93 @Test(groups = { "Functional" })
94 public void testGetAnnotation()
96 // initial state returns null not an empty array
97 assertNull(seq.getAnnotation());
98 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
100 AlignmentAnnotation[] anns = seq.getAnnotation();
101 assertEquals(1, anns.length);
102 assertSame(ann, anns[0]);
104 // removing all annotations reverts array to null
105 seq.removeAlignmentAnnotation(ann);
106 assertNull(seq.getAnnotation());
109 @Test(groups = { "Functional" })
110 public void testGetAnnotation_forLabel()
112 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
114 addAnnotation("label2", "desc2", "calcId2", 1f);
115 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
117 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
118 assertEquals(2, anns.length);
119 assertSame(ann1, anns[0]);
120 assertSame(ann3, anns[1]);
123 private AlignmentAnnotation addAnnotation(String label,
124 String description, String calcId, float value)
126 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
128 annotation.setCalcId(calcId);
129 seq.addAlignmentAnnotation(annotation);
133 @Test(groups = { "Functional" })
134 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
136 addAnnotation("label1", "desc1", "calcId1", 1f);
137 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
139 addAnnotation("label2", "desc3", "calcId3", 1f);
140 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
142 addAnnotation("label5", "desc3", null, 1f);
143 addAnnotation(null, "desc3", "calcId3", 1f);
145 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
147 assertEquals(2, anns.size());
148 assertSame(ann2, anns.get(0));
149 assertSame(ann4, anns.get(1));
151 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
152 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
153 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
154 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
155 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
159 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
160 * setting the sequenceRef on the annotation. Adding the same annotation twice
163 @Test(groups = { "Functional" })
164 public void testAddAlignmentAnnotation()
166 assertNull(seq.getAnnotation());
167 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
169 assertNull(annotation.sequenceRef);
170 seq.addAlignmentAnnotation(annotation);
171 assertSame(seq, annotation.sequenceRef);
172 AlignmentAnnotation[] anns = seq.getAnnotation();
173 assertEquals(1, anns.length);
174 assertSame(annotation, anns[0]);
176 // re-adding does nothing
177 seq.addAlignmentAnnotation(annotation);
178 anns = seq.getAnnotation();
179 assertEquals(1, anns.length);
180 assertSame(annotation, anns[0]);
182 // an identical but different annotation can be added
183 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
185 seq.addAlignmentAnnotation(annotation2);
186 anns = seq.getAnnotation();
187 assertEquals(2, anns.length);
188 assertSame(annotation, anns[0]);
189 assertSame(annotation2, anns[1]);
192 @Test(groups = { "Functional" })
193 public void testGetStartGetEnd()
195 SequenceI sq = new Sequence("test", "ABCDEF");
196 assertEquals(1, sq.getStart());
197 assertEquals(6, sq.getEnd());
199 sq = new Sequence("test", "--AB-C-DEF--");
200 assertEquals(1, sq.getStart());
201 assertEquals(6, sq.getEnd());
203 sq = new Sequence("test", "----");
204 assertEquals(1, sq.getStart());
205 assertEquals(0, sq.getEnd()); // ??
209 * Tests for the method that returns an alignment column position (base 1) for
210 * a given sequence position (base 1).
212 @Test(groups = { "Functional" })
213 public void testFindIndex()
215 SequenceI sq = new Sequence("test", "ABCDEF");
216 assertEquals(0, sq.findIndex(0));
217 assertEquals(1, sq.findIndex(1));
218 assertEquals(5, sq.findIndex(5));
219 assertEquals(6, sq.findIndex(6));
220 assertEquals(6, sq.findIndex(9));
222 sq = new Sequence("test", "-A--B-C-D-E-F--");
223 assertEquals(2, sq.findIndex(1));
224 assertEquals(5, sq.findIndex(2));
225 assertEquals(7, sq.findIndex(3));
227 // before start returns 0
228 assertEquals(0, sq.findIndex(0));
229 assertEquals(0, sq.findIndex(-1));
231 // beyond end returns last residue column
232 assertEquals(13, sq.findIndex(99));
237 * Tests for the method that returns a dataset sequence position (base 1) for
238 * an aligned column position (base 0).
240 @Test(groups = { "Functional" })
241 public void testFindPosition()
243 SequenceI sq = new Sequence("test", "ABCDEF");
244 assertEquals(1, sq.findPosition(0));
245 assertEquals(6, sq.findPosition(5));
246 // assertEquals(-1, seq.findPosition(6)); // fails
248 sq = new Sequence("test", "AB-C-D--");
249 assertEquals(1, sq.findPosition(0));
250 assertEquals(2, sq.findPosition(1));
251 // gap position 'finds' residue to the right (not the left as per javadoc)
252 assertEquals(3, sq.findPosition(2));
253 assertEquals(3, sq.findPosition(3));
254 assertEquals(4, sq.findPosition(4));
255 assertEquals(4, sq.findPosition(5));
256 // returns 1 more than sequence length if off the end ?!?
257 assertEquals(5, sq.findPosition(6));
258 assertEquals(5, sq.findPosition(7));
260 sq = new Sequence("test", "--AB-C-DEF--");
261 assertEquals(1, sq.findPosition(0));
262 assertEquals(1, sq.findPosition(1));
263 assertEquals(1, sq.findPosition(2));
264 assertEquals(2, sq.findPosition(3));
265 assertEquals(3, sq.findPosition(4));
266 assertEquals(3, sq.findPosition(5));
267 assertEquals(4, sq.findPosition(6));
268 assertEquals(4, sq.findPosition(7));
269 assertEquals(5, sq.findPosition(8));
270 assertEquals(6, sq.findPosition(9));
271 assertEquals(7, sq.findPosition(10));
272 assertEquals(7, sq.findPosition(11));
275 @Test(groups = { "Functional" })
276 public void testDeleteChars()
278 SequenceI sq = new Sequence("test", "ABCDEF");
279 assertEquals(1, sq.getStart());
280 assertEquals(6, sq.getEnd());
281 sq.deleteChars(2, 3);
282 assertEquals("ABDEF", sq.getSequenceAsString());
283 assertEquals(1, sq.getStart());
284 assertEquals(5, sq.getEnd());
286 sq = new Sequence("test", "ABCDEF");
287 sq.deleteChars(0, 2);
288 assertEquals("CDEF", sq.getSequenceAsString());
289 assertEquals(3, sq.getStart());
290 assertEquals(6, sq.getEnd());
293 @Test(groups = { "Functional" })
294 public void testInsertCharAt()
296 // non-static methods:
297 SequenceI sq = new Sequence("test", "ABCDEF");
298 sq.insertCharAt(0, 'z');
299 assertEquals("zABCDEF", sq.getSequenceAsString());
300 sq.insertCharAt(2, 2, 'x');
301 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
303 // for static method see StringUtilsTest
307 * Test the method that returns an array of aligned sequence positions where
308 * the array index is the data sequence position (both base 0).
310 @Test(groups = { "Functional" })
311 public void testGapMap()
313 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
314 sq.createDatasetSequence();
315 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
319 * Test the method that gets sequence features, either from the sequence or
322 @Test(groups = { "Functional" })
323 public void testGetSequenceFeatures()
325 SequenceI sq = new Sequence("test", "GATCAT");
326 sq.createDatasetSequence();
328 assertNull(sq.getSequenceFeatures());
331 * SequenceFeature on sequence
333 SequenceFeature sf = new SequenceFeature();
334 sq.addSequenceFeature(sf);
335 SequenceFeature[] sfs = sq.getSequenceFeatures();
336 assertEquals(1, sfs.length);
337 assertSame(sf, sfs[0]);
341 * SequenceFeature on sequence and dataset sequence; returns that on
344 * Note JAL-2046: spurious: we have no use case for this at the moment.
345 * This test also buggy - as sf2.equals(sf), no new feature is added
347 SequenceFeature sf2 = new SequenceFeature();
348 sq.getDatasetSequence().addSequenceFeature(sf2);
349 sfs = sq.getSequenceFeatures();
350 assertEquals(1, sfs.length);
351 assertSame(sf, sfs[0]);
354 * SequenceFeature on dataset sequence only
355 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
357 sq.setSequenceFeatures(null);
358 assertNull(sq.getDatasetSequence().getSequenceFeatures());
361 * Corrupt case - no SequenceFeature, dataset's dataset is the original
362 * sequence. Test shows no infinite loop results.
364 sq.getDatasetSequence().setSequenceFeatures(null);
366 * is there a usecase for this ? setDatasetSequence should throw an error if
367 * this actually occurs.
371 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
372 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
375 // TODO Jalview error/exception class for raising implementation errors
376 assertTrue(e.getMessage().toLowerCase()
377 .contains("implementation error"));
379 assertNull(sq.getSequenceFeatures());
383 * Test the method that returns an array, indexed by sequence position, whose
384 * entries are the residue positions at the sequence position (or to the right
387 @Test(groups = { "Functional" })
388 public void testFindPositionMap()
391 * Note: Javadoc for findPosition says it returns the residue position to
392 * the left of a gapped position; in fact it returns the position to the
393 * right. Also it returns a non-existent residue position for a gap beyond
396 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
397 int[] map = sq.findPositionMap();
398 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
399 Arrays.toString(map));
403 * Test for getSubsequence
405 @Test(groups = { "Functional" })
406 public void testGetSubsequence()
408 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
409 sq.createDatasetSequence();
411 // positions are base 0, end position is exclusive
412 SequenceI subseq = sq.getSubSequence(2, 4);
414 assertEquals("CD", subseq.getSequenceAsString());
415 // start/end are base 1 positions
416 assertEquals(3, subseq.getStart());
417 assertEquals(4, subseq.getEnd());
418 // subsequence shares the full dataset sequence
419 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
423 * test createDatasetSequence behaves to doc
425 @Test(groups = { "Functional" })
426 public void testCreateDatasetSequence()
428 SequenceI sq = new Sequence("my","ASDASD");
429 assertNull(sq.getDatasetSequence());
430 SequenceI rds = sq.createDatasetSequence();
432 assertNull(rds.getDatasetSequence());
433 assertEquals(sq.getDatasetSequence(), rds);
437 * Test for deriveSequence applied to a sequence with a dataset
439 @Test(groups = { "Functional" })
440 public void testDeriveSequence_existingDataset()
442 Sequence sq = new Sequence("Seq1", "CD");
443 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
444 sq.getDatasetSequence().addSequenceFeature(
445 new SequenceFeature("", "", 1, 2, 0f, null));
449 sq.setDescription("Test sequence description..");
450 sq.setVamsasId("TestVamsasId");
451 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
453 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
454 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
455 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
456 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
458 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
459 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
460 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
461 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
463 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
464 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version1", "2PDB");
467 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
470 sq.getDatasetSequence().addDBRef(pdb1pdb);
471 sq.getDatasetSequence().addDBRef(pdb2pdb);
472 sq.getDatasetSequence().addDBRef(
473 new DBRefEntry("PDB", "version3", "3PDB"));
474 sq.getDatasetSequence().addDBRef(
475 new DBRefEntry("PDB", "version4", "4PDB"));
477 PDBEntry pdbe1a=new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
478 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
479 PDBEntry pdbe2a=new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2");
480 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2");
481 sq.getDatasetSequence().addPDBId(
483 sq.getDatasetSequence().addPDBId(
485 sq.getDatasetSequence().addPDBId(pdbe2a);
486 sq.getDatasetSequence().addPDBId(pdbe2b);
489 * test we added pdb entries to the dataset sequence
491 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
492 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
493 "PDB Entries were not found on dataset sequence.");
496 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
498 Assert.assertEquals(pdbe1a,
499 sq.getDatasetSequence().getPDBEntry("1PDB"),
500 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
501 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
502 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
503 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
504 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
505 Annotation[] annots = annotsList.toArray(new Annotation[0]);
506 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
507 "Test annot description", annots));
508 sq.getDatasetSequence().addAlignmentAnnotation(
509 new AlignmentAnnotation("Test annot", "Test annot description",
511 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
512 Assert.assertEquals(sq.getDBRefs().length, 5);
513 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
514 Assert.assertNotNull(sq.getAnnotation());
515 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
516 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 4);
517 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
519 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
521 Sequence derived = (Sequence) sq.deriveSequence();
523 Assert.assertEquals(derived.getDescription(),
524 "Test sequence description..");
525 Assert.assertEquals(derived.getDBRefs().length, 4); // come from dataset
526 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
527 Assert.assertNotNull(derived.getAnnotation());
528 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
529 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 4);
530 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
532 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
534 assertEquals("CD", derived.getSequenceAsString());
535 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
537 assertNull(sq.sequenceFeatures);
538 assertNull(derived.sequenceFeatures);
539 // derived sequence should access dataset sequence features
540 assertNotNull(sq.getSequenceFeatures());
541 assertArrayEquals(sq.getSequenceFeatures(),
542 derived.getSequenceFeatures());
545 * verify we have primary db refs *just* for PDB IDs with associated
549 assertEquals(primRefs, sq.getPrimaryDBRefs());
550 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
552 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
557 * Test for deriveSequence applied to an ungapped sequence with no dataset
559 @Test(groups = { "Functional" })
560 public void testDeriveSequence_noDatasetUngapped()
562 SequenceI sq = new Sequence("Seq1", "ABCDEF");
563 assertEquals(1, sq.getStart());
564 assertEquals(6, sq.getEnd());
565 SequenceI derived = sq.deriveSequence();
566 assertEquals("ABCDEF", derived.getSequenceAsString());
567 assertEquals("ABCDEF", derived.getDatasetSequence()
568 .getSequenceAsString());
572 * Test for deriveSequence applied to a gapped sequence with no dataset
574 @Test(groups = { "Functional" })
575 public void testDeriveSequence_noDatasetGapped()
577 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
578 assertEquals(1, sq.getStart());
579 assertEquals(6, sq.getEnd());
580 assertNull(sq.getDatasetSequence());
581 SequenceI derived = sq.deriveSequence();
582 assertEquals("AB-C.D EF", derived.getSequenceAsString());
583 assertEquals("ABCDEF", derived.getDatasetSequence()
584 .getSequenceAsString());
587 @Test(groups = { "Functional" })
588 public void testCopyConstructor_noDataset()
590 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
591 seq1.setDescription("description");
592 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
594 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
596 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
597 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
599 SequenceI copy = new Sequence(seq1);
601 assertNull(copy.getDatasetSequence());
603 verifyCopiedSequence(seq1, copy);
605 // copy has a copy of the DBRefEntry
606 // this is murky - DBrefs are only copied for dataset sequences
607 // where the test for 'dataset sequence' is 'dataset is null'
608 // but that doesn't distinguish it from an aligned sequence
609 // which has not yet generated a dataset sequence
610 // NB getDBRef looks inside dataset sequence if not null
611 DBRefEntry[] dbrefs = copy.getDBRefs();
612 assertEquals(1, dbrefs.length);
613 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
614 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
617 @Test(groups = { "Functional" })
618 public void testCopyConstructor_withDataset()
620 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
621 seq1.createDatasetSequence();
622 seq1.setDescription("description");
623 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
625 // JAL-2046 - what is the contract for using a derived sequence's
626 // addSequenceFeature ?
627 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
629 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
630 // here we add DBRef to the dataset sequence:
631 seq1.getDatasetSequence().addDBRef(
632 new DBRefEntry("EMBL", "1.2", "AZ12345"));
634 SequenceI copy = new Sequence(seq1);
636 assertNotNull(copy.getDatasetSequence());
637 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
639 verifyCopiedSequence(seq1, copy);
641 // getDBRef looks inside dataset sequence and this is shared,
642 // so holds the same dbref objects
643 DBRefEntry[] dbrefs = copy.getDBRefs();
644 assertEquals(1, dbrefs.length);
645 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
649 * Helper to make assertions about a copied sequence
654 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
656 // verify basic properties:
657 assertEquals(copy.getName(), seq1.getName());
658 assertEquals(copy.getDescription(), seq1.getDescription());
659 assertEquals(copy.getStart(), seq1.getStart());
660 assertEquals(copy.getEnd(), seq1.getEnd());
661 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
663 // copy has a copy of the annotation:
664 AlignmentAnnotation[] anns = copy.getAnnotation();
665 assertEquals(1, anns.length);
666 assertFalse(anns[0] == seq1.getAnnotation()[0]);
667 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
668 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
669 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
671 // copy has a copy of the sequence feature:
672 SequenceFeature[] sfs = copy.getSequenceFeatures();
673 assertEquals(1, sfs.length);
674 if (seq1.getDatasetSequence()!=null && copy.getDatasetSequence()==seq1.getDatasetSequence()) {
675 assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
677 assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
679 assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
681 // copy has a copy of the PDB entry
682 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
683 assertEquals(1, pdbs.size());
684 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
685 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
688 @Test(groups = "Functional")
689 public void testGetCharAt()
691 SequenceI sq = new Sequence("", "abcde");
692 assertEquals('a', sq.getCharAt(0));
693 assertEquals('e', sq.getCharAt(4));
694 assertEquals(' ', sq.getCharAt(5));
695 assertEquals(' ', sq.getCharAt(-1));
699 * Tests for adding (or updating) dbrefs
701 * @see DBRefEntry#updateFrom(DBRefEntry)
703 @Test(groups = { "Functional" })
704 public void testAddDBRef()
706 SequenceI sq = new Sequence("", "abcde");
707 assertNull(sq.getDBRefs());
708 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
710 assertEquals(1, sq.getDBRefs().length);
711 assertSame(dbref, sq.getDBRefs()[0]);
714 * change of version - new entry
716 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
718 assertEquals(2, sq.getDBRefs().length);
719 assertSame(dbref, sq.getDBRefs()[0]);
720 assertSame(dbref2, sq.getDBRefs()[1]);
723 * matches existing entry - not added
725 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
726 assertEquals(2, sq.getDBRefs().length);
729 * different source = new entry
731 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
733 assertEquals(3, sq.getDBRefs().length);
734 assertSame(dbref3, sq.getDBRefs()[2]);
737 * different ref = new entry
739 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
741 assertEquals(4, sq.getDBRefs().length);
742 assertSame(dbref4, sq.getDBRefs()[3]);
745 * matching ref with a mapping - map updated
747 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
748 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
752 assertEquals(4, sq.getDBRefs().length);
753 assertSame(dbref4, sq.getDBRefs()[3]);
754 assertSame(map, dbref4.getMap());
757 * 'real' version replaces "0" version
759 dbref2.setVersion("0");
760 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
761 dbref2.getAccessionId());
763 assertEquals(4, sq.getDBRefs().length);
764 assertSame(dbref2, sq.getDBRefs()[1]);
765 assertEquals("3", dbref2.getVersion());
768 * 'real' version replaces "source:0" version
770 dbref3.setVersion("Uniprot:0");
771 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
772 dbref3.getAccessionId());
774 assertEquals(4, sq.getDBRefs().length);
775 assertSame(dbref3, sq.getDBRefs()[2]);
776 assertEquals("3", dbref2.getVersion());
779 @Test(groups = { "Functional" })
780 public void testGetPrimaryDBRefs_peptide()
782 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
785 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
786 assertTrue(primaryDBRefs.isEmpty());
789 sq.setDBRefs(new DBRefEntry[] {});
790 primaryDBRefs = sq.getPrimaryDBRefs();
791 assertTrue(primaryDBRefs.isEmpty());
794 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
795 sq.addDBRef(upentry1);
797 // primary - uniprot with congruent map
798 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
799 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
800 new int[] { 10, 22 }, 1, 1)));
801 sq.addDBRef(upentry2);
803 // primary - uniprot with map of enclosing sequence
804 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
805 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
806 new int[] { 8, 24 }, 1, 1)));
807 sq.addDBRef(upentry3);
809 // not primary - uniprot with map of sub-sequence (5')
810 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
811 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
812 new int[] { 10, 18 }, 1, 1)));
813 sq.addDBRef(upentry4);
815 // not primary - uniprot with map that overlaps 3'
816 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
817 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
818 new int[] { 12, 22 }, 1, 1)));
819 sq.addDBRef(upentry5);
821 // not primary - uniprot with map to different coordinates frame
822 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
823 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
824 new int[] { 112, 118 }, 1, 1)));
825 sq.addDBRef(upentry6);
827 // not primary - dbref to 'non-core' database
828 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
829 sq.addDBRef(upentry7);
831 // primary - type is PDB
832 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
833 sq.addDBRef(pdbentry);
835 // not primary - PDBEntry has no file
836 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
838 // not primary - no PDBEntry
839 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
841 // add corroborating PDB entry for primary DBref -
842 // needs to have a file as well as matching ID
843 // note PDB ID is not treated as case sensitive
844 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
847 // not valid DBRef - no file..
848 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
850 primaryDBRefs = sq.getPrimaryDBRefs();
851 assertEquals(4, primaryDBRefs.size());
852 assertTrue("Couldn't find simple primary reference (UNIPROT)",
853 primaryDBRefs.contains(upentry1));
854 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
855 primaryDBRefs.contains(upentry2));
856 assertTrue("Couldn't find mapped context reference (UNIPROT)",
857 primaryDBRefs.contains(upentry3));
858 assertTrue("Couldn't find expected PDB primary reference",
859 primaryDBRefs.contains(pdbentry));
862 @Test(groups = { "Functional" })
863 public void testGetPrimaryDBRefs_nucleotide()
865 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
868 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
871 // not primary - Ensembl 'transcript' mapping of sub-sequence
872 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
873 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
874 new int[] { 1, 11 }, 1, 1)));
877 // primary - EMBL with congruent map
878 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
879 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
880 new int[] { 10, 34 }, 1, 1)));
883 // not primary - to non-core database
884 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
887 // not primary - to protein
888 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
891 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
892 assertEquals(2, primaryDBRefs.size());
893 assertTrue(primaryDBRefs.contains(dbr1));
894 assertTrue(primaryDBRefs.contains(dbr3));
898 * Test the method that updates the list of PDBEntry from any new DBRefEntry
901 @Test(groups = { "Functional" })
902 public void testUpdatePDBIds()
904 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
906 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
907 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
908 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
909 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
910 // 7 is not a valid chain code:
911 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
914 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
915 assertEquals(4, pdbIds.size());
916 assertSame(pdbe1, pdbIds.get(0));
917 // chain code got added to 3A6S:
918 assertEquals("B", pdbe1.getChainCode());
919 assertEquals("1A70", pdbIds.get(1).getId());
920 // 4BQGA is parsed into id + chain
921 assertEquals("4BQG", pdbIds.get(2).getId());
922 assertEquals("a", pdbIds.get(2).getChainCode());
923 assertEquals("2GIS7", pdbIds.get(3).getId());
924 assertNull(pdbIds.get(3).getChainCode());
928 * Test the method that either adds a pdbid or updates an existing one
930 @Test(groups = { "Functional" })
931 public void testAddPDBId()
933 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
935 assertEquals(1, seq.getAllPDBEntries().size());
936 assertSame(pdbe, seq.getPDBEntry("3A6S"));
937 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
939 // add the same entry
941 assertEquals(1, seq.getAllPDBEntries().size());
942 assertSame(pdbe, seq.getPDBEntry("3A6S"));
944 // add an identical entry
945 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
946 assertEquals(1, seq.getAllPDBEntries().size());
947 assertSame(pdbe, seq.getPDBEntry("3A6S"));
949 // add a different entry
950 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
952 assertEquals(2, seq.getAllPDBEntries().size());
953 assertSame(pdbe, seq.getAllPDBEntries().get(0));
954 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
956 // update pdbe with chain code, file, type
957 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
959 assertEquals(2, seq.getAllPDBEntries().size());
960 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
961 assertEquals("3A6S", pdbe.getId()); // unchanged
962 assertEquals("A", pdbe.getChainCode()); // updated
963 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
964 assertEquals("filepath", pdbe.getFile()); // updated
965 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
967 // add with a different file path
968 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
970 assertEquals(3, seq.getAllPDBEntries().size());
971 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
973 // add with a different chain code
974 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
976 assertEquals(4, seq.getAllPDBEntries().size());
977 assertSame(pdbe5, seq.getAllPDBEntries().get(3));