JAL-1483 sort by feature score/density made first class alignment view controller...
[jalview.git] / src / jalview / controller / AlignViewController.java
index 077f9cd..3cf8000 100644 (file)
 package jalview.controller;
 
 import java.awt.Color;
+import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.List;
 
+import jalview.analysis.AlignmentSorter;
 import jalview.api.AlignViewControllerGuiI;
 import jalview.api.AlignViewControllerI;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
+import jalview.api.FeatureRenderer;
+import jalview.commands.OrderCommand;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.datamodel.ColumnSelection;
@@ -294,4 +298,74 @@ public class AlignViewController implements AlignViewControllerI
       return false;
     }
   }
+
+  @Override
+  public void sortAlignmentByFeatureDensity(String[] typ)
+  {
+    sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
+  }
+
+  protected void sortBy(String[] typ, String methodText, final String method)
+  {
+    FeatureRenderer fr = alignPanel.getFeatureRenderer();
+    if (typ == null)
+    {
+      typ = fr==null ? null : fr.getDisplayedFeatureTypes();
+    }
+    String gps[] = null;
+    gps = fr==null ? null : fr.getDisplayedFeatureGroups();
+    if (typ != null)
+    {
+      ArrayList types = new ArrayList();
+      for (int i = 0; i < typ.length; i++)
+      {
+        if (typ[i] != null)
+        {
+          types.add(typ[i]);
+        }
+        typ = new String[types.size()];
+        types.toArray(typ);
+      }
+    }
+    if (gps != null)
+    {
+      ArrayList grps = new ArrayList();
+
+      for (int i = 0; i < gps.length; i++)
+      {
+        if (gps[i] != null)
+        {
+          grps.add(gps[i]);
+        }
+      }
+      gps = new String[grps.size()];
+      grps.toArray(gps);
+    }
+    AlignmentI al = viewport.getAlignment();
+
+    int start, stop;
+    SequenceGroup sg = viewport.getSelectionGroup();
+    if (sg != null)
+    {
+      start = sg.getStartRes();
+      stop = sg.getEndRes();
+    }
+    else
+    {
+      start = 0;
+      stop = al.getWidth();
+    }
+    SequenceI[] oldOrder = al.getSequencesArray();
+    AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
+    avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
+            .getAlignment()));
+    alignPanel.paintAlignment(true);
+
+  }
+
+  @Override
+  public void sortAlignmentByFeatureScore(String[] typ)
+  {
+    sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
+  }
 }