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;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.BitSet;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Locale;
38 import java.util.Vector;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeClass;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
45 import jalview.analysis.AlignmentGenerator;
46 import jalview.bin.Cache;
47 import jalview.commands.EditCommand;
48 import jalview.commands.EditCommand.Action;
49 import jalview.datamodel.PDBEntry.Type;
50 import jalview.gui.JvOptionPane;
51 import jalview.util.MapList;
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);
63 @BeforeMethod(alwaysRun = true)
64 public void loadProperties()
66 Cache.loadProperties("test/jalview/util/comparisonTestProps.jvprops");
71 @BeforeMethod(alwaysRun = true)
74 seq = new Sequence("FER1", "AKPNGVL");
77 @Test(groups = { "Functional" })
78 public void testInsertGapsAndGapmaps()
80 SequenceI aseq = seq.deriveSequence();
81 aseq.insertCharAt(2, 3, '-');
82 aseq.insertCharAt(6, 3, '-');
83 assertEquals("Gap insertions not correct", "AK---P---NGVL",
84 aseq.getSequenceAsString());
85 List<int[]> gapInt = aseq.getInsertions();
86 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
87 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
88 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
89 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
91 BitSet gapfield = aseq.getInsertionsAsBits();
92 BitSet expectedgaps = new BitSet();
93 expectedgaps.set(2, 5);
94 expectedgaps.set(6, 9);
96 assertEquals(6, expectedgaps.cardinality());
98 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
99 6, gapfield.cardinality());
101 assertEquals("getInsertionsAsBits not correct.", expectedgaps,
105 @Test(groups = ("Functional"))
106 public void testIsProtein()
109 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
111 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
113 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
114 assertFalse(sq.isProtein());
115 // change sequence, should trigger an update of cached result
116 sq.setSequence("ASDFASDFADSF");
117 assertTrue(sq.isProtein());
120 @Test(groups = ("Functional"))
121 public void testIsProteinWithXorNAmbiguityCodes()
123 // test Protein with N - poly asparagine
124 assertTrue(new Sequence("prot", "ASDFASDFASDFNNNNNNNNN").isProtein());
125 assertTrue(new Sequence("prot", "NNNNNNNNNNNNNNNNNNNNN").isProtein());
126 // test Protein with X
127 assertTrue(new Sequence("prot", "ASDFASDFASDFXXXXXXXXX").isProtein());
129 assertFalse(new Sequence("prot", "ACGTACGTACGTXXXXXXXX").isProtein());
130 // short sequence is nucleotide only if 50% is nucleotide and remaining N/X
131 // is either N or X only
132 assertTrue(new Sequence("prot", "ACGTACGTACGTXN").isProtein());
134 assertFalse(new Sequence("prot", "ACGTACGTACGTNNNNNNNN").isProtein());
136 assertFalse(new Sequence("prot", "ACGUACGUACGUACTGACAXX").isProtein());
137 assertFalse(new Sequence("prot", "ACGUACGUACGUXXXXXXXXX").isProtein());
138 assertFalse(new Sequence("prot", "ACGUACGUACGUNNNNNNNNN").isProtein());
141 @Test(groups = { "Functional" })
142 public void testGetAnnotation()
144 // initial state returns null not an empty array
145 assertNull(seq.getAnnotation());
146 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
148 AlignmentAnnotation[] anns = seq.getAnnotation();
149 assertEquals(1, anns.length);
150 assertSame(ann, anns[0]);
152 // removing all annotations reverts array to null
153 seq.removeAlignmentAnnotation(ann);
154 assertNull(seq.getAnnotation());
157 @Test(groups = { "Functional" })
158 public void testGetAnnotation_forLabel()
160 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
162 addAnnotation("label2", "desc2", "calcId2", 1f);
163 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
165 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
166 assertEquals(2, anns.length);
167 assertSame(ann1, anns[0]);
168 assertSame(ann3, anns[1]);
171 private AlignmentAnnotation addAnnotation(String label,
172 String description, String calcId, float value)
174 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
176 annotation.setCalcId(calcId);
177 seq.addAlignmentAnnotation(annotation);
181 @Test(groups = { "Functional" })
182 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
184 addAnnotation("label1", "desc1", "calcId1", 1f);
185 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
187 addAnnotation("label2", "desc3", "calcId3", 1f);
188 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
190 addAnnotation("label5", "desc3", null, 1f);
191 addAnnotation(null, "desc3", "calcId3", 1f);
193 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
195 assertEquals(2, anns.size());
196 assertSame(ann2, anns.get(0));
197 assertSame(ann4, anns.get(1));
199 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
200 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
201 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
202 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
203 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
206 @Test(groups = { "Functional" })
207 public void testGetAlignmentAnnotations_forCalcIdLabelAndDescription()
209 addAnnotation("label1", "desc1", "calcId1", 1f);
210 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
212 addAnnotation("label2", "desc3", "calcId3", 1f);
213 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
215 addAnnotation("label5", "desc3", null, 1f);
216 addAnnotation(null, "desc3", "calcId3", 1f);
218 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
220 assertEquals(1, anns.size());
221 assertSame(ann4, anns.get(0));
223 * null matching should fail
225 assertTrue(seq.getAlignmentAnnotations("calcId3", "label2", null)
228 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3", null)
230 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5", null)
233 seq.getAlignmentAnnotations("calcId2", null, null).isEmpty());
234 assertTrue(seq.getAlignmentAnnotations(null, "label3", null).isEmpty());
235 assertTrue(seq.getAlignmentAnnotations(null, null, null).isEmpty());
239 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
240 * setting the sequenceRef on the annotation. Adding the same annotation twice
243 @Test(groups = { "Functional" })
244 public void testAddAlignmentAnnotation()
246 assertNull(seq.getAnnotation());
247 final AlignmentAnnotation annotation = new AlignmentAnnotation("a", "b",
249 assertNull(annotation.sequenceRef);
250 seq.addAlignmentAnnotation(annotation);
251 assertSame(seq, annotation.sequenceRef);
252 AlignmentAnnotation[] anns = seq.getAnnotation();
253 assertEquals(1, anns.length);
254 assertSame(annotation, anns[0]);
256 // re-adding does nothing
257 seq.addAlignmentAnnotation(annotation);
258 anns = seq.getAnnotation();
259 assertEquals(1, anns.length);
260 assertSame(annotation, anns[0]);
262 // an identical but different annotation can be added
263 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
265 seq.addAlignmentAnnotation(annotation2);
266 anns = seq.getAnnotation();
267 assertEquals(2, anns.length);
268 assertSame(annotation, anns[0]);
269 assertSame(annotation2, anns[1]);
272 @Test(groups = { "Functional" })
273 public void testGetStartGetEnd()
275 SequenceI sq = new Sequence("test", "ABCDEF");
276 assertEquals(1, sq.getStart());
277 assertEquals(6, sq.getEnd());
279 sq = new Sequence("test", "--AB-C-DEF--");
280 assertEquals(1, sq.getStart());
281 assertEquals(6, sq.getEnd());
283 sq = new Sequence("test", "----");
284 assertEquals(1, sq.getStart());
285 assertEquals(0, sq.getEnd()); // ??
289 * Tests for the method that returns an alignment column position (base 1) for
290 * a given sequence position (base 1).
292 @Test(groups = { "Functional" })
293 public void testFindIndex()
296 * call sequenceChanged() after each test to invalidate any cursor,
297 * forcing the 1-arg findIndex to be executed
299 SequenceI sq = new Sequence("test", "ABCDEF");
300 assertEquals(0, sq.findIndex(0));
301 sq.sequenceChanged();
302 assertEquals(1, sq.findIndex(1));
303 sq.sequenceChanged();
304 assertEquals(5, sq.findIndex(5));
305 sq.sequenceChanged();
306 assertEquals(6, sq.findIndex(6));
307 sq.sequenceChanged();
308 assertEquals(6, sq.findIndex(9));
310 final String aligned = "-A--B-C-D-E-F--";
311 assertEquals(15, aligned.length());
312 sq = new Sequence("test/8-13", aligned);
313 assertEquals(2, sq.findIndex(8));
314 sq.sequenceChanged();
315 assertEquals(5, sq.findIndex(9));
316 sq.sequenceChanged();
317 assertEquals(7, sq.findIndex(10));
319 // before start returns 0
320 sq.sequenceChanged();
321 assertEquals(0, sq.findIndex(0));
322 sq.sequenceChanged();
323 assertEquals(0, sq.findIndex(-1));
325 // beyond end returns last residue column
326 sq.sequenceChanged();
327 assertEquals(13, sq.findIndex(99));
330 * residue before sequence 'end' but beyond end of sequence returns
331 * length of sequence (last column) (rightly or wrongly!)
333 sq = new Sequence("test/8-15", "A-B-C-"); // trailing gap case
334 assertEquals(6, sq.getLength());
335 sq.sequenceChanged();
336 assertEquals(sq.getLength(), sq.findIndex(14));
337 sq = new Sequence("test/8-99", "-A--B-C-D"); // trailing residue case
338 sq.sequenceChanged();
339 assertEquals(sq.getLength(), sq.findIndex(65));
342 * residue after sequence 'start' but before first residue returns
343 * zero (before first column) (rightly or wrongly!)
345 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
346 sq.sequenceChanged();
347 assertEquals(0, sq.findIndex(3));
348 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
349 sq.sequenceChanged();
350 assertEquals(0, sq.findIndex(2));
353 @Test(groups = { "Functional" })
354 public void testFindPositions()
356 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
361 assertNull(sq.findPositions(6, 5));
362 assertNull(sq.findPositions(0, 5));
363 assertNull(sq.findPositions(-1, 5));
368 assertNull(sq.findPositions(1, 1)); // 1-based columns
369 assertNull(sq.findPositions(5, 5));
370 assertNull(sq.findPositions(5, 6));
371 assertNull(sq.findPositions(5, 7));
374 * all ungapped ranges
376 assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
377 assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
378 assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
379 assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
382 * gap to ungapped range
384 assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
385 assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
388 * ungapped to gapped range
390 assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
391 assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
394 * ungapped to ungapped enclosing gaps
396 assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
397 assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
400 * gapped to gapped enclosing ungapped
402 assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
403 assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
404 assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
405 assertEquals(new Range(8, 13), sq.findPositions(1, 99));
409 * Tests for the method that returns a dataset sequence position (start..) for
410 * an aligned column position (base 0).
412 @Test(groups = { "Functional" })
413 public void testFindPosition()
416 * call sequenceChanged() after each test to invalidate any cursor,
417 * forcing the 1-arg findPosition to be executed
419 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
420 assertEquals(8, sq.findPosition(0));
421 // Sequence should now hold a cursor at [8, 0]
422 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
423 PA.getValue(sq, "cursor").toString());
424 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
425 int token = (int) PA.getValue(sq, "changeCount");
426 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
428 sq.sequenceChanged();
431 * find F13 at column offset 5, cursor should update to [13, 6]
432 * endColumn is found and saved in cursor
434 assertEquals(13, sq.findPosition(5));
435 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
436 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
437 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
438 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok2",
439 PA.getValue(sq, "cursor").toString());
441 // assertEquals(-1, seq.findPosition(6)); // fails
443 sq = new Sequence("test/8-11", "AB-C-D--");
444 token = (int) PA.getValue(sq, "changeCount"); // 1 for setStart
445 assertEquals(8, sq.findPosition(0));
446 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
447 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
448 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
449 PA.getValue(sq, "cursor").toString());
451 sq.sequenceChanged();
452 assertEquals(9, sq.findPosition(1));
453 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
454 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
455 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
456 PA.getValue(sq, "cursor").toString());
458 sq.sequenceChanged();
459 // gap position 'finds' residue to the right (not the left as per javadoc)
460 // cursor is set to the last residue position found [B 2]
461 assertEquals(10, sq.findPosition(2));
462 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
463 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
464 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok3",
465 PA.getValue(sq, "cursor").toString());
467 sq.sequenceChanged();
468 assertEquals(10, sq.findPosition(3));
469 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
470 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
471 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
472 PA.getValue(sq, "cursor").toString());
474 sq.sequenceChanged();
475 // column[4] is the gap after C - returns D11
476 // cursor is set to [C 4]
477 assertEquals(11, sq.findPosition(4));
478 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
479 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
480 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok5",
481 PA.getValue(sq, "cursor").toString());
483 sq.sequenceChanged();
484 assertEquals(11, sq.findPosition(5)); // D
485 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
486 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
487 // lastCol has been found and saved in the cursor
488 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok6",
489 PA.getValue(sq, "cursor").toString());
491 sq.sequenceChanged();
492 // returns 1 more than sequence length if off the end ?!?
493 assertEquals(12, sq.findPosition(6));
495 sq.sequenceChanged();
496 assertEquals(12, sq.findPosition(7));
499 * first findPosition should also set firstResCol in cursor
501 sq = new Sequence("test/8-13", "--AB-C-DEF--");
502 assertEquals(8, sq.findPosition(0));
503 assertNull(PA.getValue(sq, "cursor"));
504 assertEquals(1, PA.getValue(sq, "changeCount"));
506 sq.sequenceChanged();
507 assertEquals(8, sq.findPosition(1));
508 assertNull(PA.getValue(sq, "cursor"));
510 sq.sequenceChanged();
511 assertEquals(8, sq.findPosition(2));
512 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok3",
513 PA.getValue(sq, "cursor").toString());
515 sq.sequenceChanged();
516 assertEquals(9, sq.findPosition(3));
517 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
518 PA.getValue(sq, "cursor").toString());
520 sq.sequenceChanged();
521 // column[4] is a gap, returns next residue pos (C10)
522 // cursor is set to last residue found [B]
523 assertEquals(10, sq.findPosition(4));
524 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok5",
525 PA.getValue(sq, "cursor").toString());
527 sq.sequenceChanged();
528 assertEquals(10, sq.findPosition(5));
529 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
530 PA.getValue(sq, "cursor").toString());
532 sq.sequenceChanged();
533 // column[6] is a gap, returns next residue pos (D11)
534 // cursor is set to last residue found [C]
535 assertEquals(11, sq.findPosition(6));
536 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok7",
537 PA.getValue(sq, "cursor").toString());
539 sq.sequenceChanged();
540 assertEquals(11, sq.findPosition(7));
541 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok8",
542 PA.getValue(sq, "cursor").toString());
544 sq.sequenceChanged();
545 assertEquals(12, sq.findPosition(8));
546 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok9",
547 PA.getValue(sq, "cursor").toString());
550 * when the last residue column is found, it is set in the cursor
552 sq.sequenceChanged();
553 assertEquals(13, sq.findPosition(9));
554 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
555 PA.getValue(sq, "cursor").toString());
557 sq.sequenceChanged();
558 assertEquals(14, sq.findPosition(10));
559 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
560 PA.getValue(sq, "cursor").toString());
563 * findPosition for column beyond sequence length
564 * returns 1 more than last residue position
566 sq.sequenceChanged();
567 assertEquals(14, sq.findPosition(11));
568 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
569 PA.getValue(sq, "cursor").toString());
571 sq.sequenceChanged();
572 assertEquals(14, sq.findPosition(99));
573 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok13",
574 PA.getValue(sq, "cursor").toString());
577 * gapped sequence ending in non-gap
579 sq = new Sequence("test/8-13", "--AB-C-DEF");
580 assertEquals(13, sq.findPosition(9));
581 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
582 PA.getValue(sq, "cursor").toString());
583 sq.sequenceChanged();
584 assertEquals(12, sq.findPosition(8)); // E12
585 // sequenceChanged() invalidates cursor.lastResidueColumn
586 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
587 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok2",
589 // findPosition with cursor accepts base 1 column values
590 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
591 assertEquals(13, sq.findPosition(9)); // F13
592 // lastResidueColumn has now been found and saved in cursor
593 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok2",
594 PA.getValue(sq, "cursor").toString());
597 @Test(groups = { "Functional" })
598 public void testDeleteChars()
603 SequenceI sq = new Sequence("test", "ABCDEF");
604 assertNull(PA.getValue(sq, "datasetSequence"));
605 assertEquals(1, sq.getStart());
606 assertEquals(6, sq.getEnd());
607 sq.deleteChars(2, 3);
608 assertEquals("ABDEF", sq.getSequenceAsString());
609 assertEquals(1, sq.getStart());
610 assertEquals(5, sq.getEnd());
611 assertNull(PA.getValue(sq, "datasetSequence"));
616 sq = new Sequence("test", "ABCDEF");
617 sq.deleteChars(0, 2);
618 assertEquals("CDEF", sq.getSequenceAsString());
619 assertEquals(3, sq.getStart());
620 assertEquals(6, sq.getEnd());
621 assertNull(PA.getValue(sq, "datasetSequence"));
623 sq = new Sequence("test", "ABCDE");
624 sq.deleteChars(0, 3);
625 assertEquals("DE", sq.getSequenceAsString());
626 assertEquals(4, sq.getStart());
627 assertEquals(5, sq.getEnd());
628 assertNull(PA.getValue(sq, "datasetSequence"));
633 sq = new Sequence("test", "ABCDEF");
634 sq.deleteChars(4, 6);
635 assertEquals("ABCD", sq.getSequenceAsString());
636 assertEquals(1, sq.getStart());
637 assertEquals(4, sq.getEnd());
638 assertNull(PA.getValue(sq, "datasetSequence"));
641 * delete more positions than there are
643 sq = new Sequence("test/8-11", "ABCD");
644 sq.deleteChars(0, 99);
645 assertEquals("", sq.getSequenceAsString());
646 assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
647 assertEquals(11, sq.getEnd());
649 sq = new Sequence("test/8-11", "----");
650 sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
651 assertEquals("", sq.getSequenceAsString());
652 assertEquals(8, sq.getStart());
653 assertEquals(11, sq.getEnd());
656 @Test(groups = { "Functional" })
657 public void testDeleteChars_withDbRefsAndFeatures()
660 * internal delete - new dataset sequence created
661 * gets a copy of any dbrefs
663 SequenceI sq = new Sequence("test", "ABCDEF");
664 sq.createDatasetSequence();
665 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
667 Object ds = PA.getValue(sq, "datasetSequence");
669 assertEquals(1, sq.getStart());
670 assertEquals(6, sq.getEnd());
671 sq.deleteChars(2, 3);
672 assertEquals("ABDEF", sq.getSequenceAsString());
673 assertEquals(1, sq.getStart());
674 assertEquals(5, sq.getEnd());
675 Object newDs = PA.getValue(sq, "datasetSequence");
676 assertNotNull(newDs);
677 assertNotSame(ds, newDs);
678 assertNotNull(sq.getDBRefs());
679 assertEquals(1, sq.getDBRefs().size());
680 assertNotSame(dbr1, sq.getDBRefs().get(0));
681 assertEquals(dbr1, sq.getDBRefs().get(0));
684 * internal delete with sequence features
685 * (failure case for JAL-2541)
687 sq = new Sequence("test", "ABCDEF");
688 sq.createDatasetSequence();
689 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
691 sq.addSequenceFeature(sf1);
692 ds = PA.getValue(sq, "datasetSequence");
694 assertEquals(1, sq.getStart());
695 assertEquals(6, sq.getEnd());
696 sq.deleteChars(2, 4);
697 assertEquals("ABEF", sq.getSequenceAsString());
698 assertEquals(1, sq.getStart());
699 assertEquals(4, sq.getEnd());
700 newDs = PA.getValue(sq, "datasetSequence");
701 assertNotNull(newDs);
702 assertNotSame(ds, newDs);
703 List<SequenceFeature> sfs = sq.getSequenceFeatures();
704 assertEquals(1, sfs.size());
705 assertNotSame(sf1, sfs.get(0));
706 assertEquals(sf1, sfs.get(0));
709 * delete at start - no new dataset sequence created
710 * any sequence features remain as before
712 sq = new Sequence("test", "ABCDEF");
713 sq.createDatasetSequence();
714 ds = PA.getValue(sq, "datasetSequence");
715 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
716 sq.addSequenceFeature(sf1);
717 sq.deleteChars(0, 2);
718 assertEquals("CDEF", sq.getSequenceAsString());
719 assertEquals(3, sq.getStart());
720 assertEquals(6, sq.getEnd());
721 assertSame(ds, PA.getValue(sq, "datasetSequence"));
722 sfs = sq.getSequenceFeatures();
724 assertEquals(1, sfs.size());
725 assertSame(sf1, sfs.get(0));
728 * delete at end - no new dataset sequence created
729 * any dbrefs remain as before
731 sq = new Sequence("test", "ABCDEF");
732 sq.createDatasetSequence();
733 ds = PA.getValue(sq, "datasetSequence");
734 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
736 sq.deleteChars(4, 6);
737 assertEquals("ABCD", sq.getSequenceAsString());
738 assertEquals(1, sq.getStart());
739 assertEquals(4, sq.getEnd());
740 assertSame(ds, PA.getValue(sq, "datasetSequence"));
741 assertNotNull(sq.getDBRefs());
742 assertEquals(1, sq.getDBRefs().size());
743 assertSame(dbr1, sq.getDBRefs().get(0));
746 @Test(groups = { "Functional" })
747 public void testInsertCharAt()
749 // non-static methods:
750 SequenceI sq = new Sequence("test", "ABCDEF");
751 sq.insertCharAt(0, 'z');
752 assertEquals("zABCDEF", sq.getSequenceAsString());
753 sq.insertCharAt(2, 2, 'x');
754 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
756 // for static method see StringUtilsTest
760 * Test the method that returns an array of aligned sequence positions where
761 * the array index is the data sequence position (both base 0).
763 @Test(groups = { "Functional" })
764 public void testGapMap()
766 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
767 sq.createDatasetSequence();
768 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
772 * Test the method that gets sequence features, either from the sequence or
775 @Test(groups = { "Functional" })
776 public void testGetSequenceFeatures()
778 SequenceI sq = new Sequence("test", "GATCAT");
779 sq.createDatasetSequence();
781 assertTrue(sq.getSequenceFeatures().isEmpty());
784 * SequenceFeature on sequence
786 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f,
788 sq.addSequenceFeature(sf);
789 List<SequenceFeature> sfs = sq.getSequenceFeatures();
790 assertEquals(1, sfs.size());
791 assertSame(sf, sfs.get(0));
794 * SequenceFeature on sequence and dataset sequence; returns that on
797 * Note JAL-2046: spurious: we have no use case for this at the moment.
798 * This test also buggy - as sf2.equals(sf), no new feature is added
800 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
802 sq.getDatasetSequence().addSequenceFeature(sf2);
803 sfs = sq.getSequenceFeatures();
804 assertEquals(1, sfs.size());
805 assertSame(sf, sfs.get(0));
808 * SequenceFeature on dataset sequence only
809 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
811 sq.setSequenceFeatures(null);
812 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
815 * Corrupt case - no SequenceFeature, dataset's dataset is the original
816 * sequence. Test shows no infinite loop results.
818 sq.getDatasetSequence().setSequenceFeatures(null);
820 * is there a usecase for this ? setDatasetSequence should throw an error if
821 * this actually occurs.
825 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
827 "Expected Error to be raised when calling setDatasetSequence with self reference");
828 } catch (IllegalArgumentException e)
830 // TODO Jalview error/exception class for raising implementation errors
831 assertTrue(e.getMessage().toLowerCase(Locale.ROOT)
832 .contains("implementation error"));
834 assertTrue(sq.getSequenceFeatures().isEmpty());
838 * Test the method that returns an array, indexed by sequence position, whose
839 * entries are the residue positions at the sequence position (or to the right
842 @Test(groups = { "Functional" })
843 public void testFindPositionMap()
846 * Note: Javadoc for findPosition says it returns the residue position to
847 * the left of a gapped position; in fact it returns the position to the
848 * right. Also it returns a non-existent residue position for a gap beyond
851 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
852 int[] map = sq.findPositionMap();
853 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
854 Arrays.toString(map));
858 * Test for getSubsequence
860 @Test(groups = { "Functional" })
861 public void testGetSubsequence()
863 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
864 sq.createDatasetSequence();
866 // positions are base 0, end position is exclusive
867 SequenceI subseq = sq.getSubSequence(2, 4);
869 assertEquals("CD", subseq.getSequenceAsString());
870 // start/end are base 1 positions
871 assertEquals(3, subseq.getStart());
872 assertEquals(4, subseq.getEnd());
873 // subsequence shares the full dataset sequence
874 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
878 * test createDatasetSequence behaves to doc
880 @Test(groups = { "Functional" })
881 public void testCreateDatasetSequence()
883 SequenceI sq = new Sequence("my", "ASDASD");
884 sq.addSequenceFeature(
885 new SequenceFeature("type", "desc", 1, 10, 1f, "group"));
886 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
887 assertNull(sq.getDatasetSequence());
888 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
889 assertNotNull(PA.getValue(sq, "dbrefs"));
891 SequenceI rds = sq.createDatasetSequence();
893 assertNull(rds.getDatasetSequence());
894 assertSame(sq.getDatasetSequence(), rds);
896 // sequence features and dbrefs transferred to dataset sequence
897 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
898 assertNull(PA.getValue(sq, "dbrefs"));
899 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
900 assertNotNull(PA.getValue(rds, "dbrefs"));
904 * Test for deriveSequence applied to a sequence with a dataset
906 @Test(groups = { "Functional" })
907 public void testDeriveSequence_existingDataset()
909 Sequence sq = new Sequence("Seq1", "CD");
910 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
911 sq.getDatasetSequence().addSequenceFeature(
912 new SequenceFeature("", "", 1, 2, 0f, null));
916 sq.setDescription("Test sequence description..");
917 sq.setVamsasId("TestVamsasId");
918 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
920 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
921 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
922 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
923 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
925 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
926 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
927 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
928 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
930 // these are the same as ones already added
931 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
932 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
934 List<DBRefEntry> primRefs = Arrays
935 .asList(new DBRefEntry[]
936 { pdb1pdb, pdb2pdb });
938 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
939 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
940 sq.getDatasetSequence()
941 .addDBRef(new DBRefEntry("PDB", "version3", "3PDB")); // should do
943 sq.getDatasetSequence()
944 .addDBRef(new DBRefEntry("PDB", "version4", "4PDB")); // should do
947 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
948 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
949 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
951 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
953 sq.getDatasetSequence().addPDBId(pdbe1a);
954 sq.getDatasetSequence().addPDBId(pdbe1b);
955 sq.getDatasetSequence().addPDBId(pdbe2a);
956 sq.getDatasetSequence().addPDBId(pdbe2b);
959 * test we added pdb entries to the dataset sequence
961 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(),
962 Arrays.asList(new PDBEntry[]
963 { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
964 "PDB Entries were not found on dataset sequence.");
967 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
969 Assert.assertEquals(pdbe1a, sq.getDatasetSequence().getPDBEntry("1PDB"),
970 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
971 ArrayList<Annotation> annotsList = new ArrayList<>();
972 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
973 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
974 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
975 Annotation[] annots = annotsList.toArray(new Annotation[0]);
976 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
977 "Test annot description", annots));
978 sq.getDatasetSequence().addAlignmentAnnotation(new AlignmentAnnotation(
979 "Test annot", "Test annot description", annots));
980 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
981 Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
983 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
984 Assert.assertNotNull(sq.getAnnotation());
985 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
986 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
989 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
991 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
993 Sequence derived = (Sequence) sq.deriveSequence();
995 Assert.assertEquals(derived.getDescription(),
996 "Test sequence description..");
997 Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
998 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
999 Assert.assertNotNull(derived.getAnnotation());
1000 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
1001 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
1002 Assert.assertEquals(
1003 derived.getDatasetSequence().getAllPDBEntries().size(), 4);
1004 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
1006 assertEquals("CD", derived.getSequenceAsString());
1007 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
1009 // derived sequence should access dataset sequence features
1010 assertNotNull(sq.getSequenceFeatures());
1011 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
1014 * verify we have primary db refs *just* for PDB IDs with associated
1018 assertEquals(primRefs, sq.getPrimaryDBRefs());
1019 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
1021 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
1026 * Test for deriveSequence applied to an ungapped sequence with no dataset
1028 @Test(groups = { "Functional" })
1029 public void testDeriveSequence_noDatasetUngapped()
1031 SequenceI sq = new Sequence("Seq1", "ABCDEF");
1032 assertEquals(1, sq.getStart());
1033 assertEquals(6, sq.getEnd());
1034 SequenceI derived = sq.deriveSequence();
1035 assertEquals("ABCDEF", derived.getSequenceAsString());
1036 assertEquals("ABCDEF",
1037 derived.getDatasetSequence().getSequenceAsString());
1041 * Test for deriveSequence applied to a gapped sequence with no dataset
1043 @Test(groups = { "Functional" })
1044 public void testDeriveSequence_noDatasetGapped()
1046 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
1047 assertEquals(1, sq.getStart());
1048 assertEquals(6, sq.getEnd());
1049 assertNull(sq.getDatasetSequence());
1050 SequenceI derived = sq.deriveSequence();
1051 assertEquals("AB-C.D EF", derived.getSequenceAsString());
1052 assertEquals("ABCDEF",
1053 derived.getDatasetSequence().getSequenceAsString());
1057 * test that creating a copy of an existing sequence with dataset sequence and
1058 * associated contact matrix yields annotation associated with the same
1059 * contact matrix in the copy
1061 @Test(groups= {"Functional"})
1062 public void testCopyPasteStyleDerivesequence_withcontactMatrixAnn()
1064 SequenceI seq1=new Sequence("seq1","ACDACDACD");
1065 seq1.createDatasetSequence();
1066 ContactMatrixI cm = new SeqDistanceContactMatrix(seq1.getLength());
1067 // addContactList needs to return annotation addable to the sequence reference it was called from
1068 AlignmentAnnotation aann = seq1.addContactList(cm);
1069 assertTrue(aann.sequenceRef==seq1);
1070 assertEquals(1,seq1.getAnnotation().length);
1071 assertNotNull(seq1.getContactListFor(seq1.getAnnotation()[0], 1));
1073 SequenceI seq_derived=seq1.deriveSequence();
1074 assertEquals(1,seq_derived.getAnnotation().length);
1075 assertTrue(cm==seq_derived.getContactMatrixFor(seq_derived.getAnnotation()[0]));
1076 assertNotNull(seq_derived.getContactListFor(seq_derived.getAnnotation()[0], 1));
1078 // copy paste actually uses the copy constructor .. so
1080 SequenceI seq_copied=new Sequence((Sequence)seq_derived);
1081 assertEquals(1,seq_copied.getAnnotation().length);
1082 assertTrue(cm==seq_copied.getContactMatrixFor(seq_copied.getAnnotation()[0]));
1083 assertNotNull(seq_copied.getContactListFor(seq_copied.getAnnotation()[0], 1));
1088 @Test(groups = { "Functional" })
1089 public void testCopyConstructor_noDataset()
1091 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1092 seq1.setDescription("description");
1093 seq1.addAlignmentAnnotation(
1094 new AlignmentAnnotation("label", "desc", 1.3d));
1095 seq1.addSequenceFeature(
1096 new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1097 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1098 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1100 SequenceI copy = new Sequence(seq1);
1102 assertNull(copy.getDatasetSequence());
1104 verifyCopiedSequence(seq1, copy);
1106 // copy has a copy of the DBRefEntry
1107 // this is murky - DBrefs are only copied for dataset sequences
1108 // where the test for 'dataset sequence' is 'dataset is null'
1109 // but that doesn't distinguish it from an aligned sequence
1110 // which has not yet generated a dataset sequence
1111 // NB getDBRef looks inside dataset sequence if not null
1112 List<DBRefEntry> dbrefs = copy.getDBRefs();
1113 assertEquals(1, dbrefs.size());
1114 assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
1115 assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
1118 @Test(groups = { "Functional" })
1119 public void testCopyConstructor_withDataset()
1121 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1122 seq1.createDatasetSequence();
1123 seq1.setDescription("description");
1124 seq1.addAlignmentAnnotation(
1125 new AlignmentAnnotation("label", "desc", 1.3d));
1126 // JAL-2046 - what is the contract for using a derived sequence's
1127 // addSequenceFeature ?
1128 seq1.addSequenceFeature(
1129 new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1130 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1131 // here we add DBRef to the dataset sequence:
1132 seq1.getDatasetSequence()
1133 .addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1135 SequenceI copy = new Sequence(seq1);
1137 assertNotNull(copy.getDatasetSequence());
1138 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1140 verifyCopiedSequence(seq1, copy);
1142 // getDBRef looks inside dataset sequence and this is shared,
1143 // so holds the same dbref objects
1144 List<DBRefEntry> dbrefs = copy.getDBRefs();
1145 assertEquals(1, dbrefs.size());
1146 assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
1150 * Helper to make assertions about a copied sequence
1155 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1157 // verify basic properties:
1158 assertEquals(copy.getName(), seq1.getName());
1159 assertEquals(copy.getDescription(), seq1.getDescription());
1160 assertEquals(copy.getStart(), seq1.getStart());
1161 assertEquals(copy.getEnd(), seq1.getEnd());
1162 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1164 // copy has a copy of the annotation:
1165 AlignmentAnnotation[] anns = copy.getAnnotation();
1166 assertEquals(1, anns.length);
1167 assertFalse(anns[0] == seq1.getAnnotation()[0]);
1168 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1169 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1170 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1172 // copy has a copy of the sequence feature:
1173 List<SequenceFeature> sfs = copy.getSequenceFeatures();
1174 assertEquals(1, sfs.size());
1175 if (seq1.getDatasetSequence() != null
1176 && copy.getDatasetSequence() == seq1.getDatasetSequence())
1178 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1182 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1184 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1186 // copy has a copy of the PDB entry
1187 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1188 assertEquals(1, pdbs.size());
1189 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1190 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1193 @Test(groups = "Functional")
1194 public void testGetCharAt()
1196 SequenceI sq = new Sequence("", "abcde");
1197 assertEquals('a', sq.getCharAt(0));
1198 assertEquals('e', sq.getCharAt(4));
1199 assertEquals(' ', sq.getCharAt(5));
1200 assertEquals(' ', sq.getCharAt(-1));
1203 @Test(groups = { "Functional" })
1204 public void testAddSequenceFeatures()
1206 SequenceI sq = new Sequence("", "abcde");
1207 // type may not be null
1208 assertFalse(sq.addSequenceFeature(
1209 new SequenceFeature(null, "desc", 4, 8, 0f, null)));
1210 assertTrue(sq.addSequenceFeature(
1211 new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1212 // can't add a duplicate feature
1213 assertFalse(sq.addSequenceFeature(
1214 new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1215 // can add a different feature
1216 assertTrue(sq.addSequenceFeature(
1217 new SequenceFeature("Scop", "desc", 4, 8, 0f, null))); // different
1219 assertTrue(sq.addSequenceFeature(
1220 new SequenceFeature("Cath", "description", 4, 8, 0f, null)));// different
1222 assertTrue(sq.addSequenceFeature(
1223 new SequenceFeature("Cath", "desc", 3, 8, 0f, null))); // different
1226 assertTrue(sq.addSequenceFeature(
1227 new SequenceFeature("Cath", "desc", 4, 9, 0f, null))); // different
1230 assertTrue(sq.addSequenceFeature(
1231 new SequenceFeature("Cath", "desc", 4, 8, 1f, null))); // different
1233 assertTrue(sq.addSequenceFeature(
1234 new SequenceFeature("Cath", "desc", 4, 8, Float.NaN, null))); // score
1236 assertTrue(sq.addSequenceFeature(
1237 new SequenceFeature("Cath", "desc", 4, 8, 0f, "Metal"))); // different
1239 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1243 * Tests for adding (or updating) dbrefs
1245 * @see DBRefEntry#updateFrom(DBRefEntry)
1247 @Test(groups = { "Functional" })
1248 public void testAddDBRef()
1250 SequenceI sq = new Sequence("", "abcde");
1251 assertNull(sq.getDBRefs());
1252 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1254 assertEquals(1, sq.getDBRefs().size());
1255 assertSame(dbref, sq.getDBRefs().get(0));
1258 * change of version - new entry
1260 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1261 sq.addDBRef(dbref2);
1262 assertEquals(2, sq.getDBRefs().size());
1263 assertSame(dbref, sq.getDBRefs().get(0));
1264 assertSame(dbref2, sq.getDBRefs().get(1));
1267 * matches existing entry - not added
1269 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1270 assertEquals(2, sq.getDBRefs().size());
1273 * different source = new entry
1275 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1276 sq.addDBRef(dbref3);
1277 assertEquals(3, sq.getDBRefs().size());
1278 assertSame(dbref3, sq.getDBRefs().get(2));
1281 * different ref = new entry
1283 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1284 sq.addDBRef(dbref4);
1285 assertEquals(4, sq.getDBRefs().size());
1286 assertSame(dbref4, sq.getDBRefs().get(3));
1289 * matching ref with a mapping - map updated
1291 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1292 Mapping map = new Mapping(
1293 new MapList(new int[]
1294 { 1, 3 }, new int[] { 1, 1 }, 3, 1));
1296 sq.addDBRef(dbref5);
1297 assertEquals(4, sq.getDBRefs().size());
1298 assertSame(dbref4, sq.getDBRefs().get(3));
1299 assertSame(map, dbref4.getMap());
1302 * 'real' version replaces "0" version
1304 dbref2.setVersion("0");
1305 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1306 dbref2.getAccessionId());
1307 sq.addDBRef(dbref6);
1308 assertEquals(4, sq.getDBRefs().size());
1309 assertSame(dbref2, sq.getDBRefs().get(1));
1310 assertEquals("3", dbref2.getVersion());
1313 * 'real' version replaces "source:0" version
1315 dbref3.setVersion("Uniprot:0");
1316 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1317 dbref3.getAccessionId());
1318 sq.addDBRef(dbref7);
1319 assertEquals(4, sq.getDBRefs().size());
1320 assertSame(dbref3, sq.getDBRefs().get(2));
1321 assertEquals("3", dbref2.getVersion());
1324 @Test(groups = { "Functional" })
1325 public void testGetPrimaryDBRefs_peptide()
1327 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1330 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1331 assertTrue(primaryDBRefs.isEmpty());
1335 primaryDBRefs = sq.getPrimaryDBRefs();
1336 assertTrue(primaryDBRefs.isEmpty());
1338 // primary - uniprot
1339 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1340 sq.addDBRef(upentry1);
1342 // primary - uniprot with congruent map
1343 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1345 new Mapping(null, new MapList(new int[]
1346 { 10, 22 }, new int[] { 10, 22 }, 1, 1)));
1347 sq.addDBRef(upentry2);
1349 // primary - uniprot with map of enclosing sequence
1350 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1352 new Mapping(null, new MapList(new int[]
1353 { 8, 24 }, new int[] { 8, 24 }, 1, 1)));
1354 sq.addDBRef(upentry3);
1356 // not primary - uniprot with map of sub-sequence (5')
1357 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1359 new Mapping(null, new MapList(new int[]
1360 { 10, 18 }, new int[] { 10, 18 }, 1, 1)));
1361 sq.addDBRef(upentry4);
1363 // not primary - uniprot with map that overlaps 3'
1364 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1366 new Mapping(null, new MapList(new int[]
1367 { 12, 22 }, new int[] { 12, 22 }, 1, 1)));
1368 sq.addDBRef(upentry5);
1370 // not primary - uniprot with map to different coordinates frame
1371 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1373 new Mapping(null, new MapList(new int[]
1374 { 12, 18 }, new int[] { 112, 118 }, 1, 1)));
1375 sq.addDBRef(upentry6);
1377 // not primary - dbref to 'non-core' database
1378 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1379 sq.addDBRef(upentry7);
1381 // primary - type is PDB
1382 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1383 sq.addDBRef(pdbentry);
1385 // not primary - PDBEntry has no file
1386 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1388 // not primary - no PDBEntry
1389 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1391 // add corroborating PDB entry for primary DBref -
1392 // needs to have a file as well as matching ID
1393 // note PDB ID is not treated as case sensitive
1394 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB,
1395 new File("/blah").toString()));
1397 // not valid DBRef - no file..
1398 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1400 primaryDBRefs = sq.getPrimaryDBRefs();
1401 assertEquals(4, primaryDBRefs.size());
1402 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1403 primaryDBRefs.contains(upentry1));
1404 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1405 primaryDBRefs.contains(upentry2));
1406 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1407 primaryDBRefs.contains(upentry3));
1408 assertTrue("Couldn't find expected PDB primary reference",
1409 primaryDBRefs.contains(pdbentry));
1412 @Test(groups = { "Functional" })
1413 public void testGetPrimaryDBRefs_nucleotide()
1415 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10,
1418 // primary - Ensembl
1419 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1422 // not primary - Ensembl 'transcript' mapping of sub-sequence
1423 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1425 new Mapping(null, new MapList(new int[]
1426 { 15, 25 }, new int[] { 1, 11 }, 1, 1)));
1429 // primary - EMBL with congruent map
1430 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1432 new Mapping(null, new MapList(new int[]
1433 { 10, 34 }, new int[] { 10, 34 }, 1, 1)));
1436 // not primary - to non-core database
1437 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1440 // not primary - to protein
1441 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1444 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1445 assertEquals(2, primaryDBRefs.size());
1446 assertTrue(primaryDBRefs.contains(dbr1));
1447 assertTrue(primaryDBRefs.contains(dbr3));
1451 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1454 @Test(groups = { "Functional" })
1455 public void testUpdatePDBIds()
1457 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1458 seq.addPDBId(pdbe1);
1459 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1460 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1461 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1462 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1463 // 7 is not a valid chain code:
1464 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1467 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1468 assertEquals(4, pdbIds.size());
1469 assertSame(pdbe1, pdbIds.get(0));
1470 // chain code got added to 3A6S:
1471 assertEquals("B", pdbe1.getChainCode());
1472 assertEquals("1A70", pdbIds.get(1).getId());
1473 // 4BQGA is parsed into id + chain
1474 assertEquals("4BQG", pdbIds.get(2).getId());
1475 assertEquals("a", pdbIds.get(2).getChainCode());
1476 assertEquals("2GIS7", pdbIds.get(3).getId());
1477 assertNull(pdbIds.get(3).getChainCode());
1481 * Test the method that either adds a pdbid or updates an existing one
1483 @Test(groups = { "Functional" })
1484 public void testAddPDBId()
1486 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1488 assertEquals(1, seq.getAllPDBEntries().size());
1489 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1490 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1492 // add the same entry
1494 assertEquals(1, seq.getAllPDBEntries().size());
1495 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1497 // add an identical entry
1498 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1499 assertEquals(1, seq.getAllPDBEntries().size());
1500 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1502 // add a different entry
1503 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1504 seq.addPDBId(pdbe2);
1505 assertEquals(2, seq.getAllPDBEntries().size());
1506 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1507 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1509 // update pdbe with chain code, file, type
1510 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1511 seq.addPDBId(pdbe3);
1512 assertEquals(2, seq.getAllPDBEntries().size());
1513 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1514 assertEquals("3A6S", pdbe.getId()); // unchanged
1515 assertEquals("A", pdbe.getChainCode()); // updated
1516 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1517 assertEquals("filepath", pdbe.getFile()); // updated
1518 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1520 // add with a different file path
1521 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1522 seq.addPDBId(pdbe4);
1523 assertEquals(3, seq.getAllPDBEntries().size());
1524 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1526 // add with a different chain code
1527 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1528 seq.addPDBId(pdbe5);
1529 assertEquals(4, seq.getAllPDBEntries().size());
1530 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1532 // add with a fake pdbid
1533 // (models don't have an embedded ID)
1534 String realId = "RealIDQ";
1535 PDBEntry pdbe6 = new PDBEntry(realId, null, Type.PDB, "real/localpath");
1536 PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath", "C", Type.MMCIF,
1538 pdbe7.setFakedPDBId(true);
1539 seq.addPDBId(pdbe6);
1540 assertEquals(5, seq.getAllPDBEntries().size());
1541 seq.addPDBId(pdbe7);
1542 assertEquals(5, seq.getAllPDBEntries().size());
1543 assertFalse(pdbe6.fakedPDBId());
1544 assertSame(pdbe6, seq.getAllPDBEntries().get(4));
1545 assertEquals("C", pdbe6.getChainCode());
1546 assertEquals(realId, pdbe6.getId());
1552 expectedExceptions =
1553 { IllegalArgumentException.class })
1554 public void testSetDatasetSequence_toSelf()
1556 seq.setDatasetSequence(seq);
1562 expectedExceptions =
1563 { IllegalArgumentException.class })
1564 public void testSetDatasetSequence_cascading()
1566 SequenceI seq2 = new Sequence("Seq2", "xyz");
1567 seq2.createDatasetSequence();
1568 seq.setDatasetSequence(seq2);
1571 @Test(groups = { "Functional" })
1572 public void testFindFeatures()
1574 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1575 sq.createDatasetSequence();
1577 assertTrue(sq.findFeatures(1, 99).isEmpty());
1579 // add non-positional feature
1580 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1582 sq.addSequenceFeature(sf0);
1583 // add feature on BCD
1584 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1586 sq.addSequenceFeature(sfBCD);
1587 // add feature on DE
1588 SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1590 sq.addSequenceFeature(sfDE);
1591 // add contact feature at [B, H]
1592 SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1593 "desc", 9, 15, 2f, null);
1594 sq.addSequenceFeature(sfContactBH);
1595 // add contact feature at [F, G]
1596 SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1597 "desc", 13, 14, 2f, null);
1598 sq.addSequenceFeature(sfContactFG);
1599 // add single position feature at [I]
1600 SequenceFeature sfI = new SequenceFeature("Disulfide Bond", "desc", 16,
1602 sq.addSequenceFeature(sfI);
1604 // no features in columns 1-2 (-A)
1605 List<SequenceFeature> found = sq.findFeatures(1, 2);
1606 assertTrue(found.isEmpty());
1608 // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1609 found = sq.findFeatures(1, 6);
1610 assertEquals(2, found.size());
1611 assertTrue(found.contains(sfBCD));
1612 assertTrue(found.contains(sfContactBH));
1614 // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1615 found = sq.findFeatures(5, 6);
1616 assertEquals(1, found.size());
1617 assertTrue(found.contains(sfBCD));
1619 // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1620 found = sq.findFeatures(7, 10);
1621 assertEquals(3, found.size());
1622 assertTrue(found.contains(sfBCD));
1623 assertTrue(found.contains(sfDE));
1624 assertTrue(found.contains(sfContactFG));
1626 // columns 10-11 (--) should find nothing
1627 found = sq.findFeatures(10, 11);
1628 assertEquals(0, found.size());
1630 // columns 14-14 (I) should find variant feature
1631 found = sq.findFeatures(14, 14);
1632 assertEquals(1, found.size());
1633 assertTrue(found.contains(sfI));
1636 @Test(groups = { "Functional" })
1637 public void testFindIndex_withCursor()
1639 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1640 final int tok = (int) PA.getValue(sq, "changeCount");
1641 assertEquals(1, tok);
1643 // find F given A, check cursor is now at the found position
1644 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
1645 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1646 assertEquals(13, cursor.residuePosition);
1647 assertEquals(10, cursor.columnPosition);
1650 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
1651 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1652 assertEquals(8, cursor.residuePosition);
1653 assertEquals(2, cursor.columnPosition);
1655 // find C given C (no cursor update is done for this case)
1656 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
1657 SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1658 assertSame(cursor2, cursor);
1661 * sequence 'end' beyond end of sequence returns length of sequence
1662 * (for compatibility with pre-cursor code)
1663 * - also verify the cursor is left in a valid state
1665 sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
1666 assertEquals(7, sq.findIndex(10)); // establishes a cursor
1667 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1668 assertEquals(10, cursor.residuePosition);
1669 assertEquals(7, cursor.columnPosition);
1670 assertEquals(sq.getLength(), sq.findIndex(65));
1671 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1672 assertSame(cursor, cursor2); // not updated for this case!
1674 sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
1675 sq.findIndex(10); // establishes a cursor
1676 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1677 assertEquals(sq.getLength(), sq.findIndex(65));
1678 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1679 assertSame(cursor, cursor2); // not updated for this case!
1682 * residue after sequence 'start' but before first residue should return
1683 * zero (for compatibility with pre-cursor code)
1685 sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
1686 sq.findIndex(10); // establishes a cursor
1687 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1688 assertEquals(0, sq.findIndex(3));
1689 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1690 assertSame(cursor, cursor2); // not updated for this case!
1692 sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
1693 sq.findIndex(10); // establishes a cursor
1694 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1695 assertEquals(0, sq.findIndex(2));
1696 cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1697 assertSame(cursor, cursor2); // not updated for this case!
1700 @Test(groups = { "Functional" })
1701 public void testFindPosition_withCursor()
1703 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1704 final int tok = (int) PA.getValue(sq, "changeCount");
1705 assertEquals(1, tok);
1707 // find F pos given A - lastCol gets set in cursor
1709 sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
1710 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1711 PA.getValue(sq, "cursor").toString());
1713 // find A pos given F - first residue column is saved in cursor
1715 sq.findPosition(2, new SequenceCursor(sq, 13, 10, tok)));
1716 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
1717 PA.getValue(sq, "cursor").toString());
1719 // find C pos given C (neither startCol nor endCol is set)
1721 sq.findPosition(6, new SequenceCursor(sq, 10, 6, tok)));
1722 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1723 PA.getValue(sq, "cursor").toString());
1725 // now the grey area - what residue position for a gapped column? JAL-2562
1727 // find 'residue' for column 3 given cursor for D (so working left)
1728 // returns B9; cursor is updated to [B 5]
1729 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, tok)));
1730 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
1731 PA.getValue(sq, "cursor").toString());
1733 // find 'residue' for column 8 given cursor for D (so working right)
1734 // returns E12; cursor is updated to [D 7]
1736 sq.findPosition(8, new SequenceCursor(sq, 11, 7, tok)));
1737 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
1738 PA.getValue(sq, "cursor").toString());
1740 // find 'residue' for column 12 given cursor for B
1741 // returns 1 more than last residue position; cursor is updated to [F 10]
1742 // lastCol position is saved in cursor
1744 sq.findPosition(12, new SequenceCursor(sq, 9, 5, tok)));
1745 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1746 PA.getValue(sq, "cursor").toString());
1749 * findPosition for column beyond length of sequence
1750 * returns 1 more than the last residue position
1751 * cursor is set to last real residue position [F 10]
1754 sq.findPosition(99, new SequenceCursor(sq, 8, 2, tok)));
1755 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1756 PA.getValue(sq, "cursor").toString());
1759 * and the case without a trailing gap
1761 sq = new Sequence("test/8-13", "-A--BCD-EF");
1762 // first find C from A
1763 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, tok)));
1764 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1765 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1767 // now 'find' 99 from C
1768 // cursor is set to [F 10] and saved lastCol
1769 assertEquals(14, sq.findPosition(99, cursor));
1770 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1771 PA.getValue(sq, "cursor").toString());
1775 public void testIsValidCursor()
1777 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1778 assertFalse(sq.isValidCursor(null));
1781 * cursor is valid if it has valid sequence ref and changeCount token
1782 * and positions within the range of the sequence
1784 int changeCount = (int) PA.getValue(sq, "changeCount");
1785 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1786 assertTrue(sq.isValidCursor(cursor));
1789 * column position outside [0 - length] is rejected
1791 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1792 assertFalse(sq.isValidCursor(cursor));
1793 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1794 assertFalse(sq.isValidCursor(cursor));
1795 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1796 assertFalse(sq.isValidCursor(cursor));
1797 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1798 assertFalse(sq.isValidCursor(cursor));
1801 * wrong sequence is rejected
1803 cursor = new SequenceCursor(null, 13, 1, changeCount);
1804 assertFalse(sq.isValidCursor(cursor));
1805 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1807 assertFalse(sq.isValidCursor(cursor));
1810 * wrong token value is rejected
1812 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1813 assertFalse(sq.isValidCursor(cursor));
1814 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1815 assertFalse(sq.isValidCursor(cursor));
1818 @Test(groups = { "Functional" })
1819 public void testFindPosition_withCursorAndEdits()
1821 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1823 // find F pos given A
1824 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1825 int token = (int) PA.getValue(sq, "changeCount"); // 0
1826 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1827 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1830 * setSequence should invalidate the cursor cached by the sequence
1832 sq.setSequence("-A-BCD-EF---"); // one gap removed
1833 assertEquals(8, sq.getStart()); // sanity check
1834 assertEquals(11, sq.findPosition(5)); // D11
1835 // cursor should now be at [D 6]
1836 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1837 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1838 assertEquals(0, cursor.lastColumnPosition); // not yet found
1839 assertEquals(13, sq.findPosition(8)); // E13
1840 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1841 assertEquals(9, cursor.lastColumnPosition); // found
1844 * deleteChars should invalidate the cached cursor
1846 sq.deleteChars(2, 5); // delete -BC
1847 assertEquals("-AD-EF---", sq.getSequenceAsString());
1848 assertEquals(8, sq.getStart()); // sanity check
1849 assertEquals(10, sq.findPosition(4)); // E10
1850 // cursor should now be at [E 5]
1851 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1852 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1855 * Edit to insert gaps should invalidate the cached cursor
1856 * insert 2 gaps at column[3] to make -AD---EF---
1858 SequenceI[] seqs = new SequenceI[] { sq };
1859 AlignmentI al = new Alignment(seqs);
1860 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1861 assertEquals("-AD---EF---", sq.getSequenceAsString());
1862 assertEquals(10, sq.findPosition(4)); // E10
1863 // cursor should now be at [D 3]
1864 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1865 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1868 * insertCharAt should invalidate the cached cursor
1869 * insert CC at column[4] to make -AD-CC--EF---
1871 sq.insertCharAt(4, 2, 'C');
1872 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1873 assertEquals(13, sq.findPosition(9)); // F13
1874 // cursor should now be at [F 10]
1875 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1876 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1879 * changing sequence start should invalidate cursor
1881 sq = new Sequence("test/8-13", "-A--BCD-EF--");
1882 assertEquals(8, sq.getStart());
1883 assertEquals(9, sq.findPosition(4)); // B(9)
1885 assertEquals(8, sq.findPosition(4)); // is now B(8)
1887 assertEquals(11, sq.findPosition(4)); // is now B(11)
1890 @Test(groups = { "Functional" })
1891 public void testGetSequence()
1893 String seqstring = "-A--BCD-EF--";
1894 Sequence sq = new Sequence("test/8-13", seqstring);
1895 sq.createDatasetSequence();
1896 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1897 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1898 "ABCDEF".toCharArray()));
1900 // verify a copy of the sequence array is returned
1901 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1902 assertNotSame(theSeq, sq.getSequence());
1903 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1904 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1907 @Test(groups = { "Functional" })
1908 public void testReplace()
1910 String seqstring = "-A--BCD-EF--";
1911 SequenceI sq = new Sequence("test/8-13", seqstring);
1912 // changeCount is incremented for setStart
1913 assertEquals(1, PA.getValue(sq, "changeCount"));
1915 assertEquals(0, sq.replace('A', 'A')); // same char
1916 assertEquals(seqstring, sq.getSequenceAsString());
1917 assertEquals(1, PA.getValue(sq, "changeCount"));
1919 assertEquals(0, sq.replace('X', 'Y')); // not there
1920 assertEquals(seqstring, sq.getSequenceAsString());
1921 assertEquals(1, PA.getValue(sq, "changeCount"));
1923 assertEquals(1, sq.replace('A', 'K'));
1924 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1925 assertEquals(2, PA.getValue(sq, "changeCount"));
1927 assertEquals(6, sq.replace('-', '.'));
1928 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1929 assertEquals(3, PA.getValue(sq, "changeCount"));
1932 @Test(groups = { "Functional" })
1933 public void testGapBitset()
1935 SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1936 BitSet bs = sq.gapBitset();
1937 BitSet expected = new BitSet();
1941 expected.set(11, 13);
1943 assertTrue(bs.equals(expected));
1947 public void testFindFeatures_largeEndPos()
1950 * imitate a PDB sequence where end is larger than end position
1952 SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
1953 sq.createDatasetSequence();
1955 assertTrue(sq.findFeatures(1, 9).isEmpty());
1956 // should be no array bounds exception - JAL-2772
1957 assertTrue(sq.findFeatures(1, 15).isEmpty());
1959 // add feature on BCD
1960 SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
1962 sq.addSequenceFeature(sfBCD);
1964 // no features in columns 1-2 (-A)
1965 List<SequenceFeature> found = sq.findFeatures(1, 2);
1966 assertTrue(found.isEmpty());
1968 // columns 1-6 (-ABC--) includes BCD
1969 found = sq.findFeatures(1, 6);
1970 assertEquals(1, found.size());
1971 assertTrue(found.contains(sfBCD));
1973 // columns 10-11 (--) should find nothing
1974 found = sq.findFeatures(10, 11);
1975 assertEquals(0, found.size());
1978 @Test(groups = { "Functional" })
1979 public void testSetName()
1981 SequenceI sq = new Sequence("test", "-ABC---DE-F--");
1982 assertEquals("test", sq.getName());
1983 assertEquals(1, sq.getStart());
1984 assertEquals(6, sq.getEnd());
1986 sq.setName("testing");
1987 assertEquals("testing", sq.getName());
1989 sq.setName("test/8-10");
1990 assertEquals("test", sq.getName());
1991 assertEquals(8, sq.getStart());
1992 assertEquals(13, sq.getEnd()); // note end is recomputed
1994 sq.setName("testing/7-99");
1995 assertEquals("testing", sq.getName());
1996 assertEquals(7, sq.getStart());
1997 assertEquals(99, sq.getEnd()); // end may be beyond physical end
2000 assertEquals("", sq.getName());
2001 assertEquals(2, sq.getStart());
2002 assertEquals(7, sq.getEnd());
2004 sq.setName("test/"); // invalid
2005 assertEquals("test/", sq.getName());
2006 assertEquals(2, sq.getStart());
2007 assertEquals(7, sq.getEnd());
2009 sq.setName("test/6-13/7-99");
2010 assertEquals("test/6-13", sq.getName());
2011 assertEquals(7, sq.getStart());
2012 assertEquals(99, sq.getEnd());
2014 sq.setName("test/0-5"); // 0 is invalid - ignored
2015 assertEquals("test/0-5", sq.getName());
2016 assertEquals(7, sq.getStart());
2017 assertEquals(99, sq.getEnd());
2019 sq.setName("test/a-5"); // a is invalid - ignored
2020 assertEquals("test/a-5", sq.getName());
2021 assertEquals(7, sq.getStart());
2022 assertEquals(99, sq.getEnd());
2024 sq.setName("test/6-5"); // start > end is invalid - ignored
2025 assertEquals("test/6-5", sq.getName());
2026 assertEquals(7, sq.getStart());
2027 assertEquals(99, sq.getEnd());
2029 sq.setName("test/5"); // invalid - ignored
2030 assertEquals("test/5", sq.getName());
2031 assertEquals(7, sq.getStart());
2032 assertEquals(99, sq.getEnd());
2034 sq.setName("test/-5"); // invalid - ignored
2035 assertEquals("test/-5", sq.getName());
2036 assertEquals(7, sq.getStart());
2037 assertEquals(99, sq.getEnd());
2039 sq.setName("test/5-"); // invalid - ignored
2040 assertEquals("test/5-", sq.getName());
2041 assertEquals(7, sq.getStart());
2042 assertEquals(99, sq.getEnd());
2044 sq.setName("test/5-6-7"); // invalid - ignored
2045 assertEquals("test/5-6-7", sq.getName());
2046 assertEquals(7, sq.getStart());
2047 assertEquals(99, sq.getEnd());
2049 sq.setName(null); // invalid, gets converted to space
2050 assertEquals("", sq.getName());
2051 assertEquals(7, sq.getStart());
2052 assertEquals(99, sq.getEnd());
2055 @Test(groups = { "Functional" })
2056 public void testCheckValidRange()
2058 Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
2059 assertEquals(7, sq.getStart());
2060 assertEquals(12, sq.getEnd());
2063 * checkValidRange ensures end is at least the last residue position
2065 PA.setValue(sq, "end", 2);
2066 sq.checkValidRange();
2067 assertEquals(12, sq.getEnd());
2070 * end may be beyond the last residue position
2072 PA.setValue(sq, "end", 22);
2073 sq.checkValidRange();
2074 assertEquals(22, sq.getEnd());
2077 @Test(groups = { "Functional" })
2078 public void testDeleteChars_withGaps()
2083 SequenceI sq = new Sequence("test/8-10", "A-B-C");
2084 sq.createDatasetSequence();
2085 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2086 sq.deleteChars(1, 2); // delete first gap
2087 assertEquals("AB-C", sq.getSequenceAsString());
2088 assertEquals(8, sq.getStart());
2089 assertEquals(10, sq.getEnd());
2090 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2093 * delete gaps and residues at start (no new dataset sequence)
2095 sq = new Sequence("test/8-10", "A-B-C");
2096 sq.createDatasetSequence();
2097 sq.deleteChars(0, 3); // delete A-B
2098 assertEquals("-C", sq.getSequenceAsString());
2099 assertEquals(10, sq.getStart());
2100 assertEquals(10, sq.getEnd());
2101 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2104 * delete gaps and residues at end (no new dataset sequence)
2106 sq = new Sequence("test/8-10", "A-B-C");
2107 sq.createDatasetSequence();
2108 sq.deleteChars(2, 5); // delete B-C
2109 assertEquals("A-", sq.getSequenceAsString());
2110 assertEquals(8, sq.getStart());
2111 assertEquals(8, sq.getEnd());
2112 assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2115 * delete gaps and residues internally (new dataset sequence)
2116 * first delete from gap to residue
2118 sq = new Sequence("test/8-10", "A-B-C");
2119 sq.createDatasetSequence();
2120 sq.deleteChars(1, 3); // delete -B
2121 assertEquals("A-C", sq.getSequenceAsString());
2122 assertEquals(8, sq.getStart());
2123 assertEquals(9, sq.getEnd());
2124 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2125 assertEquals(8, sq.getDatasetSequence().getStart());
2126 assertEquals(9, sq.getDatasetSequence().getEnd());
2129 * internal delete from gap to gap
2131 sq = new Sequence("test/8-10", "A-B-C");
2132 sq.createDatasetSequence();
2133 sq.deleteChars(1, 4); // delete -B-
2134 assertEquals("AC", sq.getSequenceAsString());
2135 assertEquals(8, sq.getStart());
2136 assertEquals(9, sq.getEnd());
2137 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2138 assertEquals(8, sq.getDatasetSequence().getStart());
2139 assertEquals(9, sq.getDatasetSequence().getEnd());
2142 * internal delete from residue to residue
2144 sq = new Sequence("test/8-10", "A-B-C");
2145 sq.createDatasetSequence();
2146 sq.deleteChars(2, 3); // delete B
2147 assertEquals("A--C", sq.getSequenceAsString());
2148 assertEquals(8, sq.getStart());
2149 assertEquals(9, sq.getEnd());
2150 assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2151 assertEquals(8, sq.getDatasetSequence().getStart());
2152 assertEquals(9, sq.getDatasetSequence().getEnd());
2156 * Test the code used to locate the reference sequence ruler origin
2158 @Test(groups = { "Functional" })
2159 public void testLocateVisibleStartofSequence()
2161 // create random alignment
2162 AlignmentGenerator gen = new AlignmentGenerator(false);
2163 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
2165 HiddenColumns cs = al.getHiddenColumns();
2166 ColumnSelection colsel = new ColumnSelection();
2168 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
2169 assertEquals(2, seq.findIndex(seq.getStart()));
2171 // no hidden columns
2172 assertEquals(seq.findIndex(seq.getStart()) - 1,
2173 seq.firstResidueOutsideIterator(cs.iterator()));
2175 // hidden column on gap after end of sequence - should not affect bounds
2176 colsel.hideSelectedColumns(13, al.getHiddenColumns());
2177 assertEquals(seq.findIndex(seq.getStart()) - 1,
2178 seq.firstResidueOutsideIterator(cs.iterator()));
2180 cs.revealAllHiddenColumns(colsel);
2181 // hidden column on gap before beginning of sequence - should vis bounds by
2183 colsel.hideSelectedColumns(0, al.getHiddenColumns());
2184 assertEquals(seq.findIndex(seq.getStart()) - 2,
2185 cs.absoluteToVisibleColumn(
2186 seq.firstResidueOutsideIterator(cs.iterator())));
2188 cs.revealAllHiddenColumns(colsel);
2189 // hide columns around most of sequence - leave one residue remaining
2190 cs.hideColumns(1, 3);
2191 cs.hideColumns(6, 11);
2193 Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
2195 assertEquals("-D", seq.getSequenceStringFromIterator(it));
2196 // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
2199 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2200 cs.revealAllHiddenColumns(colsel);
2202 // hide whole sequence - should just get location of hidden region
2203 // containing sequence
2204 cs.hideColumns(1, 11);
2205 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2207 cs.revealAllHiddenColumns(colsel);
2208 cs.hideColumns(0, 15);
2209 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2211 SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
2213 cs.revealAllHiddenColumns(colsel);
2214 cs.hideColumns(7, 17);
2215 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2217 cs.revealAllHiddenColumns(colsel);
2218 cs.hideColumns(3, 17);
2219 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2221 cs.revealAllHiddenColumns(colsel);
2222 cs.hideColumns(3, 19);
2223 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2225 cs.revealAllHiddenColumns(colsel);
2226 cs.hideColumns(0, 0);
2227 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2229 cs.revealAllHiddenColumns(colsel);
2230 cs.hideColumns(0, 1);
2231 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2233 cs.revealAllHiddenColumns(colsel);
2234 cs.hideColumns(0, 2);
2235 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2237 cs.revealAllHiddenColumns(colsel);
2238 cs.hideColumns(1, 1);
2239 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2241 cs.revealAllHiddenColumns(colsel);
2242 cs.hideColumns(1, 2);
2243 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2245 cs.revealAllHiddenColumns(colsel);
2246 cs.hideColumns(1, 3);
2247 assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2249 cs.revealAllHiddenColumns(colsel);
2250 cs.hideColumns(0, 2);
2251 cs.hideColumns(5, 6);
2252 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2254 cs.revealAllHiddenColumns(colsel);
2255 cs.hideColumns(0, 2);
2256 cs.hideColumns(5, 6);
2257 cs.hideColumns(9, 10);
2258 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2260 cs.revealAllHiddenColumns(colsel);
2261 cs.hideColumns(0, 2);
2262 cs.hideColumns(7, 11);
2263 assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2265 cs.revealAllHiddenColumns(colsel);
2266 cs.hideColumns(2, 4);
2267 cs.hideColumns(7, 11);
2268 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2270 cs.revealAllHiddenColumns(colsel);
2271 cs.hideColumns(2, 4);
2272 cs.hideColumns(7, 12);
2273 assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2275 cs.revealAllHiddenColumns(colsel);
2276 cs.hideColumns(1, 11);
2277 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2279 cs.revealAllHiddenColumns(colsel);
2280 cs.hideColumns(0, 12);
2281 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2283 cs.revealAllHiddenColumns(colsel);
2284 cs.hideColumns(0, 4);
2285 cs.hideColumns(6, 12);
2286 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2288 cs.revealAllHiddenColumns(colsel);
2289 cs.hideColumns(0, 1);
2290 cs.hideColumns(3, 12);
2291 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2293 cs.revealAllHiddenColumns(colsel);
2294 cs.hideColumns(3, 14);
2295 cs.hideColumns(17, 19);
2296 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2298 cs.revealAllHiddenColumns(colsel);
2299 cs.hideColumns(3, 7);
2300 cs.hideColumns(9, 14);
2301 cs.hideColumns(17, 19);
2302 assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2304 cs.revealAllHiddenColumns(colsel);
2305 cs.hideColumns(0, 1);
2306 cs.hideColumns(3, 4);
2307 cs.hideColumns(6, 8);
2308 cs.hideColumns(10, 12);
2309 assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2313 @Test(groups = { "Functional" })
2314 public void testTransferAnnotation()
2316 Sequence origSeq = new Sequence("MYSEQ", "THISISASEQ");
2317 Sequence toSeq = new Sequence("MYSEQ", "THISISASEQ");
2318 origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
2319 toSeq.transferAnnotation(origSeq, null);
2320 assertTrue(toSeq.getDBRefs().size() == 1);
2322 assertTrue(toSeq.getDBRefs().get(0).isCanonical());
2324 // check for promotion of non-canonical
2325 // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
2326 toSeq.setDBRefs(null);
2327 toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
2328 toSeq.transferAnnotation(origSeq, null);
2329 assertTrue(toSeq.getDBRefs().size() == 1);
2331 assertTrue("Promotion of non-canonical DBRefEntry failed",
2332 toSeq.getDBRefs().get(0).isCanonical());