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.commands.EditCommand;
32 import jalview.commands.EditCommand.Action;
33 import jalview.datamodel.PDBEntry.Type;
34 import jalview.gui.JvOptionPane;
35 import jalview.util.MapList;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Vector;
43 import junit.extensions.PA;
45 import org.testng.Assert;
46 import org.testng.annotations.BeforeClass;
47 import org.testng.annotations.BeforeMethod;
48 import org.testng.annotations.Test;
50 public class SequenceTest
53 @BeforeClass(alwaysRun = true)
54 public void setUpJvOptionPane()
56 JvOptionPane.setInteractiveMode(false);
57 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62 @BeforeMethod(alwaysRun = true)
65 seq = new Sequence("FER1", "AKPNGVL");
68 @Test(groups = { "Functional" })
69 public void testInsertGapsAndGapmaps()
71 SequenceI aseq = seq.deriveSequence();
72 aseq.insertCharAt(2, 3, '-');
73 aseq.insertCharAt(6, 3, '-');
74 assertEquals("Gap insertions not correct", "AK---P---NGVL",
75 aseq.getSequenceAsString());
76 List<int[]> gapInt = aseq.getInsertions();
77 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
78 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
79 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
80 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
83 @Test(groups = ("Functional"))
84 public void testIsProtein()
87 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
89 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
91 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
92 assertFalse(sq.isProtein());
93 // change sequence, should trigger an update of cached result
94 sq.setSequence("ASDFASDFADSF");
95 assertTrue(sq.isProtein());
97 * in situ change of sequence doesn't change hashcode :-O
98 * (sequence should not expose internal implementation)
100 for (int i = 0; i < sq.getSequence().length; i++)
102 sq.getSequence()[i] = "acgtu".charAt(i % 5);
104 assertTrue(sq.isProtein()); // but it isn't
107 @Test(groups = { "Functional" })
108 public void testGetAnnotation()
110 // initial state returns null not an empty array
111 assertNull(seq.getAnnotation());
112 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
114 AlignmentAnnotation[] anns = seq.getAnnotation();
115 assertEquals(1, anns.length);
116 assertSame(ann, anns[0]);
118 // removing all annotations reverts array to null
119 seq.removeAlignmentAnnotation(ann);
120 assertNull(seq.getAnnotation());
123 @Test(groups = { "Functional" })
124 public void testGetAnnotation_forLabel()
126 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
128 addAnnotation("label2", "desc2", "calcId2", 1f);
129 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
131 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
132 assertEquals(2, anns.length);
133 assertSame(ann1, anns[0]);
134 assertSame(ann3, anns[1]);
137 private AlignmentAnnotation addAnnotation(String label,
138 String description, String calcId, float value)
140 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
142 annotation.setCalcId(calcId);
143 seq.addAlignmentAnnotation(annotation);
147 @Test(groups = { "Functional" })
148 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
150 addAnnotation("label1", "desc1", "calcId1", 1f);
151 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
153 addAnnotation("label2", "desc3", "calcId3", 1f);
154 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
156 addAnnotation("label5", "desc3", null, 1f);
157 addAnnotation(null, "desc3", "calcId3", 1f);
159 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
161 assertEquals(2, anns.size());
162 assertSame(ann2, anns.get(0));
163 assertSame(ann4, anns.get(1));
165 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
166 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
167 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
168 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
169 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
173 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
174 * setting the sequenceRef on the annotation. Adding the same annotation twice
177 @Test(groups = { "Functional" })
178 public void testAddAlignmentAnnotation()
180 assertNull(seq.getAnnotation());
181 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
183 assertNull(annotation.sequenceRef);
184 seq.addAlignmentAnnotation(annotation);
185 assertSame(seq, annotation.sequenceRef);
186 AlignmentAnnotation[] anns = seq.getAnnotation();
187 assertEquals(1, anns.length);
188 assertSame(annotation, anns[0]);
190 // re-adding does nothing
191 seq.addAlignmentAnnotation(annotation);
192 anns = seq.getAnnotation();
193 assertEquals(1, anns.length);
194 assertSame(annotation, anns[0]);
196 // an identical but different annotation can be added
197 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
199 seq.addAlignmentAnnotation(annotation2);
200 anns = seq.getAnnotation();
201 assertEquals(2, anns.length);
202 assertSame(annotation, anns[0]);
203 assertSame(annotation2, anns[1]);
206 @Test(groups = { "Functional" })
207 public void testGetStartGetEnd()
209 SequenceI sq = new Sequence("test", "ABCDEF");
210 assertEquals(1, sq.getStart());
211 assertEquals(6, sq.getEnd());
213 sq = new Sequence("test", "--AB-C-DEF--");
214 assertEquals(1, sq.getStart());
215 assertEquals(6, sq.getEnd());
217 sq = new Sequence("test", "----");
218 assertEquals(1, sq.getStart());
219 assertEquals(0, sq.getEnd()); // ??
223 * Tests for the method that returns an alignment column position (base 1) for
224 * a given sequence position (base 1).
226 @Test(groups = { "Functional" })
227 public void testFindIndex()
230 * call sequenceChanged() after each test to invalidate any cursor,
231 * forcing the 1-arg findIndex to be executed
233 SequenceI sq = new Sequence("test", "ABCDEF");
234 assertEquals(0, sq.findIndex(0));
235 sq.sequenceChanged();
236 assertEquals(1, sq.findIndex(1));
237 sq.sequenceChanged();
238 assertEquals(5, sq.findIndex(5));
239 sq.sequenceChanged();
240 assertEquals(6, sq.findIndex(6));
241 sq.sequenceChanged();
242 assertEquals(6, sq.findIndex(9));
244 sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
245 assertEquals(2, sq.findIndex(8));
246 sq.sequenceChanged();
247 assertEquals(5, sq.findIndex(9));
248 sq.sequenceChanged();
249 assertEquals(7, sq.findIndex(10));
251 // before start returns 0
252 sq.sequenceChanged();
253 assertEquals(0, sq.findIndex(0));
254 sq.sequenceChanged();
255 assertEquals(0, sq.findIndex(-1));
257 // beyond end returns last residue column
258 sq.sequenceChanged();
259 assertEquals(13, sq.findIndex(99));
263 * Tests for the method that returns a dataset sequence position (start..) for
264 * an aligned column position (base 0).
266 @Test(groups = { "Functional" })
267 public void testFindPosition()
270 * call sequenceChanged() after each test to invalidate any cursor,
271 * forcing the 1-arg findPosition to be executed
273 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
274 assertEquals(8, sq.findPosition(0));
275 // Sequence should now hold a cursor at [8, 0]
276 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
277 int token = (int) PA.getValue(sq, "changeCount");
278 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
280 sq.sequenceChanged();
283 * find F13 at column offset 5, cursor should update to [13, 6]
285 assertEquals(13, sq.findPosition(5));
286 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
287 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
288 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
290 // assertEquals(-1, seq.findPosition(6)); // fails
292 sq = new Sequence("test/8-11", "AB-C-D--");
293 token = (int) PA.getValue(sq, "changeCount"); // 0
294 assertEquals(8, sq.findPosition(0));
295 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
296 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
298 sq.sequenceChanged();
299 assertEquals(9, sq.findPosition(1));
300 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
301 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
303 sq.sequenceChanged();
304 // gap position 'finds' residue to the right (not the left as per javadoc)
305 // cursor is set to the last residue position found [B 2]
306 assertEquals(10, sq.findPosition(2));
307 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
308 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
310 sq.sequenceChanged();
311 assertEquals(10, sq.findPosition(3));
312 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
313 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
315 sq.sequenceChanged();
316 // column[4] is the gap after C - returns D11
317 // cursor is set to [C 4]
318 assertEquals(11, sq.findPosition(4));
319 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
320 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
322 sq.sequenceChanged();
323 assertEquals(11, sq.findPosition(5)); // D
324 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
325 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
327 sq.sequenceChanged();
328 // returns 1 more than sequence length if off the end ?!?
329 assertEquals(12, sq.findPosition(6));
331 sq.sequenceChanged();
332 assertEquals(12, sq.findPosition(7));
334 sq = new Sequence("test/8-13", "--AB-C-DEF--");
335 assertEquals(8, sq.findPosition(0));
337 sq.sequenceChanged();
338 assertEquals(8, sq.findPosition(1));
340 sq.sequenceChanged();
341 assertEquals(8, sq.findPosition(2));
343 sq.sequenceChanged();
344 assertEquals(9, sq.findPosition(3));
346 sq.sequenceChanged();
347 assertEquals(10, sq.findPosition(4));
349 sq.sequenceChanged();
350 assertEquals(10, sq.findPosition(5));
352 sq.sequenceChanged();
353 assertEquals(11, sq.findPosition(6));
355 sq.sequenceChanged();
356 assertEquals(11, sq.findPosition(7));
358 sq.sequenceChanged();
359 assertEquals(12, sq.findPosition(8));
361 sq.sequenceChanged();
362 assertEquals(13, sq.findPosition(9));
364 sq.sequenceChanged();
365 assertEquals(14, sq.findPosition(10));
368 * findPosition for column beyond sequence length
369 * returns 1 more than last residue position
371 sq.sequenceChanged();
372 assertEquals(14, sq.findPosition(11));
373 sq.sequenceChanged();
374 assertEquals(14, sq.findPosition(99));
377 @Test(groups = { "Functional" })
378 public void testDeleteChars()
383 SequenceI sq = new Sequence("test", "ABCDEF");
384 assertNull(PA.getValue(sq, "datasetSequence"));
385 assertEquals(1, sq.getStart());
386 assertEquals(6, sq.getEnd());
387 sq.deleteChars(2, 3);
388 assertEquals("ABDEF", sq.getSequenceAsString());
389 assertEquals(1, sq.getStart());
390 assertEquals(5, sq.getEnd());
391 assertNull(PA.getValue(sq, "datasetSequence"));
396 sq = new Sequence("test", "ABCDEF");
397 sq.deleteChars(0, 2);
398 assertEquals("CDEF", sq.getSequenceAsString());
399 assertEquals(3, sq.getStart());
400 assertEquals(6, sq.getEnd());
401 assertNull(PA.getValue(sq, "datasetSequence"));
406 sq = new Sequence("test", "ABCDEF");
407 sq.deleteChars(4, 6);
408 assertEquals("ABCD", sq.getSequenceAsString());
409 assertEquals(1, sq.getStart());
410 assertEquals(4, sq.getEnd());
411 assertNull(PA.getValue(sq, "datasetSequence"));
414 @Test(groups = { "Functional" })
415 public void testDeleteChars_withDbRefsAndFeatures()
418 * internal delete - new dataset sequence created
419 * gets a copy of any dbrefs
421 SequenceI sq = new Sequence("test", "ABCDEF");
422 sq.createDatasetSequence();
423 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
425 Object ds = PA.getValue(sq, "datasetSequence");
427 assertEquals(1, sq.getStart());
428 assertEquals(6, sq.getEnd());
429 sq.deleteChars(2, 3);
430 assertEquals("ABDEF", sq.getSequenceAsString());
431 assertEquals(1, sq.getStart());
432 assertEquals(5, sq.getEnd());
433 Object newDs = PA.getValue(sq, "datasetSequence");
434 assertNotNull(newDs);
435 assertNotSame(ds, newDs);
436 assertNotNull(sq.getDBRefs());
437 assertEquals(1, sq.getDBRefs().length);
438 assertNotSame(dbr1, sq.getDBRefs()[0]);
439 assertEquals(dbr1, sq.getDBRefs()[0]);
442 * internal delete with sequence features
443 * (failure case for JAL-2541)
445 sq = new Sequence("test", "ABCDEF");
446 sq.createDatasetSequence();
447 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
449 sq.addSequenceFeature(sf1);
450 ds = PA.getValue(sq, "datasetSequence");
452 assertEquals(1, sq.getStart());
453 assertEquals(6, sq.getEnd());
454 sq.deleteChars(2, 4);
455 assertEquals("ABEF", sq.getSequenceAsString());
456 assertEquals(1, sq.getStart());
457 assertEquals(4, sq.getEnd());
458 newDs = PA.getValue(sq, "datasetSequence");
459 assertNotNull(newDs);
460 assertNotSame(ds, newDs);
461 List<SequenceFeature> sfs = sq.getSequenceFeatures();
462 assertEquals(1, sfs.size());
463 assertNotSame(sf1, sfs.get(0));
464 assertEquals(sf1, sfs.get(0));
467 * delete at start - no new dataset sequence created
468 * any sequence features remain as before
470 sq = new Sequence("test", "ABCDEF");
471 sq.createDatasetSequence();
472 ds = PA.getValue(sq, "datasetSequence");
473 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
474 sq.addSequenceFeature(sf1);
475 sq.deleteChars(0, 2);
476 assertEquals("CDEF", sq.getSequenceAsString());
477 assertEquals(3, sq.getStart());
478 assertEquals(6, sq.getEnd());
479 assertSame(ds, PA.getValue(sq, "datasetSequence"));
480 sfs = sq.getSequenceFeatures();
482 assertEquals(1, sfs.size());
483 assertSame(sf1, sfs.get(0));
486 * delete at end - no new dataset sequence created
487 * any dbrefs remain as before
489 sq = new Sequence("test", "ABCDEF");
490 sq.createDatasetSequence();
491 ds = PA.getValue(sq, "datasetSequence");
492 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
494 sq.deleteChars(4, 6);
495 assertEquals("ABCD", sq.getSequenceAsString());
496 assertEquals(1, sq.getStart());
497 assertEquals(4, sq.getEnd());
498 assertSame(ds, PA.getValue(sq, "datasetSequence"));
499 assertNotNull(sq.getDBRefs());
500 assertEquals(1, sq.getDBRefs().length);
501 assertSame(dbr1, sq.getDBRefs()[0]);
504 @Test(groups = { "Functional" })
505 public void testInsertCharAt()
507 // non-static methods:
508 SequenceI sq = new Sequence("test", "ABCDEF");
509 sq.insertCharAt(0, 'z');
510 assertEquals("zABCDEF", sq.getSequenceAsString());
511 sq.insertCharAt(2, 2, 'x');
512 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
514 // for static method see StringUtilsTest
518 * Test the method that returns an array of aligned sequence positions where
519 * the array index is the data sequence position (both base 0).
521 @Test(groups = { "Functional" })
522 public void testGapMap()
524 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
525 sq.createDatasetSequence();
526 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
530 * Test the method that gets sequence features, either from the sequence or
533 @Test(groups = { "Functional" })
534 public void testGetSequenceFeatures()
536 SequenceI sq = new Sequence("test", "GATCAT");
537 sq.createDatasetSequence();
539 assertTrue(sq.getSequenceFeatures().isEmpty());
542 * SequenceFeature on sequence
544 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
545 sq.addSequenceFeature(sf);
546 List<SequenceFeature> sfs = sq.getSequenceFeatures();
547 assertEquals(1, sfs.size());
548 assertSame(sf, sfs.get(0));
551 * SequenceFeature on sequence and dataset sequence; returns that on
554 * Note JAL-2046: spurious: we have no use case for this at the moment.
555 * This test also buggy - as sf2.equals(sf), no new feature is added
557 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
559 sq.getDatasetSequence().addSequenceFeature(sf2);
560 sfs = sq.getSequenceFeatures();
561 assertEquals(1, sfs.size());
562 assertSame(sf, sfs.get(0));
565 * SequenceFeature on dataset sequence only
566 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
568 sq.setSequenceFeatures(null);
569 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
572 * Corrupt case - no SequenceFeature, dataset's dataset is the original
573 * sequence. Test shows no infinite loop results.
575 sq.getDatasetSequence().setSequenceFeatures(null);
577 * is there a usecase for this ? setDatasetSequence should throw an error if
578 * this actually occurs.
582 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
583 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
584 } catch (IllegalArgumentException e)
586 // TODO Jalview error/exception class for raising implementation errors
587 assertTrue(e.getMessage().toLowerCase()
588 .contains("implementation error"));
590 assertTrue(sq.getSequenceFeatures().isEmpty());
594 * Test the method that returns an array, indexed by sequence position, whose
595 * entries are the residue positions at the sequence position (or to the right
598 @Test(groups = { "Functional" })
599 public void testFindPositionMap()
602 * Note: Javadoc for findPosition says it returns the residue position to
603 * the left of a gapped position; in fact it returns the position to the
604 * right. Also it returns a non-existent residue position for a gap beyond
607 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
608 int[] map = sq.findPositionMap();
609 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
610 Arrays.toString(map));
614 * Test for getSubsequence
616 @Test(groups = { "Functional" })
617 public void testGetSubsequence()
619 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
620 sq.createDatasetSequence();
622 // positions are base 0, end position is exclusive
623 SequenceI subseq = sq.getSubSequence(2, 4);
625 assertEquals("CD", subseq.getSequenceAsString());
626 // start/end are base 1 positions
627 assertEquals(3, subseq.getStart());
628 assertEquals(4, subseq.getEnd());
629 // subsequence shares the full dataset sequence
630 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
634 * test createDatasetSequence behaves to doc
636 @Test(groups = { "Functional" })
637 public void testCreateDatasetSequence()
639 SequenceI sq = new Sequence("my", "ASDASD");
640 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
642 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
643 assertNull(sq.getDatasetSequence());
644 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
645 assertNotNull(PA.getValue(sq, "dbrefs"));
647 SequenceI rds = sq.createDatasetSequence();
649 assertNull(rds.getDatasetSequence());
650 assertSame(sq.getDatasetSequence(), rds);
652 // sequence features and dbrefs transferred to dataset sequence
653 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
654 assertNull(PA.getValue(sq, "dbrefs"));
655 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
656 assertNotNull(PA.getValue(rds, "dbrefs"));
660 * Test for deriveSequence applied to a sequence with a dataset
662 @Test(groups = { "Functional" })
663 public void testDeriveSequence_existingDataset()
665 Sequence sq = new Sequence("Seq1", "CD");
666 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
667 sq.getDatasetSequence().addSequenceFeature(
668 new SequenceFeature("", "", 1, 2, 0f, null));
672 sq.setDescription("Test sequence description..");
673 sq.setVamsasId("TestVamsasId");
674 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
676 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
677 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
678 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
679 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
681 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
682 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
683 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
684 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
686 // these are the same as ones already added
687 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
688 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
690 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
693 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
694 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
695 sq.getDatasetSequence().addDBRef(
696 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
697 sq.getDatasetSequence().addDBRef(
698 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
700 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
701 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
702 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
704 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
706 sq.getDatasetSequence().addPDBId(pdbe1a);
707 sq.getDatasetSequence().addPDBId(pdbe1b);
708 sq.getDatasetSequence().addPDBId(pdbe2a);
709 sq.getDatasetSequence().addPDBId(pdbe2b);
712 * test we added pdb entries to the dataset sequence
714 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
715 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
716 "PDB Entries were not found on dataset sequence.");
719 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
721 Assert.assertEquals(pdbe1a,
722 sq.getDatasetSequence().getPDBEntry("1PDB"),
723 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
724 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
725 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
726 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
727 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
728 Annotation[] annots = annotsList.toArray(new Annotation[0]);
729 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
730 "Test annot description", annots));
731 sq.getDatasetSequence().addAlignmentAnnotation(
732 new AlignmentAnnotation("Test annot", "Test annot description",
734 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
735 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
737 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
738 Assert.assertNotNull(sq.getAnnotation());
739 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
740 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
743 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
745 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
747 Sequence derived = (Sequence) sq.deriveSequence();
749 Assert.assertEquals(derived.getDescription(),
750 "Test sequence description..");
751 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
752 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
753 Assert.assertNotNull(derived.getAnnotation());
754 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
755 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
756 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
758 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
760 assertEquals("CD", derived.getSequenceAsString());
761 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
763 // derived sequence should access dataset sequence features
764 assertNotNull(sq.getSequenceFeatures());
765 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
768 * verify we have primary db refs *just* for PDB IDs with associated
772 assertEquals(primRefs, sq.getPrimaryDBRefs());
773 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
775 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
780 * Test for deriveSequence applied to an ungapped sequence with no dataset
782 @Test(groups = { "Functional" })
783 public void testDeriveSequence_noDatasetUngapped()
785 SequenceI sq = new Sequence("Seq1", "ABCDEF");
786 assertEquals(1, sq.getStart());
787 assertEquals(6, sq.getEnd());
788 SequenceI derived = sq.deriveSequence();
789 assertEquals("ABCDEF", derived.getSequenceAsString());
790 assertEquals("ABCDEF", derived.getDatasetSequence()
791 .getSequenceAsString());
795 * Test for deriveSequence applied to a gapped sequence with no dataset
797 @Test(groups = { "Functional" })
798 public void testDeriveSequence_noDatasetGapped()
800 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
801 assertEquals(1, sq.getStart());
802 assertEquals(6, sq.getEnd());
803 assertNull(sq.getDatasetSequence());
804 SequenceI derived = sq.deriveSequence();
805 assertEquals("AB-C.D EF", derived.getSequenceAsString());
806 assertEquals("ABCDEF", derived.getDatasetSequence()
807 .getSequenceAsString());
810 @Test(groups = { "Functional" })
811 public void testCopyConstructor_noDataset()
813 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
814 seq1.setDescription("description");
815 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
817 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
819 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
820 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
822 SequenceI copy = new Sequence(seq1);
824 assertNull(copy.getDatasetSequence());
826 verifyCopiedSequence(seq1, copy);
828 // copy has a copy of the DBRefEntry
829 // this is murky - DBrefs are only copied for dataset sequences
830 // where the test for 'dataset sequence' is 'dataset is null'
831 // but that doesn't distinguish it from an aligned sequence
832 // which has not yet generated a dataset sequence
833 // NB getDBRef looks inside dataset sequence if not null
834 DBRefEntry[] dbrefs = copy.getDBRefs();
835 assertEquals(1, dbrefs.length);
836 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
837 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
840 @Test(groups = { "Functional" })
841 public void testCopyConstructor_withDataset()
843 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
844 seq1.createDatasetSequence();
845 seq1.setDescription("description");
846 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
848 // JAL-2046 - what is the contract for using a derived sequence's
849 // addSequenceFeature ?
850 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
852 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
853 // here we add DBRef to the dataset sequence:
854 seq1.getDatasetSequence().addDBRef(
855 new DBRefEntry("EMBL", "1.2", "AZ12345"));
857 SequenceI copy = new Sequence(seq1);
859 assertNotNull(copy.getDatasetSequence());
860 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
862 verifyCopiedSequence(seq1, copy);
864 // getDBRef looks inside dataset sequence and this is shared,
865 // so holds the same dbref objects
866 DBRefEntry[] dbrefs = copy.getDBRefs();
867 assertEquals(1, dbrefs.length);
868 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
872 * Helper to make assertions about a copied sequence
877 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
879 // verify basic properties:
880 assertEquals(copy.getName(), seq1.getName());
881 assertEquals(copy.getDescription(), seq1.getDescription());
882 assertEquals(copy.getStart(), seq1.getStart());
883 assertEquals(copy.getEnd(), seq1.getEnd());
884 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
886 // copy has a copy of the annotation:
887 AlignmentAnnotation[] anns = copy.getAnnotation();
888 assertEquals(1, anns.length);
889 assertFalse(anns[0] == seq1.getAnnotation()[0]);
890 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
891 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
892 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
894 // copy has a copy of the sequence feature:
895 List<SequenceFeature> sfs = copy.getSequenceFeatures();
896 assertEquals(1, sfs.size());
897 if (seq1.getDatasetSequence() != null
898 && copy.getDatasetSequence() == seq1.getDatasetSequence())
900 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
904 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
906 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
908 // copy has a copy of the PDB entry
909 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
910 assertEquals(1, pdbs.size());
911 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
912 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
915 @Test(groups = "Functional")
916 public void testGetCharAt()
918 SequenceI sq = new Sequence("", "abcde");
919 assertEquals('a', sq.getCharAt(0));
920 assertEquals('e', sq.getCharAt(4));
921 assertEquals(' ', sq.getCharAt(5));
922 assertEquals(' ', sq.getCharAt(-1));
925 @Test(groups = { "Functional" })
926 public void testAddSequenceFeatures()
928 SequenceI sq = new Sequence("", "abcde");
929 // type may not be null
930 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
932 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
934 // can't add a duplicate feature
935 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
937 // can add a different feature
938 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
939 8, 0f, null))); // different type
940 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
941 "description", 4, 8, 0f, null)));// different description
942 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
943 8, 0f, null))); // different start position
944 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
945 9, 0f, null))); // different end position
946 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
947 8, 1f, null))); // different score
948 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
949 8, Float.NaN, null))); // score NaN
950 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
951 8, 0f, "Metal"))); // different group
952 assertEquals(8, sq.getFeatures().getAllFeatures().size());
956 * Tests for adding (or updating) dbrefs
958 * @see DBRefEntry#updateFrom(DBRefEntry)
960 @Test(groups = { "Functional" })
961 public void testAddDBRef()
963 SequenceI sq = new Sequence("", "abcde");
964 assertNull(sq.getDBRefs());
965 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
967 assertEquals(1, sq.getDBRefs().length);
968 assertSame(dbref, sq.getDBRefs()[0]);
971 * change of version - new entry
973 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
975 assertEquals(2, sq.getDBRefs().length);
976 assertSame(dbref, sq.getDBRefs()[0]);
977 assertSame(dbref2, sq.getDBRefs()[1]);
980 * matches existing entry - not added
982 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
983 assertEquals(2, sq.getDBRefs().length);
986 * different source = new entry
988 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
990 assertEquals(3, sq.getDBRefs().length);
991 assertSame(dbref3, sq.getDBRefs()[2]);
994 * different ref = new entry
996 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
998 assertEquals(4, sq.getDBRefs().length);
999 assertSame(dbref4, sq.getDBRefs()[3]);
1002 * matching ref with a mapping - map updated
1004 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1005 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1008 sq.addDBRef(dbref5);
1009 assertEquals(4, sq.getDBRefs().length);
1010 assertSame(dbref4, sq.getDBRefs()[3]);
1011 assertSame(map, dbref4.getMap());
1014 * 'real' version replaces "0" version
1016 dbref2.setVersion("0");
1017 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1018 dbref2.getAccessionId());
1019 sq.addDBRef(dbref6);
1020 assertEquals(4, sq.getDBRefs().length);
1021 assertSame(dbref2, sq.getDBRefs()[1]);
1022 assertEquals("3", dbref2.getVersion());
1025 * 'real' version replaces "source:0" version
1027 dbref3.setVersion("Uniprot:0");
1028 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1029 dbref3.getAccessionId());
1030 sq.addDBRef(dbref7);
1031 assertEquals(4, sq.getDBRefs().length);
1032 assertSame(dbref3, sq.getDBRefs()[2]);
1033 assertEquals("3", dbref2.getVersion());
1036 @Test(groups = { "Functional" })
1037 public void testGetPrimaryDBRefs_peptide()
1039 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1042 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1043 assertTrue(primaryDBRefs.isEmpty());
1046 sq.setDBRefs(new DBRefEntry[] {});
1047 primaryDBRefs = sq.getPrimaryDBRefs();
1048 assertTrue(primaryDBRefs.isEmpty());
1050 // primary - uniprot
1051 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1052 sq.addDBRef(upentry1);
1054 // primary - uniprot with congruent map
1055 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1056 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1057 new int[] { 10, 22 }, 1, 1)));
1058 sq.addDBRef(upentry2);
1060 // primary - uniprot with map of enclosing sequence
1061 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1062 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1063 new int[] { 8, 24 }, 1, 1)));
1064 sq.addDBRef(upentry3);
1066 // not primary - uniprot with map of sub-sequence (5')
1067 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1068 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1069 new int[] { 10, 18 }, 1, 1)));
1070 sq.addDBRef(upentry4);
1072 // not primary - uniprot with map that overlaps 3'
1073 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1074 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1075 new int[] { 12, 22 }, 1, 1)));
1076 sq.addDBRef(upentry5);
1078 // not primary - uniprot with map to different coordinates frame
1079 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1080 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1081 new int[] { 112, 118 }, 1, 1)));
1082 sq.addDBRef(upentry6);
1084 // not primary - dbref to 'non-core' database
1085 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1086 sq.addDBRef(upentry7);
1088 // primary - type is PDB
1089 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1090 sq.addDBRef(pdbentry);
1092 // not primary - PDBEntry has no file
1093 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1095 // not primary - no PDBEntry
1096 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1098 // add corroborating PDB entry for primary DBref -
1099 // needs to have a file as well as matching ID
1100 // note PDB ID is not treated as case sensitive
1101 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1104 // not valid DBRef - no file..
1105 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1107 primaryDBRefs = sq.getPrimaryDBRefs();
1108 assertEquals(4, primaryDBRefs.size());
1109 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1110 primaryDBRefs.contains(upentry1));
1111 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1112 primaryDBRefs.contains(upentry2));
1113 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1114 primaryDBRefs.contains(upentry3));
1115 assertTrue("Couldn't find expected PDB primary reference",
1116 primaryDBRefs.contains(pdbentry));
1119 @Test(groups = { "Functional" })
1120 public void testGetPrimaryDBRefs_nucleotide()
1122 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1124 // primary - Ensembl
1125 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1128 // not primary - Ensembl 'transcript' mapping of sub-sequence
1129 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1130 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1131 new int[] { 1, 11 }, 1, 1)));
1134 // primary - EMBL with congruent map
1135 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1136 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1137 new int[] { 10, 34 }, 1, 1)));
1140 // not primary - to non-core database
1141 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1144 // not primary - to protein
1145 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1148 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1149 assertEquals(2, primaryDBRefs.size());
1150 assertTrue(primaryDBRefs.contains(dbr1));
1151 assertTrue(primaryDBRefs.contains(dbr3));
1155 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1158 @Test(groups = { "Functional" })
1159 public void testUpdatePDBIds()
1161 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1162 seq.addPDBId(pdbe1);
1163 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1164 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1165 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1166 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1167 // 7 is not a valid chain code:
1168 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1171 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1172 assertEquals(4, pdbIds.size());
1173 assertSame(pdbe1, pdbIds.get(0));
1174 // chain code got added to 3A6S:
1175 assertEquals("B", pdbe1.getChainCode());
1176 assertEquals("1A70", pdbIds.get(1).getId());
1177 // 4BQGA is parsed into id + chain
1178 assertEquals("4BQG", pdbIds.get(2).getId());
1179 assertEquals("a", pdbIds.get(2).getChainCode());
1180 assertEquals("2GIS7", pdbIds.get(3).getId());
1181 assertNull(pdbIds.get(3).getChainCode());
1185 * Test the method that either adds a pdbid or updates an existing one
1187 @Test(groups = { "Functional" })
1188 public void testAddPDBId()
1190 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1192 assertEquals(1, seq.getAllPDBEntries().size());
1193 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1194 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1196 // add the same entry
1198 assertEquals(1, seq.getAllPDBEntries().size());
1199 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1201 // add an identical entry
1202 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1203 assertEquals(1, seq.getAllPDBEntries().size());
1204 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1206 // add a different entry
1207 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1208 seq.addPDBId(pdbe2);
1209 assertEquals(2, seq.getAllPDBEntries().size());
1210 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1211 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1213 // update pdbe with chain code, file, type
1214 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1215 seq.addPDBId(pdbe3);
1216 assertEquals(2, seq.getAllPDBEntries().size());
1217 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1218 assertEquals("3A6S", pdbe.getId()); // unchanged
1219 assertEquals("A", pdbe.getChainCode()); // updated
1220 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1221 assertEquals("filepath", pdbe.getFile()); // updated
1222 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1224 // add with a different file path
1225 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1226 seq.addPDBId(pdbe4);
1227 assertEquals(3, seq.getAllPDBEntries().size());
1228 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1230 // add with a different chain code
1231 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1232 seq.addPDBId(pdbe5);
1233 assertEquals(4, seq.getAllPDBEntries().size());
1234 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1238 groups = { "Functional" },
1239 expectedExceptions = { IllegalArgumentException.class })
1240 public void testSetDatasetSequence_toSelf()
1242 seq.setDatasetSequence(seq);
1246 groups = { "Functional" },
1247 expectedExceptions = { IllegalArgumentException.class })
1248 public void testSetDatasetSequence_cascading()
1250 SequenceI seq2 = new Sequence("Seq2", "xyz");
1251 seq2.createDatasetSequence();
1252 seq.setDatasetSequence(seq2);
1256 public void testFindPositions()
1258 SequenceI sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1260 Range range = sq.findPositions(1, 4); // BC
1261 assertEquals(new Range(9, 10), range);
1263 range = sq.findPositions(2, 4); // C
1264 assertEquals(new Range(10, 10), range);
1266 assertNull(sq.findPositions(3, 4)); // all gaps
1268 range = sq.findPositions(2, 6); // CDE
1269 assertEquals(new Range(10, 12), range);
1271 range = sq.findPositions(3, 7); // DE
1272 assertEquals(new Range(11, 12), range);
1275 @Test(groups = { "Functional" })
1276 public void testFindIndex_withCursor()
1278 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1281 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1284 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1287 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1290 @Test(groups = { "Functional" })
1291 public void testFindPosition_withCursor()
1293 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1295 // find F pos given A
1296 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1297 int token = (int) PA.getValue(sq, "changeCount"); // 0
1298 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1299 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1301 // find A pos given F
1302 assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1303 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1304 assertEquals(new SequenceCursor(sq, 8, 2, token), cursor);
1306 // find C pos given C
1307 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1308 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1309 assertEquals(new SequenceCursor(sq, 10, 6, token), cursor);
1311 // now the grey area - what residue position for a gapped column? JAL-2562
1313 // find 'residue' for column 3 given cursor for D (so working left)
1314 // returns B9; cursor is updated to [B 5]
1315 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1316 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1317 assertEquals(new SequenceCursor(sq, 9, 5, token), cursor);
1319 // find 'residue' for column 8 given cursor for D (so working right)
1320 // returns E12; cursor is updated to [D 7]
1321 assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1322 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1323 assertEquals(new SequenceCursor(sq, 11, 7, token), cursor);
1325 // find 'residue' for column 12 given cursor for B
1326 // returns 1 more than last residue position; cursor is updated to [F 10]
1327 assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1328 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1329 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1332 * findPosition for column beyond length of sequence
1333 * returns 1 more than the last residue position
1334 * cursor is set to last real residue position [F 10]
1336 assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
1337 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1338 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1341 * and the case without a trailing gap
1343 sq = new Sequence("test/8-13", "-A--BCD-EF");
1344 // first find C from A
1345 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
1346 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1347 assertEquals(new SequenceCursor(sq, 10, 6, token), cursor);
1348 // now 'find' 99 from C
1349 // cursor is set to [F 10]
1350 assertEquals(14, sq.findPosition(99, cursor));
1351 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1352 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1356 public void testFindPositions_withCursor()
1358 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1360 // find positions for columns 1-4 (BC--) given E cursor
1361 Range range = sq.findPositions(1, 4, new SequenceCursor(sq, 12, 7, 0)); // BC
1362 assertEquals(new Range(9, 10), range);
1364 // repeat using B cursor
1365 range = sq.findPositions(1, 4, new SequenceCursor(sq, 9, 2, 0)); // BC
1366 assertEquals(new Range(9, 10), range);
1368 // find positions for columns 2-4 (C--) given A cursor
1369 range = sq.findPositions(2, 4, new SequenceCursor(sq, 8, 1, 0)); // C
1370 assertEquals(new Range(10, 10), range);
1373 assertNull(sq.findPositions(3, 4, new SequenceCursor(sq, 10, 3, 0)));
1374 assertNull(sq.findPositions(3, 4, new SequenceCursor(sq, 12, 7, 0)));
1376 // find positions for columns 2-6 (C--DE) given B cursor
1377 range = sq.findPositions(2, 6, new SequenceCursor(sq, 9, 2, 0)); // CDE
1378 assertEquals(new Range(10, 12), range);
1380 // repeat using C as cursor
1381 range = sq.findPositions(2, 6, new SequenceCursor(sq, 10, 3, 0));
1382 assertEquals(new Range(10, 12), range);
1384 // repeat using D as cursor
1385 range = sq.findPositions(2, 6, new SequenceCursor(sq, 11, 6, 0));
1386 assertEquals(new Range(10, 12), range);
1388 // repeat using E as cursor
1389 range = sq.findPositions(2, 6, new SequenceCursor(sq, 12, 7, 0));
1390 assertEquals(new Range(10, 12), range);
1392 // repeat using F as cursor
1393 range = sq.findPositions(2, 6, new SequenceCursor(sq, 13, 9, 0));
1394 assertEquals(new Range(10, 12), range);
1398 public void testIsValidCursor()
1400 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1401 assertFalse(sq.isValidCursor(null));
1404 * cursor is valid if it has valid sequence ref and changeCount token
1405 * and positions within the range of the sequence
1407 int changeCount = (int) PA.getValue(sq, "changeCount");
1408 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1409 assertTrue(sq.isValidCursor(cursor));
1412 * column position outside [0 - length-1] is rejected
1414 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1415 assertFalse(sq.isValidCursor(cursor));
1416 cursor = new SequenceCursor(sq, 13, 9, changeCount);
1417 assertFalse(sq.isValidCursor(cursor));
1418 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1419 assertFalse(sq.isValidCursor(cursor));
1420 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1421 assertFalse(sq.isValidCursor(cursor));
1424 * wrong sequence is rejected
1426 cursor = new SequenceCursor(null, 13, 1, changeCount);
1427 assertFalse(sq.isValidCursor(cursor));
1428 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1430 assertFalse(sq.isValidCursor(cursor));
1433 * wrong token value is rejected
1435 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1436 assertFalse(sq.isValidCursor(cursor));
1437 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1438 assertFalse(sq.isValidCursor(cursor));
1441 @Test(groups = { "Functional" })
1442 public void testFindPosition_withCursorAndEdits()
1444 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1446 // find F pos given A
1447 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1448 int token = (int) PA.getValue(sq, "changeCount"); // 0
1449 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1450 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1453 * setSequence should invalidate the cursor cached by the sequence
1455 sq.setSequence("-A-BCD-EF---"); // one gap removed
1456 assertEquals(8, sq.getStart()); // sanity check
1457 assertEquals(11, sq.findPosition(5)); // D11
1458 // cursor should now be at [D 6]
1459 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1460 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1463 * deleteChars should invalidate the cached cursor
1465 sq.deleteChars(2, 5); // delete -BC
1466 assertEquals("-AD-EF---", sq.getSequenceAsString());
1467 assertEquals(8, sq.getStart()); // sanity check
1468 assertEquals(10, sq.findPosition(4)); // E10
1469 // cursor should now be at [E 5]
1470 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1471 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1474 * Edit to insert gaps should invalidate the cached cursor
1475 * insert 2 gaps at column[3] to make -AD---EF---
1477 SequenceI[] seqs = new SequenceI[] { sq };
1478 AlignmentI al = new Alignment(seqs);
1479 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1480 assertEquals("-AD---EF---", sq.getSequenceAsString());
1481 assertEquals(10, sq.findPosition(4)); // E10
1482 // cursor should now be at [D 3]
1483 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1484 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1487 * insertCharAt should invalidate the cached cursor
1488 * insert CC at column[4] to make -AD-CC--EF---
1490 sq.insertCharAt(4, 2, 'C');
1491 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1492 assertEquals(13, sq.findPosition(9)); // F13
1493 // cursor should now be at [F 10]
1494 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1495 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);