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;
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.BitSet;
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]);
81 BitSet gapfield = aseq.getInsertionsAsBits();
82 BitSet expectedgaps = new BitSet();
83 expectedgaps.set(2, 5);
84 expectedgaps.set(6, 9);
86 assertEquals(6, expectedgaps.cardinality());
88 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
89 6, gapfield.cardinality());
91 assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield);
94 @Test(groups = ("Functional"))
95 public void testIsProtein()
98 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
100 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
102 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
103 assertFalse(sq.isProtein());
104 // change sequence, should trigger an update of cached result
105 sq.setSequence("ASDFASDFADSF");
106 assertTrue(sq.isProtein());
108 * in situ change of sequence doesn't change hashcode :-O
109 * (sequence should not expose internal implementation)
111 for (int i = 0; i < sq.getSequence().length; i++)
113 sq.getSequence()[i] = "acgtu".charAt(i % 5);
115 assertTrue(sq.isProtein()); // but it isn't
118 @Test(groups = { "Functional" })
119 public void testGetAnnotation()
121 // initial state returns null not an empty array
122 assertNull(seq.getAnnotation());
123 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
125 AlignmentAnnotation[] anns = seq.getAnnotation();
126 assertEquals(1, anns.length);
127 assertSame(ann, anns[0]);
129 // removing all annotations reverts array to null
130 seq.removeAlignmentAnnotation(ann);
131 assertNull(seq.getAnnotation());
134 @Test(groups = { "Functional" })
135 public void testGetAnnotation_forLabel()
137 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
139 addAnnotation("label2", "desc2", "calcId2", 1f);
140 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
142 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
143 assertEquals(2, anns.length);
144 assertSame(ann1, anns[0]);
145 assertSame(ann3, anns[1]);
148 private AlignmentAnnotation addAnnotation(String label,
149 String description, String calcId, float value)
151 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
153 annotation.setCalcId(calcId);
154 seq.addAlignmentAnnotation(annotation);
158 @Test(groups = { "Functional" })
159 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
161 addAnnotation("label1", "desc1", "calcId1", 1f);
162 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
164 addAnnotation("label2", "desc3", "calcId3", 1f);
165 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
167 addAnnotation("label5", "desc3", null, 1f);
168 addAnnotation(null, "desc3", "calcId3", 1f);
170 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
172 assertEquals(2, anns.size());
173 assertSame(ann2, anns.get(0));
174 assertSame(ann4, anns.get(1));
176 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
177 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
178 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
179 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
180 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
184 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
185 * setting the sequenceRef on the annotation. Adding the same annotation twice
188 @Test(groups = { "Functional" })
189 public void testAddAlignmentAnnotation()
191 assertNull(seq.getAnnotation());
192 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
194 assertNull(annotation.sequenceRef);
195 seq.addAlignmentAnnotation(annotation);
196 assertSame(seq, annotation.sequenceRef);
197 AlignmentAnnotation[] anns = seq.getAnnotation();
198 assertEquals(1, anns.length);
199 assertSame(annotation, anns[0]);
201 // re-adding does nothing
202 seq.addAlignmentAnnotation(annotation);
203 anns = seq.getAnnotation();
204 assertEquals(1, anns.length);
205 assertSame(annotation, anns[0]);
207 // an identical but different annotation can be added
208 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
210 seq.addAlignmentAnnotation(annotation2);
211 anns = seq.getAnnotation();
212 assertEquals(2, anns.length);
213 assertSame(annotation, anns[0]);
214 assertSame(annotation2, anns[1]);
217 @Test(groups = { "Functional" })
218 public void testGetStartGetEnd()
220 SequenceI sq = new Sequence("test", "ABCDEF");
221 assertEquals(1, sq.getStart());
222 assertEquals(6, sq.getEnd());
224 sq = new Sequence("test", "--AB-C-DEF--");
225 assertEquals(1, sq.getStart());
226 assertEquals(6, sq.getEnd());
228 sq = new Sequence("test", "----");
229 assertEquals(1, sq.getStart());
230 assertEquals(0, sq.getEnd()); // ??
234 * Tests for the method that returns an alignment column position (base 1) for
235 * a given sequence position (base 1).
237 @Test(groups = { "Functional" })
238 public void testFindIndex()
240 SequenceI sq = new Sequence("test", "ABCDEF");
241 assertEquals(0, sq.findIndex(0));
242 assertEquals(1, sq.findIndex(1));
243 assertEquals(5, sq.findIndex(5));
244 assertEquals(6, sq.findIndex(6));
245 assertEquals(6, sq.findIndex(9));
247 sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
248 assertEquals(2, sq.findIndex(8));
249 assertEquals(5, sq.findIndex(9));
250 assertEquals(7, sq.findIndex(10));
252 // before start returns 0
253 assertEquals(0, sq.findIndex(0));
254 assertEquals(0, sq.findIndex(-1));
256 // beyond end returns last residue column
257 assertEquals(13, sq.findIndex(99));
262 * Tests for the method that returns a dataset sequence position (base 1) for
263 * an aligned column position (base 0).
265 @Test(groups = { "Functional" })
266 public void testFindPosition()
268 SequenceI sq = new Sequence("test", "ABCDEF");
269 assertEquals(1, sq.findPosition(0));
270 assertEquals(6, sq.findPosition(5));
271 // assertEquals(-1, seq.findPosition(6)); // fails
273 sq = new Sequence("test", "AB-C-D--");
274 assertEquals(1, sq.findPosition(0));
275 assertEquals(2, sq.findPosition(1));
276 // gap position 'finds' residue to the right (not the left as per javadoc)
277 assertEquals(3, sq.findPosition(2));
278 assertEquals(3, sq.findPosition(3));
279 assertEquals(4, sq.findPosition(4));
280 assertEquals(4, sq.findPosition(5));
281 // returns 1 more than sequence length if off the end ?!?
282 assertEquals(5, sq.findPosition(6));
283 assertEquals(5, sq.findPosition(7));
285 sq = new Sequence("test", "--AB-C-DEF--");
286 assertEquals(1, sq.findPosition(0));
287 assertEquals(1, sq.findPosition(1));
288 assertEquals(1, sq.findPosition(2));
289 assertEquals(2, sq.findPosition(3));
290 assertEquals(3, sq.findPosition(4));
291 assertEquals(3, sq.findPosition(5));
292 assertEquals(4, sq.findPosition(6));
293 assertEquals(4, sq.findPosition(7));
294 assertEquals(5, sq.findPosition(8));
295 assertEquals(6, sq.findPosition(9));
296 assertEquals(7, sq.findPosition(10));
297 assertEquals(7, sq.findPosition(11));
300 @Test(groups = { "Functional" })
301 public void testDeleteChars()
306 SequenceI sq = new Sequence("test", "ABCDEF");
307 assertNull(PA.getValue(sq, "datasetSequence"));
308 assertEquals(1, sq.getStart());
309 assertEquals(6, sq.getEnd());
310 sq.deleteChars(2, 3);
311 assertEquals("ABDEF", sq.getSequenceAsString());
312 assertEquals(1, sq.getStart());
313 assertEquals(5, sq.getEnd());
314 assertNull(PA.getValue(sq, "datasetSequence"));
319 sq = new Sequence("test", "ABCDEF");
320 sq.deleteChars(0, 2);
321 assertEquals("CDEF", sq.getSequenceAsString());
322 assertEquals(3, sq.getStart());
323 assertEquals(6, sq.getEnd());
324 assertNull(PA.getValue(sq, "datasetSequence"));
329 sq = new Sequence("test", "ABCDEF");
330 sq.deleteChars(4, 6);
331 assertEquals("ABCD", sq.getSequenceAsString());
332 assertEquals(1, sq.getStart());
333 assertEquals(4, sq.getEnd());
334 assertNull(PA.getValue(sq, "datasetSequence"));
337 @Test(groups = { "Functional" })
338 public void testDeleteChars_withDbRefsAndFeatures()
341 * internal delete - new dataset sequence created
342 * gets a copy of any dbrefs
344 SequenceI sq = new Sequence("test", "ABCDEF");
345 sq.createDatasetSequence();
346 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
348 Object ds = PA.getValue(sq, "datasetSequence");
350 assertEquals(1, sq.getStart());
351 assertEquals(6, sq.getEnd());
352 sq.deleteChars(2, 3);
353 assertEquals("ABDEF", sq.getSequenceAsString());
354 assertEquals(1, sq.getStart());
355 assertEquals(5, sq.getEnd());
356 Object newDs = PA.getValue(sq, "datasetSequence");
357 assertNotNull(newDs);
358 assertNotSame(ds, newDs);
359 assertNotNull(sq.getDBRefs());
360 assertEquals(1, sq.getDBRefs().length);
361 assertNotSame(dbr1, sq.getDBRefs()[0]);
362 assertEquals(dbr1, sq.getDBRefs()[0]);
365 * internal delete with sequence features
366 * (failure case for JAL-2541)
368 sq = new Sequence("test", "ABCDEF");
369 sq.createDatasetSequence();
370 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
372 sq.addSequenceFeature(sf1);
373 ds = PA.getValue(sq, "datasetSequence");
375 assertEquals(1, sq.getStart());
376 assertEquals(6, sq.getEnd());
377 sq.deleteChars(2, 4);
378 assertEquals("ABEF", sq.getSequenceAsString());
379 assertEquals(1, sq.getStart());
380 assertEquals(4, sq.getEnd());
381 newDs = PA.getValue(sq, "datasetSequence");
382 assertNotNull(newDs);
383 assertNotSame(ds, newDs);
384 List<SequenceFeature> sfs = sq.getSequenceFeatures();
385 assertEquals(1, sfs.size());
386 assertNotSame(sf1, sfs.get(0));
387 assertEquals(sf1, sfs.get(0));
390 * delete at start - no new dataset sequence created
391 * any sequence features remain as before
393 sq = new Sequence("test", "ABCDEF");
394 sq.createDatasetSequence();
395 ds = PA.getValue(sq, "datasetSequence");
396 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
397 sq.addSequenceFeature(sf1);
398 sq.deleteChars(0, 2);
399 assertEquals("CDEF", sq.getSequenceAsString());
400 assertEquals(3, sq.getStart());
401 assertEquals(6, sq.getEnd());
402 assertSame(ds, PA.getValue(sq, "datasetSequence"));
403 sfs = sq.getSequenceFeatures();
405 assertEquals(1, sfs.size());
406 assertSame(sf1, sfs.get(0));
409 * delete at end - no new dataset sequence created
410 * any dbrefs remain as before
412 sq = new Sequence("test", "ABCDEF");
413 sq.createDatasetSequence();
414 ds = PA.getValue(sq, "datasetSequence");
415 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
417 sq.deleteChars(4, 6);
418 assertEquals("ABCD", sq.getSequenceAsString());
419 assertEquals(1, sq.getStart());
420 assertEquals(4, sq.getEnd());
421 assertSame(ds, PA.getValue(sq, "datasetSequence"));
422 assertNotNull(sq.getDBRefs());
423 assertEquals(1, sq.getDBRefs().length);
424 assertSame(dbr1, sq.getDBRefs()[0]);
427 @Test(groups = { "Functional" })
428 public void testInsertCharAt()
430 // non-static methods:
431 SequenceI sq = new Sequence("test", "ABCDEF");
432 sq.insertCharAt(0, 'z');
433 assertEquals("zABCDEF", sq.getSequenceAsString());
434 sq.insertCharAt(2, 2, 'x');
435 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
437 // for static method see StringUtilsTest
441 * Test the method that returns an array of aligned sequence positions where
442 * the array index is the data sequence position (both base 0).
444 @Test(groups = { "Functional" })
445 public void testGapMap()
447 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
448 sq.createDatasetSequence();
449 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
453 * Test the method that gets sequence features, either from the sequence or
456 @Test(groups = { "Functional" })
457 public void testGetSequenceFeatures()
459 SequenceI sq = new Sequence("test", "GATCAT");
460 sq.createDatasetSequence();
462 assertTrue(sq.getSequenceFeatures().isEmpty());
465 * SequenceFeature on sequence
467 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
468 sq.addSequenceFeature(sf);
469 List<SequenceFeature> sfs = sq.getSequenceFeatures();
470 assertEquals(1, sfs.size());
471 assertSame(sf, sfs.get(0));
474 * SequenceFeature on sequence and dataset sequence; returns that on
477 * Note JAL-2046: spurious: we have no use case for this at the moment.
478 * This test also buggy - as sf2.equals(sf), no new feature is added
480 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
482 sq.getDatasetSequence().addSequenceFeature(sf2);
483 sfs = sq.getSequenceFeatures();
484 assertEquals(1, sfs.size());
485 assertSame(sf, sfs.get(0));
488 * SequenceFeature on dataset sequence only
489 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
491 sq.setSequenceFeatures(null);
492 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
495 * Corrupt case - no SequenceFeature, dataset's dataset is the original
496 * sequence. Test shows no infinite loop results.
498 sq.getDatasetSequence().setSequenceFeatures(null);
500 * is there a usecase for this ? setDatasetSequence should throw an error if
501 * this actually occurs.
505 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
506 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
507 } catch (IllegalArgumentException e)
509 // TODO Jalview error/exception class for raising implementation errors
510 assertTrue(e.getMessage().toLowerCase()
511 .contains("implementation error"));
513 assertTrue(sq.getSequenceFeatures().isEmpty());
517 * Test the method that returns an array, indexed by sequence position, whose
518 * entries are the residue positions at the sequence position (or to the right
521 @Test(groups = { "Functional" })
522 public void testFindPositionMap()
525 * Note: Javadoc for findPosition says it returns the residue position to
526 * the left of a gapped position; in fact it returns the position to the
527 * right. Also it returns a non-existent residue position for a gap beyond
530 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
531 int[] map = sq.findPositionMap();
532 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
533 Arrays.toString(map));
537 * Test for getSubsequence
539 @Test(groups = { "Functional" })
540 public void testGetSubsequence()
542 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
543 sq.createDatasetSequence();
545 // positions are base 0, end position is exclusive
546 SequenceI subseq = sq.getSubSequence(2, 4);
548 assertEquals("CD", subseq.getSequenceAsString());
549 // start/end are base 1 positions
550 assertEquals(3, subseq.getStart());
551 assertEquals(4, subseq.getEnd());
552 // subsequence shares the full dataset sequence
553 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
557 * test createDatasetSequence behaves to doc
559 @Test(groups = { "Functional" })
560 public void testCreateDatasetSequence()
562 SequenceI sq = new Sequence("my", "ASDASD");
563 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
565 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
566 assertNull(sq.getDatasetSequence());
567 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
568 assertNotNull(PA.getValue(sq, "dbrefs"));
570 SequenceI rds = sq.createDatasetSequence();
572 assertNull(rds.getDatasetSequence());
573 assertSame(sq.getDatasetSequence(), rds);
575 // sequence features and dbrefs transferred to dataset sequence
576 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
577 assertNull(PA.getValue(sq, "dbrefs"));
578 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
579 assertNotNull(PA.getValue(rds, "dbrefs"));
583 * Test for deriveSequence applied to a sequence with a dataset
585 @Test(groups = { "Functional" })
586 public void testDeriveSequence_existingDataset()
588 Sequence sq = new Sequence("Seq1", "CD");
589 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
590 sq.getDatasetSequence().addSequenceFeature(
591 new SequenceFeature("", "", 1, 2, 0f, null));
595 sq.setDescription("Test sequence description..");
596 sq.setVamsasId("TestVamsasId");
597 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
599 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
600 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
601 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
602 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
604 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
605 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
606 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
607 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
609 // these are the same as ones already added
610 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
611 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
613 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
616 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
617 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
618 sq.getDatasetSequence().addDBRef(
619 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
620 sq.getDatasetSequence().addDBRef(
621 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
623 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
624 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
625 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
627 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
629 sq.getDatasetSequence().addPDBId(pdbe1a);
630 sq.getDatasetSequence().addPDBId(pdbe1b);
631 sq.getDatasetSequence().addPDBId(pdbe2a);
632 sq.getDatasetSequence().addPDBId(pdbe2b);
635 * test we added pdb entries to the dataset sequence
637 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
638 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
639 "PDB Entries were not found on dataset sequence.");
642 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
644 Assert.assertEquals(pdbe1a,
645 sq.getDatasetSequence().getPDBEntry("1PDB"),
646 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
647 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
648 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
649 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
650 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
651 Annotation[] annots = annotsList.toArray(new Annotation[0]);
652 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
653 "Test annot description", annots));
654 sq.getDatasetSequence().addAlignmentAnnotation(
655 new AlignmentAnnotation("Test annot", "Test annot description",
657 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
658 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
660 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
661 Assert.assertNotNull(sq.getAnnotation());
662 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
663 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
666 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
668 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
670 Sequence derived = (Sequence) sq.deriveSequence();
672 Assert.assertEquals(derived.getDescription(),
673 "Test sequence description..");
674 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
675 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
676 Assert.assertNotNull(derived.getAnnotation());
677 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
678 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
679 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
681 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
683 assertEquals("CD", derived.getSequenceAsString());
684 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
686 // derived sequence should access dataset sequence features
687 assertNotNull(sq.getSequenceFeatures());
688 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
691 * verify we have primary db refs *just* for PDB IDs with associated
695 assertEquals(primRefs, sq.getPrimaryDBRefs());
696 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
698 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
703 * Test for deriveSequence applied to an ungapped sequence with no dataset
705 @Test(groups = { "Functional" })
706 public void testDeriveSequence_noDatasetUngapped()
708 SequenceI sq = new Sequence("Seq1", "ABCDEF");
709 assertEquals(1, sq.getStart());
710 assertEquals(6, sq.getEnd());
711 SequenceI derived = sq.deriveSequence();
712 assertEquals("ABCDEF", derived.getSequenceAsString());
713 assertEquals("ABCDEF", derived.getDatasetSequence()
714 .getSequenceAsString());
718 * Test for deriveSequence applied to a gapped sequence with no dataset
720 @Test(groups = { "Functional" })
721 public void testDeriveSequence_noDatasetGapped()
723 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
724 assertEquals(1, sq.getStart());
725 assertEquals(6, sq.getEnd());
726 assertNull(sq.getDatasetSequence());
727 SequenceI derived = sq.deriveSequence();
728 assertEquals("AB-C.D EF", derived.getSequenceAsString());
729 assertEquals("ABCDEF", derived.getDatasetSequence()
730 .getSequenceAsString());
733 @Test(groups = { "Functional" })
734 public void testCopyConstructor_noDataset()
736 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
737 seq1.setDescription("description");
738 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
740 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
742 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
743 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
745 SequenceI copy = new Sequence(seq1);
747 assertNull(copy.getDatasetSequence());
749 verifyCopiedSequence(seq1, copy);
751 // copy has a copy of the DBRefEntry
752 // this is murky - DBrefs are only copied for dataset sequences
753 // where the test for 'dataset sequence' is 'dataset is null'
754 // but that doesn't distinguish it from an aligned sequence
755 // which has not yet generated a dataset sequence
756 // NB getDBRef looks inside dataset sequence if not null
757 DBRefEntry[] dbrefs = copy.getDBRefs();
758 assertEquals(1, dbrefs.length);
759 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
760 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
763 @Test(groups = { "Functional" })
764 public void testCopyConstructor_withDataset()
766 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
767 seq1.createDatasetSequence();
768 seq1.setDescription("description");
769 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
771 // JAL-2046 - what is the contract for using a derived sequence's
772 // addSequenceFeature ?
773 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
775 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
776 // here we add DBRef to the dataset sequence:
777 seq1.getDatasetSequence().addDBRef(
778 new DBRefEntry("EMBL", "1.2", "AZ12345"));
780 SequenceI copy = new Sequence(seq1);
782 assertNotNull(copy.getDatasetSequence());
783 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
785 verifyCopiedSequence(seq1, copy);
787 // getDBRef looks inside dataset sequence and this is shared,
788 // so holds the same dbref objects
789 DBRefEntry[] dbrefs = copy.getDBRefs();
790 assertEquals(1, dbrefs.length);
791 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
795 * Helper to make assertions about a copied sequence
800 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
802 // verify basic properties:
803 assertEquals(copy.getName(), seq1.getName());
804 assertEquals(copy.getDescription(), seq1.getDescription());
805 assertEquals(copy.getStart(), seq1.getStart());
806 assertEquals(copy.getEnd(), seq1.getEnd());
807 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
809 // copy has a copy of the annotation:
810 AlignmentAnnotation[] anns = copy.getAnnotation();
811 assertEquals(1, anns.length);
812 assertFalse(anns[0] == seq1.getAnnotation()[0]);
813 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
814 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
815 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
817 // copy has a copy of the sequence feature:
818 List<SequenceFeature> sfs = copy.getSequenceFeatures();
819 assertEquals(1, sfs.size());
820 if (seq1.getDatasetSequence() != null
821 && copy.getDatasetSequence() == seq1.getDatasetSequence())
823 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
827 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
829 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
831 // copy has a copy of the PDB entry
832 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
833 assertEquals(1, pdbs.size());
834 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
835 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
838 @Test(groups = "Functional")
839 public void testGetCharAt()
841 SequenceI sq = new Sequence("", "abcde");
842 assertEquals('a', sq.getCharAt(0));
843 assertEquals('e', sq.getCharAt(4));
844 assertEquals(' ', sq.getCharAt(5));
845 assertEquals(' ', sq.getCharAt(-1));
848 @Test(groups = { "Functional" })
849 public void testAddSequenceFeatures()
851 SequenceI sq = new Sequence("", "abcde");
852 // type may not be null
853 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
855 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
857 // can't add a duplicate feature
858 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
860 // can add a different feature
861 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
862 8, 0f, null))); // different type
863 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
864 "description", 4, 8, 0f, null)));// different description
865 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
866 8, 0f, null))); // different start position
867 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
868 9, 0f, null))); // different end position
869 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
870 8, 1f, null))); // different score
871 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
872 8, Float.NaN, null))); // score NaN
873 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
874 8, 0f, "Metal"))); // different group
875 assertEquals(8, sq.getFeatures().getAllFeatures().size());
879 * Tests for adding (or updating) dbrefs
881 * @see DBRefEntry#updateFrom(DBRefEntry)
883 @Test(groups = { "Functional" })
884 public void testAddDBRef()
886 SequenceI sq = new Sequence("", "abcde");
887 assertNull(sq.getDBRefs());
888 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
890 assertEquals(1, sq.getDBRefs().length);
891 assertSame(dbref, sq.getDBRefs()[0]);
894 * change of version - new entry
896 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
898 assertEquals(2, sq.getDBRefs().length);
899 assertSame(dbref, sq.getDBRefs()[0]);
900 assertSame(dbref2, sq.getDBRefs()[1]);
903 * matches existing entry - not added
905 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
906 assertEquals(2, sq.getDBRefs().length);
909 * different source = new entry
911 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
913 assertEquals(3, sq.getDBRefs().length);
914 assertSame(dbref3, sq.getDBRefs()[2]);
917 * different ref = new entry
919 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
921 assertEquals(4, sq.getDBRefs().length);
922 assertSame(dbref4, sq.getDBRefs()[3]);
925 * matching ref with a mapping - map updated
927 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
928 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
932 assertEquals(4, sq.getDBRefs().length);
933 assertSame(dbref4, sq.getDBRefs()[3]);
934 assertSame(map, dbref4.getMap());
937 * 'real' version replaces "0" version
939 dbref2.setVersion("0");
940 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
941 dbref2.getAccessionId());
943 assertEquals(4, sq.getDBRefs().length);
944 assertSame(dbref2, sq.getDBRefs()[1]);
945 assertEquals("3", dbref2.getVersion());
948 * 'real' version replaces "source:0" version
950 dbref3.setVersion("Uniprot:0");
951 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
952 dbref3.getAccessionId());
954 assertEquals(4, sq.getDBRefs().length);
955 assertSame(dbref3, sq.getDBRefs()[2]);
956 assertEquals("3", dbref2.getVersion());
959 @Test(groups = { "Functional" })
960 public void testGetPrimaryDBRefs_peptide()
962 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
965 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
966 assertTrue(primaryDBRefs.isEmpty());
969 sq.setDBRefs(new DBRefEntry[] {});
970 primaryDBRefs = sq.getPrimaryDBRefs();
971 assertTrue(primaryDBRefs.isEmpty());
974 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
975 sq.addDBRef(upentry1);
977 // primary - uniprot with congruent map
978 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
979 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
980 new int[] { 10, 22 }, 1, 1)));
981 sq.addDBRef(upentry2);
983 // primary - uniprot with map of enclosing sequence
984 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
985 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
986 new int[] { 8, 24 }, 1, 1)));
987 sq.addDBRef(upentry3);
989 // not primary - uniprot with map of sub-sequence (5')
990 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
991 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
992 new int[] { 10, 18 }, 1, 1)));
993 sq.addDBRef(upentry4);
995 // not primary - uniprot with map that overlaps 3'
996 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
997 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
998 new int[] { 12, 22 }, 1, 1)));
999 sq.addDBRef(upentry5);
1001 // not primary - uniprot with map to different coordinates frame
1002 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1003 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1004 new int[] { 112, 118 }, 1, 1)));
1005 sq.addDBRef(upentry6);
1007 // not primary - dbref to 'non-core' database
1008 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1009 sq.addDBRef(upentry7);
1011 // primary - type is PDB
1012 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1013 sq.addDBRef(pdbentry);
1015 // not primary - PDBEntry has no file
1016 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1018 // not primary - no PDBEntry
1019 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1021 // add corroborating PDB entry for primary DBref -
1022 // needs to have a file as well as matching ID
1023 // note PDB ID is not treated as case sensitive
1024 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1027 // not valid DBRef - no file..
1028 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1030 primaryDBRefs = sq.getPrimaryDBRefs();
1031 assertEquals(4, primaryDBRefs.size());
1032 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1033 primaryDBRefs.contains(upentry1));
1034 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1035 primaryDBRefs.contains(upentry2));
1036 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1037 primaryDBRefs.contains(upentry3));
1038 assertTrue("Couldn't find expected PDB primary reference",
1039 primaryDBRefs.contains(pdbentry));
1042 @Test(groups = { "Functional" })
1043 public void testGetPrimaryDBRefs_nucleotide()
1045 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1047 // primary - Ensembl
1048 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1051 // not primary - Ensembl 'transcript' mapping of sub-sequence
1052 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1053 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1054 new int[] { 1, 11 }, 1, 1)));
1057 // primary - EMBL with congruent map
1058 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1059 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1060 new int[] { 10, 34 }, 1, 1)));
1063 // not primary - to non-core database
1064 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1067 // not primary - to protein
1068 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1071 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1072 assertEquals(2, primaryDBRefs.size());
1073 assertTrue(primaryDBRefs.contains(dbr1));
1074 assertTrue(primaryDBRefs.contains(dbr3));
1078 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1081 @Test(groups = { "Functional" })
1082 public void testUpdatePDBIds()
1084 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1085 seq.addPDBId(pdbe1);
1086 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1087 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1088 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1089 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1090 // 7 is not a valid chain code:
1091 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1094 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1095 assertEquals(4, pdbIds.size());
1096 assertSame(pdbe1, pdbIds.get(0));
1097 // chain code got added to 3A6S:
1098 assertEquals("B", pdbe1.getChainCode());
1099 assertEquals("1A70", pdbIds.get(1).getId());
1100 // 4BQGA is parsed into id + chain
1101 assertEquals("4BQG", pdbIds.get(2).getId());
1102 assertEquals("a", pdbIds.get(2).getChainCode());
1103 assertEquals("2GIS7", pdbIds.get(3).getId());
1104 assertNull(pdbIds.get(3).getChainCode());
1108 * Test the method that either adds a pdbid or updates an existing one
1110 @Test(groups = { "Functional" })
1111 public void testAddPDBId()
1113 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1115 assertEquals(1, seq.getAllPDBEntries().size());
1116 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1117 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1119 // add the same entry
1121 assertEquals(1, seq.getAllPDBEntries().size());
1122 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1124 // add an identical entry
1125 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1126 assertEquals(1, seq.getAllPDBEntries().size());
1127 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1129 // add a different entry
1130 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1131 seq.addPDBId(pdbe2);
1132 assertEquals(2, seq.getAllPDBEntries().size());
1133 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1134 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1136 // update pdbe with chain code, file, type
1137 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1138 seq.addPDBId(pdbe3);
1139 assertEquals(2, seq.getAllPDBEntries().size());
1140 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1141 assertEquals("3A6S", pdbe.getId()); // unchanged
1142 assertEquals("A", pdbe.getChainCode()); // updated
1143 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1144 assertEquals("filepath", pdbe.getFile()); // updated
1145 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1147 // add with a different file path
1148 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1149 seq.addPDBId(pdbe4);
1150 assertEquals(3, seq.getAllPDBEntries().size());
1151 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1153 // add with a different chain code
1154 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1155 seq.addPDBId(pdbe5);
1156 assertEquals(4, seq.getAllPDBEntries().size());
1157 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1161 groups = { "Functional" },
1162 expectedExceptions = { IllegalArgumentException.class })
1163 public void testSetDatasetSequence_toSelf()
1165 seq.setDatasetSequence(seq);
1169 groups = { "Functional" },
1170 expectedExceptions = { IllegalArgumentException.class })
1171 public void testSetDatasetSequence_cascading()
1173 SequenceI seq2 = new Sequence("Seq2", "xyz");
1174 seq2.createDatasetSequence();
1175 seq.setDatasetSequence(seq2);
1179 public void testFindPositions()
1181 SequenceI sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1183 Range range = sq.findPositions(1, 4); // BC
1184 assertEquals(new Range(9, 10), range);
1186 range = sq.findPositions(2, 4); // C
1187 assertEquals(new Range(10, 10), range);
1189 assertNull(sq.findPositions(3, 4)); // all gaps
1191 range = sq.findPositions(2, 6); // CDE
1192 assertEquals(new Range(10, 12), range);
1194 range = sq.findPositions(3, 7); // DE
1195 assertEquals(new Range(11, 12), range);