3f9750c135666b326069044dec2c7ee5ab263c62
[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 jalview.commands.EditCommand;
32 import jalview.commands.EditCommand.Action;
33 import jalview.datamodel.PDBEntry.Type;
34 import jalview.gui.JvOptionPane;
35 import jalview.util.MapList;
36
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.BitSet;
41 import java.util.List;
42 import java.util.Vector;
43
44 import junit.extensions.PA;
45
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.Test;
50
51 public class SequenceTest
52 {
53
54   @BeforeClass(alwaysRun = true)
55   public void setUpJvOptionPane()
56   {
57     JvOptionPane.setInteractiveMode(false);
58     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
59   }
60
61   Sequence seq;
62
63   @BeforeMethod(alwaysRun = true)
64   public void setUp()
65   {
66     seq = new Sequence("FER1", "AKPNGVL");
67   }
68
69   @Test(groups = { "Functional" })
70   public void testInsertGapsAndGapmaps()
71   {
72     SequenceI aseq = seq.deriveSequence();
73     aseq.insertCharAt(2, 3, '-');
74     aseq.insertCharAt(6, 3, '-');
75     assertEquals("Gap insertions not correct", "AK---P---NGVL",
76             aseq.getSequenceAsString());
77     List<int[]> gapInt = aseq.getInsertions();
78     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
79     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
80     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
81     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
82
83     BitSet gapfield = aseq.getInsertionsAsBits();
84     BitSet expectedgaps = new BitSet();
85     expectedgaps.set(2, 5);
86     expectedgaps.set(6, 9);
87
88     assertEquals(6, expectedgaps.cardinality());
89
90     assertEquals("getInsertionsAsBits didn't mark expected number of gaps",
91             6, gapfield.cardinality());
92
93     assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield);
94   }
95
96   @Test(groups = ("Functional"))
97   public void testIsProtein()
98   {
99     // test Protein
100     assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
101     // test DNA
102     assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
103     // test RNA
104     SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
105     assertFalse(sq.isProtein());
106     // change sequence, should trigger an update of cached result
107     sq.setSequence("ASDFASDFADSF");
108     assertTrue(sq.isProtein());
109   }
110
111   @Test(groups = { "Functional" })
112   public void testGetAnnotation()
113   {
114     // initial state returns null not an empty array
115     assertNull(seq.getAnnotation());
116     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
117             1f);
118     AlignmentAnnotation[] anns = seq.getAnnotation();
119     assertEquals(1, anns.length);
120     assertSame(ann, anns[0]);
121
122     // removing all annotations reverts array to null
123     seq.removeAlignmentAnnotation(ann);
124     assertNull(seq.getAnnotation());
125   }
126
127   @Test(groups = { "Functional" })
128   public void testGetAnnotation_forLabel()
129   {
130     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
131             1f);
132     addAnnotation("label2", "desc2", "calcId2", 1f);
133     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
134             1f);
135     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
136     assertEquals(2, anns.length);
137     assertSame(ann1, anns[0]);
138     assertSame(ann3, anns[1]);
139   }
140
141   private AlignmentAnnotation addAnnotation(String label,
142           String description, String calcId, float value)
143   {
144     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
145             description, value);
146     annotation.setCalcId(calcId);
147     seq.addAlignmentAnnotation(annotation);
148     return annotation;
149   }
150
151   @Test(groups = { "Functional" })
152   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
153   {
154     addAnnotation("label1", "desc1", "calcId1", 1f);
155     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
156             1f);
157     addAnnotation("label2", "desc3", "calcId3", 1f);
158     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
159             1f);
160     addAnnotation("label5", "desc3", null, 1f);
161     addAnnotation(null, "desc3", "calcId3", 1f);
162
163     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
164             "label2");
165     assertEquals(2, anns.size());
166     assertSame(ann2, anns.get(0));
167     assertSame(ann4, anns.get(1));
168
169     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
170     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
171     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
172     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
173     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
174   }
175
176   /**
177    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
178    * setting the sequenceRef on the annotation. Adding the same annotation twice
179    * should be ignored.
180    */
181   @Test(groups = { "Functional" })
182   public void testAddAlignmentAnnotation()
183   {
184     assertNull(seq.getAnnotation());
185     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
186             "b", 2d);
187     assertNull(annotation.sequenceRef);
188     seq.addAlignmentAnnotation(annotation);
189     assertSame(seq, annotation.sequenceRef);
190     AlignmentAnnotation[] anns = seq.getAnnotation();
191     assertEquals(1, anns.length);
192     assertSame(annotation, anns[0]);
193
194     // re-adding does nothing
195     seq.addAlignmentAnnotation(annotation);
196     anns = seq.getAnnotation();
197     assertEquals(1, anns.length);
198     assertSame(annotation, anns[0]);
199
200     // an identical but different annotation can be added
201     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
202             "b", 2d);
203     seq.addAlignmentAnnotation(annotation2);
204     anns = seq.getAnnotation();
205     assertEquals(2, anns.length);
206     assertSame(annotation, anns[0]);
207     assertSame(annotation2, anns[1]);
208   }
209
210   @Test(groups = { "Functional" })
211   public void testGetStartGetEnd()
212   {
213     SequenceI sq = new Sequence("test", "ABCDEF");
214     assertEquals(1, sq.getStart());
215     assertEquals(6, sq.getEnd());
216
217     sq = new Sequence("test", "--AB-C-DEF--");
218     assertEquals(1, sq.getStart());
219     assertEquals(6, sq.getEnd());
220
221     sq = new Sequence("test", "----");
222     assertEquals(1, sq.getStart());
223     assertEquals(0, sq.getEnd()); // ??
224   }
225
226   /**
227    * Tests for the method that returns an alignment column position (base 1) for
228    * a given sequence position (base 1).
229    */
230   @Test(groups = { "Functional" })
231   public void testFindIndex()
232   {
233     /* 
234      * call sequenceChanged() after each test to invalidate any cursor,
235      * forcing the 1-arg findIndex to be executed
236      */
237     SequenceI sq = new Sequence("test", "ABCDEF");
238     assertEquals(0, sq.findIndex(0));
239     sq.sequenceChanged();
240     assertEquals(1, sq.findIndex(1));
241     sq.sequenceChanged();
242     assertEquals(5, sq.findIndex(5));
243     sq.sequenceChanged();
244     assertEquals(6, sq.findIndex(6));
245     sq.sequenceChanged();
246     assertEquals(6, sq.findIndex(9));
247
248     sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
249     assertEquals(2, sq.findIndex(8));
250     sq.sequenceChanged();
251     assertEquals(5, sq.findIndex(9));
252     sq.sequenceChanged();
253     assertEquals(7, sq.findIndex(10));
254
255     // before start returns 0
256     sq.sequenceChanged();
257     assertEquals(0, sq.findIndex(0));
258     sq.sequenceChanged();
259     assertEquals(0, sq.findIndex(-1));
260
261     // beyond end returns last residue column
262     sq.sequenceChanged();
263     assertEquals(13, sq.findIndex(99));
264   }
265
266   @Test(groups = { "Functional" })
267   public void testFindPositions()
268   {
269     SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
270
271     /*
272      * invalid inputs
273      */
274     assertNull(sq.findPositions(6, 5));
275     assertNull(sq.findPositions(0, 5));
276     assertNull(sq.findPositions(-1, 5));
277
278     /*
279      * all gapped ranges
280      */
281     assertNull(sq.findPositions(1, 1)); // 1-based columns
282     assertNull(sq.findPositions(5, 5));
283     assertNull(sq.findPositions(5, 6));
284     assertNull(sq.findPositions(5, 7));
285
286     /*
287      * all ungapped ranges
288      */
289     assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
290     assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
291     assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
292     assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
293
294     /*
295      * gap to ungapped range
296      */
297     assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
298     assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
299
300     /*
301      * ungapped to gapped range
302      */
303     assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
304     assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
305
306     /*
307      * ungapped to ungapped enclosing gaps
308      */
309     assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
310     assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
311
312     /*
313      * gapped to gapped enclosing ungapped
314      */
315     assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
316     assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
317     assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
318     assertEquals(new Range(8, 13), sq.findPositions(1, 99));
319   }
320
321   /**
322    * Tests for the method that returns a dataset sequence position (start..) for
323    * an aligned column position (base 0).
324    */
325   @Test(groups = { "Functional" })
326   public void testFindPosition()
327   {
328     /* 
329      * call sequenceChanged() after each test to invalidate any cursor,
330      * forcing the 1-arg findPosition to be executed
331      */
332     SequenceI sq = new Sequence("test/8-13", "ABCDEF");
333     assertEquals(8, sq.findPosition(0));
334     // Sequence should now hold a cursor at [8, 0]
335     assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
336             PA.getValue(sq, "cursor").toString());
337     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
338     int token = (int) PA.getValue(sq, "changeCount");
339     assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
340
341     sq.sequenceChanged();
342
343     /*
344      * find F13 at column offset 5, cursor should update to [13, 6]
345      * endColumn is found and saved in cursor
346      */
347     assertEquals(13, sq.findPosition(5));
348     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
349     assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
350     assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
351     assertEquals("test:Pos13:Col6:startCol1:endCol6:tok1",
352             PA.getValue(sq, "cursor").toString());
353
354     // assertEquals(-1, seq.findPosition(6)); // fails
355
356     sq = new Sequence("test/8-11", "AB-C-D--");
357     token = (int) PA.getValue(sq, "changeCount"); // 0
358     assertEquals(8, sq.findPosition(0));
359     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
360     assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
361     assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
362             PA.getValue(sq, "cursor").toString());
363
364     sq.sequenceChanged();
365     assertEquals(9, sq.findPosition(1));
366     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
367     assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
368     assertEquals("test:Pos9:Col2:startCol1:endCol0:tok1",
369             PA.getValue(sq, "cursor").toString());
370
371     sq.sequenceChanged();
372     // gap position 'finds' residue to the right (not the left as per javadoc)
373     // cursor is set to the last residue position found [B 2]
374     assertEquals(10, sq.findPosition(2));
375     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
376     assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
377     assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
378             PA.getValue(sq, "cursor").toString());
379
380     sq.sequenceChanged();
381     assertEquals(10, sq.findPosition(3));
382     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
383     assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
384     assertEquals("test:Pos10:Col4:startCol1:endCol0:tok3",
385             PA.getValue(sq, "cursor").toString());
386
387     sq.sequenceChanged();
388     // column[4] is the gap after C - returns D11
389     // cursor is set to [C 4]
390     assertEquals(11, sq.findPosition(4));
391     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
392     assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
393     assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
394             PA.getValue(sq, "cursor").toString());
395
396     sq.sequenceChanged();
397     assertEquals(11, sq.findPosition(5)); // D
398     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
399     assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
400     // lastCol has been found and saved in the cursor
401     assertEquals("test:Pos11:Col6:startCol1:endCol6:tok5",
402             PA.getValue(sq, "cursor").toString());
403
404     sq.sequenceChanged();
405     // returns 1 more than sequence length if off the end ?!?
406     assertEquals(12, sq.findPosition(6));
407
408     sq.sequenceChanged();
409     assertEquals(12, sq.findPosition(7));
410
411     /*
412      * first findPosition should also set firstResCol in cursor
413      */
414     sq = new Sequence("test/8-13", "--AB-C-DEF--");
415     assertEquals(8, sq.findPosition(0));
416     assertNull(PA.getValue(sq, "cursor"));
417
418     sq.sequenceChanged();
419     assertEquals(8, sq.findPosition(1));
420     assertNull(PA.getValue(sq, "cursor"));
421
422     sq.sequenceChanged();
423     assertEquals(8, sq.findPosition(2));
424     assertEquals("test:Pos8:Col3:startCol3:endCol0:tok2",
425             PA.getValue(sq, "cursor").toString());
426
427     sq.sequenceChanged();
428     assertEquals(9, sq.findPosition(3));
429     assertEquals("test:Pos9:Col4:startCol3:endCol0:tok3",
430             PA.getValue(sq, "cursor").toString());
431
432     sq.sequenceChanged();
433     // column[4] is a gap, returns next residue pos (C10)
434     // cursor is set to last residue found [B]
435     assertEquals(10, sq.findPosition(4));
436     assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
437             PA.getValue(sq, "cursor").toString());
438
439     sq.sequenceChanged();
440     assertEquals(10, sq.findPosition(5));
441     assertEquals("test:Pos10:Col6:startCol3:endCol0:tok5",
442             PA.getValue(sq, "cursor").toString());
443
444     sq.sequenceChanged();
445     // column[6] is a gap, returns next residue pos (D11)
446     // cursor is set to last residue found [C]
447     assertEquals(11, sq.findPosition(6));
448     assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
449             PA.getValue(sq, "cursor").toString());
450
451     sq.sequenceChanged();
452     assertEquals(11, sq.findPosition(7));
453     assertEquals("test:Pos11:Col8:startCol3:endCol0:tok7",
454             PA.getValue(sq, "cursor").toString());
455
456     sq.sequenceChanged();
457     assertEquals(12, sq.findPosition(8));
458     assertEquals("test:Pos12:Col9:startCol3:endCol0:tok8",
459             PA.getValue(sq, "cursor").toString());
460
461     /*
462      * when the last residue column is found, it is set in the cursor
463      */
464     sq.sequenceChanged();
465     assertEquals(13, sq.findPosition(9));
466     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok9",
467             PA.getValue(sq, "cursor").toString());
468
469     sq.sequenceChanged();
470     assertEquals(14, sq.findPosition(10));
471     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
472             PA.getValue(sq, "cursor").toString());
473
474     /*
475      * findPosition for column beyond sequence length
476      * returns 1 more than last residue position
477      */
478     sq.sequenceChanged();
479     assertEquals(14, sq.findPosition(11));
480     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
481             PA.getValue(sq, "cursor").toString());
482
483     sq.sequenceChanged();
484     assertEquals(14, sq.findPosition(99));
485     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
486             PA.getValue(sq, "cursor").toString());
487
488     /*
489      * gapped sequence ending in non-gap
490      */
491     sq = new Sequence("test/8-13", "--AB-C-DEF");
492     assertEquals(13, sq.findPosition(9));
493     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok0",
494             PA.getValue(sq, "cursor").toString());
495     sq.sequenceChanged();
496     assertEquals(12, sq.findPosition(8)); // E12
497     // sequenceChanged() invalidates cursor.lastResidueColumn
498     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
499     assertEquals("test:Pos12:Col9:startCol3:endCol0:tok1",
500             cursor.toString());
501     // findPosition with cursor accepts base 1 column values
502     assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
503     assertEquals(13, sq.findPosition(9)); // F13
504     // lastResidueColumn has now been found and saved in cursor
505     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
506             PA.getValue(sq, "cursor").toString());
507   }
508
509   @Test(groups = { "Functional" })
510   public void testDeleteChars()
511   {
512     /*
513      * internal delete
514      */
515     SequenceI sq = new Sequence("test", "ABCDEF");
516     assertNull(PA.getValue(sq, "datasetSequence"));
517     assertEquals(1, sq.getStart());
518     assertEquals(6, sq.getEnd());
519     sq.deleteChars(2, 3);
520     assertEquals("ABDEF", sq.getSequenceAsString());
521     assertEquals(1, sq.getStart());
522     assertEquals(5, sq.getEnd());
523     assertNull(PA.getValue(sq, "datasetSequence"));
524
525     /*
526      * delete at start
527      */
528     sq = new Sequence("test", "ABCDEF");
529     sq.deleteChars(0, 2);
530     assertEquals("CDEF", sq.getSequenceAsString());
531     assertEquals(3, sq.getStart());
532     assertEquals(6, sq.getEnd());
533     assertNull(PA.getValue(sq, "datasetSequence"));
534
535     sq = new Sequence("test", "ABCDE");
536     sq.deleteChars(0, 3);
537     assertEquals("DE", sq.getSequenceAsString());
538     assertEquals(4, sq.getStart());
539     assertEquals(5, sq.getEnd());
540     assertNull(PA.getValue(sq, "datasetSequence"));
541
542     /*
543      * delete at end
544      */
545     sq = new Sequence("test", "ABCDEF");
546     sq.deleteChars(4, 6);
547     assertEquals("ABCD", sq.getSequenceAsString());
548     assertEquals(1, sq.getStart());
549     assertEquals(4, sq.getEnd());
550     assertNull(PA.getValue(sq, "datasetSequence"));
551
552     /*
553      * delete more positions than there are
554      */
555     sq = new Sequence("test/8-11", "ABCD");
556     sq.deleteChars(0, 99);
557     assertEquals("", sq.getSequenceAsString());
558     assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
559     assertEquals(11, sq.getEnd());
560
561     sq = new Sequence("test/8-11", "----");
562     sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
563     assertEquals("", sq.getSequenceAsString());
564     assertEquals(8, sq.getStart());
565     assertEquals(11, sq.getEnd());
566   }
567
568   @Test(groups = { "Functional" })
569   public void testDeleteChars_withDbRefsAndFeatures()
570   {
571     /*
572      * internal delete - new dataset sequence created
573      * gets a copy of any dbrefs
574      */
575     SequenceI sq = new Sequence("test", "ABCDEF");
576     sq.createDatasetSequence();
577     DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
578     sq.addDBRef(dbr1);
579     Object ds = PA.getValue(sq, "datasetSequence");
580     assertNotNull(ds);
581     assertEquals(1, sq.getStart());
582     assertEquals(6, sq.getEnd());
583     sq.deleteChars(2, 3);
584     assertEquals("ABDEF", sq.getSequenceAsString());
585     assertEquals(1, sq.getStart());
586     assertEquals(5, sq.getEnd());
587     Object newDs = PA.getValue(sq, "datasetSequence");
588     assertNotNull(newDs);
589     assertNotSame(ds, newDs);
590     assertNotNull(sq.getDBRefs());
591     assertEquals(1, sq.getDBRefs().length);
592     assertNotSame(dbr1, sq.getDBRefs()[0]);
593     assertEquals(dbr1, sq.getDBRefs()[0]);
594
595     /*
596      * internal delete with sequence features
597      * (failure case for JAL-2541)
598      */
599     sq = new Sequence("test", "ABCDEF");
600     sq.createDatasetSequence();
601     SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
602             "CathGroup");
603     sq.addSequenceFeature(sf1);
604     ds = PA.getValue(sq, "datasetSequence");
605     assertNotNull(ds);
606     assertEquals(1, sq.getStart());
607     assertEquals(6, sq.getEnd());
608     sq.deleteChars(2, 4);
609     assertEquals("ABEF", sq.getSequenceAsString());
610     assertEquals(1, sq.getStart());
611     assertEquals(4, sq.getEnd());
612     newDs = PA.getValue(sq, "datasetSequence");
613     assertNotNull(newDs);
614     assertNotSame(ds, newDs);
615     List<SequenceFeature> sfs = sq.getSequenceFeatures();
616     assertEquals(1, sfs.size());
617     assertNotSame(sf1, sfs.get(0));
618     assertEquals(sf1, sfs.get(0));
619
620     /*
621      * delete at start - no new dataset sequence created
622      * any sequence features remain as before
623      */
624     sq = new Sequence("test", "ABCDEF");
625     sq.createDatasetSequence();
626     ds = PA.getValue(sq, "datasetSequence");
627     sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
628     sq.addSequenceFeature(sf1);
629     sq.deleteChars(0, 2);
630     assertEquals("CDEF", sq.getSequenceAsString());
631     assertEquals(3, sq.getStart());
632     assertEquals(6, sq.getEnd());
633     assertSame(ds, PA.getValue(sq, "datasetSequence"));
634     sfs = sq.getSequenceFeatures();
635     assertNotNull(sfs);
636     assertEquals(1, sfs.size());
637     assertSame(sf1, sfs.get(0));
638
639     /*
640      * delete at end - no new dataset sequence created
641      * any dbrefs remain as before
642      */
643     sq = new Sequence("test", "ABCDEF");
644     sq.createDatasetSequence();
645     ds = PA.getValue(sq, "datasetSequence");
646     dbr1 = new DBRefEntry("Uniprot", "0", "a123");
647     sq.addDBRef(dbr1);
648     sq.deleteChars(4, 6);
649     assertEquals("ABCD", sq.getSequenceAsString());
650     assertEquals(1, sq.getStart());
651     assertEquals(4, sq.getEnd());
652     assertSame(ds, PA.getValue(sq, "datasetSequence"));
653     assertNotNull(sq.getDBRefs());
654     assertEquals(1, sq.getDBRefs().length);
655     assertSame(dbr1, sq.getDBRefs()[0]);
656   }
657
658   @Test(groups = { "Functional" })
659   public void testInsertCharAt()
660   {
661     // non-static methods:
662     SequenceI sq = new Sequence("test", "ABCDEF");
663     sq.insertCharAt(0, 'z');
664     assertEquals("zABCDEF", sq.getSequenceAsString());
665     sq.insertCharAt(2, 2, 'x');
666     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
667
668     // for static method see StringUtilsTest
669   }
670
671   /**
672    * Test the method that returns an array of aligned sequence positions where
673    * the array index is the data sequence position (both base 0).
674    */
675   @Test(groups = { "Functional" })
676   public void testGapMap()
677   {
678     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
679     sq.createDatasetSequence();
680     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
681   }
682
683   /**
684    * Test the method that gets sequence features, either from the sequence or
685    * its dataset.
686    */
687   @Test(groups = { "Functional" })
688   public void testGetSequenceFeatures()
689   {
690     SequenceI sq = new Sequence("test", "GATCAT");
691     sq.createDatasetSequence();
692
693     assertTrue(sq.getSequenceFeatures().isEmpty());
694
695     /*
696      * SequenceFeature on sequence
697      */
698     SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
699     sq.addSequenceFeature(sf);
700     List<SequenceFeature> sfs = sq.getSequenceFeatures();
701     assertEquals(1, sfs.size());
702     assertSame(sf, sfs.get(0));
703
704     /*
705      * SequenceFeature on sequence and dataset sequence; returns that on
706      * sequence
707      * 
708      * Note JAL-2046: spurious: we have no use case for this at the moment.
709      * This test also buggy - as sf2.equals(sf), no new feature is added
710      */
711     SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
712             null);
713     sq.getDatasetSequence().addSequenceFeature(sf2);
714     sfs = sq.getSequenceFeatures();
715     assertEquals(1, sfs.size());
716     assertSame(sf, sfs.get(0));
717
718     /*
719      * SequenceFeature on dataset sequence only
720      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
721      */
722     sq.setSequenceFeatures(null);
723     assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
724
725     /*
726      * Corrupt case - no SequenceFeature, dataset's dataset is the original
727      * sequence. Test shows no infinite loop results.
728      */
729     sq.getDatasetSequence().setSequenceFeatures(null);
730     /**
731      * is there a usecase for this ? setDatasetSequence should throw an error if
732      * this actually occurs.
733      */
734     try
735     {
736       sq.getDatasetSequence().setDatasetSequence(sq); // loop!
737       Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
738     } catch (IllegalArgumentException e)
739     {
740       // TODO Jalview error/exception class for raising implementation errors
741       assertTrue(e.getMessage().toLowerCase()
742               .contains("implementation error"));
743     }
744     assertTrue(sq.getSequenceFeatures().isEmpty());
745   }
746
747   /**
748    * Test the method that returns an array, indexed by sequence position, whose
749    * entries are the residue positions at the sequence position (or to the right
750    * if a gap)
751    */
752   @Test(groups = { "Functional" })
753   public void testFindPositionMap()
754   {
755     /*
756      * Note: Javadoc for findPosition says it returns the residue position to
757      * the left of a gapped position; in fact it returns the position to the
758      * right. Also it returns a non-existent residue position for a gap beyond
759      * the sequence.
760      */
761     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
762     int[] map = sq.findPositionMap();
763     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
764             Arrays.toString(map));
765   }
766
767   /**
768    * Test for getSubsequence
769    */
770   @Test(groups = { "Functional" })
771   public void testGetSubsequence()
772   {
773     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
774     sq.createDatasetSequence();
775
776     // positions are base 0, end position is exclusive
777     SequenceI subseq = sq.getSubSequence(2, 4);
778
779     assertEquals("CD", subseq.getSequenceAsString());
780     // start/end are base 1 positions
781     assertEquals(3, subseq.getStart());
782     assertEquals(4, subseq.getEnd());
783     // subsequence shares the full dataset sequence
784     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
785   }
786
787   /**
788    * test createDatasetSequence behaves to doc
789    */
790   @Test(groups = { "Functional" })
791   public void testCreateDatasetSequence()
792   {
793     SequenceI sq = new Sequence("my", "ASDASD");
794     sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
795             "group"));
796     sq.addDBRef(new DBRefEntry("source", "version", "accession"));
797     assertNull(sq.getDatasetSequence());
798     assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
799     assertNotNull(PA.getValue(sq, "dbrefs"));
800
801     SequenceI rds = sq.createDatasetSequence();
802     assertNotNull(rds);
803     assertNull(rds.getDatasetSequence());
804     assertSame(sq.getDatasetSequence(), rds);
805
806     // sequence features and dbrefs transferred to dataset sequence
807     assertNull(PA.getValue(sq, "sequenceFeatureStore"));
808     assertNull(PA.getValue(sq, "dbrefs"));
809     assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
810     assertNotNull(PA.getValue(rds, "dbrefs"));
811   }
812
813   /**
814    * Test for deriveSequence applied to a sequence with a dataset
815    */
816   @Test(groups = { "Functional" })
817   public void testDeriveSequence_existingDataset()
818   {
819     Sequence sq = new Sequence("Seq1", "CD");
820     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
821     sq.getDatasetSequence().addSequenceFeature(
822             new SequenceFeature("", "", 1, 2, 0f, null));
823     sq.setStart(3);
824     sq.setEnd(4);
825
826     sq.setDescription("Test sequence description..");
827     sq.setVamsasId("TestVamsasId");
828     sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
829
830     sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
831     sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
832     sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
833     sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
834
835     sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
836     sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
837     sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
838     sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
839
840     // these are the same as ones already added
841     DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
842     DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
843
844     List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
845         pdb2pdb });
846
847     sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
848     sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
849     sq.getDatasetSequence().addDBRef(
850             new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
851     sq.getDatasetSequence().addDBRef(
852             new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
853
854     PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
855     PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
856     PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
857             "filePath/test2");
858     PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
859             "filePath/test2");
860     sq.getDatasetSequence().addPDBId(pdbe1a);
861     sq.getDatasetSequence().addPDBId(pdbe1b);
862     sq.getDatasetSequence().addPDBId(pdbe2a);
863     sq.getDatasetSequence().addPDBId(pdbe2b);
864
865     /*
866      * test we added pdb entries to the dataset sequence
867      */
868     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
869             .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
870             "PDB Entries were not found on dataset sequence.");
871
872     /*
873      * we should recover a pdb entry that is on the dataset sequence via PDBEntry
874      */
875     Assert.assertEquals(pdbe1a,
876             sq.getDatasetSequence().getPDBEntry("1PDB"),
877             "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
878     ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
879     System.out.println(">>>>>> " + sq.getSequenceAsString().length());
880     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
881     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
882     Annotation[] annots = annotsList.toArray(new Annotation[0]);
883     sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
884             "Test annot description", annots));
885     sq.getDatasetSequence().addAlignmentAnnotation(
886             new AlignmentAnnotation("Test annot", "Test annot description",
887                     annots));
888     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
889     Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
890                                                    // sequence
891     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
892     Assert.assertNotNull(sq.getAnnotation());
893     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
894     Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
895                                                                         // as
896                                                                         // sq.getDBRefs()
897     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
898             4);
899     Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
900
901     Sequence derived = (Sequence) sq.deriveSequence();
902
903     Assert.assertEquals(derived.getDescription(),
904             "Test sequence description..");
905     Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
906     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
907     Assert.assertNotNull(derived.getAnnotation());
908     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
909     Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
910     Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
911             .size(), 4);
912     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
913
914     assertEquals("CD", derived.getSequenceAsString());
915     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
916
917     // derived sequence should access dataset sequence features
918     assertNotNull(sq.getSequenceFeatures());
919     assertEquals(sq.getSequenceFeatures(), derived.getSequenceFeatures());
920
921     /*
922      *  verify we have primary db refs *just* for PDB IDs with associated
923      *  PDBEntry objects
924      */
925
926     assertEquals(primRefs, sq.getPrimaryDBRefs());
927     assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
928
929     assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
930
931   }
932
933   /**
934    * Test for deriveSequence applied to an ungapped sequence with no dataset
935    */
936   @Test(groups = { "Functional" })
937   public void testDeriveSequence_noDatasetUngapped()
938   {
939     SequenceI sq = new Sequence("Seq1", "ABCDEF");
940     assertEquals(1, sq.getStart());
941     assertEquals(6, sq.getEnd());
942     SequenceI derived = sq.deriveSequence();
943     assertEquals("ABCDEF", derived.getSequenceAsString());
944     assertEquals("ABCDEF", derived.getDatasetSequence()
945             .getSequenceAsString());
946   }
947
948   /**
949    * Test for deriveSequence applied to a gapped sequence with no dataset
950    */
951   @Test(groups = { "Functional" })
952   public void testDeriveSequence_noDatasetGapped()
953   {
954     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
955     assertEquals(1, sq.getStart());
956     assertEquals(6, sq.getEnd());
957     assertNull(sq.getDatasetSequence());
958     SequenceI derived = sq.deriveSequence();
959     assertEquals("AB-C.D EF", derived.getSequenceAsString());
960     assertEquals("ABCDEF", derived.getDatasetSequence()
961             .getSequenceAsString());
962   }
963
964   @Test(groups = { "Functional" })
965   public void testCopyConstructor_noDataset()
966   {
967     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
968     seq1.setDescription("description");
969     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
970             1.3d));
971     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
972             12.4f, "group"));
973     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
974     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
975
976     SequenceI copy = new Sequence(seq1);
977
978     assertNull(copy.getDatasetSequence());
979
980     verifyCopiedSequence(seq1, copy);
981
982     // copy has a copy of the DBRefEntry
983     // this is murky - DBrefs are only copied for dataset sequences
984     // where the test for 'dataset sequence' is 'dataset is null'
985     // but that doesn't distinguish it from an aligned sequence
986     // which has not yet generated a dataset sequence
987     // NB getDBRef looks inside dataset sequence if not null
988     DBRefEntry[] dbrefs = copy.getDBRefs();
989     assertEquals(1, dbrefs.length);
990     assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
991     assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
992   }
993
994   @Test(groups = { "Functional" })
995   public void testCopyConstructor_withDataset()
996   {
997     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
998     seq1.createDatasetSequence();
999     seq1.setDescription("description");
1000     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
1001             1.3d));
1002     // JAL-2046 - what is the contract for using a derived sequence's
1003     // addSequenceFeature ?
1004     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
1005             12.4f, "group"));
1006     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
1007     // here we add DBRef to the dataset sequence:
1008     seq1.getDatasetSequence().addDBRef(
1009             new DBRefEntry("EMBL", "1.2", "AZ12345"));
1010
1011     SequenceI copy = new Sequence(seq1);
1012
1013     assertNotNull(copy.getDatasetSequence());
1014     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
1015
1016     verifyCopiedSequence(seq1, copy);
1017
1018     // getDBRef looks inside dataset sequence and this is shared,
1019     // so holds the same dbref objects
1020     DBRefEntry[] dbrefs = copy.getDBRefs();
1021     assertEquals(1, dbrefs.length);
1022     assertSame(dbrefs[0], seq1.getDBRefs()[0]);
1023   }
1024
1025   /**
1026    * Helper to make assertions about a copied sequence
1027    * 
1028    * @param seq1
1029    * @param copy
1030    */
1031   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
1032   {
1033     // verify basic properties:
1034     assertEquals(copy.getName(), seq1.getName());
1035     assertEquals(copy.getDescription(), seq1.getDescription());
1036     assertEquals(copy.getStart(), seq1.getStart());
1037     assertEquals(copy.getEnd(), seq1.getEnd());
1038     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
1039
1040     // copy has a copy of the annotation:
1041     AlignmentAnnotation[] anns = copy.getAnnotation();
1042     assertEquals(1, anns.length);
1043     assertFalse(anns[0] == seq1.getAnnotation()[0]);
1044     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
1045     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
1046     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
1047
1048     // copy has a copy of the sequence feature:
1049     List<SequenceFeature> sfs = copy.getSequenceFeatures();
1050     assertEquals(1, sfs.size());
1051     if (seq1.getDatasetSequence() != null
1052             && copy.getDatasetSequence() == seq1.getDatasetSequence())
1053     {
1054       assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1055     }
1056     else
1057     {
1058       assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
1059     }
1060     assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
1061
1062     // copy has a copy of the PDB entry
1063     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
1064     assertEquals(1, pdbs.size());
1065     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
1066     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
1067   }
1068
1069   @Test(groups = "Functional")
1070   public void testGetCharAt()
1071   {
1072     SequenceI sq = new Sequence("", "abcde");
1073     assertEquals('a', sq.getCharAt(0));
1074     assertEquals('e', sq.getCharAt(4));
1075     assertEquals(' ', sq.getCharAt(5));
1076     assertEquals(' ', sq.getCharAt(-1));
1077   }
1078
1079   @Test(groups = { "Functional" })
1080   public void testAddSequenceFeatures()
1081   {
1082     SequenceI sq = new Sequence("", "abcde");
1083     // type may not be null
1084     assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
1085             8, 0f, null)));
1086     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1087             8, 0f, null)));
1088     // can't add a duplicate feature
1089     assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
1090             4, 8, 0f, null)));
1091     // can add a different feature
1092     assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
1093             8, 0f, null))); // different type
1094     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
1095             "description", 4, 8, 0f, null)));// different description
1096     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
1097             8, 0f, null))); // different start position
1098     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1099             9, 0f, null))); // different end position
1100     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1101             8, 1f, null))); // different score
1102     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1103             8, Float.NaN, null))); // score NaN
1104     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
1105             8, 0f, "Metal"))); // different group
1106     assertEquals(8, sq.getFeatures().getAllFeatures().size());
1107   }
1108
1109   /**
1110    * Tests for adding (or updating) dbrefs
1111    * 
1112    * @see DBRefEntry#updateFrom(DBRefEntry)
1113    */
1114   @Test(groups = { "Functional" })
1115   public void testAddDBRef()
1116   {
1117     SequenceI sq = new Sequence("", "abcde");
1118     assertNull(sq.getDBRefs());
1119     DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
1120     sq.addDBRef(dbref);
1121     assertEquals(1, sq.getDBRefs().length);
1122     assertSame(dbref, sq.getDBRefs()[0]);
1123
1124     /*
1125      * change of version - new entry
1126      */
1127     DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
1128     sq.addDBRef(dbref2);
1129     assertEquals(2, sq.getDBRefs().length);
1130     assertSame(dbref, sq.getDBRefs()[0]);
1131     assertSame(dbref2, sq.getDBRefs()[1]);
1132
1133     /*
1134      * matches existing entry - not added
1135      */
1136     sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
1137     assertEquals(2, sq.getDBRefs().length);
1138
1139     /*
1140      * different source = new entry
1141      */
1142     DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
1143     sq.addDBRef(dbref3);
1144     assertEquals(3, sq.getDBRefs().length);
1145     assertSame(dbref3, sq.getDBRefs()[2]);
1146
1147     /*
1148      * different ref = new entry
1149      */
1150     DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
1151     sq.addDBRef(dbref4);
1152     assertEquals(4, sq.getDBRefs().length);
1153     assertSame(dbref4, sq.getDBRefs()[3]);
1154
1155     /*
1156      * matching ref with a mapping - map updated
1157      */
1158     DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
1159     Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
1160         1, 1 }, 3, 1));
1161     dbref5.setMap(map);
1162     sq.addDBRef(dbref5);
1163     assertEquals(4, sq.getDBRefs().length);
1164     assertSame(dbref4, sq.getDBRefs()[3]);
1165     assertSame(map, dbref4.getMap());
1166
1167     /*
1168      * 'real' version replaces "0" version
1169      */
1170     dbref2.setVersion("0");
1171     DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
1172             dbref2.getAccessionId());
1173     sq.addDBRef(dbref6);
1174     assertEquals(4, sq.getDBRefs().length);
1175     assertSame(dbref2, sq.getDBRefs()[1]);
1176     assertEquals("3", dbref2.getVersion());
1177
1178     /*
1179      * 'real' version replaces "source:0" version
1180      */
1181     dbref3.setVersion("Uniprot:0");
1182     DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
1183             dbref3.getAccessionId());
1184     sq.addDBRef(dbref7);
1185     assertEquals(4, sq.getDBRefs().length);
1186     assertSame(dbref3, sq.getDBRefs()[2]);
1187     assertEquals("3", dbref2.getVersion());
1188   }
1189
1190   @Test(groups = { "Functional" })
1191   public void testGetPrimaryDBRefs_peptide()
1192   {
1193     SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
1194
1195     // no dbrefs
1196     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1197     assertTrue(primaryDBRefs.isEmpty());
1198
1199     // empty dbrefs
1200     sq.setDBRefs(new DBRefEntry[] {});
1201     primaryDBRefs = sq.getPrimaryDBRefs();
1202     assertTrue(primaryDBRefs.isEmpty());
1203
1204     // primary - uniprot
1205     DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
1206     sq.addDBRef(upentry1);
1207
1208     // primary - uniprot with congruent map
1209     DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
1210     upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
1211             new int[] { 10, 22 }, 1, 1)));
1212     sq.addDBRef(upentry2);
1213
1214     // primary - uniprot with map of enclosing sequence
1215     DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
1216     upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
1217             new int[] { 8, 24 }, 1, 1)));
1218     sq.addDBRef(upentry3);
1219
1220     // not primary - uniprot with map of sub-sequence (5')
1221     DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
1222     upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
1223             new int[] { 10, 18 }, 1, 1)));
1224     sq.addDBRef(upentry4);
1225
1226     // not primary - uniprot with map that overlaps 3'
1227     DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
1228     upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
1229             new int[] { 12, 22 }, 1, 1)));
1230     sq.addDBRef(upentry5);
1231
1232     // not primary - uniprot with map to different coordinates frame
1233     DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
1234     upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
1235             new int[] { 112, 118 }, 1, 1)));
1236     sq.addDBRef(upentry6);
1237
1238     // not primary - dbref to 'non-core' database
1239     DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1240     sq.addDBRef(upentry7);
1241
1242     // primary - type is PDB
1243     DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1244     sq.addDBRef(pdbentry);
1245
1246     // not primary - PDBEntry has no file
1247     sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1248
1249     // not primary - no PDBEntry
1250     sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1251
1252     // add corroborating PDB entry for primary DBref -
1253     // needs to have a file as well as matching ID
1254     // note PDB ID is not treated as case sensitive
1255     sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1256             .toString()));
1257
1258     // not valid DBRef - no file..
1259     sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1260
1261     primaryDBRefs = sq.getPrimaryDBRefs();
1262     assertEquals(4, primaryDBRefs.size());
1263     assertTrue("Couldn't find simple primary reference (UNIPROT)",
1264             primaryDBRefs.contains(upentry1));
1265     assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1266             primaryDBRefs.contains(upentry2));
1267     assertTrue("Couldn't find mapped context reference (UNIPROT)",
1268             primaryDBRefs.contains(upentry3));
1269     assertTrue("Couldn't find expected PDB primary reference",
1270             primaryDBRefs.contains(pdbentry));
1271   }
1272
1273   @Test(groups = { "Functional" })
1274   public void testGetPrimaryDBRefs_nucleotide()
1275   {
1276     SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1277
1278     // primary - Ensembl
1279     DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1280     sq.addDBRef(dbr1);
1281
1282     // not primary - Ensembl 'transcript' mapping of sub-sequence
1283     DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1284     dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1285             new int[] { 1, 11 }, 1, 1)));
1286     sq.addDBRef(dbr2);
1287
1288     // primary - EMBL with congruent map
1289     DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1290     dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1291             new int[] { 10, 34 }, 1, 1)));
1292     sq.addDBRef(dbr3);
1293
1294     // not primary - to non-core database
1295     DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1296     sq.addDBRef(dbr4);
1297
1298     // not primary - to protein
1299     DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1300     sq.addDBRef(dbr5);
1301
1302     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1303     assertEquals(2, primaryDBRefs.size());
1304     assertTrue(primaryDBRefs.contains(dbr1));
1305     assertTrue(primaryDBRefs.contains(dbr3));
1306   }
1307
1308   /**
1309    * Test the method that updates the list of PDBEntry from any new DBRefEntry
1310    * for PDB
1311    */
1312   @Test(groups = { "Functional" })
1313   public void testUpdatePDBIds()
1314   {
1315     PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1316     seq.addPDBId(pdbe1);
1317     seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1318     seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1319     seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1320     seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1321     // 7 is not a valid chain code:
1322     seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1323
1324     seq.updatePDBIds();
1325     List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1326     assertEquals(4, pdbIds.size());
1327     assertSame(pdbe1, pdbIds.get(0));
1328     // chain code got added to 3A6S:
1329     assertEquals("B", pdbe1.getChainCode());
1330     assertEquals("1A70", pdbIds.get(1).getId());
1331     // 4BQGA is parsed into id + chain
1332     assertEquals("4BQG", pdbIds.get(2).getId());
1333     assertEquals("a", pdbIds.get(2).getChainCode());
1334     assertEquals("2GIS7", pdbIds.get(3).getId());
1335     assertNull(pdbIds.get(3).getChainCode());
1336   }
1337
1338   /**
1339    * Test the method that either adds a pdbid or updates an existing one
1340    */
1341   @Test(groups = { "Functional" })
1342   public void testAddPDBId()
1343   {
1344     PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1345     seq.addPDBId(pdbe);
1346     assertEquals(1, seq.getAllPDBEntries().size());
1347     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1348     assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1349
1350     // add the same entry
1351     seq.addPDBId(pdbe);
1352     assertEquals(1, seq.getAllPDBEntries().size());
1353     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1354
1355     // add an identical entry
1356     seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1357     assertEquals(1, seq.getAllPDBEntries().size());
1358     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1359
1360     // add a different entry
1361     PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1362     seq.addPDBId(pdbe2);
1363     assertEquals(2, seq.getAllPDBEntries().size());
1364     assertSame(pdbe, seq.getAllPDBEntries().get(0));
1365     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1366
1367     // update pdbe with chain code, file, type
1368     PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1369     seq.addPDBId(pdbe3);
1370     assertEquals(2, seq.getAllPDBEntries().size());
1371     assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1372     assertEquals("3A6S", pdbe.getId()); // unchanged
1373     assertEquals("A", pdbe.getChainCode()); // updated
1374     assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1375     assertEquals("filepath", pdbe.getFile()); // updated
1376     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1377
1378     // add with a different file path
1379     PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1380     seq.addPDBId(pdbe4);
1381     assertEquals(3, seq.getAllPDBEntries().size());
1382     assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1383
1384     // add with a different chain code
1385     PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1386     seq.addPDBId(pdbe5);
1387     assertEquals(4, seq.getAllPDBEntries().size());
1388     assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1389   }
1390
1391   @Test(
1392     groups = { "Functional" },
1393     expectedExceptions = { IllegalArgumentException.class })
1394   public void testSetDatasetSequence_toSelf()
1395   {
1396     seq.setDatasetSequence(seq);
1397   }
1398
1399   @Test(
1400     groups = { "Functional" },
1401     expectedExceptions = { IllegalArgumentException.class })
1402   public void testSetDatasetSequence_cascading()
1403   {
1404     SequenceI seq2 = new Sequence("Seq2", "xyz");
1405     seq2.createDatasetSequence();
1406     seq.setDatasetSequence(seq2);
1407   }
1408
1409   @Test(groups = { "Functional" })
1410   public void testFindFeatures()
1411   {
1412     SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
1413     sq.createDatasetSequence();
1414
1415     assertTrue(sq.findFeatures(1, 99).isEmpty());
1416
1417     // add non-positional feature
1418     SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
1419             null);
1420     sq.addSequenceFeature(sf0);
1421     // add feature on BCD
1422     SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
1423             null);
1424     sq.addSequenceFeature(sfBCD);
1425     // add feature on DE
1426     SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
1427             null);
1428     sq.addSequenceFeature(sfDE);
1429     // add contact feature at [B, H]
1430     SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond", "desc", 9,
1431             15, 2f,
1432             null);
1433     sq.addSequenceFeature(sfContactBH);
1434     // add contact feature at [F, G]
1435     SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
1436             "desc", 13, 14, 2f, null);
1437     sq.addSequenceFeature(sfContactFG);
1438
1439     // no features in columns 1-2 (-A)
1440     List<SequenceFeature> found = sq.findFeatures(1, 2);
1441     assertTrue(found.isEmpty());
1442
1443     // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
1444     found = sq.findFeatures(1, 6);
1445     assertEquals(2, found.size());
1446     assertTrue(found.contains(sfBCD));
1447     assertTrue(found.contains(sfContactBH));
1448
1449     // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
1450     found = sq.findFeatures(5, 6);
1451     assertEquals(1, found.size());
1452     assertTrue(found.contains(sfBCD));
1453
1454     // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
1455     found = sq.findFeatures(7, 10);
1456     assertEquals(3, found.size());
1457     assertTrue(found.contains(sfBCD));
1458     assertTrue(found.contains(sfDE));
1459     assertTrue(found.contains(sfContactFG));
1460
1461     // columns 10-11 (--) should find nothing
1462     found = sq.findFeatures(10, 11);
1463     assertEquals(0, found.size());
1464   }
1465
1466   @Test(groups = { "Functional" })
1467   public void testFindIndex_withCursor()
1468   {
1469     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1470
1471     // find F given A
1472     assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1473
1474     // find A given F
1475     assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1476
1477     // find C given C
1478     assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1479   }
1480
1481   @Test(groups = { "Functional" })
1482   public void testFindPosition_withCursor()
1483   {
1484     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1485   
1486     // find F pos given A - lastCol gets set in cursor
1487     assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1488     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1489             PA.getValue(sq, "cursor").toString());
1490
1491     // find A pos given F - first residue column is saved in cursor
1492     assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1493     assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
1494             PA.getValue(sq, "cursor").toString());
1495   
1496     // find C pos given C (neither startCol nor endCol is set)
1497     assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1498     assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1499             PA.getValue(sq, "cursor").toString());
1500
1501     // now the grey area - what residue position for a gapped column? JAL-2562
1502
1503     // find 'residue' for column 3 given cursor for D (so working left)
1504     // returns B9; cursor is updated to [B 5]
1505     assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1506     assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
1507             PA.getValue(sq, "cursor").toString());
1508
1509     // find 'residue' for column 8 given cursor for D (so working right)
1510     // returns E12; cursor is updated to [D 7]
1511     assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1512     assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
1513             PA.getValue(sq, "cursor").toString());
1514
1515     // find 'residue' for column 12 given cursor for B
1516     // returns 1 more than last residue position; cursor is updated to [F 10]
1517     // lastCol position is saved in cursor
1518     assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1519     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1520             PA.getValue(sq, "cursor").toString());
1521
1522     /*
1523      * findPosition for column beyond length of sequence
1524      * returns 1 more than the last residue position
1525      * cursor is set to last real residue position [F 10]
1526      */
1527     assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
1528     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1529             PA.getValue(sq, "cursor").toString());
1530
1531     /*
1532      * and the case without a trailing gap
1533      */
1534     sq = new Sequence("test/8-13", "-A--BCD-EF");
1535     // first find C from A
1536     assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
1537     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1538     assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
1539             cursor.toString());
1540     // now 'find' 99 from C
1541     // cursor is set to [F 10] and saved lastCol
1542     assertEquals(14, sq.findPosition(99, cursor));
1543     assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
1544             PA.getValue(sq, "cursor").toString());
1545   }
1546
1547   @Test
1548   public void testIsValidCursor()
1549   {
1550     Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1551     assertFalse(sq.isValidCursor(null));
1552
1553     /*
1554      * cursor is valid if it has valid sequence ref and changeCount token
1555      * and positions within the range of the sequence
1556      */
1557     int changeCount = (int) PA.getValue(sq, "changeCount");
1558     SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
1559     assertTrue(sq.isValidCursor(cursor));
1560
1561     /*
1562      * column position outside [0 - length] is rejected
1563      */
1564     cursor = new SequenceCursor(sq, 13, -1, changeCount);
1565     assertFalse(sq.isValidCursor(cursor));
1566     cursor = new SequenceCursor(sq, 13, 10, changeCount);
1567     assertFalse(sq.isValidCursor(cursor));
1568     cursor = new SequenceCursor(sq, 7, 8, changeCount);
1569     assertFalse(sq.isValidCursor(cursor));
1570     cursor = new SequenceCursor(sq, 14, 2, changeCount);
1571     assertFalse(sq.isValidCursor(cursor));
1572
1573     /*
1574      * wrong sequence is rejected
1575      */
1576     cursor = new SequenceCursor(null, 13, 1, changeCount);
1577     assertFalse(sq.isValidCursor(cursor));
1578     cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
1579             changeCount);
1580     assertFalse(sq.isValidCursor(cursor));
1581
1582     /*
1583      * wrong token value is rejected
1584      */
1585     cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
1586     assertFalse(sq.isValidCursor(cursor));
1587     cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
1588     assertFalse(sq.isValidCursor(cursor));
1589   }
1590
1591   @Test(groups = { "Functional" })
1592   public void testFindPosition_withCursorAndEdits()
1593   {
1594     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1595   
1596     // find F pos given A
1597     assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1598     int token = (int) PA.getValue(sq, "changeCount"); // 0
1599     SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1600     assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
1601
1602     /*
1603      * setSequence should invalidate the cursor cached by the sequence
1604      */
1605     sq.setSequence("-A-BCD-EF---"); // one gap removed
1606     assertEquals(8, sq.getStart()); // sanity check
1607     assertEquals(11, sq.findPosition(5)); // D11
1608     // cursor should now be at [D 6]
1609     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1610     assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
1611     assertEquals(0, cursor.lastResidueColumn); // not yet found
1612     assertEquals(13, sq.findPosition(8)); // E13
1613     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1614     assertEquals(9, cursor.lastResidueColumn); // found
1615
1616     /*
1617      * deleteChars should invalidate the cached cursor
1618      */
1619     sq.deleteChars(2, 5); // delete -BC
1620     assertEquals("-AD-EF---", sq.getSequenceAsString());
1621     assertEquals(8, sq.getStart()); // sanity check
1622     assertEquals(10, sq.findPosition(4)); // E10
1623     // cursor should now be at [E 5]
1624     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1625     assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
1626
1627     /*
1628      * Edit to insert gaps should invalidate the cached cursor
1629      * insert 2 gaps at column[3] to make -AD---EF---
1630      */
1631     SequenceI[] seqs = new SequenceI[] { sq };
1632     AlignmentI al = new Alignment(seqs);
1633     new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
1634     assertEquals("-AD---EF---", sq.getSequenceAsString());
1635     assertEquals(10, sq.findPosition(4)); // E10
1636     // cursor should now be at [D 3]
1637     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1638     assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
1639
1640     /*
1641      * insertCharAt should invalidate the cached cursor
1642      * insert CC at column[4] to make -AD-CC--EF---
1643      */
1644     sq.insertCharAt(4, 2, 'C');
1645     assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
1646     assertEquals(13, sq.findPosition(9)); // F13
1647     // cursor should now be at [F 10]
1648     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
1649     assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
1650   }
1651
1652   @Test(groups = { "Functional" })
1653   public void testGetSequence()
1654   {
1655     String seqstring = "-A--BCD-EF--";
1656     Sequence sq = new Sequence("test/8-13", seqstring);
1657     sq.createDatasetSequence();
1658     assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
1659     assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
1660             "ABCDEF".toCharArray()));
1661
1662     // verify a copy of the sequence array is returned
1663     char[] theSeq = (char[]) PA.getValue(sq, "sequence");
1664     assertNotSame(theSeq, sq.getSequence());
1665     theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
1666     assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
1667   }
1668
1669   @Test(groups = { "Functional" })
1670   public void testReplace()
1671   {
1672     String seqstring = "-A--BCD-EF--";
1673     SequenceI sq = new Sequence("test/8-13", seqstring);
1674     assertEquals(0, PA.getValue(sq, "changeCount"));
1675
1676     assertEquals(0, sq.replace('A', 'A')); // same char
1677     assertEquals(seqstring, sq.getSequenceAsString());
1678     assertEquals(0, PA.getValue(sq, "changeCount"));
1679
1680     assertEquals(0, sq.replace('X', 'Y')); // not there
1681     assertEquals(seqstring, sq.getSequenceAsString());
1682     assertEquals(0, PA.getValue(sq, "changeCount"));
1683
1684     assertEquals(1, sq.replace('A', 'K'));
1685     assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
1686     assertEquals(1, PA.getValue(sq, "changeCount"));
1687
1688     assertEquals(6, sq.replace('-', '.'));
1689     assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
1690     assertEquals(2, PA.getValue(sq, "changeCount"));
1691   }
1692 }