FeatureColourI removed
[jalview.git] / src / jalview / analysis / AlignmentSorter.java
index cf5a57a..bb55426 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
+ * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -64,6 +64,8 @@ public class AlignmentSorter
    */
   private static String lastSortByFeatureScore;
 
+  private static boolean sortLengthAscending;
+
   /**
    * Sort by Percentage Identity w.r.t. s
    * 
@@ -77,6 +79,23 @@ public class AlignmentSorter
   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)
+  {
     int nSeq = align.getHeight();
 
     float[] scores = new float[nSeq];
@@ -202,6 +221,38 @@ public class AlignmentSorter
 
     sortIdAscending = !sortIdAscending;
   }
+  /**
+   * Sorts by sequence length
+   * 
+   * @param align
+   *                The alignment object to sort
+   */
+  public static void sortByLength(AlignmentI align)
+  {
+    int nSeq = align.getHeight();
+
+    float[] length = new float[nSeq];
+    SequenceI[] seqs = new SequenceI[nSeq];
+    
+    for (int i = 0; i < nSeq; i++)
+    {
+      seqs[i] = align.getSequenceAt(i);
+      length[i] = (float) (seqs[i].getEnd()-seqs[i].getStart());
+    }
+
+    QuickSort.sort(length, seqs);
+
+    if (sortLengthAscending)
+    {
+      setReverseOrder(align, seqs);
+    }
+    else
+    {
+      setOrder(align, seqs);
+    }
+
+    sortLengthAscending = !sortLengthAscending;
+  }
 
   /**
    * Sorts the alignment by size of group. <br>
@@ -718,9 +769,10 @@ public class AlignmentSorter
                 ||
                 // or ignore based on selection criteria
                 (featureLabels != null && !AlignmentSorter.containsIgnoreCase(sf[f].type, featureLabels))
-                || (groupLabels != null 
-                        && (sf[f].getFeatureGroup() == null 
-                                || !AlignmentSorter.containsIgnoreCase(sf[f].getFeatureGroup(), groupLabels))))
+                || (groupLabels != null
+                        // problem here: we cannot eliminate null feature group features
+                        && (sf[f].getFeatureGroup() != null 
+                                && !AlignmentSorter.containsIgnoreCase(sf[f].getFeatureGroup(), groupLabels))))
         {
           // forget about this feature
           sf[f] = null;