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