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