JAL-3081 refactored AnnotationSorter constructor and sort parameters
[jalview.git] / src / jalview / analysis / AnnotationSorter.java
index f16d9ea..83f3adf 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.analysis;
 
+import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
@@ -83,33 +84,35 @@ public class AnnotationSorter
     }
   }
 
-  // the alignment with respect to which annotations are sorted
+  /*
+   * the alignment with respect to which annotations are sorted
+   */
   private final AlignmentI alignment;
 
-  // user preference for placement of non-sequence annotations
+  /*
+   * if true, autocalculated are sorted first, if false, last
+   */
   private boolean showAutocalcAbove;
 
-  // working map of sequence index in alignment
+  /*
+   * working map of sequence index in alignment
+   */
   private final Map<SequenceI, Integer> sequenceIndices = new HashMap<>();
 
-  // if true, sort only repositions auto-calculated annotation (to top or
-  // bottom)
-  private final boolean autocalcOnly;
+  /*
+   * if true, sort only repositions auto-calculated annotation (to top or bottom)
+   */
+  private boolean autocalcOnly;
 
   /**
-   * Constructor given an alignment and the location (top or bottom) of
-   * Consensus and similar.
+   * Constructor
    * 
-   * @param alignmentI
-   * @param showAutocalculatedAbove
-   * @param autoCalcOnly
+   * @param av
    */
-  public AnnotationSorter(AlignmentI alignmentI,
-          boolean showAutocalculatedAbove, boolean autoCalcOnly)
+  public AnnotationSorter(AlignViewportI av)
   {
-    this.alignment = alignmentI;
-    this.showAutocalcAbove = showAutocalculatedAbove;
-    this.autocalcOnly = autoCalcOnly;
+    this.alignment = av.getAlignment();
+    this.showAutocalcAbove = av.isShowAutocalculatedAbove();
   }
 
   /**
@@ -283,55 +286,56 @@ public class AnnotationSorter
   };
 
   /**
-   * Sort by the specified ordering of sequence-specific annotations.
+   * Sorts by the specified ordering. If order is {@code CUSTOM}, meaning
+   * annotations have been manually ordered by the user, no sort is performed.
    * 
-   * @param alignmentAnnotations
-   * @param order
+   * @param sortBy
+   *          the sort order to apply
+   * @param autoCalcOnly
+   *          if true, only autocalculated annotations are repositioned (to top
+   *          or bottom), others are left in their current order
    */
-  public void sort(AlignmentAnnotation[] alignmentAnnotations,
-          SequenceAnnotationOrder order)
+  public void sort(SequenceAnnotationOrder sortBy, boolean autoCalcOnly)
   {
-    if (alignmentAnnotations == null
-            || order == SequenceAnnotationOrder.CUSTOM)
+    if (sortBy == null || sortBy == SequenceAnnotationOrder.CUSTOM)
     {
       return;
     }
 
+    this.autocalcOnly = autoCalcOnly;
+
     /*
      * cache 'alignment sequence positions' if required for sorting
      */
-    if (order == SequenceAnnotationOrder.SEQUENCE_AND_LABEL
-            || order == SequenceAnnotationOrder.LABEL_AND_SEQUENCE)
+    if (sortBy == SequenceAnnotationOrder.SEQUENCE_AND_LABEL
+            || sortBy == SequenceAnnotationOrder.LABEL_AND_SEQUENCE)
     {
-      saveSequenceIndices(alignmentAnnotations);
+      saveSequenceIndices();
     }
 
     Comparator<? super AlignmentAnnotation> comparator = getComparator(
-            order);
+            sortBy);
 
-    if (alignmentAnnotations != null)
+    AlignmentAnnotation[] annotations = alignment.getAlignmentAnnotation();
+    synchronized (annotations)
     {
-      synchronized (alignmentAnnotations)
-      {
-        Arrays.sort(alignmentAnnotations, comparator);
-      }
+      Arrays.sort(annotations, comparator);
     }
   }
 
   /**
-   * Calculate and save in a temporary map the position of each annotation's
-   * sequence (if it has one) in the alignment. Faster to do this once than for
-   * every annotation comparison.
-   * 
-   * @param alignmentAnnotations
+   * Calculates and saves in a temporary map the position of each annotation's
+   * associated sequence (if it has one) in the alignment. Faster to do this
+   * once than for every annotation comparison.
    */
-  private void saveSequenceIndices(
-          AlignmentAnnotation[] alignmentAnnotations)
+  private void saveSequenceIndices()
   {
     sequenceIndices.clear();
 
     Map<SequenceI, Integer> seqPositions = alignment.getSequencePositions();
 
+    AlignmentAnnotation[] alignmentAnnotations = alignment
+            .getAlignmentAnnotation();
     for (AlignmentAnnotation ann : alignmentAnnotations)
     {
       SequenceI seq = ann.sequenceRef;