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