JAL-2505 unit test for sort by feature density, Javadoc
[jalview.git] / src / jalview / analysis / AlignmentSorter.java
index 007d538..cc4c469 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.analysis;
 
+import jalview.analysis.scoremodels.PIDModel;
+import jalview.analysis.scoremodels.SimilarityParams;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
@@ -27,7 +29,6 @@ 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;
 
@@ -65,7 +66,7 @@ public class AlignmentSorter
 
   static boolean sortOrderAscending = true;
 
-  static NJTree lastTree = null;
+  static TreeModel lastTree = null;
 
   static boolean sortTreeAscending = true;
 
@@ -86,46 +87,29 @@ public class AlignmentSorter
   private static boolean sortLengthAscending;
 
   /**
-   * Sort by Percentage Identity w.r.t. s
+   * Sorts sequences in the alignment by Percentage Identity with the given
+   * reference sequence, sorting the highest identity to the top
    * 
    * @param align
    *          AlignmentI
    * @param s
    *          SequenceI
-   * @param tosort
-   *          sequences from align that are to be sorted.
-   */
-  public static void sortByPID(AlignmentI align, SequenceI s,
-          SequenceI[] tosort)
-  {
-    sortByPID(align, s, tosort, 0, -1);
-  }
-
-  /**
-   * Sort by Percentage Identity w.r.t. s
-   * 
-   * @param align
-   *          AlignmentI
-   * @param s
-   *          SequenceI
-   * @param tosort
-   *          sequences from align that are to be sorted.
-   * @param start
-   *          start column (0 for beginning
    * @param end
    */
-  public static void sortByPID(AlignmentI align, SequenceI s,
-          SequenceI[] tosort, int start, int end)
+  public static void sortByPID(AlignmentI align, SequenceI s)
   {
     int nSeq = align.getHeight();
 
     float[] scores = new float[nSeq];
     SequenceI[] seqs = new SequenceI[nSeq];
+    String refSeq = s.getSequenceAsString();
 
+    SimilarityParams pidParams = new SimilarityParams(true, true, true,
+            true);
     for (int i = 0; i < nSeq; i++)
     {
-      scores[i] = Comparison.PID(align.getSequenceAt(i)
-              .getSequenceAsString(), s.getSequenceAsString());
+      scores[i] = (float) PIDModel.computePID(align.getSequenceAt(i)
+              .getSequenceAsString(), refSeq, pidParams);
       seqs[i] = align.getSequenceAt(i);
     }
 
@@ -446,7 +430,7 @@ public class AlignmentSorter
    * @return DOCUMENT ME!
    */
   private static List<SequenceI> getOrderByTree(AlignmentI align,
-          NJTree tree)
+          TreeModel tree)
   {
     int nSeq = align.getHeight();
 
@@ -486,7 +470,7 @@ public class AlignmentSorter
    * @param tree
    *          tree which has
    */
-  public static void sortByTree(AlignmentI align, NJTree tree)
+  public static void sortByTree(AlignmentI align, TreeModel tree)
   {
     List<SequenceI> tmp = getOrderByTree(align, tree);
 
@@ -697,34 +681,8 @@ public class AlignmentSorter
 
   public static String FEATURE_DENSITY = "density";
 
-  /**
-   * sort the alignment using the features on each sequence found between start
-   * and stop with the given featureLabel (and optional group qualifier)
-   * 
-   * @param featureLabel
-   *          (may not be null)
-   * @param groupLabel
-   *          (may be null)
-   * @param start
-   *          (-1 to include non-positional features)
-   * @param stop
-   *          (-1 to only sort on non-positional features)
-   * @param alignment
-   *          - aligned sequences containing features
-   * @param method
-   *          - one of the string constants FEATURE_SCORE, FEATURE_LABEL,
-   *          FEATURE_DENSITY
-   */
-  public static void sortByFeature(String featureLabel, String groupLabel,
-          int start, int stop, AlignmentI alignment, String method)
-  {
-    sortByFeature(featureLabel == null ? null
-            : new String[] { featureLabel }, groupLabel == null ? null
-            : new String[] { groupLabel }, start, stop, alignment, method);
-  }
-
   private static boolean containsIgnoreCase(final String lab,
-          final String[] labs)
+          final List<String> labs)
   {
     if (labs == null)
     {
@@ -734,9 +692,9 @@ public class AlignmentSorter
     {
       return false;
     }
-    for (int q = 0; q < labs.length; q++)
+    for (String label : labs)
     {
-      if (labs[q] != null && lab.equalsIgnoreCase(labs[q]))
+      if (lab.equalsIgnoreCase(label))
       {
         return true;
       }
@@ -744,9 +702,29 @@ public class AlignmentSorter
     return false;
   }
 
-  public static void sortByFeature(String[] featureLabels,
-          String[] groupLabels, int start, int stop, AlignmentI alignment,
-          String method)
+  /**
+   * Sort sequences by feature score or density, optionally restricted by
+   * feature types, feature groups, or alignment start/end positions.
+   * <p>
+   * If the sort is repeated for the same combination of types and groups, sort
+   * order is reversed.
+   * 
+   * @param featureLabels
+   *          a list of feature types to include (or null for all)
+   * @param groupLabels
+   *          a list of feature groups to include (or null for all)
+   * @param start
+   *          start column position to include (base zero)
+   * @param stop
+   *          end column position to include (base zero)
+   * @param alignment
+   *          the alignment to be sorted
+   * @param method
+   *          either "average_score" or "density" ("text" not yet implemented)
+   */
+  public static void sortByFeature(List<String> featureLabels,
+          List<String> groupLabels, int start, int stop,
+          AlignmentI alignment, String method)
   {
     if (method != FEATURE_SCORE && method != FEATURE_LABEL
             && method != FEATURE_DENSITY)
@@ -761,14 +739,19 @@ public class AlignmentSorter
     scoreLabel.append(start + stop + method);
     // This doesn't quite work yet - we'd like to have a canonical ordering that
     // can be preserved from call to call
-    for (int i = 0; featureLabels != null && i < featureLabels.length; i++)
+    if (featureLabels != null)
     {
-      scoreLabel.append(featureLabels[i] == null ? "null"
-              : featureLabels[i]);
+      for (String label : featureLabels)
+      {
+        scoreLabel.append(label);
+      }
     }
-    for (int i = 0; groupLabels != null && i < groupLabels.length; i++)
+    if (groupLabels != null)
     {
-      scoreLabel.append(groupLabels[i] == null ? "null" : groupLabels[i]);
+      for (String label : groupLabels)
+      {
+        scoreLabel.append(label);
+      }
     }
 
     /*