c34bb688f365cc263b588a724654302505df56cd
[jalview.git] / test / jalview / analysis / AlignmentAnnotationUtilsTest.java
1 package jalview.analysis;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.BitSet;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.junit.Before;
16 import org.junit.Test;
17
18 import jalview.datamodel.AlignmentAnnotation;
19 import jalview.datamodel.AlignmentI;
20 import jalview.datamodel.Annotation;
21 import jalview.datamodel.SequenceI;
22 import jalview.io.AppletFormatAdapter;
23
24 public class AlignmentAnnotationUtilsTest
25 {
26   // 4 sequences x 13 positions
27   final static String EOL = "\n";
28
29   // @formatter:off
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;
39   // @formatter:on
40
41   private static final int SEQ_ANN_COUNT = 12;
42
43   private AlignmentI alignment;
44
45   /**
46    * Test method that converts a (possibly null) array to a list.
47    */
48   @Test
49   public void testAsList()
50   {
51     // null array
52     Collection<AlignmentAnnotation> c1 = AlignmentAnnotationUtils
53             .asList(null);
54     assertTrue(c1.isEmpty());
55     
56     // empty array
57     AlignmentAnnotation[] anns = new AlignmentAnnotation[0];
58     c1 = AlignmentAnnotationUtils.asList(anns);
59     assertTrue(c1.isEmpty());
60
61     // non-empty array
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]));
69   }
70
71   /**
72    * This output is not part of the test but may help make sense of it...
73    * 
74    * @param shownTypes
75    * @param hiddenTypes
76    */
77   protected void consoleDebug(Map<String, List<List<String>>> shownTypes,
78           Map<String, List<List<String>>> hiddenTypes)
79   {
80     for (String calcId : shownTypes.keySet())
81     {
82       System.out.println("Visible annotation types for calcId=" + calcId);
83       for (List<String> type : shownTypes.get(calcId))
84       {
85         System.out.println("   " + type);
86       }
87     }
88     for (String calcId : hiddenTypes.keySet())
89     {
90       System.out.println("Hidden annotation types for calcId=" + calcId);
91       for (List<String> type : hiddenTypes.get(calcId))
92       {
93         System.out.println("   " + type);
94       }
95     }
96   }
97
98   /**
99    * Add a sequence group to the alignment with the specified sequences (base 0)
100    * in it
101    * 
102    * @param i
103    * @param more
104    */
105   private List<SequenceI> selectSequences(int... selected)
106   {
107     List<SequenceI> result = new ArrayList<SequenceI>();
108     SequenceI[] seqs = alignment.getSequencesArray();
109     for (int i : selected)
110     {
111       result.add(seqs[i]);
112     }
113     return result;
114   }
115
116   /**
117    * Load the test alignment and generate annotations on it
118    * 
119    * @throws IOException
120    */
121   @Before
122   public void setUp() throws IOException
123   {
124     alignment = new jalview.io.FormatAdapter().readFile(TEST_DATA,
125             AppletFormatAdapter.PASTE, "FASTA");
126   
127     AlignmentAnnotation[] anns = new AlignmentAnnotation[SEQ_ANN_COUNT];
128     for (int i = 0; i < anns.length; i++)
129     {
130       /*
131        * Use the constructor for a positional annotation (with an Annotation
132        * array)
133        */
134       anns[i] = new AlignmentAnnotation("Label" + i, "Desc " + i,
135               new Annotation[]
136               {});
137       anns[i].setCalcId("CalcId" + i);
138       anns[i].visible = true;
139       alignment.addAnnotation(anns[i]);
140     }
141   }
142
143   /**
144    * Test a mixture of show/hidden annotations in/outside selection group.
145    */
146   @Test
147   public void testGetShownHiddenTypes_forSelectionGroup()
148   {
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();
153   
154     /*
155      * Configure annotation properties for test
156      */
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;
163   
164     /*
165      * in selection group, hidden:
166      */
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");
180     /*
181      * in selection group, visible
182      */
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;
187   
188     List<SequenceI> selected = selectSequences(0, 3);
189     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
190             AlignmentAnnotationUtils.asList(anns),
191             selected);
192   
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));
203   
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));
217   
218     consoleDebug(shownTypes, hiddenTypes);
219   }
220
221   /**
222    * Test case where there are 'grouped' annotations, visible and hidden, within
223    * and without the selection group.
224    */
225   @Test
226   public void testGetShownHiddenTypes_withGraphGroups()
227   {
228     final int GROUP_3 = 3;
229     final int GROUP_4 = 4;
230     final int GROUP_5 = 5;
231     final int GROUP_6 = 6;
232   
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();
237   
238     /*
239      * Annotations for selection group and graph group
240      * 
241      * Hidden annotations Label2, Label3, in (hidden) group 5
242      */
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");
266   
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");
278     /*
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.
283      */
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");
296   
297     /*
298      * Annotations outwith selection group - should be ignored.
299      */
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;
309
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;
319   
320     /*
321      * Generate annotations[] arrays to match aligned columns
322      */
323     // adjustForAlignment(anns);
324
325     List<SequenceI> selected = selectSequences(0, 3);
326     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
327             AlignmentAnnotationUtils.asList(anns),
328             selected);
329   
330     consoleDebug(shownTypes, hiddenTypes);
331   
332     // CalcId1 / Label1, Label5 (only) should be 'shown', once, as a compound
333     // type
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));
339   
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));
346   }
347
348   /**
349    * Test method that determines visible graph groups.
350    */
351   @Test
352   public void testGetVisibleGraphGroups()
353   {
354     AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
355     /*
356      * a bar graph group is not included
357      */
358     anns[0].graph = AlignmentAnnotation.BAR_GRAPH;
359     anns[0].graphGroup = 1;
360     anns[0].visible = true;
361   
362     /*
363      * a line graph group is included as long as one of its members is visible
364      */
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;
371   
372     /*
373      * a line graph group with no visible rows is not included
374      */
375     anns[3].graph = AlignmentAnnotation.LINE_GRAPH;
376     anns[3].graphGroup = 3;
377     anns[3].visible = false;
378   
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;
383   
384     BitSet result = AlignmentAnnotationUtils
385             .getVisibleLineGraphGroups(AlignmentAnnotationUtils
386                     .asList(anns));
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));
392   }
393
394   /**
395    * Test for case where no sequence is selected. Shouldn't normally arise but
396    * check it handles it gracefully.
397    */
398   @Test
399   public void testGetShownHiddenTypes_noSequenceSelected()
400   {
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());
409
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());
416   }
417 }