JAL-2046 RFC about cyclic dataset sequence references - should be disallowed ?
[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
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Vector;
36
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39
40 public class SequenceTest
41 {
42   Sequence seq;
43
44   @BeforeMethod(alwaysRun = true)
45   public void setUp()
46   {
47     seq = new Sequence("FER1", "AKPNGVL");
48   }
49
50   @Test(groups = { "Functional" })
51   public void testInsertGapsAndGapmaps()
52   {
53     SequenceI aseq = seq.deriveSequence();
54     aseq.insertCharAt(2, 3, '-');
55     aseq.insertCharAt(6, 3, '-');
56     assertEquals("Gap insertions not correct", "AK---P---NGVL",
57             aseq.getSequenceAsString());
58     List<int[]> gapInt = aseq.getInsertions();
59     assertEquals("Gap interval 1 start wrong", 2, gapInt.get(0)[0]);
60     assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]);
61     assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]);
62     assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]);
63   }
64
65   @Test(groups = { "Functional" })
66   public void testGetAnnotation()
67   {
68     // initial state returns null not an empty array
69     assertNull(seq.getAnnotation());
70     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
71             1f);
72     AlignmentAnnotation[] anns = seq.getAnnotation();
73     assertEquals(1, anns.length);
74     assertSame(ann, anns[0]);
75
76     // removing all annotations reverts array to null
77     seq.removeAlignmentAnnotation(ann);
78     assertNull(seq.getAnnotation());
79   }
80
81   @Test(groups = { "Functional" })
82   public void testGetAnnotation_forLabel()
83   {
84     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
85             1f);
86     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
87             1f);
88     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3",
89             1f);
90     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
91     assertEquals(2, anns.length);
92     assertSame(ann1, anns[0]);
93     assertSame(ann3, anns[1]);
94   }
95
96   private AlignmentAnnotation addAnnotation(String label,
97           String description, String calcId, float value)
98   {
99     final AlignmentAnnotation annotation = new AlignmentAnnotation(label,
100             description, value);
101     annotation.setCalcId(calcId);
102     seq.addAlignmentAnnotation(annotation);
103     return annotation;
104   }
105
106   @Test(groups = { "Functional" })
107   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
108   {
109     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
110             1f);
111     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
112             1f);
113     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
114             1f);
115     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
116             1f);
117     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null, 1f);
118     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3", 1f);
119     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
120             "label2");
121     assertEquals(2, anns.size());
122     assertSame(ann2, anns.get(0));
123     assertSame(ann4, anns.get(1));
124
125     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
126     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
127     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
128     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
129     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
130   }
131
132   /**
133    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
134    * setting the sequenceRef on the annotation. Adding the same annotation twice
135    * should be ignored.
136    */
137   @Test(groups = { "Functional" })
138   public void testAddAlignmentAnnotation()
139   {
140     assertNull(seq.getAnnotation());
141     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
142             "b", 2d);
143     assertNull(annotation.sequenceRef);
144     seq.addAlignmentAnnotation(annotation);
145     assertSame(seq, annotation.sequenceRef);
146     AlignmentAnnotation[] anns = seq.getAnnotation();
147     assertEquals(1, anns.length);
148     assertSame(annotation, anns[0]);
149
150     // re-adding does nothing
151     seq.addAlignmentAnnotation(annotation);
152     anns = seq.getAnnotation();
153     assertEquals(1, anns.length);
154     assertSame(annotation, anns[0]);
155
156     // an identical but different annotation can be added
157     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
158             "b", 2d);
159     seq.addAlignmentAnnotation(annotation2);
160     anns = seq.getAnnotation();
161     assertEquals(2, anns.length);
162     assertSame(annotation, anns[0]);
163     assertSame(annotation2, anns[1]);
164   }
165
166   @Test(groups = { "Functional" })
167   public void testGetStartGetEnd()
168   {
169     SequenceI sq = new Sequence("test", "ABCDEF");
170     assertEquals(1, sq.getStart());
171     assertEquals(6, sq.getEnd());
172
173     sq = new Sequence("test", "--AB-C-DEF--");
174     assertEquals(1, sq.getStart());
175     assertEquals(6, sq.getEnd());
176
177     sq = new Sequence("test", "----");
178     assertEquals(1, sq.getStart());
179     assertEquals(0, sq.getEnd()); // ??
180   }
181
182   /**
183    * Tests for the method that returns an alignment column position (base 1) for
184    * a given sequence position (base 1).
185    */
186   @Test(groups = { "Functional" })
187   public void testFindIndex()
188   {
189     SequenceI sq = new Sequence("test", "ABCDEF");
190     assertEquals(0, sq.findIndex(0));
191     assertEquals(1, sq.findIndex(1));
192     assertEquals(5, sq.findIndex(5));
193     assertEquals(6, sq.findIndex(6));
194     assertEquals(6, sq.findIndex(9));
195
196     sq = new Sequence("test", "-A--B-C-D-E-F--");
197     assertEquals(2, sq.findIndex(1));
198     assertEquals(5, sq.findIndex(2));
199     assertEquals(7, sq.findIndex(3));
200
201     // before start returns 0
202     assertEquals(0, sq.findIndex(0));
203     assertEquals(0, sq.findIndex(-1));
204
205     // beyond end returns last residue column
206     assertEquals(13, sq.findIndex(99));
207
208   }
209
210   /**
211    * Tests for the method that returns a dataset sequence position (base 1) for
212    * an aligned column position (base 0).
213    */
214   @Test(groups = { "Functional" })
215   public void testFindPosition()
216   {
217     SequenceI sq = new Sequence("test", "ABCDEF");
218     assertEquals(1, sq.findPosition(0));
219     assertEquals(6, sq.findPosition(5));
220     // assertEquals(-1, seq.findPosition(6)); // fails
221
222     sq = new Sequence("test", "AB-C-D--");
223     assertEquals(1, sq.findPosition(0));
224     assertEquals(2, sq.findPosition(1));
225     // gap position 'finds' residue to the right (not the left as per javadoc)
226     assertEquals(3, sq.findPosition(2));
227     assertEquals(3, sq.findPosition(3));
228     assertEquals(4, sq.findPosition(4));
229     assertEquals(4, sq.findPosition(5));
230     // returns 1 more than sequence length if off the end ?!?
231     assertEquals(5, sq.findPosition(6));
232     assertEquals(5, sq.findPosition(7));
233
234     sq = new Sequence("test", "--AB-C-DEF--");
235     assertEquals(1, sq.findPosition(0));
236     assertEquals(1, sq.findPosition(1));
237     assertEquals(1, sq.findPosition(2));
238     assertEquals(2, sq.findPosition(3));
239     assertEquals(3, sq.findPosition(4));
240     assertEquals(3, sq.findPosition(5));
241     assertEquals(4, sq.findPosition(6));
242     assertEquals(4, sq.findPosition(7));
243     assertEquals(5, sq.findPosition(8));
244     assertEquals(6, sq.findPosition(9));
245     assertEquals(7, sq.findPosition(10));
246     assertEquals(7, sq.findPosition(11));
247   }
248
249   @Test(groups = { "Functional" })
250   public void testDeleteChars()
251   {
252     SequenceI sq = new Sequence("test", "ABCDEF");
253     assertEquals(1, sq.getStart());
254     assertEquals(6, sq.getEnd());
255     sq.deleteChars(2, 3);
256     assertEquals("ABDEF", sq.getSequenceAsString());
257     assertEquals(1, sq.getStart());
258     assertEquals(5, sq.getEnd());
259
260     sq = new Sequence("test", "ABCDEF");
261     sq.deleteChars(0, 2);
262     assertEquals("CDEF", sq.getSequenceAsString());
263     assertEquals(3, sq.getStart());
264     assertEquals(6, sq.getEnd());
265   }
266
267   @Test(groups = { "Functional" })
268   public void testInsertCharAt()
269   {
270     // non-static methods:
271     SequenceI sq = new Sequence("test", "ABCDEF");
272     sq.insertCharAt(0, 'z');
273     assertEquals("zABCDEF", sq.getSequenceAsString());
274     sq.insertCharAt(2, 2, 'x');
275     assertEquals("zAxxBCDEF", sq.getSequenceAsString());
276
277     // for static method see StringUtilsTest
278   }
279
280   /**
281    * Test the method that returns an array of aligned sequence positions where
282    * the array index is the data sequence position (both base 0).
283    */
284   @Test(groups = { "Functional" })
285   public void testGapMap()
286   {
287     SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
288     sq.createDatasetSequence();
289     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
290   }
291
292   /**
293    * Test the method that gets sequence features, either from the sequence or
294    * its dataset.
295    */
296   @Test(groups = { "Functional" })
297   public void testGetSequenceFeatures()
298   {
299     SequenceI sq = new Sequence("test", "GATCAT");
300     sq.createDatasetSequence();
301
302     assertNull(sq.getSequenceFeatures());
303
304     /*
305      * SequenceFeature on sequence
306      */
307     SequenceFeature sf = new SequenceFeature();
308     sq.addSequenceFeature(sf);
309     SequenceFeature[] sfs = sq.getSequenceFeatures();
310     assertEquals(1, sfs.length);
311     assertSame(sf, sfs[0]);
312
313
314     /*
315      * SequenceFeature on sequence and dataset sequence; returns that on
316      * sequence
317      * 
318      * Note JAL-2046: spurious: we have no use case for this at the moment.
319      * This test also buggy - as sf2.equals(sf), no new feature is added
320      */
321     SequenceFeature sf2 = new SequenceFeature();
322     sq.getDatasetSequence().addSequenceFeature(sf2);
323     sfs = sq.getSequenceFeatures();
324     assertEquals(1, sfs.length);
325     assertSame(sf, sfs[0]);
326
327     /*
328      * SequenceFeature on dataset sequence only
329      * Note JAL-2046: spurious: we have no use case for setting a non-dataset sequence's feature array to null at the moment.
330      */
331     sq.setSequenceFeatures(null);
332     assertNull(sq.getDatasetSequence().getSequenceFeatures());
333
334     /*
335      * Corrupt case - no SequenceFeature, dataset's dataset is the original
336      * sequence. Test shows no infinite loop results.
337      */
338     sq.getDatasetSequence().setSequenceFeatures(null);
339     /**
340      * is there a usecase for this ? setDatasetSequence should throw an error if
341      * this actually occurs.
342      */
343     sq.getDatasetSequence().setDatasetSequence(sq); // loop!
344     assertNull(sq.getSequenceFeatures());
345   }
346
347   /**
348    * Test the method that returns an array, indexed by sequence position, whose
349    * entries are the residue positions at the sequence position (or to the right
350    * if a gap)
351    */
352   @Test(groups = { "Functional" })
353   public void testFindPositionMap()
354   {
355     /*
356      * Note: Javadoc for findPosition says it returns the residue position to
357      * the left of a gapped position; in fact it returns the position to the
358      * right. Also it returns a non-existent residue position for a gap beyond
359      * the sequence.
360      */
361     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
362     int[] map = sq.findPositionMap();
363     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
364             Arrays.toString(map));
365   }
366
367   /**
368    * Test for getSubsequence
369    */
370   @Test(groups = { "Functional" })
371   public void testGetSubsequence()
372   {
373     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
374     sq.createDatasetSequence();
375
376     // positions are base 0, end position is exclusive
377     SequenceI subseq = sq.getSubSequence(2, 4);
378
379     assertEquals("CD", subseq.getSequenceAsString());
380     // start/end are base 1 positions
381     assertEquals(3, subseq.getStart());
382     assertEquals(4, subseq.getEnd());
383     // subsequence shares the full dataset sequence
384     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
385   }
386
387   /**
388    * Test for deriveSequence applied to a sequence with a dataset
389    */
390   @Test(groups = { "Functional" })
391   public void testDeriveSequence_existingDataset()
392   {
393     Sequence sq = new Sequence("Seq1", "CD");
394     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
395     sq.getDatasetSequence().addSequenceFeature(
396             new SequenceFeature("", "", 1, 2, 0f, null));
397     sq.setStart(3);
398     sq.setEnd(4);
399
400     Sequence derived = (Sequence) sq.deriveSequence();
401     assertEquals("CD", derived.getSequenceAsString());
402     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
403
404     assertNull(sq.sequenceFeatures);
405     assertNull(derived.sequenceFeatures);
406     // derived sequence should access dataset sequence features
407     assertNotNull(sq.getSequenceFeatures());
408     assertArrayEquals(sq.getSequenceFeatures(),
409             derived.getSequenceFeatures());
410   }
411
412   /**
413    * Test for deriveSequence applied to an ungapped sequence with no dataset
414    */
415   @Test(groups = { "Functional" })
416   public void testDeriveSequence_noDatasetUngapped()
417   {
418     SequenceI sq = new Sequence("Seq1", "ABCDEF");
419     assertEquals(1, sq.getStart());
420     assertEquals(6, sq.getEnd());
421     SequenceI derived = sq.deriveSequence();
422     assertEquals("ABCDEF", derived.getSequenceAsString());
423     assertEquals("ABCDEF", derived.getDatasetSequence()
424             .getSequenceAsString());
425   }
426
427   /**
428    * Test for deriveSequence applied to a gapped sequence with no dataset
429    */
430   @Test(groups = { "Functional" })
431   public void testDeriveSequence_noDatasetGapped()
432   {
433     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
434     assertEquals(1, sq.getStart());
435     assertEquals(6, sq.getEnd());
436     assertNull(sq.getDatasetSequence());
437     SequenceI derived = sq.deriveSequence();
438     assertEquals("AB-C.D EF", derived.getSequenceAsString());
439     assertEquals("ABCDEF", derived.getDatasetSequence()
440             .getSequenceAsString());
441   }
442
443   @Test(groups = { "Functional" })
444   public void testCopyConstructor_noDataset()
445   {
446     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
447     seq1.setDescription("description");
448     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
449             1.3d));
450     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
451             12.4f, "group"));
452     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
453     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
454     
455     SequenceI copy = new Sequence(seq1);
456
457     assertNull(copy.getDatasetSequence());
458
459     verifyCopiedSequence(seq1, copy);
460
461     // copy has a copy of the DBRefEntry
462     // this is murky - DBrefs are only copied for dataset sequences
463     // where the test for 'dataset sequence' is 'dataset is null'
464     // but that doesn't distinguish it from an aligned sequence
465     // which has not yet generated a dataset sequence
466     // NB getDBRef looks inside dataset sequence if not null
467     DBRefEntry[] dbrefs = copy.getDBRefs();
468     assertEquals(1, dbrefs.length);
469     assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
470     assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
471   }
472
473   @Test(groups = { "Functional" })
474   public void testCopyConstructor_withDataset()
475   {
476     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
477     seq1.createDatasetSequence();
478     seq1.setDescription("description");
479     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
480             1.3d));
481     // JAL-2046 - what is the contract for using a derived sequence's
482     // addSequenceFeature ?
483     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
484             12.4f, "group"));
485     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
486     // here we add DBRef to the dataset sequence:
487     seq1.getDatasetSequence().addDBRef(
488             new DBRefEntry("EMBL", "1.2", "AZ12345"));
489
490     SequenceI copy = new Sequence(seq1);
491
492     assertNotNull(copy.getDatasetSequence());
493     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
494
495     verifyCopiedSequence(seq1, copy);
496
497     // getDBRef looks inside dataset sequence and this is shared,
498     // so holds the same dbref objects
499     DBRefEntry[] dbrefs = copy.getDBRefs();
500     assertEquals(1, dbrefs.length);
501     assertSame(dbrefs[0], seq1.getDBRefs()[0]);
502   }
503
504   /**
505    * Helper to make assertions about a copied sequence
506    * 
507    * @param seq1
508    * @param copy
509    */
510   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
511   {
512     // verify basic properties:
513     assertEquals(copy.getName(), seq1.getName());
514     assertEquals(copy.getDescription(), seq1.getDescription());
515     assertEquals(copy.getStart(), seq1.getStart());
516     assertEquals(copy.getEnd(), seq1.getEnd());
517     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
518
519     // copy has a copy of the annotation:
520     AlignmentAnnotation[] anns = copy.getAnnotation();
521     assertEquals(1, anns.length);
522     assertFalse(anns[0] == seq1.getAnnotation()[0]);
523     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
524     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
525     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
526
527     // copy has a copy of the sequence feature:
528     SequenceFeature[] sfs = copy.getSequenceFeatures();
529     assertEquals(1, sfs.length);
530     if (seq1.getDatasetSequence()!=null && copy.getDatasetSequence()==seq1.getDatasetSequence()) {
531       assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
532     } else {
533       assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
534     }
535     assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
536
537     // copy has a copy of the PDB entry
538     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
539     assertEquals(1, pdbs.size());
540     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
541     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
542   }
543
544   @Test(groups = "Functional")
545   public void testGetCharAt()
546   {
547     SequenceI sq = new Sequence("", "abcde");
548     assertEquals('a', sq.getCharAt(0));
549     assertEquals('e', sq.getCharAt(4));
550     assertEquals(' ', sq.getCharAt(5));
551     assertEquals(' ', sq.getCharAt(-1));
552   }
553 }