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

index 2d46714..514406a 100644 (file)
@@ -74,13 +74,16 @@ public class PDBSearchPanel extends GPDBSearchPanel
       wantedFields = PDBDocFieldPreferences
               .getSearchSummaryFields();
 
+      String searchTerm = decodeSearchTerm(txt_search.getText(),
+              searchTarget);
+
       PDBRestRequest request = new PDBRestRequest();
       request.setAllowEmptySeq(allowEmptySequence);
       request.setResponseSize(100);
-      request.setFieldToSearchBy(searchTarget + ":");
-      request.setSearchTerm(txt_search.getText());
+      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)
@@ -99,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()
   {
@@ -131,15 +171,45 @@ public class PDBSearchPanel extends GPDBSearchPanel
     {
       String pdbIdStr = tbl_summary.getValueAt(summaryRow, pdbIdCol)
               .toString();
-      selectedIds.append(";").append(pdbIdStr);
+
+      String searchTerm = txt_search.getText();
+      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 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"));
   }
+
 }