Merge branch 'develop' into patch/JAL-4281_idwidthandannotHeight_in_project
[jalview.git] / src / jalview / datamodel / SearchResults.java
index 2ba1612..546dd6f 100755 (executable)
@@ -21,7 +21,6 @@
 package jalview.datamodel;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.BitSet;
 import java.util.List;
 
@@ -34,26 +33,27 @@ import java.util.List;
  */
 public class SearchResults implements SearchResultsI
 {
+  private int count;
 
-  private List<SearchResultMatchI> matches = new ArrayList<SearchResultMatchI>();
+  private ArrayList<SearchResultMatchI> matches = new ArrayList<>();
 
   /**
    * 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>
   {
-    SequenceI sequence;
+    final SequenceI sequence;
 
     /**
      * Start position of match in sequence (base 1)
      */
-    int start;
+    final int start;
 
     /**
      * End position (inclusive) (base 1)
      */
-    int end;
+    final int end;
 
     /**
      * create a Match on a range of sequence. Match always holds region in
@@ -92,27 +92,18 @@ public class SearchResults implements SearchResultsI
       }
     }
 
-    /* (non-Javadoc)
-     * @see jalview.datamodel.SearchResultMatchI#getSequence()
-     */
     @Override
     public SequenceI getSequence()
     {
       return sequence;
     }
 
-    /* (non-Javadoc)
-     * @see jalview.datamodel.SearchResultMatchI#getStart()
-     */
     @Override
     public int getStart()
     {
       return start;
     }
 
-    /* (non-Javadoc)
-     * @see jalview.datamodel.SearchResultMatchI#getEnd()
-     */
     @Override
     public int getEnd()
     {
@@ -120,33 +111,18 @@ public class SearchResults implements SearchResultsI
     }
 
     /**
-     * Returns the string of characters in the matched region, prefixed by the
-     * start position, e.g. "12CGT" or "208K"
+     * Returns a representation as "seqid/start-end"
      */
     @Override
     public String toString()
     {
-      final int from = Math.max(start - 1, 0);
-      String startPosition = String.valueOf(from);
-      return startPosition + getCharacters();
-    }
-
-    /* (non-Javadoc)
-     * @see jalview.datamodel.SearchResultMatchI#getCharacters()
-     */
-    @Override
-    public String getCharacters()
-    {
-      char[] chars = sequence.getSequence();
-      // convert start/end to base 0 (with bounds check)
-      final int from = Math.max(start - 1, 0);
-      final int to = Math.min(end, chars.length + 1);
-      return String.valueOf(Arrays.copyOfRange(chars, from, to));
-    }
-
-    public void setSequence(SequenceI seq)
-    {
-      this.sequence = seq;
+      StringBuilder sb = new StringBuilder();
+      if (sequence != null)
+      {
+        sb.append(sequence.getName()).append("/");
+      }
+      sb.append(start).append("-").append(end);
+      return sb.toString();
     }
 
     /**
@@ -175,35 +151,140 @@ public class SearchResults implements SearchResultsI
         return false;
       }
       SearchResultMatchI m = (SearchResultMatchI) obj;
-      return (sequence == m.getSequence() && start == m.getStart() && end == m
-              .getEnd());
+      return (sequence == m.getSequence() && start == m.getStart()
+              && end == m.getEnd());
+    }
+
+    @Override
+    public boolean contains(SequenceI seq, int from, int to)
+    {
+      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;
     }
+    
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
-   */
   @Override
   public SearchResultMatchI addResult(SequenceI seq, int start, int end)
   {
     Match m = new Match(seq, start, end);
-    matches.add(m);
+    if (!matches.contains(m))
+    {
+      matches.add(m);
+      count++;
+    }
     return m;
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#involvesSequence(jalview.datamodel.SequenceI)
-   */
+  @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);
+    
+    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.contains(sequence,cstart,cend))
+        {
+          // found a match containing the current range. nothing else to do except report if we operated on the list
+          return appending;
+        }
+        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();
+    final int end = sequence.getEnd();
+
     SequenceI ds = sequence.getDatasetSequence();
-    Match m;
-    for (SearchResultMatchI _m : matches)
+    for (SearchResultMatchI m : matches)
     {
-      m = (Match) _m;
-      if (m.sequence != null
-              && (m.sequence == sequence || m.sequence == ds))
+      SequenceI matched = m.getSequence();
+      if (matched != null && (matched == sequence || matched == ds)
+              && (m.getEnd() >= start) && (m.getStart() <= end))
       {
         return true;
       }
@@ -211,9 +292,6 @@ public class SearchResults implements SearchResultsI
     return false;
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getResults(jalview.datamodel.SequenceI, int, int)
-   */
   @Override
   public int[] getResults(SequenceI sequence, int start, int end)
   {
@@ -232,20 +310,15 @@ public class SearchResults implements SearchResultsI
       m = (Match) _m;
 
       mfound = false;
-      if (m.sequence == sequence)
-      {
-        mfound = true;
-        // locate aligned position
-        matchStart = sequence.findIndex(m.start) - 1;
-        matchEnd = sequence.findIndex(m.end) - 1;
-      }
-      else if (m.sequence == sequence.getDatasetSequence())
+      if (m.sequence == sequence
+              || m.sequence == sequence.getDatasetSequence())
       {
         mfound = true;
-        // locate region in local context
         matchStart = sequence.findIndex(m.start) - 1;
-        matchEnd = sequence.findIndex(m.end) - 1;
+        matchEnd = m.start == m.end ? matchStart
+                : sequence.findIndex(m.end) - 1;
       }
+
       if (mfound)
       {
         if (matchStart <= end && matchEnd >= start)
@@ -277,7 +350,7 @@ public class SearchResults implements SearchResultsI
         else
         {
           // debug
-          // System.err.println("Outwith bounds!" + matchStart+">"+end +"  or "
+          // jalview.bin.Console.errPrintln("Outwith bounds!" + matchStart+">"+end +" or "
           // + matchEnd+"<"+start);
         }
       }
@@ -290,9 +363,12 @@ public class SearchResults implements SearchResultsI
   {
     int count = 0;
     BitSet mask = new BitSet();
+    int startRes = sqcol.getStartRes();
+    int endRes = sqcol.getEndRes();
+
     for (SequenceI s : sqcol.getSequences())
     {
-      int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes());
+      int[] cols = getResults(s, startRes, endRes);
       if (cols != null)
       {
         for (int pair = 0; pair < cols.length; pair += 2)
@@ -310,54 +386,18 @@ public class SearchResults implements SearchResultsI
     return count;
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getSize()
-   */
   @Override
-  public int getSize()
+  public int getCount()
   {
-    return matches.size();
-  }
-
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getResultSequence(int)
-   */
-  @Override
-  public SequenceI getResultSequence(int index)
-  {
-    return matches.get(index).getSequence();
-  }
-
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getResultStart(int)
-   */
-  @Override
-  public int getResultStart(int i)
-  {
-    return matches.get(i).getStart();
-  }
-
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getResultEnd(int)
-   */
-  @Override
-  public int getResultEnd(int i)
-  {
-    return matches.get(i).getEnd();
+    return count;
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#isEmpty()
-   */
   @Override
   public boolean isEmpty()
   {
     return matches.isEmpty();
   }
 
-  /* (non-Javadoc)
-   * @see jalview.datamodel.SearchResultsI#getResults()
-   */
   @Override
   public List<SearchResultMatchI> getResults()
   {
@@ -365,44 +405,23 @@ public class SearchResults implements SearchResultsI
   }
 
   /**
-   * Return the results as a string of characters (bases) prefixed by start
-   * position(s). Meant for use when the context ensures that all matches are to
-   * regions of the same sequence (otherwise the result is meaningless).
+   * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
    * 
    * @return
    */
   @Override
   public String toString()
   {
-    StringBuilder result = new StringBuilder(256);
-    for (SearchResultMatchI m : matches)
-    {
-      result.append(m.toString());
-    }
-    return result.toString();
+    return matches == null ? "" : matches.toString();
   }
 
   /**
-   * Return the results as a string of characters (bases). Meant for use when
-   * the context ensures that all matches are to regions of the same sequence
-   * (otherwise the result is meaningless).
-   * 
-   * @return
-   */
-  public String getCharacters()
-  {
-    StringBuilder result = new StringBuilder(256);
-    for (SearchResultMatchI m : matches)
-    {
-      result.append(m.getCharacters());
-    }
-    return result.toString();
-  }
-
-  /**
-   * Hashcode is has derived from the list of matches. This ensures that when
-   * two SearchResults objects satisfy the test for equals(), then they have the
+   * Hashcode is derived from the list of matches. This ensures that when two
+   * SearchResults objects satisfy the test for equals(), then they have the
    * same hashcode.
+   * 
+   * @see Match#hashCode()
+   * @see java.util.AbstractList#hashCode()
    */
   @Override
   public int hashCode()
@@ -411,8 +430,10 @@ public class SearchResults implements SearchResultsI
   }
 
   /**
-   * Two SearchResults are considered equal if they contain the same matches in
-   * the same order.
+   * Two SearchResults are considered equal if they contain the same matches
+   * (Sequence, start position, end position) in the same order
+   * 
+   * @see Match#equals(Object)
    */
   @Override
   public boolean equals(Object obj)
@@ -424,4 +445,34 @@ public class SearchResults implements SearchResultsI
     SearchResultsI sr = (SearchResultsI) obj;
     return matches.equals(sr.getResults());
   }
+
+  @Override
+  public void addSearchResults(SearchResultsI toAdd)
+  {
+    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;
+  }
+
 }