JAL-1738 JAL-345 fix for JAL-2302 only report Matches that are not contained by the...
[jalview.git] / src / jalview / analysis / Finder.java
index 224c60a..ff03788 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -21,7 +21,9 @@
 package jalview.analysis;
 
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SearchResultMatchI;
 import jalview.datamodel.SearchResults;
+import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
 
@@ -32,7 +34,7 @@ public class Finder
   /**
    * Implements the search algorithms for the Find dialog box.
    */
-  SearchResults searchResults;
+  SearchResultsI searchResults;
 
   AlignmentI alignment;
 
@@ -102,6 +104,7 @@ public class Finder
         selection = null;
       }
     }
+    SearchResultMatchI lastm = null;
 
     while (!found && (seqIndex < end))
     {
@@ -152,7 +155,8 @@ public class Finder
           }
         }
 
-        if (isIncludeDescription() && regex.search(seq.getDescription()))
+        if (isIncludeDescription() && seq.getDescription() != null
+                && regex.search(seq.getDescription()))
         {
           idMatch.addElement(seq);
           hasResults = true;
@@ -191,7 +195,6 @@ public class Finder
       }
 
       String noGaps = noGapsSB.toString();
-
       for (int r = resIndex; r < noGaps.length(); r++)
       {
 
@@ -205,7 +208,7 @@ public class Finder
           {
             continue;
           }
-
+          // if invalid string used, then regex has no matched to/from
           int sres = seq
                   .findPosition(resIndex
                           + Integer.parseInt(spaces.elementAt(resIndex)
@@ -214,8 +217,13 @@ public class Finder
                   - 1
                   + Integer.parseInt(spaces
                           .elementAt(regex.matchedTo() - 1).toString()));
-
-          searchResults.addResult(seq, sres, eres);
+          // only add result if not contained in previous result
+          if (lastm == null
+                  || (lastm.getSequence() != seq || (!(lastm.getStart() <= sres && lastm
+                          .getEnd() >= eres))))
+          {
+            lastm = searchResults.addResult(seq, sres, eres);
+          }
           hasResults = true;
           if (!findAll)
           {
@@ -337,7 +345,7 @@ public class Finder
   /**
    * @return the searchResults
    */
-  public SearchResults getSearchResults()
+  public SearchResultsI getSearchResults()
   {
     return searchResults;
   }