JAL-2110 use shared alignment dataset when copying alignment for split
[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.assertNull;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
29 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
30
31 import jalview.datamodel.PDBEntry.Type;
32 import jalview.util.MapList;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Vector;
38
39 import org.testng.Assert;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 public class SequenceTest
44 {
45   Sequence seq;
46
47   @BeforeMethod(alwaysRun = true)
48   public void setUp()
49   {
50     seq = new Sequence("FER1", "AKPNGVL");
51   }
52
53   @Test(groups = { "Functional" })
54   public void testInsertGapsAndGapmaps()
55   {
56     SequenceI aseq = seq.deriveSequence();
57     aseq.insertCharAt(2, 3, '-');
58     aseq.insertCharAt(6, 3, '-');
59     assertEquals("Gap insertions not correct", "AK---P---NGVL",
60             aseq.getSequenceAsString());
61     List<int[]> gapInt = aseq.getInsertions();
62     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
63     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
64     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
65     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
66   }
67
68   @Test(groups = ("Functional"))
69   public void testIsProtein()
70   {
71     // test Protein
72     assertTrue(new Sequence("prot","ASDFASDFASDF").isProtein());
73     // test DNA
74     assertFalse(new Sequence("prot","ACGTACGTACGT").isProtein());
75     // test RNA
76     SequenceI sq = new Sequence("prot","ACGUACGUACGU");
77     assertFalse(sq.isProtein());
78     // change sequence, should trigger an update of cached result
79     sq.setSequence("ASDFASDFADSF");
80     assertTrue(sq.isProtein());
81
82     /*
83      * in situ change of sequence doesn't change hashcode :-O
84      * (sequence should not expose internal implementation)
85      */
86     for (int i = 0; i < sq.getSequence().length; i++)
87     {
88       sq.getSequence()[i] = "acgtu".charAt(i % 5);
89     }
90     assertTrue(sq.isProtein()); // but it isn't
91   }
92
93   @Test(groups = { "Functional" })
94   public void testGetAnnotation()
95   {
96     // initial state returns null not an empty array
97     assertNull(seq.getAnnotation());
98     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
99             1f);
100     AlignmentAnnotation[] anns = seq.getAnnotation();
101     assertEquals(1, anns.length);
102     assertSame(ann, anns[0]);
103
104     // removing all annotations reverts array to null
105     seq.removeAlignmentAnnotation(ann);
106     assertNull(seq.getAnnotation());
107   }
108
109   @Test(groups = { "Functional" })
110   public void testGetAnnotation_forLabel()
111   {
112     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
113             1f);
114     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
115             1f);
116     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
117             1f);
118     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
119     assertEquals(2, anns.length);
120     assertSame(ann1, anns[0]);
121     assertSame(ann3, anns[1]);
122   }
123
124   private AlignmentAnnotation addAnnotation(String label,
125           String description, String calcId, float value)
126   {
127     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
128             description, value);
129     annotation.setCalcId(calcId);
130     seq.addAlignmentAnnotation(annotation);
131     return annotation;
132   }
133
134   @Test(groups = { "Functional" })
135   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
136   {
137     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
138             1f);
139     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
140             1f);
141     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
142             1f);
143     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
144             1f);
145     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null, 1f);
146     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3", 1f);
147     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
148             "label2");
149     assertEquals(2, anns.size());
150     assertSame(ann2, anns.get(0));
151     assertSame(ann4, anns.get(1));
152
153     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
154     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
155     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
156     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
157     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
158   }
159
160   /**
161    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
162    * setting the sequenceRef on the annotation. Adding the same annotation twice
163    * should be ignored.
164    */
165   @Test(groups = { "Functional" })
166   public void testAddAlignmentAnnotation()
167   {
168     assertNull(seq.getAnnotation());
169     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
170             "b", 2d);
171     assertNull(annotation.sequenceRef);
172     seq.addAlignmentAnnotation(annotation);
173     assertSame(seq, annotation.sequenceRef);
174     AlignmentAnnotation[] anns = seq.getAnnotation();
175     assertEquals(1, anns.length);
176     assertSame(annotation, anns[0]);
177
178     // re-adding does nothing
179     seq.addAlignmentAnnotation(annotation);
180     anns = seq.getAnnotation();
181     assertEquals(1, anns.length);
182     assertSame(annotation, anns[0]);
183
184     // an identical but different annotation can be added
185     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
186             "b", 2d);
187     seq.addAlignmentAnnotation(annotation2);
188     anns = seq.getAnnotation();
189     assertEquals(2, anns.length);
190     assertSame(annotation, anns[0]);
191     assertSame(annotation2, anns[1]);
192   }
193
194   @Test(groups = { "Functional" })
195   public void testGetStartGetEnd()
196   {
197     SequenceI sq = new Sequence("test", "ABCDEF");
198     assertEquals(1, sq.getStart());
199     assertEquals(6, sq.getEnd());
200
201     sq = new Sequence("test", "--AB-C-DEF--");
202     assertEquals(1, sq.getStart());
203     assertEquals(6, sq.getEnd());
204
205     sq = new Sequence("test", "----");
206     assertEquals(1, sq.getStart());
207     assertEquals(0, sq.getEnd()); // ??
208   }
209
210   /**
211    * Tests for the method that returns an alignment column position (base 1) for
212    * a given sequence position (base 1).
213    */
214   @Test(groups = { "Functional" })
215   public void testFindIndex()
216   {
217     SequenceI sq = new Sequence("test", "ABCDEF");
218     assertEquals(0, sq.findIndex(0));
219     assertEquals(1, sq.findIndex(1));
220     assertEquals(5, sq.findIndex(5));
221     assertEquals(6, sq.findIndex(6));
222     assertEquals(6, sq.findIndex(9));
223
224     sq = new Sequence("test", "-A--B-C-D-E-F--");
225     assertEquals(2, sq.findIndex(1));
226     assertEquals(5, sq.findIndex(2));
227     assertEquals(7, sq.findIndex(3));
228
229     // before start returns 0
230     assertEquals(0, sq.findIndex(0));
231     assertEquals(0, sq.findIndex(-1));
232
233     // beyond end returns last residue column
234     assertEquals(13, sq.findIndex(99));
235
236   }
237
238   /**
239    * Tests for the method that returns a dataset sequence position (base 1) for
240    * an aligned column position (base 0).
241    */
242   @Test(groups = { "Functional" })
243   public void testFindPosition()
244   {
245     SequenceI sq = new Sequence("test", "ABCDEF");
246     assertEquals(1, sq.findPosition(0));
247     assertEquals(6, sq.findPosition(5));
248     // assertEquals(-1, seq.findPosition(6)); // fails
249
250     sq = new Sequence("test", "AB-C-D--");
251     assertEquals(1, sq.findPosition(0));
252     assertEquals(2, sq.findPosition(1));
253     // gap position 'finds' residue to the right (not the left as per javadoc)
254     assertEquals(3, sq.findPosition(2));
255     assertEquals(3, sq.findPosition(3));
256     assertEquals(4, sq.findPosition(4));
257     assertEquals(4, sq.findPosition(5));
258     // returns 1 more than sequence length if off the end ?!?
259     assertEquals(5, sq.findPosition(6));
260     assertEquals(5, sq.findPosition(7));
261
262     sq = new Sequence("test", "--AB-C-DEF--");
263     assertEquals(1, sq.findPosition(0));
264     assertEquals(1, sq.findPosition(1));
265     assertEquals(1, sq.findPosition(2));
266     assertEquals(2, sq.findPosition(3));
267     assertEquals(3, sq.findPosition(4));
268     assertEquals(3, sq.findPosition(5));
269     assertEquals(4, sq.findPosition(6));
270     assertEquals(4, sq.findPosition(7));
271     assertEquals(5, sq.findPosition(8));
272     assertEquals(6, sq.findPosition(9));
273     assertEquals(7, sq.findPosition(10));
274     assertEquals(7, sq.findPosition(11));
275   }
276
277   @Test(groups = { "Functional" })
278   public void testDeleteChars()
279   {
280     SequenceI sq = new Sequence("test", "ABCDEF");
281     assertEquals(1, sq.getStart());
282     assertEquals(6, sq.getEnd());
283     sq.deleteChars(2, 3);
284     assertEquals("ABDEF", sq.getSequenceAsString());
285     assertEquals(1, sq.getStart());
286     assertEquals(5, sq.getEnd());
287
288     sq = new Sequence("test", "ABCDEF");
289     sq.deleteChars(0, 2);
290     assertEquals("CDEF", sq.getSequenceAsString());
291     assertEquals(3, sq.getStart());
292     assertEquals(6, sq.getEnd());
293   }
294
295   @Test(groups = { "Functional" })
296   public void testInsertCharAt()
297   {
298     // non-static methods:
299     SequenceI sq = new Sequence("test", "ABCDEF");
300     sq.insertCharAt(0, 'z');
301     assertEquals("zABCDEF", sq.getSequenceAsString());
302     sq.insertCharAt(2, 2, 'x');
303     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
304
305     // for static method see StringUtilsTest
306   }
307
308   /**
309    * Test the method that returns an array of aligned sequence positions where
310    * the array index is the data sequence position (both base 0).
311    */
312   @Test(groups = { "Functional" })
313   public void testGapMap()
314   {
315     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
316     sq.createDatasetSequence();
317     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
318   }
319
320   /**
321    * Test the method that gets sequence features, either from the sequence or
322    * its dataset.
323    */
324   @Test(groups = { "Functional" })
325   public void testGetSequenceFeatures()
326   {
327     SequenceI sq = new Sequence("test", "GATCAT");
328     sq.createDatasetSequence();
329
330     assertNull(sq.getSequenceFeatures());
331
332     /*
333      * SequenceFeature on sequence
334      */
335     SequenceFeature sf = new SequenceFeature();
336     sq.addSequenceFeature(sf);
337     SequenceFeature[] sfs = sq.getSequenceFeatures();
338     assertEquals(1, sfs.length);
339     assertSame(sf, sfs[0]);
340
341
342     /*
343      * SequenceFeature on sequence and dataset sequence; returns that on
344      * sequence
345      * 
346      * Note JAL-2046: spurious: we have no use case for this at the moment.
347      * This test also buggy - as sf2.equals(sf), no new feature is added
348      */
349     SequenceFeature sf2 = new SequenceFeature();
350     sq.getDatasetSequence().addSequenceFeature(sf2);
351     sfs = sq.getSequenceFeatures();
352     assertEquals(1, sfs.length);
353     assertSame(sf, sfs[0]);
354
355     /*
356      * SequenceFeature on dataset sequence only
357      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
358      */
359     sq.setSequenceFeatures(null);
360     assertNull(sq.getDatasetSequence().getSequenceFeatures());
361
362     /*
363      * Corrupt case - no SequenceFeature, dataset's dataset is the original
364      * sequence. Test shows no infinite loop results.
365      */
366     sq.getDatasetSequence().setSequenceFeatures(null);
367     /**
368      * is there a usecase for this ? setDatasetSequence should throw an error if
369      * this actually occurs.
370      */
371     sq.getDatasetSequence().setDatasetSequence(sq); // loop!
372     assertNull(sq.getSequenceFeatures());
373   }
374
375   /**
376    * Test the method that returns an array, indexed by sequence position, whose
377    * entries are the residue positions at the sequence position (or to the right
378    * if a gap)
379    */
380   @Test(groups = { "Functional" })
381   public void testFindPositionMap()
382   {
383     /*
384      * Note: Javadoc for findPosition says it returns the residue position to
385      * the left of a gapped position; in fact it returns the position to the
386      * right. Also it returns a non-existent residue position for a gap beyond
387      * the sequence.
388      */
389     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
390     int[] map = sq.findPositionMap();
391     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
392             Arrays.toString(map));
393   }
394
395   /**
396    * Test for getSubsequence
397    */
398   @Test(groups = { "Functional" })
399   public void testGetSubsequence()
400   {
401     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
402     sq.createDatasetSequence();
403
404     // positions are base 0, end position is exclusive
405     SequenceI subseq = sq.getSubSequence(2, 4);
406
407     assertEquals("CD", subseq.getSequenceAsString());
408     // start/end are base 1 positions
409     assertEquals(3, subseq.getStart());
410     assertEquals(4, subseq.getEnd());
411     // subsequence shares the full dataset sequence
412     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
413   }
414
415   /**
416    * test createDatasetSequence behaves to doc
417    */
418   @Test(groups = { "Functional" })
419   public void testCreateDatasetSequence()
420   {
421     SequenceI sq = new Sequence("my","ASDASD");
422     assertNull(sq.getDatasetSequence());
423     SequenceI rds = sq.createDatasetSequence();
424     assertNotNull(rds);
425     assertNull(rds.getDatasetSequence());
426     assertEquals(sq.getDatasetSequence(), rds);
427   }
428
429   /**
430    * Test for deriveSequence applied to a sequence with a dataset
431    */
432   @Test(groups = { "Functional" })
433   public void testDeriveSequence_existingDataset()
434   {
435     Sequence sq = new Sequence("Seq1", "CD");
436     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
437     sq.getDatasetSequence().addSequenceFeature(
438             new SequenceFeature("", "", 1, 2, 0f, null));
439     sq.setStart(3);
440     sq.setEnd(4);
441
442     sq.setDescription("Test sequence description..");
443     sq.setVamsasId("TestVamsasId");
444     sq.setSourceDBRef(new DBRefEntry("PDB", "version0", "1TST"));
445
446     sq.addDBRef(new DBRefEntry("PDB", "version1", "1Tst"));
447     sq.addDBRef(new DBRefEntry("PDB", "version2", "2Tst"));
448     sq.addDBRef(new DBRefEntry("PDB", "version3", "3Tst"));
449     sq.addDBRef(new DBRefEntry("PDB", "version4", "4Tst"));
450
451     sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
452     sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
453     sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
454     sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
455
456     sq.getDatasetSequence().addDBRef(
457             new DBRefEntry("PDB", "version1", "1Tst"));
458     sq.getDatasetSequence().addDBRef(
459             new DBRefEntry("PDB", "version2", "2Tst"));
460     sq.getDatasetSequence().addDBRef(
461             new DBRefEntry("PDB", "version3", "3Tst"));
462     sq.getDatasetSequence().addDBRef(
463             new DBRefEntry("PDB", "version4", "4Tst"));
464
465     sq.getDatasetSequence().addPDBId(
466             new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
467     sq.getDatasetSequence().addPDBId(
468             new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
469     sq.getDatasetSequence().addPDBId(
470             new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
471     sq.getDatasetSequence().addPDBId(
472             new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
473
474     ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
475     System.out.println(">>>>>> " + sq.getSequenceAsString().length());
476     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
477     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
478     Annotation[] annots = annotsList.toArray(new Annotation[0]);
479     sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
480             "Test annot description", annots));
481     sq.getDatasetSequence().addAlignmentAnnotation(
482             new AlignmentAnnotation("Test annot", "Test annot description",
483                     annots));
484     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
485     Assert.assertEquals(sq.getDBRefs().length, 4);
486     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
487     Assert.assertNotNull(sq.getAnnotation());
488     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
489     Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 4);
490     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
491             4);
492     Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
493
494     Sequence derived = (Sequence) sq.deriveSequence();
495
496     Assert.assertEquals(derived.getDescription(),
497             "Test sequence description..");
498     Assert.assertEquals(derived.getDBRefs().length, 4);
499     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
500     Assert.assertNotNull(derived.getAnnotation());
501     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
502     Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 4);
503     Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
504             .size(), 4);
505     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
506
507     assertEquals("CD", derived.getSequenceAsString());
508     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
509
510     assertNull(sq.sequenceFeatures);
511     assertNull(derived.sequenceFeatures);
512     // derived sequence should access dataset sequence features
513     assertNotNull(sq.getSequenceFeatures());
514     assertArrayEquals(sq.getSequenceFeatures(),
515             derived.getSequenceFeatures());
516   }
517
518   /**
519    * Test for deriveSequence applied to an ungapped sequence with no dataset
520    */
521   @Test(groups = { "Functional" })
522   public void testDeriveSequence_noDatasetUngapped()
523   {
524     SequenceI sq = new Sequence("Seq1", "ABCDEF");
525     assertEquals(1, sq.getStart());
526     assertEquals(6, sq.getEnd());
527     SequenceI derived = sq.deriveSequence();
528     assertEquals("ABCDEF", derived.getSequenceAsString());
529     assertEquals("ABCDEF", derived.getDatasetSequence()
530             .getSequenceAsString());
531   }
532
533   /**
534    * Test for deriveSequence applied to a gapped sequence with no dataset
535    */
536   @Test(groups = { "Functional" })
537   public void testDeriveSequence_noDatasetGapped()
538   {
539     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
540     assertEquals(1, sq.getStart());
541     assertEquals(6, sq.getEnd());
542     assertNull(sq.getDatasetSequence());
543     SequenceI derived = sq.deriveSequence();
544     assertEquals("AB-C.D EF", derived.getSequenceAsString());
545     assertEquals("ABCDEF", derived.getDatasetSequence()
546             .getSequenceAsString());
547   }
548
549   @Test(groups = { "Functional" })
550   public void testCopyConstructor_noDataset()
551   {
552     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
553     seq1.setDescription("description");
554     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
555             1.3d));
556     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
557             12.4f, "group"));
558     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
559     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
560     
561     SequenceI copy = new Sequence(seq1);
562
563     assertNull(copy.getDatasetSequence());
564
565     verifyCopiedSequence(seq1, copy);
566
567     // copy has a copy of the DBRefEntry
568     // this is murky - DBrefs are only copied for dataset sequences
569     // where the test for 'dataset sequence' is 'dataset is null'
570     // but that doesn't distinguish it from an aligned sequence
571     // which has not yet generated a dataset sequence
572     // NB getDBRef looks inside dataset sequence if not null
573     DBRefEntry[] dbrefs = copy.getDBRefs();
574     assertEquals(1, dbrefs.length);
575     assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
576     assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
577   }
578
579   @Test(groups = { "Functional" })
580   public void testCopyConstructor_withDataset()
581   {
582     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
583     seq1.createDatasetSequence();
584     seq1.setDescription("description");
585     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
586             1.3d));
587     // JAL-2046 - what is the contract for using a derived sequence's
588     // addSequenceFeature ?
589     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
590             12.4f, "group"));
591     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
592     // here we add DBRef to the dataset sequence:
593     seq1.getDatasetSequence().addDBRef(
594             new DBRefEntry("EMBL", "1.2", "AZ12345"));
595
596     SequenceI copy = new Sequence(seq1);
597
598     assertNotNull(copy.getDatasetSequence());
599     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
600
601     verifyCopiedSequence(seq1, copy);
602
603     // getDBRef looks inside dataset sequence and this is shared,
604     // so holds the same dbref objects
605     DBRefEntry[] dbrefs = copy.getDBRefs();
606     assertEquals(1, dbrefs.length);
607     assertSame(dbrefs[0], seq1.getDBRefs()[0]);
608   }
609
610   /**
611    * Helper to make assertions about a copied sequence
612    * 
613    * @param seq1
614    * @param copy
615    */
616   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
617   {
618     // verify basic properties:
619     assertEquals(copy.getName(), seq1.getName());
620     assertEquals(copy.getDescription(), seq1.getDescription());
621     assertEquals(copy.getStart(), seq1.getStart());
622     assertEquals(copy.getEnd(), seq1.getEnd());
623     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
624
625     // copy has a copy of the annotation:
626     AlignmentAnnotation[] anns = copy.getAnnotation();
627     assertEquals(1, anns.length);
628     assertFalse(anns[0] == seq1.getAnnotation()[0]);
629     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
630     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
631     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
632
633     // copy has a copy of the sequence feature:
634     SequenceFeature[] sfs = copy.getSequenceFeatures();
635     assertEquals(1, sfs.length);
636     if (seq1.getDatasetSequence()!=null && copy.getDatasetSequence()==seq1.getDatasetSequence()) {
637       assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
638     } else {
639       assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
640     }
641     assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
642
643     // copy has a copy of the PDB entry
644     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
645     assertEquals(1, pdbs.size());
646     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
647     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
648   }
649
650   @Test(groups = "Functional")
651   public void testGetCharAt()
652   {
653     SequenceI sq = new Sequence("", "abcde");
654     assertEquals('a', sq.getCharAt(0));
655     assertEquals('e', sq.getCharAt(4));
656     assertEquals(' ', sq.getCharAt(5));
657     assertEquals(' ', sq.getCharAt(-1));
658   }
659
660   /**
661    * Tests for adding (or updating) dbrefs
662    * 
663    * @see DBRefEntry#updateFrom(DBRefEntry)
664    */
665   @Test(groups = { "Functional" })
666   public void testAddDBRef()
667   {
668     SequenceI sq = new Sequence("", "abcde");
669     assertNull(sq.getDBRefs());
670     DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
671     sq.addDBRef(dbref);
672     assertEquals(1, sq.getDBRefs().length);
673     assertSame(dbref, sq.getDBRefs()[0]);
674
675     /*
676      * change of version - new entry
677      */
678     DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
679     sq.addDBRef(dbref2);
680     assertEquals(2, sq.getDBRefs().length);
681     assertSame(dbref, sq.getDBRefs()[0]);
682     assertSame(dbref2, sq.getDBRefs()[1]);
683
684     /*
685      * matches existing entry - not added
686      */
687     sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
688     assertEquals(2, sq.getDBRefs().length);
689
690     /*
691      * different source = new entry
692      */
693     DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
694     sq.addDBRef(dbref3);
695     assertEquals(3, sq.getDBRefs().length);
696     assertSame(dbref3, sq.getDBRefs()[2]);
697
698     /*
699      * different ref = new entry
700      */
701     DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
702     sq.addDBRef(dbref4);
703     assertEquals(4, sq.getDBRefs().length);
704     assertSame(dbref4, sq.getDBRefs()[3]);
705
706     /*
707      * matching ref with a mapping - map updated
708      */
709     DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
710     Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
711         1, 1 }, 3, 1));
712     dbref5.setMap(map);
713     sq.addDBRef(dbref5);
714     assertEquals(4, sq.getDBRefs().length);
715     assertSame(dbref4, sq.getDBRefs()[3]);
716     assertSame(map, dbref4.getMap());
717
718     /*
719      * 'real' version replaces "0" version
720      */
721     dbref2.setVersion("0");
722     DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
723             dbref2.getAccessionId());
724     sq.addDBRef(dbref6);
725     assertEquals(4, sq.getDBRefs().length);
726     assertSame(dbref2, sq.getDBRefs()[1]);
727     assertEquals("3", dbref2.getVersion());
728
729     /*
730      * 'real' version replaces "source:0" version
731      */
732     dbref3.setVersion("Uniprot:0");
733     DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
734             dbref3.getAccessionId());
735     sq.addDBRef(dbref7);
736     assertEquals(4, sq.getDBRefs().length);
737     assertSame(dbref3, sq.getDBRefs()[2]);
738     assertEquals("3", dbref2.getVersion());
739   }
740 }