JAL-2046 ambiguous test for addSequenceFeature when dataset ref is present or absent
[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      */
330     sq.setSequenceFeatures(null);
331     sfs = sq.getSequenceFeatures();
332     assertEquals(1, sfs.length);
333     assertSame(sf2, sfs[0]);
334
335     /*
336      * Corrupt case - no SequenceFeature, dataset's dataset is the original
337      * sequence. Test shows no infinite loop results.
338      */
339     sq.getDatasetSequence().setSequenceFeatures(null);
340     sq.getDatasetSequence().setDatasetSequence(sq); // loop!
341     assertNull(sq.getSequenceFeatures());
342   }
343
344   /**
345    * Test the method that returns an array, indexed by sequence position, whose
346    * entries are the residue positions at the sequence position (or to the right
347    * if a gap)
348    */
349   @Test(groups = { "Functional" })
350   public void testFindPositionMap()
351   {
352     /*
353      * Note: Javadoc for findPosition says it returns the residue position to
354      * the left of a gapped position; in fact it returns the position to the
355      * right. Also it returns a non-existent residue position for a gap beyond
356      * the sequence.
357      */
358     Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
359     int[] map = sq.findPositionMap();
360     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
361             Arrays.toString(map));
362   }
363
364   /**
365    * Test for getSubsequence
366    */
367   @Test(groups = { "Functional" })
368   public void testGetSubsequence()
369   {
370     SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
371     sq.createDatasetSequence();
372
373     // positions are base 0, end position is exclusive
374     SequenceI subseq = sq.getSubSequence(2, 4);
375
376     assertEquals("CD", subseq.getSequenceAsString());
377     // start/end are base 1 positions
378     assertEquals(3, subseq.getStart());
379     assertEquals(4, subseq.getEnd());
380     // subsequence shares the full dataset sequence
381     assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
382   }
383
384   /**
385    * Test for deriveSequence applied to a sequence with a dataset
386    */
387   @Test(groups = { "Functional" })
388   public void testDeriveSequence_existingDataset()
389   {
390     Sequence sq = new Sequence("Seq1", "CD");
391     sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
392     sq.getDatasetSequence().addSequenceFeature(
393             new SequenceFeature("", "", 1, 2, 0f, null));
394     sq.setStart(3);
395     sq.setEnd(4);
396
397     Sequence derived = (Sequence) sq.deriveSequence();
398     assertEquals("CD", derived.getSequenceAsString());
399     assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
400
401     assertNull(sq.sequenceFeatures);
402     assertNull(derived.sequenceFeatures);
403     // derived sequence should access dataset sequence features
404     assertNotNull(sq.getSequenceFeatures());
405     assertArrayEquals(sq.getSequenceFeatures(),
406             derived.getSequenceFeatures());
407   }
408
409   /**
410    * Test for deriveSequence applied to an ungapped sequence with no dataset
411    */
412   @Test(groups = { "Functional" })
413   public void testDeriveSequence_noDatasetUngapped()
414   {
415     SequenceI sq = new Sequence("Seq1", "ABCDEF");
416     assertEquals(1, sq.getStart());
417     assertEquals(6, sq.getEnd());
418     SequenceI derived = sq.deriveSequence();
419     assertEquals("ABCDEF", derived.getSequenceAsString());
420     assertEquals("ABCDEF", derived.getDatasetSequence()
421             .getSequenceAsString());
422   }
423
424   /**
425    * Test for deriveSequence applied to a gapped sequence with no dataset
426    */
427   @Test(groups = { "Functional" })
428   public void testDeriveSequence_noDatasetGapped()
429   {
430     SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
431     assertEquals(1, sq.getStart());
432     assertEquals(6, sq.getEnd());
433     assertNull(sq.getDatasetSequence());
434     SequenceI derived = sq.deriveSequence();
435     assertEquals("AB-C.D EF", derived.getSequenceAsString());
436     assertEquals("ABCDEF", derived.getDatasetSequence()
437             .getSequenceAsString());
438   }
439
440   @Test(groups = { "Functional" })
441   public void testCopyConstructor_noDataset()
442   {
443     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
444     seq1.setDescription("description");
445     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
446             1.3d));
447     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
448             12.4f, "group"));
449     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
450     seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
451     
452     SequenceI copy = new Sequence(seq1);
453
454     assertNull(copy.getDatasetSequence());
455
456     verifyCopiedSequence(seq1, copy);
457
458     // copy has a copy of the DBRefEntry
459     // this is murky - DBrefs are only copied for dataset sequences
460     // where the test for 'dataset sequence' is 'dataset is null'
461     // but that doesn't distinguish it from an aligned sequence
462     // which has not yet generated a dataset sequence
463     // NB getDBRef looks inside dataset sequence if not null
464     DBRefEntry[] dbrefs = copy.getDBRefs();
465     assertEquals(1, dbrefs.length);
466     assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
467     assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
468   }
469
470   @Test(groups = { "Functional" })
471   public void testCopyConstructor_withDataset()
472   {
473     SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
474     seq1.createDatasetSequence();
475     seq1.setDescription("description");
476     seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
477             1.3d));
478     // JAL-2046 - what is the contract for using a derived sequence's
479     // addSequenceFeature ?
480     seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
481             12.4f, "group"));
482     seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
483     // here we add DBRef to the dataset sequence:
484     seq1.getDatasetSequence().addDBRef(
485             new DBRefEntry("EMBL", "1.2", "AZ12345"));
486
487     SequenceI copy = new Sequence(seq1);
488
489     assertNotNull(copy.getDatasetSequence());
490     assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
491
492     verifyCopiedSequence(seq1, copy);
493
494     // getDBRef looks inside dataset sequence and this is shared,
495     // so holds the same dbref objects
496     DBRefEntry[] dbrefs = copy.getDBRefs();
497     assertEquals(1, dbrefs.length);
498     assertSame(dbrefs[0], seq1.getDBRefs()[0]);
499   }
500
501   /**
502    * Helper to make assertions about a copied sequence
503    * 
504    * @param seq1
505    * @param copy
506    */
507   protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
508   {
509     // verify basic properties:
510     assertEquals(copy.getName(), seq1.getName());
511     assertEquals(copy.getDescription(), seq1.getDescription());
512     assertEquals(copy.getStart(), seq1.getStart());
513     assertEquals(copy.getEnd(), seq1.getEnd());
514     assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
515
516     // copy has a copy of the annotation:
517     AlignmentAnnotation[] anns = copy.getAnnotation();
518     assertEquals(1, anns.length);
519     assertFalse(anns[0] == seq1.getAnnotation()[0]);
520     assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
521     assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
522     assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
523
524     // copy has a copy of the sequence feature:
525     SequenceFeature[] sfs = copy.getSequenceFeatures();
526     assertEquals(1, sfs.length);
527     if (seq1.getDatasetSequence()!=null && copy.getDatasetSequence()==seq1.getDatasetSequence()) {
528       assertTrue(sfs[0] == seq1.getSequenceFeatures()[0]);
529     } else {
530       assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
531     }
532     assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
533
534     // copy has a copy of the PDB entry
535     Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
536     assertEquals(1, pdbs.size());
537     assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
538     assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
539   }
540
541   @Test(groups = "Functional")
542   public void testGetCharAt()
543   {
544     SequenceI sq = new Sequence("", "abcde");
545     assertEquals('a', sq.getCharAt(0));
546     assertEquals('e', sq.getCharAt(4));
547     assertEquals(' ', sq.getCharAt(5));
548     assertEquals(' ', sq.getCharAt(-1));
549   }
550 }