1 package jalview.analysis;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.SequenceI;
11 import jalview.io.AppletFormatAdapter;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.BitSet;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
21 import org.testng.annotations.BeforeMethod;
22 import org.testng.annotations.Test;
24 public class AlignmentAnnotationUtilsTest
26 // 4 sequences x 13 positions
27 final static String EOL = "\n";
30 final static String TEST_DATA =
31 ">FER_CAPAA Ferredoxin" + EOL +
32 "TIETHKEAELVG-" + EOL +
33 ">FER_CAPAN Ferredoxin, chloroplast precursor" + EOL +
34 "TIETHKEAELVG-" + EOL +
35 ">FER1_SOLLC Ferredoxin-1, chloroplast precursor" + EOL +
36 "TIETHKEEELTA-" + EOL +
37 ">Q93XJ9_SOLTU Ferredoxin I precursor" + EOL +
38 "TIETHKEEELTA-" + EOL;
41 private static final int SEQ_ANN_COUNT = 12;
43 private AlignmentI alignment;
46 * Test method that converts a (possibly null) array to a list.
49 public void testAsList()
52 Collection<AlignmentAnnotation> c1 = AlignmentAnnotationUtils
54 assertTrue(c1.isEmpty());
57 AlignmentAnnotation[] anns = new AlignmentAnnotation[0];
58 c1 = AlignmentAnnotationUtils.asList(anns);
59 assertTrue(c1.isEmpty());
62 anns = new AlignmentAnnotation[2];
63 anns[0] = new AlignmentAnnotation("label0", "desc0", 0.0f);
64 anns[1] = new AlignmentAnnotation("label1", "desc1", 1.0f);
65 c1 = AlignmentAnnotationUtils.asList(anns);
66 assertEquals(2, c1.size());
67 assertTrue(c1.contains(anns[0]));
68 assertTrue(c1.contains(anns[1]));
72 * This output is not part of the test but may help make sense of it...
77 protected void consoleDebug(Map<String, List<List<String>>> shownTypes,
78 Map<String, List<List<String>>> hiddenTypes)
80 for (String calcId : shownTypes.keySet())
82 System.out.println("Visible annotation types for calcId=" + calcId);
83 for (List<String> type : shownTypes.get(calcId))
85 System.out.println(" " + type);
88 for (String calcId : hiddenTypes.keySet())
90 System.out.println("Hidden annotation types for calcId=" + calcId);
91 for (List<String> type : hiddenTypes.get(calcId))
93 System.out.println(" " + type);
99 * Add a sequence group to the alignment with the specified sequences (base 0)
105 private List<SequenceI> selectSequences(int... selected)
107 List<SequenceI> result = new ArrayList<SequenceI>();
108 SequenceI[] seqs = alignment.getSequencesArray();
109 for (int i : selected)
117 * Load the test alignment and generate annotations on it
119 * @throws IOException
122 public void setUp() throws IOException
124 alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA,
125 AppletFormatAdapter.PASTE, "FASTA");
127 AlignmentAnnotation[] anns = new AlignmentAnnotation[SEQ_ANN_COUNT];
128 for (int i = 0; i < anns.length; i++)
131 * Use the constructor for a positional annotation (with an Annotation
134 anns[i] = new AlignmentAnnotation("Label" + i, "Desc " + i,
137 anns[i].setCalcId("CalcId" + i);
138 anns[i].visible = true;
139 alignment.addAnnotation(anns[i]);
144 * Test a mixture of show/hidden annotations in/outside selection group.
147 public void testGetShownHiddenTypes_forSelectionGroup()
149 Map<String, List<List<String>>> shownTypes = new HashMap<String, List<List<String>>>();
150 Map<String, List<List<String>>> hiddenTypes = new HashMap<String, List<List<String>>>();
151 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
152 SequenceI[] seqs = alignment.getSequencesArray();
155 * Configure annotation properties for test
157 // not in selection group (should be ignored):
158 // hidden annotation Label4 not in selection group
159 anns[4].sequenceRef = seqs[2];
160 anns[4].visible = false;
161 anns[7].sequenceRef = seqs[1];
162 anns[7].visible = true;
165 * in selection group, hidden:
167 anns[2].sequenceRef = seqs[3]; // CalcId2/Label2
168 anns[2].visible = false;
169 anns[3].sequenceRef = seqs[3]; // CalcId3/Label2
170 anns[3].visible = false;
171 anns[3].label = "Label2";
172 anns[4].sequenceRef = seqs[3]; // CalcId2/Label3
173 anns[4].visible = false;
174 anns[4].label = "Label3";
175 anns[4].setCalcId("CalcId2");
176 anns[8].sequenceRef = seqs[0]; // CalcId9/Label9
177 anns[8].visible = false;
178 anns[8].label = "Label9";
179 anns[8].setCalcId("CalcId9");
181 * in selection group, visible
183 anns[6].sequenceRef = seqs[0]; // CalcId6/Label6
184 anns[6].visible = true;
185 anns[9].sequenceRef = seqs[3]; // CalcId9/Label9
186 anns[9].visible = true;
188 List<SequenceI> selected = selectSequences(0, 3);
189 AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
190 AlignmentAnnotationUtils.asList(anns),
193 // check results; note CalcId9/Label9 is both hidden and shown (for
194 // different sequences) so should be in both
195 // shown: CalcId6/Label6 and CalcId9/Label9
196 assertEquals(2, shownTypes.size());
197 assertEquals(1, shownTypes.get("CalcId6").size());
198 assertEquals(1, shownTypes.get("CalcId6").get(0).size());
199 assertEquals("Label6", shownTypes.get("CalcId6").get(0).get(0));
200 assertEquals(1, shownTypes.get("CalcId9").size());
201 assertEquals(1, shownTypes.get("CalcId9").get(0).size());
202 assertEquals("Label9", shownTypes.get("CalcId9").get(0).get(0));
204 // hidden: CalcId2/Label2, CalcId2/Label3, CalcId3/Label2, CalcId9/Label9
205 assertEquals(3, hiddenTypes.size());
206 assertEquals(2, hiddenTypes.get("CalcId2").size());
207 assertEquals(1, hiddenTypes.get("CalcId2").get(0).size());
208 assertEquals("Label2", hiddenTypes.get("CalcId2").get(0).get(0));
209 assertEquals(1, hiddenTypes.get("CalcId2").get(1).size());
210 assertEquals("Label3", hiddenTypes.get("CalcId2").get(1).get(0));
211 assertEquals(1, hiddenTypes.get("CalcId3").size());
212 assertEquals(1, hiddenTypes.get("CalcId3").get(0).size());
213 assertEquals("Label2", hiddenTypes.get("CalcId3").get(0).get(0));
214 assertEquals(1, hiddenTypes.get("CalcId9").size());
215 assertEquals(1, hiddenTypes.get("CalcId9").get(0).size());
216 assertEquals("Label9", hiddenTypes.get("CalcId9").get(0).get(0));
218 consoleDebug(shownTypes, hiddenTypes);
222 * Test case where there are 'grouped' annotations, visible and hidden, within
223 * and without the selection group.
226 public void testGetShownHiddenTypes_withGraphGroups()
228 final int GROUP_3 = 3;
229 final int GROUP_4 = 4;
230 final int GROUP_5 = 5;
231 final int GROUP_6 = 6;
233 Map<String, List<List<String>>> shownTypes = new HashMap<String, List<List<String>>>();
234 Map<String, List<List<String>>> hiddenTypes = new HashMap<String, List<List<String>>>();
235 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
236 SequenceI[] seqs = alignment.getSequencesArray();
239 * Annotations for selection group and graph group
241 * Hidden annotations Label2, Label3, in (hidden) group 5
243 anns[2].sequenceRef = seqs[3];
244 anns[2].visible = false;
245 anns[2].graph = AlignmentAnnotation.LINE_GRAPH;
246 anns[2].graphGroup = GROUP_5; // not a visible group
247 anns[3].sequenceRef = seqs[0];
248 anns[3].visible = false;
249 anns[3].graph = AlignmentAnnotation.LINE_GRAPH;
250 anns[3].graphGroup = GROUP_5;
251 // need to ensure annotations have the same calcId as well
252 anns[3].setCalcId("CalcId2");
253 // annotations for a different hidden group generating the same group label
254 anns[10].sequenceRef = seqs[0];
255 anns[10].visible = false;
256 anns[10].graph = AlignmentAnnotation.LINE_GRAPH;
257 anns[10].graphGroup = GROUP_3;
258 anns[10].label = "Label3";
259 anns[10].setCalcId("CalcId2");
260 anns[11].sequenceRef = seqs[3];
261 anns[11].visible = false;
262 anns[11].graph = AlignmentAnnotation.LINE_GRAPH;
263 anns[11].graphGroup = GROUP_3;
264 anns[11].label = "Label2";
265 anns[11].setCalcId("CalcId2");
267 // annotations Label1 (hidden), Label5 (visible) in group 6 (visible)
268 anns[1].sequenceRef = seqs[3];
269 // being in a visible group should take precedence over this visibility
270 anns[1].visible = false;
271 anns[1].graph = AlignmentAnnotation.LINE_GRAPH;
272 anns[1].graphGroup = GROUP_6;
273 anns[5].sequenceRef = seqs[0];
274 anns[5].visible = true;
275 anns[5].graph = AlignmentAnnotation.LINE_GRAPH;
276 anns[5].graphGroup = GROUP_6;
277 anns[5].setCalcId("CalcId1");
279 * Annotations 0 and 4 are visible, for a different CalcId and graph group.
280 * They produce the same label as annotations 1 and 5, which should not be
281 * duplicated in the results. This case corresponds to (e.g.) many
282 * occurrences of an IUPred Short/Long annotation group, one per sequence.
284 anns[4].sequenceRef = seqs[0];
285 anns[4].visible = false;
286 anns[4].graph = AlignmentAnnotation.LINE_GRAPH;
287 anns[4].graphGroup = GROUP_4;
288 anns[4].label = "Label1";
289 anns[4].setCalcId("CalcId1");
290 anns[0].sequenceRef = seqs[0];
291 anns[0].visible = true;
292 anns[0].graph = AlignmentAnnotation.LINE_GRAPH;
293 anns[0].graphGroup = GROUP_4;
294 anns[0].label = "Label5";
295 anns[0].setCalcId("CalcId1");
298 * Annotations outwith selection group - should be ignored.
300 // Hidden grouped annotations
301 anns[6].sequenceRef = seqs[2];
302 anns[6].visible = false;
303 anns[6].graph = AlignmentAnnotation.LINE_GRAPH;
304 anns[6].graphGroup = GROUP_4;
305 anns[8].sequenceRef = seqs[1];
306 anns[8].visible = false;
307 anns[8].graph = AlignmentAnnotation.LINE_GRAPH;
308 anns[8].graphGroup = GROUP_4;
310 // visible grouped annotations Label7, Label9
311 anns[7].sequenceRef = seqs[2];
312 anns[7].visible = true;
313 anns[7].graph = AlignmentAnnotation.LINE_GRAPH;
314 anns[7].graphGroup = GROUP_4;
315 anns[9].sequenceRef = seqs[1];
316 anns[9].visible = true;
317 anns[9].graph = AlignmentAnnotation.LINE_GRAPH;
318 anns[9].graphGroup = GROUP_4;
321 * Generate annotations[] arrays to match aligned columns
323 // adjustForAlignment(anns);
325 List<SequenceI> selected = selectSequences(0, 3);
326 AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
327 AlignmentAnnotationUtils.asList(anns),
330 consoleDebug(shownTypes, hiddenTypes);
332 // CalcId1 / Label1, Label5 (only) should be 'shown', once, as a compound
334 assertEquals(1, shownTypes.size());
335 assertEquals(1, shownTypes.get("CalcId1").size());
336 assertEquals(2, shownTypes.get("CalcId1").get(0).size());
337 assertEquals("Label1", shownTypes.get("CalcId1").get(0).get(0));
338 assertEquals("Label5", shownTypes.get("CalcId1").get(0).get(1));
340 // CalcId2 / Label2, Label3 (only) should be 'hidden'
341 assertEquals(1, hiddenTypes.size());
342 assertEquals(1, hiddenTypes.get("CalcId2").size());
343 assertEquals(2, hiddenTypes.get("CalcId2").get(0).size());
344 assertEquals("Label2", hiddenTypes.get("CalcId2").get(0).get(0));
345 assertEquals("Label3", hiddenTypes.get("CalcId2").get(0).get(1));
349 * Test method that determines visible graph groups.
352 public void testGetVisibleGraphGroups()
354 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
356 * a bar graph group is not included
358 anns[0].graph = AlignmentAnnotation.BAR_GRAPH;
359 anns[0].graphGroup = 1;
360 anns[0].visible = true;
363 * a line graph group is included as long as one of its members is visible
365 anns[1].graph = AlignmentAnnotation.LINE_GRAPH;
366 anns[1].graphGroup = 5;
367 anns[1].visible = false;
368 anns[2].graph = AlignmentAnnotation.LINE_GRAPH;
369 anns[2].graphGroup = 5;
370 anns[2].visible = true;
373 * a line graph group with no visible rows is not included
375 anns[3].graph = AlignmentAnnotation.LINE_GRAPH;
376 anns[3].graphGroup = 3;
377 anns[3].visible = false;
379 // a visible line graph with no graph group is not included
380 anns[4].graph = AlignmentAnnotation.LINE_GRAPH;
381 anns[4].graphGroup = -1;
382 anns[4].visible = true;
384 BitSet result = AlignmentAnnotationUtils
385 .getVisibleLineGraphGroups(AlignmentAnnotationUtils
387 assertTrue(result.get(5));
388 assertFalse(result.get(0));
389 assertFalse(result.get(1));
390 assertFalse(result.get(2));
391 assertFalse(result.get(3));
395 * Test for case where no sequence is selected. Shouldn't normally arise but
396 * check it handles it gracefully.
399 public void testGetShownHiddenTypes_noSequenceSelected()
401 Map<String, List<List<String>>> shownTypes = new HashMap<String, List<List<String>>>();
402 Map<String, List<List<String>>> hiddenTypes = new HashMap<String, List<List<String>>>();
403 AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
404 // selected sequences null
405 AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
406 AlignmentAnnotationUtils.asList(anns), null);
407 assertTrue(shownTypes.isEmpty());
408 assertTrue(hiddenTypes.isEmpty());
410 List<SequenceI> sequences = new ArrayList<SequenceI>();
411 // selected sequences empty list
412 AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
413 AlignmentAnnotationUtils.asList(anns), sequences);
414 assertTrue(shownTypes.isEmpty());
415 assertTrue(hiddenTypes.isEmpty());