JAL-4062 appendResult(seq,start,end) called by structureSelectionManager sweep-search...
[jalview.git] / src / jalview / datamodel / SearchResults.java
index 6b067c8..8bca20d 100755 (executable)
@@ -22,7 +22,12 @@ package jalview.datamodel;
 
 import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.Collections;
 import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import com.google.common.collect.Lists;
 
 /**
  * Holds a list of search result matches, where each match is a contiguous
@@ -33,14 +38,15 @@ import java.util.List;
  */
 public class SearchResults implements SearchResultsI
 {
+  private int count;
 
-  private List<SearchResultMatchI> matches = new ArrayList<>();
+  private SortedSet<SearchResultMatchI> matches = new TreeSet<>();
 
   /**
    * One match consists of a sequence reference, start and end positions.
    * Discontiguous ranges in a sequence require two or more Match objects.
    */
-  public class Match implements SearchResultMatchI
+  public class Match implements SearchResultMatchI, Comparable<SearchResultMatchI>
   {
     final SequenceI sequence;
 
@@ -159,6 +165,39 @@ public class SearchResults implements SearchResultsI
     {
       return (sequence == seq && start <= from && end >= to);
     }
+    @Override
+    public boolean adjacent(SequenceI seq, int from, int to)
+    {
+      return (sequence == seq && ((start <= from && end >= to) || (from<=(end+1) && to >=(end+1)) || (from<=(start-1) && to>=(start-1))));
+    }
+
+    @Override
+    public int compareTo(SearchResultMatchI o)
+    {
+      if (start<o.getStart())
+      {
+        return -1;
+      }
+      if (start > o.getStart())
+      {
+        return +1;
+      }
+      if (end < o.getEnd())
+      {
+        return -1;
+      }
+      if (end > o.getEnd())
+      {
+        return +1;
+      }
+      if (sequence!=o.getSequence())
+      {
+        int hashc =sequence.hashCode(),oseq=o.getSequence().hashCode();
+        return (hashc < oseq) ? -1 : 1;
+      }
+      return 0;
+    }
+    
   }
 
   @Override
@@ -168,11 +207,78 @@ public class SearchResults implements SearchResultsI
     if (!matches.contains(m))
     {
       matches.add(m);
+      count++;
     }
     return m;
   }
 
   @Override
+  public void addResult(SequenceI seq, int[] positions)
+  {
+    /*
+     * we only increment the match count by 1 - or not at all,
+     * if the matches are all duplicates of existing
+     */
+    int beforeCount = count;
+    for (int i = 0; i < positions.length - 1; i += 2)
+    {
+      addResult(seq, positions[i], positions[i + 1]);
+    }
+    if (count > beforeCount)
+    {
+      count = beforeCount + 1;
+    }
+  }
+  
+
+  @Override
+  public boolean appendResult(SequenceI sequence, int start, int end)
+  {
+
+    Match m = new Match(sequence, start, end);
+    Match toAdd=null;
+    
+    if (matches.contains(m))
+    {
+      return false;
+    }
+    boolean appending=false;
+    
+    // we dynamically maintain an interval to add as we test each range in the list
+    
+    int cstart=start,cend=end;
+    List<SearchResultMatchI> toRemove=new ArrayList<>();
+    for (SearchResultMatchI thatm:matches)
+    {
+      if (thatm.getSequence()==sequence)
+      {
+        if (thatm.adjacent(sequence, cstart, cend))
+        {
+          // update the match to add with the adjacent start/end
+          start = Math.min(m.start, thatm.getStart());
+          end = Math.max(m.end, thatm.getEnd());
+          // and check if we keep or remove the old one
+          if (thatm.getStart()!=start || thatm.getEnd()!=end)
+          { 
+            toRemove.add(thatm);
+            count--;
+            cstart = start;
+            cend = end;
+            appending=true;
+          } else {
+            return false;
+          }
+        }
+      }
+    }
+    matches.removeAll(toRemove);
+    {
+      matches.add(new Match(sequence,cstart,cend));
+      count++;
+    }
+    return appending;
+  }
+  @Override
   public boolean involvesSequence(SequenceI sequence)
   {
     final int start = sequence.getStart();
@@ -214,8 +320,8 @@ public class SearchResults implements SearchResultsI
       {
         mfound = true;
         matchStart = sequence.findIndex(m.start) - 1;
-        matchEnd = m.start == m.end ? matchStart : sequence
-                .findIndex(m.end) - 1;
+        matchEnd = m.start == m.end ? matchStart
+                : sequence.findIndex(m.end) - 1;
       }
 
       if (mfound)
@@ -286,9 +392,9 @@ public class SearchResults implements SearchResultsI
   }
 
   @Override
-  public int getSize()
+  public int getCount()
   {
-    return matches.size();
+    return count;
   }
 
   @Override
@@ -300,7 +406,7 @@ public class SearchResults implements SearchResultsI
   @Override
   public List<SearchResultMatchI> getResults()
   {
-    return matches;
+    return List.copyOf(matches);
   }
 
   /**
@@ -350,4 +456,28 @@ public class SearchResults implements SearchResultsI
   {
     matches.addAll(toAdd.getResults());
   }
+
+  @Override
+  public List<SequenceI> getMatchingSubSequences()
+  {
+    List<SequenceI> seqs = new ArrayList<>();
+
+    /*
+     * assemble dataset sequences, and template new sequence features,
+     * for the amend features dialog
+     */
+    for (SearchResultMatchI match : matches)
+    {
+      SequenceI seq = match.getSequence();
+      while (seq.getDatasetSequence() != null)
+      {
+        seq = seq.getDatasetSequence();
+      }
+      // getSubSequence is index-base0, findIndex returns index-base1
+      seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1,
+              seq.findIndex(match.getEnd())));
+    }
+    return seqs;
+  }
+
 }