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.analysis.AlignmentGenerator;
32 import jalview.commands.EditCommand;
33 import jalview.commands.EditCommand.Action;
34 import jalview.datamodel.PDBEntry.Type;
35 import jalview.gui.JvOptionPane;
36 import jalview.util.MapList;
37 import jalview.ws.params.InvalidArgumentException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.BitSet;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Vector;
47 import org.testng.Assert;
48 import org.testng.annotations.BeforeClass;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.Test;
52 import junit.extensions.PA;
54 public class SequenceTest
56 @BeforeClass(alwaysRun = true)
57 public void setUpJvOptionPane()
59 JvOptionPane.setInteractiveMode(false);
60 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
65 @BeforeMethod(alwaysRun = true)
68 seq = new Sequence("FER1", "AKPNGVL");
71 @Test(groups = { "Functional" })
72 public void testInsertGapsAndGapmaps()
74 SequenceI aseq = seq.deriveSequence();
75 aseq.insertCharAt(2, 3, '-');
76 aseq.insertCharAt(6, 3, '-');
77 assertEquals("Gap insertions not correct", "AK---P---NGVL",
78 aseq.getSequenceAsString());
79 List<int[]> gapInt = aseq.getInsertions();
80 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
81 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
82 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
83 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
85 BitSet gapfield = aseq.getInsertionsAsBits();
86 BitSet expectedgaps = new BitSet();
87 expectedgaps.set(2, 5);
88 expectedgaps.set(6, 9);
90 assertEquals(6, expectedgaps.cardinality());
92 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
93 6, gapfield.cardinality());
95 assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield);
98 @Test(groups = ("Functional"))
99 public void testIsProtein()
102 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
104 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
106 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
107 assertFalse(sq.isProtein());
108 // change sequence, should trigger an update of cached result
109 sq.setSequence("ASDFASDFADSF");
110 assertTrue(sq.isProtein());
113 @Test(groups = { "Functional" })
114 public void testGetAnnotation()
116 // initial state returns null not an empty array
117 assertNull(seq.getAnnotation());
118 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
120 AlignmentAnnotation[] anns = seq.getAnnotation();
121 assertEquals(1, anns.length);
122 assertSame(ann, anns[0]);
124 // removing all annotations reverts array to null
125 seq.removeAlignmentAnnotation(ann);
126 assertNull(seq.getAnnotation());
129 @Test(groups = { "Functional" })
130 public void testGetAnnotation_forLabel()
132 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
134 addAnnotation("label2", "desc2", "calcId2", 1f);
135 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
137 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
138 assertEquals(2, anns.length);
139 assertSame(ann1, anns[0]);
140 assertSame(ann3, anns[1]);
143 private AlignmentAnnotation addAnnotation(String label,
144 String description, String calcId, float value)
146 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
148 annotation.setCalcId(calcId);
149 seq.addAlignmentAnnotation(annotation);
153 @Test(groups = { "Functional" })
154 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
156 addAnnotation("label1", "desc1", "calcId1", 1f);
157 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
159 addAnnotation("label2", "desc3", "calcId3", 1f);
160 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
162 addAnnotation("label5", "desc3", null, 1f);
163 addAnnotation(null, "desc3", "calcId3", 1f);
165 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
167 assertEquals(2, anns.size());
168 assertSame(ann2, anns.get(0));
169 assertSame(ann4, anns.get(1));
171 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
172 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
173 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
174 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
175 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
179 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
180 * setting the sequenceRef on the annotation. Adding the same annotation twice
183 @Test(groups = { "Functional" })
184 public void testAddAlignmentAnnotation()
186 assertNull(seq.getAnnotation());
187 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
189 assertNull(annotation.sequenceRef);
190 seq.addAlignmentAnnotation(annotation);
191 assertSame(seq, annotation.sequenceRef);
192 AlignmentAnnotation[] anns = seq.getAnnotation();
193 assertEquals(1, anns.length);
194 assertSame(annotation, anns[0]);
196 // re-adding does nothing
197 seq.addAlignmentAnnotation(annotation);
198 anns = seq.getAnnotation();
199 assertEquals(1, anns.length);
200 assertSame(annotation, anns[0]);
202 // an identical but different annotation can be added
203 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
205 seq.addAlignmentAnnotation(annotation2);
206 anns = seq.getAnnotation();
207 assertEquals(2, anns.length);
208 assertSame(annotation, anns[0]);
209 assertSame(annotation2, anns[1]);
212 @Test(groups = { "Functional" })
213 public void testGetStartGetEnd()
215 SequenceI sq = new Sequence("test", "ABCDEF");
216 assertEquals(1, sq.getStart());
217 assertEquals(6, sq.getEnd());
219 sq = new Sequence("test", "--AB-C-DEF--");
220 assertEquals(1, sq.getStart());
221 assertEquals(6, sq.getEnd());
223 sq = new Sequence("test", "----");
224 assertEquals(1, sq.getStart());
225 assertEquals(0, sq.getEnd()); // ??
229 * Tests for the method that returns an alignment column position (base 1) for
230 * a given sequence position (base 1).
232 @Test(groups = { "Functional" })
233 public void testFindIndex()
236 * call sequenceChanged() after each test to invalidate any cursor,
237 * forcing the 1-arg findIndex to be executed
239 SequenceI sq = new Sequence("test", "ABCDEF");
240 assertEquals(0, sq.findIndex(0));
241 sq.sequenceChanged();
242 assertEquals(1, sq.findIndex(1));
243 sq.sequenceChanged();
244 assertEquals(5, sq.findIndex(5));
245 sq.sequenceChanged();
246 assertEquals(6, sq.findIndex(6));
247 sq.sequenceChanged();
248 assertEquals(6, sq.findIndex(9));
250 final String aligned = "-A--B-C-D-E-F--";
251 assertEquals(15, aligned.length());
252 sq = new Sequence("test/8-13", aligned);
253 assertEquals(2, sq.findIndex(8));
254 sq.sequenceChanged();
255 assertEquals(5, sq.findIndex(9));
256 sq.sequenceChanged();
257 assertEquals(7, sq.findIndex(10));
259 // before start returns 0
260 sq.sequenceChanged();
261 assertEquals(0, sq.findIndex(0));
262 sq.sequenceChanged();
263 assertEquals(0, sq.findIndex(-1));
265 // beyond end returns last residue column
266 sq.sequenceChanged();
267 assertEquals(13, sq.findIndex(99));
270 * residue before sequence 'end' but beyond end of sequence returns
271 * length of sequence (last column) (rightly or wrongly!)
273 sq = new Sequence("test/8-15", "A-B-C-"); // trailing gap case
274 assertEquals(6, sq.getLength());
275 sq.sequenceChanged();
276 assertEquals(sq.getLength(), sq.findIndex(14));
277 sq = new Sequence("test/8-99", "-A--B-C-D"); // trailing residue case
278 sq.sequenceChanged();
279 assertEquals(sq.getLength(), sq.findIndex(65));
282 * residue after sequence 'start' but before first residue returns
283 * zero (before first column) (rightly or wrongly!)
285 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
286 sq.sequenceChanged();
287 assertEquals(0, sq.findIndex(3));
288 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
289 sq.sequenceChanged();
290 assertEquals(0, sq.findIndex(2));
293 @Test(groups = { "Functional" })
294 public void testFindPositions()
296 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
301 assertNull(sq.findPositions(6, 5));
302 assertNull(sq.findPositions(0, 5));
303 assertNull(sq.findPositions(-1, 5));
308 assertNull(sq.findPositions(1, 1)); // 1-based columns
309 assertNull(sq.findPositions(5, 5));
310 assertNull(sq.findPositions(5, 6));
311 assertNull(sq.findPositions(5, 7));
314 * all ungapped ranges
316 assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
317 assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
318 assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
319 assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
322 * gap to ungapped range
324 assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
325 assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
328 * ungapped to gapped range
330 assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
331 assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
334 * ungapped to ungapped enclosing gaps
336 assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
337 assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
340 * gapped to gapped enclosing ungapped
342 assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
343 assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
344 assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
345 assertEquals(new Range(8, 13), sq.findPositions(1, 99));
349 * Tests for the method that returns a dataset sequence position (start..) for
350 * an aligned column position (base 0).
352 @Test(groups = { "Functional" })
353 public void testFindPosition()
356 * call sequenceChanged() after each test to invalidate any cursor,
357 * forcing the 1-arg findPosition to be executed
359 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
360 assertEquals(8, sq.findPosition(0));
361 // Sequence should now hold a cursor at [8, 0]
362 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
363 PA.getValue(sq, "cursor").toString());
364 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
365 int token = (int) PA.getValue(sq, "changeCount");
366 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
368 sq.sequenceChanged();
371 * find F13 at column offset 5, cursor should update to [13, 6]
372 * endColumn is found and saved in cursor
374 assertEquals(13, sq.findPosition(5));
375 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
376 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
377 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
378 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok2",
379 PA.getValue(sq, "cursor").toString());
381 // assertEquals(-1, seq.findPosition(6)); // fails
383 sq = new Sequence("test/8-11", "AB-C-D--");
384 token = (int) PA.getValue(sq, "changeCount"); // 1 for setStart
385 assertEquals(8, sq.findPosition(0));
386 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
387 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
388 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
389 PA.getValue(sq, "cursor").toString());
391 sq.sequenceChanged();
392 assertEquals(9, sq.findPosition(1));
393 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
394 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
395 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
396 PA.getValue(sq, "cursor").toString());
398 sq.sequenceChanged();
399 // gap position 'finds' residue to the right (not the left as per javadoc)
400 // cursor is set to the last residue position found [B 2]
401 assertEquals(10, sq.findPosition(2));
402 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
403 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
404 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok3",
405 PA.getValue(sq, "cursor").toString());
407 sq.sequenceChanged();
408 assertEquals(10, sq.findPosition(3));
409 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
410 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
411 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
412 PA.getValue(sq, "cursor").toString());
414 sq.sequenceChanged();
415 // column[4] is the gap after C - returns D11
416 // cursor is set to [C 4]
417 assertEquals(11, sq.findPosition(4));
418 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
419 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
420 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok5",
421 PA.getValue(sq, "cursor").toString());
423 sq.sequenceChanged();
424 assertEquals(11, sq.findPosition(5)); // D
425 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
426 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
427 // lastCol has been found and saved in the cursor
428 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok6",
429 PA.getValue(sq, "cursor").toString());
431 sq.sequenceChanged();
432 // returns 1 more than sequence length if off the end ?!?
433 assertEquals(12, sq.findPosition(6));
435 sq.sequenceChanged();
436 assertEquals(12, sq.findPosition(7));
439 * first findPosition should also set firstResCol in cursor
441 sq = new Sequence("test/8-13", "--AB-C-DEF--");
442 assertEquals(8, sq.findPosition(0));
443 assertNull(PA.getValue(sq, "cursor"));
444 assertEquals(1, PA.getValue(sq, "changeCount"));
446 sq.sequenceChanged();
447 assertEquals(8, sq.findPosition(1));
448 assertNull(PA.getValue(sq, "cursor"));
450 sq.sequenceChanged();
451 assertEquals(8, sq.findPosition(2));
452 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok3",
453 PA.getValue(sq, "cursor").toString());
455 sq.sequenceChanged();
456 assertEquals(9, sq.findPosition(3));
457 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
458 PA.getValue(sq, "cursor").toString());
460 sq.sequenceChanged();
461 // column[4] is a gap, returns next residue pos (C10)
462 // cursor is set to last residue found [B]
463 assertEquals(10, sq.findPosition(4));
464 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok5",
465 PA.getValue(sq, "cursor").toString());
467 sq.sequenceChanged();
468 assertEquals(10, sq.findPosition(5));
469 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
470 PA.getValue(sq, "cursor").toString());
472 sq.sequenceChanged();
473 // column[6] is a gap, returns next residue pos (D11)
474 // cursor is set to last residue found [C]
475 assertEquals(11, sq.findPosition(6));
476 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok7",
477 PA.getValue(sq, "cursor").toString());
479 sq.sequenceChanged();
480 assertEquals(11, sq.findPosition(7));
481 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok8",
482 PA.getValue(sq, "cursor").toString());
484 sq.sequenceChanged();
485 assertEquals(12, sq.findPosition(8));
486 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok9",
487 PA.getValue(sq, "cursor").toString());
490 * when the last residue column is found, it is set in the cursor
492 sq.sequenceChanged();
493 assertEquals(13, sq.findPosition(9));
494 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
495 PA.getValue(sq, "cursor").toString());
497 sq.sequenceChanged();
498 assertEquals(14, sq.findPosition(10));
499 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
500 PA.getValue(sq, "cursor").toString());
503 * findPosition for column beyond sequence length
504 * returns 1 more than last residue position
506 sq.sequenceChanged();
507 assertEquals(14, sq.findPosition(11));
508 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
509 PA.getValue(sq, "cursor").toString());
511 sq.sequenceChanged();
512 assertEquals(14, sq.findPosition(99));
513 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok13",
514 PA.getValue(sq, "cursor").toString());
517 * gapped sequence ending in non-gap
519 sq = new Sequence("test/8-13", "--AB-C-DEF");
520 assertEquals(13, sq.findPosition(9));
521 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
522 PA.getValue(sq, "cursor").toString());
523 sq.sequenceChanged();
524 assertEquals(12, sq.findPosition(8)); // E12
525 // sequenceChanged() invalidates cursor.lastResidueColumn
526 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
527 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok2",
529 // findPosition with cursor accepts base 1 column values
530 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
531 assertEquals(13, sq.findPosition(9)); // F13
532 // lastResidueColumn has now been found and saved in cursor
533 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok2",
534 PA.getValue(sq, "cursor").toString());
537 @Test(groups = { "Functional" })
538 public void testDeleteChars()
543 SequenceI sq = new Sequence("test", "ABCDEF");
544 assertNull(PA.getValue(sq, "datasetSequence"));
545 assertEquals(1, sq.getStart());
546 assertEquals(6, sq.getEnd());
547 sq.deleteChars(2, 3);
548 assertEquals("ABDEF", sq.getSequenceAsString());
549 assertEquals(1, sq.getStart());
550 assertEquals(5, sq.getEnd());
551 assertNull(PA.getValue(sq, "datasetSequence"));
556 sq = new Sequence("test", "ABCDEF");
557 sq.deleteChars(0, 2);
558 assertEquals("CDEF", sq.getSequenceAsString());
559 assertEquals(3, sq.getStart());
560 assertEquals(6, sq.getEnd());
561 assertNull(PA.getValue(sq, "datasetSequence"));
563 sq = new Sequence("test", "ABCDE");
564 sq.deleteChars(0, 3);
565 assertEquals("DE", sq.getSequenceAsString());
566 assertEquals(4, sq.getStart());
567 assertEquals(5, sq.getEnd());
568 assertNull(PA.getValue(sq, "datasetSequence"));
573 sq = new Sequence("test", "ABCDEF");
574 sq.deleteChars(4, 6);
575 assertEquals("ABCD", sq.getSequenceAsString());
576 assertEquals(1, sq.getStart());
577 assertEquals(4, sq.getEnd());
578 assertNull(PA.getValue(sq, "datasetSequence"));
581 * delete more positions than there are
583 sq = new Sequence("test/8-11", "ABCD");
584 sq.deleteChars(0, 99);
585 assertEquals("", sq.getSequenceAsString());
586 assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
587 assertEquals(11, sq.getEnd());
589 sq = new Sequence("test/8-11", "----");
590 sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
591 assertEquals("", sq.getSequenceAsString());
592 assertEquals(8, sq.getStart());
593 assertEquals(11, sq.getEnd());
596 @Test(groups = { "Functional" })
597 public void testDeleteChars_withDbRefsAndFeatures()
600 * internal delete - new dataset sequence created
601 * gets a copy of any dbrefs
603 SequenceI sq = new Sequence("test", "ABCDEF");
604 sq.createDatasetSequence();
605 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
607 Object ds = PA.getValue(sq, "datasetSequence");
609 assertEquals(1, sq.getStart());
610 assertEquals(6, sq.getEnd());
611 sq.deleteChars(2, 3);
612 assertEquals("ABDEF", sq.getSequenceAsString());
613 assertEquals(1, sq.getStart());
614 assertEquals(5, sq.getEnd());
615 Object newDs = PA.getValue(sq, "datasetSequence");
616 assertNotNull(newDs);
617 assertNotSame(ds, newDs);
618 assertNotNull(sq.getDBRefs());
619 assertEquals(1, sq.getDBRefs().size());
620 assertNotSame(dbr1, sq.getDBRefs().get(0));
621 assertEquals(dbr1, sq.getDBRefs().get(0));
624 * internal delete with sequence features
625 * (failure case for JAL-2541)
627 sq = new Sequence("test", "ABCDEF");
628 sq.createDatasetSequence();
629 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
631 sq.addSequenceFeature(sf1);
632 ds = PA.getValue(sq, "datasetSequence");
634 assertEquals(1, sq.getStart());
635 assertEquals(6, sq.getEnd());
636 sq.deleteChars(2, 4);
637 assertEquals("ABEF", sq.getSequenceAsString());
638 assertEquals(1, sq.getStart());
639 assertEquals(4, sq.getEnd());
640 newDs = PA.getValue(sq, "datasetSequence");
641 assertNotNull(newDs);
642 assertNotSame(ds, newDs);
643 List<SequenceFeature> sfs = sq.getSequenceFeatures();
644 assertEquals(1, sfs.size());
645 assertNotSame(sf1, sfs.get(0));
646 assertEquals(sf1, sfs.get(0));
649 * delete at start - no new dataset sequence created
650 * any sequence features remain as before
652 sq = new Sequence("test", "ABCDEF");
653 sq.createDatasetSequence();
654 ds = PA.getValue(sq, "datasetSequence");
655 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
656 sq.addSequenceFeature(sf1);
657 sq.deleteChars(0, 2);
658 assertEquals("CDEF", sq.getSequenceAsString());
659 assertEquals(3, sq.getStart());
660 assertEquals(6, sq.getEnd());
661 assertSame(ds, PA.getValue(sq, "datasetSequence"));
662 sfs = sq.getSequenceFeatures();
664 assertEquals(1, sfs.size());
665 assertSame(sf1, sfs.get(0));
668 * delete at end - no new dataset sequence created
669 * any dbrefs remain as before
671 sq = new Sequence("test", "ABCDEF");
672 sq.createDatasetSequence();
673 ds = PA.getValue(sq, "datasetSequence");
674 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
676 sq.deleteChars(4, 6);
677 assertEquals("ABCD", sq.getSequenceAsString());
678 assertEquals(1, sq.getStart());
679 assertEquals(4, sq.getEnd());
680 assertSame(ds, PA.getValue(sq, "datasetSequence"));
681 assertNotNull(sq.getDBRefs());
682 assertEquals(1, sq.getDBRefs().size());
683 assertSame(dbr1, sq.getDBRefs().get(0));
686 @Test(groups = { "Functional" })
687 public void testInsertCharAt()
689 // non-static methods:
690 SequenceI sq = new Sequence("test", "ABCDEF");
691 sq.insertCharAt(0, 'z');
692 assertEquals("zABCDEF", sq.getSequenceAsString());
693 sq.insertCharAt(2, 2, 'x');
694 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
696 // for static method see StringUtilsTest
700 * Test the method that returns an array of aligned sequence positions where
701 * the array index is the data sequence position (both base 0).
703 @Test(groups = { "Functional" })
704 public void testGapMap()
706 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
707 sq.createDatasetSequence();
708 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
712 * Test the method that gets sequence features, either from the sequence or
715 @Test(groups = { "Functional" })
716 public void testGetSequenceFeatures()
718 SequenceI sq = new Sequence("test", "GATCAT");
719 sq.createDatasetSequence();
721 assertTrue(sq.getSequenceFeatures().isEmpty());
724 * SequenceFeature on sequence
726 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
727 sq.addSequenceFeature(sf);
728 List<SequenceFeature> sfs = sq.getSequenceFeatures();
729 assertEquals(1, sfs.size());
730 assertSame(sf, sfs.get(0));
733 * SequenceFeature on sequence and dataset sequence; returns that on
736 * Note JAL-2046: spurious: we have no use case for this at the moment.
737 * This test also buggy - as sf2.equals(sf), no new feature is added
739 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
741 sq.getDatasetSequence().addSequenceFeature(sf2);
742 sfs = sq.getSequenceFeatures();
743 assertEquals(1, sfs.size());
744 assertSame(sf, sfs.get(0));
747 * SequenceFeature on dataset sequence only
748 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
750 sq.setSequenceFeatures(null);
751 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
754 * Corrupt case - no SequenceFeature, dataset's dataset is the original
755 * sequence. Test shows no infinite loop results.
757 sq.getDatasetSequence().setSequenceFeatures(null);
759 * is there a usecase for this ? setDatasetSequence should throw an error if
760 * this actually occurs.
764 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
765 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
766 } catch (IllegalArgumentException e)
768 // TODO Jalview error/exception class for raising implementation errors
769 assertTrue(e.getMessage().toLowerCase()
770 .contains("implementation error"));
772 assertTrue(sq.getSequenceFeatures().isEmpty());
776 * Test the method that returns an array, indexed by sequence position, whose
777 * entries are the residue positions at the sequence position (or to the right
780 @Test(groups = { "Functional" })
781 public void testFindPositionMap()
784 * Note: Javadoc for findPosition says it returns the residue position to
785 * the left of a gapped position; in fact it returns the position to the
786 * right. Also it returns a non-existent residue position for a gap beyond
789 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
790 int[] map = sq.findPositionMap();
791 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
792 Arrays.toString(map));
796 * Test for getSubsequence
798 @Test(groups = { "Functional" })
799 public void testGetSubsequence()
801 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
802 sq.createDatasetSequence();
804 // positions are base 0, end position is exclusive
805 SequenceI subseq = sq.getSubSequence(2, 4);
807 assertEquals("CD", subseq.getSequenceAsString());
808 // start/end are base 1 positions
809 assertEquals(3, subseq.getStart());
810 assertEquals(4, subseq.getEnd());
811 // subsequence shares the full dataset sequence
812 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
816 * test createDatasetSequence behaves to doc
818 @Test(groups = { "Functional" })
819 public void testCreateDatasetSequence()
821 SequenceI sq = new Sequence("my", "ASDASD");
822 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
824 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
825 assertNull(sq.getDatasetSequence());
826 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
827 assertNotNull(PA.getValue(sq, "dbrefs"));
829 SequenceI rds = sq.createDatasetSequence();
831 assertNull(rds.getDatasetSequence());
832 assertSame(sq.getDatasetSequence(), rds);
834 // sequence features and dbrefs transferred to dataset sequence
835 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
836 assertNull(PA.getValue(sq, "dbrefs"));
837 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
838 assertNotNull(PA.getValue(rds, "dbrefs"));
842 * Test for deriveSequence applied to a sequence with a dataset
844 @Test(groups = { "Functional" })
845 public void testDeriveSequence_existingDataset()
847 Sequence sq = new Sequence("Seq1", "CD");
848 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
849 sq.getDatasetSequence().addSequenceFeature(
850 new SequenceFeature("", "", 1, 2, 0f, null));
854 sq.setDescription("Test sequence description..");
855 sq.setVamsasId("TestVamsasId");
856 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
858 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
859 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
860 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
861 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
863 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
864 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
865 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
866 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
868 // these are the same as ones already added
869 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
870 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
872 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
875 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
876 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
877 sq.getDatasetSequence().addDBRef(
878 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
879 sq.getDatasetSequence().addDBRef(
880 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
882 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
883 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
884 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
886 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
888 sq.getDatasetSequence().addPDBId(pdbe1a);
889 sq.getDatasetSequence().addPDBId(pdbe1b);
890 sq.getDatasetSequence().addPDBId(pdbe2a);
891 sq.getDatasetSequence().addPDBId(pdbe2b);
894 * test we added pdb entries to the dataset sequence
896 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
897 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
898 "PDB Entries were not found on dataset sequence.");
901 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
903 Assert.assertEquals(pdbe1a,
904 sq.getDatasetSequence().getPDBEntry("1PDB"),
905 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
906 ArrayList<Annotation> annotsList = new ArrayList<>();
907 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
908 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
909 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
910 Annotation[] annots = annotsList.toArray(new Annotation[0]);
911 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
912 "Test annot description", annots));
913 sq.getDatasetSequence().addAlignmentAnnotation(
914 new AlignmentAnnotation("Test annot", "Test annot description",
916 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
917 Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
919 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
920 Assert.assertNotNull(sq.getAnnotation());
921 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
922 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
925 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
927 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
929 Sequence derived = (Sequence) sq.deriveSequence();
931 Assert.assertEquals(derived.getDescription(),
932 "Test sequence description..");
933 Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
934 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
935 Assert.assertNotNull(derived.getAnnotation());
936 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
937 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
938 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
940 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
942 assertEquals("CD", derived.getSequenceAsString());
943 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
945 // derived sequence should access dataset sequence features
946 assertNotNull(sq.getSequenceFeatures());
947 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
950 * verify we have primary db refs *just* for PDB IDs with associated
954 assertEquals(primRefs, sq.getPrimaryDBRefs());
955 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
957 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
962 * Test for deriveSequence applied to an ungapped sequence with no dataset
964 @Test(groups = { "Functional" })
965 public void testDeriveSequence_noDatasetUngapped()
967 SequenceI sq = new Sequence("Seq1", "ABCDEF");
968 assertEquals(1, sq.getStart());
969 assertEquals(6, sq.getEnd());
970 SequenceI derived = sq.deriveSequence();
971 assertEquals("ABCDEF", derived.getSequenceAsString());
972 assertEquals("ABCDEF", derived.getDatasetSequence()
973 .getSequenceAsString());
977 * Test for deriveSequence applied to a gapped sequence with no dataset
979 @Test(groups = { "Functional" })
980 public void testDeriveSequence_noDatasetGapped()
982 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
983 assertEquals(1, sq.getStart());
984 assertEquals(6, sq.getEnd());
985 assertNull(sq.getDatasetSequence());
986 SequenceI derived = sq.deriveSequence();
987 assertEquals("AB-C.D EF", derived.getSequenceAsString());
988 assertEquals("ABCDEF", derived.getDatasetSequence()
989 .getSequenceAsString());
992 @Test(groups = { "Functional" })
993 public void testCopyConstructor_noDataset()
995 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
996 seq1.setDescription("description");
997 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
999 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
1001 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1002 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1004 SequenceI copy = new Sequence(seq1);
1006 assertNull(copy.getDatasetSequence());
1008 verifyCopiedSequence(seq1, copy);
1010 // copy has a copy of the DBRefEntry
1011 // this is murky - DBrefs are only copied for dataset sequences
1012 // where the test for 'dataset sequence' is 'dataset is null'
1013 // but that doesn't distinguish it from an aligned sequence
1014 // which has not yet generated a dataset sequence
1015 // NB getDBRef looks inside dataset sequence if not null
1016 List<DBRefEntry> dbrefs = copy.getDBRefs();
1017 assertEquals(1, dbrefs.size());
1018 assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
1019 assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
1022 @Test(groups = { "Functional" })
1023 public void testCopyConstructor_withDataset()
1025 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1026 seq1.createDatasetSequence();
1027 seq1.setDescription("description");
1028 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
1030 // JAL-2046 - what is the contract for using a derived sequence's
1031 // addSequenceFeature ?
1032 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
1034 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1035 // here we add DBRef to the dataset sequence:
1036 seq1.getDatasetSequence().addDBRef(
1037 new DBRefEntry("EMBL", "1.2", "AZ12345"));
1039 SequenceI copy = new Sequence(seq1);
1041 assertNotNull(copy.getDatasetSequence());
1042 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1044 verifyCopiedSequence(seq1, copy);
1046 // getDBRef looks inside dataset sequence and this is shared,
1047 // so holds the same dbref objects
1048 List<DBRefEntry> dbrefs = copy.getDBRefs();
1049 assertEquals(1, dbrefs.size());
1050 assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
1054 * Helper to make assertions about a copied sequence
1059 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1061 // verify basic properties:
1062 assertEquals(copy.getName(), seq1.getName());
1063 assertEquals(copy.getDescription(), seq1.getDescription());
1064 assertEquals(copy.getStart(), seq1.getStart());
1065 assertEquals(copy.getEnd(), seq1.getEnd());
1066 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1068 // copy has a copy of the annotation:
1069 AlignmentAnnotation[] anns = copy.getAnnotation();
1070 assertEquals(1, anns.length);
1071 assertFalse(anns[0] == seq1.getAnnotation()[0]);
1072 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1073 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1074 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1076 // copy has a copy of the sequence feature:
1077 List<SequenceFeature> sfs = copy.getSequenceFeatures();
1078 assertEquals(1, sfs.size());
1079 if (seq1.getDatasetSequence() != null
1080 && copy.getDatasetSequence() == seq1.getDatasetSequence())
1082 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1086 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1088 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1090 // copy has a copy of the PDB entry
1091 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1092 assertEquals(1, pdbs.size());
1093 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1094 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1097 @Test(groups = "Functional")
1098 public void testGetCharAt()
1100 SequenceI sq = new Sequence("", "abcde");
1101 assertEquals('a', sq.getCharAt(0));
1102 assertEquals('e', sq.getCharAt(4));
1103 assertEquals(' ', sq.getCharAt(5));
1104 assertEquals(' ', sq.getCharAt(-1));
1107 @Test(groups = { "Functional" })
1108 public void testAddSequenceFeatures()
1110 SequenceI sq = new Sequence("", "abcde");
1111 // type may not be null
1112 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
1114 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1116 // can't add a duplicate feature
1117 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
1119 // can add a different feature
1120 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
1121 8, 0f, null))); // different type
1122 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
1123 "description", 4, 8, 0f, null)));// different description
1124 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
1125 8, 0f, null))); // different start position
1126 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1127 9, 0f, null))); // different end position
1128 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1129 8, 1f, null))); // different score
1130 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1131 8, Float.NaN, null))); // score NaN
1132 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1133 8, 0f, "Metal"))); // different group
1134 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1138 * Tests for adding (or updating) dbrefs
1140 * @see DBRefEntry#updateFrom(DBRefEntry)
1142 @Test(groups = { "Functional" })
1143 public void testAddDBRef()
1145 SequenceI sq = new Sequence("", "abcde");
1146 assertNull(sq.getDBRefs());
1147 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1149 assertEquals(1, sq.getDBRefs().size());
1150 assertSame(dbref, sq.getDBRefs().get(0));
1153 * change of version - new entry
1155 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1156 sq.addDBRef(dbref2);
1157 assertEquals(2, sq.getDBRefs().size());
1158 assertSame(dbref, sq.getDBRefs().get(0));
1159 assertSame(dbref2, sq.getDBRefs().get(1));
1162 * matches existing entry - not added
1164 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1165 assertEquals(2, sq.getDBRefs().size());
1168 * different source = new entry
1170 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1171 sq.addDBRef(dbref3);
1172 assertEquals(3, sq.getDBRefs().size());
1173 assertSame(dbref3, sq.getDBRefs().get(2));
1176 * different ref = new entry
1178 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1179 sq.addDBRef(dbref4);
1180 assertEquals(4, sq.getDBRefs().size());
1181 assertSame(dbref4, sq.getDBRefs().get(3));
1184 * matching ref with a mapping - map updated
1186 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1187 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1190 sq.addDBRef(dbref5);
1191 assertEquals(4, sq.getDBRefs().size());
1192 assertSame(dbref4, sq.getDBRefs().get(3));
1193 assertSame(map, dbref4.getMap());
1196 * 'real' version replaces "0" version
1198 dbref2.setVersion("0");
1199 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1200 dbref2.getAccessionId());
1201 sq.addDBRef(dbref6);
1202 assertEquals(4, sq.getDBRefs().size());
1203 assertSame(dbref2, sq.getDBRefs().get(1));
1204 assertEquals("3", dbref2.getVersion());
1207 * 'real' version replaces "source:0" version
1209 dbref3.setVersion("Uniprot:0");
1210 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1211 dbref3.getAccessionId());
1212 sq.addDBRef(dbref7);
1213 assertEquals(4, sq.getDBRefs().size());
1214 assertSame(dbref3, sq.getDBRefs().get(2));
1215 assertEquals("3", dbref2.getVersion());
1218 @Test(groups = { "Functional" })
1219 public void testGetPrimaryDBRefs_peptide()
1221 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1224 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1225 assertTrue(primaryDBRefs.isEmpty());
1229 primaryDBRefs = sq.getPrimaryDBRefs();
1230 assertTrue(primaryDBRefs.isEmpty());
1232 // primary - uniprot
1233 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1234 sq.addDBRef(upentry1);
1236 // primary - uniprot with congruent map
1237 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1238 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1239 new int[] { 10, 22 }, 1, 1)));
1240 sq.addDBRef(upentry2);
1242 // primary - uniprot with map of enclosing sequence
1243 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1244 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1245 new int[] { 8, 24 }, 1, 1)));
1246 sq.addDBRef(upentry3);
1248 // not primary - uniprot with map of sub-sequence (5')
1249 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1250 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1251 new int[] { 10, 18 }, 1, 1)));
1252 sq.addDBRef(upentry4);
1254 // not primary - uniprot with map that overlaps 3'
1255 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1256 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1257 new int[] { 12, 22 }, 1, 1)));
1258 sq.addDBRef(upentry5);
1260 // not primary - uniprot with map to different coordinates frame
1261 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1262 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1263 new int[] { 112, 118 }, 1, 1)));
1264 sq.addDBRef(upentry6);
1266 // not primary - dbref to 'non-core' database
1267 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1268 sq.addDBRef(upentry7);
1270 // primary - type is PDB
1271 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1272 sq.addDBRef(pdbentry);
1274 // not primary - PDBEntry has no file
1275 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1277 // not primary - no PDBEntry
1278 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1280 // add corroborating PDB entry for primary DBref -
1281 // needs to have a file as well as matching ID
1282 // note PDB ID is not treated as case sensitive
1283 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1286 // not valid DBRef - no file..
1287 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1289 primaryDBRefs = sq.getPrimaryDBRefs();
1290 assertEquals(4, primaryDBRefs.size());
1291 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1292 primaryDBRefs.contains(upentry1));
1293 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1294 primaryDBRefs.contains(upentry2));
1295 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1296 primaryDBRefs.contains(upentry3));
1297 assertTrue("Couldn't find expected PDB primary reference",
1298 primaryDBRefs.contains(pdbentry));
1301 @Test(groups = { "Functional" })
1302 public void testGetPrimaryDBRefs_nucleotide()
1304 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1306 // primary - Ensembl
1307 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1310 // not primary - Ensembl 'transcript' mapping of sub-sequence
1311 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1312 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1313 new int[] { 1, 11 }, 1, 1)));
1316 // primary - EMBL with congruent map
1317 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1318 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1319 new int[] { 10, 34 }, 1, 1)));
1322 // not primary - to non-core database
1323 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1326 // not primary - to protein
1327 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1330 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1331 assertEquals(2, primaryDBRefs.size());
1332 assertTrue(primaryDBRefs.contains(dbr1));
1333 assertTrue(primaryDBRefs.contains(dbr3));
1337 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1340 @Test(groups = { "Functional" })
1341 public void testUpdatePDBIds()
1343 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1344 seq.addPDBId(pdbe1);
1345 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1346 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1347 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1348 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1349 // 7 is not a valid chain code:
1350 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1353 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1354 assertEquals(4, pdbIds.size());
1355 assertSame(pdbe1, pdbIds.get(0));
1356 // chain code got added to 3A6S:
1357 assertEquals("B", pdbe1.getChainCode());
1358 assertEquals("1A70", pdbIds.get(1).getId());
1359 // 4BQGA is parsed into id + chain
1360 assertEquals("4BQG", pdbIds.get(2).getId());
1361 assertEquals("a", pdbIds.get(2).getChainCode());
1362 assertEquals("2GIS7", pdbIds.get(3).getId());
1363 assertNull(pdbIds.get(3).getChainCode());
1367 * Test the method that either adds a pdbid or updates an existing one
1369 @Test(groups = { "Functional" })
1370 public void testAddPDBId()
1372 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1374 assertEquals(1, seq.getAllPDBEntries().size());
1375 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1376 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1378 // add the same entry
1380 assertEquals(1, seq.getAllPDBEntries().size());
1381 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1383 // add an identical entry
1384 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1385 assertEquals(1, seq.getAllPDBEntries().size());
1386 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1388 // add a different entry
1389 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1390 seq.addPDBId(pdbe2);
1391 assertEquals(2, seq.getAllPDBEntries().size());
1392 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1393 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1395 // update pdbe with chain code, file, type
1396 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1397 seq.addPDBId(pdbe3);
1398 assertEquals(2, seq.getAllPDBEntries().size());
1399 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1400 assertEquals("3A6S", pdbe.getId()); // unchanged
1401 assertEquals("A", pdbe.getChainCode()); // updated
1402 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1403 assertEquals("filepath", pdbe.getFile()); // updated
1404 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1406 // add with a different file path
1407 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1408 seq.addPDBId(pdbe4);
1409 assertEquals(3, seq.getAllPDBEntries().size());
1410 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1412 // add with a different chain code
1413 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1414 seq.addPDBId(pdbe5);
1415 assertEquals(4, seq.getAllPDBEntries().size());
1416 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1420 groups = { "Functional" },
1421 expectedExceptions = { IllegalArgumentException.class })
1422 public void testSetDatasetSequence_toSelf()
1424 seq.setDatasetSequence(seq);
1428 groups = { "Functional" },
1429 expectedExceptions = { IllegalArgumentException.class })
1430 public void testSetDatasetSequence_cascading()
1432 SequenceI seq2 = new Sequence("Seq2", "xyz");
1433 seq2.createDatasetSequence();
1434 seq.setDatasetSequence(seq2);
1437 @Test(groups = { "Functional" })
1438 public void testFindFeatures()
1440 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1441 sq.createDatasetSequence();
1443 assertTrue(sq.findFeatures(1, 99).isEmpty());
1445 // add non-positional feature
1446 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1448 sq.addSequenceFeature(sf0);
1449 // add feature on BCD
1450 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1452 sq.addSequenceFeature(sfBCD);
1453 // add feature on DE
1454 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1456 sq.addSequenceFeature(sfDE);
1457 // add contact feature at [B, H]
1458 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1459 "desc", 9, 15, 2f, null);
1460 sq.addSequenceFeature(sfContactBH);
1461 // add contact feature at [F, G]
1462 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1463 "desc", 13, 14, 2f, null);
1464 sq.addSequenceFeature(sfContactFG);
1465 // add single position feature at [I]
1466 SequenceFeature sfI = new SequenceFeature("Disulfide Bond",
1467 "desc", 16, 16, null);
1468 sq.addSequenceFeature(sfI);
1470 // no features in columns 1-2 (-A)
1471 List<SequenceFeature> found = sq.findFeatures(1, 2);
1472 assertTrue(found.isEmpty());
1474 // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1475 found = sq.findFeatures(1, 6);
1476 assertEquals(2, found.size());
1477 assertTrue(found.contains(sfBCD));
1478 assertTrue(found.contains(sfContactBH));
1480 // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1481 found = sq.findFeatures(5, 6);
1482 assertEquals(1, found.size());
1483 assertTrue(found.contains(sfBCD));
1485 // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1486 found = sq.findFeatures(7, 10);
1487 assertEquals(3, found.size());
1488 assertTrue(found.contains(sfBCD));
1489 assertTrue(found.contains(sfDE));
1490 assertTrue(found.contains(sfContactFG));
1492 // columns 10-11 (--) should find nothing
1493 found = sq.findFeatures(10, 11);
1494 assertEquals(0, found.size());
1496 // columns 14-14 (I) should find variant feature
1497 found = sq.findFeatures(14, 14);
1498 assertEquals(1, found.size());
1499 assertTrue(found.contains(sfI));
1502 @Test(groups = { "Functional" })
1503 public void testFindIndex_withCursor()
1505 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1506 final int tok = (int) PA.getValue(sq, "changeCount");
1507 assertEquals(1, tok);
1509 // find F given A, check cursor is now at the found position
1510 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
1511 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1512 assertEquals(13, cursor.residuePosition);
1513 assertEquals(10, cursor.columnPosition);
1516 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
1517 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1518 assertEquals(8, cursor.residuePosition);
1519 assertEquals(2, cursor.columnPosition);
1521 // find C given C (no cursor update is done for this case)
1522 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
1523 SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1524 assertSame(cursor2, cursor);
1527 * sequence 'end' beyond end of sequence returns length of sequence
1528 * (for compatibility with pre-cursor code)
1529 * - also verify the cursor is left in a valid state
1531 sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
1532 assertEquals(7, sq.findIndex(10)); // establishes a cursor
1533 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1534 assertEquals(10, cursor.residuePosition);
1535 assertEquals(7, cursor.columnPosition);
1536 assertEquals(sq.getLength(), sq.findIndex(65));
1537 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1538 assertSame(cursor, cursor2); // not updated for this case!
1540 sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
1541 sq.findIndex(10); // establishes a cursor
1542 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1543 assertEquals(sq.getLength(), sq.findIndex(65));
1544 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1545 assertSame(cursor, cursor2); // not updated for this case!
1548 * residue after sequence 'start' but before first residue should return
1549 * zero (for compatibility with pre-cursor code)
1551 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
1552 sq.findIndex(10); // establishes a cursor
1553 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1554 assertEquals(0, sq.findIndex(3));
1555 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1556 assertSame(cursor, cursor2); // not updated for this case!
1558 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
1559 sq.findIndex(10); // establishes a cursor
1560 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1561 assertEquals(0, sq.findIndex(2));
1562 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1563 assertSame(cursor, cursor2); // not updated for this case!
1566 @Test(groups = { "Functional" })
1567 public void testFindPosition_withCursor()
1569 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1570 final int tok = (int) PA.getValue(sq, "changeCount");
1571 assertEquals(1, tok);
1573 // find F pos given A - lastCol gets set in cursor
1575 sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
1576 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1577 PA.getValue(sq, "cursor").toString());
1579 // find A pos given F - first residue column is saved in cursor
1581 sq.findPosition(2, new SequenceCursor(sq, 13, 10, tok)));
1582 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
1583 PA.getValue(sq, "cursor").toString());
1585 // find C pos given C (neither startCol nor endCol is set)
1587 sq.findPosition(6, new SequenceCursor(sq, 10, 6, tok)));
1588 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1589 PA.getValue(sq, "cursor").toString());
1591 // now the grey area - what residue position for a gapped column? JAL-2562
1593 // find 'residue' for column 3 given cursor for D (so working left)
1594 // returns B9; cursor is updated to [B 5]
1595 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, tok)));
1596 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
1597 PA.getValue(sq, "cursor").toString());
1599 // find 'residue' for column 8 given cursor for D (so working right)
1600 // returns E12; cursor is updated to [D 7]
1602 sq.findPosition(8, new SequenceCursor(sq, 11, 7, tok)));
1603 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
1604 PA.getValue(sq, "cursor").toString());
1606 // find 'residue' for column 12 given cursor for B
1607 // returns 1 more than last residue position; cursor is updated to [F 10]
1608 // lastCol position is saved in cursor
1610 sq.findPosition(12, new SequenceCursor(sq, 9, 5, tok)));
1611 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1612 PA.getValue(sq, "cursor").toString());
1615 * findPosition for column beyond length of sequence
1616 * returns 1 more than the last residue position
1617 * cursor is set to last real residue position [F 10]
1620 sq.findPosition(99, new SequenceCursor(sq, 8, 2, tok)));
1621 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1622 PA.getValue(sq, "cursor").toString());
1625 * and the case without a trailing gap
1627 sq = new Sequence("test/8-13", "-A--BCD-EF");
1628 // first find C from A
1629 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, tok)));
1630 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1631 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1633 // now 'find' 99 from C
1634 // cursor is set to [F 10] and saved lastCol
1635 assertEquals(14, sq.findPosition(99, cursor));
1636 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1637 PA.getValue(sq, "cursor").toString());
1641 public void testIsValidCursor()
1643 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1644 assertFalse(sq.isValidCursor(null));
1647 * cursor is valid if it has valid sequence ref and changeCount token
1648 * and positions within the range of the sequence
1650 int changeCount = (int) PA.getValue(sq, "changeCount");
1651 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1652 assertTrue(sq.isValidCursor(cursor));
1655 * column position outside [0 - length] is rejected
1657 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1658 assertFalse(sq.isValidCursor(cursor));
1659 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1660 assertFalse(sq.isValidCursor(cursor));
1661 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1662 assertFalse(sq.isValidCursor(cursor));
1663 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1664 assertFalse(sq.isValidCursor(cursor));
1667 * wrong sequence is rejected
1669 cursor = new SequenceCursor(null, 13, 1, changeCount);
1670 assertFalse(sq.isValidCursor(cursor));
1671 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1673 assertFalse(sq.isValidCursor(cursor));
1676 * wrong token value is rejected
1678 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1679 assertFalse(sq.isValidCursor(cursor));
1680 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1681 assertFalse(sq.isValidCursor(cursor));
1684 @Test(groups = { "Functional" })
1685 public void testFindPosition_withCursorAndEdits()
1687 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1689 // find F pos given A
1690 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1691 int token = (int) PA.getValue(sq, "changeCount"); // 0
1692 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1693 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1696 * setSequence should invalidate the cursor cached by the sequence
1698 sq.setSequence("-A-BCD-EF---"); // one gap removed
1699 assertEquals(8, sq.getStart()); // sanity check
1700 assertEquals(11, sq.findPosition(5)); // D11
1701 // cursor should now be at [D 6]
1702 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1703 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1704 assertEquals(0, cursor.lastColumnPosition); // not yet found
1705 assertEquals(13, sq.findPosition(8)); // E13
1706 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1707 assertEquals(9, cursor.lastColumnPosition); // found
1710 * deleteChars should invalidate the cached cursor
1712 sq.deleteChars(2, 5); // delete -BC
1713 assertEquals("-AD-EF---", sq.getSequenceAsString());
1714 assertEquals(8, sq.getStart()); // sanity check
1715 assertEquals(10, sq.findPosition(4)); // E10
1716 // cursor should now be at [E 5]
1717 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1718 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1721 * Edit to insert gaps should invalidate the cached cursor
1722 * insert 2 gaps at column[3] to make -AD---EF---
1724 SequenceI[] seqs = new SequenceI[] { sq };
1725 AlignmentI al = new Alignment(seqs);
1726 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1727 assertEquals("-AD---EF---", sq.getSequenceAsString());
1728 assertEquals(10, sq.findPosition(4)); // E10
1729 // cursor should now be at [D 3]
1730 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1731 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1734 * insertCharAt should invalidate the cached cursor
1735 * insert CC at column[4] to make -AD-CC--EF---
1737 sq.insertCharAt(4, 2, 'C');
1738 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1739 assertEquals(13, sq.findPosition(9)); // F13
1740 // cursor should now be at [F 10]
1741 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1742 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1745 * changing sequence start should invalidate cursor
1747 sq = new Sequence("test/8-13", "-A--BCD-EF--");
1748 assertEquals(8, sq.getStart());
1749 assertEquals(9, sq.findPosition(4)); // B(9)
1751 assertEquals(8, sq.findPosition(4)); // is now B(8)
1753 assertEquals(11, sq.findPosition(4)); // is now B(11)
1756 @Test(groups = { "Functional" })
1757 public void testGetSequence()
1759 String seqstring = "-A--BCD-EF--";
1760 Sequence sq = new Sequence("test/8-13", seqstring);
1761 sq.createDatasetSequence();
1762 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1763 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1764 "ABCDEF".toCharArray()));
1766 // verify a copy of the sequence array is returned
1767 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1768 assertNotSame(theSeq, sq.getSequence());
1769 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1770 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1773 @Test(groups = { "Functional" })
1774 public void testReplace()
1776 String seqstring = "-A--BCD-EF--";
1777 SequenceI sq = new Sequence("test/8-13", seqstring);
1778 // changeCount is incremented for setStart
1779 assertEquals(1, PA.getValue(sq, "changeCount"));
1781 assertEquals(0, sq.replace('A', 'A')); // same char
1782 assertEquals(seqstring, sq.getSequenceAsString());
1783 assertEquals(1, PA.getValue(sq, "changeCount"));
1785 assertEquals(0, sq.replace('X', 'Y')); // not there
1786 assertEquals(seqstring, sq.getSequenceAsString());
1787 assertEquals(1, PA.getValue(sq, "changeCount"));
1789 assertEquals(1, sq.replace('A', 'K'));
1790 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1791 assertEquals(2, PA.getValue(sq, "changeCount"));
1793 assertEquals(6, sq.replace('-', '.'));
1794 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1795 assertEquals(3, PA.getValue(sq, "changeCount"));
1798 @Test(groups = { "Functional" })
1799 public void testGapBitset()
1801 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1802 BitSet bs = sq.gapBitset();
1803 BitSet expected = new BitSet();
1807 expected.set(11, 13);
1809 assertTrue(bs.equals(expected));
1813 public void testFindFeatures_largeEndPos()
1816 * imitate a PDB sequence where end is larger than end position
1818 SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
1819 sq.createDatasetSequence();
1821 assertTrue(sq.findFeatures(1, 9).isEmpty());
1822 // should be no array bounds exception - JAL-2772
1823 assertTrue(sq.findFeatures(1, 15).isEmpty());
1825 // add feature on BCD
1826 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
1828 sq.addSequenceFeature(sfBCD);
1830 // no features in columns 1-2 (-A)
1831 List<SequenceFeature> found = sq.findFeatures(1, 2);
1832 assertTrue(found.isEmpty());
1834 // columns 1-6 (-ABC--) includes BCD
1835 found = sq.findFeatures(1, 6);
1836 assertEquals(1, found.size());
1837 assertTrue(found.contains(sfBCD));
1839 // columns 10-11 (--) should find nothing
1840 found = sq.findFeatures(10, 11);
1841 assertEquals(0, found.size());
1844 @Test(groups = { "Functional" })
1845 public void testSetName()
1847 SequenceI sq = new Sequence("test", "-ABC---DE-F--");
1848 assertEquals("test", sq.getName());
1849 assertEquals(1, sq.getStart());
1850 assertEquals(6, sq.getEnd());
1852 sq.setName("testing");
1853 assertEquals("testing", sq.getName());
1855 sq.setName("test/8-10");
1856 assertEquals("test", sq.getName());
1857 assertEquals(8, sq.getStart());
1858 assertEquals(13, sq.getEnd()); // note end is recomputed
1860 sq.setName("testing/7-99");
1861 assertEquals("testing", sq.getName());
1862 assertEquals(7, sq.getStart());
1863 assertEquals(99, sq.getEnd()); // end may be beyond physical end
1866 assertEquals("", sq.getName());
1867 assertEquals(2, sq.getStart());
1868 assertEquals(7, sq.getEnd());
1870 sq.setName("test/"); // invalid
1871 assertEquals("test/", sq.getName());
1872 assertEquals(2, sq.getStart());
1873 assertEquals(7, sq.getEnd());
1875 sq.setName("test/6-13/7-99");
1876 assertEquals("test/6-13", sq.getName());
1877 assertEquals(7, sq.getStart());
1878 assertEquals(99, sq.getEnd());
1880 sq.setName("test/0-5"); // 0 is invalid - ignored
1881 assertEquals("test/0-5", sq.getName());
1882 assertEquals(7, sq.getStart());
1883 assertEquals(99, sq.getEnd());
1885 sq.setName("test/a-5"); // a is invalid - ignored
1886 assertEquals("test/a-5", sq.getName());
1887 assertEquals(7, sq.getStart());
1888 assertEquals(99, sq.getEnd());
1890 sq.setName("test/6-5"); // start > end is invalid - ignored
1891 assertEquals("test/6-5", sq.getName());
1892 assertEquals(7, sq.getStart());
1893 assertEquals(99, sq.getEnd());
1895 sq.setName("test/5"); // invalid - ignored
1896 assertEquals("test/5", sq.getName());
1897 assertEquals(7, sq.getStart());
1898 assertEquals(99, sq.getEnd());
1900 sq.setName("test/-5"); // invalid - ignored
1901 assertEquals("test/-5", sq.getName());
1902 assertEquals(7, sq.getStart());
1903 assertEquals(99, sq.getEnd());
1905 sq.setName("test/5-"); // invalid - ignored
1906 assertEquals("test/5-", sq.getName());
1907 assertEquals(7, sq.getStart());
1908 assertEquals(99, sq.getEnd());
1910 sq.setName("test/5-6-7"); // invalid - ignored
1911 assertEquals("test/5-6-7", sq.getName());
1912 assertEquals(7, sq.getStart());
1913 assertEquals(99, sq.getEnd());
1915 sq.setName(null); // invalid, gets converted to space
1916 assertEquals("", sq.getName());
1917 assertEquals(7, sq.getStart());
1918 assertEquals(99, sq.getEnd());
1921 @Test(groups = { "Functional" })
1922 public void testCheckValidRange()
1924 Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
1925 assertEquals(7, sq.getStart());
1926 assertEquals(12, sq.getEnd());
1929 * checkValidRange ensures end is at least the last residue position
1931 PA.setValue(sq, "end", 2);
1932 sq.checkValidRange();
1933 assertEquals(12, sq.getEnd());
1936 * end may be beyond the last residue position
1938 PA.setValue(sq, "end", 22);
1939 sq.checkValidRange();
1940 assertEquals(22, sq.getEnd());
1943 @Test(groups = { "Functional" })
1944 public void testDeleteChars_withGaps()
1949 SequenceI sq = new Sequence("test/8-10", "A-B-C");
1950 sq.createDatasetSequence();
1951 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
1952 sq.deleteChars(1, 2); // delete first gap
1953 assertEquals("AB-C", sq.getSequenceAsString());
1954 assertEquals(8, sq.getStart());
1955 assertEquals(10, sq.getEnd());
1956 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
1959 * delete gaps and residues at start (no new dataset sequence)
1961 sq = new Sequence("test/8-10", "A-B-C");
1962 sq.createDatasetSequence();
1963 sq.deleteChars(0, 3); // delete A-B
1964 assertEquals("-C", sq.getSequenceAsString());
1965 assertEquals(10, sq.getStart());
1966 assertEquals(10, sq.getEnd());
1967 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
1970 * delete gaps and residues at end (no new dataset sequence)
1972 sq = new Sequence("test/8-10", "A-B-C");
1973 sq.createDatasetSequence();
1974 sq.deleteChars(2, 5); // delete B-C
1975 assertEquals("A-", sq.getSequenceAsString());
1976 assertEquals(8, sq.getStart());
1977 assertEquals(8, sq.getEnd());
1978 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
1981 * delete gaps and residues internally (new dataset sequence)
1982 * first delete from gap to residue
1984 sq = new Sequence("test/8-10", "A-B-C");
1985 sq.createDatasetSequence();
1986 sq.deleteChars(1, 3); // delete -B
1987 assertEquals("A-C", sq.getSequenceAsString());
1988 assertEquals(8, sq.getStart());
1989 assertEquals(9, sq.getEnd());
1990 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
1991 assertEquals(8, sq.getDatasetSequence().getStart());
1992 assertEquals(9, sq.getDatasetSequence().getEnd());
1995 * internal delete from gap to gap
1997 sq = new Sequence("test/8-10", "A-B-C");
1998 sq.createDatasetSequence();
1999 sq.deleteChars(1, 4); // delete -B-
2000 assertEquals("AC", sq.getSequenceAsString());
2001 assertEquals(8, sq.getStart());
2002 assertEquals(9, sq.getEnd());
2003 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2004 assertEquals(8, sq.getDatasetSequence().getStart());
2005 assertEquals(9, sq.getDatasetSequence().getEnd());
2008 * internal delete from residue to residue
2010 sq = new Sequence("test/8-10", "A-B-C");
2011 sq.createDatasetSequence();
2012 sq.deleteChars(2, 3); // delete B
2013 assertEquals("A--C", sq.getSequenceAsString());
2014 assertEquals(8, sq.getStart());
2015 assertEquals(9, sq.getEnd());
2016 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2017 assertEquals(8, sq.getDatasetSequence().getStart());
2018 assertEquals(9, sq.getDatasetSequence().getEnd());
2022 * Test the code used to locate the reference sequence ruler origin
2024 @Test(groups = { "Functional" })
2025 public void testLocateVisibleStartofSequence()
2027 // create random alignment
2028 AlignmentGenerator gen = new AlignmentGenerator(false);
2029 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
2031 HiddenColumns cs = al.getHiddenColumns();
2032 ColumnSelection colsel = new ColumnSelection();
2034 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
2035 assertEquals(2, seq.findIndex(seq.getStart()));
2037 // no hidden columns
2038 assertEquals(seq.findIndex(seq.getStart()) - 1,
2039 seq.firstResidueOutsideIterator(cs.iterator()));
2041 // hidden column on gap after end of sequence - should not affect bounds
2042 colsel.hideSelectedColumns(13, al.getHiddenColumns());
2043 assertEquals(seq.findIndex(seq.getStart()) - 1,
2044 seq.firstResidueOutsideIterator(cs.iterator()));
2046 cs.revealAllHiddenColumns(colsel);
2047 // hidden column on gap before beginning of sequence - should vis bounds by
2049 colsel.hideSelectedColumns(0, al.getHiddenColumns());
2050 assertEquals(seq.findIndex(seq.getStart()) - 2,
2051 cs.absoluteToVisibleColumn(
2052 seq.firstResidueOutsideIterator(cs.iterator())));
2054 cs.revealAllHiddenColumns(colsel);
2055 // hide columns around most of sequence - leave one residue remaining
2056 cs.hideColumns(1, 3);
2057 cs.hideColumns(6, 11);
2059 Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
2061 assertEquals("-D", seq.getSequenceStringFromIterator(it));
2062 // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
2065 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2066 cs.revealAllHiddenColumns(colsel);
2068 // hide whole sequence - should just get location of hidden region
2069 // containing sequence
2070 cs.hideColumns(1, 11);
2071 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2073 cs.revealAllHiddenColumns(colsel);
2074 cs.hideColumns(0, 15);
2075 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2077 SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
2079 cs.revealAllHiddenColumns(colsel);
2080 cs.hideColumns(7, 17);
2081 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2083 cs.revealAllHiddenColumns(colsel);
2084 cs.hideColumns(3, 17);
2085 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2087 cs.revealAllHiddenColumns(colsel);
2088 cs.hideColumns(3, 19);
2089 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2091 cs.revealAllHiddenColumns(colsel);
2092 cs.hideColumns(0, 0);
2093 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2095 cs.revealAllHiddenColumns(colsel);
2096 cs.hideColumns(0, 1);
2097 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2099 cs.revealAllHiddenColumns(colsel);
2100 cs.hideColumns(0, 2);
2101 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2103 cs.revealAllHiddenColumns(colsel);
2104 cs.hideColumns(1, 1);
2105 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2107 cs.revealAllHiddenColumns(colsel);
2108 cs.hideColumns(1, 2);
2109 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2111 cs.revealAllHiddenColumns(colsel);
2112 cs.hideColumns(1, 3);
2113 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2115 cs.revealAllHiddenColumns(colsel);
2116 cs.hideColumns(0, 2);
2117 cs.hideColumns(5, 6);
2118 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2120 cs.revealAllHiddenColumns(colsel);
2121 cs.hideColumns(0, 2);
2122 cs.hideColumns(5, 6);
2123 cs.hideColumns(9, 10);
2124 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2126 cs.revealAllHiddenColumns(colsel);
2127 cs.hideColumns(0, 2);
2128 cs.hideColumns(7, 11);
2129 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2131 cs.revealAllHiddenColumns(colsel);
2132 cs.hideColumns(2, 4);
2133 cs.hideColumns(7, 11);
2134 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2136 cs.revealAllHiddenColumns(colsel);
2137 cs.hideColumns(2, 4);
2138 cs.hideColumns(7, 12);
2139 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2141 cs.revealAllHiddenColumns(colsel);
2142 cs.hideColumns(1, 11);
2143 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2145 cs.revealAllHiddenColumns(colsel);
2146 cs.hideColumns(0, 12);
2147 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2149 cs.revealAllHiddenColumns(colsel);
2150 cs.hideColumns(0, 4);
2151 cs.hideColumns(6, 12);
2152 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2154 cs.revealAllHiddenColumns(colsel);
2155 cs.hideColumns(0, 1);
2156 cs.hideColumns(3, 12);
2157 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2159 cs.revealAllHiddenColumns(colsel);
2160 cs.hideColumns(3, 14);
2161 cs.hideColumns(17, 19);
2162 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2164 cs.revealAllHiddenColumns(colsel);
2165 cs.hideColumns(3, 7);
2166 cs.hideColumns(9, 14);
2167 cs.hideColumns(17, 19);
2168 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2170 cs.revealAllHiddenColumns(colsel);
2171 cs.hideColumns(0, 1);
2172 cs.hideColumns(3, 4);
2173 cs.hideColumns(6, 8);
2174 cs.hideColumns(10, 12);
2175 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));