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.api.AlignViewportI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
28 import java.util.Arrays;
29 import java.util.Comparator;
30 import java.util.HashMap;
34 * A helper class to sort all annotations associated with an alignment in
40 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))
88 * the alignment with respect to which annotations are sorted
90 private final AlignmentI alignment;
93 * if true, autocalculated are sorted first, if false, last
95 private boolean showAutocalcAbove;
98 * working map of sequence index in alignment
100 private final Map<SequenceI, Integer> sequenceIndices = new HashMap<>();
103 * if true, sort only repositions auto-calculated annotation (to top or bottom)
105 private boolean autocalcOnly;
112 public AnnotationSorter(AlignViewportI av)
114 this.alignment = av.getAlignment();
115 this.showAutocalcAbove = av.isShowAutocalculatedAbove();
119 * Default comparator sorts as follows by annotation type within sequence
122 * <li>annotations with a reference to a sequence in the alignment are sorted
123 * on sequence ordering</li>
124 * <li>other annotations go 'at the end', with their mutual order
126 * <li>within the same sequence ref, sort by label (non-case-sensitive)</li>
129 private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>()
132 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
134 if (o1 == null && o2 == null)
147 // TODO how to treat sequence-related autocalculated annotation
148 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
149 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
151 * Ignore label (keep existing ordering) for
152 * Conservation/Quality/Consensus etc
154 if (o1auto && o2auto)
160 * Sort autocalculated before or after sequence-related.
164 return showAutocalcAbove ? -1 : 1;
168 return showAutocalcAbove ? 1 : -1;
172 return 0; // don't reorder other annotations
174 int sequenceOrder = compareSequences(o1, o2);
175 return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
179 public String toString()
181 return "Sort by sequence and label";
186 * This comparator sorts as follows by sequence order within annotation type
188 * <li>annotations with a reference to a sequence in the alignment are sorted
189 * on label (non-case-sensitive)</li>
190 * <li>other annotations go 'at the end', with their mutual order
192 * <li>within the same label, sort by order of the related sequences</li>
195 private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>()
198 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
200 if (o1 == null && o2 == null)
213 // TODO how to treat sequence-related autocalculated annotation
214 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
215 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
217 * Ignore label (keep existing ordering) for
218 * Conservation/Quality/Consensus etc
220 if (o1auto && o2auto)
226 * Sort autocalculated before or after sequence-related.
230 return showAutocalcAbove ? -1 : 1;
234 return showAutocalcAbove ? 1 : -1;
238 return 0; // don't reorder other annotations
240 int labelOrder = compareLabels(o1, o2);
241 return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
245 public String toString()
247 return "Sort by label and sequence";
252 * noSort leaves sort order unchanged, within sequence- and autocalculated
253 * annotations, but may switch the ordering of these groups. Note this is
254 * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
255 * 'stable' (not change ordering of equal items).
257 private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
260 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
262 // TODO how to treat sequence-related autocalculated annotation
263 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
264 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
265 // TODO skip this test to allow customised ordering of all annotations
266 // - needs a third option: place autocalculated first / last / none
267 if (o1 != null && o2 != null)
269 if (o1auto && !o2auto)
271 return showAutocalcAbove ? -1 : 1;
273 if (!o1auto && o2auto)
275 return showAutocalcAbove ? 1 : -1;
282 public String toString()
289 * Sorts by the specified ordering. If order is {@code CUSTOM}, meaning
290 * annotations have been manually ordered by the user, no sort is performed.
293 * the sort order to apply
294 * @param autoCalcOnly
295 * if true, only autocalculated annotations are repositioned (to top
296 * or bottom), others are left in their current order
298 public void sort(SequenceAnnotationOrder sortBy, boolean autoCalcOnly)
300 if (sortBy == null || sortBy == SequenceAnnotationOrder.CUSTOM)
305 this.autocalcOnly = autoCalcOnly;
308 * cache 'alignment sequence positions' if required for sorting
310 if (sortBy == SequenceAnnotationOrder.SEQUENCE_AND_LABEL
311 || sortBy == SequenceAnnotationOrder.LABEL_AND_SEQUENCE)
313 saveSequenceIndices();
316 Comparator<? super AlignmentAnnotation> comparator = getComparator(
319 AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
320 synchronized (annotations)
322 Arrays.sort(annotations, comparator);
327 * Calculates and saves in a temporary map the position of each annotation's
328 * associated sequence (if it has one) in the alignment. Faster to do this
329 * once than for every annotation comparison.
331 private void saveSequenceIndices()
333 sequenceIndices.clear();
335 Map<SequenceI, Integer> seqPositions = alignment.getSequencePositions();
337 AlignmentAnnotation[] alignmentAnnotations = alignment
338 .getAlignmentAnnotation();
339 for (AlignmentAnnotation ann : alignmentAnnotations)
341 SequenceI seq = ann.sequenceRef;
344 Integer index = seqPositions.get(seq);
347 sequenceIndices.put(seq, index);
354 * Get the comparator for the specified sort order.
359 private Comparator<? super AlignmentAnnotation> getComparator(
360 SequenceAnnotationOrder order)
370 case SEQUENCE_AND_LABEL:
371 return this.bySequenceAndLabel;
372 case LABEL_AND_SEQUENCE:
373 return this.byLabelAndSequence;
375 throw new UnsupportedOperationException(order.toString());
380 * Non-case-sensitive comparison of annotation labels. Returns zero if either
387 private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)
389 if (o1 == null || o2 == null)
393 String label1 = o1.label;
394 String label2 = o2.label;
395 if (label1 == null && label2 == null)
407 return label1.toUpperCase().compareTo(label2.toUpperCase());
411 * Comparison based on position of associated sequence (if any) in the
418 private int compareSequences(AlignmentAnnotation o1,
419 AlignmentAnnotation o2)
421 SequenceI seq1 = o1.sequenceRef;
422 SequenceI seq2 = o2.sequenceRef;
423 if (seq1 == null && seq2 == null)
429 * Sort non-sequence-related before or after sequence-related
433 return showAutocalcAbove ? -1 : 1;
437 return showAutocalcAbove ? 1 : -1;
441 * else sort by associated sequence position
443 Integer index1 = sequenceIndices.get(seq1);
444 Integer index2 = sequenceIndices.get(seq2);
447 return index2 == null ? 0 : -1;
453 return Integer.compare(index1.intValue(), index2.intValue());