3ccf88c9d5426518cd098f96087edc9444133762
[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   @Test
126   public void testGetStartGetEnd()
127   {
128     SequenceI seq = new Sequence("test", "ABCDEF");
129     assertEquals(1, seq.getStart());
130     assertEquals(6, seq.getEnd());
131
132     seq = new Sequence("test", "--AB-C-DEF--");
133     assertEquals(1, seq.getStart());
134     assertEquals(6, seq.getEnd());
135
136     seq = new Sequence("test", "----");
137     assertEquals(1, seq.getStart());
138     assertEquals(0, seq.getEnd()); // ??
139   }
140
141   /**
142    * Tests for the method that returns an alignment column position (base 1) for
143    * a given sequence position (base 1).
144    */
145   @Test
146   public void testFindIndex()
147   {
148     SequenceI seq = new Sequence("test", "ABCDEF");
149     assertEquals(0, seq.findIndex(0));
150     assertEquals(1, seq.findIndex(1));
151     assertEquals(5, seq.findIndex(5));
152     assertEquals(6, seq.findIndex(6));
153     assertEquals(6, seq.findIndex(9));
154
155     seq = new Sequence("test", "-A--B-C-D-E-F--");
156     assertEquals(2, seq.findIndex(1));
157     assertEquals(5, seq.findIndex(2));
158     assertEquals(7, seq.findIndex(3));
159
160     // before start returns 0
161     assertEquals(0, seq.findIndex(0));
162     assertEquals(0, seq.findIndex(-1));
163
164     // beyond end returns last residue column
165     assertEquals(13, seq.findIndex(99));
166
167   }
168
169   /**
170    * Tests for the method that returns a dataset sequence position (base 1) for
171    * an aligned column position (base 0).
172    */
173   @Test
174   public void testFindPosition()
175   {
176     SequenceI seq = new Sequence("test", "ABCDEF");
177     assertEquals(1, seq.findPosition(0));
178     assertEquals(6, seq.findPosition(5));
179     // assertEquals(-1, seq.findPosition(6)); // fails
180
181     seq = new Sequence("test", "AB-C-D--");
182     assertEquals(1, seq.findPosition(0));
183     assertEquals(2, seq.findPosition(1));
184     // gap position 'finds' residue to the right (not the left as per javadoc)
185     assertEquals(3, seq.findPosition(2));
186     assertEquals(3, seq.findPosition(3));
187     assertEquals(4, seq.findPosition(4));
188     assertEquals(4, seq.findPosition(5));
189     // returns 1 more than sequence length if off the end ?!?
190     assertEquals(5, seq.findPosition(6));
191     assertEquals(5, seq.findPosition(7));
192
193     seq = new Sequence("test", "--AB-C-DEF--");
194     assertEquals(1, seq.findPosition(0));
195     assertEquals(1, seq.findPosition(1));
196     assertEquals(1, seq.findPosition(2));
197     assertEquals(2, seq.findPosition(3));
198     assertEquals(3, seq.findPosition(4));
199     assertEquals(3, seq.findPosition(5));
200     assertEquals(4, seq.findPosition(6));
201     assertEquals(4, seq.findPosition(7));
202     assertEquals(5, seq.findPosition(8));
203     assertEquals(6, seq.findPosition(9));
204     assertEquals(7, seq.findPosition(10));
205     assertEquals(7, seq.findPosition(11));
206   }
207
208   @Test
209   public void testDeleteChars()
210   {
211     SequenceI seq = new Sequence("test", "ABCDEF");
212     assertEquals(1, seq.getStart());
213     assertEquals(6, seq.getEnd());
214     seq.deleteChars(2, 3);
215     assertEquals("ABDEF", seq.getSequenceAsString());
216     assertEquals(1, seq.getStart());
217     assertEquals(5, seq.getEnd());
218
219     seq = new Sequence("test", "ABCDEF");
220     seq.deleteChars(0, 2);
221     assertEquals("CDEF", seq.getSequenceAsString());
222     assertEquals(3, seq.getStart());
223     assertEquals(6, seq.getEnd());
224   }
225
226   @Test
227   public void testInsertCharAt()
228   {
229     // non-static methods:
230     SequenceI seq = new Sequence("test", "ABCDEF");
231     seq.insertCharAt(0, 'z');
232     assertEquals("zABCDEF", seq.getSequenceAsString());
233     seq.insertCharAt(2, 2, 'x');
234     assertEquals("zAxxBCDEF", seq.getSequenceAsString());
235     
236     // for static method see StringUtilsTest
237   }
238
239   /**
240    * Test the method that returns an array of aligned sequence positions where
241    * the array index is the data sequence position (both base 0).
242    */
243   @Test
244   public void testGapMap()
245   {
246     SequenceI seq = new Sequence("test", "-A--B-CD-E--F-");
247     seq.createDatasetSequence();
248     assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(seq.gapMap()));
249   }
250 }