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 java.util.Locale;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceI;
29 import java.util.Arrays;
30 import java.util.Comparator;
31 import java.util.HashMap;
35 * A helper class to sort all annotations associated with an alignment in
41 public class AnnotationSorter
45 * enum for annotation sort options. The text description is used in the
46 * Preferences drop-down options. The enum name is saved in the preferences
52 public enum SequenceAnnotationOrder
54 // Text descriptions surface in the Preferences Sort by... options
55 SEQUENCE_AND_LABEL("Sequence"), LABEL_AND_SEQUENCE("Label"),
58 private String description;
60 private SequenceAnnotationOrder(String s)
66 public String toString()
71 public static SequenceAnnotationOrder forDescription(String d)
73 for (SequenceAnnotationOrder order : values())
75 if (order.toString().equals(d))
84 // the alignment with respect to which annotations are sorted
85 private final AlignmentI alignment;
87 // user preference for placement of non-sequence annotations
88 private boolean showAutocalcAbove;
90 // working map of sequence index in alignment
91 private final Map<SequenceI, Integer> sequenceIndices = new HashMap<SequenceI, Integer>();
94 * Constructor given an alignment and the location (top or bottom) of
95 * Consensus and similar.
98 * @param showAutocalculatedAbove
100 public AnnotationSorter(AlignmentI alignmentI,
101 boolean showAutocalculatedAbove)
103 this.alignment = alignmentI;
104 this.showAutocalcAbove = showAutocalculatedAbove;
108 * Default comparator sorts as follows by annotation type within sequence
111 * <li>annotations with a reference to a sequence in the alignment are sorted
112 * on sequence ordering</li>
113 * <li>other annotations go 'at the end', with their mutual order
115 * <li>within the same sequence ref, sort by label (non-case-sensitive)</li>
118 private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>()
121 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
123 if (o1 == null && o2 == null)
136 // TODO how to treat sequence-related autocalculated annotation
137 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
138 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
140 * Ignore label (keep existing ordering) for
141 * Conservation/Quality/Consensus etc
143 if (o1auto && o2auto)
149 * Sort autocalculated before or after sequence-related.
153 return showAutocalcAbove ? -1 : 1;
157 return showAutocalcAbove ? 1 : -1;
159 int sequenceOrder = compareSequences(o1, o2);
160 return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
164 public String toString()
166 return "Sort by sequence and label";
171 * This comparator sorts as follows by sequence order within annotation type
173 * <li>annotations with a reference to a sequence in the alignment are sorted
174 * on label (non-case-sensitive)</li>
175 * <li>other annotations go 'at the end', with their mutual order
177 * <li>within the same label, sort by order of the related sequences</li>
180 private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>()
183 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
185 if (o1 == null && o2 == null)
198 // TODO how to treat sequence-related autocalculated annotation
199 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
200 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
202 * Ignore label (keep existing ordering) for
203 * Conservation/Quality/Consensus etc
205 if (o1auto && o2auto)
211 * Sort autocalculated before or after sequence-related.
215 return showAutocalcAbove ? -1 : 1;
219 return showAutocalcAbove ? 1 : -1;
221 int labelOrder = compareLabels(o1, o2);
222 return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
226 public String toString()
228 return "Sort by label and sequence";
233 * noSort leaves sort order unchanged, within sequence- and autocalculated
234 * annotations, but may switch the ordering of these groups. Note this is
235 * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
236 * 'stable' (not change ordering of equal items).
238 private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
241 public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
243 // TODO how to treat sequence-related autocalculated annotation
244 boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
245 boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
246 // TODO skip this test to allow customised ordering of all annotations
247 // - needs a third option: place autocalculated first / last / none
248 if (o1 != null && o2 != null)
250 if (o1auto && !o2auto)
252 return showAutocalcAbove ? -1 : 1;
254 if (!o1auto && o2auto)
256 return showAutocalcAbove ? 1 : -1;
263 public String toString()
270 * Sort by the specified ordering of sequence-specific annotations.
272 * @param alignmentAnnotations
275 public void sort(AlignmentAnnotation[] alignmentAnnotations,
276 SequenceAnnotationOrder order)
278 if (alignmentAnnotations == null)
282 // cache 'alignment sequence position' for the annotations
283 saveSequenceIndices(alignmentAnnotations);
285 Comparator<? super AlignmentAnnotation> comparator = getComparator(
288 if (alignmentAnnotations != null)
290 synchronized (alignmentAnnotations)
292 Arrays.sort(alignmentAnnotations, comparator);
298 * Calculate and save in a temporary map the position of each annotation's
299 * sequence (if it has one) in the alignment. Faster to do this once than for
300 * every annotation comparison.
302 * @param alignmentAnnotations
304 private void saveSequenceIndices(
305 AlignmentAnnotation[] alignmentAnnotations)
307 sequenceIndices.clear();
308 for (AlignmentAnnotation ann : alignmentAnnotations)
310 SequenceI seq = ann.sequenceRef;
313 int index = AlignmentUtils.getSequenceIndex(alignment, seq);
314 sequenceIndices.put(seq, index);
320 * Get the comparator for the specified sort order.
325 private Comparator<? super AlignmentAnnotation> getComparator(
326 SequenceAnnotationOrder order)
336 case SEQUENCE_AND_LABEL:
337 return this.bySequenceAndLabel;
338 case LABEL_AND_SEQUENCE:
339 return this.byLabelAndSequence;
341 throw new UnsupportedOperationException(order.toString());
346 * Non-case-sensitive comparison of annotation labels. Returns zero if either
353 private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)
355 if (o1 == null || o2 == null)
359 String label1 = o1.label;
360 String label2 = o2.label;
361 if (label1 == null && label2 == null)
373 return label1.toUpperCase(Locale.ROOT)
374 .compareTo(label2.toUpperCase(Locale.ROOT));
378 * Comparison based on position of associated sequence (if any) in the
379 * alignment. Returns zero if either argument is null.
385 private int compareSequences(AlignmentAnnotation o1,
386 AlignmentAnnotation o2)
388 SequenceI seq1 = o1.sequenceRef;
389 SequenceI seq2 = o2.sequenceRef;
390 if (seq1 == null && seq2 == null)
395 * Sort non-sequence-related before or after sequence-related.
399 return showAutocalcAbove ? -1 : 1;
403 return showAutocalcAbove ? 1 : -1;
405 // get sequence index - but note -1 means 'at end' so needs special handling
406 int index1 = sequenceIndices.get(seq1);
407 int index2 = sequenceIndices.get(seq2);
408 if (index1 == index2)
420 return Integer.compare(index1, index2);