1 package jalview.analysis;
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.SequenceI;
5 import jalview.renderer.AnnotationRenderer;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.BitSet;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
15 public class AlignmentAnnotationUtils
19 * Helper method to populate lists of annotation types for the Show/Hide
20 * Annotations menus. If sequenceGroup is not null, this is restricted to
21 * annotations which are associated with sequences in the selection group.
23 * If an annotation row is currently visible, its type (label) is added (once
24 * only per type), to the shownTypes list. If it is currently hidden, it is
25 * added to the hiddenTypesList.
27 * For rows that belong to a line graph group, so are always rendered
30 * <li>Treat all rows in the group as visible, if at least one of them is</li>
31 * <li>Build a list of all the annotation types that belong to the group</li>
35 * a map, keyed by calcId (annotation source), whose entries are the
36 * lists of annotation types found for the calcId; each annotation
37 * type in turn may be a list (in the case of grouped annotations)
39 * a map, similar to shownTypes, but for hidden annotation types
41 * the annotations on the alignment to scan
43 * the sequences to restrict search to
45 public static void getShownHiddenTypes(
46 Map<String, List<List<String>>> shownTypes,
47 Map<String, List<List<String>>> hiddenTypes,
48 List<AlignmentAnnotation> annotations,
49 List<SequenceI> forSequences)
51 BitSet visibleGraphGroups = AlignmentAnnotationUtils
52 .getVisibleLineGraphGroups(annotations);
55 * Build a lookup, by calcId (annotation source), of all annotation types in
58 Map<String, Map<Integer, List<String>>> groupLabels = new HashMap<String, Map<Integer, List<String>>>();
60 // trackers for which calcId!label combinations we have dealt with
61 List<String> addedToShown = new ArrayList<String>();
62 List<String> addedToHidden = new ArrayList<String>();
64 for (AlignmentAnnotation aa : annotations)
66 if (forSequences != null
67 && (aa.sequenceRef != null && forSequences
68 .contains(aa.sequenceRef)))
70 String calcId = aa.getCalcId();
73 * Build a 'composite label' for types in line graph groups.
75 final List<String> labelAsList = new ArrayList<String>();
76 final String displayLabel = aa.label;
77 labelAsList.add(displayLabel);
78 if (aa.graph == AlignmentAnnotation.LINE_GRAPH
79 && aa.graphGroup > -1)
81 if (!groupLabels.containsKey(calcId))
83 groupLabels.put(calcId, new HashMap<Integer, List<String>>());
85 Map<Integer, List<String>> groupLabelsForCalcId = groupLabels
87 if (groupLabelsForCalcId.containsKey(aa.graphGroup))
89 if (!groupLabelsForCalcId.get(aa.graphGroup).contains(
92 groupLabelsForCalcId.get(aa.graphGroup).add(displayLabel);
97 groupLabelsForCalcId.put(aa.graphGroup, labelAsList);
102 * 'Simple case' - not a grouped annotation type - list of one label
106 String rememberAs = calcId + "!" + displayLabel;
107 if (aa.visible && !addedToShown.contains(rememberAs))
109 if (!shownTypes.containsKey(calcId))
111 shownTypes.put(calcId, new ArrayList<List<String>>());
113 shownTypes.get(calcId).add(labelAsList);
114 addedToShown.add(rememberAs);
118 if (!aa.visible && !addedToHidden.contains(rememberAs))
120 if (!hiddenTypes.containsKey(calcId))
122 hiddenTypes.put(calcId, new ArrayList<List<String>>());
124 hiddenTypes.get(calcId).add(labelAsList);
125 addedToHidden.add(rememberAs);
132 * Finally add the 'composite group labels' to the appropriate lists,
133 * depending on whether the group is identified as visible or hidden. Don't
134 * add the same label more than once (there may be many graph groups that
137 for (String calcId : groupLabels.keySet())
139 for (int group : groupLabels.get(calcId).keySet())
141 final List<String> groupLabel = groupLabels.get(calcId).get(group);
142 // don't want to duplicate 'same types in different order'
143 Collections.sort(groupLabel);
144 if (visibleGraphGroups.get(group))
146 if (!shownTypes.containsKey(calcId))
148 shownTypes.put(calcId, new ArrayList<List<String>>());
150 if (!shownTypes.get(calcId).contains(groupLabel))
152 shownTypes.get(calcId).add(groupLabel);
157 if (!hiddenTypes.containsKey(calcId))
159 hiddenTypes.put(calcId, new ArrayList<List<String>>());
161 if (!hiddenTypes.get(calcId).contains(groupLabel))
163 hiddenTypes.get(calcId).add(groupLabel);
171 * Returns a BitSet (possibly empty) of those graphGroups for line graph
172 * annotations, which have at least one member annotation row marked visible.
174 * Only one row in each visible group is marked visible, but when it is drawn,
175 * so are all the other rows in the same group.
177 * This lookup set allows us to check whether rows apparently marked not
178 * visible are in fact shown.
180 * @see AnnotationRenderer#drawComponent
184 public static BitSet getVisibleLineGraphGroups(
185 List<AlignmentAnnotation> annotations)
187 BitSet result = new BitSet();
188 for (AlignmentAnnotation ann : annotations)
190 if (ann.graph == AlignmentAnnotation.LINE_GRAPH && ann.visible)
192 int gg = ann.graphGroup;
203 * Converts an array of AlignmentAnnotation into a List of
204 * AlignmentAnnotation. A null array is converted to an empty list.
209 public static List<AlignmentAnnotation> asList(AlignmentAnnotation[] anns)
211 // TODO use AlignmentAnnotationI instead when it exists
212 return (anns == null ? Collections.<AlignmentAnnotation> emptyList()
213 : Arrays.asList(anns));