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"),
57 * custom is set if user drags to reorder annotations
61 private String description;
63 private SequenceAnnotationOrder(String s)
69 public String toString()
74 public static SequenceAnnotationOrder forDescription(String d)
76 for (SequenceAnnotationOrder order : values())
78 if (order.toString().equals(d))
87 // the alignment with respect to which annotations are sorted
88 private final AlignmentI alignment;
90 // user preference for placement of non-sequence annotations
91 private boolean showAutocalcAbove;
93 // working map of sequence index in alignment
94 private final Map<SequenceI, Integer> sequenceIndices = new HashMap<>();
97 * Constructor given an alignment and the location (top or bottom) of
98 * Consensus and similar.
101 * @param showAutocalculatedAbove
103 public AnnotationSorter(AlignmentI alignmentI,
104 boolean showAutocalculatedAbove)
106 this.alignment = alignmentI;
107 this.showAutocalcAbove = showAutocalculatedAbove;
111 * Default comparator sorts as follows by annotation type within sequence
114 * <li>annotations with a reference to a sequence in the alignment are sorted
115 * on sequence ordering</li>
116 * <li>other annotations go 'at the end', with their mutual order
118 * <li>within the same sequence ref, sort by label (non-case-sensitive)</li>
121 private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>()
124 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
126 if (o1 == null && o2 == null)
139 // TODO how to treat sequence-related autocalculated annotation
140 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
141 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
143 * Ignore label (keep existing ordering) for
144 * Conservation/Quality/Consensus etc
146 if (o1auto && o2auto)
152 * Sort autocalculated before or after sequence-related.
156 return showAutocalcAbove ? -1 : 1;
160 return showAutocalcAbove ? 1 : -1;
162 int sequenceOrder = compareSequences(o1, o2);
163 return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
167 public String toString()
169 return "Sort by sequence and label";
174 * This comparator sorts as follows by sequence order within annotation type
176 * <li>annotations with a reference to a sequence in the alignment are sorted
177 * on label (non-case-sensitive)</li>
178 * <li>other annotations go 'at the end', with their mutual order
180 * <li>within the same label, sort by order of the related sequences</li>
183 private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>()
186 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
188 if (o1 == null && o2 == null)
201 // TODO how to treat sequence-related autocalculated annotation
202 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
203 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
205 * Ignore label (keep existing ordering) for
206 * Conservation/Quality/Consensus etc
208 if (o1auto && o2auto)
214 * Sort autocalculated before or after sequence-related.
218 return showAutocalcAbove ? -1 : 1;
222 return showAutocalcAbove ? 1 : -1;
224 int labelOrder = compareLabels(o1, o2);
225 return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
229 public String toString()
231 return "Sort by label and sequence";
236 * noSort leaves sort order unchanged, within sequence- and autocalculated
237 * annotations, but may switch the ordering of these groups. Note this is
238 * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
239 * 'stable' (not change ordering of equal items).
241 private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
244 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
246 // TODO how to treat sequence-related autocalculated annotation
247 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
248 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
249 // TODO skip this test to allow customised ordering of all annotations
250 // - needs a third option: place autocalculated first / last / none
251 if (o1 != null && o2 != null)
253 if (o1auto && !o2auto)
255 return showAutocalcAbove ? -1 : 1;
257 if (!o1auto && o2auto)
259 return showAutocalcAbove ? 1 : -1;
266 public String toString()
273 * Sort by the specified ordering of sequence-specific annotations.
275 * @param alignmentAnnotations
278 public void sort(AlignmentAnnotation[] alignmentAnnotations,
279 SequenceAnnotationOrder order)
281 if (alignmentAnnotations == null
282 || order == SequenceAnnotationOrder.CUSTOM)
286 // cache 'alignment sequence position' for the annotations
287 saveSequenceIndices(alignmentAnnotations);
289 Comparator<? super AlignmentAnnotation> comparator = getComparator(
292 if (alignmentAnnotations != null)
294 synchronized (alignmentAnnotations)
296 Arrays.sort(alignmentAnnotations, comparator);
302 * Calculate and save in a temporary map the position of each annotation's
303 * sequence (if it has one) in the alignment. Faster to do this once than for
304 * every annotation comparison.
306 * @param alignmentAnnotations
308 private void saveSequenceIndices(
309 AlignmentAnnotation[] alignmentAnnotations)
311 sequenceIndices.clear();
312 for (AlignmentAnnotation ann : alignmentAnnotations)
314 SequenceI seq = ann.sequenceRef;
317 int index = AlignmentUtils.getSequenceIndex(alignment, seq);
318 sequenceIndices.put(seq, index);
324 * Get the comparator for the specified sort order.
329 private Comparator<? super AlignmentAnnotation> getComparator(
330 SequenceAnnotationOrder order)
340 case SEQUENCE_AND_LABEL:
341 return this.bySequenceAndLabel;
342 case LABEL_AND_SEQUENCE:
343 return this.byLabelAndSequence;
345 throw new UnsupportedOperationException(order.toString());
350 * Non-case-sensitive comparison of annotation labels. Returns zero if either
357 private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)
359 if (o1 == null || o2 == null)
363 String label1 = o1.label;
364 String label2 = o2.label;
365 if (label1 == null && label2 == null)
377 return label1.toUpperCase().compareTo(label2.toUpperCase());
381 * Comparison based on position of associated sequence (if any) in the
382 * alignment. Returns zero if either argument is null.
388 private int compareSequences(AlignmentAnnotation o1,
389 AlignmentAnnotation o2)
391 SequenceI seq1 = o1.sequenceRef;
392 SequenceI seq2 = o2.sequenceRef;
393 if (seq1 == null && seq2 == null)
398 * Sort non-sequence-related before or after sequence-related.
402 return showAutocalcAbove ? -1 : 1;
406 return showAutocalcAbove ? 1 : -1;
408 // get sequence index - but note -1 means 'at end' so needs special handling
409 int index1 = sequenceIndices.get(seq1);
410 int index2 = sequenceIndices.get(seq2);
411 if (index1 == index2)
423 return Integer.compare(index1, index2);