JAL-345 javadoc and note about JAL-2300
[jalview.git] / src / jalview / datamodel / SearchResults.java
index 2f62991..2ba1612 100755 (executable)
@@ -22,6 +22,7 @@ package jalview.datamodel;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.BitSet;
 import java.util.List;
 
 /**
@@ -34,7 +35,7 @@ import java.util.List;
 public class SearchResults implements SearchResultsI
 {
 
-  private List<Match> matches = new ArrayList<Match>();
+  private List<SearchResultMatchI> matches = new ArrayList<SearchResultMatchI>();
 
   /**
    * One match consists of a sequence reference, start and end positions.
@@ -55,7 +56,10 @@ public class SearchResults implements SearchResultsI
     int end;
 
     /**
-     * Constructor
+     * create a Match on a range of sequence. Match always holds region in
+     * forwards order, even if given in reverse order (such as from a mapping to
+     * a reverse strand); this avoids trouble for routines that highlight search
+     * results etc
      * 
      * @param seq
      *          a sequence
@@ -80,6 +84,9 @@ public class SearchResults implements SearchResultsI
       }
       else
       {
+        // TODO: JBP could mark match as being specified in reverse direction
+        // for use
+        // by caller ? e.g. visualizing reverse strand highlight
         this.start = end;
         this.end = start;
       }
@@ -163,12 +170,13 @@ public class SearchResults implements SearchResultsI
     @Override
     public boolean equals(Object obj)
     {
-      if (obj == null || !(obj instanceof Match))
+      if (obj == null || !(obj instanceof SearchResultMatchI))
       {
         return false;
       }
-      Match m = (Match) obj;
-      return (this.sequence == m.sequence && this.start == m.start && this.end == m.end);
+      SearchResultMatchI m = (SearchResultMatchI) obj;
+      return (sequence == m.getSequence() && start == m.getStart() && end == m
+              .getEnd());
     }
   }
 
@@ -176,9 +184,11 @@ public class SearchResults implements SearchResultsI
    * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
    */
   @Override
-  public void addResult(SequenceI seq, int start, int end)
+  public SearchResultMatchI addResult(SequenceI seq, int start, int end)
   {
-    matches.add(new Match(seq, start, end));
+    Match m = new Match(seq, start, end);
+    matches.add(m);
+    return m;
   }
 
   /* (non-Javadoc)
@@ -188,8 +198,10 @@ public class SearchResults implements SearchResultsI
   public boolean involvesSequence(SequenceI sequence)
   {
     SequenceI ds = sequence.getDatasetSequence();
-    for (Match m : matches)
+    Match m;
+    for (SearchResultMatchI _m : matches)
     {
+      m = (Match) _m;
       if (m.sequence != null
               && (m.sequence == sequence || m.sequence == ds))
       {
@@ -214,8 +226,11 @@ public class SearchResults implements SearchResultsI
     int[] tmp = null;
     int resultLength, matchStart = 0, matchEnd = 0;
     boolean mfound;
-    for (Match m : matches)
+    Match m;
+    for (SearchResultMatchI _m : matches)
     {
+      m = (Match) _m;
+
       mfound = false;
       if (m.sequence == sequence)
       {
@@ -270,6 +285,31 @@ public class SearchResults implements SearchResultsI
     return result;
   }
 
+  @Override
+  public int markColumns(SequenceCollectionI sqcol, BitSet bs)
+  {
+    int count = 0;
+    BitSet mask = new BitSet();
+    for (SequenceI s : sqcol.getSequences())
+    {
+      int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes());
+      if (cols != null)
+      {
+        for (int pair = 0; pair < cols.length; pair += 2)
+        {
+          mask.set(cols[pair], cols[pair + 1] + 1);
+        }
+      }
+    }
+    // compute columns that were newly selected
+    BitSet original = (BitSet) bs.clone();
+    original.and(mask);
+    count = mask.cardinality() - original.cardinality();
+    // and mark ranges not already marked
+    bs.or(mask);
+    return count;
+  }
+
   /* (non-Javadoc)
    * @see jalview.datamodel.SearchResultsI#getSize()
    */
@@ -285,7 +325,7 @@ public class SearchResults implements SearchResultsI
   @Override
   public SequenceI getResultSequence(int index)
   {
-    return matches.get(index).sequence;
+    return matches.get(index).getSequence();
   }
 
   /* (non-Javadoc)
@@ -294,7 +334,7 @@ public class SearchResults implements SearchResultsI
   @Override
   public int getResultStart(int i)
   {
-    return matches.get(i).start;
+    return matches.get(i).getStart();
   }
 
   /* (non-Javadoc)
@@ -303,7 +343,7 @@ public class SearchResults implements SearchResultsI
   @Override
   public int getResultEnd(int i)
   {
-    return matches.get(i).end;
+    return matches.get(i).getEnd();
   }
 
   /* (non-Javadoc)
@@ -319,7 +359,7 @@ public class SearchResults implements SearchResultsI
    * @see jalview.datamodel.SearchResultsI#getResults()
    */
   @Override
-  public List<Match> getResults()
+  public List<SearchResultMatchI> getResults()
   {
     return matches;
   }
@@ -377,11 +417,11 @@ public class SearchResults implements SearchResultsI
   @Override
   public boolean equals(Object obj)
   {
-    if (obj == null || !(obj instanceof SearchResults))
+    if (obj == null || !(obj instanceof SearchResultsI))
     {
       return false;
     }
-    SearchResults sr = (SearchResults) obj;
-    return ((ArrayList<Match>) this.matches).equals(sr.matches);
+    SearchResultsI sr = (SearchResultsI) obj;
+    return matches.equals(sr.getResults());
   }
 }