1 package jalview.analysis;
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.SequenceI;
6 import jalview.renderer.AnnotationRenderer;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.BitSet;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
16 public class AlignmentAnnotationUtils
20 * Helper method to populate lists of annotation types for the Show/Hide
21 * Annotations menus. If sequenceGroup is not null, this is restricted to
22 * annotations which are associated with sequences in the selection group.
24 * If an annotation row is currently visible, its type (label) is added (once
25 * only per type), to the shownTypes list. If it is currently hidden, it is
26 * added to the hiddenTypesList.
28 * For rows that belong to a line graph group, so are always rendered
31 * <li>Treat all rows in the group as visible, if at least one of them is</li>
32 * <li>Build a list of all the annotation types that belong to the group</li>
36 * a map, keyed by calcId (annotation source), whose entries are the
37 * lists of annotation types found for the calcId; each annotation
38 * type in turn may be a list (in the case of grouped annotations)
40 * a map, similar to shownTypes, but for hidden annotation types
42 * the annotations on the alignment to scan
44 * the sequences to restrict search to
46 public static void getShownHiddenTypes(
47 Map<String, List<List<String>>> shownTypes,
48 Map<String, List<List<String>>> hiddenTypes,
49 List<AlignmentAnnotation> annotations,
50 List<SequenceI> forSequences)
52 BitSet visibleGraphGroups = AlignmentAnnotationUtils
53 .getVisibleLineGraphGroups(annotations);
56 * Build a lookup, by calcId (annotation source), of all annotation types in
59 Map<String, Map<Integer, List<String>>> groupLabels = new HashMap<String, Map<Integer, List<String>>>();
61 // trackers for which calcId!label combinations we have dealt with
62 List<String> addedToShown = new ArrayList<String>();
63 List<String> addedToHidden = new ArrayList<String>();
65 for (AlignmentAnnotation aa : annotations)
67 if (forSequences != null
68 && (aa.sequenceRef != null && forSequences
69 .contains(aa.sequenceRef)))
71 String calcId = aa.getCalcId();
74 * Build a 'composite label' for types in line graph groups.
76 final List<String> labelAsList = new ArrayList<String>();
77 final String displayLabel = aa.label;
78 labelAsList.add(displayLabel);
79 if (aa.graph == AlignmentAnnotation.LINE_GRAPH
80 && aa.graphGroup > -1)
82 if (!groupLabels.containsKey(calcId))
84 groupLabels.put(calcId, new HashMap<Integer, List<String>>());
86 Map<Integer, List<String>> groupLabelsForCalcId = groupLabels
88 if (groupLabelsForCalcId.containsKey(aa.graphGroup))
90 if (!groupLabelsForCalcId.get(aa.graphGroup).contains(
93 groupLabelsForCalcId.get(aa.graphGroup).add(displayLabel);
98 groupLabelsForCalcId.put(aa.graphGroup, labelAsList);
103 * 'Simple case' - not a grouped annotation type - list of one label
107 String rememberAs = calcId + "!" + displayLabel;
108 if (aa.visible && !addedToShown.contains(rememberAs))
110 if (!shownTypes.containsKey(calcId))
112 shownTypes.put(calcId, new ArrayList<List<String>>());
114 shownTypes.get(calcId).add(labelAsList);
115 addedToShown.add(rememberAs);
119 if (!aa.visible && !addedToHidden.contains(rememberAs))
121 if (!hiddenTypes.containsKey(calcId))
123 hiddenTypes.put(calcId, new ArrayList<List<String>>());
125 hiddenTypes.get(calcId).add(labelAsList);
126 addedToHidden.add(rememberAs);
133 * finally add the 'composite group labels' to the appropriate lists,
134 * depending on whether the group is identified as visible or hidden
136 for (String calcId : groupLabels.keySet())
138 for (int group : groupLabels.get(calcId).keySet())
140 final List<String> groupLabel = groupLabels.get(calcId).get(group);
141 if (visibleGraphGroups.get(group))
143 if (!shownTypes.containsKey(calcId))
145 shownTypes.put(calcId, new ArrayList<List<String>>());
147 shownTypes.get(calcId).add(groupLabel);
151 if (!hiddenTypes.containsKey(calcId))
153 hiddenTypes.put(calcId, new ArrayList<List<String>>());
155 hiddenTypes.get(calcId).add(groupLabel);
162 * Returns a BitSet (possibly empty) of those graphGroups for line graph
163 * annotations, which have at least one member annotation row marked visible.
165 * Only one row in each visible group is marked visible, but when it is drawn,
166 * so are all the other rows in the same group.
168 * This lookup set allows us to check whether rows apparently marked not
169 * visible are in fact shown.
171 * @see AnnotationRenderer#drawComponent
175 public static BitSet getVisibleLineGraphGroups(
176 List<AlignmentAnnotation> annotations)
178 BitSet result = new BitSet();
179 for (AlignmentAnnotation ann : annotations)
181 if (ann.graph == AlignmentAnnotation.LINE_GRAPH && ann.visible)
183 int gg = ann.graphGroup;
194 * Converts an array of AlignmentAnnotation into a List of
195 * AlignmentAnnotation. A null array is converted to an empty list.
200 public static List<AlignmentAnnotation> asList(AlignmentAnnotation[] anns)
202 // TODO use AlignmentAnnotationI instead when it exists
203 return (anns == null ? Collections.<AlignmentAnnotation> emptyList()
204 : Arrays.asList(anns));