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