JAL-2814 help links from PDB and Uniprot FTS panels
[jalview.git] / src / jalview / fts / service / pdb / PDBFTSPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.fts.service.pdb;
23
24 import jalview.fts.api.FTSDataColumnI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.gui.Help;
30 import jalview.gui.Help.HelpId;
31 import jalview.gui.SequenceFetcher;
32 import jalview.util.MessageManager;
33
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.Map;
37
38 import javax.help.HelpSetException;
39
40 @SuppressWarnings("serial")
41 public class PDBFTSPanel extends GFTSPanel
42 {
43   private static String defaultFTSFrameTitle = MessageManager
44           .getString("label.pdb_sequence_fetcher");
45
46   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
47
48   private static final String PDB_FTS_CACHE_KEY = "CACHE.PDB_FTS";
49
50   private static final String PDB_AUTOSEARCH = "FTS.PDB.AUTOSEARCH";
51
52   public PDBFTSPanel(SequenceFetcher fetcher)
53   {
54     super(fetcher);
55     pageLimit = PDBFTSRestClient.getInstance().getDefaultResponsePageSize();
56     this.seqFetcher = fetcher;
57     this.progressIndicator = (fetcher == null) ? null
58             : fetcher.getProgressIndicator();
59   }
60
61   @Override
62   public void searchAction(boolean isFreshSearch)
63   {
64     mainFrame.requestFocusInWindow();
65     if (isFreshSearch)
66     {
67       offSet = 0;
68     }
69     new Thread()
70     {
71       @Override
72       public void run()
73       {
74         reset();
75         boolean allowEmptySequence = false;
76         if (getTypedText().length() > 0)
77         {
78           setSearchInProgress(true);
79           long startTime = System.currentTimeMillis();
80
81           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
82                   .getSelectedItem()).getCode();
83           wantedFields = PDBFTSRestClient.getInstance()
84                   .getAllDefaultDisplayedFTSDataColumns();
85           String searchTerm = decodeSearchTerm(getTypedText(),
86                   searchTarget);
87
88           FTSRestRequest request = new FTSRestRequest();
89           request.setAllowEmptySeq(allowEmptySequence);
90           request.setResponseSize(100);
91           request.setFieldToSearchBy("(" + searchTarget + ":");
92           request.setSearchTerm(searchTerm + ")");
93           request.setOffSet(offSet);
94           request.setWantedFields(wantedFields);
95           FTSRestClientI pdbRestClient = PDBFTSRestClient.getInstance();
96           FTSRestResponse resultList;
97           try
98           {
99             resultList = pdbRestClient.executeRequest(request);
100           } catch (Exception e)
101           {
102             setErrorMessage(e.getMessage());
103             checkForErrors();
104             setSearchInProgress(false);
105             return;
106           }
107
108           if (resultList.getSearchSummary() != null
109                   && resultList.getSearchSummary().size() > 0)
110           {
111             getResultTable().setModel(FTSRestResponse.getTableModel(request,
112                     resultList.getSearchSummary()));
113             FTSRestResponse.configureTableColumn(getResultTable(),
114                     wantedFields, tempUserPrefs);
115             getResultTable().setVisible(true);
116           }
117
118           long endTime = System.currentTimeMillis();
119           totalResultSetCount = resultList.getNumberOfItemsFound();
120           resultSetCount = resultList.getSearchSummary() == null ? 0
121                   : resultList.getSearchSummary().size();
122           String result = (resultSetCount > 0)
123                   ? MessageManager.getString("label.results")
124                   : MessageManager.getString("label.result");
125
126           if (isPaginationEnabled() && resultSetCount > 0)
127           {
128             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
129                     + " "
130                     + totalNumberformatter.format((Number) (offSet + 1))
131                     + " to "
132                     + totalNumberformatter
133                             .format((Number) (offSet + resultSetCount))
134                     + " of "
135                     + totalNumberformatter
136                             .format((Number) totalResultSetCount)
137                     + " " + " (" + (endTime - startTime) + " milli secs)");
138           }
139           else
140           {
141             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
142                     + resultSetCount + " " + result + " ("
143                     + (endTime - startTime) + " milli secs)");
144           }
145
146           setSearchInProgress(false);
147           refreshPaginatorState();
148           updateSummaryTableSelections();
149         }
150         txt_search.updateCache();
151       }
152     }.start();
153   }
154
155   public static String decodeSearchTerm(String enteredText,
156           String targetField)
157   {
158     String foundSearchTerms = enteredText;
159     StringBuilder foundSearchTermsBuilder = new StringBuilder();
160     if (enteredText.contains(";"))
161     {
162       String[] searchTerms = enteredText.split(";");
163       for (String searchTerm : searchTerms)
164       {
165         if (searchTerm.contains(":"))
166         {
167           foundSearchTermsBuilder.append(targetField).append(":")
168                   .append(searchTerm.split(":")[0]).append(" OR ");
169         }
170         else
171         {
172           foundSearchTermsBuilder.append(targetField).append(":")
173                   .append(searchTerm).append(" OR ");
174         }
175       }
176       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
177       foundSearchTerms = foundSearchTermsBuilder.toString();
178       if (foundSearchTerms.contains(" OR "))
179       {
180         foundSearchTerms = foundSearchTerms
181                 .substring(targetField.length() + 1, endIndex);
182       }
183     }
184     else if (enteredText.contains(":"))
185     {
186       foundSearchTerms = foundSearchTerms.split(":")[0];
187     }
188     return foundSearchTerms;
189   }
190
191   @Override
192   public void okAction()
193   {
194     // mainFrame.dispose();
195     disableActionButtons();
196     StringBuilder selectedIds = new StringBuilder();
197     HashSet<String> selectedIdsSet = new HashSet<>();
198     int primaryKeyColIndex = 0;
199     try
200     {
201       primaryKeyColIndex = getFTSRestClient()
202               .getPrimaryKeyColumIndex(wantedFields, false);
203     } catch (Exception e)
204     {
205       e.printStackTrace();
206     }
207     int[] selectedRows = getResultTable().getSelectedRows();
208     String searchTerm = getTypedText();
209     for (int summaryRow : selectedRows)
210     {
211       String idStr = getResultTable()
212               .getValueAt(summaryRow, primaryKeyColIndex).toString();
213       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
214     }
215
216     for (String idStr : paginatorCart)
217     {
218       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
219     }
220
221     for (String selectedId : selectedIdsSet)
222     {
223       selectedIds.append(selectedId).append(";");
224     }
225
226     String ids = selectedIds.toString();
227     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
228     seqFetcher.getTextArea().setText(ids);
229     Thread worker = new Thread(seqFetcher);
230     worker.start();
231     delayAndEnableActionButtons();
232   }
233
234   public static String getPDBIdwithSpecifiedChain(String pdbId,
235           String searchTerm)
236   {
237     String pdbIdWithChainCode = "";
238     if (searchTerm.contains(";"))
239     {
240       String[] foundTerms = searchTerm.split(";");
241       for (String foundTerm : foundTerms)
242       {
243         if (foundTerm.contains(pdbId))
244         {
245           pdbIdWithChainCode = foundTerm;
246         }
247       }
248     }
249     else if (searchTerm.contains(pdbId))
250     {
251       pdbIdWithChainCode = searchTerm;
252     }
253     else
254     {
255       pdbIdWithChainCode = pdbId;
256     }
257     return pdbIdWithChainCode;
258   }
259
260   @Override
261   public FTSRestClientI getFTSRestClient()
262   {
263     return PDBFTSRestClient.getInstance();
264   }
265
266   @Override
267   public String getFTSFrameTitle()
268   {
269     return defaultFTSFrameTitle;
270   }
271
272   @Override
273   public boolean isPaginationEnabled()
274   {
275     return true;
276   }
277
278   @Override
279   public Map<String, Integer> getTempUserPrefs()
280   {
281     return tempUserPrefs;
282   }
283
284   @Override
285   public String getCacheKey()
286   {
287     return PDB_FTS_CACHE_KEY;
288   }
289
290   @Override
291   public String getAutosearchPreference()
292   {
293     return PDB_AUTOSEARCH;
294   }
295
296   @Override
297   protected void showHelp()
298   {
299     try
300     {
301       Help.showHelpWindow(HelpId.PdbFts);
302     } catch (HelpSetException e1)
303     {
304       e1.printStackTrace();
305     }
306  }
307 }