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