JAL-2050 make SequenceFeature.score final, adjust constructor calls
[jalview.git] / test / jalview / datamodel / SequenceTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNotSame;
27 import static org.testng.AssertJUnit.assertNull;
28 import static org.testng.AssertJUnit.assertSame;
29 import static org.testng.AssertJUnit.assertTrue;
30 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
31
32 import jalview.datamodel.PDBEntry.Type;
33 import jalview.gui.JvOptionPane;
34 import jalview.util.MapList;
35
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Vector;
41
42 import junit.extensions.PA;
43
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
48
49 public class SequenceTest
50 {
51
52   @BeforeClass(alwaysRun = true)
53   public void setUpJvOptionPane()
54   {
55     JvOptionPane.setInteractiveMode(false);
56     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57   }
58
59   Sequence seq;
60
61   @BeforeMethod(alwaysRun = true)
62   public void setUp()
63   {
64     seq = new Sequence("FER1", "AKPNGVL");
65   }
66
67   @Test(groups = { "Functional" })
68   public void testInsertGapsAndGapmaps()
69   {
70     SequenceI aseq = seq.deriveSequence();
71     aseq.insertCharAt(2, 3, '-');
72     aseq.insertCharAt(6, 3, '-');
73     assertEquals("Gap insertions not correct", "AK---P---NGVL",
74             aseq.getSequenceAsString());
75     List<int[]> gapInt = aseq.getInsertions();
76     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
77     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
78     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
79     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
80   }
81
82   @Test(groups = ("Functional"))
83   public void testIsProtein()
84   {
85     // test Protein
86     assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
87     // test DNA
88     assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
89     // test RNA
90     SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
91     assertFalse(sq.isProtein());
92     // change sequence, should trigger an update of cached result
93     sq.setSequence("ASDFASDFADSF");
94     assertTrue(sq.isProtein());
95     /*
96      * in situ change of sequence doesn't change hashcode :-O
97      * (sequence should not expose internal implementation)
98      */
99     for (int i = 0; i < sq.getSequence().length; i++)
100     {
101       sq.getSequence()[i] = "acgtu".charAt(i % 5);
102     }
103     assertTrue(sq.isProtein()); // but it isn't
104   }
105
106   @Test(groups = { "Functional" })
107   public void testGetAnnotation()
108   {
109     // initial state returns null not an empty array
110     assertNull(seq.getAnnotation());
111     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
112             1f);
113     AlignmentAnnotation[] anns = seq.getAnnotation();
114     assertEquals(1, anns.length);
115     assertSame(ann, anns[0]);
116
117     // removing all annotations reverts array to null
118     seq.removeAlignmentAnnotation(ann);
119     assertNull(seq.getAnnotation());
120   }
121
122   @Test(groups = { "Functional" })
123   public void testGetAnnotation_forLabel()
124   {
125     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
126             1f);
127     addAnnotation("label2", "desc2", "calcId2", 1f);
128     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
129             1f);
130     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
131     assertEquals(2, anns.length);
132     assertSame(ann1, anns[0]);
133     assertSame(ann3, anns[1]);
134   }
135
136   private AlignmentAnnotation addAnnotation(String label,
137           String description, String calcId, float value)
138   {
139     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
140             description, value);
141     annotation.setCalcId(calcId);
142     seq.addAlignmentAnnotation(annotation);
143     return annotation;
144   }
145
146   @Test(groups = { "Functional" })
147   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
148   {
149     addAnnotation("label1", "desc1", "calcId1", 1f);
150     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
151             1f);
152     addAnnotation("label2", "desc3", "calcId3", 1f);
153     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
154             1f);
155     addAnnotation("label5", "desc3", null, 1f);
156     addAnnotation(null, "desc3", "calcId3", 1f);
157
158     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
159             "label2");
160     assertEquals(2, anns.size());
161     assertSame(ann2, anns.get(0));
162     assertSame(ann4, anns.get(1));
163
164     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
165     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
166     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
167     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
168     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
169   }
170
171   /**
172    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
173    * setting the sequenceRef on the annotation. Adding the same annotation twice
174    * should be ignored.
175    */
176   @Test(groups = { "Functional" })
177   public void testAddAlignmentAnnotation()
178   {
179     assertNull(seq.getAnnotation());
180     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
181             "b", 2d);
182     assertNull(annotation.sequenceRef);
183     seq.addAlignmentAnnotation(annotation);
184     assertSame(seq, annotation.sequenceRef);
185     AlignmentAnnotation[] anns = seq.getAnnotation();
186     assertEquals(1, anns.length);
187     assertSame(annotation, anns[0]);
188
189     // re-adding does nothing
190     seq.addAlignmentAnnotation(annotation);
191     anns = seq.getAnnotation();
192     assertEquals(1, anns.length);
193     assertSame(annotation, anns[0]);
194
195     // an identical but different annotation can be added
196     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
197             "b", 2d);
198     seq.addAlignmentAnnotation(annotation2);
199     anns = seq.getAnnotation();
200     assertEquals(2, anns.length);
201     assertSame(annotation, anns[0]);
202     assertSame(annotation2, anns[1]);
203   }
204
205   @Test(groups = { "Functional" })
206   public void testGetStartGetEnd()
207   {
208     SequenceI sq = new Sequence("test", "ABCDEF");
209     assertEquals(1, sq.getStart());
210     assertEquals(6, sq.getEnd());
211
212     sq = new Sequence("test", "--AB-C-DEF--");
213     assertEquals(1, sq.getStart());
214     assertEquals(6, sq.getEnd());
215
216     sq = new Sequence("test", "----");
217     assertEquals(1, sq.getStart());
218     assertEquals(0, sq.getEnd()); // ??
219   }
220
221   /**
222    * Tests for the method that returns an alignment column position (base 1) for
223    * a given sequence position (base 1).
224    */
225   @Test(groups = { "Functional" })
226   public void testFindIndex()
227   {
228     SequenceI sq = new Sequence("test", "ABCDEF");
229     assertEquals(0, sq.findIndex(0));
230     assertEquals(1, sq.findIndex(1));
231     assertEquals(5, sq.findIndex(5));
232     assertEquals(6, sq.findIndex(6));
233     assertEquals(6, sq.findIndex(9));
234
235     sq = new Sequence("test", "-A--B-C-D-E-F--");
236     assertEquals(2, sq.findIndex(1));
237     assertEquals(5, sq.findIndex(2));
238     assertEquals(7, sq.findIndex(3));
239
240     // before start returns 0
241     assertEquals(0, sq.findIndex(0));
242     assertEquals(0, sq.findIndex(-1));
243
244     // beyond end returns last residue column
245     assertEquals(13, sq.findIndex(99));
246
247   }
248
249   /**
250    * Tests for the method that returns a dataset sequence position (base 1) for
251    * an aligned column position (base 0).
252    */
253   @Test(groups = { "Functional" })
254   public void testFindPosition()
255   {
256     SequenceI sq = new Sequence("test", "ABCDEF");
257     assertEquals(1, sq.findPosition(0));
258     assertEquals(6, sq.findPosition(5));
259     // assertEquals(-1, seq.findPosition(6)); // fails
260
261     sq = new Sequence("test", "AB-C-D--");
262     assertEquals(1, sq.findPosition(0));
263     assertEquals(2, sq.findPosition(1));
264     // gap position 'finds' residue to the right (not the left as per javadoc)
265     assertEquals(3, sq.findPosition(2));
266     assertEquals(3, sq.findPosition(3));
267     assertEquals(4, sq.findPosition(4));
268     assertEquals(4, sq.findPosition(5));
269     // returns 1 more than sequence length if off the end ?!?
270     assertEquals(5, sq.findPosition(6));
271     assertEquals(5, sq.findPosition(7));
272
273     sq = new Sequence("test", "--AB-C-DEF--");
274     assertEquals(1, sq.findPosition(0));
275     assertEquals(1, sq.findPosition(1));
276     assertEquals(1, sq.findPosition(2));
277     assertEquals(2, sq.findPosition(3));
278     assertEquals(3, sq.findPosition(4));
279     assertEquals(3, sq.findPosition(5));
280     assertEquals(4, sq.findPosition(6));
281     assertEquals(4, sq.findPosition(7));
282     assertEquals(5, sq.findPosition(8));
283     assertEquals(6, sq.findPosition(9));
284     assertEquals(7, sq.findPosition(10));
285     assertEquals(7, sq.findPosition(11));
286   }
287
288   @Test(groups = { "Functional" })
289   public void testDeleteChars()
290   {
291     /*
292      * internal delete
293      */
294     SequenceI sq = new Sequence("test", "ABCDEF");
295     assertNull(PA.getValue(sq, "datasetSequence"));
296     assertEquals(1, sq.getStart());
297     assertEquals(6, sq.getEnd());
298     sq.deleteChars(2, 3);
299     assertEquals("ABDEF", sq.getSequenceAsString());
300     assertEquals(1, sq.getStart());
301     assertEquals(5, sq.getEnd());
302     assertNull(PA.getValue(sq, "datasetSequence"));
303
304     /*
305      * delete at start
306      */
307     sq = new Sequence("test", "ABCDEF");
308     sq.deleteChars(0, 2);
309     assertEquals("CDEF", sq.getSequenceAsString());
310     assertEquals(3, sq.getStart());
311     assertEquals(6, sq.getEnd());
312     assertNull(PA.getValue(sq, "datasetSequence"));
313
314     /*
315      * delete at end
316      */
317     sq = new Sequence("test", "ABCDEF");
318     sq.deleteChars(4, 6);
319     assertEquals("ABCD", sq.getSequenceAsString());
320     assertEquals(1, sq.getStart());
321     assertEquals(4, sq.getEnd());
322     assertNull(PA.getValue(sq, "datasetSequence"));
323   }
324
325   @Test(groups = { "Functional" })
326   public void testDeleteChars_withDbRefsAndFeatures()
327   {
328     /*
329      * internal delete - new dataset sequence created
330      * gets a copy of any dbrefs
331      */
332     SequenceI sq = new Sequence("test", "ABCDEF");
333     sq.createDatasetSequence();
334     DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
335     sq.addDBRef(dbr1);
336     Object ds = PA.getValue(sq, "datasetSequence");
337     assertNotNull(ds);
338     assertEquals(1, sq.getStart());
339     assertEquals(6, sq.getEnd());
340     sq.deleteChars(2, 3);
341     assertEquals("ABDEF", sq.getSequenceAsString());
342     assertEquals(1, sq.getStart());
343     assertEquals(5, sq.getEnd());
344     Object newDs = PA.getValue(sq, "datasetSequence");
345     assertNotNull(newDs);
346     assertNotSame(ds, newDs);
347     assertNotNull(sq.getDBRefs());
348     assertEquals(1, sq.getDBRefs().length);
349     assertNotSame(dbr1, sq.getDBRefs()[0]);
350     assertEquals(dbr1, sq.getDBRefs()[0]);
351
352     /*
353      * internal delete with sequence features
354      * (failure case for JAL-2541)
355      */
356     sq = new Sequence("test", "ABCDEF");
357     sq.createDatasetSequence();
358     SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
359             "CathGroup");
360     sq.addSequenceFeature(sf1);
361     ds = PA.getValue(sq, "datasetSequence");
362     assertNotNull(ds);
363     assertEquals(1, sq.getStart());
364     assertEquals(6, sq.getEnd());
365     sq.deleteChars(2, 4);
366     assertEquals("ABEF", sq.getSequenceAsString());
367     assertEquals(1, sq.getStart());
368     assertEquals(4, sq.getEnd());
369     newDs = PA.getValue(sq, "datasetSequence");
370     assertNotNull(newDs);
371     assertNotSame(ds, newDs);
372     SequenceFeature[] sfs = sq.getSequenceFeatures();
373     assertNotNull(sfs);
374     assertEquals(1, sfs.length);
375     assertNotSame(sf1, sfs[0]);
376     assertEquals(sf1, sfs[0]);
377
378     /*
379      * delete at start - no new dataset sequence created
380      * any sequence features remain as before
381      */
382     sq = new Sequence("test", "ABCDEF");
383     sq.createDatasetSequence();
384     ds = PA.getValue(sq, "datasetSequence");
385     sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
386     sq.addSequenceFeature(sf1);
387     sq.deleteChars(0, 2);
388     assertEquals("CDEF", sq.getSequenceAsString());
389     assertEquals(3, sq.getStart());
390     assertEquals(6, sq.getEnd());
391     assertSame(ds, PA.getValue(sq, "datasetSequence"));
392     sfs = sq.getSequenceFeatures();
393     assertNotNull(sfs);
394     assertEquals(1, sfs.length);
395     assertSame(sf1, sfs[0]);
396
397     /*
398      * delete at end - no new dataset sequence created
399      * any dbrefs remain as before
400      */
401     sq = new Sequence("test", "ABCDEF");
402     sq.createDatasetSequence();
403     ds = PA.getValue(sq, "datasetSequence");
404     dbr1 = new DBRefEntry("Uniprot", "0", "a123");
405     sq.addDBRef(dbr1);
406     sq.deleteChars(4, 6);
407     assertEquals("ABCD", sq.getSequenceAsString());
408     assertEquals(1, sq.getStart());
409     assertEquals(4, sq.getEnd());
410     assertSame(ds, PA.getValue(sq, "datasetSequence"));
411     assertNotNull(sq.getDBRefs());
412     assertEquals(1, sq.getDBRefs().length);
413     assertSame(dbr1, sq.getDBRefs()[0]);
414   }
415
416   @Test(groups = { "Functional" })
417   public void testInsertCharAt()
418   {
419     // non-static methods:
420     SequenceI sq = new Sequence("test", "ABCDEF");
421     sq.insertCharAt(0, 'z');
422     assertEquals("zABCDEF", sq.getSequenceAsString());
423     sq.insertCharAt(2, 2, 'x');
424     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
425
426     // for static method see StringUtilsTest
427   }
428
429   /**
430    * Test the method that returns an array of aligned sequence positions where
431    * the array index is the data sequence position (both base 0).
432    */
433   @Test(groups = { "Functional" })
434   public void testGapMap()
435   {
436     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
437     sq.createDatasetSequence();
438     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
439   }
440
441   /**
442    * Test the method that gets sequence features, either from the sequence or
443    * its dataset.
444    */
445   @Test(groups = { "Functional" })
446   public void testGetSequenceFeatures()
447   {
448     SequenceI sq = new Sequence("test", "GATCAT");
449     sq.createDatasetSequence();
450
451     assertNull(sq.getSequenceFeatures());
452
453     /*
454      * SequenceFeature on sequence
455      */
456     SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
457     sq.addSequenceFeature(sf);
458     SequenceFeature[] sfs = sq.getSequenceFeatures();
459     assertEquals(1, sfs.length);
460     assertSame(sf, sfs[0]);
461
462     /*
463      * SequenceFeature on sequence and dataset sequence; returns that on
464      * sequence
465      * 
466      * Note JAL-2046: spurious: we have no use case for this at the moment.
467      * This test also buggy - as sf2.equals(sf), no new feature is added
468      */
469     SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
470             null);
471     sq.getDatasetSequence().addSequenceFeature(sf2);
472     sfs = sq.getSequenceFeatures();
473     assertEquals(1, sfs.length);
474     assertSame(sf, sfs[0]);
475
476     /*
477      * SequenceFeature on dataset sequence only
478      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
479      */
480     sq.setSequenceFeatures(null);
481     assertNull(sq.getDatasetSequence().getSequenceFeatures());
482
483     /*
484      * Corrupt case - no SequenceFeature, dataset's dataset is the original
485      * sequence. Test shows no infinite loop results.
486      */
487     sq.getDatasetSequence().setSequenceFeatures(null);
488     /**
489      * is there a usecase for this ? setDatasetSequence should throw an error if
490      * this actually occurs.
491      */
492     try
493     {
494       sq.getDatasetSequence().setDatasetSequence(sq); // loop!
495       Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
496     } catch (IllegalArgumentException e)
497     {
498       // TODO Jalview error/exception class for raising implementation errors
499       assertTrue(e.getMessage().toLowerCase()
500               .contains("implementation error"));
501     }
502     assertNull(sq.getSequenceFeatures());
503   }
504
505   /**
506    * Test the method that returns an array, indexed by sequence position, whose
507    * entries are the residue positions at the sequence position (or to the right
508    * if a gap)
509    */
510   @Test(groups = { "Functional" })
511   public void testFindPositionMap()
512   {
513     /*
514      * Note: Javadoc for findPosition says it returns the residue position to
515      * the left of a gapped position; in fact it returns the position to the
516      * right. Also it returns a non-existent residue position for a gap beyond
517      * the sequence.
518      */
519     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
520     int[] map = sq.findPositionMap();
521     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
522             Arrays.toString(map));
523   }
524
525   /**
526    * Test for getSubsequence
527    */
528   @Test(groups = { "Functional" })
529   public void testGetSubsequence()
530   {
531     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
532     sq.createDatasetSequence();
533
534     // positions are base 0, end position is exclusive
535     SequenceI subseq = sq.getSubSequence(2, 4);
536
537     assertEquals("CD", subseq.getSequenceAsString());
538     // start/end are base 1 positions
539     assertEquals(3, subseq.getStart());
540     assertEquals(4, subseq.getEnd());
541     // subsequence shares the full dataset sequence
542     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
543   }
544
545   /**
546    * test createDatasetSequence behaves to doc
547    */
548   @Test(groups = { "Functional" })
549   public void testCreateDatasetSequence()
550   {
551     SequenceI sq = new Sequence("my", "ASDASD");
552     sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
553             "group"));
554     sq.addDBRef(new DBRefEntry("source", "version", "accession"));
555     assertNull(sq.getDatasetSequence());
556     assertNotNull(PA.getValue(sq, "sequenceFeatures")); // to be removed!
557     assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
558     assertNotNull(PA.getValue(sq, "dbrefs"));
559
560     SequenceI rds = sq.createDatasetSequence();
561     assertNotNull(rds);
562     assertNull(rds.getDatasetSequence());
563     assertSame(sq.getDatasetSequence(), rds);
564
565     // sequence features and dbrefs transferred to dataset sequence
566     assertNull(PA.getValue(sq, "sequenceFeatures"));
567     assertNull(PA.getValue(sq, "sequenceFeatureStore"));
568     assertNull(PA.getValue(sq, "dbrefs"));
569     assertNotNull(PA.getValue(rds, "sequenceFeatures"));
570     assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
571     assertNotNull(PA.getValue(rds, "dbrefs"));
572   }
573
574   /**
575    * Test for deriveSequence applied to a sequence with a dataset
576    */
577   @Test(groups = { "Functional" })
578   public void testDeriveSequence_existingDataset()
579   {
580     Sequence sq = new Sequence("Seq1", "CD");
581     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
582     sq.getDatasetSequence().addSequenceFeature(
583             new SequenceFeature("", "", 1, 2, 0f, null));
584     sq.setStart(3);
585     sq.setEnd(4);
586
587     sq.setDescription("Test sequence description..");
588     sq.setVamsasId("TestVamsasId");
589     sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
590
591     sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
592     sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
593     sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
594     sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
595
596     sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
597     sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
598     sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
599     sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
600
601     // these are the same as ones already added
602     DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
603     DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
604
605     List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
606         pdb2pdb });
607
608     sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
609     sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
610     sq.getDatasetSequence().addDBRef(
611             new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
612     sq.getDatasetSequence().addDBRef(
613             new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
614
615     PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
616     PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
617     PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
618             "filePath/test2");
619     PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
620             "filePath/test2");
621     sq.getDatasetSequence().addPDBId(pdbe1a);
622     sq.getDatasetSequence().addPDBId(pdbe1b);
623     sq.getDatasetSequence().addPDBId(pdbe2a);
624     sq.getDatasetSequence().addPDBId(pdbe2b);
625
626     /*
627      * test we added pdb entries to the dataset sequence
628      */
629     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
630             .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
631             "PDB Entries were not found on dataset sequence.");
632
633     /*
634      * we should recover a pdb entry that is on the dataset sequence via PDBEntry
635      */
636     Assert.assertEquals(pdbe1a,
637             sq.getDatasetSequence().getPDBEntry("1PDB"),
638             "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
639     ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
640     System.out.println(">>>>>> " + sq.getSequenceAsString().length());
641     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
642     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
643     Annotation[] annots = annotsList.toArray(new Annotation[0]);
644     sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
645             "Test annot description", annots));
646     sq.getDatasetSequence().addAlignmentAnnotation(
647             new AlignmentAnnotation("Test annot", "Test annot description",
648                     annots));
649     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
650     Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
651                                                    // sequence
652     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
653     Assert.assertNotNull(sq.getAnnotation());
654     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
655     Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
656                                                                         // as
657                                                                         // sq.getDBRefs()
658     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
659             4);
660     Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
661
662     Sequence derived = (Sequence) sq.deriveSequence();
663
664     Assert.assertEquals(derived.getDescription(),
665             "Test sequence description..");
666     Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
667     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
668     Assert.assertNotNull(derived.getAnnotation());
669     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
670     Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
671     Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
672             .size(), 4);
673     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
674
675     assertEquals("CD", derived.getSequenceAsString());
676     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
677
678     assertNull(sq.sequenceFeatures);
679     assertNull(derived.sequenceFeatures);
680     // derived sequence should access dataset sequence features
681     assertNotNull(sq.getSequenceFeatures());
682     assertArrayEquals(sq.getSequenceFeatures(),
683             derived.getSequenceFeatures());
684
685     /*
686      *  verify we have primary db refs *just* for PDB IDs with associated
687      *  PDBEntry objects
688      */
689
690     assertEquals(primRefs, sq.getPrimaryDBRefs());
691     assertEquals(primRefs, sq.getDatasetSequence().getPrimaryDBRefs());
692
693     assertEquals(sq.getPrimaryDBRefs(), derived.getPrimaryDBRefs());
694
695   }
696
697   /**
698    * Test for deriveSequence applied to an ungapped sequence with no dataset
699    */
700   @Test(groups = { "Functional" })
701   public void testDeriveSequence_noDatasetUngapped()
702   {
703     SequenceI sq = new Sequence("Seq1", "ABCDEF");
704     assertEquals(1, sq.getStart());
705     assertEquals(6, sq.getEnd());
706     SequenceI derived = sq.deriveSequence();
707     assertEquals("ABCDEF", derived.getSequenceAsString());
708     assertEquals("ABCDEF", derived.getDatasetSequence()
709             .getSequenceAsString());
710   }
711
712   /**
713    * Test for deriveSequence applied to a gapped sequence with no dataset
714    */
715   @Test(groups = { "Functional" })
716   public void testDeriveSequence_noDatasetGapped()
717   {
718     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
719     assertEquals(1, sq.getStart());
720     assertEquals(6, sq.getEnd());
721     assertNull(sq.getDatasetSequence());
722     SequenceI derived = sq.deriveSequence();
723     assertEquals("AB-C.D EF", derived.getSequenceAsString());
724     assertEquals("ABCDEF", derived.getDatasetSequence()
725             .getSequenceAsString());
726   }
727
728   @Test(groups = { "Functional" })
729   public void testCopyConstructor_noDataset()
730   {
731     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
732     seq1.setDescription("description");
733     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
734             1.3d));
735     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
736             12.4f, "group"));
737     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
738     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
739
740     SequenceI copy = new Sequence(seq1);
741
742     assertNull(copy.getDatasetSequence());
743
744     verifyCopiedSequence(seq1, copy);
745
746     // copy has a copy of the DBRefEntry
747     // this is murky - DBrefs are only copied for dataset sequences
748     // where the test for 'dataset sequence' is 'dataset is null'
749     // but that doesn't distinguish it from an aligned sequence
750     // which has not yet generated a dataset sequence
751     // NB getDBRef looks inside dataset sequence if not null
752     DBRefEntry[] dbrefs = copy.getDBRefs();
753     assertEquals(1, dbrefs.length);
754     assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
755     assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
756   }
757
758   @Test(groups = { "Functional" })
759   public void testCopyConstructor_withDataset()
760   {
761     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
762     seq1.createDatasetSequence();
763     seq1.setDescription("description");
764     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
765             1.3d));
766     // JAL-2046 - what is the contract for using a derived sequence's
767     // addSequenceFeature ?
768     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
769             12.4f, "group"));
770     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
771     // here we add DBRef to the dataset sequence:
772     seq1.getDatasetSequence().addDBRef(
773             new DBRefEntry("EMBL", "1.2", "AZ12345"));
774
775     SequenceI copy = new Sequence(seq1);
776
777     assertNotNull(copy.getDatasetSequence());
778     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
779
780     verifyCopiedSequence(seq1, copy);
781
782     // getDBRef looks inside dataset sequence and this is shared,
783     // so holds the same dbref objects
784     DBRefEntry[] dbrefs = copy.getDBRefs();
785     assertEquals(1, dbrefs.length);
786     assertSame(dbrefs[0], seq1.getDBRefs()[0]);
787   }
788
789   /**
790    * Helper to make assertions about a copied sequence
791    * 
792    * @param seq1
793    * @param copy
794    */
795   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
796   {
797     // verify basic properties:
798     assertEquals(copy.getName(), seq1.getName());
799     assertEquals(copy.getDescription(), seq1.getDescription());
800     assertEquals(copy.getStart(), seq1.getStart());
801     assertEquals(copy.getEnd(), seq1.getEnd());
802     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
803
804     // copy has a copy of the annotation:
805     AlignmentAnnotation[] anns = copy.getAnnotation();
806     assertEquals(1, anns.length);
807     assertFalse(anns[0] == seq1.getAnnotation()[0]);
808     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
809     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
810     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
811
812     // copy has a copy of the sequence feature:
813     SequenceFeature[] sfs = copy.getSequenceFeatures();
814     assertEquals(1, sfs.length);
815     if (seq1.getDatasetSequence() != null
816             && copy.getDatasetSequence() == seq1.getDatasetSequence())
817     {
818       assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
819     }
820     else
821     {
822       assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
823     }
824     assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
825
826     // copy has a copy of the PDB entry
827     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
828     assertEquals(1, pdbs.size());
829     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
830     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
831   }
832
833   @Test(groups = "Functional")
834   public void testGetCharAt()
835   {
836     SequenceI sq = new Sequence("", "abcde");
837     assertEquals('a', sq.getCharAt(0));
838     assertEquals('e', sq.getCharAt(4));
839     assertEquals(' ', sq.getCharAt(5));
840     assertEquals(' ', sq.getCharAt(-1));
841   }
842
843   @Test(groups = { "Functional" })
844   public void testAddSequenceFeatures()
845   {
846     SequenceI sq = new Sequence("", "abcde");
847     // type may not be null
848     assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
849             8, 0f, null)));
850     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
851             8, 0f, null)));
852     // can't add a duplicate feature
853     assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
854             4, 8, 0f, null)));
855     // can add a different feature
856     assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
857             8, 0f, null))); // different type
858     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
859             "description", 4, 8, 0f, null)));// different description
860     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
861             8, 0f, null))); // different start position
862     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
863             9, 0f, null))); // different end position
864     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
865             8, 1f, null))); // different score
866     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
867             8, Float.NaN, null))); // score NaN
868     assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
869             8, 0f, "Metal"))); // different group
870     assertEquals(8, sq.getFeatures().getAllFeatures().size());
871   }
872
873   /**
874    * Tests for adding (or updating) dbrefs
875    * 
876    * @see DBRefEntry#updateFrom(DBRefEntry)
877    */
878   @Test(groups = { "Functional" })
879   public void testAddDBRef()
880   {
881     SequenceI sq = new Sequence("", "abcde");
882     assertNull(sq.getDBRefs());
883     DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
884     sq.addDBRef(dbref);
885     assertEquals(1, sq.getDBRefs().length);
886     assertSame(dbref, sq.getDBRefs()[0]);
887
888     /*
889      * change of version - new entry
890      */
891     DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
892     sq.addDBRef(dbref2);
893     assertEquals(2, sq.getDBRefs().length);
894     assertSame(dbref, sq.getDBRefs()[0]);
895     assertSame(dbref2, sq.getDBRefs()[1]);
896
897     /*
898      * matches existing entry - not added
899      */
900     sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
901     assertEquals(2, sq.getDBRefs().length);
902
903     /*
904      * different source = new entry
905      */
906     DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
907     sq.addDBRef(dbref3);
908     assertEquals(3, sq.getDBRefs().length);
909     assertSame(dbref3, sq.getDBRefs()[2]);
910
911     /*
912      * different ref = new entry
913      */
914     DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
915     sq.addDBRef(dbref4);
916     assertEquals(4, sq.getDBRefs().length);
917     assertSame(dbref4, sq.getDBRefs()[3]);
918
919     /*
920      * matching ref with a mapping - map updated
921      */
922     DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
923     Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
924         1, 1 }, 3, 1));
925     dbref5.setMap(map);
926     sq.addDBRef(dbref5);
927     assertEquals(4, sq.getDBRefs().length);
928     assertSame(dbref4, sq.getDBRefs()[3]);
929     assertSame(map, dbref4.getMap());
930
931     /*
932      * 'real' version replaces "0" version
933      */
934     dbref2.setVersion("0");
935     DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
936             dbref2.getAccessionId());
937     sq.addDBRef(dbref6);
938     assertEquals(4, sq.getDBRefs().length);
939     assertSame(dbref2, sq.getDBRefs()[1]);
940     assertEquals("3", dbref2.getVersion());
941
942     /*
943      * 'real' version replaces "source:0" version
944      */
945     dbref3.setVersion("Uniprot:0");
946     DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
947             dbref3.getAccessionId());
948     sq.addDBRef(dbref7);
949     assertEquals(4, sq.getDBRefs().length);
950     assertSame(dbref3, sq.getDBRefs()[2]);
951     assertEquals("3", dbref2.getVersion());
952   }
953
954   @Test(groups = { "Functional" })
955   public void testGetPrimaryDBRefs_peptide()
956   {
957     SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
958
959     // no dbrefs
960     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
961     assertTrue(primaryDBRefs.isEmpty());
962
963     // empty dbrefs
964     sq.setDBRefs(new DBRefEntry[] {});
965     primaryDBRefs = sq.getPrimaryDBRefs();
966     assertTrue(primaryDBRefs.isEmpty());
967
968     // primary - uniprot
969     DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
970     sq.addDBRef(upentry1);
971
972     // primary - uniprot with congruent map
973     DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
974     upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
975             new int[] { 10, 22 }, 1, 1)));
976     sq.addDBRef(upentry2);
977
978     // primary - uniprot with map of enclosing sequence
979     DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
980     upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
981             new int[] { 8, 24 }, 1, 1)));
982     sq.addDBRef(upentry3);
983
984     // not primary - uniprot with map of sub-sequence (5')
985     DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
986     upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
987             new int[] { 10, 18 }, 1, 1)));
988     sq.addDBRef(upentry4);
989
990     // not primary - uniprot with map that overlaps 3'
991     DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
992     upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
993             new int[] { 12, 22 }, 1, 1)));
994     sq.addDBRef(upentry5);
995
996     // not primary - uniprot with map to different coordinates frame
997     DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
998     upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
999             new int[] { 112, 118 }, 1, 1)));
1000     sq.addDBRef(upentry6);
1001
1002     // not primary - dbref to 'non-core' database
1003     DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
1004     sq.addDBRef(upentry7);
1005
1006     // primary - type is PDB
1007     DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
1008     sq.addDBRef(pdbentry);
1009
1010     // not primary - PDBEntry has no file
1011     sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
1012
1013     // not primary - no PDBEntry
1014     sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
1015
1016     // add corroborating PDB entry for primary DBref -
1017     // needs to have a file as well as matching ID
1018     // note PDB ID is not treated as case sensitive
1019     sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
1020             .toString()));
1021
1022     // not valid DBRef - no file..
1023     sq.addPDBId(new PDBEntry("1AAA", null, null, null));
1024
1025     primaryDBRefs = sq.getPrimaryDBRefs();
1026     assertEquals(4, primaryDBRefs.size());
1027     assertTrue("Couldn't find simple primary reference (UNIPROT)",
1028             primaryDBRefs.contains(upentry1));
1029     assertTrue("Couldn't find mapped primary reference (UNIPROT)",
1030             primaryDBRefs.contains(upentry2));
1031     assertTrue("Couldn't find mapped context reference (UNIPROT)",
1032             primaryDBRefs.contains(upentry3));
1033     assertTrue("Couldn't find expected PDB primary reference",
1034             primaryDBRefs.contains(pdbentry));
1035   }
1036
1037   @Test(groups = { "Functional" })
1038   public void testGetPrimaryDBRefs_nucleotide()
1039   {
1040     SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
1041
1042     // primary - Ensembl
1043     DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
1044     sq.addDBRef(dbr1);
1045
1046     // not primary - Ensembl 'transcript' mapping of sub-sequence
1047     DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
1048     dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
1049             new int[] { 1, 11 }, 1, 1)));
1050     sq.addDBRef(dbr2);
1051
1052     // primary - EMBL with congruent map
1053     DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
1054     dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
1055             new int[] { 10, 34 }, 1, 1)));
1056     sq.addDBRef(dbr3);
1057
1058     // not primary - to non-core database
1059     DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
1060     sq.addDBRef(dbr4);
1061
1062     // not primary - to protein
1063     DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
1064     sq.addDBRef(dbr5);
1065
1066     List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
1067     assertEquals(2, primaryDBRefs.size());
1068     assertTrue(primaryDBRefs.contains(dbr1));
1069     assertTrue(primaryDBRefs.contains(dbr3));
1070   }
1071
1072   /**
1073    * Test the method that updates the list of PDBEntry from any new DBRefEntry
1074    * for PDB
1075    */
1076   @Test(groups = { "Functional" })
1077   public void testUpdatePDBIds()
1078   {
1079     PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
1080     seq.addPDBId(pdbe1);
1081     seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
1082     seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
1083     seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
1084     seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
1085     // 7 is not a valid chain code:
1086     seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
1087
1088     seq.updatePDBIds();
1089     List<PDBEntry> pdbIds = seq.getAllPDBEntries();
1090     assertEquals(4, pdbIds.size());
1091     assertSame(pdbe1, pdbIds.get(0));
1092     // chain code got added to 3A6S:
1093     assertEquals("B", pdbe1.getChainCode());
1094     assertEquals("1A70", pdbIds.get(1).getId());
1095     // 4BQGA is parsed into id + chain
1096     assertEquals("4BQG", pdbIds.get(2).getId());
1097     assertEquals("a", pdbIds.get(2).getChainCode());
1098     assertEquals("2GIS7", pdbIds.get(3).getId());
1099     assertNull(pdbIds.get(3).getChainCode());
1100   }
1101
1102   /**
1103    * Test the method that either adds a pdbid or updates an existing one
1104    */
1105   @Test(groups = { "Functional" })
1106   public void testAddPDBId()
1107   {
1108     PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
1109     seq.addPDBId(pdbe);
1110     assertEquals(1, seq.getAllPDBEntries().size());
1111     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1112     assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
1113
1114     // add the same entry
1115     seq.addPDBId(pdbe);
1116     assertEquals(1, seq.getAllPDBEntries().size());
1117     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1118
1119     // add an identical entry
1120     seq.addPDBId(new PDBEntry("3A6S", null, null, null));
1121     assertEquals(1, seq.getAllPDBEntries().size());
1122     assertSame(pdbe, seq.getPDBEntry("3A6S"));
1123
1124     // add a different entry
1125     PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
1126     seq.addPDBId(pdbe2);
1127     assertEquals(2, seq.getAllPDBEntries().size());
1128     assertSame(pdbe, seq.getAllPDBEntries().get(0));
1129     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1130
1131     // update pdbe with chain code, file, type
1132     PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
1133     seq.addPDBId(pdbe3);
1134     assertEquals(2, seq.getAllPDBEntries().size());
1135     assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
1136     assertEquals("3A6S", pdbe.getId()); // unchanged
1137     assertEquals("A", pdbe.getChainCode()); // updated
1138     assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
1139     assertEquals("filepath", pdbe.getFile()); // updated
1140     assertSame(pdbe2, seq.getAllPDBEntries().get(1));
1141
1142     // add with a different file path
1143     PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
1144     seq.addPDBId(pdbe4);
1145     assertEquals(3, seq.getAllPDBEntries().size());
1146     assertSame(pdbe4, seq.getAllPDBEntries().get(2));
1147
1148     // add with a different chain code
1149     PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
1150     seq.addPDBId(pdbe5);
1151     assertEquals(4, seq.getAllPDBEntries().size());
1152     assertSame(pdbe5, seq.getAllPDBEntries().get(3));
1153   }
1154
1155   @Test(
1156     groups = { "Functional" },
1157     expectedExceptions = { IllegalArgumentException.class })
1158   public void testSetDatasetSequence_toSelf()
1159   {
1160     seq.setDatasetSequence(seq);
1161   }
1162
1163   @Test(
1164     groups = { "Functional" },
1165     expectedExceptions = { IllegalArgumentException.class })
1166   public void testSetDatasetSequence_cascading()
1167   {
1168     SequenceI seq2 = new Sequence("Seq2", "xyz");
1169     seq2.createDatasetSequence();
1170     seq.setDatasetSequence(seq2);
1171   }
1172 }