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));
266 @Test(groups = { "Functional" })
267 public void testFindPositions()
269 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
274 assertNull(sq.findPositions(6, 5));
275 assertNull(sq.findPositions(0, 5));
276 assertNull(sq.findPositions(-1, 5));
281 assertNull(sq.findPositions(1, 1)); // 1-based columns
282 assertNull(sq.findPositions(5, 5));
283 assertNull(sq.findPositions(5, 6));
284 assertNull(sq.findPositions(5, 7));
287 * all ungapped ranges
289 assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
290 assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
291 assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
292 assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
295 * gap to ungapped range
297 assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
298 assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
301 * ungapped to gapped range
303 assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
304 assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
307 * ungapped to ungapped enclosing gaps
309 assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
310 assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
313 * gapped to gapped enclosing ungapped
315 assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
316 assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
317 assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
318 assertEquals(new Range(8, 13), sq.findPositions(1, 99));
322 * Tests for the method that returns a dataset sequence position (start..) for
323 * an aligned column position (base 0).
325 @Test(groups = { "Functional" })
326 public void testFindPosition()
329 * call sequenceChanged() after each test to invalidate any cursor,
330 * forcing the 1-arg findPosition to be executed
332 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
333 assertEquals(8, sq.findPosition(0));
334 // Sequence should now hold a cursor at [8, 0]
335 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
336 PA.getValue(sq, "cursor").toString());
337 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
338 int token = (int) PA.getValue(sq, "changeCount");
339 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
341 sq.sequenceChanged();
344 * find F13 at column offset 5, cursor should update to [13, 6]
345 * endColumn is found and saved in cursor
347 assertEquals(13, sq.findPosition(5));
348 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
349 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
350 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
351 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok1",
352 PA.getValue(sq, "cursor").toString());
354 // assertEquals(-1, seq.findPosition(6)); // fails
356 sq = new Sequence("test/8-11", "AB-C-D--");
357 token = (int) PA.getValue(sq, "changeCount"); // 0
358 assertEquals(8, sq.findPosition(0));
359 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
360 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
361 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
362 PA.getValue(sq, "cursor").toString());
364 sq.sequenceChanged();
365 assertEquals(9, sq.findPosition(1));
366 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
367 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
368 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok1",
369 PA.getValue(sq, "cursor").toString());
371 sq.sequenceChanged();
372 // gap position 'finds' residue to the right (not the left as per javadoc)
373 // cursor is set to the last residue position found [B 2]
374 assertEquals(10, sq.findPosition(2));
375 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
376 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
377 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
378 PA.getValue(sq, "cursor").toString());
380 sq.sequenceChanged();
381 assertEquals(10, sq.findPosition(3));
382 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
383 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
384 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok3",
385 PA.getValue(sq, "cursor").toString());
387 sq.sequenceChanged();
388 // column[4] is the gap after C - returns D11
389 // cursor is set to [C 4]
390 assertEquals(11, sq.findPosition(4));
391 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
392 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
393 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
394 PA.getValue(sq, "cursor").toString());
396 sq.sequenceChanged();
397 assertEquals(11, sq.findPosition(5)); // D
398 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
399 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
400 // lastCol has been found and saved in the cursor
401 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok5",
402 PA.getValue(sq, "cursor").toString());
404 sq.sequenceChanged();
405 // returns 1 more than sequence length if off the end ?!?
406 assertEquals(12, sq.findPosition(6));
408 sq.sequenceChanged();
409 assertEquals(12, sq.findPosition(7));
412 * first findPosition should also set firstResCol in cursor
414 sq = new Sequence("test/8-13", "--AB-C-DEF--");
415 assertEquals(8, sq.findPosition(0));
416 assertNull(PA.getValue(sq, "cursor"));
418 sq.sequenceChanged();
419 assertEquals(8, sq.findPosition(1));
420 assertNull(PA.getValue(sq, "cursor"));
422 sq.sequenceChanged();
423 assertEquals(8, sq.findPosition(2));
424 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok2",
425 PA.getValue(sq, "cursor").toString());
427 sq.sequenceChanged();
428 assertEquals(9, sq.findPosition(3));
429 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok3",
430 PA.getValue(sq, "cursor").toString());
432 sq.sequenceChanged();
433 // column[4] is a gap, returns next residue pos (C10)
434 // cursor is set to last residue found [B]
435 assertEquals(10, sq.findPosition(4));
436 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
437 PA.getValue(sq, "cursor").toString());
439 sq.sequenceChanged();
440 assertEquals(10, sq.findPosition(5));
441 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok5",
442 PA.getValue(sq, "cursor").toString());
444 sq.sequenceChanged();
445 // column[6] is a gap, returns next residue pos (D11)
446 // cursor is set to last residue found [C]
447 assertEquals(11, sq.findPosition(6));
448 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
449 PA.getValue(sq, "cursor").toString());
451 sq.sequenceChanged();
452 assertEquals(11, sq.findPosition(7));
453 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok7",
454 PA.getValue(sq, "cursor").toString());
456 sq.sequenceChanged();
457 assertEquals(12, sq.findPosition(8));
458 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok8",
459 PA.getValue(sq, "cursor").toString());
462 * when the last residue column is found, it is set in the cursor
464 sq.sequenceChanged();
465 assertEquals(13, sq.findPosition(9));
466 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok9",
467 PA.getValue(sq, "cursor").toString());
469 sq.sequenceChanged();
470 assertEquals(14, sq.findPosition(10));
471 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
472 PA.getValue(sq, "cursor").toString());
475 * findPosition for column beyond sequence length
476 * returns 1 more than last residue position
478 sq.sequenceChanged();
479 assertEquals(14, sq.findPosition(11));
480 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
481 PA.getValue(sq, "cursor").toString());
483 sq.sequenceChanged();
484 assertEquals(14, sq.findPosition(99));
485 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
486 PA.getValue(sq, "cursor").toString());
489 * gapped sequence ending in non-gap
491 sq = new Sequence("test/8-13", "--AB-C-DEF");
492 assertEquals(13, sq.findPosition(9));
493 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok0",
494 PA.getValue(sq, "cursor").toString());
495 sq.sequenceChanged();
496 assertEquals(12, sq.findPosition(8)); // E12
497 // sequenceChanged() invalidates cursor.lastResidueColumn
498 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
499 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok1",
501 // findPosition with cursor accepts base 1 column values
502 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
503 assertEquals(13, sq.findPosition(9)); // F13
504 // lastResidueColumn has now been found and saved in cursor
505 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
506 PA.getValue(sq, "cursor").toString());
509 @Test(groups = { "Functional" })
510 public void testDeleteChars()
515 SequenceI sq = new Sequence("test", "ABCDEF");
516 assertNull(PA.getValue(sq, "datasetSequence"));
517 assertEquals(1, sq.getStart());
518 assertEquals(6, sq.getEnd());
519 sq.deleteChars(2, 3);
520 assertEquals("ABDEF", sq.getSequenceAsString());
521 assertEquals(1, sq.getStart());
522 assertEquals(5, sq.getEnd());
523 assertNull(PA.getValue(sq, "datasetSequence"));
528 sq = new Sequence("test", "ABCDEF");
529 sq.deleteChars(0, 2);
530 assertEquals("CDEF", sq.getSequenceAsString());
531 assertEquals(3, sq.getStart());
532 assertEquals(6, sq.getEnd());
533 assertNull(PA.getValue(sq, "datasetSequence"));
535 sq = new Sequence("test", "ABCDE");
536 sq.deleteChars(0, 3);
537 assertEquals("DE", sq.getSequenceAsString());
538 assertEquals(4, sq.getStart());
539 assertEquals(5, sq.getEnd());
540 assertNull(PA.getValue(sq, "datasetSequence"));
545 sq = new Sequence("test", "ABCDEF");
546 sq.deleteChars(4, 6);
547 assertEquals("ABCD", sq.getSequenceAsString());
548 assertEquals(1, sq.getStart());
549 assertEquals(4, sq.getEnd());
550 assertNull(PA.getValue(sq, "datasetSequence"));
553 * delete more positions than there are
555 sq = new Sequence("test/8-11", "ABCD");
556 sq.deleteChars(0, 99);
557 assertEquals("", sq.getSequenceAsString());
558 assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
559 assertEquals(11, sq.getEnd());
561 sq = new Sequence("test/8-11", "----");
562 sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
563 assertEquals("", sq.getSequenceAsString());
564 assertEquals(8, sq.getStart());
565 assertEquals(11, sq.getEnd());
568 @Test(groups = { "Functional" })
569 public void testDeleteChars_withDbRefsAndFeatures()
572 * internal delete - new dataset sequence created
573 * gets a copy of any dbrefs
575 SequenceI sq = new Sequence("test", "ABCDEF");
576 sq.createDatasetSequence();
577 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
579 Object ds = PA.getValue(sq, "datasetSequence");
581 assertEquals(1, sq.getStart());
582 assertEquals(6, sq.getEnd());
583 sq.deleteChars(2, 3);
584 assertEquals("ABDEF", sq.getSequenceAsString());
585 assertEquals(1, sq.getStart());
586 assertEquals(5, sq.getEnd());
587 Object newDs = PA.getValue(sq, "datasetSequence");
588 assertNotNull(newDs);
589 assertNotSame(ds, newDs);
590 assertNotNull(sq.getDBRefs());
591 assertEquals(1, sq.getDBRefs().length);
592 assertNotSame(dbr1, sq.getDBRefs()[0]);
593 assertEquals(dbr1, sq.getDBRefs()[0]);
596 * internal delete with sequence features
597 * (failure case for JAL-2541)
599 sq = new Sequence("test", "ABCDEF");
600 sq.createDatasetSequence();
601 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
603 sq.addSequenceFeature(sf1);
604 ds = PA.getValue(sq, "datasetSequence");
606 assertEquals(1, sq.getStart());
607 assertEquals(6, sq.getEnd());
608 sq.deleteChars(2, 4);
609 assertEquals("ABEF", sq.getSequenceAsString());
610 assertEquals(1, sq.getStart());
611 assertEquals(4, sq.getEnd());
612 newDs = PA.getValue(sq, "datasetSequence");
613 assertNotNull(newDs);
614 assertNotSame(ds, newDs);
615 List<SequenceFeature> sfs = sq.getSequenceFeatures();
616 assertEquals(1, sfs.size());
617 assertNotSame(sf1, sfs.get(0));
618 assertEquals(sf1, sfs.get(0));
621 * delete at start - no new dataset sequence created
622 * any sequence features remain as before
624 sq = new Sequence("test", "ABCDEF");
625 sq.createDatasetSequence();
626 ds = PA.getValue(sq, "datasetSequence");
627 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
628 sq.addSequenceFeature(sf1);
629 sq.deleteChars(0, 2);
630 assertEquals("CDEF", sq.getSequenceAsString());
631 assertEquals(3, sq.getStart());
632 assertEquals(6, sq.getEnd());
633 assertSame(ds, PA.getValue(sq, "datasetSequence"));
634 sfs = sq.getSequenceFeatures();
636 assertEquals(1, sfs.size());
637 assertSame(sf1, sfs.get(0));
640 * delete at end - no new dataset sequence created
641 * any dbrefs remain as before
643 sq = new Sequence("test", "ABCDEF");
644 sq.createDatasetSequence();
645 ds = PA.getValue(sq, "datasetSequence");
646 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
648 sq.deleteChars(4, 6);
649 assertEquals("ABCD", sq.getSequenceAsString());
650 assertEquals(1, sq.getStart());
651 assertEquals(4, sq.getEnd());
652 assertSame(ds, PA.getValue(sq, "datasetSequence"));
653 assertNotNull(sq.getDBRefs());
654 assertEquals(1, sq.getDBRefs().length);
655 assertSame(dbr1, sq.getDBRefs()[0]);
658 @Test(groups = { "Functional" })
659 public void testInsertCharAt()
661 // non-static methods:
662 SequenceI sq = new Sequence("test", "ABCDEF");
663 sq.insertCharAt(0, 'z');
664 assertEquals("zABCDEF", sq.getSequenceAsString());
665 sq.insertCharAt(2, 2, 'x');
666 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
668 // for static method see StringUtilsTest
672 * Test the method that returns an array of aligned sequence positions where
673 * the array index is the data sequence position (both base 0).
675 @Test(groups = { "Functional" })
676 public void testGapMap()
678 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
679 sq.createDatasetSequence();
680 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
684 * Test the method that gets sequence features, either from the sequence or
687 @Test(groups = { "Functional" })
688 public void testGetSequenceFeatures()
690 SequenceI sq = new Sequence("test", "GATCAT");
691 sq.createDatasetSequence();
693 assertTrue(sq.getSequenceFeatures().isEmpty());
696 * SequenceFeature on sequence
698 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
699 sq.addSequenceFeature(sf);
700 List<SequenceFeature> sfs = sq.getSequenceFeatures();
701 assertEquals(1, sfs.size());
702 assertSame(sf, sfs.get(0));
705 * SequenceFeature on sequence and dataset sequence; returns that on
708 * Note JAL-2046: spurious: we have no use case for this at the moment.
709 * This test also buggy - as sf2.equals(sf), no new feature is added
711 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
713 sq.getDatasetSequence().addSequenceFeature(sf2);
714 sfs = sq.getSequenceFeatures();
715 assertEquals(1, sfs.size());
716 assertSame(sf, sfs.get(0));
719 * SequenceFeature on dataset sequence only
720 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
722 sq.setSequenceFeatures(null);
723 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
726 * Corrupt case - no SequenceFeature, dataset's dataset is the original
727 * sequence. Test shows no infinite loop results.
729 sq.getDatasetSequence().setSequenceFeatures(null);
731 * is there a usecase for this ? setDatasetSequence should throw an error if
732 * this actually occurs.
736 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
737 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
738 } catch (IllegalArgumentException e)
740 // TODO Jalview error/exception class for raising implementation errors
741 assertTrue(e.getMessage().toLowerCase()
742 .contains("implementation error"));
744 assertTrue(sq.getSequenceFeatures().isEmpty());
748 * Test the method that returns an array, indexed by sequence position, whose
749 * entries are the residue positions at the sequence position (or to the right
752 @Test(groups = { "Functional" })
753 public void testFindPositionMap()
756 * Note: Javadoc for findPosition says it returns the residue position to
757 * the left of a gapped position; in fact it returns the position to the
758 * right. Also it returns a non-existent residue position for a gap beyond
761 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
762 int[] map = sq.findPositionMap();
763 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
764 Arrays.toString(map));
768 * Test for getSubsequence
770 @Test(groups = { "Functional" })
771 public void testGetSubsequence()
773 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
774 sq.createDatasetSequence();
776 // positions are base 0, end position is exclusive
777 SequenceI subseq = sq.getSubSequence(2, 4);
779 assertEquals("CD", subseq.getSequenceAsString());
780 // start/end are base 1 positions
781 assertEquals(3, subseq.getStart());
782 assertEquals(4, subseq.getEnd());
783 // subsequence shares the full dataset sequence
784 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
788 * test createDatasetSequence behaves to doc
790 @Test(groups = { "Functional" })
791 public void testCreateDatasetSequence()
793 SequenceI sq = new Sequence("my", "ASDASD");
794 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
796 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
797 assertNull(sq.getDatasetSequence());
798 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
799 assertNotNull(PA.getValue(sq, "dbrefs"));
801 SequenceI rds = sq.createDatasetSequence();
803 assertNull(rds.getDatasetSequence());
804 assertSame(sq.getDatasetSequence(), rds);
806 // sequence features and dbrefs transferred to dataset sequence
807 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
808 assertNull(PA.getValue(sq, "dbrefs"));
809 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
810 assertNotNull(PA.getValue(rds, "dbrefs"));
814 * Test for deriveSequence applied to a sequence with a dataset
816 @Test(groups = { "Functional" })
817 public void testDeriveSequence_existingDataset()
819 Sequence sq = new Sequence("Seq1", "CD");
820 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
821 sq.getDatasetSequence().addSequenceFeature(
822 new SequenceFeature("", "", 1, 2, 0f, null));
826 sq.setDescription("Test sequence description..");
827 sq.setVamsasId("TestVamsasId");
828 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
830 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
831 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
832 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
833 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
835 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
836 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
837 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
838 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
840 // these are the same as ones already added
841 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
842 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
844 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
847 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
848 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
849 sq.getDatasetSequence().addDBRef(
850 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
851 sq.getDatasetSequence().addDBRef(
852 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
854 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
855 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
856 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
858 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
860 sq.getDatasetSequence().addPDBId(pdbe1a);
861 sq.getDatasetSequence().addPDBId(pdbe1b);
862 sq.getDatasetSequence().addPDBId(pdbe2a);
863 sq.getDatasetSequence().addPDBId(pdbe2b);
866 * test we added pdb entries to the dataset sequence
868 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
869 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
870 "PDB Entries were not found on dataset sequence.");
873 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
875 Assert.assertEquals(pdbe1a,
876 sq.getDatasetSequence().getPDBEntry("1PDB"),
877 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
878 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
879 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
880 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
881 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
882 Annotation[] annots = annotsList.toArray(new Annotation[0]);
883 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
884 "Test annot description", annots));
885 sq.getDatasetSequence().addAlignmentAnnotation(
886 new AlignmentAnnotation("Test annot", "Test annot description",
888 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
889 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
891 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
892 Assert.assertNotNull(sq.getAnnotation());
893 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
894 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
897 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
899 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
901 Sequence derived = (Sequence) sq.deriveSequence();
903 Assert.assertEquals(derived.getDescription(),
904 "Test sequence description..");
905 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
906 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
907 Assert.assertNotNull(derived.getAnnotation());
908 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
909 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
910 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
912 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
914 assertEquals("CD", derived.getSequenceAsString());
915 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
917 // derived sequence should access dataset sequence features
918 assertNotNull(sq.getSequenceFeatures());
919 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
922 * verify we have primary db refs *just* for PDB IDs with associated
926 assertEquals(primRefs, sq.getPrimaryDBRefs());
927 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
929 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
934 * Test for deriveSequence applied to an ungapped sequence with no dataset
936 @Test(groups = { "Functional" })
937 public void testDeriveSequence_noDatasetUngapped()
939 SequenceI sq = new Sequence("Seq1", "ABCDEF");
940 assertEquals(1, sq.getStart());
941 assertEquals(6, sq.getEnd());
942 SequenceI derived = sq.deriveSequence();
943 assertEquals("ABCDEF", derived.getSequenceAsString());
944 assertEquals("ABCDEF", derived.getDatasetSequence()
945 .getSequenceAsString());
949 * Test for deriveSequence applied to a gapped sequence with no dataset
951 @Test(groups = { "Functional" })
952 public void testDeriveSequence_noDatasetGapped()
954 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
955 assertEquals(1, sq.getStart());
956 assertEquals(6, sq.getEnd());
957 assertNull(sq.getDatasetSequence());
958 SequenceI derived = sq.deriveSequence();
959 assertEquals("AB-C.D EF", derived.getSequenceAsString());
960 assertEquals("ABCDEF", derived.getDatasetSequence()
961 .getSequenceAsString());
964 @Test(groups = { "Functional" })
965 public void testCopyConstructor_noDataset()
967 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
968 seq1.setDescription("description");
969 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
971 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
973 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
974 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
976 SequenceI copy = new Sequence(seq1);
978 assertNull(copy.getDatasetSequence());
980 verifyCopiedSequence(seq1, copy);
982 // copy has a copy of the DBRefEntry
983 // this is murky - DBrefs are only copied for dataset sequences
984 // where the test for 'dataset sequence' is 'dataset is null'
985 // but that doesn't distinguish it from an aligned sequence
986 // which has not yet generated a dataset sequence
987 // NB getDBRef looks inside dataset sequence if not null
988 DBRefEntry[] dbrefs = copy.getDBRefs();
989 assertEquals(1, dbrefs.length);
990 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
991 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
994 @Test(groups = { "Functional" })
995 public void testCopyConstructor_withDataset()
997 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
998 seq1.createDatasetSequence();
999 seq1.setDescription("description");
1000 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
1002 // JAL-2046 - what is the contract for using a derived sequence's
1003 // addSequenceFeature ?
1004 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
1006 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1007 // here we add DBRef to the dataset sequence:
1008 seq1.getDatasetSequence().addDBRef(
1009 new DBRefEntry("EMBL", "1.2", "AZ12345"));
1011 SequenceI copy = new Sequence(seq1);
1013 assertNotNull(copy.getDatasetSequence());
1014 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1016 verifyCopiedSequence(seq1, copy);
1018 // getDBRef looks inside dataset sequence and this is shared,
1019 // so holds the same dbref objects
1020 DBRefEntry[] dbrefs = copy.getDBRefs();
1021 assertEquals(1, dbrefs.length);
1022 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
1026 * Helper to make assertions about a copied sequence
1031 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1033 // verify basic properties:
1034 assertEquals(copy.getName(), seq1.getName());
1035 assertEquals(copy.getDescription(), seq1.getDescription());
1036 assertEquals(copy.getStart(), seq1.getStart());
1037 assertEquals(copy.getEnd(), seq1.getEnd());
1038 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1040 // copy has a copy of the annotation:
1041 AlignmentAnnotation[] anns = copy.getAnnotation();
1042 assertEquals(1, anns.length);
1043 assertFalse(anns[0] == seq1.getAnnotation()[0]);
1044 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1045 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1046 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1048 // copy has a copy of the sequence feature:
1049 List<SequenceFeature> sfs = copy.getSequenceFeatures();
1050 assertEquals(1, sfs.size());
1051 if (seq1.getDatasetSequence() != null
1052 && copy.getDatasetSequence() == seq1.getDatasetSequence())
1054 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1058 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1060 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1062 // copy has a copy of the PDB entry
1063 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1064 assertEquals(1, pdbs.size());
1065 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1066 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1069 @Test(groups = "Functional")
1070 public void testGetCharAt()
1072 SequenceI sq = new Sequence("", "abcde");
1073 assertEquals('a', sq.getCharAt(0));
1074 assertEquals('e', sq.getCharAt(4));
1075 assertEquals(' ', sq.getCharAt(5));
1076 assertEquals(' ', sq.getCharAt(-1));
1079 @Test(groups = { "Functional" })
1080 public void testAddSequenceFeatures()
1082 SequenceI sq = new Sequence("", "abcde");
1083 // type may not be null
1084 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
1086 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1088 // can't add a duplicate feature
1089 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
1091 // can add a different feature
1092 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
1093 8, 0f, null))); // different type
1094 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
1095 "description", 4, 8, 0f, null)));// different description
1096 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
1097 8, 0f, null))); // different start position
1098 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1099 9, 0f, null))); // different end position
1100 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1101 8, 1f, null))); // different score
1102 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1103 8, Float.NaN, null))); // score NaN
1104 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1105 8, 0f, "Metal"))); // different group
1106 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1110 * Tests for adding (or updating) dbrefs
1112 * @see DBRefEntry#updateFrom(DBRefEntry)
1114 @Test(groups = { "Functional" })
1115 public void testAddDBRef()
1117 SequenceI sq = new Sequence("", "abcde");
1118 assertNull(sq.getDBRefs());
1119 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1121 assertEquals(1, sq.getDBRefs().length);
1122 assertSame(dbref, sq.getDBRefs()[0]);
1125 * change of version - new entry
1127 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1128 sq.addDBRef(dbref2);
1129 assertEquals(2, sq.getDBRefs().length);
1130 assertSame(dbref, sq.getDBRefs()[0]);
1131 assertSame(dbref2, sq.getDBRefs()[1]);
1134 * matches existing entry - not added
1136 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1137 assertEquals(2, sq.getDBRefs().length);
1140 * different source = new entry
1142 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1143 sq.addDBRef(dbref3);
1144 assertEquals(3, sq.getDBRefs().length);
1145 assertSame(dbref3, sq.getDBRefs()[2]);
1148 * different ref = new entry
1150 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1151 sq.addDBRef(dbref4);
1152 assertEquals(4, sq.getDBRefs().length);
1153 assertSame(dbref4, sq.getDBRefs()[3]);
1156 * matching ref with a mapping - map updated
1158 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1159 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1162 sq.addDBRef(dbref5);
1163 assertEquals(4, sq.getDBRefs().length);
1164 assertSame(dbref4, sq.getDBRefs()[3]);
1165 assertSame(map, dbref4.getMap());
1168 * 'real' version replaces "0" version
1170 dbref2.setVersion("0");
1171 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1172 dbref2.getAccessionId());
1173 sq.addDBRef(dbref6);
1174 assertEquals(4, sq.getDBRefs().length);
1175 assertSame(dbref2, sq.getDBRefs()[1]);
1176 assertEquals("3", dbref2.getVersion());
1179 * 'real' version replaces "source:0" version
1181 dbref3.setVersion("Uniprot:0");
1182 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1183 dbref3.getAccessionId());
1184 sq.addDBRef(dbref7);
1185 assertEquals(4, sq.getDBRefs().length);
1186 assertSame(dbref3, sq.getDBRefs()[2]);
1187 assertEquals("3", dbref2.getVersion());
1190 @Test(groups = { "Functional" })
1191 public void testGetPrimaryDBRefs_peptide()
1193 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1196 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1197 assertTrue(primaryDBRefs.isEmpty());
1200 sq.setDBRefs(new DBRefEntry[] {});
1201 primaryDBRefs = sq.getPrimaryDBRefs();
1202 assertTrue(primaryDBRefs.isEmpty());
1204 // primary - uniprot
1205 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1206 sq.addDBRef(upentry1);
1208 // primary - uniprot with congruent map
1209 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1210 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1211 new int[] { 10, 22 }, 1, 1)));
1212 sq.addDBRef(upentry2);
1214 // primary - uniprot with map of enclosing sequence
1215 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1216 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1217 new int[] { 8, 24 }, 1, 1)));
1218 sq.addDBRef(upentry3);
1220 // not primary - uniprot with map of sub-sequence (5')
1221 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1222 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1223 new int[] { 10, 18 }, 1, 1)));
1224 sq.addDBRef(upentry4);
1226 // not primary - uniprot with map that overlaps 3'
1227 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1228 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1229 new int[] { 12, 22 }, 1, 1)));
1230 sq.addDBRef(upentry5);
1232 // not primary - uniprot with map to different coordinates frame
1233 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1234 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1235 new int[] { 112, 118 }, 1, 1)));
1236 sq.addDBRef(upentry6);
1238 // not primary - dbref to 'non-core' database
1239 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1240 sq.addDBRef(upentry7);
1242 // primary - type is PDB
1243 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1244 sq.addDBRef(pdbentry);
1246 // not primary - PDBEntry has no file
1247 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1249 // not primary - no PDBEntry
1250 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1252 // add corroborating PDB entry for primary DBref -
1253 // needs to have a file as well as matching ID
1254 // note PDB ID is not treated as case sensitive
1255 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1258 // not valid DBRef - no file..
1259 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1261 primaryDBRefs = sq.getPrimaryDBRefs();
1262 assertEquals(4, primaryDBRefs.size());
1263 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1264 primaryDBRefs.contains(upentry1));
1265 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1266 primaryDBRefs.contains(upentry2));
1267 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1268 primaryDBRefs.contains(upentry3));
1269 assertTrue("Couldn't find expected PDB primary reference",
1270 primaryDBRefs.contains(pdbentry));
1273 @Test(groups = { "Functional" })
1274 public void testGetPrimaryDBRefs_nucleotide()
1276 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1278 // primary - Ensembl
1279 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1282 // not primary - Ensembl 'transcript' mapping of sub-sequence
1283 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1284 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1285 new int[] { 1, 11 }, 1, 1)));
1288 // primary - EMBL with congruent map
1289 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1290 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1291 new int[] { 10, 34 }, 1, 1)));
1294 // not primary - to non-core database
1295 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1298 // not primary - to protein
1299 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1302 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1303 assertEquals(2, primaryDBRefs.size());
1304 assertTrue(primaryDBRefs.contains(dbr1));
1305 assertTrue(primaryDBRefs.contains(dbr3));
1309 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1312 @Test(groups = { "Functional" })
1313 public void testUpdatePDBIds()
1315 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1316 seq.addPDBId(pdbe1);
1317 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1318 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1319 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1320 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1321 // 7 is not a valid chain code:
1322 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1325 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1326 assertEquals(4, pdbIds.size());
1327 assertSame(pdbe1, pdbIds.get(0));
1328 // chain code got added to 3A6S:
1329 assertEquals("B", pdbe1.getChainCode());
1330 assertEquals("1A70", pdbIds.get(1).getId());
1331 // 4BQGA is parsed into id + chain
1332 assertEquals("4BQG", pdbIds.get(2).getId());
1333 assertEquals("a", pdbIds.get(2).getChainCode());
1334 assertEquals("2GIS7", pdbIds.get(3).getId());
1335 assertNull(pdbIds.get(3).getChainCode());
1339 * Test the method that either adds a pdbid or updates an existing one
1341 @Test(groups = { "Functional" })
1342 public void testAddPDBId()
1344 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1346 assertEquals(1, seq.getAllPDBEntries().size());
1347 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1348 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1350 // add the same entry
1352 assertEquals(1, seq.getAllPDBEntries().size());
1353 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1355 // add an identical entry
1356 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1357 assertEquals(1, seq.getAllPDBEntries().size());
1358 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1360 // add a different entry
1361 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1362 seq.addPDBId(pdbe2);
1363 assertEquals(2, seq.getAllPDBEntries().size());
1364 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1365 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1367 // update pdbe with chain code, file, type
1368 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1369 seq.addPDBId(pdbe3);
1370 assertEquals(2, seq.getAllPDBEntries().size());
1371 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1372 assertEquals("3A6S", pdbe.getId()); // unchanged
1373 assertEquals("A", pdbe.getChainCode()); // updated
1374 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1375 assertEquals("filepath", pdbe.getFile()); // updated
1376 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1378 // add with a different file path
1379 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1380 seq.addPDBId(pdbe4);
1381 assertEquals(3, seq.getAllPDBEntries().size());
1382 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1384 // add with a different chain code
1385 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1386 seq.addPDBId(pdbe5);
1387 assertEquals(4, seq.getAllPDBEntries().size());
1388 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1392 groups = { "Functional" },
1393 expectedExceptions = { IllegalArgumentException.class })
1394 public void testSetDatasetSequence_toSelf()
1396 seq.setDatasetSequence(seq);
1400 groups = { "Functional" },
1401 expectedExceptions = { IllegalArgumentException.class })
1402 public void testSetDatasetSequence_cascading()
1404 SequenceI seq2 = new Sequence("Seq2", "xyz");
1405 seq2.createDatasetSequence();
1406 seq.setDatasetSequence(seq2);
1409 @Test(groups = { "Functional" })
1410 public void testFindFeatures()
1412 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1413 sq.createDatasetSequence();
1415 assertTrue(sq.findFeatures(1, 99).isEmpty());
1417 // add non-positional feature
1418 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1420 sq.addSequenceFeature(sf0);
1421 // add feature on BCD
1422 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1424 sq.addSequenceFeature(sfBCD);
1425 // add feature on DE
1426 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1428 sq.addSequenceFeature(sfDE);
1429 // add contact feature at [B, H]
1430 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1431 "desc", 9, 15, 2f, null);
1432 sq.addSequenceFeature(sfContactBH);
1433 // add contact feature at [F, G]
1434 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1435 "desc", 13, 14, 2f, null);
1436 sq.addSequenceFeature(sfContactFG);
1437 // add single position feature at [I]
1438 SequenceFeature sfI = new SequenceFeature("Disulfide Bond",
1439 "desc", 16, 16, null);
1440 sq.addSequenceFeature(sfI);
1442 // no features in columns 1-2 (-A)
1443 List<SequenceFeature> found = sq.findFeatures(1, 2);
1444 assertTrue(found.isEmpty());
1446 // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1447 found = sq.findFeatures(1, 6);
1448 assertEquals(2, found.size());
1449 assertTrue(found.contains(sfBCD));
1450 assertTrue(found.contains(sfContactBH));
1452 // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1453 found = sq.findFeatures(5, 6);
1454 assertEquals(1, found.size());
1455 assertTrue(found.contains(sfBCD));
1457 // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1458 found = sq.findFeatures(7, 10);
1459 assertEquals(3, found.size());
1460 assertTrue(found.contains(sfBCD));
1461 assertTrue(found.contains(sfDE));
1462 assertTrue(found.contains(sfContactFG));
1464 // columns 10-11 (--) should find nothing
1465 found = sq.findFeatures(10, 11);
1466 assertEquals(0, found.size());
1468 // columns 14-14 (I) should find variant feature
1469 found = sq.findFeatures(14, 14);
1470 assertEquals(1, found.size());
1471 assertTrue(found.contains(sfI));
1474 @Test(groups = { "Functional" })
1475 public void testFindIndex_withCursor()
1477 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1480 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1483 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1486 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1489 @Test(groups = { "Functional" })
1490 public void testFindPosition_withCursor()
1492 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1494 // find F pos given A - lastCol gets set in cursor
1495 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1496 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1497 PA.getValue(sq, "cursor").toString());
1499 // find A pos given F - first residue column is saved in cursor
1500 assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1501 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
1502 PA.getValue(sq, "cursor").toString());
1504 // find C pos given C (neither startCol nor endCol is set)
1505 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1506 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1507 PA.getValue(sq, "cursor").toString());
1509 // now the grey area - what residue position for a gapped column? JAL-2562
1511 // find 'residue' for column 3 given cursor for D (so working left)
1512 // returns B9; cursor is updated to [B 5]
1513 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1514 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
1515 PA.getValue(sq, "cursor").toString());
1517 // find 'residue' for column 8 given cursor for D (so working right)
1518 // returns E12; cursor is updated to [D 7]
1519 assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1520 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
1521 PA.getValue(sq, "cursor").toString());
1523 // find 'residue' for column 12 given cursor for B
1524 // returns 1 more than last residue position; cursor is updated to [F 10]
1525 // lastCol position is saved in cursor
1526 assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1527 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1528 PA.getValue(sq, "cursor").toString());
1531 * findPosition for column beyond length of sequence
1532 * returns 1 more than the last residue position
1533 * cursor is set to last real residue position [F 10]
1535 assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
1536 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1537 PA.getValue(sq, "cursor").toString());
1540 * and the case without a trailing gap
1542 sq = new Sequence("test/8-13", "-A--BCD-EF");
1543 // first find C from A
1544 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
1545 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1546 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1548 // now 'find' 99 from C
1549 // cursor is set to [F 10] and saved lastCol
1550 assertEquals(14, sq.findPosition(99, cursor));
1551 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1552 PA.getValue(sq, "cursor").toString());
1556 public void testIsValidCursor()
1558 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1559 assertFalse(sq.isValidCursor(null));
1562 * cursor is valid if it has valid sequence ref and changeCount token
1563 * and positions within the range of the sequence
1565 int changeCount = (int) PA.getValue(sq, "changeCount");
1566 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1567 assertTrue(sq.isValidCursor(cursor));
1570 * column position outside [0 - length] is rejected
1572 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1573 assertFalse(sq.isValidCursor(cursor));
1574 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1575 assertFalse(sq.isValidCursor(cursor));
1576 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1577 assertFalse(sq.isValidCursor(cursor));
1578 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1579 assertFalse(sq.isValidCursor(cursor));
1582 * wrong sequence is rejected
1584 cursor = new SequenceCursor(null, 13, 1, changeCount);
1585 assertFalse(sq.isValidCursor(cursor));
1586 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1588 assertFalse(sq.isValidCursor(cursor));
1591 * wrong token value is rejected
1593 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1594 assertFalse(sq.isValidCursor(cursor));
1595 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1596 assertFalse(sq.isValidCursor(cursor));
1599 @Test(groups = { "Functional" })
1600 public void testFindPosition_withCursorAndEdits()
1602 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1604 // find F pos given A
1605 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1606 int token = (int) PA.getValue(sq, "changeCount"); // 0
1607 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1608 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1611 * setSequence should invalidate the cursor cached by the sequence
1613 sq.setSequence("-A-BCD-EF---"); // one gap removed
1614 assertEquals(8, sq.getStart()); // sanity check
1615 assertEquals(11, sq.findPosition(5)); // D11
1616 // cursor should now be at [D 6]
1617 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1618 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1619 assertEquals(0, cursor.lastColumnPosition); // not yet found
1620 assertEquals(13, sq.findPosition(8)); // E13
1621 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1622 assertEquals(9, cursor.lastColumnPosition); // found
1625 * deleteChars should invalidate the cached cursor
1627 sq.deleteChars(2, 5); // delete -BC
1628 assertEquals("-AD-EF---", sq.getSequenceAsString());
1629 assertEquals(8, sq.getStart()); // sanity check
1630 assertEquals(10, sq.findPosition(4)); // E10
1631 // cursor should now be at [E 5]
1632 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1633 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1636 * Edit to insert gaps should invalidate the cached cursor
1637 * insert 2 gaps at column[3] to make -AD---EF---
1639 SequenceI[] seqs = new SequenceI[] { sq };
1640 AlignmentI al = new Alignment(seqs);
1641 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1642 assertEquals("-AD---EF---", sq.getSequenceAsString());
1643 assertEquals(10, sq.findPosition(4)); // E10
1644 // cursor should now be at [D 3]
1645 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1646 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1649 * insertCharAt should invalidate the cached cursor
1650 * insert CC at column[4] to make -AD-CC--EF---
1652 sq.insertCharAt(4, 2, 'C');
1653 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1654 assertEquals(13, sq.findPosition(9)); // F13
1655 // cursor should now be at [F 10]
1656 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1657 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1660 @Test(groups = { "Functional" })
1661 public void testGetSequence()
1663 String seqstring = "-A--BCD-EF--";
1664 Sequence sq = new Sequence("test/8-13", seqstring);
1665 sq.createDatasetSequence();
1666 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1667 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1668 "ABCDEF".toCharArray()));
1670 // verify a copy of the sequence array is returned
1671 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1672 assertNotSame(theSeq, sq.getSequence());
1673 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1674 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1677 @Test(groups = { "Functional" })
1678 public void testReplace()
1680 String seqstring = "-A--BCD-EF--";
1681 SequenceI sq = new Sequence("test/8-13", seqstring);
1682 assertEquals(0, PA.getValue(sq, "changeCount"));
1684 assertEquals(0, sq.replace('A', 'A')); // same char
1685 assertEquals(seqstring, sq.getSequenceAsString());
1686 assertEquals(0, PA.getValue(sq, "changeCount"));
1688 assertEquals(0, sq.replace('X', 'Y')); // not there
1689 assertEquals(seqstring, sq.getSequenceAsString());
1690 assertEquals(0, PA.getValue(sq, "changeCount"));
1692 assertEquals(1, sq.replace('A', 'K'));
1693 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1694 assertEquals(1, PA.getValue(sq, "changeCount"));
1696 assertEquals(6, sq.replace('-', '.'));
1697 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1698 assertEquals(2, PA.getValue(sq, "changeCount"));