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