JAL-1712 fixes/tests for Castor binding and 'show flanking regions'
[jalview.git] / test / jalview / datamodel / SequenceTest.java
1 package jalview.datamodel;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
7
8 import java.util.Arrays;
9 import java.util.List;
10
11 import org.junit.Before;
12 import org.junit.Test;
13
14 public class SequenceTest
15 {
16   Sequence seq;
17
18   @Before
19   public void setUp()
20   {
21     seq = new Sequence("FER1", "AKPNGVL");
22   }
23   @Test
24   public void testGetAnnotation()
25   {
26     // initial state returns null not an empty array
27     assertNull(seq.getAnnotation());
28     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
29             1f);
30     AlignmentAnnotation[] anns = seq.getAnnotation();
31     assertEquals(1, anns.length);
32     assertSame(ann, anns[0]);
33
34     // removing all annotations reverts array to null
35     seq.removeAlignmentAnnotation(ann);
36     assertNull(seq.getAnnotation());
37   }
38
39   @Test
40   public void testGetAnnotation_forLabel()
41   {
42     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
43     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
44     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
45     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
46     assertEquals(2, anns.length);
47     assertSame(ann1, anns[0]);
48     assertSame(ann3, anns[1]);
49   }
50
51   private AlignmentAnnotation addAnnotation(String label,
52           String description, String calcId,
53           float value)
54   {
55     final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
56             value);
57     annotation.setCalcId(calcId);
58     seq.addAlignmentAnnotation(annotation);
59     return annotation;
60   }
61
62   @Test
63   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
64   {
65     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
66             1f);
67     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
68             1f);
69     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
70             1f);
71     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
72             1f);
73     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
74             1f);
75     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
76             1f);
77     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
78             "label2");
79     assertEquals(2, anns.size());
80     assertSame(ann2, anns.get(0));
81     assertSame(ann4, anns.get(1));
82     
83     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
84     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
85     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
86     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
87     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
88   }
89
90   /**
91    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
92    * setting the sequenceRef on the annotation. Adding the same annotation twice
93    * should be ignored.
94    */
95   @Test
96   public void testAddAlignmentAnnotation()
97   {
98     assertNull(seq.annotation);
99     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
100             "b", 2d);
101     assertNull(annotation.sequenceRef);
102     seq.addAlignmentAnnotation(annotation);
103     assertSame(seq, annotation.sequenceRef);
104     AlignmentAnnotation[] anns = seq.getAnnotation();
105     assertEquals(1, anns.length);
106     assertSame(annotation, anns[0]);
107
108     // re-adding does nothing
109     seq.addAlignmentAnnotation(annotation);
110     anns = seq.getAnnotation();
111     assertEquals(1, anns.length);
112     assertSame(annotation, anns[0]);
113
114     // an identical but different annotation can be added
115     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
116             "b", 2d);
117     seq.addAlignmentAnnotation(annotation2);
118     anns = seq.getAnnotation();
119     assertEquals(2, anns.length);
120     assertSame(annotation, anns[0]);
121     assertSame(annotation2, anns[1]);
122
123   }
124
125   /**
126    * Test the method that returns an array, indexed by sequence position, whose
127    * entries are the residue positions at the sequence position (or to the right
128    * if a gap)
129    */
130   @Test
131   public void testFindPositionMap()
132   {
133     /*
134      * Note: Javadoc for findPosition says it returns the residue position to
135      * the left of a gapped position; in fact it returns the position to the
136      * right. Also it returns a non-existent residue position for a gap beyond
137      * the sequence.
138      */
139     Sequence seq = new Sequence("TestSeq", "AB.C-D E.");
140     int[] map = seq.findPositionMap();
141     assertEquals(Arrays.toString(new int[]
142     { 1, 2, 3, 3, 4, 4, 5, 5, 6 }), Arrays.toString(map));
143   }
144
145   /**
146    * Test for getSubsequence
147    */
148   @Test
149   public void testGetSubsequence()
150   {
151     SequenceI seq = new Sequence("TestSeq", "ABCDEFG");
152     seq.createDatasetSequence();
153
154     // positions are base 0, end position is exclusive
155     SequenceI subseq = seq.getSubSequence(2, 4);
156
157     assertEquals("CD", subseq.getSequenceAsString());
158     // start/end are base 1 positions
159     assertEquals(3, subseq.getStart());
160     assertEquals(4, subseq.getEnd());
161     // subsequence shares the full dataset sequence
162     assertSame(seq.getDatasetSequence(), subseq.getDatasetSequence());
163   }
164
165   /**
166    * Test for deriveSequence applied to a sequence with a dataset
167    */
168   @Test
169   public void testDeriveSequence_existingDataset()
170   {
171     SequenceI seq = new Sequence("Seq1", "CD");
172     seq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
173     seq.setStart(3);
174     seq.setEnd(4);
175     SequenceI derived = seq.deriveSequence();
176     assertEquals("CD", derived.getSequenceAsString());
177     assertSame(seq.getDatasetSequence(), derived.getDatasetSequence());
178   }
179
180   /**
181    * Test for deriveSequence applied to an ungapped sequence with no dataset
182    */
183   @Test
184   public void testDeriveSequence_noDatasetUngapped()
185   {
186     SequenceI seq = new Sequence("Seq1", "ABCDEF");
187     assertEquals(1, seq.getStart());
188     assertEquals(6, seq.getEnd());
189     SequenceI derived = seq.deriveSequence();
190     assertEquals("ABCDEF", derived.getSequenceAsString());
191     assertEquals("ABCDEF", derived.getDatasetSequence()
192             .getSequenceAsString());
193   }
194
195   /**
196    * Test for deriveSequence applied to a gapped sequence with no dataset
197    */
198   @Test
199   public void testDeriveSequence_noDatasetGapped()
200   {
201     SequenceI seq = new Sequence("Seq1", "AB-C.D EF");
202     assertEquals(1, seq.getStart());
203     assertEquals(6, seq.getEnd());
204     assertNull(seq.getDatasetSequence());
205     SequenceI derived = seq.deriveSequence();
206     assertEquals("AB-C.D EF", derived.getSequenceAsString());
207     assertEquals("ABCDEF", derived.getDatasetSequence()
208             .getSequenceAsString());
209   }
210 }