JAL-1807 disambiguated method signatures with numeric primitive args
[jalview.git] / src / jalview / analysis / AlignmentSorter.java
index 0ea479d..4f7f2e2 100755 (executable)
@@ -1,26 +1,38 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- * 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.analysis;
 
-import java.util.*;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.AlignmentOrder;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.datamodel.SequenceNode;
+import jalview.util.Comparison;
+import jalview.util.MessageManager;
+import jalview.util.QuickSort;
 
-import jalview.datamodel.*;
-import jalview.util.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Routines for manipulating the order of a multiple sequence alignment TODO:
@@ -117,7 +129,7 @@ public class AlignmentSorter
       seqs[i] = align.getSequenceAt(i);
     }
 
-    QuickSort.sort(scores, 0, scores.length - 1, seqs);
+    QuickSort.sortFloat(scores, seqs);
 
     setReverseOrder(align, seqs);
   }
@@ -146,11 +158,15 @@ public class AlignmentSorter
     }
 
     // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
-    for (int i = 0; i < len; i++)
+    List<SequenceI> asq;
+    synchronized (asq = align.getSequences())
     {
-      // SequenceI tmp = seqs[i];
-      align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
-      align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
+      for (int i = 0; i < len; i++)
+      {
+        // SequenceI tmp = seqs[i];
+        asq.set(i, seqs[nSeq - i - 1]);
+        asq.set(nSeq - i - 1, seqs[i]);
+      }
     }
   }
 
@@ -162,7 +178,7 @@ public class AlignmentSorter
    * @param tmp
    *          sequences as a vector
    */
-  private static void setOrder(AlignmentI align, Vector tmp)
+  private static void setOrder(AlignmentI align, List<SequenceI> tmp)
   {
     setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
   }
@@ -178,24 +194,26 @@ public class AlignmentSorter
   public static void setOrder(AlignmentI align, SequenceI[] seqs)
   {
     // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
-    Vector algn = align.getSequences();
-    Vector tmp = new Vector();
-
-    for (int i = 0; i < seqs.length; i++)
+    List<SequenceI> algn;
+    synchronized (algn = align.getSequences())
     {
-      if (algn.contains(seqs[i]))
+      List<SequenceI> tmp = new ArrayList<SequenceI>();
+
+      for (int i = 0; i < seqs.length; i++)
       {
-        tmp.addElement(seqs[i]);
+        if (algn.contains(seqs[i]))
+        {
+          tmp.add(seqs[i]);
+        }
       }
-    }
 
-    algn.removeAllElements();
-    // User may have hidden seqs, then clicked undo or redo
-    for (int i = 0; i < tmp.size(); i++)
-    {
-      algn.addElement(tmp.elementAt(i));
+      algn.clear();
+      // User may have hidden seqs, then clicked undo or redo
+      for (int i = 0; i < tmp.size(); i++)
+      {
+        algn.add(tmp.get(i));
+      }
     }
-
   }
 
   /**
@@ -247,10 +265,10 @@ public class AlignmentSorter
     for (int i = 0; i < nSeq; i++)
     {
       seqs[i] = align.getSequenceAt(i);
-      length[i] = (float) (seqs[i].getEnd() - seqs[i].getStart());
+      length[i] = (seqs[i].getEnd() - seqs[i].getStart());
     }
 
-    QuickSort.sort(length, seqs);
+    QuickSort.sortFloat(length, seqs);
 
     if (sortLengthAscending)
     {
@@ -276,7 +294,7 @@ public class AlignmentSorter
   {
     // MAINTAINS ORIGNAL SEQUENCE ORDER,
     // ORDERS BY GROUP SIZE
-    Vector groups = new Vector();
+    List<SequenceGroup> groups = new ArrayList<SequenceGroup>();
 
     if (groups.hashCode() != lastGroupHash)
     {
@@ -290,17 +308,15 @@ public class AlignmentSorter
 
     // SORTS GROUPS BY SIZE
     // ////////////////////
-    for (int i = 0; i < align.getGroups().size(); i++)
+    for (SequenceGroup sg : align.getGroups())
     {
-      SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
-
       for (int j = 0; j < groups.size(); j++)
       {
-        SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
+        SequenceGroup sg2 = groups.get(j);
 
         if (sg.getSize() > sg2.getSize())
         {
-          groups.insertElementAt(sg, j);
+          groups.add(j, sg);
 
           break;
         }
@@ -308,22 +324,22 @@ public class AlignmentSorter
 
       if (!groups.contains(sg))
       {
-        groups.addElement(sg);
+        groups.add(sg);
       }
     }
 
     // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
     // /////////////////////////////////////////////
-    Vector seqs = new Vector();
+    List<SequenceI> seqs = new ArrayList<SequenceI>();
 
     for (int i = 0; i < groups.size(); i++)
     {
-      SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
+      SequenceGroup sg = groups.get(i);
       SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
 
       for (int j = 0; j < orderedseqs.length; j++)
       {
-        seqs.addElement(orderedseqs[j]);
+        seqs.add(orderedseqs[j]);
       }
     }
 
@@ -339,38 +355,24 @@ public class AlignmentSorter
   }
 
   /**
-   * Converts Vector to array. java 1.18 does not have Vector.toArray()
+   * Select sequences in order from tmp that is present in mask, and any
+   * remaining sequences in mask not in tmp
    * 
    * @param tmp
-   *          Vector of SequenceI objects
-   * 
-   * @return array of Sequence[]
-   */
-  private static SequenceI[] vectorToArray(Vector tmp)
-  {
-    SequenceI[] seqs = new SequenceI[tmp.size()];
-
-    for (int i = 0; i < tmp.size(); i++)
-    {
-      seqs[i] = (SequenceI) tmp.elementAt(i);
-    }
-
-    return seqs;
-  }
-
-  /**
-   * DOCUMENT ME!
-   * 
-   * @param tmp
-   *          DOCUMENT ME!
+   *          thread safe collection of sequences
    * @param mask
-   *          DOCUMENT ME!
+   *          thread safe collection of sequences
    * 
-   * @return DOCUMENT ME!
+   * @return intersect(tmp,mask)+intersect(complement(tmp),mask)
    */
-  private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
+  private static SequenceI[] vectorSubsetToArray(List<SequenceI> tmp,
+          List<SequenceI> mask)
   {
-    Vector seqs = new Vector();
+    // or?
+    // tmp2 = tmp.retainAll(mask);
+    // return tmp2.addAll(mask.removeAll(tmp2))
+
+    ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
     int i, idx;
     boolean[] tmask = new boolean[mask.size()];
 
@@ -381,12 +383,12 @@ public class AlignmentSorter
 
     for (i = 0; i < tmp.size(); i++)
     {
-      Object sq = tmp.elementAt(i);
+      SequenceI sq = tmp.get(i);
       idx = mask.indexOf(sq);
       if (idx > -1 && tmask[idx])
       {
         tmask[idx] = false;
-        seqs.addElement(sq);
+        seqs.add(sq);
       }
     }
 
@@ -394,11 +396,11 @@ public class AlignmentSorter
     {
       if (tmask[i])
       {
-        seqs.addElement(mask.elementAt(i));
+        seqs.add(mask.get(i));
       }
     }
 
-    return vectorToArray(seqs);
+    return seqs.toArray(new SequenceI[seqs.size()]);
   }
 
   /**
@@ -412,7 +414,7 @@ public class AlignmentSorter
   public static void sortBy(AlignmentI align, AlignmentOrder order)
   {
     // Get an ordered vector of sequences which may also be present in align
-    Vector tmp = order.getOrder();
+    List<SequenceI> tmp = order.getOrder();
 
     if (lastOrder == order)
     {
@@ -443,11 +445,12 @@ public class AlignmentSorter
    * 
    * @return DOCUMENT ME!
    */
-  private static Vector getOrderByTree(AlignmentI align, NJTree tree)
+  private static List<SequenceI> getOrderByTree(AlignmentI align,
+          NJTree tree)
   {
     int nSeq = align.getHeight();
 
-    Vector tmp = new Vector();
+    List<SequenceI> tmp = new ArrayList<SequenceI>();
 
     tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
 
@@ -456,15 +459,19 @@ public class AlignmentSorter
       // TODO: JBPNote - decide if this is always an error
       // (eg. not when a tree is associated to another alignment which has more
       // sequences)
-      if (tmp.size() < nSeq)
+      if (tmp.size() != nSeq)
       {
         addStrays(align, tmp);
       }
 
       if (tmp.size() != nSeq)
       {
-        System.err.println("ERROR: tmp.size()=" + tmp.size() + " != nseq="
-                + nSeq + " in getOrderByTree");
+        System.err
+                .println("WARNING: tmp.size()="
+                        + tmp.size()
+                        + " != nseq="
+                        + nSeq
+                        + " in getOrderByTree - tree contains sequences not in alignment");
       }
     }
 
@@ -481,7 +488,7 @@ public class AlignmentSorter
    */
   public static void sortByTree(AlignmentI align, NJTree tree)
   {
-    Vector tmp = getOrderByTree(align, tree);
+    List<SequenceI> tmp = getOrderByTree(align, tree);
 
     // tmp should properly permute align with tree.
     if (lastTree != tree)
@@ -509,22 +516,22 @@ public class AlignmentSorter
    * 
    * @param align
    *          DOCUMENT ME!
-   * @param seqs
+   * @param tmp
    *          DOCUMENT ME!
    */
-  private static void addStrays(AlignmentI align, Vector seqs)
+  private static void addStrays(AlignmentI align, List<SequenceI> tmp)
   {
     int nSeq = align.getHeight();
 
     for (int i = 0; i < nSeq; i++)
     {
-      if (!seqs.contains(align.getSequenceAt(i)))
+      if (!tmp.contains(align.getSequenceAt(i)))
       {
-        seqs.addElement(align.getSequenceAt(i));
+        tmp.add(align.getSequenceAt(i));
       }
     }
 
-    if (nSeq != seqs.size())
+    if (nSeq != tmp.size())
     {
       System.err
               .println("ERROR: Size still not right even after addStrays");
@@ -543,8 +550,9 @@ public class AlignmentSorter
    * 
    * @return DOCUMENT ME!
    */
-  private static Vector _sortByTree(SequenceNode node, Vector tmp,
-          Vector seqset)
+  private static List<SequenceI> _sortByTree(SequenceNode node,
+          List<SequenceI> tmp,
+          List<SequenceI> seqset)
   {
     if (node == null)
     {
@@ -560,9 +568,11 @@ public class AlignmentSorter
       {
         if (node.element() instanceof SequenceI)
         {
-          if (!tmp.contains(node.element()))
+          if (!tmp.contains(node.element())) // && (seqset==null ||
+                                             // seqset.size()==0 ||
+                                             // seqset.contains(tmp)))
           {
-            tmp.addElement((SequenceI) node.element());
+            tmp.add((SequenceI) node.element());
           }
         }
       }
@@ -597,7 +607,7 @@ public class AlignmentSorter
               .floatValue();
     }
 
-    jalview.util.QuickSort.sort(ids, alignment);
+    jalview.util.QuickSort.sortFloat(ids, alignment);
   }
 
   /**
@@ -664,7 +674,7 @@ public class AlignmentSorter
       }
     }
 
-    jalview.util.QuickSort.sort(scores, seqs);
+    jalview.util.QuickSort.sortDouble(scores, seqs);
     if (lastSortByScore != scoreLabel)
     {
       lastSortByScore = scoreLabel;
@@ -742,8 +752,7 @@ public class AlignmentSorter
     if (method != FEATURE_SCORE && method != FEATURE_LABEL
             && method != FEATURE_DENSITY)
     {
-      throw new Error(
-              "Implementation Error - sortByFeature method must be one of FEATURE_SCORE, FEATURE_LABEL or FEATURE_DENSITY.");
+      throw new Error(MessageManager.getString("error.implementation_error_sortbyfeature"));
     }
     boolean ignoreScore = method != FEATURE_SCORE;
     StringBuffer scoreLabel = new StringBuffer();
@@ -771,10 +780,6 @@ public class AlignmentSorter
     for (int i = 0; i < seqs.length; i++)
     {
       SequenceFeature[] sf = seqs[i].getSequenceFeatures();
-      if (sf == null && seqs[i].getDatasetSequence() != null)
-      {
-        sf = seqs[i].getDatasetSequence().getSequenceFeatures();
-      }
       if (sf == null)
       {
         sf = new SequenceFeature[0];
@@ -798,9 +803,8 @@ public class AlignmentSorter
         // filter for selection criteria
         if (
         // ignore features outwith alignment start-stop positions.
-        (sf[f].end < sstart || sf[f].begin > sstop)
-                ||
-                // or ignore based on selection criteria
+        (sf[f].end < sstart || sf[f].begin > sstop) ||
+        // or ignore based on selection criteria
                 (featureLabels != null && !AlignmentSorter
                         .containsIgnoreCase(sf[f].type, featureLabels))
                 || (groupLabels != null
@@ -901,7 +905,7 @@ public class AlignmentSorter
         }
       }
 
-      jalview.util.QuickSort.sort(scores, seqs);
+      jalview.util.QuickSort.sortDouble(scores, seqs);
     }
     else if (method == FEATURE_DENSITY)
     {
@@ -918,13 +922,13 @@ public class AlignmentSorter
         // System.err.println("Sorting on Density: seq "+seqs[i].getName()+
         // " Feats: "+nf+" Score : "+scores[i]);
       }
-      jalview.util.QuickSort.sort(scores, seqs);
+      jalview.util.QuickSort.sortDouble(scores, seqs);
     }
     else
     {
       if (method == FEATURE_LABEL)
       {
-        throw new Error("Not yet implemented.");
+        throw new Error(MessageManager.getString("error.not_yet_implemented"));
       }
     }
     if (lastSortByFeatureScore == null