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