JAL-4019 Added tests for new Nucleotide sequence detection
[jalview.git] / test / jalview / datamodel / SequenceTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
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;
30
31 import java.io.File;
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;
39
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeClass;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
44
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;
53
54 public class SequenceTest
55 {
56   @BeforeClass(alwaysRun = true)
57   public void setUpJvOptionPane()
58   {
59     JvOptionPane.setInteractiveMode(false);
60     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
61   }
62
63   @BeforeMethod(alwaysRun = true)
64   public void loadProperties()
65   {
66     Cache.loadProperties("test/jalview/util/comparisonTestProps.jvprops");
67   }
68
69   Sequence seq;
70
71   @BeforeMethod(alwaysRun = true)
72   public void setUp()
73   {
74     seq = new Sequence("FER1", "AKPNGVL");
75   }
76
77   @Test(groups = { "Functional" })
78   public void testInsertGapsAndGapmaps()
79   {
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]);
90
91     BitSet gapfield = aseq.getInsertionsAsBits();
92     BitSet expectedgaps = new BitSet();
93     expectedgaps.set(2, 5);
94     expectedgaps.set(6, 9);
95
96     assertEquals(6, expectedgaps.cardinality());
97
98     assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
99             6, gapfield.cardinality());
100
101     assertEquals("getInsertionsAsBits not correct.", expectedgaps,
102             gapfield);
103   }
104
105   @Test(groups = ("Functional"))
106   public void testIsProtein()
107   {
108     // test Protein
109     assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
110     // test DNA
111     assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
112     // test RNA
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());
118   }
119
120   @Test(groups = ("Functional"))
121   public void testIsProteinWithXorNAmbiguityCodes()
122   {
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());
128     // test DNA with X
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());
133     // test DNA with N
134     assertFalse(new Sequence("prot", "ACGTACGTACGTNNNNNNNN").isProtein());
135     // test RNA with X
136     assertFalse(new Sequence("prot", "ACGUACGUACGUACTGACAXX").isProtein());
137     assertFalse(new Sequence("prot", "ACGUACGUACGUNNNNNNNNN").isProtein());
138   }
139
140   @Test(groups = { "Functional" })
141   public void testGetAnnotation()
142   {
143     // initial state returns null not an empty array
144     assertNull(seq.getAnnotation());
145     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
146             1f);
147     AlignmentAnnotation[] anns = seq.getAnnotation();
148     assertEquals(1, anns.length);
149     assertSame(ann, anns[0]);
150
151     // removing all annotations reverts array to null
152     seq.removeAlignmentAnnotation(ann);
153     assertNull(seq.getAnnotation());
154   }
155
156   @Test(groups = { "Functional" })
157   public void testGetAnnotation_forLabel()
158   {
159     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
160             1f);
161     addAnnotation("label2", "desc2", "calcId2", 1f);
162     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
163             1f);
164     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
165     assertEquals(2, anns.length);
166     assertSame(ann1, anns[0]);
167     assertSame(ann3, anns[1]);
168   }
169
170   private AlignmentAnnotation addAnnotation(String label,
171           String description, String calcId, float value)
172   {
173     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
174             description, value);
175     annotation.setCalcId(calcId);
176     seq.addAlignmentAnnotation(annotation);
177     return annotation;
178   }
179
180   @Test(groups = { "Functional" })
181   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
182   {
183     addAnnotation("label1", "desc1", "calcId1", 1f);
184     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
185             1f);
186     addAnnotation("label2", "desc3", "calcId3", 1f);
187     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
188             1f);
189     addAnnotation("label5", "desc3", null, 1f);
190     addAnnotation(null, "desc3", "calcId3", 1f);
191
192     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
193             "label2");
194     assertEquals(2, anns.size());
195     assertSame(ann2, anns.get(0));
196     assertSame(ann4, anns.get(1));
197
198     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
199     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
200     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
201     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
202     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
203   }
204
205   @Test(groups = { "Functional" })
206   public void testGetAlignmentAnnotations_forCalcIdLabelAndDescription()
207   {
208     addAnnotation("label1", "desc1", "calcId1", 1f);
209     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
210             1f);
211     addAnnotation("label2", "desc3", "calcId3", 1f);
212     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
213             1f);
214     addAnnotation("label5", "desc3", null, 1f);
215     addAnnotation(null, "desc3", "calcId3", 1f);
216
217     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
218             "label2", "desc3");
219     assertEquals(1, anns.size());
220     assertSame(ann4, anns.get(0));
221     /**
222      * null matching should fail
223      */
224     assertTrue(seq.getAlignmentAnnotations("calcId3", "label2", null)
225             .isEmpty());
226
227     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3", null)
228             .isEmpty());
229     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5", null)
230             .isEmpty());
231     assertTrue(
232             seq.getAlignmentAnnotations("calcId2", null, null).isEmpty());
233     assertTrue(seq.getAlignmentAnnotations(null, "label3", null).isEmpty());
234     assertTrue(seq.getAlignmentAnnotations(null, null, null).isEmpty());
235   }
236
237   /**
238    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
239    * setting the sequenceRef on the annotation. Adding the same annotation twice
240    * should be ignored.
241    */
242   @Test(groups = { "Functional" })
243   public void testAddAlignmentAnnotation()
244   {
245     assertNull(seq.getAnnotation());
246     final AlignmentAnnotation annotation = new AlignmentAnnotation("a", "b",
247             2d);
248     assertNull(annotation.sequenceRef);
249     seq.addAlignmentAnnotation(annotation);
250     assertSame(seq, annotation.sequenceRef);
251     AlignmentAnnotation[] anns = seq.getAnnotation();
252     assertEquals(1, anns.length);
253     assertSame(annotation, anns[0]);
254
255     // re-adding does nothing
256     seq.addAlignmentAnnotation(annotation);
257     anns = seq.getAnnotation();
258     assertEquals(1, anns.length);
259     assertSame(annotation, anns[0]);
260
261     // an identical but different annotation can be added
262     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
263             "b", 2d);
264     seq.addAlignmentAnnotation(annotation2);
265     anns = seq.getAnnotation();
266     assertEquals(2, anns.length);
267     assertSame(annotation, anns[0]);
268     assertSame(annotation2, anns[1]);
269   }
270
271   @Test(groups = { "Functional" })
272   public void testGetStartGetEnd()
273   {
274     SequenceI sq = new Sequence("test", "ABCDEF");
275     assertEquals(1, sq.getStart());
276     assertEquals(6, sq.getEnd());
277
278     sq = new Sequence("test", "--AB-C-DEF--");
279     assertEquals(1, sq.getStart());
280     assertEquals(6, sq.getEnd());
281
282     sq = new Sequence("test", "----");
283     assertEquals(1, sq.getStart());
284     assertEquals(0, sq.getEnd()); // ??
285   }
286
287   /**
288    * Tests for the method that returns an alignment column position (base 1) for
289    * a given sequence position (base 1).
290    */
291   @Test(groups = { "Functional" })
292   public void testFindIndex()
293   {
294     /* 
295      * call sequenceChanged() after each test to invalidate any cursor,
296      * forcing the 1-arg findIndex to be executed
297      */
298     SequenceI sq = new Sequence("test", "ABCDEF");
299     assertEquals(0, sq.findIndex(0));
300     sq.sequenceChanged();
301     assertEquals(1, sq.findIndex(1));
302     sq.sequenceChanged();
303     assertEquals(5, sq.findIndex(5));
304     sq.sequenceChanged();
305     assertEquals(6, sq.findIndex(6));
306     sq.sequenceChanged();
307     assertEquals(6, sq.findIndex(9));
308
309     final String aligned = "-A--B-C-D-E-F--";
310     assertEquals(15, aligned.length());
311     sq = new Sequence("test/8-13", aligned);
312     assertEquals(2, sq.findIndex(8));
313     sq.sequenceChanged();
314     assertEquals(5, sq.findIndex(9));
315     sq.sequenceChanged();
316     assertEquals(7, sq.findIndex(10));
317
318     // before start returns 0
319     sq.sequenceChanged();
320     assertEquals(0, sq.findIndex(0));
321     sq.sequenceChanged();
322     assertEquals(0, sq.findIndex(-1));
323
324     // beyond end returns last residue column
325     sq.sequenceChanged();
326     assertEquals(13, sq.findIndex(99));
327
328     /*
329      * residue before sequence 'end' but beyond end of sequence returns 
330      * length of sequence (last column) (rightly or wrongly!)
331      */
332     sq = new Sequence("test/8-15", "A-B-C-"); // trailing gap case
333     assertEquals(6, sq.getLength());
334     sq.sequenceChanged();
335     assertEquals(sq.getLength(), sq.findIndex(14));
336     sq = new Sequence("test/8-99", "-A--B-C-D"); // trailing residue case
337     sq.sequenceChanged();
338     assertEquals(sq.getLength(), sq.findIndex(65));
339
340     /*
341      * residue after sequence 'start' but before first residue returns 
342      * zero (before first column) (rightly or wrongly!)
343      */
344     sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
345     sq.sequenceChanged();
346     assertEquals(0, sq.findIndex(3));
347     sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
348     sq.sequenceChanged();
349     assertEquals(0, sq.findIndex(2));
350   }
351
352   @Test(groups = { "Functional" })
353   public void testFindPositions()
354   {
355     SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
356
357     /*
358      * invalid inputs
359      */
360     assertNull(sq.findPositions(6, 5));
361     assertNull(sq.findPositions(0, 5));
362     assertNull(sq.findPositions(-1, 5));
363
364     /*
365      * all gapped ranges
366      */
367     assertNull(sq.findPositions(1, 1)); // 1-based columns
368     assertNull(sq.findPositions(5, 5));
369     assertNull(sq.findPositions(5, 6));
370     assertNull(sq.findPositions(5, 7));
371
372     /*
373      * all ungapped ranges
374      */
375     assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
376     assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
377     assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
378     assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
379
380     /*
381      * gap to ungapped range
382      */
383     assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
384     assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
385
386     /*
387      * ungapped to gapped range
388      */
389     assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
390     assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
391
392     /*
393      * ungapped to ungapped enclosing gaps
394      */
395     assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
396     assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
397
398     /*
399      * gapped to gapped enclosing ungapped
400      */
401     assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
402     assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
403     assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
404     assertEquals(new Range(8, 13), sq.findPositions(1, 99));
405   }
406
407   /**
408    * Tests for the method that returns a dataset sequence position (start..) for
409    * an aligned column position (base 0).
410    */
411   @Test(groups = { "Functional" })
412   public void testFindPosition()
413   {
414     /* 
415      * call sequenceChanged() after each test to invalidate any cursor,
416      * forcing the 1-arg findPosition to be executed
417      */
418     SequenceI sq = new Sequence("test/8-13", "ABCDEF");
419     assertEquals(8, sq.findPosition(0));
420     // Sequence should now hold a cursor at [8, 0]
421     assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
422             PA.getValue(sq, "cursor").toString());
423     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
424     int token = (int) PA.getValue(sq, "changeCount");
425     assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
426
427     sq.sequenceChanged();
428
429     /*
430      * find F13 at column offset 5, cursor should update to [13, 6]
431      * endColumn is found and saved in cursor
432      */
433     assertEquals(13, sq.findPosition(5));
434     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
435     assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
436     assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
437     assertEquals("test:Pos13:Col6:startCol1:endCol6:tok2",
438             PA.getValue(sq, "cursor").toString());
439
440     // assertEquals(-1, seq.findPosition(6)); // fails
441
442     sq = new Sequence("test/8-11", "AB-C-D--");
443     token = (int) PA.getValue(sq, "changeCount"); // 1 for setStart
444     assertEquals(8, sq.findPosition(0));
445     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
446     assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
447     assertEquals("test:Pos8:Col1:startCol1:endCol0:tok1",
448             PA.getValue(sq, "cursor").toString());
449
450     sq.sequenceChanged();
451     assertEquals(9, sq.findPosition(1));
452     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
453     assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
454     assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
455             PA.getValue(sq, "cursor").toString());
456
457     sq.sequenceChanged();
458     // gap position 'finds' residue to the right (not the left as per javadoc)
459     // cursor is set to the last residue position found [B 2]
460     assertEquals(10, sq.findPosition(2));
461     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
462     assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
463     assertEquals("test:Pos9:Col2:startCol1:endCol0:tok3",
464             PA.getValue(sq, "cursor").toString());
465
466     sq.sequenceChanged();
467     assertEquals(10, sq.findPosition(3));
468     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
469     assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
470     assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
471             PA.getValue(sq, "cursor").toString());
472
473     sq.sequenceChanged();
474     // column[4] is the gap after C - returns D11
475     // cursor is set to [C 4]
476     assertEquals(11, sq.findPosition(4));
477     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
478     assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
479     assertEquals("test:Pos10:Col4:startCol1:endCol0:tok5",
480             PA.getValue(sq, "cursor").toString());
481
482     sq.sequenceChanged();
483     assertEquals(11, sq.findPosition(5)); // D
484     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
485     assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
486     // lastCol has been found and saved in the cursor
487     assertEquals("test:Pos11:Col6:startCol1:endCol6:tok6",
488             PA.getValue(sq, "cursor").toString());
489
490     sq.sequenceChanged();
491     // returns 1 more than sequence length if off the end ?!?
492     assertEquals(12, sq.findPosition(6));
493
494     sq.sequenceChanged();
495     assertEquals(12, sq.findPosition(7));
496
497     /*
498      * first findPosition should also set firstResCol in cursor
499      */
500     sq = new Sequence("test/8-13", "--AB-C-DEF--");
501     assertEquals(8, sq.findPosition(0));
502     assertNull(PA.getValue(sq, "cursor"));
503     assertEquals(1, PA.getValue(sq, "changeCount"));
504
505     sq.sequenceChanged();
506     assertEquals(8, sq.findPosition(1));
507     assertNull(PA.getValue(sq, "cursor"));
508
509     sq.sequenceChanged();
510     assertEquals(8, sq.findPosition(2));
511     assertEquals("test:Pos8:Col3:startCol3:endCol0:tok3",
512             PA.getValue(sq, "cursor").toString());
513
514     sq.sequenceChanged();
515     assertEquals(9, sq.findPosition(3));
516     assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
517             PA.getValue(sq, "cursor").toString());
518
519     sq.sequenceChanged();
520     // column[4] is a gap, returns next residue pos (C10)
521     // cursor is set to last residue found [B]
522     assertEquals(10, sq.findPosition(4));
523     assertEquals("test:Pos9:Col4:startCol3:endCol0:tok5",
524             PA.getValue(sq, "cursor").toString());
525
526     sq.sequenceChanged();
527     assertEquals(10, sq.findPosition(5));
528     assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
529             PA.getValue(sq, "cursor").toString());
530
531     sq.sequenceChanged();
532     // column[6] is a gap, returns next residue pos (D11)
533     // cursor is set to last residue found [C]
534     assertEquals(11, sq.findPosition(6));
535     assertEquals("test:Pos10:Col6:startCol3:endCol0:tok7",
536             PA.getValue(sq, "cursor").toString());
537
538     sq.sequenceChanged();
539     assertEquals(11, sq.findPosition(7));
540     assertEquals("test:Pos11:Col8:startCol3:endCol0:tok8",
541             PA.getValue(sq, "cursor").toString());
542
543     sq.sequenceChanged();
544     assertEquals(12, sq.findPosition(8));
545     assertEquals("test:Pos12:Col9:startCol3:endCol0:tok9",
546             PA.getValue(sq, "cursor").toString());
547
548     /*
549      * when the last residue column is found, it is set in the cursor
550      */
551     sq.sequenceChanged();
552     assertEquals(13, sq.findPosition(9));
553     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
554             PA.getValue(sq, "cursor").toString());
555
556     sq.sequenceChanged();
557     assertEquals(14, sq.findPosition(10));
558     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
559             PA.getValue(sq, "cursor").toString());
560
561     /*
562      * findPosition for column beyond sequence length
563      * returns 1 more than last residue position
564      */
565     sq.sequenceChanged();
566     assertEquals(14, sq.findPosition(11));
567     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
568             PA.getValue(sq, "cursor").toString());
569
570     sq.sequenceChanged();
571     assertEquals(14, sq.findPosition(99));
572     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok13",
573             PA.getValue(sq, "cursor").toString());
574
575     /*
576      * gapped sequence ending in non-gap
577      */
578     sq = new Sequence("test/8-13", "--AB-C-DEF");
579     assertEquals(13, sq.findPosition(9));
580     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
581             PA.getValue(sq, "cursor").toString());
582     sq.sequenceChanged();
583     assertEquals(12, sq.findPosition(8)); // E12
584     // sequenceChanged() invalidates cursor.lastResidueColumn
585     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
586     assertEquals("test:Pos12:Col9:startCol3:endCol0:tok2",
587             cursor.toString());
588     // findPosition with cursor accepts base 1 column values
589     assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
590     assertEquals(13, sq.findPosition(9)); // F13
591     // lastResidueColumn has now been found and saved in cursor
592     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok2",
593             PA.getValue(sq, "cursor").toString());
594   }
595
596   @Test(groups = { "Functional" })
597   public void testDeleteChars()
598   {
599     /*
600      * internal delete
601      */
602     SequenceI sq = new Sequence("test", "ABCDEF");
603     assertNull(PA.getValue(sq, "datasetSequence"));
604     assertEquals(1, sq.getStart());
605     assertEquals(6, sq.getEnd());
606     sq.deleteChars(2, 3);
607     assertEquals("ABDEF", sq.getSequenceAsString());
608     assertEquals(1, sq.getStart());
609     assertEquals(5, sq.getEnd());
610     assertNull(PA.getValue(sq, "datasetSequence"));
611
612     /*
613      * delete at start
614      */
615     sq = new Sequence("test", "ABCDEF");
616     sq.deleteChars(0, 2);
617     assertEquals("CDEF", sq.getSequenceAsString());
618     assertEquals(3, sq.getStart());
619     assertEquals(6, sq.getEnd());
620     assertNull(PA.getValue(sq, "datasetSequence"));
621
622     sq = new Sequence("test", "ABCDE");
623     sq.deleteChars(0, 3);
624     assertEquals("DE", sq.getSequenceAsString());
625     assertEquals(4, sq.getStart());
626     assertEquals(5, sq.getEnd());
627     assertNull(PA.getValue(sq, "datasetSequence"));
628
629     /*
630      * delete at end
631      */
632     sq = new Sequence("test", "ABCDEF");
633     sq.deleteChars(4, 6);
634     assertEquals("ABCD", sq.getSequenceAsString());
635     assertEquals(1, sq.getStart());
636     assertEquals(4, sq.getEnd());
637     assertNull(PA.getValue(sq, "datasetSequence"));
638
639     /*
640      * delete more positions than there are
641      */
642     sq = new Sequence("test/8-11", "ABCD");
643     sq.deleteChars(0, 99);
644     assertEquals("", sq.getSequenceAsString());
645     assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
646     assertEquals(11, sq.getEnd());
647
648     sq = new Sequence("test/8-11", "----");
649     sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
650     assertEquals("", sq.getSequenceAsString());
651     assertEquals(8, sq.getStart());
652     assertEquals(11, sq.getEnd());
653   }
654
655   @Test(groups = { "Functional" })
656   public void testDeleteChars_withDbRefsAndFeatures()
657   {
658     /*
659      * internal delete - new dataset sequence created
660      * gets a copy of any dbrefs
661      */
662     SequenceI sq = new Sequence("test", "ABCDEF");
663     sq.createDatasetSequence();
664     DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
665     sq.addDBRef(dbr1);
666     Object ds = PA.getValue(sq, "datasetSequence");
667     assertNotNull(ds);
668     assertEquals(1, sq.getStart());
669     assertEquals(6, sq.getEnd());
670     sq.deleteChars(2, 3);
671     assertEquals("ABDEF", sq.getSequenceAsString());
672     assertEquals(1, sq.getStart());
673     assertEquals(5, sq.getEnd());
674     Object newDs = PA.getValue(sq, "datasetSequence");
675     assertNotNull(newDs);
676     assertNotSame(ds, newDs);
677     assertNotNull(sq.getDBRefs());
678     assertEquals(1, sq.getDBRefs().size());
679     assertNotSame(dbr1, sq.getDBRefs().get(0));
680     assertEquals(dbr1, sq.getDBRefs().get(0));
681
682     /*
683      * internal delete with sequence features
684      * (failure case for JAL-2541)
685      */
686     sq = new Sequence("test", "ABCDEF");
687     sq.createDatasetSequence();
688     SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
689             "CathGroup");
690     sq.addSequenceFeature(sf1);
691     ds = PA.getValue(sq, "datasetSequence");
692     assertNotNull(ds);
693     assertEquals(1, sq.getStart());
694     assertEquals(6, sq.getEnd());
695     sq.deleteChars(2, 4);
696     assertEquals("ABEF", sq.getSequenceAsString());
697     assertEquals(1, sq.getStart());
698     assertEquals(4, sq.getEnd());
699     newDs = PA.getValue(sq, "datasetSequence");
700     assertNotNull(newDs);
701     assertNotSame(ds, newDs);
702     List<SequenceFeature> sfs = sq.getSequenceFeatures();
703     assertEquals(1, sfs.size());
704     assertNotSame(sf1, sfs.get(0));
705     assertEquals(sf1, sfs.get(0));
706
707     /*
708      * delete at start - no new dataset sequence created
709      * any sequence features remain as before
710      */
711     sq = new Sequence("test", "ABCDEF");
712     sq.createDatasetSequence();
713     ds = PA.getValue(sq, "datasetSequence");
714     sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
715     sq.addSequenceFeature(sf1);
716     sq.deleteChars(0, 2);
717     assertEquals("CDEF", sq.getSequenceAsString());
718     assertEquals(3, sq.getStart());
719     assertEquals(6, sq.getEnd());
720     assertSame(ds, PA.getValue(sq, "datasetSequence"));
721     sfs = sq.getSequenceFeatures();
722     assertNotNull(sfs);
723     assertEquals(1, sfs.size());
724     assertSame(sf1, sfs.get(0));
725
726     /*
727      * delete at end - no new dataset sequence created
728      * any dbrefs remain as before
729      */
730     sq = new Sequence("test", "ABCDEF");
731     sq.createDatasetSequence();
732     ds = PA.getValue(sq, "datasetSequence");
733     dbr1 = new DBRefEntry("Uniprot", "0", "a123");
734     sq.addDBRef(dbr1);
735     sq.deleteChars(4, 6);
736     assertEquals("ABCD", sq.getSequenceAsString());
737     assertEquals(1, sq.getStart());
738     assertEquals(4, sq.getEnd());
739     assertSame(ds, PA.getValue(sq, "datasetSequence"));
740     assertNotNull(sq.getDBRefs());
741     assertEquals(1, sq.getDBRefs().size());
742     assertSame(dbr1, sq.getDBRefs().get(0));
743   }
744
745   @Test(groups = { "Functional" })
746   public void testInsertCharAt()
747   {
748     // non-static methods:
749     SequenceI sq = new Sequence("test", "ABCDEF");
750     sq.insertCharAt(0, 'z');
751     assertEquals("zABCDEF", sq.getSequenceAsString());
752     sq.insertCharAt(2, 2, 'x');
753     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
754
755     // for static method see StringUtilsTest
756   }
757
758   /**
759    * Test the method that returns an array of aligned sequence positions where
760    * the array index is the data sequence position (both base 0).
761    */
762   @Test(groups = { "Functional" })
763   public void testGapMap()
764   {
765     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
766     sq.createDatasetSequence();
767     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
768   }
769
770   /**
771    * Test the method that gets sequence features, either from the sequence or
772    * its dataset.
773    */
774   @Test(groups = { "Functional" })
775   public void testGetSequenceFeatures()
776   {
777     SequenceI sq = new Sequence("test", "GATCAT");
778     sq.createDatasetSequence();
779
780     assertTrue(sq.getSequenceFeatures().isEmpty());
781
782     /*
783      * SequenceFeature on sequence
784      */
785     SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f,
786             null);
787     sq.addSequenceFeature(sf);
788     List<SequenceFeature> sfs = sq.getSequenceFeatures();
789     assertEquals(1, sfs.size());
790     assertSame(sf, sfs.get(0));
791
792     /*
793      * SequenceFeature on sequence and dataset sequence; returns that on
794      * sequence
795      * 
796      * Note JAL-2046: spurious: we have no use case for this at the moment.
797      * This test also buggy - as sf2.equals(sf), no new feature is added
798      */
799     SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
800             null);
801     sq.getDatasetSequence().addSequenceFeature(sf2);
802     sfs = sq.getSequenceFeatures();
803     assertEquals(1, sfs.size());
804     assertSame(sf, sfs.get(0));
805
806     /*
807      * SequenceFeature on dataset sequence only
808      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
809      */
810     sq.setSequenceFeatures(null);
811     assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
812
813     /*
814      * Corrupt case - no SequenceFeature, dataset's dataset is the original
815      * sequence. Test shows no infinite loop results.
816      */
817     sq.getDatasetSequence().setSequenceFeatures(null);
818     /**
819      * is there a usecase for this ? setDatasetSequence should throw an error if
820      * this actually occurs.
821      */
822     try
823     {
824       sq.getDatasetSequence().setDatasetSequence(sq); // loop!
825       Assert.fail(
826               "Expected Error to be raised when calling setDatasetSequence with self reference");
827     } catch (IllegalArgumentException e)
828     {
829       // TODO Jalview error/exception class for raising implementation errors
830       assertTrue(e.getMessage().toLowerCase(Locale.ROOT)
831               .contains("implementation error"));
832     }
833     assertTrue(sq.getSequenceFeatures().isEmpty());
834   }
835
836   /**
837    * Test the method that returns an array, indexed by sequence position, whose
838    * entries are the residue positions at the sequence position (or to the right
839    * if a gap)
840    */
841   @Test(groups = { "Functional" })
842   public void testFindPositionMap()
843   {
844     /*
845      * Note: Javadoc for findPosition says it returns the residue position to
846      * the left of a gapped position; in fact it returns the position to the
847      * right. Also it returns a non-existent residue position for a gap beyond
848      * the sequence.
849      */
850     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
851     int[] map = sq.findPositionMap();
852     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
853             Arrays.toString(map));
854   }
855
856   /**
857    * Test for getSubsequence
858    */
859   @Test(groups = { "Functional" })
860   public void testGetSubsequence()
861   {
862     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
863     sq.createDatasetSequence();
864
865     // positions are base 0, end position is exclusive
866     SequenceI subseq = sq.getSubSequence(2, 4);
867
868     assertEquals("CD", subseq.getSequenceAsString());
869     // start/end are base 1 positions
870     assertEquals(3, subseq.getStart());
871     assertEquals(4, subseq.getEnd());
872     // subsequence shares the full dataset sequence
873     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
874   }
875
876   /**
877    * test createDatasetSequence behaves to doc
878    */
879   @Test(groups = { "Functional" })
880   public void testCreateDatasetSequence()
881   {
882     SequenceI sq = new Sequence("my", "ASDASD");
883     sq.addSequenceFeature(
884             new SequenceFeature("type", "desc", 1, 10, 1f, "group"));
885     sq.addDBRef(new DBRefEntry("source", "version", "accession"));
886     assertNull(sq.getDatasetSequence());
887     assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
888     assertNotNull(PA.getValue(sq, "dbrefs"));
889
890     SequenceI rds = sq.createDatasetSequence();
891     assertNotNull(rds);
892     assertNull(rds.getDatasetSequence());
893     assertSame(sq.getDatasetSequence(), rds);
894
895     // sequence features and dbrefs transferred to dataset sequence
896     assertNull(PA.getValue(sq, "sequenceFeatureStore"));
897     assertNull(PA.getValue(sq, "dbrefs"));
898     assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
899     assertNotNull(PA.getValue(rds, "dbrefs"));
900   }
901
902   /**
903    * Test for deriveSequence applied to a sequence with a dataset
904    */
905   @Test(groups = { "Functional" })
906   public void testDeriveSequence_existingDataset()
907   {
908     Sequence sq = new Sequence("Seq1", "CD");
909     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
910     sq.getDatasetSequence().addSequenceFeature(
911             new SequenceFeature("", "", 1, 2, 0f, null));
912     sq.setStart(3);
913     sq.setEnd(4);
914
915     sq.setDescription("Test sequence description..");
916     sq.setVamsasId("TestVamsasId");
917     sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
918
919     sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
920     sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
921     sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
922     sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
923
924     sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
925     sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
926     sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
927     sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
928
929     // these are the same as ones already added
930     DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
931     DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
932
933     List<DBRefEntry> primRefs = Arrays
934             .asList(new DBRefEntry[]
935             { pdb1pdb, pdb2pdb });
936
937     sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
938     sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
939     sq.getDatasetSequence()
940             .addDBRef(new DBRefEntry("PDB", "version3", "3PDB")); // should do
941                                                                   // nothing
942     sq.getDatasetSequence()
943             .addDBRef(new DBRefEntry("PDB", "version4", "4PDB")); // should do
944                                                                   // nothing
945
946     PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
947     PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
948     PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
949             "filePath/test2");
950     PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
951             "filePath/test2");
952     sq.getDatasetSequence().addPDBId(pdbe1a);
953     sq.getDatasetSequence().addPDBId(pdbe1b);
954     sq.getDatasetSequence().addPDBId(pdbe2a);
955     sq.getDatasetSequence().addPDBId(pdbe2b);
956
957     /*
958      * test we added pdb entries to the dataset sequence
959      */
960     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(),
961             Arrays.asList(new PDBEntry[]
962             { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
963             "PDB Entries were not found on dataset sequence.");
964
965     /*
966      * we should recover a pdb entry that is on the dataset sequence via PDBEntry
967      */
968     Assert.assertEquals(pdbe1a, sq.getDatasetSequence().getPDBEntry("1PDB"),
969             "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
970     ArrayList<Annotation> annotsList = new ArrayList<>();
971     System.out.println(">>>>>> " + sq.getSequenceAsString().length());
972     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
973     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
974     Annotation[] annots = annotsList.toArray(new Annotation[0]);
975     sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
976             "Test annot description", annots));
977     sq.getDatasetSequence().addAlignmentAnnotation(new AlignmentAnnotation(
978             "Test annot", "Test annot description", annots));
979     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
980     Assert.assertEquals(sq.getDBRefs().size(), 5); // DBRefs are on dataset
981                                                    // sequence
982     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
983     Assert.assertNotNull(sq.getAnnotation());
984     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
985     Assert.assertEquals(sq.getDatasetSequence().getDBRefs().size(), 5); // same
986                                                                         // as
987                                                                         // sq.getDBRefs()
988     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
989             4);
990     Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
991
992     Sequence derived = (Sequence) sq.deriveSequence();
993
994     Assert.assertEquals(derived.getDescription(),
995             "Test sequence description..");
996     Assert.assertEquals(derived.getDBRefs().size(), 5); // come from dataset
997     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
998     Assert.assertNotNull(derived.getAnnotation());
999     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
1000     Assert.assertEquals(derived.getDatasetSequence().getDBRefs().size(), 5);
1001     Assert.assertEquals(
1002             derived.getDatasetSequence().getAllPDBEntries().size(), 4);
1003     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
1004
1005     assertEquals("CD", derived.getSequenceAsString());
1006     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
1007
1008     // derived sequence should access dataset sequence features
1009     assertNotNull(sq.getSequenceFeatures());
1010     assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
1011
1012     /*
1013      *  verify we have primary db refs *just* for PDB IDs with associated
1014      *  PDBEntry objects
1015      */
1016
1017     assertEquals(primRefs, sq.getPrimaryDBRefs());
1018     assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
1019
1020     assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
1021
1022   }
1023
1024   /**
1025    * Test for deriveSequence applied to an ungapped sequence with no dataset
1026    */
1027   @Test(groups = { "Functional" })
1028   public void testDeriveSequence_noDatasetUngapped()
1029   {
1030     SequenceI sq = new Sequence("Seq1", "ABCDEF");
1031     assertEquals(1, sq.getStart());
1032     assertEquals(6, sq.getEnd());
1033     SequenceI derived = sq.deriveSequence();
1034     assertEquals("ABCDEF", derived.getSequenceAsString());
1035     assertEquals("ABCDEF",
1036             derived.getDatasetSequence().getSequenceAsString());
1037   }
1038
1039   /**
1040    * Test for deriveSequence applied to a gapped sequence with no dataset
1041    */
1042   @Test(groups = { "Functional" })
1043   public void testDeriveSequence_noDatasetGapped()
1044   {
1045     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
1046     assertEquals(1, sq.getStart());
1047     assertEquals(6, sq.getEnd());
1048     assertNull(sq.getDatasetSequence());
1049     SequenceI derived = sq.deriveSequence();
1050     assertEquals("AB-C.D EF", derived.getSequenceAsString());
1051     assertEquals("ABCDEF",
1052             derived.getDatasetSequence().getSequenceAsString());
1053   }
1054
1055   @Test(groups = { "Functional" })
1056   public void testCopyConstructor_noDataset()
1057   {
1058     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1059     seq1.setDescription("description");
1060     seq1.addAlignmentAnnotation(
1061             new AlignmentAnnotation("label", "desc", 1.3d));
1062     seq1.addSequenceFeature(
1063             new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1064     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1065     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1066
1067     SequenceI copy = new Sequence(seq1);
1068
1069     assertNull(copy.getDatasetSequence());
1070
1071     verifyCopiedSequence(seq1, copy);
1072
1073     // copy has a copy of the DBRefEntry
1074     // this is murky - DBrefs are only copied for dataset sequences
1075     // where the test for 'dataset sequence' is 'dataset is null'
1076     // but that doesn't distinguish it from an aligned sequence
1077     // which has not yet generated a dataset sequence
1078     // NB getDBRef looks inside dataset sequence if not null
1079     List<DBRefEntry> dbrefs = copy.getDBRefs();
1080     assertEquals(1, dbrefs.size());
1081     assertFalse(dbrefs.get(0) == seq1.getDBRefs().get(0));
1082     assertTrue(dbrefs.get(0).equals(seq1.getDBRefs().get(0)));
1083   }
1084
1085   @Test(groups = { "Functional" })
1086   public void testCopyConstructor_withDataset()
1087   {
1088     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
1089     seq1.createDatasetSequence();
1090     seq1.setDescription("description");
1091     seq1.addAlignmentAnnotation(
1092             new AlignmentAnnotation("label", "desc", 1.3d));
1093     // JAL-2046 - what is the contract for using a derived sequence's
1094     // addSequenceFeature ?
1095     seq1.addSequenceFeature(
1096             new SequenceFeature("type", "desc", 22, 33, 12.4f, "group"));
1097     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1098     // here we add DBRef to the dataset sequence:
1099     seq1.getDatasetSequence()
1100             .addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
1101
1102     SequenceI copy = new Sequence(seq1);
1103
1104     assertNotNull(copy.getDatasetSequence());
1105     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1106
1107     verifyCopiedSequence(seq1, copy);
1108
1109     // getDBRef looks inside dataset sequence and this is shared,
1110     // so holds the same dbref objects
1111     List<DBRefEntry> dbrefs = copy.getDBRefs();
1112     assertEquals(1, dbrefs.size());
1113     assertSame(dbrefs.get(0), seq1.getDBRefs().get(0));
1114   }
1115
1116   /**
1117    * Helper to make assertions about a copied sequence
1118    * 
1119    * @param seq1
1120    * @param copy
1121    */
1122   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1123   {
1124     // verify basic properties:
1125     assertEquals(copy.getName(), seq1.getName());
1126     assertEquals(copy.getDescription(), seq1.getDescription());
1127     assertEquals(copy.getStart(), seq1.getStart());
1128     assertEquals(copy.getEnd(), seq1.getEnd());
1129     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1130
1131     // copy has a copy of the annotation:
1132     AlignmentAnnotation[] anns = copy.getAnnotation();
1133     assertEquals(1, anns.length);
1134     assertFalse(anns[0] == seq1.getAnnotation()[0]);
1135     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1136     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1137     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1138
1139     // copy has a copy of the sequence feature:
1140     List<SequenceFeature> sfs = copy.getSequenceFeatures();
1141     assertEquals(1, sfs.size());
1142     if (seq1.getDatasetSequence() != null
1143             && copy.getDatasetSequence() == seq1.getDatasetSequence())
1144     {
1145       assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1146     }
1147     else
1148     {
1149       assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1150     }
1151     assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1152
1153     // copy has a copy of the PDB entry
1154     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1155     assertEquals(1, pdbs.size());
1156     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1157     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1158   }
1159
1160   @Test(groups = "Functional")
1161   public void testGetCharAt()
1162   {
1163     SequenceI sq = new Sequence("", "abcde");
1164     assertEquals('a', sq.getCharAt(0));
1165     assertEquals('e', sq.getCharAt(4));
1166     assertEquals(' ', sq.getCharAt(5));
1167     assertEquals(' ', sq.getCharAt(-1));
1168   }
1169
1170   @Test(groups = { "Functional" })
1171   public void testAddSequenceFeatures()
1172   {
1173     SequenceI sq = new Sequence("", "abcde");
1174     // type may not be null
1175     assertFalse(sq.addSequenceFeature(
1176             new SequenceFeature(null, "desc", 4, 8, 0f, null)));
1177     assertTrue(sq.addSequenceFeature(
1178             new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1179     // can't add a duplicate feature
1180     assertFalse(sq.addSequenceFeature(
1181             new SequenceFeature("Cath", "desc", 4, 8, 0f, null)));
1182     // can add a different feature
1183     assertTrue(sq.addSequenceFeature(
1184             new SequenceFeature("Scop", "desc", 4, 8, 0f, null))); // different
1185                                                                    // type
1186     assertTrue(sq.addSequenceFeature(
1187             new SequenceFeature("Cath", "description", 4, 8, 0f, null)));// different
1188                                                                          // description
1189     assertTrue(sq.addSequenceFeature(
1190             new SequenceFeature("Cath", "desc", 3, 8, 0f, null))); // different
1191                                                                    // start
1192                                                                    // position
1193     assertTrue(sq.addSequenceFeature(
1194             new SequenceFeature("Cath", "desc", 4, 9, 0f, null))); // different
1195                                                                    // end
1196                                                                    // position
1197     assertTrue(sq.addSequenceFeature(
1198             new SequenceFeature("Cath", "desc", 4, 8, 1f, null))); // different
1199                                                                    // score
1200     assertTrue(sq.addSequenceFeature(
1201             new SequenceFeature("Cath", "desc", 4, 8, Float.NaN, null))); // score
1202                                                                           // NaN
1203     assertTrue(sq.addSequenceFeature(
1204             new SequenceFeature("Cath", "desc", 4, 8, 0f, "Metal"))); // different
1205                                                                       // group
1206     assertEquals(8, sq.getFeatures().getAllFeatures().size());
1207   }
1208
1209   /**
1210    * Tests for adding (or updating) dbrefs
1211    * 
1212    * @see DBRefEntry#updateFrom(DBRefEntry)
1213    */
1214   @Test(groups = { "Functional" })
1215   public void testAddDBRef()
1216   {
1217     SequenceI sq = new Sequence("", "abcde");
1218     assertNull(sq.getDBRefs());
1219     DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1220     sq.addDBRef(dbref);
1221     assertEquals(1, sq.getDBRefs().size());
1222     assertSame(dbref, sq.getDBRefs().get(0));
1223
1224     /*
1225      * change of version - new entry
1226      */
1227     DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1228     sq.addDBRef(dbref2);
1229     assertEquals(2, sq.getDBRefs().size());
1230     assertSame(dbref, sq.getDBRefs().get(0));
1231     assertSame(dbref2, sq.getDBRefs().get(1));
1232
1233     /*
1234      * matches existing entry - not added
1235      */
1236     sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1237     assertEquals(2, sq.getDBRefs().size());
1238
1239     /*
1240      * different source = new entry
1241      */
1242     DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1243     sq.addDBRef(dbref3);
1244     assertEquals(3, sq.getDBRefs().size());
1245     assertSame(dbref3, sq.getDBRefs().get(2));
1246
1247     /*
1248      * different ref = new entry
1249      */
1250     DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1251     sq.addDBRef(dbref4);
1252     assertEquals(4, sq.getDBRefs().size());
1253     assertSame(dbref4, sq.getDBRefs().get(3));
1254
1255     /*
1256      * matching ref with a mapping - map updated
1257      */
1258     DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1259     Mapping map = new Mapping(
1260             new MapList(new int[]
1261             { 1, 3 }, new int[] { 1, 1 }, 3, 1));
1262     dbref5.setMap(map);
1263     sq.addDBRef(dbref5);
1264     assertEquals(4, sq.getDBRefs().size());
1265     assertSame(dbref4, sq.getDBRefs().get(3));
1266     assertSame(map, dbref4.getMap());
1267
1268     /*
1269      * 'real' version replaces "0" version
1270      */
1271     dbref2.setVersion("0");
1272     DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1273             dbref2.getAccessionId());
1274     sq.addDBRef(dbref6);
1275     assertEquals(4, sq.getDBRefs().size());
1276     assertSame(dbref2, sq.getDBRefs().get(1));
1277     assertEquals("3", dbref2.getVersion());
1278
1279     /*
1280      * 'real' version replaces "source:0" version
1281      */
1282     dbref3.setVersion("Uniprot:0");
1283     DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1284             dbref3.getAccessionId());
1285     sq.addDBRef(dbref7);
1286     assertEquals(4, sq.getDBRefs().size());
1287     assertSame(dbref3, sq.getDBRefs().get(2));
1288     assertEquals("3", dbref2.getVersion());
1289   }
1290
1291   @Test(groups = { "Functional" })
1292   public void testGetPrimaryDBRefs_peptide()
1293   {
1294     SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1295
1296     // no dbrefs
1297     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1298     assertTrue(primaryDBRefs.isEmpty());
1299
1300     // empty dbrefs
1301     sq.setDBRefs(null);
1302     primaryDBRefs = sq.getPrimaryDBRefs();
1303     assertTrue(primaryDBRefs.isEmpty());
1304
1305     // primary - uniprot
1306     DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1307     sq.addDBRef(upentry1);
1308
1309     // primary - uniprot with congruent map
1310     DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1311     upentry2.setMap(
1312             new Mapping(null, new MapList(new int[]
1313             { 10, 22 }, new int[] { 10, 22 }, 1, 1)));
1314     sq.addDBRef(upentry2);
1315
1316     // primary - uniprot with map of enclosing sequence
1317     DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1318     upentry3.setMap(
1319             new Mapping(null, new MapList(new int[]
1320             { 8, 24 }, new int[] { 8, 24 }, 1, 1)));
1321     sq.addDBRef(upentry3);
1322
1323     // not primary - uniprot with map of sub-sequence (5')
1324     DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1325     upentry4.setMap(
1326             new Mapping(null, new MapList(new int[]
1327             { 10, 18 }, new int[] { 10, 18 }, 1, 1)));
1328     sq.addDBRef(upentry4);
1329
1330     // not primary - uniprot with map that overlaps 3'
1331     DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1332     upentry5.setMap(
1333             new Mapping(null, new MapList(new int[]
1334             { 12, 22 }, new int[] { 12, 22 }, 1, 1)));
1335     sq.addDBRef(upentry5);
1336
1337     // not primary - uniprot with map to different coordinates frame
1338     DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1339     upentry6.setMap(
1340             new Mapping(null, new MapList(new int[]
1341             { 12, 18 }, new int[] { 112, 118 }, 1, 1)));
1342     sq.addDBRef(upentry6);
1343
1344     // not primary - dbref to 'non-core' database
1345     DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1346     sq.addDBRef(upentry7);
1347
1348     // primary - type is PDB
1349     DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1350     sq.addDBRef(pdbentry);
1351
1352     // not primary - PDBEntry has no file
1353     sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1354
1355     // not primary - no PDBEntry
1356     sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1357
1358     // add corroborating PDB entry for primary DBref -
1359     // needs to have a file as well as matching ID
1360     // note PDB ID is not treated as case sensitive
1361     sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB,
1362             new File("/blah").toString()));
1363
1364     // not valid DBRef - no file..
1365     sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1366
1367     primaryDBRefs = sq.getPrimaryDBRefs();
1368     assertEquals(4, primaryDBRefs.size());
1369     assertTrue("Couldn't find simple primary reference (UNIPROT)",
1370             primaryDBRefs.contains(upentry1));
1371     assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1372             primaryDBRefs.contains(upentry2));
1373     assertTrue("Couldn't find mapped context reference (UNIPROT)",
1374             primaryDBRefs.contains(upentry3));
1375     assertTrue("Couldn't find expected PDB primary reference",
1376             primaryDBRefs.contains(pdbentry));
1377   }
1378
1379   @Test(groups = { "Functional" })
1380   public void testGetPrimaryDBRefs_nucleotide()
1381   {
1382     SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10,
1383             34);
1384
1385     // primary - Ensembl
1386     DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1387     sq.addDBRef(dbr1);
1388
1389     // not primary - Ensembl 'transcript' mapping of sub-sequence
1390     DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1391     dbr2.setMap(
1392             new Mapping(null, new MapList(new int[]
1393             { 15, 25 }, new int[] { 1, 11 }, 1, 1)));
1394     sq.addDBRef(dbr2);
1395
1396     // primary - EMBL with congruent map
1397     DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1398     dbr3.setMap(
1399             new Mapping(null, new MapList(new int[]
1400             { 10, 34 }, new int[] { 10, 34 }, 1, 1)));
1401     sq.addDBRef(dbr3);
1402
1403     // not primary - to non-core database
1404     DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1405     sq.addDBRef(dbr4);
1406
1407     // not primary - to protein
1408     DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1409     sq.addDBRef(dbr5);
1410
1411     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1412     assertEquals(2, primaryDBRefs.size());
1413     assertTrue(primaryDBRefs.contains(dbr1));
1414     assertTrue(primaryDBRefs.contains(dbr3));
1415   }
1416
1417   /**
1418    * Test the method that updates the list of PDBEntry from any new DBRefEntry
1419    * for PDB
1420    */
1421   @Test(groups = { "Functional" })
1422   public void testUpdatePDBIds()
1423   {
1424     PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1425     seq.addPDBId(pdbe1);
1426     seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1427     seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1428     seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1429     seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1430     // 7 is not a valid chain code:
1431     seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1432
1433     seq.updatePDBIds();
1434     List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1435     assertEquals(4, pdbIds.size());
1436     assertSame(pdbe1, pdbIds.get(0));
1437     // chain code got added to 3A6S:
1438     assertEquals("B", pdbe1.getChainCode());
1439     assertEquals("1A70", pdbIds.get(1).getId());
1440     // 4BQGA is parsed into id + chain
1441     assertEquals("4BQG", pdbIds.get(2).getId());
1442     assertEquals("a", pdbIds.get(2).getChainCode());
1443     assertEquals("2GIS7", pdbIds.get(3).getId());
1444     assertNull(pdbIds.get(3).getChainCode());
1445   }
1446
1447   /**
1448    * Test the method that either adds a pdbid or updates an existing one
1449    */
1450   @Test(groups = { "Functional" })
1451   public void testAddPDBId()
1452   {
1453     PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1454     seq.addPDBId(pdbe);
1455     assertEquals(1, seq.getAllPDBEntries().size());
1456     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1457     assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1458
1459     // add the same entry
1460     seq.addPDBId(pdbe);
1461     assertEquals(1, seq.getAllPDBEntries().size());
1462     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1463
1464     // add an identical entry
1465     seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1466     assertEquals(1, seq.getAllPDBEntries().size());
1467     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1468
1469     // add a different entry
1470     PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1471     seq.addPDBId(pdbe2);
1472     assertEquals(2, seq.getAllPDBEntries().size());
1473     assertSame(pdbe, seq.getAllPDBEntries().get(0));
1474     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1475
1476     // update pdbe with chain code, file, type
1477     PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1478     seq.addPDBId(pdbe3);
1479     assertEquals(2, seq.getAllPDBEntries().size());
1480     assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1481     assertEquals("3A6S", pdbe.getId()); // unchanged
1482     assertEquals("A", pdbe.getChainCode()); // updated
1483     assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1484     assertEquals("filepath", pdbe.getFile()); // updated
1485     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1486
1487     // add with a different file path
1488     PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1489     seq.addPDBId(pdbe4);
1490     assertEquals(3, seq.getAllPDBEntries().size());
1491     assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1492
1493     // add with a different chain code
1494     PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1495     seq.addPDBId(pdbe5);
1496     assertEquals(4, seq.getAllPDBEntries().size());
1497     assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1498
1499     // add with a fake pdbid
1500     // (models don't have an embedded ID)
1501     String realId = "RealIDQ";
1502     PDBEntry pdbe6 = new PDBEntry(realId, null, Type.PDB, "real/localpath");
1503     PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath", "C", Type.MMCIF,
1504             "real/localpath");
1505     pdbe7.setFakedPDBId(true);
1506     seq.addPDBId(pdbe6);
1507     assertEquals(5, seq.getAllPDBEntries().size());
1508     seq.addPDBId(pdbe7);
1509     assertEquals(5, seq.getAllPDBEntries().size());
1510     assertFalse(pdbe6.fakedPDBId());
1511     assertSame(pdbe6, seq.getAllPDBEntries().get(4));
1512     assertEquals("C", pdbe6.getChainCode());
1513     assertEquals(realId, pdbe6.getId());
1514   }
1515
1516   @Test(
1517     groups =
1518     { "Functional" },
1519     expectedExceptions =
1520     { IllegalArgumentException.class })
1521   public void testSetDatasetSequence_toSelf()
1522   {
1523     seq.setDatasetSequence(seq);
1524   }
1525
1526   @Test(
1527     groups =
1528     { "Functional" },
1529     expectedExceptions =
1530     { IllegalArgumentException.class })
1531   public void testSetDatasetSequence_cascading()
1532   {
1533     SequenceI seq2 = new Sequence("Seq2", "xyz");
1534     seq2.createDatasetSequence();
1535     seq.setDatasetSequence(seq2);
1536   }
1537
1538   @Test(groups = { "Functional" })
1539   public void testFindFeatures()
1540   {
1541     SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1542     sq.createDatasetSequence();
1543
1544     assertTrue(sq.findFeatures(1, 99).isEmpty());
1545
1546     // add non-positional feature
1547     SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1548             null);
1549     sq.addSequenceFeature(sf0);
1550     // add feature on BCD
1551     SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1552             null);
1553     sq.addSequenceFeature(sfBCD);
1554     // add feature on DE
1555     SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1556             null);
1557     sq.addSequenceFeature(sfDE);
1558     // add contact feature at [B, H]
1559     SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
1560             "desc", 9, 15, 2f, null);
1561     sq.addSequenceFeature(sfContactBH);
1562     // add contact feature at [F, G]
1563     SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1564             "desc", 13, 14, 2f, null);
1565     sq.addSequenceFeature(sfContactFG);
1566     // add single position feature at [I]
1567     SequenceFeature sfI = new SequenceFeature("Disulfide Bond", "desc", 16,
1568             16, null);
1569     sq.addSequenceFeature(sfI);
1570
1571     // no features in columns 1-2 (-A)
1572     List<SequenceFeature> found = sq.findFeatures(1, 2);
1573     assertTrue(found.isEmpty());
1574
1575     // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1576     found = sq.findFeatures(1, 6);
1577     assertEquals(2, found.size());
1578     assertTrue(found.contains(sfBCD));
1579     assertTrue(found.contains(sfContactBH));
1580
1581     // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1582     found = sq.findFeatures(5, 6);
1583     assertEquals(1, found.size());
1584     assertTrue(found.contains(sfBCD));
1585
1586     // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1587     found = sq.findFeatures(7, 10);
1588     assertEquals(3, found.size());
1589     assertTrue(found.contains(sfBCD));
1590     assertTrue(found.contains(sfDE));
1591     assertTrue(found.contains(sfContactFG));
1592
1593     // columns 10-11 (--) should find nothing
1594     found = sq.findFeatures(10, 11);
1595     assertEquals(0, found.size());
1596
1597     // columns 14-14 (I) should find variant feature
1598     found = sq.findFeatures(14, 14);
1599     assertEquals(1, found.size());
1600     assertTrue(found.contains(sfI));
1601   }
1602
1603   @Test(groups = { "Functional" })
1604   public void testFindIndex_withCursor()
1605   {
1606     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1607     final int tok = (int) PA.getValue(sq, "changeCount");
1608     assertEquals(1, tok);
1609
1610     // find F given A, check cursor is now at the found position
1611     assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
1612     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1613     assertEquals(13, cursor.residuePosition);
1614     assertEquals(10, cursor.columnPosition);
1615
1616     // find A given F
1617     assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
1618     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1619     assertEquals(8, cursor.residuePosition);
1620     assertEquals(2, cursor.columnPosition);
1621
1622     // find C given C (no cursor update is done for this case)
1623     assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
1624     SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1625     assertSame(cursor2, cursor);
1626
1627     /*
1628      * sequence 'end' beyond end of sequence returns length of sequence 
1629      *  (for compatibility with pre-cursor code)
1630      *  - also verify the cursor is left in a valid state
1631      */
1632     sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
1633     assertEquals(7, sq.findIndex(10)); // establishes a cursor
1634     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1635     assertEquals(10, cursor.residuePosition);
1636     assertEquals(7, cursor.columnPosition);
1637     assertEquals(sq.getLength(), sq.findIndex(65));
1638     cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1639     assertSame(cursor, cursor2); // not updated for this case!
1640
1641     sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
1642     sq.findIndex(10); // establishes a cursor
1643     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1644     assertEquals(sq.getLength(), sq.findIndex(65));
1645     cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1646     assertSame(cursor, cursor2); // not updated for this case!
1647
1648     /*
1649      * residue after sequence 'start' but before first residue should return 
1650      * zero (for compatibility with pre-cursor code)
1651      */
1652     sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
1653     sq.findIndex(10); // establishes a cursor
1654     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1655     assertEquals(0, sq.findIndex(3));
1656     cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1657     assertSame(cursor, cursor2); // not updated for this case!
1658
1659     sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
1660     sq.findIndex(10); // establishes a cursor
1661     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1662     assertEquals(0, sq.findIndex(2));
1663     cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
1664     assertSame(cursor, cursor2); // not updated for this case!
1665   }
1666
1667   @Test(groups = { "Functional" })
1668   public void testFindPosition_withCursor()
1669   {
1670     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1671     final int tok = (int) PA.getValue(sq, "changeCount");
1672     assertEquals(1, tok);
1673
1674     // find F pos given A - lastCol gets set in cursor
1675     assertEquals(13,
1676             sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
1677     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1678             PA.getValue(sq, "cursor").toString());
1679
1680     // find A pos given F - first residue column is saved in cursor
1681     assertEquals(8,
1682             sq.findPosition(2, new SequenceCursor(sq, 13, 10, tok)));
1683     assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
1684             PA.getValue(sq, "cursor").toString());
1685
1686     // find C pos given C (neither startCol nor endCol is set)
1687     assertEquals(10,
1688             sq.findPosition(6, new SequenceCursor(sq, 10, 6, tok)));
1689     assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1690             PA.getValue(sq, "cursor").toString());
1691
1692     // now the grey area - what residue position for a gapped column? JAL-2562
1693
1694     // find 'residue' for column 3 given cursor for D (so working left)
1695     // returns B9; cursor is updated to [B 5]
1696     assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, tok)));
1697     assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
1698             PA.getValue(sq, "cursor").toString());
1699
1700     // find 'residue' for column 8 given cursor for D (so working right)
1701     // returns E12; cursor is updated to [D 7]
1702     assertEquals(12,
1703             sq.findPosition(8, new SequenceCursor(sq, 11, 7, tok)));
1704     assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
1705             PA.getValue(sq, "cursor").toString());
1706
1707     // find 'residue' for column 12 given cursor for B
1708     // returns 1 more than last residue position; cursor is updated to [F 10]
1709     // lastCol position is saved in cursor
1710     assertEquals(14,
1711             sq.findPosition(12, new SequenceCursor(sq, 9, 5, tok)));
1712     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1713             PA.getValue(sq, "cursor").toString());
1714
1715     /*
1716      * findPosition for column beyond length of sequence
1717      * returns 1 more than the last residue position
1718      * cursor is set to last real residue position [F 10]
1719      */
1720     assertEquals(14,
1721             sq.findPosition(99, new SequenceCursor(sq, 8, 2, tok)));
1722     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1723             PA.getValue(sq, "cursor").toString());
1724
1725     /*
1726      * and the case without a trailing gap
1727      */
1728     sq = new Sequence("test/8-13", "-A--BCD-EF");
1729     // first find C from A
1730     assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, tok)));
1731     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1732     assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
1733             cursor.toString());
1734     // now 'find' 99 from C
1735     // cursor is set to [F 10] and saved lastCol
1736     assertEquals(14, sq.findPosition(99, cursor));
1737     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
1738             PA.getValue(sq, "cursor").toString());
1739   }
1740
1741   @Test
1742   public void testIsValidCursor()
1743   {
1744     Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1745     assertFalse(sq.isValidCursor(null));
1746
1747     /*
1748      * cursor is valid if it has valid sequence ref and changeCount token
1749      * and positions within the range of the sequence
1750      */
1751     int changeCount = (int) PA.getValue(sq, "changeCount");
1752     SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1753     assertTrue(sq.isValidCursor(cursor));
1754
1755     /*
1756      * column position outside [0 - length] is rejected
1757      */
1758     cursor = new SequenceCursor(sq, 13, -1, changeCount);
1759     assertFalse(sq.isValidCursor(cursor));
1760     cursor = new SequenceCursor(sq, 13, 10, changeCount);
1761     assertFalse(sq.isValidCursor(cursor));
1762     cursor = new SequenceCursor(sq, 7, 8, changeCount);
1763     assertFalse(sq.isValidCursor(cursor));
1764     cursor = new SequenceCursor(sq, 14, 2, changeCount);
1765     assertFalse(sq.isValidCursor(cursor));
1766
1767     /*
1768      * wrong sequence is rejected
1769      */
1770     cursor = new SequenceCursor(null, 13, 1, changeCount);
1771     assertFalse(sq.isValidCursor(cursor));
1772     cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1773             changeCount);
1774     assertFalse(sq.isValidCursor(cursor));
1775
1776     /*
1777      * wrong token value is rejected
1778      */
1779     cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1780     assertFalse(sq.isValidCursor(cursor));
1781     cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1782     assertFalse(sq.isValidCursor(cursor));
1783   }
1784
1785   @Test(groups = { "Functional" })
1786   public void testFindPosition_withCursorAndEdits()
1787   {
1788     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1789
1790     // find F pos given A
1791     assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1792     int token = (int) PA.getValue(sq, "changeCount"); // 0
1793     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1794     assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1795
1796     /*
1797      * setSequence should invalidate the cursor cached by the sequence
1798      */
1799     sq.setSequence("-A-BCD-EF---"); // one gap removed
1800     assertEquals(8, sq.getStart()); // sanity check
1801     assertEquals(11, sq.findPosition(5)); // D11
1802     // cursor should now be at [D 6]
1803     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1804     assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1805     assertEquals(0, cursor.lastColumnPosition); // not yet found
1806     assertEquals(13, sq.findPosition(8)); // E13
1807     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1808     assertEquals(9, cursor.lastColumnPosition); // found
1809
1810     /*
1811      * deleteChars should invalidate the cached cursor
1812      */
1813     sq.deleteChars(2, 5); // delete -BC
1814     assertEquals("-AD-EF---", sq.getSequenceAsString());
1815     assertEquals(8, sq.getStart()); // sanity check
1816     assertEquals(10, sq.findPosition(4)); // E10
1817     // cursor should now be at [E 5]
1818     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1819     assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1820
1821     /*
1822      * Edit to insert gaps should invalidate the cached cursor
1823      * insert 2 gaps at column[3] to make -AD---EF---
1824      */
1825     SequenceI[] seqs = new SequenceI[] { sq };
1826     AlignmentI al = new Alignment(seqs);
1827     new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1828     assertEquals("-AD---EF---", sq.getSequenceAsString());
1829     assertEquals(10, sq.findPosition(4)); // E10
1830     // cursor should now be at [D 3]
1831     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1832     assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1833
1834     /*
1835      * insertCharAt should invalidate the cached cursor
1836      * insert CC at column[4] to make -AD-CC--EF---
1837      */
1838     sq.insertCharAt(4, 2, 'C');
1839     assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1840     assertEquals(13, sq.findPosition(9)); // F13
1841     // cursor should now be at [F 10]
1842     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1843     assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1844
1845     /*
1846      * changing sequence start should invalidate cursor
1847      */
1848     sq = new Sequence("test/8-13", "-A--BCD-EF--");
1849     assertEquals(8, sq.getStart());
1850     assertEquals(9, sq.findPosition(4)); // B(9)
1851     sq.setStart(7);
1852     assertEquals(8, sq.findPosition(4)); // is now B(8)
1853     sq.setStart(10);
1854     assertEquals(11, sq.findPosition(4)); // is now B(11)
1855   }
1856
1857   @Test(groups = { "Functional" })
1858   public void testGetSequence()
1859   {
1860     String seqstring = "-A--BCD-EF--";
1861     Sequence sq = new Sequence("test/8-13", seqstring);
1862     sq.createDatasetSequence();
1863     assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1864     assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1865             "ABCDEF".toCharArray()));
1866
1867     // verify a copy of the sequence array is returned
1868     char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1869     assertNotSame(theSeq, sq.getSequence());
1870     theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1871     assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1872   }
1873
1874   @Test(groups = { "Functional" })
1875   public void testReplace()
1876   {
1877     String seqstring = "-A--BCD-EF--";
1878     SequenceI sq = new Sequence("test/8-13", seqstring);
1879     // changeCount is incremented for setStart
1880     assertEquals(1, PA.getValue(sq, "changeCount"));
1881
1882     assertEquals(0, sq.replace('A', 'A')); // same char
1883     assertEquals(seqstring, sq.getSequenceAsString());
1884     assertEquals(1, PA.getValue(sq, "changeCount"));
1885
1886     assertEquals(0, sq.replace('X', 'Y')); // not there
1887     assertEquals(seqstring, sq.getSequenceAsString());
1888     assertEquals(1, PA.getValue(sq, "changeCount"));
1889
1890     assertEquals(1, sq.replace('A', 'K'));
1891     assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1892     assertEquals(2, PA.getValue(sq, "changeCount"));
1893
1894     assertEquals(6, sq.replace('-', '.'));
1895     assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1896     assertEquals(3, PA.getValue(sq, "changeCount"));
1897   }
1898
1899   @Test(groups = { "Functional" })
1900   public void testGapBitset()
1901   {
1902     SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
1903     BitSet bs = sq.gapBitset();
1904     BitSet expected = new BitSet();
1905     expected.set(0);
1906     expected.set(4, 7);
1907     expected.set(9);
1908     expected.set(11, 13);
1909
1910     assertTrue(bs.equals(expected));
1911
1912   }
1913
1914   public void testFindFeatures_largeEndPos()
1915   {
1916     /*
1917      * imitate a PDB sequence where end is larger than end position
1918      */
1919     SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
1920     sq.createDatasetSequence();
1921
1922     assertTrue(sq.findFeatures(1, 9).isEmpty());
1923     // should be no array bounds exception - JAL-2772
1924     assertTrue(sq.findFeatures(1, 15).isEmpty());
1925
1926     // add feature on BCD
1927     SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
1928             null);
1929     sq.addSequenceFeature(sfBCD);
1930
1931     // no features in columns 1-2 (-A)
1932     List<SequenceFeature> found = sq.findFeatures(1, 2);
1933     assertTrue(found.isEmpty());
1934
1935     // columns 1-6 (-ABC--) includes BCD
1936     found = sq.findFeatures(1, 6);
1937     assertEquals(1, found.size());
1938     assertTrue(found.contains(sfBCD));
1939
1940     // columns 10-11 (--) should find nothing
1941     found = sq.findFeatures(10, 11);
1942     assertEquals(0, found.size());
1943   }
1944
1945   @Test(groups = { "Functional" })
1946   public void testSetName()
1947   {
1948     SequenceI sq = new Sequence("test", "-ABC---DE-F--");
1949     assertEquals("test", sq.getName());
1950     assertEquals(1, sq.getStart());
1951     assertEquals(6, sq.getEnd());
1952
1953     sq.setName("testing");
1954     assertEquals("testing", sq.getName());
1955
1956     sq.setName("test/8-10");
1957     assertEquals("test", sq.getName());
1958     assertEquals(8, sq.getStart());
1959     assertEquals(13, sq.getEnd()); // note end is recomputed
1960
1961     sq.setName("testing/7-99");
1962     assertEquals("testing", sq.getName());
1963     assertEquals(7, sq.getStart());
1964     assertEquals(99, sq.getEnd()); // end may be beyond physical end
1965
1966     sq.setName("/2-3");
1967     assertEquals("", sq.getName());
1968     assertEquals(2, sq.getStart());
1969     assertEquals(7, sq.getEnd());
1970
1971     sq.setName("test/"); // invalid
1972     assertEquals("test/", sq.getName());
1973     assertEquals(2, sq.getStart());
1974     assertEquals(7, sq.getEnd());
1975
1976     sq.setName("test/6-13/7-99");
1977     assertEquals("test/6-13", sq.getName());
1978     assertEquals(7, sq.getStart());
1979     assertEquals(99, sq.getEnd());
1980
1981     sq.setName("test/0-5"); // 0 is invalid - ignored
1982     assertEquals("test/0-5", sq.getName());
1983     assertEquals(7, sq.getStart());
1984     assertEquals(99, sq.getEnd());
1985
1986     sq.setName("test/a-5"); // a is invalid - ignored
1987     assertEquals("test/a-5", sq.getName());
1988     assertEquals(7, sq.getStart());
1989     assertEquals(99, sq.getEnd());
1990
1991     sq.setName("test/6-5"); // start > end is invalid - ignored
1992     assertEquals("test/6-5", sq.getName());
1993     assertEquals(7, sq.getStart());
1994     assertEquals(99, sq.getEnd());
1995
1996     sq.setName("test/5"); // invalid - ignored
1997     assertEquals("test/5", sq.getName());
1998     assertEquals(7, sq.getStart());
1999     assertEquals(99, sq.getEnd());
2000
2001     sq.setName("test/-5"); // invalid - ignored
2002     assertEquals("test/-5", sq.getName());
2003     assertEquals(7, sq.getStart());
2004     assertEquals(99, sq.getEnd());
2005
2006     sq.setName("test/5-"); // invalid - ignored
2007     assertEquals("test/5-", sq.getName());
2008     assertEquals(7, sq.getStart());
2009     assertEquals(99, sq.getEnd());
2010
2011     sq.setName("test/5-6-7"); // invalid - ignored
2012     assertEquals("test/5-6-7", sq.getName());
2013     assertEquals(7, sq.getStart());
2014     assertEquals(99, sq.getEnd());
2015
2016     sq.setName(null); // invalid, gets converted to space
2017     assertEquals("", sq.getName());
2018     assertEquals(7, sq.getStart());
2019     assertEquals(99, sq.getEnd());
2020   }
2021
2022   @Test(groups = { "Functional" })
2023   public void testCheckValidRange()
2024   {
2025     Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
2026     assertEquals(7, sq.getStart());
2027     assertEquals(12, sq.getEnd());
2028
2029     /*
2030      * checkValidRange ensures end is at least the last residue position
2031      */
2032     PA.setValue(sq, "end", 2);
2033     sq.checkValidRange();
2034     assertEquals(12, sq.getEnd());
2035
2036     /*
2037      * end may be beyond the last residue position
2038      */
2039     PA.setValue(sq, "end", 22);
2040     sq.checkValidRange();
2041     assertEquals(22, sq.getEnd());
2042   }
2043
2044   @Test(groups = { "Functional" })
2045   public void testDeleteChars_withGaps()
2046   {
2047     /*
2048      * delete gaps only
2049      */
2050     SequenceI sq = new Sequence("test/8-10", "A-B-C");
2051     sq.createDatasetSequence();
2052     assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2053     sq.deleteChars(1, 2); // delete first gap
2054     assertEquals("AB-C", sq.getSequenceAsString());
2055     assertEquals(8, sq.getStart());
2056     assertEquals(10, sq.getEnd());
2057     assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2058
2059     /*
2060      * delete gaps and residues at start (no new dataset sequence)
2061      */
2062     sq = new Sequence("test/8-10", "A-B-C");
2063     sq.createDatasetSequence();
2064     sq.deleteChars(0, 3); // delete A-B
2065     assertEquals("-C", sq.getSequenceAsString());
2066     assertEquals(10, sq.getStart());
2067     assertEquals(10, sq.getEnd());
2068     assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2069
2070     /*
2071      * delete gaps and residues at end (no new dataset sequence)
2072      */
2073     sq = new Sequence("test/8-10", "A-B-C");
2074     sq.createDatasetSequence();
2075     sq.deleteChars(2, 5); // delete B-C
2076     assertEquals("A-", sq.getSequenceAsString());
2077     assertEquals(8, sq.getStart());
2078     assertEquals(8, sq.getEnd());
2079     assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
2080
2081     /*
2082      * delete gaps and residues internally (new dataset sequence)
2083      * first delete from gap to residue
2084      */
2085     sq = new Sequence("test/8-10", "A-B-C");
2086     sq.createDatasetSequence();
2087     sq.deleteChars(1, 3); // delete -B
2088     assertEquals("A-C", sq.getSequenceAsString());
2089     assertEquals(8, sq.getStart());
2090     assertEquals(9, sq.getEnd());
2091     assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2092     assertEquals(8, sq.getDatasetSequence().getStart());
2093     assertEquals(9, sq.getDatasetSequence().getEnd());
2094
2095     /*
2096      * internal delete from gap to gap
2097      */
2098     sq = new Sequence("test/8-10", "A-B-C");
2099     sq.createDatasetSequence();
2100     sq.deleteChars(1, 4); // delete -B-
2101     assertEquals("AC", sq.getSequenceAsString());
2102     assertEquals(8, sq.getStart());
2103     assertEquals(9, sq.getEnd());
2104     assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2105     assertEquals(8, sq.getDatasetSequence().getStart());
2106     assertEquals(9, sq.getDatasetSequence().getEnd());
2107
2108     /*
2109      * internal delete from residue to residue
2110      */
2111     sq = new Sequence("test/8-10", "A-B-C");
2112     sq.createDatasetSequence();
2113     sq.deleteChars(2, 3); // delete B
2114     assertEquals("A--C", sq.getSequenceAsString());
2115     assertEquals(8, sq.getStart());
2116     assertEquals(9, sq.getEnd());
2117     assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
2118     assertEquals(8, sq.getDatasetSequence().getStart());
2119     assertEquals(9, sq.getDatasetSequence().getEnd());
2120   }
2121
2122   /**
2123    * Test the code used to locate the reference sequence ruler origin
2124    */
2125   @Test(groups = { "Functional" })
2126   public void testLocateVisibleStartofSequence()
2127   {
2128     // create random alignment
2129     AlignmentGenerator gen = new AlignmentGenerator(false);
2130     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
2131
2132     HiddenColumns cs = al.getHiddenColumns();
2133     ColumnSelection colsel = new ColumnSelection();
2134
2135     SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
2136     assertEquals(2, seq.findIndex(seq.getStart()));
2137
2138     // no hidden columns
2139     assertEquals(seq.findIndex(seq.getStart()) - 1,
2140             seq.firstResidueOutsideIterator(cs.iterator()));
2141
2142     // hidden column on gap after end of sequence - should not affect bounds
2143     colsel.hideSelectedColumns(13, al.getHiddenColumns());
2144     assertEquals(seq.findIndex(seq.getStart()) - 1,
2145             seq.firstResidueOutsideIterator(cs.iterator()));
2146
2147     cs.revealAllHiddenColumns(colsel);
2148     // hidden column on gap before beginning of sequence - should vis bounds by
2149     // one
2150     colsel.hideSelectedColumns(0, al.getHiddenColumns());
2151     assertEquals(seq.findIndex(seq.getStart()) - 2,
2152             cs.absoluteToVisibleColumn(
2153                     seq.firstResidueOutsideIterator(cs.iterator())));
2154
2155     cs.revealAllHiddenColumns(colsel);
2156     // hide columns around most of sequence - leave one residue remaining
2157     cs.hideColumns(1, 3);
2158     cs.hideColumns(6, 11);
2159
2160     Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
2161
2162     assertEquals("-D", seq.getSequenceStringFromIterator(it));
2163     // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
2164     // { seq })[0]);
2165
2166     assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2167     cs.revealAllHiddenColumns(colsel);
2168
2169     // hide whole sequence - should just get location of hidden region
2170     // containing sequence
2171     cs.hideColumns(1, 11);
2172     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2173
2174     cs.revealAllHiddenColumns(colsel);
2175     cs.hideColumns(0, 15);
2176     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2177
2178     SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
2179
2180     cs.revealAllHiddenColumns(colsel);
2181     cs.hideColumns(7, 17);
2182     assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2183
2184     cs.revealAllHiddenColumns(colsel);
2185     cs.hideColumns(3, 17);
2186     assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2187
2188     cs.revealAllHiddenColumns(colsel);
2189     cs.hideColumns(3, 19);
2190     assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2191
2192     cs.revealAllHiddenColumns(colsel);
2193     cs.hideColumns(0, 0);
2194     assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2195
2196     cs.revealAllHiddenColumns(colsel);
2197     cs.hideColumns(0, 1);
2198     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2199
2200     cs.revealAllHiddenColumns(colsel);
2201     cs.hideColumns(0, 2);
2202     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2203
2204     cs.revealAllHiddenColumns(colsel);
2205     cs.hideColumns(1, 1);
2206     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2207
2208     cs.revealAllHiddenColumns(colsel);
2209     cs.hideColumns(1, 2);
2210     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2211
2212     cs.revealAllHiddenColumns(colsel);
2213     cs.hideColumns(1, 3);
2214     assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
2215
2216     cs.revealAllHiddenColumns(colsel);
2217     cs.hideColumns(0, 2);
2218     cs.hideColumns(5, 6);
2219     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2220
2221     cs.revealAllHiddenColumns(colsel);
2222     cs.hideColumns(0, 2);
2223     cs.hideColumns(5, 6);
2224     cs.hideColumns(9, 10);
2225     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2226
2227     cs.revealAllHiddenColumns(colsel);
2228     cs.hideColumns(0, 2);
2229     cs.hideColumns(7, 11);
2230     assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
2231
2232     cs.revealAllHiddenColumns(colsel);
2233     cs.hideColumns(2, 4);
2234     cs.hideColumns(7, 11);
2235     assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2236
2237     cs.revealAllHiddenColumns(colsel);
2238     cs.hideColumns(2, 4);
2239     cs.hideColumns(7, 12);
2240     assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
2241
2242     cs.revealAllHiddenColumns(colsel);
2243     cs.hideColumns(1, 11);
2244     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2245
2246     cs.revealAllHiddenColumns(colsel);
2247     cs.hideColumns(0, 12);
2248     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2249
2250     cs.revealAllHiddenColumns(colsel);
2251     cs.hideColumns(0, 4);
2252     cs.hideColumns(6, 12);
2253     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2254
2255     cs.revealAllHiddenColumns(colsel);
2256     cs.hideColumns(0, 1);
2257     cs.hideColumns(3, 12);
2258     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2259
2260     cs.revealAllHiddenColumns(colsel);
2261     cs.hideColumns(3, 14);
2262     cs.hideColumns(17, 19);
2263     assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2264
2265     cs.revealAllHiddenColumns(colsel);
2266     cs.hideColumns(3, 7);
2267     cs.hideColumns(9, 14);
2268     cs.hideColumns(17, 19);
2269     assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
2270
2271     cs.revealAllHiddenColumns(colsel);
2272     cs.hideColumns(0, 1);
2273     cs.hideColumns(3, 4);
2274     cs.hideColumns(6, 8);
2275     cs.hideColumns(10, 12);
2276     assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
2277
2278   }
2279
2280   @Test(groups = { "Functional" })
2281   public void testTransferAnnotation()
2282   {
2283     Sequence origSeq = new Sequence("MYSEQ", "THISISASEQ");
2284     Sequence toSeq = new Sequence("MYSEQ", "THISISASEQ");
2285     origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
2286     toSeq.transferAnnotation(origSeq, null);
2287     assertTrue(toSeq.getDBRefs().size() == 1);
2288
2289     assertTrue(toSeq.getDBRefs().get(0).isCanonical());
2290
2291     // check for promotion of non-canonical
2292     // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
2293     toSeq.setDBRefs(null);
2294     toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
2295     toSeq.transferAnnotation(origSeq, null);
2296     assertTrue(toSeq.getDBRefs().size() == 1);
2297
2298     assertTrue("Promotion of non-canonical DBRefEntry failed",
2299             toSeq.getDBRefs().get(0).isCanonical());
2300
2301   }
2302 }