2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNotSame;
27 import static org.testng.AssertJUnit.assertNull;
28 import static org.testng.AssertJUnit.assertSame;
29 import static org.testng.AssertJUnit.assertTrue;
31 import jalview.commands.EditCommand;
32 import jalview.commands.EditCommand.Action;
33 import jalview.datamodel.PDBEntry.Type;
34 import jalview.gui.JvOptionPane;
35 import jalview.util.MapList;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.BitSet;
41 import java.util.List;
42 import java.util.Vector;
44 import junit.extensions.PA;
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.Test;
51 public class SequenceTest
54 @BeforeClass(alwaysRun = true)
55 public void setUpJvOptionPane()
57 JvOptionPane.setInteractiveMode(false);
58 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63 @BeforeMethod(alwaysRun = true)
66 seq = new Sequence("FER1", "AKPNGVL");
69 @Test(groups = { "Functional" })
70 public void testInsertGapsAndGapmaps()
72 SequenceI aseq = seq.deriveSequence();
73 aseq.insertCharAt(2, 3, '-');
74 aseq.insertCharAt(6, 3, '-');
75 assertEquals("Gap insertions not correct", "AK---P---NGVL",
76 aseq.getSequenceAsString());
77 List<int[]> gapInt = aseq.getInsertions();
78 assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
79 assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
80 assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
81 assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
83 BitSet gapfield = aseq.getInsertionsAsBits();
84 BitSet expectedgaps = new BitSet();
85 expectedgaps.set(2, 5);
86 expectedgaps.set(6, 9);
88 assertEquals(6, expectedgaps.cardinality());
90 assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
91 6, gapfield.cardinality());
93 assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield);
96 @Test(groups = ("Functional"))
97 public void testIsProtein()
100 assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
102 assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
104 SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
105 assertFalse(sq.isProtein());
106 // change sequence, should trigger an update of cached result
107 sq.setSequence("ASDFASDFADSF");
108 assertTrue(sq.isProtein());
111 @Test(groups = { "Functional" })
112 public void testGetAnnotation()
114 // initial state returns null not an empty array
115 assertNull(seq.getAnnotation());
116 AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
118 AlignmentAnnotation[] anns = seq.getAnnotation();
119 assertEquals(1, anns.length);
120 assertSame(ann, anns[0]);
122 // removing all annotations reverts array to null
123 seq.removeAlignmentAnnotation(ann);
124 assertNull(seq.getAnnotation());
127 @Test(groups = { "Functional" })
128 public void testGetAnnotation_forLabel()
130 AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
132 addAnnotation("label2", "desc2", "calcId2", 1f);
133 AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
135 AlignmentAnnotation[] anns = seq.getAnnotation("label1");
136 assertEquals(2, anns.length);
137 assertSame(ann1, anns[0]);
138 assertSame(ann3, anns[1]);
141 private AlignmentAnnotation addAnnotation(String label,
142 String description, String calcId, float value)
144 final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
146 annotation.setCalcId(calcId);
147 seq.addAlignmentAnnotation(annotation);
151 @Test(groups = { "Functional" })
152 public void testGetAlignmentAnnotations_forCalcIdAndLabel()
154 addAnnotation("label1", "desc1", "calcId1", 1f);
155 AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
157 addAnnotation("label2", "desc3", "calcId3", 1f);
158 AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
160 addAnnotation("label5", "desc3", null, 1f);
161 addAnnotation(null, "desc3", "calcId3", 1f);
163 List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
165 assertEquals(2, anns.size());
166 assertSame(ann2, anns.get(0));
167 assertSame(ann4, anns.get(1));
169 assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
170 assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
171 assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
172 assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
173 assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
177 * Tests for addAlignmentAnnotation. Note this method has the side-effect of
178 * setting the sequenceRef on the annotation. Adding the same annotation twice
181 @Test(groups = { "Functional" })
182 public void testAddAlignmentAnnotation()
184 assertNull(seq.getAnnotation());
185 final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
187 assertNull(annotation.sequenceRef);
188 seq.addAlignmentAnnotation(annotation);
189 assertSame(seq, annotation.sequenceRef);
190 AlignmentAnnotation[] anns = seq.getAnnotation();
191 assertEquals(1, anns.length);
192 assertSame(annotation, anns[0]);
194 // re-adding does nothing
195 seq.addAlignmentAnnotation(annotation);
196 anns = seq.getAnnotation();
197 assertEquals(1, anns.length);
198 assertSame(annotation, anns[0]);
200 // an identical but different annotation can be added
201 final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
203 seq.addAlignmentAnnotation(annotation2);
204 anns = seq.getAnnotation();
205 assertEquals(2, anns.length);
206 assertSame(annotation, anns[0]);
207 assertSame(annotation2, anns[1]);
210 @Test(groups = { "Functional" })
211 public void testGetStartGetEnd()
213 SequenceI sq = new Sequence("test", "ABCDEF");
214 assertEquals(1, sq.getStart());
215 assertEquals(6, sq.getEnd());
217 sq = new Sequence("test", "--AB-C-DEF--");
218 assertEquals(1, sq.getStart());
219 assertEquals(6, sq.getEnd());
221 sq = new Sequence("test", "----");
222 assertEquals(1, sq.getStart());
223 assertEquals(0, sq.getEnd()); // ??
227 * Tests for the method that returns an alignment column position (base 1) for
228 * a given sequence position (base 1).
230 @Test(groups = { "Functional" })
231 public void testFindIndex()
234 * call sequenceChanged() after each test to invalidate any cursor,
235 * forcing the 1-arg findIndex to be executed
237 SequenceI sq = new Sequence("test", "ABCDEF");
238 assertEquals(0, sq.findIndex(0));
239 sq.sequenceChanged();
240 assertEquals(1, sq.findIndex(1));
241 sq.sequenceChanged();
242 assertEquals(5, sq.findIndex(5));
243 sq.sequenceChanged();
244 assertEquals(6, sq.findIndex(6));
245 sq.sequenceChanged();
246 assertEquals(6, sq.findIndex(9));
248 sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
249 assertEquals(2, sq.findIndex(8));
250 sq.sequenceChanged();
251 assertEquals(5, sq.findIndex(9));
252 sq.sequenceChanged();
253 assertEquals(7, sq.findIndex(10));
255 // before start returns 0
256 sq.sequenceChanged();
257 assertEquals(0, sq.findIndex(0));
258 sq.sequenceChanged();
259 assertEquals(0, sq.findIndex(-1));
261 // beyond end returns last residue column
262 sq.sequenceChanged();
263 assertEquals(13, sq.findIndex(99));
267 * Tests for the method that returns a dataset sequence position (start..) for
268 * an aligned column position (base 0).
270 @Test(groups = { "Functional" })
271 public void testFindPosition()
274 * call sequenceChanged() after each test to invalidate any cursor,
275 * forcing the 1-arg findPosition to be executed
277 SequenceI sq = new Sequence("test/8-13", "ABCDEF");
278 assertEquals(8, sq.findPosition(0));
279 // Sequence should now hold a cursor at [8, 0]
280 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
281 PA.getValue(sq, "cursor").toString());
282 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
283 int token = (int) PA.getValue(sq, "changeCount");
284 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
286 sq.sequenceChanged();
289 * find F13 at column offset 5, cursor should update to [13, 6]
290 * endColumn is found and saved in cursor
292 assertEquals(13, sq.findPosition(5));
293 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
294 assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
295 assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
296 assertEquals("test:Pos13:Col6:startCol1:endCol6:tok1",
297 PA.getValue(sq, "cursor").toString());
299 // assertEquals(-1, seq.findPosition(6)); // fails
301 sq = new Sequence("test/8-11", "AB-C-D--");
302 token = (int) PA.getValue(sq, "changeCount"); // 0
303 assertEquals(8, sq.findPosition(0));
304 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
305 assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
306 assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
307 PA.getValue(sq, "cursor").toString());
309 sq.sequenceChanged();
310 assertEquals(9, sq.findPosition(1));
311 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
312 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
313 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok1",
314 PA.getValue(sq, "cursor").toString());
316 sq.sequenceChanged();
317 // gap position 'finds' residue to the right (not the left as per javadoc)
318 // cursor is set to the last residue position found [B 2]
319 assertEquals(10, sq.findPosition(2));
320 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
321 assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
322 assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
323 PA.getValue(sq, "cursor").toString());
325 sq.sequenceChanged();
326 assertEquals(10, sq.findPosition(3));
327 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
328 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
329 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok3",
330 PA.getValue(sq, "cursor").toString());
332 sq.sequenceChanged();
333 // column[4] is the gap after C - returns D11
334 // cursor is set to [C 4]
335 assertEquals(11, sq.findPosition(4));
336 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
337 assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
338 assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
339 PA.getValue(sq, "cursor").toString());
341 sq.sequenceChanged();
342 assertEquals(11, sq.findPosition(5)); // D
343 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
344 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
345 // lastCol has been found and saved in the cursor
346 assertEquals("test:Pos11:Col6:startCol1:endCol6:tok5",
347 PA.getValue(sq, "cursor").toString());
349 sq.sequenceChanged();
350 // returns 1 more than sequence length if off the end ?!?
351 assertEquals(12, sq.findPosition(6));
353 sq.sequenceChanged();
354 assertEquals(12, sq.findPosition(7));
357 * first findPosition should also set firstResCol in cursor
359 sq = new Sequence("test/8-13", "--AB-C-DEF--");
360 assertEquals(8, sq.findPosition(0));
361 assertNull(PA.getValue(sq, "cursor"));
363 sq.sequenceChanged();
364 assertEquals(8, sq.findPosition(1));
365 assertNull(PA.getValue(sq, "cursor"));
367 sq.sequenceChanged();
368 assertEquals(8, sq.findPosition(2));
369 assertEquals("test:Pos8:Col3:startCol3:endCol0:tok2",
370 PA.getValue(sq, "cursor").toString());
372 sq.sequenceChanged();
373 assertEquals(9, sq.findPosition(3));
374 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok3",
375 PA.getValue(sq, "cursor").toString());
377 sq.sequenceChanged();
378 // column[4] is a gap, returns next residue pos (C10)
379 // cursor is set to last residue found [B]
380 assertEquals(10, sq.findPosition(4));
381 assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
382 PA.getValue(sq, "cursor").toString());
384 sq.sequenceChanged();
385 assertEquals(10, sq.findPosition(5));
386 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok5",
387 PA.getValue(sq, "cursor").toString());
389 sq.sequenceChanged();
390 // column[6] is a gap, returns next residue pos (D11)
391 // cursor is set to last residue found [C]
392 assertEquals(11, sq.findPosition(6));
393 assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
394 PA.getValue(sq, "cursor").toString());
396 sq.sequenceChanged();
397 assertEquals(11, sq.findPosition(7));
398 assertEquals("test:Pos11:Col8:startCol3:endCol0:tok7",
399 PA.getValue(sq, "cursor").toString());
401 sq.sequenceChanged();
402 assertEquals(12, sq.findPosition(8));
403 assertEquals("test:Pos12:Col9:startCol3:endCol0:tok8",
404 PA.getValue(sq, "cursor").toString());
407 * when the last residue column is found, it is set in the cursor
409 sq.sequenceChanged();
410 assertEquals(13, sq.findPosition(9));
411 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok9",
412 PA.getValue(sq, "cursor").toString());
414 sq.sequenceChanged();
415 assertEquals(14, sq.findPosition(10));
416 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
417 PA.getValue(sq, "cursor").toString());
420 * findPosition for column beyond sequence length
421 * returns 1 more than last residue position
423 sq.sequenceChanged();
424 assertEquals(14, sq.findPosition(11));
425 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
426 PA.getValue(sq, "cursor").toString());
428 sq.sequenceChanged();
429 assertEquals(14, sq.findPosition(99));
430 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
431 PA.getValue(sq, "cursor").toString());
434 * gapped sequence ending in non-gap
436 sq = new Sequence("test/8-13", "--AB-C-DEF");
437 assertEquals(13, sq.findPosition(9));
438 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok0",
439 PA.getValue(sq, "cursor").toString());
440 sq.sequenceChanged();
441 assertEquals(12, sq.findPosition(8));
442 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
443 assertEquals("test:Pos12:Col9:startCol3:endCol10:tok1",
445 // findPosition with cursor accepts base 1 column values
446 assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
447 assertEquals(13, sq.findPosition(9));
448 assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
449 PA.getValue(sq, "cursor").toString());
452 @Test(groups = { "Functional" })
453 public void testDeleteChars()
458 SequenceI sq = new Sequence("test", "ABCDEF");
459 assertNull(PA.getValue(sq, "datasetSequence"));
460 assertEquals(1, sq.getStart());
461 assertEquals(6, sq.getEnd());
462 sq.deleteChars(2, 3);
463 assertEquals("ABDEF", sq.getSequenceAsString());
464 assertEquals(1, sq.getStart());
465 assertEquals(5, sq.getEnd());
466 assertNull(PA.getValue(sq, "datasetSequence"));
471 sq = new Sequence("test", "ABCDEF");
472 sq.deleteChars(0, 2);
473 assertEquals("CDEF", sq.getSequenceAsString());
474 assertEquals(3, sq.getStart());
475 assertEquals(6, sq.getEnd());
476 assertNull(PA.getValue(sq, "datasetSequence"));
481 sq = new Sequence("test", "ABCDEF");
482 sq.deleteChars(4, 6);
483 assertEquals("ABCD", sq.getSequenceAsString());
484 assertEquals(1, sq.getStart());
485 assertEquals(4, sq.getEnd());
486 assertNull(PA.getValue(sq, "datasetSequence"));
489 @Test(groups = { "Functional" })
490 public void testDeleteChars_withDbRefsAndFeatures()
493 * internal delete - new dataset sequence created
494 * gets a copy of any dbrefs
496 SequenceI sq = new Sequence("test", "ABCDEF");
497 sq.createDatasetSequence();
498 DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
500 Object ds = PA.getValue(sq, "datasetSequence");
502 assertEquals(1, sq.getStart());
503 assertEquals(6, sq.getEnd());
504 sq.deleteChars(2, 3);
505 assertEquals("ABDEF", sq.getSequenceAsString());
506 assertEquals(1, sq.getStart());
507 assertEquals(5, sq.getEnd());
508 Object newDs = PA.getValue(sq, "datasetSequence");
509 assertNotNull(newDs);
510 assertNotSame(ds, newDs);
511 assertNotNull(sq.getDBRefs());
512 assertEquals(1, sq.getDBRefs().length);
513 assertNotSame(dbr1, sq.getDBRefs()[0]);
514 assertEquals(dbr1, sq.getDBRefs()[0]);
517 * internal delete with sequence features
518 * (failure case for JAL-2541)
520 sq = new Sequence("test", "ABCDEF");
521 sq.createDatasetSequence();
522 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
524 sq.addSequenceFeature(sf1);
525 ds = PA.getValue(sq, "datasetSequence");
527 assertEquals(1, sq.getStart());
528 assertEquals(6, sq.getEnd());
529 sq.deleteChars(2, 4);
530 assertEquals("ABEF", sq.getSequenceAsString());
531 assertEquals(1, sq.getStart());
532 assertEquals(4, sq.getEnd());
533 newDs = PA.getValue(sq, "datasetSequence");
534 assertNotNull(newDs);
535 assertNotSame(ds, newDs);
536 List<SequenceFeature> sfs = sq.getSequenceFeatures();
537 assertEquals(1, sfs.size());
538 assertNotSame(sf1, sfs.get(0));
539 assertEquals(sf1, sfs.get(0));
542 * delete at start - no new dataset sequence created
543 * any sequence features remain as before
545 sq = new Sequence("test", "ABCDEF");
546 sq.createDatasetSequence();
547 ds = PA.getValue(sq, "datasetSequence");
548 sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
549 sq.addSequenceFeature(sf1);
550 sq.deleteChars(0, 2);
551 assertEquals("CDEF", sq.getSequenceAsString());
552 assertEquals(3, sq.getStart());
553 assertEquals(6, sq.getEnd());
554 assertSame(ds, PA.getValue(sq, "datasetSequence"));
555 sfs = sq.getSequenceFeatures();
557 assertEquals(1, sfs.size());
558 assertSame(sf1, sfs.get(0));
561 * delete at end - no new dataset sequence created
562 * any dbrefs remain as before
564 sq = new Sequence("test", "ABCDEF");
565 sq.createDatasetSequence();
566 ds = PA.getValue(sq, "datasetSequence");
567 dbr1 = new DBRefEntry("Uniprot", "0", "a123");
569 sq.deleteChars(4, 6);
570 assertEquals("ABCD", sq.getSequenceAsString());
571 assertEquals(1, sq.getStart());
572 assertEquals(4, sq.getEnd());
573 assertSame(ds, PA.getValue(sq, "datasetSequence"));
574 assertNotNull(sq.getDBRefs());
575 assertEquals(1, sq.getDBRefs().length);
576 assertSame(dbr1, sq.getDBRefs()[0]);
579 @Test(groups = { "Functional" })
580 public void testInsertCharAt()
582 // non-static methods:
583 SequenceI sq = new Sequence("test", "ABCDEF");
584 sq.insertCharAt(0, 'z');
585 assertEquals("zABCDEF", sq.getSequenceAsString());
586 sq.insertCharAt(2, 2, 'x');
587 assertEquals("zAxxBCDEF", sq.getSequenceAsString());
589 // for static method see StringUtilsTest
593 * Test the method that returns an array of aligned sequence positions where
594 * the array index is the data sequence position (both base 0).
596 @Test(groups = { "Functional" })
597 public void testGapMap()
599 SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
600 sq.createDatasetSequence();
601 assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
605 * Test the method that gets sequence features, either from the sequence or
608 @Test(groups = { "Functional" })
609 public void testGetSequenceFeatures()
611 SequenceI sq = new Sequence("test", "GATCAT");
612 sq.createDatasetSequence();
614 assertTrue(sq.getSequenceFeatures().isEmpty());
617 * SequenceFeature on sequence
619 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
620 sq.addSequenceFeature(sf);
621 List<SequenceFeature> sfs = sq.getSequenceFeatures();
622 assertEquals(1, sfs.size());
623 assertSame(sf, sfs.get(0));
626 * SequenceFeature on sequence and dataset sequence; returns that on
629 * Note JAL-2046: spurious: we have no use case for this at the moment.
630 * This test also buggy - as sf2.equals(sf), no new feature is added
632 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
634 sq.getDatasetSequence().addSequenceFeature(sf2);
635 sfs = sq.getSequenceFeatures();
636 assertEquals(1, sfs.size());
637 assertSame(sf, sfs.get(0));
640 * SequenceFeature on dataset sequence only
641 * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
643 sq.setSequenceFeatures(null);
644 assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
647 * Corrupt case - no SequenceFeature, dataset's dataset is the original
648 * sequence. Test shows no infinite loop results.
650 sq.getDatasetSequence().setSequenceFeatures(null);
652 * is there a usecase for this ? setDatasetSequence should throw an error if
653 * this actually occurs.
657 sq.getDatasetSequence().setDatasetSequence(sq); // loop!
658 Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
659 } catch (IllegalArgumentException e)
661 // TODO Jalview error/exception class for raising implementation errors
662 assertTrue(e.getMessage().toLowerCase()
663 .contains("implementation error"));
665 assertTrue(sq.getSequenceFeatures().isEmpty());
669 * Test the method that returns an array, indexed by sequence position, whose
670 * entries are the residue positions at the sequence position (or to the right
673 @Test(groups = { "Functional" })
674 public void testFindPositionMap()
677 * Note: Javadoc for findPosition says it returns the residue position to
678 * the left of a gapped position; in fact it returns the position to the
679 * right. Also it returns a non-existent residue position for a gap beyond
682 Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
683 int[] map = sq.findPositionMap();
684 assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
685 Arrays.toString(map));
689 * Test for getSubsequence
691 @Test(groups = { "Functional" })
692 public void testGetSubsequence()
694 SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
695 sq.createDatasetSequence();
697 // positions are base 0, end position is exclusive
698 SequenceI subseq = sq.getSubSequence(2, 4);
700 assertEquals("CD", subseq.getSequenceAsString());
701 // start/end are base 1 positions
702 assertEquals(3, subseq.getStart());
703 assertEquals(4, subseq.getEnd());
704 // subsequence shares the full dataset sequence
705 assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
709 * test createDatasetSequence behaves to doc
711 @Test(groups = { "Functional" })
712 public void testCreateDatasetSequence()
714 SequenceI sq = new Sequence("my", "ASDASD");
715 sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
717 sq.addDBRef(new DBRefEntry("source", "version", "accession"));
718 assertNull(sq.getDatasetSequence());
719 assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
720 assertNotNull(PA.getValue(sq, "dbrefs"));
722 SequenceI rds = sq.createDatasetSequence();
724 assertNull(rds.getDatasetSequence());
725 assertSame(sq.getDatasetSequence(), rds);
727 // sequence features and dbrefs transferred to dataset sequence
728 assertNull(PA.getValue(sq, "sequenceFeatureStore"));
729 assertNull(PA.getValue(sq, "dbrefs"));
730 assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
731 assertNotNull(PA.getValue(rds, "dbrefs"));
735 * Test for deriveSequence applied to a sequence with a dataset
737 @Test(groups = { "Functional" })
738 public void testDeriveSequence_existingDataset()
740 Sequence sq = new Sequence("Seq1", "CD");
741 sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
742 sq.getDatasetSequence().addSequenceFeature(
743 new SequenceFeature("", "", 1, 2, 0f, null));
747 sq.setDescription("Test sequence description..");
748 sq.setVamsasId("TestVamsasId");
749 sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
751 sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
752 sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
753 sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
754 sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
756 sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
757 sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
758 sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
759 sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
761 // these are the same as ones already added
762 DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
763 DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
765 List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
768 sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
769 sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
770 sq.getDatasetSequence().addDBRef(
771 new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
772 sq.getDatasetSequence().addDBRef(
773 new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
775 PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
776 PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
777 PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
779 PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
781 sq.getDatasetSequence().addPDBId(pdbe1a);
782 sq.getDatasetSequence().addPDBId(pdbe1b);
783 sq.getDatasetSequence().addPDBId(pdbe2a);
784 sq.getDatasetSequence().addPDBId(pdbe2b);
787 * test we added pdb entries to the dataset sequence
789 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
790 .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
791 "PDB Entries were not found on dataset sequence.");
794 * we should recover a pdb entry that is on the dataset sequence via PDBEntry
796 Assert.assertEquals(pdbe1a,
797 sq.getDatasetSequence().getPDBEntry("1PDB"),
798 "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
799 ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
800 System.out.println(">>>>>> " + sq.getSequenceAsString().length());
801 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
802 annotsList.add(new Annotation("A", "A", 'X', 0.1f));
803 Annotation[] annots = annotsList.toArray(new Annotation[0]);
804 sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
805 "Test annot description", annots));
806 sq.getDatasetSequence().addAlignmentAnnotation(
807 new AlignmentAnnotation("Test annot", "Test annot description",
809 Assert.assertEquals(sq.getDescription(), "Test sequence description..");
810 Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
812 Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
813 Assert.assertNotNull(sq.getAnnotation());
814 Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
815 Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
818 Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
820 Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
822 Sequence derived = (Sequence) sq.deriveSequence();
824 Assert.assertEquals(derived.getDescription(),
825 "Test sequence description..");
826 Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
827 Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
828 Assert.assertNotNull(derived.getAnnotation());
829 Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
830 Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
831 Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
833 Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
835 assertEquals("CD", derived.getSequenceAsString());
836 assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
838 // derived sequence should access dataset sequence features
839 assertNotNull(sq.getSequenceFeatures());
840 assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
843 * verify we have primary db refs *just* for PDB IDs with associated
847 assertEquals(primRefs, sq.getPrimaryDBRefs());
848 assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
850 assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
855 * Test for deriveSequence applied to an ungapped sequence with no dataset
857 @Test(groups = { "Functional" })
858 public void testDeriveSequence_noDatasetUngapped()
860 SequenceI sq = new Sequence("Seq1", "ABCDEF");
861 assertEquals(1, sq.getStart());
862 assertEquals(6, sq.getEnd());
863 SequenceI derived = sq.deriveSequence();
864 assertEquals("ABCDEF", derived.getSequenceAsString());
865 assertEquals("ABCDEF", derived.getDatasetSequence()
866 .getSequenceAsString());
870 * Test for deriveSequence applied to a gapped sequence with no dataset
872 @Test(groups = { "Functional" })
873 public void testDeriveSequence_noDatasetGapped()
875 SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
876 assertEquals(1, sq.getStart());
877 assertEquals(6, sq.getEnd());
878 assertNull(sq.getDatasetSequence());
879 SequenceI derived = sq.deriveSequence();
880 assertEquals("AB-C.D EF", derived.getSequenceAsString());
881 assertEquals("ABCDEF", derived.getDatasetSequence()
882 .getSequenceAsString());
885 @Test(groups = { "Functional" })
886 public void testCopyConstructor_noDataset()
888 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
889 seq1.setDescription("description");
890 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
892 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
894 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
895 seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
897 SequenceI copy = new Sequence(seq1);
899 assertNull(copy.getDatasetSequence());
901 verifyCopiedSequence(seq1, copy);
903 // copy has a copy of the DBRefEntry
904 // this is murky - DBrefs are only copied for dataset sequences
905 // where the test for 'dataset sequence' is 'dataset is null'
906 // but that doesn't distinguish it from an aligned sequence
907 // which has not yet generated a dataset sequence
908 // NB getDBRef looks inside dataset sequence if not null
909 DBRefEntry[] dbrefs = copy.getDBRefs();
910 assertEquals(1, dbrefs.length);
911 assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
912 assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
915 @Test(groups = { "Functional" })
916 public void testCopyConstructor_withDataset()
918 SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
919 seq1.createDatasetSequence();
920 seq1.setDescription("description");
921 seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
923 // JAL-2046 - what is the contract for using a derived sequence's
924 // addSequenceFeature ?
925 seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
927 seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
928 // here we add DBRef to the dataset sequence:
929 seq1.getDatasetSequence().addDBRef(
930 new DBRefEntry("EMBL", "1.2", "AZ12345"));
932 SequenceI copy = new Sequence(seq1);
934 assertNotNull(copy.getDatasetSequence());
935 assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
937 verifyCopiedSequence(seq1, copy);
939 // getDBRef looks inside dataset sequence and this is shared,
940 // so holds the same dbref objects
941 DBRefEntry[] dbrefs = copy.getDBRefs();
942 assertEquals(1, dbrefs.length);
943 assertSame(dbrefs[0], seq1.getDBRefs()[0]);
947 * Helper to make assertions about a copied sequence
952 protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
954 // verify basic properties:
955 assertEquals(copy.getName(), seq1.getName());
956 assertEquals(copy.getDescription(), seq1.getDescription());
957 assertEquals(copy.getStart(), seq1.getStart());
958 assertEquals(copy.getEnd(), seq1.getEnd());
959 assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
961 // copy has a copy of the annotation:
962 AlignmentAnnotation[] anns = copy.getAnnotation();
963 assertEquals(1, anns.length);
964 assertFalse(anns[0] == seq1.getAnnotation()[0]);
965 assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
966 assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
967 assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
969 // copy has a copy of the sequence feature:
970 List<SequenceFeature> sfs = copy.getSequenceFeatures();
971 assertEquals(1, sfs.size());
972 if (seq1.getDatasetSequence() != null
973 && copy.getDatasetSequence() == seq1.getDatasetSequence())
975 assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
979 assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
981 assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
983 // copy has a copy of the PDB entry
984 Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
985 assertEquals(1, pdbs.size());
986 assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
987 assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
990 @Test(groups = "Functional")
991 public void testGetCharAt()
993 SequenceI sq = new Sequence("", "abcde");
994 assertEquals('a', sq.getCharAt(0));
995 assertEquals('e', sq.getCharAt(4));
996 assertEquals(' ', sq.getCharAt(5));
997 assertEquals(' ', sq.getCharAt(-1));
1000 @Test(groups = { "Functional" })
1001 public void testAddSequenceFeatures()
1003 SequenceI sq = new Sequence("", "abcde");
1004 // type may not be null
1005 assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
1007 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1009 // can't add a duplicate feature
1010 assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
1012 // can add a different feature
1013 assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
1014 8, 0f, null))); // different type
1015 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
1016 "description", 4, 8, 0f, null)));// different description
1017 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
1018 8, 0f, null))); // different start position
1019 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1020 9, 0f, null))); // different end position
1021 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1022 8, 1f, null))); // different score
1023 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1024 8, Float.NaN, null))); // score NaN
1025 assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1026 8, 0f, "Metal"))); // different group
1027 assertEquals(8, sq.getFeatures().getAllFeatures().size());
1031 * Tests for adding (or updating) dbrefs
1033 * @see DBRefEntry#updateFrom(DBRefEntry)
1035 @Test(groups = { "Functional" })
1036 public void testAddDBRef()
1038 SequenceI sq = new Sequence("", "abcde");
1039 assertNull(sq.getDBRefs());
1040 DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1042 assertEquals(1, sq.getDBRefs().length);
1043 assertSame(dbref, sq.getDBRefs()[0]);
1046 * change of version - new entry
1048 DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1049 sq.addDBRef(dbref2);
1050 assertEquals(2, sq.getDBRefs().length);
1051 assertSame(dbref, sq.getDBRefs()[0]);
1052 assertSame(dbref2, sq.getDBRefs()[1]);
1055 * matches existing entry - not added
1057 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1058 assertEquals(2, sq.getDBRefs().length);
1061 * different source = new entry
1063 DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1064 sq.addDBRef(dbref3);
1065 assertEquals(3, sq.getDBRefs().length);
1066 assertSame(dbref3, sq.getDBRefs()[2]);
1069 * different ref = new entry
1071 DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1072 sq.addDBRef(dbref4);
1073 assertEquals(4, sq.getDBRefs().length);
1074 assertSame(dbref4, sq.getDBRefs()[3]);
1077 * matching ref with a mapping - map updated
1079 DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1080 Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1083 sq.addDBRef(dbref5);
1084 assertEquals(4, sq.getDBRefs().length);
1085 assertSame(dbref4, sq.getDBRefs()[3]);
1086 assertSame(map, dbref4.getMap());
1089 * 'real' version replaces "0" version
1091 dbref2.setVersion("0");
1092 DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1093 dbref2.getAccessionId());
1094 sq.addDBRef(dbref6);
1095 assertEquals(4, sq.getDBRefs().length);
1096 assertSame(dbref2, sq.getDBRefs()[1]);
1097 assertEquals("3", dbref2.getVersion());
1100 * 'real' version replaces "source:0" version
1102 dbref3.setVersion("Uniprot:0");
1103 DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1104 dbref3.getAccessionId());
1105 sq.addDBRef(dbref7);
1106 assertEquals(4, sq.getDBRefs().length);
1107 assertSame(dbref3, sq.getDBRefs()[2]);
1108 assertEquals("3", dbref2.getVersion());
1111 @Test(groups = { "Functional" })
1112 public void testGetPrimaryDBRefs_peptide()
1114 SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1117 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1118 assertTrue(primaryDBRefs.isEmpty());
1121 sq.setDBRefs(new DBRefEntry[] {});
1122 primaryDBRefs = sq.getPrimaryDBRefs();
1123 assertTrue(primaryDBRefs.isEmpty());
1125 // primary - uniprot
1126 DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1127 sq.addDBRef(upentry1);
1129 // primary - uniprot with congruent map
1130 DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1131 upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1132 new int[] { 10, 22 }, 1, 1)));
1133 sq.addDBRef(upentry2);
1135 // primary - uniprot with map of enclosing sequence
1136 DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1137 upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1138 new int[] { 8, 24 }, 1, 1)));
1139 sq.addDBRef(upentry3);
1141 // not primary - uniprot with map of sub-sequence (5')
1142 DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1143 upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1144 new int[] { 10, 18 }, 1, 1)));
1145 sq.addDBRef(upentry4);
1147 // not primary - uniprot with map that overlaps 3'
1148 DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1149 upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1150 new int[] { 12, 22 }, 1, 1)));
1151 sq.addDBRef(upentry5);
1153 // not primary - uniprot with map to different coordinates frame
1154 DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1155 upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1156 new int[] { 112, 118 }, 1, 1)));
1157 sq.addDBRef(upentry6);
1159 // not primary - dbref to 'non-core' database
1160 DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1161 sq.addDBRef(upentry7);
1163 // primary - type is PDB
1164 DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1165 sq.addDBRef(pdbentry);
1167 // not primary - PDBEntry has no file
1168 sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1170 // not primary - no PDBEntry
1171 sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1173 // add corroborating PDB entry for primary DBref -
1174 // needs to have a file as well as matching ID
1175 // note PDB ID is not treated as case sensitive
1176 sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1179 // not valid DBRef - no file..
1180 sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1182 primaryDBRefs = sq.getPrimaryDBRefs();
1183 assertEquals(4, primaryDBRefs.size());
1184 assertTrue("Couldn't find simple primary reference (UNIPROT)",
1185 primaryDBRefs.contains(upentry1));
1186 assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1187 primaryDBRefs.contains(upentry2));
1188 assertTrue("Couldn't find mapped context reference (UNIPROT)",
1189 primaryDBRefs.contains(upentry3));
1190 assertTrue("Couldn't find expected PDB primary reference",
1191 primaryDBRefs.contains(pdbentry));
1194 @Test(groups = { "Functional" })
1195 public void testGetPrimaryDBRefs_nucleotide()
1197 SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1199 // primary - Ensembl
1200 DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1203 // not primary - Ensembl 'transcript' mapping of sub-sequence
1204 DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1205 dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1206 new int[] { 1, 11 }, 1, 1)));
1209 // primary - EMBL with congruent map
1210 DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1211 dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1212 new int[] { 10, 34 }, 1, 1)));
1215 // not primary - to non-core database
1216 DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1219 // not primary - to protein
1220 DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1223 List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1224 assertEquals(2, primaryDBRefs.size());
1225 assertTrue(primaryDBRefs.contains(dbr1));
1226 assertTrue(primaryDBRefs.contains(dbr3));
1230 * Test the method that updates the list of PDBEntry from any new DBRefEntry
1233 @Test(groups = { "Functional" })
1234 public void testUpdatePDBIds()
1236 PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1237 seq.addPDBId(pdbe1);
1238 seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1239 seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1240 seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1241 seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1242 // 7 is not a valid chain code:
1243 seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1246 List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1247 assertEquals(4, pdbIds.size());
1248 assertSame(pdbe1, pdbIds.get(0));
1249 // chain code got added to 3A6S:
1250 assertEquals("B", pdbe1.getChainCode());
1251 assertEquals("1A70", pdbIds.get(1).getId());
1252 // 4BQGA is parsed into id + chain
1253 assertEquals("4BQG", pdbIds.get(2).getId());
1254 assertEquals("a", pdbIds.get(2).getChainCode());
1255 assertEquals("2GIS7", pdbIds.get(3).getId());
1256 assertNull(pdbIds.get(3).getChainCode());
1260 * Test the method that either adds a pdbid or updates an existing one
1262 @Test(groups = { "Functional" })
1263 public void testAddPDBId()
1265 PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1267 assertEquals(1, seq.getAllPDBEntries().size());
1268 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1269 assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1271 // add the same entry
1273 assertEquals(1, seq.getAllPDBEntries().size());
1274 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1276 // add an identical entry
1277 seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1278 assertEquals(1, seq.getAllPDBEntries().size());
1279 assertSame(pdbe, seq.getPDBEntry("3A6S"));
1281 // add a different entry
1282 PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1283 seq.addPDBId(pdbe2);
1284 assertEquals(2, seq.getAllPDBEntries().size());
1285 assertSame(pdbe, seq.getAllPDBEntries().get(0));
1286 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1288 // update pdbe with chain code, file, type
1289 PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1290 seq.addPDBId(pdbe3);
1291 assertEquals(2, seq.getAllPDBEntries().size());
1292 assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1293 assertEquals("3A6S", pdbe.getId()); // unchanged
1294 assertEquals("A", pdbe.getChainCode()); // updated
1295 assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1296 assertEquals("filepath", pdbe.getFile()); // updated
1297 assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1299 // add with a different file path
1300 PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1301 seq.addPDBId(pdbe4);
1302 assertEquals(3, seq.getAllPDBEntries().size());
1303 assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1305 // add with a different chain code
1306 PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1307 seq.addPDBId(pdbe5);
1308 assertEquals(4, seq.getAllPDBEntries().size());
1309 assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1313 groups = { "Functional" },
1314 expectedExceptions = { IllegalArgumentException.class })
1315 public void testSetDatasetSequence_toSelf()
1317 seq.setDatasetSequence(seq);
1321 groups = { "Functional" },
1322 expectedExceptions = { IllegalArgumentException.class })
1323 public void testSetDatasetSequence_cascading()
1325 SequenceI seq2 = new Sequence("Seq2", "xyz");
1326 seq2.createDatasetSequence();
1327 seq.setDatasetSequence(seq2);
1330 @Test(groups = { "Functional" })
1331 public void testFindFeatures()
1333 SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1334 sq.createDatasetSequence();
1336 assertTrue(sq.findFeatures(1, 99).isEmpty());
1338 // add non-positional feature
1339 SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1341 sq.addSequenceFeature(sf0);
1342 // add feature on BCD
1343 SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1345 sq.addSequenceFeature(sf1);
1346 // add feature on DE
1347 SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1349 sq.addSequenceFeature(sf2);
1350 // add contact feature at [B, H]
1351 SequenceFeature sf3 = new SequenceFeature("Disulphide bond", "desc", 9,
1354 sq.addSequenceFeature(sf3);
1355 // add contact feature at [F, G]
1356 SequenceFeature sf4 = new SequenceFeature("Disulfide Bond", "desc", 13,
1359 sq.addSequenceFeature(sf4);
1361 // no features in columns 1-2 (-A)
1362 List<SequenceFeature> found = sq.findFeatures(1, 2);
1363 assertTrue(found.isEmpty());
1365 // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1366 found = sq.findFeatures(1, 6);
1367 assertEquals(2, found.size());
1368 assertTrue(found.contains(sf1));
1369 assertTrue(found.contains(sf3));
1371 // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1372 found = sq.findFeatures(5, 6);
1373 assertEquals(1, found.size());
1374 assertTrue(found.contains(sf1));
1376 // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1377 found = sq.findFeatures(7, 10);
1378 assertEquals(3, found.size());
1379 assertTrue(found.contains(sf1));
1380 assertTrue(found.contains(sf2));
1381 assertTrue(found.contains(sf4));
1384 @Test(groups = { "Functional" })
1385 public void testFindIndex_withCursor()
1387 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1390 assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1393 assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1396 assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1399 @Test(groups = { "Functional" })
1400 public void testFindPosition_withCursor()
1402 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1404 // find F pos given A - lastCol gets set in cursor
1405 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1406 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1407 PA.getValue(sq, "cursor").toString());
1409 // find A pos given F - first residue column is saved in cursor
1410 assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1411 assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
1412 PA.getValue(sq, "cursor").toString());
1414 // find C pos given C (neither startCol nor endCol is set)
1415 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1416 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1417 PA.getValue(sq, "cursor").toString());
1419 // now the grey area - what residue position for a gapped column? JAL-2562
1421 // find 'residue' for column 3 given cursor for D (so working left)
1422 // returns B9; cursor is updated to [B 5]
1423 assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1424 assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
1425 PA.getValue(sq, "cursor").toString());
1427 // find 'residue' for column 8 given cursor for D (so working right)
1428 // returns E12; cursor is updated to [D 7]
1429 assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1430 assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
1431 PA.getValue(sq, "cursor").toString());
1433 // find 'residue' for column 12 given cursor for B
1434 // returns 1 more than last residue position; cursor is updated to [F 10]
1435 // lastCol position is saved in cursor
1436 assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1437 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1438 PA.getValue(sq, "cursor").toString());
1441 * findPosition for column beyond length of sequence
1442 * returns 1 more than the last residue position
1443 * cursor is set to last real residue position [F 10]
1445 assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
1446 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1447 PA.getValue(sq, "cursor").toString());
1450 * and the case without a trailing gap
1452 sq = new Sequence("test/8-13", "-A--BCD-EF");
1453 // first find C from A
1454 assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
1455 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1456 assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1458 // now 'find' 99 from C
1459 // cursor is set to [F 10] and saved lastCol
1460 assertEquals(14, sq.findPosition(99, cursor));
1461 assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1462 PA.getValue(sq, "cursor").toString());
1466 public void testIsValidCursor()
1468 Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1469 assertFalse(sq.isValidCursor(null));
1472 * cursor is valid if it has valid sequence ref and changeCount token
1473 * and positions within the range of the sequence
1475 int changeCount = (int) PA.getValue(sq, "changeCount");
1476 SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1477 assertTrue(sq.isValidCursor(cursor));
1480 * column position outside [0 - length] is rejected
1482 cursor = new SequenceCursor(sq, 13, -1, changeCount);
1483 assertFalse(sq.isValidCursor(cursor));
1484 cursor = new SequenceCursor(sq, 13, 10, changeCount);
1485 assertFalse(sq.isValidCursor(cursor));
1486 cursor = new SequenceCursor(sq, 7, 8, changeCount);
1487 assertFalse(sq.isValidCursor(cursor));
1488 cursor = new SequenceCursor(sq, 14, 2, changeCount);
1489 assertFalse(sq.isValidCursor(cursor));
1492 * wrong sequence is rejected
1494 cursor = new SequenceCursor(null, 13, 1, changeCount);
1495 assertFalse(sq.isValidCursor(cursor));
1496 cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1498 assertFalse(sq.isValidCursor(cursor));
1501 * wrong token value is rejected
1503 cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1504 assertFalse(sq.isValidCursor(cursor));
1505 cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1506 assertFalse(sq.isValidCursor(cursor));
1509 @Test(groups = { "Functional" })
1510 public void testFindPosition_withCursorAndEdits()
1512 Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1514 // find F pos given A
1515 assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1516 int token = (int) PA.getValue(sq, "changeCount"); // 0
1517 SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1518 assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1521 * setSequence should invalidate the cursor cached by the sequence
1523 sq.setSequence("-A-BCD-EF---"); // one gap removed
1524 assertEquals(8, sq.getStart()); // sanity check
1525 assertEquals(11, sq.findPosition(5)); // D11
1526 // cursor should now be at [D 6]
1527 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1528 assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1531 * deleteChars should invalidate the cached cursor
1533 sq.deleteChars(2, 5); // delete -BC
1534 assertEquals("-AD-EF---", sq.getSequenceAsString());
1535 assertEquals(8, sq.getStart()); // sanity check
1536 assertEquals(10, sq.findPosition(4)); // E10
1537 // cursor should now be at [E 5]
1538 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1539 assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1542 * Edit to insert gaps should invalidate the cached cursor
1543 * insert 2 gaps at column[3] to make -AD---EF---
1545 SequenceI[] seqs = new SequenceI[] { sq };
1546 AlignmentI al = new Alignment(seqs);
1547 new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1548 assertEquals("-AD---EF---", sq.getSequenceAsString());
1549 assertEquals(10, sq.findPosition(4)); // E10
1550 // cursor should now be at [D 3]
1551 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1552 assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1555 * insertCharAt should invalidate the cached cursor
1556 * insert CC at column[4] to make -AD-CC--EF---
1558 sq.insertCharAt(4, 2, 'C');
1559 assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1560 assertEquals(13, sq.findPosition(9)); // F13
1561 // cursor should now be at [F 10]
1562 cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1563 assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1566 @Test(groups = { "Functional" })
1567 public void testGetSequence()
1569 String seqstring = "-A--BCD-EF--";
1570 Sequence sq = new Sequence("test/8-13", seqstring);
1571 sq.createDatasetSequence();
1572 assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1573 assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1574 "ABCDEF".toCharArray()));
1576 // verify a copy of the sequence array is returned
1577 char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1578 assertNotSame(theSeq, sq.getSequence());
1579 theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1580 assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1583 @Test(groups = { "Functional" })
1584 public void testReplace()
1586 String seqstring = "-A--BCD-EF--";
1587 SequenceI sq = new Sequence("test/8-13", seqstring);
1588 assertEquals(0, PA.getValue(sq, "changeCount"));
1590 assertEquals(0, sq.replace('A', 'A')); // same char
1591 assertEquals(seqstring, sq.getSequenceAsString());
1592 assertEquals(0, PA.getValue(sq, "changeCount"));
1594 assertEquals(0, sq.replace('X', 'Y')); // not there
1595 assertEquals(seqstring, sq.getSequenceAsString());
1596 assertEquals(0, PA.getValue(sq, "changeCount"));
1598 assertEquals(1, sq.replace('A', 'K'));
1599 assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1600 assertEquals(1, PA.getValue(sq, "changeCount"));
1602 assertEquals(6, sq.replace('-', '.'));
1603 assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1604 assertEquals(2, PA.getValue(sq, "changeCount"));