JAL-1714 improved the new PDB Sequence fetcher by enabling it query multiple PDB...
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 27 Apr 2015 14:24:09 +0000 (15:24 +0100)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 27 Apr 2015 14:24:09 +0000 (15:24 +0100)
src/jalview/gui/PDBSearchPanel.java
src/jalview/gui/StructureChooser.java
test/jalview/gui/PDBSearchPanelTest.java

index 5a8407a..514406a 100644 (file)
@@ -74,19 +74,16 @@ public class PDBSearchPanel extends GPDBSearchPanel
       wantedFields = PDBDocFieldPreferences
               .getSearchSummaryFields();
 
-      String searchTerm = txt_search.getText();
-      if (searchTerm.contains(":"))
-      {
-        searchTerm = searchTerm.split(":")[0];
-      }
+      String searchTerm = decodeSearchTerm(txt_search.getText(),
+              searchTarget);
 
       PDBRestRequest request = new PDBRestRequest();
       request.setAllowEmptySeq(allowEmptySequence);
       request.setResponseSize(100);
-      request.setFieldToSearchBy(searchTarget + ":");
-      request.setSearchTerm(searchTerm);
+      request.setFieldToSearchBy("(" + searchTarget + ":");
+      request.setSearchTerm(searchTerm + ")");
       request.setWantedFields(wantedFields);
-
+      // System.out.println(">>>>>>>>>>>>>> " + request.getQuery());
       PDBRestClient pdbRestCleint = new PDBRestClient();
       PDBRestResponse resultList = pdbRestCleint.executeRequest(request);
       if (resultList.getSearchSummary() != null)
@@ -105,6 +102,43 @@ public class PDBSearchPanel extends GPDBSearchPanel
     }
   }
 
+  public static String decodeSearchTerm(String enteredText,
+          String targetField)
+  {
+    String foundSearchTerms = enteredText;
+    StringBuilder foundSearchTermsBuilder = new StringBuilder();
+    if (enteredText.contains(";"))
+    {
+      String[] searchTerms = enteredText.split(";");
+      for (String searchTerm : searchTerms)
+      {
+        if (searchTerm.contains(":"))
+        {
+          foundSearchTermsBuilder.append(targetField).append(":")
+                  .append(searchTerm.split(":")[0]).append(" OR ");
+        }
+        else
+        {
+          foundSearchTermsBuilder.append(targetField).append(":")
+                  .append(searchTerm).append(" OR ");
+        }
+      }
+      int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
+      foundSearchTerms = foundSearchTermsBuilder.toString();
+      if (foundSearchTerms.contains(" OR "))
+      {
+        foundSearchTerms = foundSearchTerms.substring(
+                targetField.length() + 1, endIndex);
+      }
+    }
+    else if (enteredText.contains(":"))
+    {
+      foundSearchTerms = foundSearchTerms.split(":")[0];
+    }
+    return foundSearchTerms;
+  }
+
+
   @Override
   public void btn_ok_ActionPerformed()
   {
@@ -139,23 +173,43 @@ public class PDBSearchPanel extends GPDBSearchPanel
               .toString();
 
       String searchTerm = txt_search.getText();
-      if (searchTerm.contains(":"))
-      {
-        selectedIds.append(";").append(pdbIdStr).append(":")
-                .append(searchTerm.split(":")[1].toUpperCase());
-      }
-      else
-      {
-        selectedIds.append(";").append(pdbIdStr);
-      }
+      selectedIds.append(getPDBIdwithSpecifiedChain(pdbIdStr, searchTerm))
+              .append(";");
     }
 
-    String ids = selectedIds.deleteCharAt(0).toString();
+    String ids = selectedIds.toString();
+    // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
     seqFetcher.textArea.setText(ids);
     Thread worker = new Thread(seqFetcher);
     worker.start();
   }
 
+  public static String getPDBIdwithSpecifiedChain(String pdbId,
+          String searchTerm)
+  {
+    String pdbIdWithChainCode = "";
+    if (searchTerm.contains(";"))
+    {
+      String[] foundTerms = searchTerm.split(";");
+      for (String foundTerm : foundTerms)
+      {
+        if (foundTerm.contains(pdbId))
+        {
+          pdbIdWithChainCode = foundTerm;
+        }
+      }
+    }
+    else if (searchTerm.contains(pdbId))
+    {
+      pdbIdWithChainCode = searchTerm;
+    }
+    else
+    {
+      pdbIdWithChainCode = pdbId;
+    }
+    return pdbIdWithChainCode;
+  }
+
   /**
    * Populates search target combo-box options
    */
index c913213..cd4faf6 100644 (file)
@@ -237,7 +237,7 @@ public class StructureChooser extends GStructureChooser
     String[] names = seqName.toLowerCase().split("\\|");
     for (String name : names)
     {
-      System.out.println("Found name : " + name);
+      // System.out.println("Found name : " + name);
       name.trim();
       if (isValidSeqName(name))
       {
index 17ba85a..4595ad1 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.gui;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import javax.swing.JInternalFrame;
@@ -31,6 +32,43 @@ public class PDBSearchPanelTest
   }
 
   @Test
+  public void testDecodeSearchTerm()
+  {
+    String expectedString = "1xyz OR text:2xyz OR text:3xyz";
+    String outcome = PDBSearchPanel.decodeSearchTerm("1xyz:A;2xyz;3xyz",
+            "text");
+    // System.out.println("1 >>>>>>>>>>> " + outcome);
+    assertEquals(expectedString, outcome);
+
+    expectedString = "1xyz";
+    outcome = PDBSearchPanel.decodeSearchTerm("1xyz", "text");
+    // System.out.println("2 >>>>>>>>>>> " + outcome);
+    assertEquals(expectedString, outcome);
+  }
+
+  @Test
+  public void testgetPDBIdwithSpecifiedChain()
+  {
+
+    String expectedString = "1xyz:A";
+    String outcome = PDBSearchPanel.getPDBIdwithSpecifiedChain("1xyz",
+            "2xyz;3xyz;1xyz:A");
+    System.out.println("1 >>>>>>>>>>> " + outcome);
+    assertEquals(expectedString, outcome);
+
+    expectedString = "2xyz";
+    outcome = PDBSearchPanel.getPDBIdwithSpecifiedChain("2xyz",
+            "1xyz:A;2xyz;3xyz");
+    System.out.println("2 >>>>>>>>>>> " + outcome);
+    assertEquals(expectedString, outcome);
+
+    expectedString = "2xyz:A";
+    outcome = PDBSearchPanel.getPDBIdwithSpecifiedChain("2xyz", "2xyz:A");
+    System.out.println("3 >>>>>>>>>>> " + outcome);
+    assertEquals(expectedString, outcome);
+  }
+
+  @Test
   public void txt_search_ActionPerformedTest()
   {
     PDBSearchPanel searchPanel = new PDBSearchPanel(null);
@@ -47,4 +85,5 @@ public class PDBSearchPanelTest
     assertTrue(!mainFrame.getTitle().equalsIgnoreCase(
             "PDB Sequence Fetcher"));
   }
+
 }