JAL-345 also include any sequences that have highlighted regions
[jalview.git] / src / jalview / controller / AlignViewController.java
index e44c32d..e9205f6 100644 (file)
@@ -29,6 +29,7 @@ import jalview.api.FeatureRenderer;
 import jalview.commands.OrderCommand;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
@@ -38,6 +39,7 @@ import jalview.io.FeaturesFile;
 import jalview.util.MessageManager;
 
 import java.awt.Color;
+import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.List;
 
@@ -53,20 +55,19 @@ public class AlignViewController implements AlignViewControllerI
   private AlignViewControllerGuiI avcg;
 
   public AlignViewController(AlignViewControllerGuiI alignFrame,
-          AlignViewportI viewport, AlignmentViewPanel alignPanel)
+          AlignViewportI vp, AlignmentViewPanel ap)
   {
     this.avcg = alignFrame;
-    this.viewport = viewport;
-    this.alignPanel = alignPanel;
+    this.viewport = vp;
+    this.alignPanel = ap;
   }
 
   @Override
-  public void setViewportAndAlignmentPanel(AlignViewportI viewport,
-          AlignmentViewPanel alignPanel)
+  public void setViewportAndAlignmentPanel(AlignViewportI vp,
+          AlignmentViewPanel ap)
   {
-    this.alignPanel = alignPanel;
-    this.viewport = viewport;
-
+    this.alignPanel = ap;
+    this.viewport = vp;
   }
 
   @Override
@@ -182,7 +183,7 @@ public class AlignViewController implements AlignViewControllerI
       if (changed)
       {
         viewport.setColumnSelection(cs);
-        alignPanel.paintAlignment(true);
+        alignPanel.paintAlignment(false, false);
         int columnCount = invert
                 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
                         - bs.cardinality()
@@ -207,7 +208,7 @@ public class AlignViewController implements AlignViewControllerI
       if (!extendCurrent)
       {
         cs.clear();
-        alignPanel.paintAlignment(true);
+        alignPanel.paintAlignment(false, false);
       }
     }
     return false;
@@ -215,17 +216,21 @@ public class AlignViewController implements AlignViewControllerI
 
   /**
    * Sets a bit in the BitSet for each column (base 0) in the sequence
-   * collection which includes the specified feature type. Returns the number of
-   * sequences which have the feature in the selected range.
+   * collection which includes a visible feature of the specified feature type.
+   * Returns the number of sequences which have the feature visible in the
+   * selected range.
    * 
    * @param featureType
    * @param sqcol
    * @param bs
    * @return
    */
-  static int findColumnsWithFeature(String featureType,
+  int findColumnsWithFeature(String featureType,
           SequenceCollectionI sqcol, BitSet bs)
   {
+    FeatureRenderer fr = alignPanel == null ? null : alignPanel
+            .getFeatureRenderer();
+
     final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
     final int endColumn = sqcol.getEndRes() + 1;
     List<SequenceI> seqs = sqcol.getSequences();
@@ -238,13 +243,19 @@ public class AlignViewController implements AlignViewControllerI
         List<SequenceFeature> sfs = sq.findFeatures(startColumn,
                 endColumn, featureType);
 
-        if (!sfs.isEmpty())
-        {
-          nseq++;
-        }
-
+        boolean found = false;
         for (SequenceFeature sf : sfs)
         {
+          if (fr.getColour(sf) == null)
+          {
+            continue;
+          }
+          if (!found)
+          {
+            nseq++;
+          }
+          found = true;
+
           int sfStartCol = sq.findIndex(sf.getBegin());
           int sfEndCol = sq.findIndex(sf.getEnd());
 
@@ -329,7 +340,7 @@ public class AlignViewController implements AlignViewControllerI
     AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
     avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
             viewport.getAlignment()));
-    alignPanel.paintAlignment(true);
+    alignPanel.paintAlignment(true, false);
 
   }
 
@@ -343,35 +354,135 @@ public class AlignViewController implements AlignViewControllerI
   public boolean parseFeaturesFile(String file, DataSourceType protocol,
           boolean relaxedIdMatching)
   {
-    boolean featuresFile = false;
+    boolean featuresAdded = false;
+    FeatureRenderer fr = alignPanel.getFeatureRenderer();
     try
     {
-      featuresFile = new FeaturesFile(false, file, protocol).parse(
-              viewport.getAlignment().getDataset(),
-              alignPanel.getFeatureRenderer().getFeatureColours(), false,
-              relaxedIdMatching);
+      featuresAdded = new FeaturesFile(false, file, protocol).parse(
+              viewport.getAlignment().getDataset(), fr.getFeatureColours(),
+              fr.getFeatureFilters(), false, relaxedIdMatching);
     } catch (Exception ex)
     {
       ex.printStackTrace();
     }
 
-    if (featuresFile)
+    if (featuresAdded)
     {
       avcg.refreshFeatureUI(true);
-      if (alignPanel.getFeatureRenderer() != null)
+      if (fr != null)
       {
         // update the min/max ranges where necessary
-        alignPanel.getFeatureRenderer().findAllFeatures(true);
+        fr.findAllFeatures(true);
       }
       if (avcg.getFeatureSettingsUI() != null)
       {
         avcg.getFeatureSettingsUI().discoverAllFeatureData();
       }
-      alignPanel.paintAlignment(true);
+      alignPanel.paintAlignment(true, true);
+    }
+
+    return featuresAdded;
+
+  }
+
+  /**
+   * 
+   * Add highlighted sequences to selected rows. Exclude highlighted sequences
+   * from selected rows. toggle inclusion or exclusion of highlighted sequences.
+   * or add/exclude/toggle for sequences not highlighted.
+   * 
+   * @param invert
+   *          - when true, sequences that are not highlighted are used to modify
+   *          selection
+   * @param extendCurrent
+   *          - normally true , the current selected group is modified.
+   * @param toggle
+   *          - if a select
+   * @return
+   */
+  public boolean selectHighlightedSequences(boolean invert,
+          boolean extendCurrent, boolean toggle)
+  {
+    List<SequenceI> results = alignPanel.getAlignViewport()
+            .getHighlightedSeqs();
+
+    SequenceGroup sq = (extendCurrent
+            && viewport.getSelectionGroup() != null)
+                    ? viewport.getSelectionGroup()
+                    : new SequenceGroup();
+
+    SearchResultsI searchResults = viewport.getSearchResults();
+    if (invert)
+    {
+      List<SequenceI> nothighlighted = new ArrayList();
+      for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
+              .getSequences())
+      {
+        if (!results.contains(seq) && (searchResults == null
+                || !searchResults.involvesSequence(seq)))
+        {
+          nothighlighted.add(seq);
+        }
+      }
+      results = nothighlighted;
+    }
+    else
+    {
+      // copy list and add in search results
+      results = new ArrayList(results);
+      if (searchResults != null)
+      {
+        for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
+                .getSequences())
+        {
+          if (searchResults.involvesSequence(seq))
+          {
+            results.add(seq);
+          }
+        }
+      }
+    }
+
+    if (results == null || results.size() == 0)
+    {
+      // do nothing if no selection exists
+      // unless toggle ??
+      return false;
+    }
+
+    boolean changed = false;
+
+    for (SequenceI seq : results)
+    {
+      int size = sq.getSize();
+      if (toggle)
+      {
+        sq.addOrRemove(seq, false);
+      }
+      else
+      {
+        sq.addSequence(seq, false);
+      }
+      changed |= size != sq.getSize();
+    }
+
+    if (sq.getSize() == 0)
+    {
+      viewport.setSelectionGroup(null);
+    }
+    else
+    {
+      if (sq != viewport.getSelectionGroup())
+    {
+      sq.setStartRes(0);
+      sq.setEndRes(viewport.getRanges().getAbsoluteAlignmentWidth());
+    }
+      viewport.setSelectionGroup(sq);
     }
 
-    return featuresFile;
+    alignPanel.paintAlignment(false, false);
 
+    return changed;
   }
 
   @Override
@@ -406,7 +517,7 @@ public class AlignViewController implements AlignViewControllerI
       if (changed)
       {
         viewport.setColumnSelection(cs);
-        alignPanel.paintAlignment(true);
+        alignPanel.paintAlignment(false, false);
         int columnCount = invert
                 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
                         - bs.cardinality()
@@ -430,7 +541,7 @@ public class AlignViewController implements AlignViewControllerI
       if (!extendCurrent)
       {
         cs.clear();
-        alignPanel.paintAlignment(true);
+        alignPanel.paintAlignment(false, false);
       }
     }
     return false;