JAL-2526 first draft of findIndex/findPosition with SequenceCursor
[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
31 import jalview.datamodel.PDBEntry.Type;
32 import jalview.gui.JvOptionPane;
33 import jalview.util.MapList;
34
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Vector;
40
41 import junit.extensions.PA;
42
43 import org.testng.Assert;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 public class SequenceTest
49 {
50
51   @BeforeClass(alwaysRun = true)
52   public void setUpJvOptionPane()
53   {
54     JvOptionPane.setInteractiveMode(false);
55     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56   }
57
58   Sequence seq;
59
60   @BeforeMethod(alwaysRun = true)
61   public void setUp()
62   {
63     seq = new Sequence("FER1", "AKPNGVL");
64   }
65
66   @Test(groups = { "Functional" })
67   public void testInsertGapsAndGapmaps()
68   {
69     SequenceI aseq = seq.deriveSequence();
70     aseq.insertCharAt(2, 3, '-');
71     aseq.insertCharAt(6, 3, '-');
72     assertEquals("Gap insertions not correct", "AK---P---NGVL",
73             aseq.getSequenceAsString());
74     List<int[]> gapInt = aseq.getInsertions();
75     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
76     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
77     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
78     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
79   }
80
81   @Test(groups = ("Functional"))
82   public void testIsProtein()
83   {
84     // test Protein
85     assertTrue(new Sequence("prot", "ASDFASDFASDF").isProtein());
86     // test DNA
87     assertFalse(new Sequence("prot", "ACGTACGTACGT").isProtein());
88     // test RNA
89     SequenceI sq = new Sequence("prot", "ACGUACGUACGU");
90     assertFalse(sq.isProtein());
91     // change sequence, should trigger an update of cached result
92     sq.setSequence("ASDFASDFADSF");
93     assertTrue(sq.isProtein());
94     /*
95      * in situ change of sequence doesn't change hashcode :-O
96      * (sequence should not expose internal implementation)
97      */
98     for (int i = 0; i < sq.getSequence().length; i++)
99     {
100       sq.getSequence()[i] = "acgtu".charAt(i % 5);
101     }
102     assertTrue(sq.isProtein()); // but it isn't
103   }
104
105   @Test(groups = { "Functional" })
106   public void testGetAnnotation()
107   {
108     // initial state returns null not an empty array
109     assertNull(seq.getAnnotation());
110     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
111             1f);
112     AlignmentAnnotation[] anns = seq.getAnnotation();
113     assertEquals(1, anns.length);
114     assertSame(ann, anns[0]);
115
116     // removing all annotations reverts array to null
117     seq.removeAlignmentAnnotation(ann);
118     assertNull(seq.getAnnotation());
119   }
120
121   @Test(groups = { "Functional" })
122   public void testGetAnnotation_forLabel()
123   {
124     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
125             1f);
126     addAnnotation("label2", "desc2", "calcId2", 1f);
127     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
128             1f);
129     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
130     assertEquals(2, anns.length);
131     assertSame(ann1, anns[0]);
132     assertSame(ann3, anns[1]);
133   }
134
135   private AlignmentAnnotation addAnnotation(String label,
136           String description, String calcId, float value)
137   {
138     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
139             description, value);
140     annotation.setCalcId(calcId);
141     seq.addAlignmentAnnotation(annotation);
142     return annotation;
143   }
144
145   @Test(groups = { "Functional" })
146   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
147   {
148     addAnnotation("label1", "desc1", "calcId1", 1f);
149     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
150             1f);
151     addAnnotation("label2", "desc3", "calcId3", 1f);
152     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
153             1f);
154     addAnnotation("label5", "desc3", null, 1f);
155     addAnnotation(null, "desc3", "calcId3", 1f);
156
157     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
158             "label2");
159     assertEquals(2, anns.size());
160     assertSame(ann2, anns.get(0));
161     assertSame(ann4, anns.get(1));
162
163     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
164     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
165     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
166     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
167     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
168   }
169
170   /**
171    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
172    * setting the sequenceRef on the annotation. Adding the same annotation twice
173    * should be ignored.
174    */
175   @Test(groups = { "Functional" })
176   public void testAddAlignmentAnnotation()
177   {
178     assertNull(seq.getAnnotation());
179     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
180             "b", 2d);
181     assertNull(annotation.sequenceRef);
182     seq.addAlignmentAnnotation(annotation);
183     assertSame(seq, annotation.sequenceRef);
184     AlignmentAnnotation[] anns = seq.getAnnotation();
185     assertEquals(1, anns.length);
186     assertSame(annotation, anns[0]);
187
188     // re-adding does nothing
189     seq.addAlignmentAnnotation(annotation);
190     anns = seq.getAnnotation();
191     assertEquals(1, anns.length);
192     assertSame(annotation, anns[0]);
193
194     // an identical but different annotation can be added
195     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
196             "b", 2d);
197     seq.addAlignmentAnnotation(annotation2);
198     anns = seq.getAnnotation();
199     assertEquals(2, anns.length);
200     assertSame(annotation, anns[0]);
201     assertSame(annotation2, anns[1]);
202   }
203
204   @Test(groups = { "Functional" })
205   public void testGetStartGetEnd()
206   {
207     SequenceI sq = new Sequence("test", "ABCDEF");
208     assertEquals(1, sq.getStart());
209     assertEquals(6, sq.getEnd());
210
211     sq = new Sequence("test", "--AB-C-DEF--");
212     assertEquals(1, sq.getStart());
213     assertEquals(6, sq.getEnd());
214
215     sq = new Sequence("test", "----");
216     assertEquals(1, sq.getStart());
217     assertEquals(0, sq.getEnd()); // ??
218   }
219
220   /**
221    * Tests for the method that returns an alignment column position (base 1) for
222    * a given sequence position (base 1).
223    */
224   @Test(groups = { "Functional" })
225   public void testFindIndex()
226   {
227     SequenceI sq = new Sequence("test", "ABCDEF");
228     assertEquals(0, sq.findIndex(0));
229     PA.setValue(sq, "cursor", null);
230     assertEquals(1, sq.findIndex(1));
231     PA.setValue(sq, "cursor", null);
232     assertEquals(5, sq.findIndex(5));
233     PA.setValue(sq, "cursor", null);
234     assertEquals(6, sq.findIndex(6));
235     PA.setValue(sq, "cursor", null);
236     assertEquals(6, sq.findIndex(9));
237
238     sq = new Sequence("test/8-13", "-A--B-C-D-E-F--");
239     assertEquals(2, sq.findIndex(8));
240     PA.setValue(sq, "cursor", null);
241     assertEquals(5, sq.findIndex(9));
242     PA.setValue(sq, "cursor", null);
243     assertEquals(7, sq.findIndex(10));
244
245     // before start returns 0
246     PA.setValue(sq, "cursor", null);
247     assertEquals(0, sq.findIndex(0));
248     PA.setValue(sq, "cursor", null);
249     assertEquals(0, sq.findIndex(-1));
250
251     // beyond end returns last residue column
252     PA.setValue(sq, "cursor", null);
253     assertEquals(13, sq.findIndex(99));
254   }
255
256   /**
257    * Tests for the method that returns a dataset sequence position (base 1) for
258    * an aligned column position (base 0).
259    */
260   @Test(groups = { "Functional" })
261   public void testFindPosition()
262   {
263     SequenceI sq = new Sequence("test", "ABCDEF");
264     assertEquals(1, sq.findPosition(0));
265     assertEquals(6, sq.findPosition(5));
266     // assertEquals(-1, seq.findPosition(6)); // fails
267
268     sq = new Sequence("test", "AB-C-D--");
269     assertEquals(1, sq.findPosition(0));
270     assertEquals(2, sq.findPosition(1));
271     // gap position 'finds' residue to the right (not the left as per javadoc)
272     assertEquals(3, sq.findPosition(2));
273     assertEquals(3, sq.findPosition(3));
274     assertEquals(4, sq.findPosition(4));
275     assertEquals(4, sq.findPosition(5));
276     // returns 1 more than sequence length if off the end ?!?
277     assertEquals(5, sq.findPosition(6));
278     assertEquals(5, sq.findPosition(7));
279
280     sq = new Sequence("test", "--AB-C-DEF--");
281     assertEquals(1, sq.findPosition(0));
282     assertEquals(1, sq.findPosition(1));
283     assertEquals(1, sq.findPosition(2));
284     assertEquals(2, sq.findPosition(3));
285     assertEquals(3, sq.findPosition(4));
286     assertEquals(3, sq.findPosition(5));
287     assertEquals(4, sq.findPosition(6));
288     assertEquals(4, sq.findPosition(7));
289     assertEquals(5, sq.findPosition(8));
290     assertEquals(6, sq.findPosition(9));
291     assertEquals(7, sq.findPosition(10));
292     assertEquals(7, sq.findPosition(11));
293   }
294
295   @Test(groups = { "Functional" })
296   public void testDeleteChars()
297   {
298     /*
299      * internal delete
300      */
301     SequenceI sq = new Sequence("test", "ABCDEF");
302     assertNull(PA.getValue(sq, "datasetSequence"));
303     assertEquals(1, sq.getStart());
304     assertEquals(6, sq.getEnd());
305     sq.deleteChars(2, 3);
306     assertEquals("ABDEF", sq.getSequenceAsString());
307     assertEquals(1, sq.getStart());
308     assertEquals(5, sq.getEnd());
309     assertNull(PA.getValue(sq, "datasetSequence"));
310
311     /*
312      * delete at start
313      */
314     sq = new Sequence("test", "ABCDEF");
315     sq.deleteChars(0, 2);
316     assertEquals("CDEF", sq.getSequenceAsString());
317     assertEquals(3, sq.getStart());
318     assertEquals(6, sq.getEnd());
319     assertNull(PA.getValue(sq, "datasetSequence"));
320
321     /*
322      * delete at end
323      */
324     sq = new Sequence("test", "ABCDEF");
325     sq.deleteChars(4, 6);
326     assertEquals("ABCD", sq.getSequenceAsString());
327     assertEquals(1, sq.getStart());
328     assertEquals(4, sq.getEnd());
329     assertNull(PA.getValue(sq, "datasetSequence"));
330   }
331
332   @Test(groups = { "Functional" })
333   public void testDeleteChars_withDbRefsAndFeatures()
334   {
335     /*
336      * internal delete - new dataset sequence created
337      * gets a copy of any dbrefs
338      */
339     SequenceI sq = new Sequence("test", "ABCDEF");
340     sq.createDatasetSequence();
341     DBRefEntry dbr1 = new DBRefEntry("Uniprot", "0", "a123");
342     sq.addDBRef(dbr1);
343     Object ds = PA.getValue(sq, "datasetSequence");
344     assertNotNull(ds);
345     assertEquals(1, sq.getStart());
346     assertEquals(6, sq.getEnd());
347     sq.deleteChars(2, 3);
348     assertEquals("ABDEF", sq.getSequenceAsString());
349     assertEquals(1, sq.getStart());
350     assertEquals(5, sq.getEnd());
351     Object newDs = PA.getValue(sq, "datasetSequence");
352     assertNotNull(newDs);
353     assertNotSame(ds, newDs);
354     assertNotNull(sq.getDBRefs());
355     assertEquals(1, sq.getDBRefs().length);
356     assertNotSame(dbr1, sq.getDBRefs()[0]);
357     assertEquals(dbr1, sq.getDBRefs()[0]);
358
359     /*
360      * internal delete with sequence features
361      * (failure case for JAL-2541)
362      */
363     sq = new Sequence("test", "ABCDEF");
364     sq.createDatasetSequence();
365     SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
366             "CathGroup");
367     sq.addSequenceFeature(sf1);
368     ds = PA.getValue(sq, "datasetSequence");
369     assertNotNull(ds);
370     assertEquals(1, sq.getStart());
371     assertEquals(6, sq.getEnd());
372     sq.deleteChars(2, 4);
373     assertEquals("ABEF", sq.getSequenceAsString());
374     assertEquals(1, sq.getStart());
375     assertEquals(4, sq.getEnd());
376     newDs = PA.getValue(sq, "datasetSequence");
377     assertNotNull(newDs);
378     assertNotSame(ds, newDs);
379     List<SequenceFeature> sfs = sq.getSequenceFeatures();
380     assertEquals(1, sfs.size());
381     assertNotSame(sf1, sfs.get(0));
382     assertEquals(sf1, sfs.get(0));
383
384     /*
385      * delete at start - no new dataset sequence created
386      * any sequence features remain as before
387      */
388     sq = new Sequence("test", "ABCDEF");
389     sq.createDatasetSequence();
390     ds = PA.getValue(sq, "datasetSequence");
391     sf1 = new SequenceFeature("Cath", "desc", 2, 4, 2f, "CathGroup");
392     sq.addSequenceFeature(sf1);
393     sq.deleteChars(0, 2);
394     assertEquals("CDEF", sq.getSequenceAsString());
395     assertEquals(3, sq.getStart());
396     assertEquals(6, sq.getEnd());
397     assertSame(ds, PA.getValue(sq, "datasetSequence"));
398     sfs = sq.getSequenceFeatures();
399     assertNotNull(sfs);
400     assertEquals(1, sfs.size());
401     assertSame(sf1, sfs.get(0));
402
403     /*
404      * delete at end - no new dataset sequence created
405      * any dbrefs remain as before
406      */
407     sq = new Sequence("test", "ABCDEF");
408     sq.createDatasetSequence();
409     ds = PA.getValue(sq, "datasetSequence");
410     dbr1 = new DBRefEntry("Uniprot", "0", "a123");
411     sq.addDBRef(dbr1);
412     sq.deleteChars(4, 6);
413     assertEquals("ABCD", sq.getSequenceAsString());
414     assertEquals(1, sq.getStart());
415     assertEquals(4, sq.getEnd());
416     assertSame(ds, PA.getValue(sq, "datasetSequence"));
417     assertNotNull(sq.getDBRefs());
418     assertEquals(1, sq.getDBRefs().length);
419     assertSame(dbr1, sq.getDBRefs()[0]);
420   }
421
422   @Test(groups = { "Functional" })
423   public void testInsertCharAt()
424   {
425     // non-static methods:
426     SequenceI sq = new Sequence("test", "ABCDEF");
427     sq.insertCharAt(0, 'z');
428     assertEquals("zABCDEF", sq.getSequenceAsString());
429     sq.insertCharAt(2, 2, 'x');
430     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
431
432     // for static method see StringUtilsTest
433   }
434
435   /**
436    * Test the method that returns an array of aligned sequence positions where
437    * the array index is the data sequence position (both base 0).
438    */
439   @Test(groups = { "Functional" })
440   public void testGapMap()
441   {
442     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
443     sq.createDatasetSequence();
444     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
445   }
446
447   /**
448    * Test the method that gets sequence features, either from the sequence or
449    * its dataset.
450    */
451   @Test(groups = { "Functional" })
452   public void testGetSequenceFeatures()
453   {
454     SequenceI sq = new Sequence("test", "GATCAT");
455     sq.createDatasetSequence();
456
457     assertTrue(sq.getSequenceFeatures().isEmpty());
458
459     /*
460      * SequenceFeature on sequence
461      */
462     SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 4, 2f, null);
463     sq.addSequenceFeature(sf);
464     List<SequenceFeature> sfs = sq.getSequenceFeatures();
465     assertEquals(1, sfs.size());
466     assertSame(sf, sfs.get(0));
467
468     /*
469      * SequenceFeature on sequence and dataset sequence; returns that on
470      * sequence
471      * 
472      * Note JAL-2046: spurious: we have no use case for this at the moment.
473      * This test also buggy - as sf2.equals(sf), no new feature is added
474      */
475     SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 2, 4, 2f,
476             null);
477     sq.getDatasetSequence().addSequenceFeature(sf2);
478     sfs = sq.getSequenceFeatures();
479     assertEquals(1, sfs.size());
480     assertSame(sf, sfs.get(0));
481
482     /*
483      * SequenceFeature on dataset sequence only
484      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
485      */
486     sq.setSequenceFeatures(null);
487     assertTrue(sq.getDatasetSequence().getSequenceFeatures().isEmpty());
488
489     /*
490      * Corrupt case - no SequenceFeature, dataset's dataset is the original
491      * sequence. Test shows no infinite loop results.
492      */
493     sq.getDatasetSequence().setSequenceFeatures(null);
494     /**
495      * is there a usecase for this ? setDatasetSequence should throw an error if
496      * this actually occurs.
497      */
498     try
499     {
500       sq.getDatasetSequence().setDatasetSequence(sq); // loop!
501       Assert.fail("Expected Error to be raised when calling setDatasetSequence with self reference");
502     } catch (IllegalArgumentException e)
503     {
504       // TODO Jalview error/exception class for raising implementation errors
505       assertTrue(e.getMessage().toLowerCase()
506               .contains("implementation error"));
507     }
508     assertTrue(sq.getSequenceFeatures().isEmpty());
509   }
510
511   /**
512    * Test the method that returns an array, indexed by sequence position, whose
513    * entries are the residue positions at the sequence position (or to the right
514    * if a gap)
515    */
516   @Test(groups = { "Functional" })
517   public void testFindPositionMap()
518   {
519     /*
520      * Note: Javadoc for findPosition says it returns the residue position to
521      * the left of a gapped position; in fact it returns the position to the
522      * right. Also it returns a non-existent residue position for a gap beyond
523      * the sequence.
524      */
525     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
526     int[] map = sq.findPositionMap();
527     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
528             Arrays.toString(map));
529   }
530
531   /**
532    * Test for getSubsequence
533    */
534   @Test(groups = { "Functional" })
535   public void testGetSubsequence()
536   {
537     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
538     sq.createDatasetSequence();
539
540     // positions are base 0, end position is exclusive
541     SequenceI subseq = sq.getSubSequence(2, 4);
542
543     assertEquals("CD", subseq.getSequenceAsString());
544     // start/end are base 1 positions
545     assertEquals(3, subseq.getStart());
546     assertEquals(4, subseq.getEnd());
547     // subsequence shares the full dataset sequence
548     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
549   }
550
551   /**
552    * test createDatasetSequence behaves to doc
553    */
554   @Test(groups = { "Functional" })
555   public void testCreateDatasetSequence()
556   {
557     SequenceI sq = new Sequence("my", "ASDASD");
558     sq.addSequenceFeature(new SequenceFeature("type", "desc", 1, 10, 1f,
559             "group"));
560     sq.addDBRef(new DBRefEntry("source", "version", "accession"));
561     assertNull(sq.getDatasetSequence());
562     assertNotNull(PA.getValue(sq, "sequenceFeatureStore"));
563     assertNotNull(PA.getValue(sq, "dbrefs"));
564
565     SequenceI rds = sq.createDatasetSequence();
566     assertNotNull(rds);
567     assertNull(rds.getDatasetSequence());
568     assertSame(sq.getDatasetSequence(), rds);
569
570     // sequence features and dbrefs transferred to dataset sequence
571     assertNull(PA.getValue(sq, "sequenceFeatureStore"));
572     assertNull(PA.getValue(sq, "dbrefs"));
573     assertNotNull(PA.getValue(rds, "sequenceFeatureStore"));
574     assertNotNull(PA.getValue(rds, "dbrefs"));
575   }
576
577   /**
578    * Test for deriveSequence applied to a sequence with a dataset
579    */
580   @Test(groups = { "Functional" })
581   public void testDeriveSequence_existingDataset()
582   {
583     Sequence sq = new Sequence("Seq1", "CD");
584     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
585     sq.getDatasetSequence().addSequenceFeature(
586             new SequenceFeature("", "", 1, 2, 0f, null));
587     sq.setStart(3);
588     sq.setEnd(4);
589
590     sq.setDescription("Test sequence description..");
591     sq.setVamsasId("TestVamsasId");
592     sq.addDBRef(new DBRefEntry("PDB", "version0", "1TST"));
593
594     sq.addDBRef(new DBRefEntry("PDB", "version1", "1PDB"));
595     sq.addDBRef(new DBRefEntry("PDB", "version2", "2PDB"));
596     sq.addDBRef(new DBRefEntry("PDB", "version3", "3PDB"));
597     sq.addDBRef(new DBRefEntry("PDB", "version4", "4PDB"));
598
599     sq.addPDBId(new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1"));
600     sq.addPDBId(new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1"));
601     sq.addPDBId(new PDBEntry("2PDB", "A", Type.MMCIF, "filePath/test2"));
602     sq.addPDBId(new PDBEntry("2PDB", "B", Type.MMCIF, "filePath/test2"));
603
604     // these are the same as ones already added
605     DBRefEntry pdb1pdb = new DBRefEntry("PDB", "version1", "1PDB");
606     DBRefEntry pdb2pdb = new DBRefEntry("PDB", "version2", "2PDB");
607
608     List<DBRefEntry> primRefs = Arrays.asList(new DBRefEntry[] { pdb1pdb,
609         pdb2pdb });
610
611     sq.getDatasetSequence().addDBRef(pdb1pdb); // should do nothing
612     sq.getDatasetSequence().addDBRef(pdb2pdb); // should do nothing
613     sq.getDatasetSequence().addDBRef(
614             new DBRefEntry("PDB", "version3", "3PDB")); // should do nothing
615     sq.getDatasetSequence().addDBRef(
616             new DBRefEntry("PDB", "version4", "4PDB")); // should do nothing
617
618     PDBEntry pdbe1a = new PDBEntry("1PDB", "A", Type.PDB, "filePath/test1");
619     PDBEntry pdbe1b = new PDBEntry("1PDB", "B", Type.PDB, "filePath/test1");
620     PDBEntry pdbe2a = new PDBEntry("2PDB", "A", Type.MMCIF,
621             "filePath/test2");
622     PDBEntry pdbe2b = new PDBEntry("2PDB", "B", Type.MMCIF,
623             "filePath/test2");
624     sq.getDatasetSequence().addPDBId(pdbe1a);
625     sq.getDatasetSequence().addPDBId(pdbe1b);
626     sq.getDatasetSequence().addPDBId(pdbe2a);
627     sq.getDatasetSequence().addPDBId(pdbe2b);
628
629     /*
630      * test we added pdb entries to the dataset sequence
631      */
632     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries(), Arrays
633             .asList(new PDBEntry[] { pdbe1a, pdbe1b, pdbe2a, pdbe2b }),
634             "PDB Entries were not found on dataset sequence.");
635
636     /*
637      * we should recover a pdb entry that is on the dataset sequence via PDBEntry
638      */
639     Assert.assertEquals(pdbe1a,
640             sq.getDatasetSequence().getPDBEntry("1PDB"),
641             "PDB Entry '1PDB' not found on dataset sequence via getPDBEntry.");
642     ArrayList<Annotation> annotsList = new ArrayList<Annotation>();
643     System.out.println(">>>>>> " + sq.getSequenceAsString().length());
644     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
645     annotsList.add(new Annotation("A", "A", 'X', 0.1f));
646     Annotation[] annots = annotsList.toArray(new Annotation[0]);
647     sq.addAlignmentAnnotation(new AlignmentAnnotation("Test annot",
648             "Test annot description", annots));
649     sq.getDatasetSequence().addAlignmentAnnotation(
650             new AlignmentAnnotation("Test annot", "Test annot description",
651                     annots));
652     Assert.assertEquals(sq.getDescription(), "Test sequence description..");
653     Assert.assertEquals(sq.getDBRefs().length, 5); // DBRefs are on dataset
654                                                    // sequence
655     Assert.assertEquals(sq.getAllPDBEntries().size(), 4);
656     Assert.assertNotNull(sq.getAnnotation());
657     Assert.assertEquals(sq.getAnnotation()[0].annotations.length, 2);
658     Assert.assertEquals(sq.getDatasetSequence().getDBRefs().length, 5); // same
659                                                                         // as
660                                                                         // sq.getDBRefs()
661     Assert.assertEquals(sq.getDatasetSequence().getAllPDBEntries().size(),
662             4);
663     Assert.assertNotNull(sq.getDatasetSequence().getAnnotation());
664
665     Sequence derived = (Sequence) sq.deriveSequence();
666
667     Assert.assertEquals(derived.getDescription(),
668             "Test sequence description..");
669     Assert.assertEquals(derived.getDBRefs().length, 5); // come from dataset
670     Assert.assertEquals(derived.getAllPDBEntries().size(), 4);
671     Assert.assertNotNull(derived.getAnnotation());
672     Assert.assertEquals(derived.getAnnotation()[0].annotations.length, 2);
673     Assert.assertEquals(derived.getDatasetSequence().getDBRefs().length, 5);
674     Assert.assertEquals(derived.getDatasetSequence().getAllPDBEntries()
675             .size(), 4);
676     Assert.assertNotNull(derived.getDatasetSequence().getAnnotation());
677
678     assertEquals("CD", derived.getSequenceAsString());
679     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
680
681     // derived sequence should access dataset sequence features
682     assertNotNull(sq.getSequenceFeatures());
683     assertEquals(sq.getSequenceFeatures(), 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     List<SequenceFeature> sfs = copy.getSequenceFeatures();
814     assertEquals(1, sfs.size());
815     if (seq1.getDatasetSequence() != null
816             && copy.getDatasetSequence() == seq1.getDatasetSequence())
817     {
818       assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
819     }
820     else
821     {
822       assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
823     }
824     assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(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
1173   @Test
1174   public void testFindPositions()
1175   {
1176     SequenceI sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1177
1178     Range range = sq.findPositions(1, 4); // BC
1179     assertEquals(new Range(9, 10), range);
1180
1181     range = sq.findPositions(2, 4); // C
1182     assertEquals(new Range(10, 10), range);
1183
1184     assertNull(sq.findPositions(3, 4)); // all gaps
1185
1186     range = sq.findPositions(2, 6); // CDE
1187     assertEquals(new Range(10, 12), range);
1188
1189     range = sq.findPositions(3, 7); // DE
1190     assertEquals(new Range(11, 12), range);
1191   }
1192
1193   @Test(groups = { "Functional" })
1194   public void testFindIndex_withCursor()
1195   {
1196     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1197
1198     // find F given A
1199     assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
1200
1201     // find A given F
1202     assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
1203
1204     // find C given C
1205     assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
1206   }
1207
1208   @Test(groups = { "Functional" })
1209   public void testFindPosition_withCursor()
1210   {
1211     Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
1212   
1213     // find F pos given A
1214     assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
1215   
1216     // find A pos given F
1217     assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
1218   
1219     // find C pos given C
1220     assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
1221
1222     // now the grey area - what residue position for a gapped column? JAL-2562
1223
1224     // find 'residue' for column 3 given cursor for D (so working left)
1225     assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
1226
1227     // find 'residue' for column 8 given cursor for D (so working right)
1228     assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
1229
1230     // find 'residue' for column 12 given cursor for B
1231     assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
1232   }
1233
1234   @Test
1235   public void testFindPositions_withCursor()
1236   {
1237     Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
1238   
1239     // find positions for columns 1-4 (BC--) given E cursor
1240     Range range = sq.findPositions(1, 4, new SequenceCursor(sq, 12, 7, 0)); // BC
1241     assertEquals(new Range(9, 10), range);
1242
1243     // repeat using B cursor
1244     range = sq.findPositions(1, 4, new SequenceCursor(sq, 9, 2, 0)); // BC
1245     assertEquals(new Range(9, 10), range);
1246   
1247     // find positions for columns 2-4 (C--) given A cursor
1248     range = sq.findPositions(2, 4, new SequenceCursor(sq, 8, 1, 0)); // C
1249     assertEquals(new Range(10, 10), range);
1250   
1251     // gapped region
1252     assertNull(sq.findPositions(3, 4, new SequenceCursor(sq, 10, 3, 0)));
1253     assertNull(sq.findPositions(3, 4, new SequenceCursor(sq, 12, 7, 0)));
1254   
1255     // find positions for columns 2-6 (C--DE) given B cursor
1256     range = sq.findPositions(2, 6, new SequenceCursor(sq, 9, 2, 0)); // CDE
1257     assertEquals(new Range(10, 12), range);
1258
1259     // repeat using C as cursor
1260     range = sq.findPositions(2, 6, new SequenceCursor(sq, 10, 3, 0));
1261     assertEquals(new Range(10, 12), range);
1262
1263     // repeat using D as cursor
1264     range = sq.findPositions(2, 6, new SequenceCursor(sq, 11, 6, 0));
1265     assertEquals(new Range(10, 12), range);
1266
1267     // repeat using E as cursor
1268     range = sq.findPositions(2, 6, new SequenceCursor(sq, 12, 7, 0));
1269     assertEquals(new Range(10, 12), range);
1270
1271     // repeat using F as cursor
1272     range = sq.findPositions(2, 6, new SequenceCursor(sq, 13, 9, 0));
1273     assertEquals(new Range(10, 12), range);
1274   }
1275 }