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