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;
31 import java.util.List;
35 * A helper class to sort all annotations associated with an alignment in
41 public class AnnotationSorter
44 * A special comparator for use when reloading from project, that supports
45 * reordering annotations to match their order in the project. It incorporates
46 * special handling of autocalculated annotations, that are recreated on load:
48 * <li>these are matched based on autocalc flag and label ("Consensus") etc
49 * rather than object identity</li>
50 * <li>on successful match, visibility and height settings are applied from
51 * those saved in the project</li>
54 private final class ReloadComparator
55 implements Comparator<AlignmentAnnotation>
57 private List<AlignmentAnnotation> order;
59 ReloadComparator(List<AlignmentAnnotation> theOrder)
65 public int compare(AlignmentAnnotation o1,
66 AlignmentAnnotation o2)
68 int i1 = findPosition(o1);
69 int i2 = findPosition(o2);
70 return Integer.compare(i1, i2);
74 * A helper method that returns the position of the annotation in the
75 * {@code order} list, by object identity, or failing that, by matched
76 * autocalc label ("Consensus" etc). Returns -1 if not found. This can
77 * happen if, for example, Consensus was saved but since turned off in
83 private int findPosition(AlignmentAnnotation aa)
85 int i = order.indexOf(aa);
86 if (i == -1 && aa.autoCalculated && aa.label != null)
88 for (int j = 0; j < order.size(); j++)
90 AlignmentAnnotation ann = order.get(j);
91 if (aa.label.equals(ann.label))
94 aa.visible = ann.visible;
95 if (ann.graphHeight >= 0)
97 aa.graphHeight = ann.graphHeight;
108 * enum for annotation sort options. The text description is used in the
109 * Preferences drop-down options. The enum name is saved in the preferences
112 * @author gmcarstairs
115 public enum SequenceAnnotationOrder
117 // Text descriptions surface in the Preferences Sort by... options
118 SEQUENCE_AND_LABEL("Sequence"), LABEL_AND_SEQUENCE("Label"),
122 * custom is set if user drags to reorder annotations
124 CUSTOM("Customised");
126 private String description;
128 private SequenceAnnotationOrder(String s)
134 public String toString()
139 public static SequenceAnnotationOrder forDescription(String d)
141 for (SequenceAnnotationOrder order : values())
143 if (order.toString().equals(d))
153 * the alignment with respect to which annotations are sorted
155 private final AlignmentI alignment;
158 * if true, autocalculated are sorted first, if false, last
160 private boolean showAutocalcAbove;
163 * working map of sequence index in alignment
165 private final Map<SequenceI, Integer> sequenceIndices = new HashMap<>();
168 * if true, sort only repositions auto-calculated annotation (to top or bottom)
170 private boolean autocalcOnly;
177 public AnnotationSorter(AlignViewportI av)
179 this.alignment = av.getAlignment();
180 this.showAutocalcAbove = av.isShowAutocalculatedAbove();
184 * Default comparator sorts as follows by annotation type within sequence
187 * <li>annotations with a reference to a sequence in the alignment are sorted
188 * on sequence ordering</li>
189 * <li>other annotations go 'at the end', with their mutual order
191 * <li>within the same sequence ref, sort by label (non-case-sensitive)</li>
194 private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>()
197 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
199 if (o1 == null && o2 == null)
212 // TODO how to treat sequence-related autocalculated annotation
213 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
214 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
216 * Ignore label (keep existing ordering) for
217 * Conservation/Quality/Consensus etc
219 if (o1auto && o2auto)
225 * Sort autocalculated before or after sequence-related.
229 return showAutocalcAbove ? -1 : 1;
233 return showAutocalcAbove ? 1 : -1;
237 return 0; // don't reorder other annotations
239 int sequenceOrder = compareSequences(o1, o2);
240 return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
244 public String toString()
246 return "Sort by sequence and label";
251 * This comparator sorts as follows by sequence order within annotation type
253 * <li>annotations with a reference to a sequence in the alignment are sorted
254 * on label (non-case-sensitive)</li>
255 * <li>other annotations go 'at the end', with their mutual order
257 * <li>within the same label, sort by order of the related sequences</li>
260 private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>()
263 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
265 if (o1 == null && o2 == null)
278 // TODO how to treat sequence-related autocalculated annotation
279 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
280 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
282 * Ignore label (keep existing ordering) for
283 * Conservation/Quality/Consensus etc
285 if (o1auto && o2auto)
291 * Sort autocalculated before or after sequence-related.
295 return showAutocalcAbove ? -1 : 1;
299 return showAutocalcAbove ? 1 : -1;
303 return 0; // don't reorder other annotations
305 int labelOrder = compareLabels(o1, o2);
306 return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
310 public String toString()
312 return "Sort by label and sequence";
317 * noSort leaves sort order unchanged, within sequence- and autocalculated
318 * annotations, but may switch the ordering of these groups. Note this is
319 * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
320 * 'stable' (not change ordering of equal items).
322 private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
325 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
327 // TODO how to treat sequence-related autocalculated annotation
328 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
329 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
330 // TODO skip this test to allow customised ordering of all annotations
331 // - needs a third option: place autocalculated first / last / none
332 if (o1 != null && o2 != null)
334 if (o1auto && !o2auto)
336 return showAutocalcAbove ? -1 : 1;
338 if (!o1auto && o2auto)
340 return showAutocalcAbove ? 1 : -1;
347 public String toString()
354 * Sorts by the specified ordering. If order is {@code CUSTOM}, meaning
355 * annotations have been manually ordered by the user, no sort is performed.
358 * the sort order to apply
359 * @param autoCalcOnly
360 * if true, only autocalculated annotations are repositioned (to top
361 * or bottom), others are left in their current order
363 public void sort(SequenceAnnotationOrder sortBy, boolean autoCalcOnly)
365 if (sortBy == null || sortBy == SequenceAnnotationOrder.CUSTOM)
370 this.autocalcOnly = autoCalcOnly;
373 * cache 'alignment sequence positions' if required for sorting
375 if (sortBy == SequenceAnnotationOrder.SEQUENCE_AND_LABEL
376 || sortBy == SequenceAnnotationOrder.LABEL_AND_SEQUENCE)
378 saveSequenceIndices();
381 Comparator<? super AlignmentAnnotation> comparator = getComparator(
384 AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
385 synchronized (annotations)
387 Arrays.sort(annotations, comparator);
392 * Calculates and saves in a temporary map the position of each annotation's
393 * associated sequence (if it has one) in the alignment. Faster to do this
394 * once than for every annotation comparison.
396 private void saveSequenceIndices()
398 sequenceIndices.clear();
400 Map<SequenceI, Integer> seqPositions = alignment.getSequencePositions();
402 AlignmentAnnotation[] alignmentAnnotations = alignment
403 .getAlignmentAnnotation();
404 for (AlignmentAnnotation ann : alignmentAnnotations)
406 SequenceI seq = ann.sequenceRef;
409 Integer index = seqPositions.get(seq);
412 sequenceIndices.put(seq, index);
419 * Get the comparator for the specified sort order.
424 private Comparator<? super AlignmentAnnotation> getComparator(
425 SequenceAnnotationOrder order)
435 case SEQUENCE_AND_LABEL:
436 return this.bySequenceAndLabel;
437 case LABEL_AND_SEQUENCE:
438 return this.byLabelAndSequence;
440 throw new UnsupportedOperationException(order.toString());
445 * Non-case-sensitive comparison of annotation labels. Returns zero if either
452 private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)
454 if (o1 == null || o2 == null)
458 String label1 = o1.label;
459 String label2 = o2.label;
460 if (label1 == null && label2 == null)
472 return label1.toUpperCase().compareTo(label2.toUpperCase());
476 * Comparison based on position of associated sequence (if any) in the
483 private int compareSequences(AlignmentAnnotation o1,
484 AlignmentAnnotation o2)
486 SequenceI seq1 = o1.sequenceRef;
487 SequenceI seq2 = o2.sequenceRef;
488 if (seq1 == null && seq2 == null)
494 * Sort non-sequence-related before or after sequence-related
498 return showAutocalcAbove ? -1 : 1;
502 return showAutocalcAbove ? 1 : -1;
506 * else sort by associated sequence position
508 Integer index1 = sequenceIndices.get(seq1);
509 Integer index2 = sequenceIndices.get(seq2);
512 return index2 == null ? 0 : -1;
518 return Integer.compare(index1.intValue(), index2.intValue());
522 * Sort annotations to match the order of the provided list. This is intended
523 * only for use when reloading a project, in order to set the saved order
524 * after constructing a viewport (which might sort differently based on user
525 * preferences for sort order).
527 * There is some special handling specific to auto-calculated annotations.
528 * These are saved in the project, to preserve their visibility and height
529 * properties, but are recalculated when the viewport is reconstructed.
531 * <li>Autocalculated annotations are matched to the list by label
532 * "Consensus/Quality/Conservation/Occupancy"</li>
533 * <li>those that are matched have their visibility and height set</li>
536 * @param addedAnnotation
538 public void sort(List<AlignmentAnnotation> annotations)
540 Arrays.sort(alignment.getAlignmentAnnotation(),
541 new ReloadComparator(annotations));