Merge branch 'develop' into groovy/JAL-2074_upgradeto2.4.4
[jalview.git] / src / jalview / fts / service / pdb / PDBFTSPanel.java
diff --git a/src/jalview/fts/service/pdb/PDBFTSPanel.java b/src/jalview/fts/service/pdb/PDBFTSPanel.java
new file mode 100644 (file)
index 0000000..826a505
--- /dev/null
@@ -0,0 +1,235 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
+package jalview.fts.service.pdb;
+
+import jalview.fts.api.FTSDataColumnI;
+import jalview.fts.api.FTSRestClientI;
+import jalview.fts.core.FTSRestRequest;
+import jalview.fts.core.FTSRestResponse;
+import jalview.fts.core.GFTSPanel;
+import jalview.gui.SequenceFetcher;
+import jalview.util.MessageManager;
+
+import java.util.HashSet;
+
+@SuppressWarnings("serial")
+public class PDBFTSPanel extends GFTSPanel
+{
+  private static String defaultFTSFrameTitle = MessageManager
+          .getString("label.pdb_sequence_fetcher");
+
+  private String ftsFrameTitle = defaultFTSFrameTitle;
+
+  public PDBFTSPanel(SequenceFetcher seqFetcher)
+  {
+    this.seqFetcher = seqFetcher;
+    this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
+            .getProgressIndicator();
+  }
+
+
+  @Override
+  public void searchAction()
+  {
+    new Thread()
+    {
+      @Override
+      public void run()
+      {
+        ftsFrameTitle = defaultFTSFrameTitle;
+        reset();
+        boolean allowEmptySequence = false;
+        if (getTypedText().length() > 0)
+        {
+          setSearchInProgress(true);
+          long startTime = System.currentTimeMillis();
+
+          String searchTarget = ((FTSDataColumnI) cmb_searchTarget
+                  .getSelectedItem()).getCode();
+          wantedFields = PDBFTSRestClient.getInstance()
+                  .getAllDefaulDisplayedDataColumns();
+          String searchTerm = decodeSearchTerm(txt_search.getText(),
+                  searchTarget);
+
+          FTSRestRequest request = new FTSRestRequest();
+          request.setAllowEmptySeq(allowEmptySequence);
+          request.setResponseSize(100);
+          request.setFieldToSearchBy("(" + searchTarget + ":");
+          request.setSearchTerm(searchTerm + ")");
+          request.setWantedFields(wantedFields);
+          FTSRestClientI pdbRestCleint = PDBFTSRestClient.getInstance();
+          FTSRestResponse resultList;
+          try
+          {
+            resultList = pdbRestCleint.executeRequest(request);
+          } catch (Exception e)
+          {
+            setErrorMessage(e.getMessage());
+            checkForErrors();
+            return;
+          }
+
+          if (resultList.getSearchSummary() != null
+                  && resultList.getSearchSummary().size() > 0)
+          {
+            getResultTable().setModel(
+                    FTSRestResponse.getTableModel(request,
+                    resultList.getSearchSummary()));
+            FTSRestResponse.configureTableColumn(getResultTable(),
+                    wantedFields);
+            getResultTable().setVisible(true);
+          }
+
+          long endTime = System.currentTimeMillis();
+          int resultSetCount = resultList.getNumberOfItemsFound();
+          String result = (resultSetCount > 1) ? MessageManager
+                  .getString("label.results") : MessageManager
+                  .getString("label.result");
+          updateSearchFrameTitle(defaultFTSFrameTitle + " - "
+                  + resultSetCount + " " + result + " ("
+                  + (endTime - startTime) + " milli secs)");
+          setSearchInProgress(false);
+        }
+      }
+    }.start();
+  }
+
+  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 okAction()
+  {
+    // mainFrame.dispose();
+    disableActionButtons();
+    StringBuilder selectedIds = new StringBuilder();
+    HashSet<String> selectedIdsSet = new HashSet<String>();
+    int primaryKeyColIndex = 0;
+    try
+    {
+      primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
+              wantedFields,
+              false);
+    } catch (Exception e)
+    {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
+    int[] selectedRows = getResultTable().getSelectedRows();
+    for (int summaryRow : selectedRows)
+    {
+      String idStr = getResultTable().getValueAt(summaryRow,
+              primaryKeyColIndex)
+              .toString();
+      String searchTerm = txt_search.getText();
+      selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
+    }
+
+    for (String selectedId : selectedIdsSet)
+    {
+      selectedIds.append(selectedId).append(";");
+    }
+
+    String ids = selectedIds.toString();
+    // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
+    seqFetcher.getTextArea().setText(ids);
+    Thread worker = new Thread(seqFetcher);
+    worker.start();
+    delayAndEnableActionButtons();
+  }
+
+
+  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;
+  }
+
+
+
+  @Override
+  public FTSRestClientI getFTSRestClient()
+  {
+    return PDBFTSRestClient.getInstance();
+  }
+
+  @Override
+  public String getFTSFrameTitle()
+  {
+    return ftsFrameTitle;
+  }
+
+
+}