2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.HashMap;
33 * A helper class to sort all annotations associated with an alignment in
39 public class AnnotationSorter
43 * enum for annotation sort options. The text description is used in the
44 * Preferences drop-down options. The enum name is saved in the preferences
50 public enum SequenceAnnotationOrder
52 // Text descriptions surface in the Preferences Sort by... options
53 SEQUENCE_AND_LABEL("Sequence"), LABEL_AND_SEQUENCE("Label"),
56 private String description;
58 private SequenceAnnotationOrder(String s)
64 public String toString()
69 public static SequenceAnnotationOrder forDescription(String d)
71 for (SequenceAnnotationOrder order : values())
73 if (order.toString().equals(d))
82 // the alignment with respect to which annotations are sorted
83 private final AlignmentI alignment;
85 // user preference for placement of non-sequence annotations
86 private boolean showAutocalcAbove;
88 // working map of sequence index in alignment
89 private final Map<SequenceI, Integer> sequenceIndices = new HashMap<SequenceI, Integer>();
92 * Constructor given an alignment and the location (top or bottom) of
93 * Consensus and similar.
96 * @param showAutocalculatedAbove
98 public AnnotationSorter(AlignmentI alignmentI,
99 boolean showAutocalculatedAbove)
101 this.alignment = alignmentI;
102 this.showAutocalcAbove = showAutocalculatedAbove;
106 * Default comparator sorts as follows by annotation type within sequence
109 * <li>annotations with a reference to a sequence in the alignment are sorted
110 * on sequence ordering</li>
111 * <li>other annotations go 'at the end', with their mutual order
113 * <li>within the same sequence ref, sort by label (non-case-sensitive)</li>
116 private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>()
119 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
121 if (o1 == null && o2 == null)
134 // TODO how to treat sequence-related autocalculated annotation
135 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
136 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
138 * Ignore label (keep existing ordering) for
139 * Conservation/Quality/Consensus etc
141 if (o1auto && o2auto)
147 * Sort autocalculated before or after sequence-related.
151 return showAutocalcAbove ? -1 : 1;
155 return showAutocalcAbove ? 1 : -1;
157 int sequenceOrder = compareSequences(o1, o2);
158 return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
162 public String toString()
164 return "Sort by sequence and label";
169 * This comparator sorts as follows by sequence order within annotation type
171 * <li>annotations with a reference to a sequence in the alignment are sorted
172 * on label (non-case-sensitive)</li>
173 * <li>other annotations go 'at the end', with their mutual order
175 * <li>within the same label, sort by order of the related sequences</li>
178 private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>()
181 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
183 if (o1 == null && o2 == null)
196 // TODO how to treat sequence-related autocalculated annotation
197 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
198 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
200 * Ignore label (keep existing ordering) for
201 * Conservation/Quality/Consensus etc
203 if (o1auto && o2auto)
209 * Sort autocalculated before or after sequence-related.
213 return showAutocalcAbove ? -1 : 1;
217 return showAutocalcAbove ? 1 : -1;
219 int labelOrder = compareLabels(o1, o2);
220 return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
224 public String toString()
226 return "Sort by label and sequence";
231 * noSort leaves sort order unchanged, within sequence- and autocalculated
232 * annotations, but may switch the ordering of these groups. Note this is
233 * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
234 * 'stable' (not change ordering of equal items).
236 private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
239 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
241 // TODO how to treat sequence-related autocalculated annotation
242 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
243 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
244 // TODO skip this test to allow customised ordering of all annotations
245 // - needs a third option: place autocalculated first / last / none
246 if (o1 != null && o2 != null)
248 if (o1auto && !o2auto)
250 return showAutocalcAbove ? -1 : 1;
252 if (!o1auto && o2auto)
254 return showAutocalcAbove ? 1 : -1;
261 public String toString()
268 * Sort by the specified ordering of sequence-specific annotations.
270 * @param alignmentAnnotations
273 public void sort(AlignmentAnnotation[] alignmentAnnotations,
274 SequenceAnnotationOrder order)
276 if (alignmentAnnotations == null)
280 // cache 'alignment sequence position' for the annotations
281 saveSequenceIndices(alignmentAnnotations);
283 Comparator<? super AlignmentAnnotation> comparator = getComparator(
286 if (alignmentAnnotations != null)
288 synchronized (alignmentAnnotations)
290 Arrays.sort(alignmentAnnotations, comparator);
296 * Calculate and save in a temporary map the position of each annotation's
297 * sequence (if it has one) in the alignment. Faster to do this once than for
298 * every annotation comparison.
300 * @param alignmentAnnotations
302 private void saveSequenceIndices(
303 AlignmentAnnotation[] alignmentAnnotations)
305 sequenceIndices.clear();
306 for (AlignmentAnnotation ann : alignmentAnnotations)
308 SequenceI seq = ann.sequenceRef;
311 int index = AlignmentUtils.getSequenceIndex(alignment, seq);
312 sequenceIndices.put(seq, index);
318 * Get the comparator for the specified sort order.
323 private Comparator<? super AlignmentAnnotation> getComparator(
324 SequenceAnnotationOrder order)
334 case SEQUENCE_AND_LABEL:
335 return this.bySequenceAndLabel;
336 case LABEL_AND_SEQUENCE:
337 return this.byLabelAndSequence;
339 throw new UnsupportedOperationException(order.toString());
344 * Non-case-sensitive comparison of annotation labels. Returns zero if either
351 private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)
353 if (o1 == null || o2 == null)
357 String label1 = o1.label;
358 String label2 = o2.label;
359 if (label1 == null && label2 == null)
371 return label1.toUpperCase().compareTo(label2.toUpperCase());
375 * Comparison based on position of associated sequence (if any) in the
376 * alignment. Returns zero if either argument is null.
382 private int compareSequences(AlignmentAnnotation o1,
383 AlignmentAnnotation o2)
385 SequenceI seq1 = o1.sequenceRef;
386 SequenceI seq2 = o2.sequenceRef;
387 if (seq1 == null && seq2 == null)
392 * Sort non-sequence-related before or after sequence-related.
396 return showAutocalcAbove ? -1 : 1;
400 return showAutocalcAbove ? 1 : -1;
402 // get sequence index - but note -1 means 'at end' so needs special handling
403 int index1 = sequenceIndices.get(seq1);
404 int index2 = sequenceIndices.get(seq2);
405 if (index1 == index2)
417 return Integer.compare(index1, index2);