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