JAL-1705 refactored code for dbsource reggae validator and added further refactoring...
authorJim Procter <jprocter@issues.jalview.org>
Sun, 28 Jun 2015 16:16:14 +0000 (17:16 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Sun, 28 Jun 2015 16:16:14 +0000 (17:16 +0100)
src/jalview/gui/SequenceFetcher.java
src/jalview/util/DBRefUtils.java

index c330a92..090eab7 100755 (executable)
@@ -54,8 +54,6 @@ import javax.swing.JTextArea;
 import javax.swing.SwingConstants;
 import javax.swing.tree.DefaultMutableTreeNode;
 
-import com.stevesoft.pat.Regex;
-
 public class SequenceFetcher extends JPanel implements Runnable
 {
   JLabel dbeg = new JLabel();
@@ -509,6 +507,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       resetDialog();
       return;
     }
+    // TODO: Refactor to GUI independent code and write tests.
     // indicate if successive sources should be merged into one alignment.
     boolean addToLast = false;
     ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
@@ -625,23 +624,9 @@ public class SequenceFetcher extends JPanel implements Runnable
                 DBRefEntry dbr = new DBRefEntry(), found[] = null;
                 dbr.setSource(proxy.getDbSource());
                 dbr.setVersion(null);
-                if (proxy.getAccessionValidator() != null)
-                {
-                  Regex vgr = proxy.getAccessionValidator();
-                  vgr.search(q);
-                  if (vgr.numSubs() > 0)
-                  {
-                    dbr.setAccessionId(vgr.stringMatched(1));
-                  }
-                  else
-                  {
-                    dbr.setAccessionId(vgr.stringMatched());
-                  }
-                }
-                else
-                {
-                  dbr.setAccessionId(q);
-                }
+                String accId = DBRefUtils.processQueryToAccessionFor(proxy,
+                        q);
+                dbr.setAccessionId(accId);
                 boolean rfound = false;
                 for (int r = 0; r < rs.length; r++)
                 {
index 123a4e1..d2be021 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.DBRefSource;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
+import jalview.ws.seqfetcher.DbSourceProxy;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -472,4 +473,36 @@ public class DBRefUtils
     return ref;
   }
 
+  /**
+   * Extract valid accession strings from a query string. Used by the
+   * SequenceFetcher and DBRefFetcher to create valid accession strings from an
+   * ID string for database sources with a Regex validation field.
+   * 
+   * @param proxy
+   * @param q
+   * @return q if proxy.getAccessionValidator()==null, otherwise the matched
+   *         region or the first subgroup match from the matched region
+   */
+  public static String processQueryToAccessionFor(DbSourceProxy proxy,
+          String q)
+  {
+    if (proxy.getAccessionValidator() != null)
+    {
+      Regex vgr = proxy.getAccessionValidator();
+      vgr.search(q);
+      if (vgr.numSubs() > 0)
+      {
+        return (vgr.stringMatched(1));
+      }
+      else
+      {
+        return (vgr.stringMatched());
+      }
+    }
+    else
+    {
+      return (q);
+    }
+  }
+
 }