JAL-1965 alternate ‘bestMatch’ picker for word matches, normalises list and places... features/JAL-1965_JAL-753_JAL-1427_relaxedIdMatch
authorJim Procter <jprocter@issues.jalview.org>
Fri, 13 Nov 2015 15:23:21 +0000 (15:23 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Fri, 13 Nov 2015 15:23:21 +0000 (15:23 +0000)
src/jalview/analysis/SequenceIdMatcher.java

index a5c161c..a84d31b 100755 (executable)
@@ -236,6 +236,38 @@ public class SequenceIdMatcher
     List<SequenceI> st = pickbestMatches(candName, matches);
     return st == null || st.size() == 0 ? null : st.get(0);
   }
+  
+  /**
+   * returns the SequenceI's with exact word matches to candName
+   * 
+   * @param candName
+   *          SeqIdName
+   * @param matches
+   *          List of SequenceI objects - some of which may be duplicates
+   * @return { word matches to candName }
+   */
+  private List<SequenceI> pickwordMatches(SeqIdName candName,
+          List<SequenceI> matches)
+  {
+    List<SequenceI> best = new ArrayList<SequenceI>();
+    for (SequenceI match : matches)
+    {
+      if (!best.contains(match))
+      {
+        if (candName.equalsCase(match.getDisplayId(true)))
+        {
+          // put the exact match at the beginning
+          best.add(0, match);
+        }
+        else
+        {
+          best.add(match);
+        }
+        addSeq(match);
+      }
+    }
+    return best;
+  }
 
   /**
    * returns the closest SequenceI in matches to SeqIdName and returns all the
@@ -324,14 +356,14 @@ public class SequenceIdMatcher
    * 
    * @param seqnam
    *          string to query Matcher with.
-   * @return a new array or (possibly) null
+   * @return a new array or null of no match exists
    */
   public SequenceI[] findAllIdMatches(String seqnam)
   {
 
     SeqIdName nam = new SeqIdName(seqnam);
     List<SequenceI> m = findAllIdMatches(nam);
-    if (m != null)
+    if (m != null && m.size() > 0)
     {
       return m.toArray(new SequenceI[m.size()]);
     }
@@ -408,7 +440,8 @@ public class SequenceIdMatcher
     {
       matches.addAll(names.remove(nam));
     }
-    List<SequenceI> r = pickbestMatches(nam, matches);
+    List<SequenceI> r = (wordBased) ? pickwordMatches(nam, matches)
+            : pickbestMatches(nam, matches);
     return r;
   }
 }