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.BitSet;
41 import java.util.List;
42 import java.util.Vector;
44 import junit.extensions.PA;
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.Test;
51 public class SequenceTest
54 @BeforeClass(alwaysRun = true)
55 public void setUpJvOptionPane()
57 JvOptionPane.setInteractiveMode(false);
58 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63 @BeforeMethod(alwaysRun = true)
66 seq = new Sequence("FER1", "AKPNGVL");
69 @Test(groups = { "Functional" })
70 public void testInsertGapsAndGapmaps()
72 SequenceI aseq = seq.deriveSequence();
73 aseq.insertCharAt(2, 3, '-');
74 aseq.insertCharAt(6, 3, '-');
75 assertEquals("Gap insertions not correct", "AK---P---NGVL",
76 aseq.getSequenceAsString());
77 List<int[]> gapInt = aseq.getInsertions();
78 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
79 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
80 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
81 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
83 BitSet gapfield = aseq.getInsertionsAsBits();
84 BitSet expectedgaps = new BitSet();
85 expectedgaps.set(2, 5);
86 expectedgaps.set(6, 9);
88 assertEquals(6, expectedgaps.cardinality());
90 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
91 6, gapfield.cardinality());
93 assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield);
96 @Test(groups = ("Functional"))
97 public void testIsProtein()
100 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
102 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
104 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
105 assertFalse(sq.isProtein());
106 // change sequence, should trigger an update of cached result
107 sq.setSequence("ASDFASDFADSF");
108 assertTrue(sq.isProtein());
111 @Test(groups = { "Functional" })
112 public void testGetAnnotation()
114 // initial state returns null not an empty array
115 assertNull(seq.getAnnotation());
116 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
118 AlignmentAnnotation[] anns = seq.getAnnotation();
119 assertEquals(1, anns.length);
120 assertSame(ann, anns[0]);
122 // removing all annotations reverts array to null
123 seq.removeAlignmentAnnotation(ann);
124 assertNull(seq.getAnnotation());
127 @Test(groups = { "Functional" })
128 public void testGetAnnotation_forLabel()
130 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
132 addAnnotation("label2", "desc2", "calcId2", 1f);
133 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
135 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
136 assertEquals(2, anns.length);
137 assertSame(ann1, anns[0]);
138 assertSame(ann3, anns[1]);
141 private AlignmentAnnotation addAnnotation(String label,
142 String description, String calcId, float value)
144 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
146 annotation.setCalcId(calcId);
147 seq.addAlignmentAnnotation(annotation);
151 @Test(groups = { "Functional" })
152 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
154 addAnnotation("label1", "desc1", "calcId1", 1f);
155 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
157 addAnnotation("label2", "desc3", "calcId3", 1f);
158 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
160 addAnnotation("label5", "desc3", null, 1f);
161 addAnnotation(null, "desc3", "calcId3", 1f);
163 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
165 assertEquals(2, anns.size());
166 assertSame(ann2, anns.get(0));
167 assertSame(ann4, anns.get(1));
169 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
170 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
171 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
172 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
173 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
177 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
178 * setting the sequenceRef on the annotation. Adding the same annotation twice
181 @Test(groups = { "Functional" })
182 public void testAddAlignmentAnnotation()
184 assertNull(seq.getAnnotation());
185 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
187 assertNull(annotation.sequenceRef);
188 seq.addAlignmentAnnotation(annotation);
189 assertSame(seq, annotation.sequenceRef);
190 AlignmentAnnotation[] anns = seq.getAnnotation();
191 assertEquals(1, anns.length);
192 assertSame(annotation, anns[0]);
194 // re-adding does nothing
195 seq.addAlignmentAnnotation(annotation);
196 anns = seq.getAnnotation();
197 assertEquals(1, anns.length);
198 assertSame(annotation, anns[0]);
200 // an identical but different annotation can be added
201 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
203 seq.addAlignmentAnnotation(annotation2);
204 anns = seq.getAnnotation();
205 assertEquals(2, anns.length);
206 assertSame(annotation, anns[0]);
207 assertSame(annotation2, anns[1]);
210 @Test(groups = { "Functional" })
211 public void testGetStartGetEnd()
213 SequenceI sq = new Sequence("test", "ABCDEF");
214 assertEquals(1, sq.getStart());
215 assertEquals(6, sq.getEnd());
217 sq = new Sequence("test", "--AB-C-DEF--");
218 assertEquals(1, sq.getStart());
219 assertEquals(6, sq.getEnd());
221 sq = new Sequence("test", "----");
222 assertEquals(1, sq.getStart());
223 assertEquals(0, sq.getEnd()); // ??
227 * Tests for the method that returns an alignment column position (base 1) for
228 * a given sequence position (base 1).
230 @Test(groups = { "Functional" })
231 public void testFindIndex()
234 * call sequenceChanged() after each test to invalidate any cursor,
235 * forcing the 1-arg findIndex to be executed
237 SequenceI sq = new Sequence("test", "ABCDEF");
238 assertEquals(0, sq.findIndex(0));
239 sq.sequenceChanged();
240 assertEquals(1, sq.findIndex(1));
241 sq.sequenceChanged();
242 assertEquals(5, sq.findIndex(5));
243 sq.sequenceChanged();
244 assertEquals(6, sq.findIndex(6));
245 sq.sequenceChanged();
246 assertEquals(6, sq.findIndex(9));
248 sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
249 assertEquals(2, sq.findIndex(8));
250 sq.sequenceChanged();
251 assertEquals(5, sq.findIndex(9));
252 sq.sequenceChanged();
253 assertEquals(7, sq.findIndex(10));
255 // before start returns 0
256 sq.sequenceChanged();
257 assertEquals(0, sq.findIndex(0));
258 sq.sequenceChanged();
259 assertEquals(0, sq.findIndex(-1));
261 // beyond end returns last residue column
262 sq.sequenceChanged();
263 assertEquals(13, sq.findIndex(99));
267 * Tests for the method that returns a dataset sequence position (start..) for
268 * an aligned column position (base 0).
270 @Test(groups = { "Functional" })
271 public void testFindPosition()
274 * call sequenceChanged() after each test to invalidate any cursor,
275 * forcing the 1-arg findPosition to be executed
277 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
278 assertEquals(8, sq.findPosition(0));
279 // Sequence should now hold a cursor at [8, 0]
280 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
281 PA.getValue(sq, "cursor").toString());
282 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
283 int token = (int) PA.getValue(sq, "changeCount");
284 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
286 sq.sequenceChanged();
289 * find F13 at column offset 5, cursor should update to [13, 6]
290 * endColumn is found and saved in cursor
292 assertEquals(13, sq.findPosition(5));
293 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
294 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
295 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
296 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok1",
297 PA.getValue(sq, "cursor").toString());
299 // assertEquals(-1, seq.findPosition(6)); // fails
301 sq = new Sequence("test/8-11", "AB-C-D--");
302 token = (int) PA.getValue(sq, "changeCount"); // 0
303 assertEquals(8, sq.findPosition(0));
304 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
305 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
306 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
307 PA.getValue(sq, "cursor").toString());
309 sq.sequenceChanged();
310 assertEquals(9, sq.findPosition(1));
311 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
312 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
313 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok1",
314 PA.getValue(sq, "cursor").toString());
316 sq.sequenceChanged();
317 // gap position 'finds' residue to the right (not the left as per javadoc)
318 // cursor is set to the last residue position found [B 2]
319 assertEquals(10, sq.findPosition(2));
320 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
321 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
322 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
323 PA.getValue(sq, "cursor").toString());
325 sq.sequenceChanged();
326 assertEquals(10, sq.findPosition(3));
327 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
328 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
329 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok3",
330 PA.getValue(sq, "cursor").toString());
332 sq.sequenceChanged();
333 // column[4] is the gap after C - returns D11
334 // cursor is set to [C 4]
335 assertEquals(11, sq.findPosition(4));
336 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
337 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
338 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
339 PA.getValue(sq, "cursor").toString());
341 sq.sequenceChanged();
342 assertEquals(11, sq.findPosition(5)); // D
343 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
344 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
345 // lastCol has been found and saved in the cursor
346 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok5",
347 PA.getValue(sq, "cursor").toString());
349 sq.sequenceChanged();
350 // returns 1 more than sequence length if off the end ?!?
351 assertEquals(12, sq.findPosition(6));
353 sq.sequenceChanged();
354 assertEquals(12, sq.findPosition(7));
357 * first findPosition should also set firstResCol in cursor
359 sq = new Sequence("test/8-13", "--AB-C-DEF--");
360 assertEquals(8, sq.findPosition(0));
361 assertNull(PA.getValue(sq, "cursor"));
363 sq.sequenceChanged();
364 assertEquals(8, sq.findPosition(1));
365 assertNull(PA.getValue(sq, "cursor"));
367 sq.sequenceChanged();
368 assertEquals(8, sq.findPosition(2));
369 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok2",
370 PA.getValue(sq, "cursor").toString());
372 sq.sequenceChanged();
373 assertEquals(9, sq.findPosition(3));
374 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok3",
375 PA.getValue(sq, "cursor").toString());
377 sq.sequenceChanged();
378 // column[4] is a gap, returns next residue pos (C10)
379 // cursor is set to last residue found [B]
380 assertEquals(10, sq.findPosition(4));
381 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
382 PA.getValue(sq, "cursor").toString());
384 sq.sequenceChanged();
385 assertEquals(10, sq.findPosition(5));
386 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok5",
387 PA.getValue(sq, "cursor").toString());
389 sq.sequenceChanged();
390 // column[6] is a gap, returns next residue pos (D11)
391 // cursor is set to last residue found [C]
392 assertEquals(11, sq.findPosition(6));
393 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
394 PA.getValue(sq, "cursor").toString());
396 sq.sequenceChanged();
397 assertEquals(11, sq.findPosition(7));
398 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok7",
399 PA.getValue(sq, "cursor").toString());
401 sq.sequenceChanged();
402 assertEquals(12, sq.findPosition(8));
403 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok8",
404 PA.getValue(sq, "cursor").toString());
407 * when the last residue column is found, it is set in the cursor
409 sq.sequenceChanged();
410 assertEquals(13, sq.findPosition(9));
411 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok9",
412 PA.getValue(sq, "cursor").toString());
414 sq.sequenceChanged();
415 assertEquals(14, sq.findPosition(10));
416 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
417 PA.getValue(sq, "cursor").toString());
420 * findPosition for column beyond sequence length
421 * returns 1 more than last residue position
423 sq.sequenceChanged();
424 assertEquals(14, sq.findPosition(11));
425 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
426 PA.getValue(sq, "cursor").toString());
428 sq.sequenceChanged();
429 assertEquals(14, sq.findPosition(99));
430 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
431 PA.getValue(sq, "cursor").toString());
434 * gapped sequence ending in non-gap
436 sq = new Sequence("test/8-13", "--AB-C-DEF");
437 assertEquals(13, sq.findPosition(9));
438 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok0",
439 PA.getValue(sq, "cursor").toString());
440 sq.sequenceChanged();
441 assertEquals(12, sq.findPosition(8));
442 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
443 // sequenceChanged() invalidates cursor.lastResidueColumn
444 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
445 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok1",
447 // findPosition with cursor accepts base 1 column values
448 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
449 assertEquals(13, sq.findPosition(9)); // F13
450 // lastResidueColumn has now been found and saved in cursor
451 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
452 PA.getValue(sq, "cursor").toString());
455 @Test(groups = { "Functional" })
456 public void testDeleteChars()
461 SequenceI sq = new Sequence("test", "ABCDEF");
462 assertNull(PA.getValue(sq, "datasetSequence"));
463 assertEquals(1, sq.getStart());
464 assertEquals(6, sq.getEnd());
465 sq.deleteChars(2, 3);
466 assertEquals("ABDEF", sq.getSequenceAsString());
467 assertEquals(1, sq.getStart());
468 assertEquals(5, sq.getEnd());
469 assertNull(PA.getValue(sq, "datasetSequence"));
474 sq = new Sequence("test", "ABCDEF");
475 sq.deleteChars(0, 2);
476 assertEquals("CDEF", sq.getSequenceAsString());
477 assertEquals(3, sq.getStart());
478 assertEquals(6, sq.getEnd());
479 assertNull(PA.getValue(sq, "datasetSequence"));
484 sq = new Sequence("test", "ABCDEF");
485 sq.deleteChars(4, 6);
486 assertEquals("ABCD", sq.getSequenceAsString());
487 assertEquals(1, sq.getStart());
488 assertEquals(4, sq.getEnd());
489 assertNull(PA.getValue(sq, "datasetSequence"));
492 @Test(groups = { "Functional" })
493 public void testDeleteChars_withDbRefsAndFeatures()
496 * internal delete - new dataset sequence created
497 * gets a copy of any dbrefs
499 SequenceI sq = new Sequence("test", "ABCDEF");
500 sq.createDatasetSequence();
501 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
503 Object ds = PA.getValue(sq, "datasetSequence");
505 assertEquals(1, sq.getStart());
506 assertEquals(6, sq.getEnd());
507 sq.deleteChars(2, 3);
508 assertEquals("ABDEF", sq.getSequenceAsString());
509 assertEquals(1, sq.getStart());
510 assertEquals(5, sq.getEnd());
511 Object newDs = PA.getValue(sq, "datasetSequence");
512 assertNotNull(newDs);
513 assertNotSame(ds, newDs);
514 assertNotNull(sq.getDBRefs());
515 assertEquals(1, sq.getDBRefs().length);
516 assertNotSame(dbr1, sq.getDBRefs()[0]);
517 assertEquals(dbr1, sq.getDBRefs()[0]);
520 * internal delete with sequence features
521 * (failure case for JAL-2541)
523 sq = new Sequence("test", "ABCDEF");
524 sq.createDatasetSequence();
525 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
527 sq.addSequenceFeature(sf1);
528 ds = PA.getValue(sq, "datasetSequence");
530 assertEquals(1, sq.getStart());
531 assertEquals(6, sq.getEnd());
532 sq.deleteChars(2, 4);
533 assertEquals("ABEF", sq.getSequenceAsString());
534 assertEquals(1, sq.getStart());
535 assertEquals(4, sq.getEnd());
536 newDs = PA.getValue(sq, "datasetSequence");
537 assertNotNull(newDs);
538 assertNotSame(ds, newDs);
539 List<SequenceFeature> sfs = sq.getSequenceFeatures();
540 assertEquals(1, sfs.size());
541 assertNotSame(sf1, sfs.get(0));
542 assertEquals(sf1, sfs.get(0));
545 * delete at start - no new dataset sequence created
546 * any sequence features remain as before
548 sq = new Sequence("test", "ABCDEF");
549 sq.createDatasetSequence();
550 ds = PA.getValue(sq, "datasetSequence");
551 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
552 sq.addSequenceFeature(sf1);
553 sq.deleteChars(0, 2);
554 assertEquals("CDEF", sq.getSequenceAsString());
555 assertEquals(3, sq.getStart());
556 assertEquals(6, sq.getEnd());
557 assertSame(ds, PA.getValue(sq, "datasetSequence"));
558 sfs = sq.getSequenceFeatures();
560 assertEquals(1, sfs.size());
561 assertSame(sf1, sfs.get(0));
564 * delete at end - no new dataset sequence created
565 * any dbrefs remain as before
567 sq = new Sequence("test", "ABCDEF");
568 sq.createDatasetSequence();
569 ds = PA.getValue(sq, "datasetSequence");
570 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
572 sq.deleteChars(4, 6);
573 assertEquals("ABCD", sq.getSequenceAsString());
574 assertEquals(1, sq.getStart());
575 assertEquals(4, sq.getEnd());
576 assertSame(ds, PA.getValue(sq, "datasetSequence"));
577 assertNotNull(sq.getDBRefs());
578 assertEquals(1, sq.getDBRefs().length);
579 assertSame(dbr1, sq.getDBRefs()[0]);
582 @Test(groups = { "Functional" })
583 public void testInsertCharAt()
585 // non-static methods:
586 SequenceI sq = new Sequence("test", "ABCDEF");
587 sq.insertCharAt(0, 'z');
588 assertEquals("zABCDEF", sq.getSequenceAsString());
589 sq.insertCharAt(2, 2, 'x');
590 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
592 // for static method see StringUtilsTest
596 * Test the method that returns an array of aligned sequence positions where
597 * the array index is the data sequence position (both base 0).
599 @Test(groups = { "Functional" })
600 public void testGapMap()
602 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
603 sq.createDatasetSequence();
604 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
608 * Test the method that gets sequence features, either from the sequence or
611 @Test(groups = { "Functional" })
612 public void testGetSequenceFeatures()
614 SequenceI sq = new Sequence("test", "GATCAT");
615 sq.createDatasetSequence();
617 assertTrue(sq.getSequenceFeatures().isEmpty());
620 * SequenceFeature on sequence
622 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
623 sq.addSequenceFeature(sf);
624 List<SequenceFeature> sfs = sq.getSequenceFeatures();
625 assertEquals(1, sfs.size());
626 assertSame(sf, sfs.get(0));
629 * SequenceFeature on sequence and dataset sequence; returns that on
632 * Note JAL-2046: spurious: we have no use case for this at the moment.
633 * This test also buggy - as sf2.equals(sf), no new feature is added
635 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
637 sq.getDatasetSequence().addSequenceFeature(sf2);
638 sfs = sq.getSequenceFeatures();
639 assertEquals(1, sfs.size());
640 assertSame(sf, sfs.get(0));
643 * SequenceFeature on dataset sequence only
644 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
646 sq.setSequenceFeatures(null);
647 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
650 * Corrupt case - no SequenceFeature, dataset's dataset is the original
651 * sequence. Test shows no infinite loop results.
653 sq.getDatasetSequence().setSequenceFeatures(null);
655 * is there a usecase for this ? setDatasetSequence should throw an error if
656 * this actually occurs.
660 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
661 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
662 } catch (IllegalArgumentException e)
664 // TODO Jalview error/exception class for raising implementation errors
665 assertTrue(e.getMessage().toLowerCase()
666 .contains("implementation error"));
668 assertTrue(sq.getSequenceFeatures().isEmpty());
672 * Test the method that returns an array, indexed by sequence position, whose
673 * entries are the residue positions at the sequence position (or to the right
676 @Test(groups = { "Functional" })
677 public void testFindPositionMap()
680 * Note: Javadoc for findPosition says it returns the residue position to
681 * the left of a gapped position; in fact it returns the position to the
682 * right. Also it returns a non-existent residue position for a gap beyond
685 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
686 int[] map = sq.findPositionMap();
687 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
688 Arrays.toString(map));
692 * Test for getSubsequence
694 @Test(groups = { "Functional" })
695 public void testGetSubsequence()
697 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
698 sq.createDatasetSequence();
700 // positions are base 0, end position is exclusive
701 SequenceI subseq = sq.getSubSequence(2, 4);
703 assertEquals("CD", subseq.getSequenceAsString());
704 // start/end are base 1 positions
705 assertEquals(3, subseq.getStart());
706 assertEquals(4, subseq.getEnd());
707 // subsequence shares the full dataset sequence
708 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
712 * test createDatasetSequence behaves to doc
714 @Test(groups = { "Functional" })
715 public void testCreateDatasetSequence()
717 SequenceI sq = new Sequence("my", "ASDASD");
718 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
720 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
721 assertNull(sq.getDatasetSequence());
722 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
723 assertNotNull(PA.getValue(sq, "dbrefs"));
725 SequenceI rds = sq.createDatasetSequence();
727 assertNull(rds.getDatasetSequence());
728 assertSame(sq.getDatasetSequence(), rds);
730 // sequence features and dbrefs transferred to dataset sequence
731 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
732 assertNull(PA.getValue(sq, "dbrefs"));
733 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
734 assertNotNull(PA.getValue(rds, "dbrefs"));
738 * Test for deriveSequence applied to a sequence with a dataset
740 @Test(groups = { "Functional" })
741 public void testDeriveSequence_existingDataset()
743 Sequence sq = new Sequence("Seq1", "CD");
744 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
745 sq.getDatasetSequence().addSequenceFeature(
746 new SequenceFeature("", "", 1, 2, 0f, null));
750 sq.setDescription("Test sequence description..");
751 sq.setVamsasId("TestVamsasId");
752 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
754 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
755 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
756 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
757 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
759 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
760 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
761 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
762 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
764 // these are the same as ones already added
765 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
766 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
768 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
771 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
772 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
773 sq.getDatasetSequence().addDBRef(
774 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
775 sq.getDatasetSequence().addDBRef(
776 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
778 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
779 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
780 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
782 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
784 sq.getDatasetSequence().addPDBId(pdbe1a);
785 sq.getDatasetSequence().addPDBId(pdbe1b);
786 sq.getDatasetSequence().addPDBId(pdbe2a);
787 sq.getDatasetSequence().addPDBId(pdbe2b);
790 * test we added pdb entries to the dataset sequence
792 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
793 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
794 "PDB Entries were not found on dataset sequence.");
797 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
799 Assert.assertEquals(pdbe1a,
800 sq.getDatasetSequence().getPDBEntry("1PDB"),
801 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
802 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
803 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
804 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
805 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
806 Annotation[] annots = annotsList.toArray(new Annotation[0]);
807 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
808 "Test annot description", annots));
809 sq.getDatasetSequence().addAlignmentAnnotation(
810 new AlignmentAnnotation("Test annot", "Test annot description",
812 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
813 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
815 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
816 Assert.assertNotNull(sq.getAnnotation());
817 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
818 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
821 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
823 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
825 Sequence derived = (Sequence) sq.deriveSequence();
827 Assert.assertEquals(derived.getDescription(),
828 "Test sequence description..");
829 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
830 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
831 Assert.assertNotNull(derived.getAnnotation());
832 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
833 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
834 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
836 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
838 assertEquals("CD", derived.getSequenceAsString());
839 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
841 // derived sequence should access dataset sequence features
842 assertNotNull(sq.getSequenceFeatures());
843 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
846 * verify we have primary db refs *just* for PDB IDs with associated
850 assertEquals(primRefs, sq.getPrimaryDBRefs());
851 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
853 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
858 * Test for deriveSequence applied to an ungapped sequence with no dataset
860 @Test(groups = { "Functional" })
861 public void testDeriveSequence_noDatasetUngapped()
863 SequenceI sq = new Sequence("Seq1", "ABCDEF");
864 assertEquals(1, sq.getStart());
865 assertEquals(6, sq.getEnd());
866 SequenceI derived = sq.deriveSequence();
867 assertEquals("ABCDEF", derived.getSequenceAsString());
868 assertEquals("ABCDEF", derived.getDatasetSequence()
869 .getSequenceAsString());
873 * Test for deriveSequence applied to a gapped sequence with no dataset
875 @Test(groups = { "Functional" })
876 public void testDeriveSequence_noDatasetGapped()
878 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
879 assertEquals(1, sq.getStart());
880 assertEquals(6, sq.getEnd());
881 assertNull(sq.getDatasetSequence());
882 SequenceI derived = sq.deriveSequence();
883 assertEquals("AB-C.D EF", derived.getSequenceAsString());
884 assertEquals("ABCDEF", derived.getDatasetSequence()
885 .getSequenceAsString());
888 @Test(groups = { "Functional" })
889 public void testCopyConstructor_noDataset()
891 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
892 seq1.setDescription("description");
893 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
895 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
897 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
898 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
900 SequenceI copy = new Sequence(seq1);
902 assertNull(copy.getDatasetSequence());
904 verifyCopiedSequence(seq1, copy);
906 // copy has a copy of the DBRefEntry
907 // this is murky - DBrefs are only copied for dataset sequences
908 // where the test for 'dataset sequence' is 'dataset is null'
909 // but that doesn't distinguish it from an aligned sequence
910 // which has not yet generated a dataset sequence
911 // NB getDBRef looks inside dataset sequence if not null
912 DBRefEntry[] dbrefs = copy.getDBRefs();
913 assertEquals(1, dbrefs.length);
914 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
915 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
918 @Test(groups = { "Functional" })
919 public void testCopyConstructor_withDataset()
921 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
922 seq1.createDatasetSequence();
923 seq1.setDescription("description");
924 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
926 // JAL-2046 - what is the contract for using a derived sequence's
927 // addSequenceFeature ?
928 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
930 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
931 // here we add DBRef to the dataset sequence:
932 seq1.getDatasetSequence().addDBRef(
933 new DBRefEntry("EMBL", "1.2", "AZ12345"));
935 SequenceI copy = new Sequence(seq1);
937 assertNotNull(copy.getDatasetSequence());
938 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
940 verifyCopiedSequence(seq1, copy);
942 // getDBRef looks inside dataset sequence and this is shared,
943 // so holds the same dbref objects
944 DBRefEntry[] dbrefs = copy.getDBRefs();
945 assertEquals(1, dbrefs.length);
946 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
950 * Helper to make assertions about a copied sequence
955 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
957 // verify basic properties:
958 assertEquals(copy.getName(), seq1.getName());
959 assertEquals(copy.getDescription(), seq1.getDescription());
960 assertEquals(copy.getStart(), seq1.getStart());
961 assertEquals(copy.getEnd(), seq1.getEnd());
962 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
964 // copy has a copy of the annotation:
965 AlignmentAnnotation[] anns = copy.getAnnotation();
966 assertEquals(1, anns.length);
967 assertFalse(anns[0] == seq1.getAnnotation()[0]);
968 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
969 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
970 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
972 // copy has a copy of the sequence feature:
973 List<SequenceFeature> sfs = copy.getSequenceFeatures();
974 assertEquals(1, sfs.size());
975 if (seq1.getDatasetSequence() != null
976 && copy.getDatasetSequence() == seq1.getDatasetSequence())
978 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
982 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
984 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
986 // copy has a copy of the PDB entry
987 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
988 assertEquals(1, pdbs.size());
989 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
990 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
993 @Test(groups = "Functional")
994 public void testGetCharAt()
996 SequenceI sq = new Sequence("", "abcde");
997 assertEquals('a', sq.getCharAt(0));
998 assertEquals('e', sq.getCharAt(4));
999 assertEquals(' ', sq.getCharAt(5));
1000 assertEquals(' ', sq.getCharAt(-1));
1003 @Test(groups = { "Functional" })
1004 public void testAddSequenceFeatures()
1006 SequenceI sq = new Sequence("", "abcde");
1007 // type may not be null
1008 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
1010 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1012 // can't add a duplicate feature
1013 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
1015 // can add a different feature
1016 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
1017 8, 0f, null))); // different type
1018 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
1019 "description", 4, 8, 0f, null)));// different description
1020 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
1021 8, 0f, null))); // different start position
1022 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1023 9, 0f, null))); // different end position
1024 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1025 8, 1f, null))); // different score
1026 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1027 8, Float.NaN, null))); // score NaN
1028 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1029 8, 0f, "Metal"))); // different group
1030 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1034 * Tests for adding (or updating) dbrefs
1036 * @see DBRefEntry#updateFrom(DBRefEntry)
1038 @Test(groups = { "Functional" })
1039 public void testAddDBRef()
1041 SequenceI sq = new Sequence("", "abcde");
1042 assertNull(sq.getDBRefs());
1043 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1045 assertEquals(1, sq.getDBRefs().length);
1046 assertSame(dbref, sq.getDBRefs()[0]);
1049 * change of version - new entry
1051 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1052 sq.addDBRef(dbref2);
1053 assertEquals(2, sq.getDBRefs().length);
1054 assertSame(dbref, sq.getDBRefs()[0]);
1055 assertSame(dbref2, sq.getDBRefs()[1]);
1058 * matches existing entry - not added
1060 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1061 assertEquals(2, sq.getDBRefs().length);
1064 * different source = new entry
1066 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1067 sq.addDBRef(dbref3);
1068 assertEquals(3, sq.getDBRefs().length);
1069 assertSame(dbref3, sq.getDBRefs()[2]);
1072 * different ref = new entry
1074 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1075 sq.addDBRef(dbref4);
1076 assertEquals(4, sq.getDBRefs().length);
1077 assertSame(dbref4, sq.getDBRefs()[3]);
1080 * matching ref with a mapping - map updated
1082 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1083 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1086 sq.addDBRef(dbref5);
1087 assertEquals(4, sq.getDBRefs().length);
1088 assertSame(dbref4, sq.getDBRefs()[3]);
1089 assertSame(map, dbref4.getMap());
1092 * 'real' version replaces "0" version
1094 dbref2.setVersion("0");
1095 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1096 dbref2.getAccessionId());
1097 sq.addDBRef(dbref6);
1098 assertEquals(4, sq.getDBRefs().length);
1099 assertSame(dbref2, sq.getDBRefs()[1]);
1100 assertEquals("3", dbref2.getVersion());
1103 * 'real' version replaces "source:0" version
1105 dbref3.setVersion("Uniprot:0");
1106 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1107 dbref3.getAccessionId());
1108 sq.addDBRef(dbref7);
1109 assertEquals(4, sq.getDBRefs().length);
1110 assertSame(dbref3, sq.getDBRefs()[2]);
1111 assertEquals("3", dbref2.getVersion());
1114 @Test(groups = { "Functional" })
1115 public void testGetPrimaryDBRefs_peptide()
1117 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1120 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1121 assertTrue(primaryDBRefs.isEmpty());
1124 sq.setDBRefs(new DBRefEntry[] {});
1125 primaryDBRefs = sq.getPrimaryDBRefs();
1126 assertTrue(primaryDBRefs.isEmpty());
1128 // primary - uniprot
1129 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1130 sq.addDBRef(upentry1);
1132 // primary - uniprot with congruent map
1133 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1134 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1135 new int[] { 10, 22 }, 1, 1)));
1136 sq.addDBRef(upentry2);
1138 // primary - uniprot with map of enclosing sequence
1139 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1140 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1141 new int[] { 8, 24 }, 1, 1)));
1142 sq.addDBRef(upentry3);
1144 // not primary - uniprot with map of sub-sequence (5')
1145 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1146 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1147 new int[] { 10, 18 }, 1, 1)));
1148 sq.addDBRef(upentry4);
1150 // not primary - uniprot with map that overlaps 3'
1151 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1152 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1153 new int[] { 12, 22 }, 1, 1)));
1154 sq.addDBRef(upentry5);
1156 // not primary - uniprot with map to different coordinates frame
1157 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1158 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1159 new int[] { 112, 118 }, 1, 1)));
1160 sq.addDBRef(upentry6);
1162 // not primary - dbref to 'non-core' database
1163 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1164 sq.addDBRef(upentry7);
1166 // primary - type is PDB
1167 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1168 sq.addDBRef(pdbentry);
1170 // not primary - PDBEntry has no file
1171 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1173 // not primary - no PDBEntry
1174 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1176 // add corroborating PDB entry for primary DBref -
1177 // needs to have a file as well as matching ID
1178 // note PDB ID is not treated as case sensitive
1179 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1182 // not valid DBRef - no file..
1183 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1185 primaryDBRefs = sq.getPrimaryDBRefs();
1186 assertEquals(4, primaryDBRefs.size());
1187 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1188 primaryDBRefs.contains(upentry1));
1189 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1190 primaryDBRefs.contains(upentry2));
1191 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1192 primaryDBRefs.contains(upentry3));
1193 assertTrue("Couldn't find expected PDB primary reference",
1194 primaryDBRefs.contains(pdbentry));
1197 @Test(groups = { "Functional" })
1198 public void testGetPrimaryDBRefs_nucleotide()
1200 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1202 // primary - Ensembl
1203 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1206 // not primary - Ensembl 'transcript' mapping of sub-sequence
1207 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1208 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1209 new int[] { 1, 11 }, 1, 1)));
1212 // primary - EMBL with congruent map
1213 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1214 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1215 new int[] { 10, 34 }, 1, 1)));
1218 // not primary - to non-core database
1219 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1222 // not primary - to protein
1223 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1226 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1227 assertEquals(2, primaryDBRefs.size());
1228 assertTrue(primaryDBRefs.contains(dbr1));
1229 assertTrue(primaryDBRefs.contains(dbr3));
1233 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1236 @Test(groups = { "Functional" })
1237 public void testUpdatePDBIds()
1239 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1240 seq.addPDBId(pdbe1);
1241 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1242 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1243 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1244 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1245 // 7 is not a valid chain code:
1246 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1249 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1250 assertEquals(4, pdbIds.size());
1251 assertSame(pdbe1, pdbIds.get(0));
1252 // chain code got added to 3A6S:
1253 assertEquals("B", pdbe1.getChainCode());
1254 assertEquals("1A70", pdbIds.get(1).getId());
1255 // 4BQGA is parsed into id + chain
1256 assertEquals("4BQG", pdbIds.get(2).getId());
1257 assertEquals("a", pdbIds.get(2).getChainCode());
1258 assertEquals("2GIS7", pdbIds.get(3).getId());
1259 assertNull(pdbIds.get(3).getChainCode());
1263 * Test the method that either adds a pdbid or updates an existing one
1265 @Test(groups = { "Functional" })
1266 public void testAddPDBId()
1268 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1270 assertEquals(1, seq.getAllPDBEntries().size());
1271 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1272 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1274 // add the same entry
1276 assertEquals(1, seq.getAllPDBEntries().size());
1277 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1279 // add an identical entry
1280 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1281 assertEquals(1, seq.getAllPDBEntries().size());
1282 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1284 // add a different entry
1285 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1286 seq.addPDBId(pdbe2);
1287 assertEquals(2, seq.getAllPDBEntries().size());
1288 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1289 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1291 // update pdbe with chain code, file, type
1292 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1293 seq.addPDBId(pdbe3);
1294 assertEquals(2, seq.getAllPDBEntries().size());
1295 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1296 assertEquals("3A6S", pdbe.getId()); // unchanged
1297 assertEquals("A", pdbe.getChainCode()); // updated
1298 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1299 assertEquals("filepath", pdbe.getFile()); // updated
1300 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1302 // add with a different file path
1303 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1304 seq.addPDBId(pdbe4);
1305 assertEquals(3, seq.getAllPDBEntries().size());
1306 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1308 // add with a different chain code
1309 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1310 seq.addPDBId(pdbe5);
1311 assertEquals(4, seq.getAllPDBEntries().size());
1312 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1316 groups = { "Functional" },
1317 expectedExceptions = { IllegalArgumentException.class })
1318 public void testSetDatasetSequence_toSelf()
1320 seq.setDatasetSequence(seq);
1324 groups = { "Functional" },
1325 expectedExceptions = { IllegalArgumentException.class })
1326 public void testSetDatasetSequence_cascading()
1328 SequenceI seq2 = new Sequence("Seq2", "xyz");
1329 seq2.createDatasetSequence();
1330 seq.setDatasetSequence(seq2);
1333 @Test(groups = { "Functional" })
1334 public void testFindFeatures()
1336 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1337 sq.createDatasetSequence();
1339 assertTrue(sq.findFeatures(1, 99).isEmpty());
1341 // add non-positional feature
1342 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1344 sq.addSequenceFeature(sf0);
1345 // add feature on BCD
1346 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1348 sq.addSequenceFeature(sfBCD);
1349 // add feature on DE
1350 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1352 sq.addSequenceFeature(sfDE);
1353 // add contact feature at [B, H]
1354 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1355 "desc", 9, 15, 2f, null);
1356 sq.addSequenceFeature(sfContactBH);
1357 // add contact feature at [F, G]
1358 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1359 "desc", 13, 14, 2f, null);
1360 sq.addSequenceFeature(sfContactFG);
1361 // add single position feature at [I]
1362 SequenceFeature sfI = new SequenceFeature("Disulfide Bond",
1363 "desc", 16, 16, null);
1364 sq.addSequenceFeature(sfI);
1366 // no features in columns 1-2 (-A)
1367 List<SequenceFeature> found = sq.findFeatures(1, 2);
1368 assertTrue(found.isEmpty());
1370 // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1371 found = sq.findFeatures(1, 6);
1372 assertEquals(2, found.size());
1373 assertTrue(found.contains(sfBCD));
1374 assertTrue(found.contains(sfContactBH));
1376 // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1377 found = sq.findFeatures(5, 6);
1378 assertEquals(1, found.size());
1379 assertTrue(found.contains(sfBCD));
1381 // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1382 found = sq.findFeatures(7, 10);
1383 assertEquals(3, found.size());
1384 assertTrue(found.contains(sfBCD));
1385 assertTrue(found.contains(sfDE));
1386 assertTrue(found.contains(sfContactFG));
1388 // columns 10-11 (--) should find nothing
1389 found = sq.findFeatures(10, 11);
1390 assertEquals(0, found.size());
1392 // columns 14-14 (I) should find variant feature
1393 found = sq.findFeatures(14, 14);
1394 assertEquals(1, found.size());
1395 assertTrue(found.contains(sfI));
1398 @Test(groups = { "Functional" })
1399 public void testFindIndex_withCursor()
1401 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1404 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1407 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1410 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1413 @Test(groups = { "Functional" })
1414 public void testFindPosition_withCursor()
1416 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1418 // find F pos given A - lastCol gets set in cursor
1419 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1420 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1421 PA.getValue(sq, "cursor").toString());
1423 // find A pos given F - first residue column is saved in cursor
1424 assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1425 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
1426 PA.getValue(sq, "cursor").toString());
1428 // find C pos given C (neither startCol nor endCol is set)
1429 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1430 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1431 PA.getValue(sq, "cursor").toString());
1433 // now the grey area - what residue position for a gapped column? JAL-2562
1435 // find 'residue' for column 3 given cursor for D (so working left)
1436 // returns B9; cursor is updated to [B 5]
1437 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1438 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
1439 PA.getValue(sq, "cursor").toString());
1441 // find 'residue' for column 8 given cursor for D (so working right)
1442 // returns E12; cursor is updated to [D 7]
1443 assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1444 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
1445 PA.getValue(sq, "cursor").toString());
1447 // find 'residue' for column 12 given cursor for B
1448 // returns 1 more than last residue position; cursor is updated to [F 10]
1449 // lastCol position is saved in cursor
1450 assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1451 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1452 PA.getValue(sq, "cursor").toString());
1455 * findPosition for column beyond length of sequence
1456 * returns 1 more than the last residue position
1457 * cursor is set to last real residue position [F 10]
1459 assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
1460 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1461 PA.getValue(sq, "cursor").toString());
1464 * and the case without a trailing gap
1466 sq = new Sequence("test/8-13", "-A--BCD-EF");
1467 // first find C from A
1468 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
1469 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1470 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1472 // now 'find' 99 from C
1473 // cursor is set to [F 10] and saved lastCol
1474 assertEquals(14, sq.findPosition(99, cursor));
1475 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1476 PA.getValue(sq, "cursor").toString());
1480 public void testIsValidCursor()
1482 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1483 assertFalse(sq.isValidCursor(null));
1486 * cursor is valid if it has valid sequence ref and changeCount token
1487 * and positions within the range of the sequence
1489 int changeCount = (int) PA.getValue(sq, "changeCount");
1490 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1491 assertTrue(sq.isValidCursor(cursor));
1494 * column position outside [0 - length] is rejected
1496 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1497 assertFalse(sq.isValidCursor(cursor));
1498 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1499 assertFalse(sq.isValidCursor(cursor));
1500 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1501 assertFalse(sq.isValidCursor(cursor));
1502 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1503 assertFalse(sq.isValidCursor(cursor));
1506 * wrong sequence is rejected
1508 cursor = new SequenceCursor(null, 13, 1, changeCount);
1509 assertFalse(sq.isValidCursor(cursor));
1510 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1512 assertFalse(sq.isValidCursor(cursor));
1515 * wrong token value is rejected
1517 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1518 assertFalse(sq.isValidCursor(cursor));
1519 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1520 assertFalse(sq.isValidCursor(cursor));
1523 @Test(groups = { "Functional" })
1524 public void testFindPosition_withCursorAndEdits()
1526 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1528 // find F pos given A
1529 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1530 int token = (int) PA.getValue(sq, "changeCount"); // 0
1531 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1532 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1535 * setSequence should invalidate the cursor cached by the sequence
1537 sq.setSequence("-A-BCD-EF---"); // one gap removed
1538 assertEquals(8, sq.getStart()); // sanity check
1539 assertEquals(11, sq.findPosition(5)); // D11
1540 // cursor should now be at [D 6]
1541 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1542 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1545 * deleteChars should invalidate the cached cursor
1547 sq.deleteChars(2, 5); // delete -BC
1548 assertEquals("-AD-EF---", sq.getSequenceAsString());
1549 assertEquals(8, sq.getStart()); // sanity check
1550 assertEquals(10, sq.findPosition(4)); // E10
1551 // cursor should now be at [E 5]
1552 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1553 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1556 * Edit to insert gaps should invalidate the cached cursor
1557 * insert 2 gaps at column[3] to make -AD---EF---
1559 SequenceI[] seqs = new SequenceI[] { sq };
1560 AlignmentI al = new Alignment(seqs);
1561 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1562 assertEquals("-AD---EF---", sq.getSequenceAsString());
1563 assertEquals(10, sq.findPosition(4)); // E10
1564 // cursor should now be at [D 3]
1565 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1566 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1569 * insertCharAt should invalidate the cached cursor
1570 * insert CC at column[4] to make -AD-CC--EF---
1572 sq.insertCharAt(4, 2, 'C');
1573 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1574 assertEquals(13, sq.findPosition(9)); // F13
1575 // cursor should now be at [F 10]
1576 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1577 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1580 @Test(groups = { "Functional" })
1581 public void testGetSequence()
1583 String seqstring = "-A--BCD-EF--";
1584 Sequence sq = new Sequence("test/8-13", seqstring);
1585 sq.createDatasetSequence();
1586 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1587 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1588 "ABCDEF".toCharArray()));
1590 // verify a copy of the sequence array is returned
1591 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1592 assertNotSame(theSeq, sq.getSequence());
1593 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1594 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1597 @Test(groups = { "Functional" })
1598 public void testReplace()
1600 String seqstring = "-A--BCD-EF--";
1601 SequenceI sq = new Sequence("test/8-13", seqstring);
1602 assertEquals(0, PA.getValue(sq, "changeCount"));
1604 assertEquals(0, sq.replace('A', 'A')); // same char
1605 assertEquals(seqstring, sq.getSequenceAsString());
1606 assertEquals(0, PA.getValue(sq, "changeCount"));
1608 assertEquals(0, sq.replace('X', 'Y')); // not there
1609 assertEquals(seqstring, sq.getSequenceAsString());
1610 assertEquals(0, PA.getValue(sq, "changeCount"));
1612 assertEquals(1, sq.replace('A', 'K'));
1613 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1614 assertEquals(1, PA.getValue(sq, "changeCount"));
1616 assertEquals(6, sq.replace('-', '.'));
1617 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1618 assertEquals(2, PA.getValue(sq, "changeCount"));
1621 @Test(groups = { "Functional" })
1622 public void testFindPositions()
1624 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1629 assertNull(sq.findPositions(6, 5));
1630 assertNull(sq.findPositions(0, 5));
1631 assertNull(sq.findPositions(-1, 5));
1636 assertNull(sq.findPositions(1, 1)); // 1-based columns
1637 assertNull(sq.findPositions(5, 5));
1638 assertNull(sq.findPositions(5, 6));
1639 assertNull(sq.findPositions(5, 7));
1642 * all ungapped ranges
1644 assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
1645 assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
1646 assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
1647 assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
1650 * gap to ungapped range
1652 assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
1653 assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
1656 * ungapped to gapped range
1658 assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
1659 assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
1662 * ungapped to ungapped enclosing gaps
1664 assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
1665 assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
1668 * gapped to gapped enclosing ungapped
1670 assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
1671 assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
1672 assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
1673 assertEquals(new Range(8, 13), sq.findPositions(1, 99));