JAL-2814 help links from PDB and Uniprot FTS panels
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.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.uniprot;
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 UniprotFTSPanel extends GFTSPanel
42 {
43
44   private static String defaultFTSFrameTitle = MessageManager
45           .getString("label.uniprot_sequence_fetcher");
46
47   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
48
49   private static final String UNIPROT_FTS_CACHE_KEY = "CACHE.UNIPROT_FTS";
50
51   private static final String UNIPROT_AUTOSEARCH = "FTS.UNIPROT.AUTOSEARCH";
52
53   public UniprotFTSPanel(SequenceFetcher fetcher)
54   {
55     super(fetcher);
56     pageLimit = UniProtFTSRestClient.getInstance()
57             .getDefaultResponsePageSize();
58     this.seqFetcher = fetcher;
59     this.progressIndicator = (fetcher == null) ? null
60             : fetcher.getProgressIndicator();
61   }
62
63   @Override
64   public void searchAction(boolean isFreshSearch)
65   {
66     mainFrame.requestFocusInWindow();
67     if (isFreshSearch)
68     {
69       offSet = 0;
70     }
71     new Thread()
72     {
73       @Override
74       public void run()
75       {
76         reset();
77         String searchInput = getTypedText();
78         if (searchInput.length() > 0)
79         {
80           setSearchInProgress(true);
81           long startTime = System.currentTimeMillis();
82           searchInput = getTypedText();
83           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
84                   .getSelectedItem()).getAltCode();
85           wantedFields = UniProtFTSRestClient.getInstance()
86                   .getAllDefaultDisplayedFTSDataColumns();
87           String searchTerm = decodeSearchTerm(searchInput, searchTarget);
88
89           FTSRestRequest request = new FTSRestRequest();
90           request.setFieldToSearchBy(searchTarget);
91           request.setSearchTerm(searchTerm);
92           request.setOffSet(offSet);
93           request.setWantedFields(wantedFields);
94           FTSRestClientI uniProtRestClient = UniProtFTSRestClient
95                   .getInstance();
96           FTSRestResponse resultList;
97           try
98           {
99             resultList = uniProtRestClient.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           if (isPaginationEnabled() && resultSetCount > 0)
126           {
127             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
128                     + " "
129                     + totalNumberformatter.format((Number) (offSet + 1))
130                     + " to "
131                     + totalNumberformatter
132                             .format((Number) (offSet + resultSetCount))
133                     + " of "
134                     + totalNumberformatter
135                             .format((Number) totalResultSetCount)
136                     + " " + " (" + (endTime - startTime) + " milli secs)");
137           }
138           else
139           {
140             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
141                     + resultSetCount + " " + result + " ("
142                     + (endTime - startTime) + " milli secs)");
143           }
144           setSearchInProgress(false);
145           refreshPaginatorState();
146           updateSummaryTableSelections();
147         }
148         txt_search.updateCache();
149       }
150     }.start();
151
152   }
153
154   public String decodeSearchTerm(String enteredText, String targetField)
155   {
156     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
157             : targetField.length() + 1;
158     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
159             : targetField + ":";
160     String foundSearchTerms = enteredText;
161     StringBuilder foundSearchTermsBuilder = new StringBuilder();
162     if (enteredText.contains(";"))
163     {
164       String[] searchTerms = enteredText.split(";");
165       for (String searchTerm : searchTerms)
166       {
167         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
168                 .append(" OR ");
169       }
170       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
171       foundSearchTerms = foundSearchTermsBuilder.toString();
172       if (foundSearchTerms.contains(" OR "))
173       {
174         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
175                 endIndex);
176       }
177     }
178     return foundSearchTerms;
179   }
180
181   @Override
182   public boolean isPaginationEnabled()
183   {
184     return true;
185   }
186
187   @Override
188   public void okAction()
189   {
190     disableActionButtons();
191     StringBuilder selectedIds = new StringBuilder();
192     HashSet<String> selectedIdsSet = new HashSet<>();
193     int primaryKeyColIndex = 0;
194     try
195     {
196       primaryKeyColIndex = getFTSRestClient()
197               .getPrimaryKeyColumIndex(wantedFields, false);
198     } catch (Exception e)
199     {
200       e.printStackTrace();
201     }
202     int[] selectedRows = getResultTable().getSelectedRows();
203     for (int summaryRow : selectedRows)
204     {
205       String idStr = getResultTable()
206               .getValueAt(summaryRow, primaryKeyColIndex).toString();
207       selectedIdsSet.add(idStr);
208     }
209     selectedIdsSet.addAll(paginatorCart);
210     for (String selectedId : selectedIdsSet)
211     {
212       selectedIds.append(selectedId).append(";");
213     }
214
215     String ids = selectedIds.toString();
216     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
217     seqFetcher.getTextArea().setText(ids);
218     Thread worker = new Thread(seqFetcher);
219     worker.start();
220     delayAndEnableActionButtons();
221   }
222
223   @Override
224   public FTSRestClientI getFTSRestClient()
225   {
226     return UniProtFTSRestClient.getInstance();
227   }
228
229   @Override
230   public String getFTSFrameTitle()
231   {
232     return defaultFTSFrameTitle;
233   }
234
235   @Override
236   public Map<String, Integer> getTempUserPrefs()
237   {
238     return tempUserPrefs;
239   }
240
241   @Override
242   public String getCacheKey()
243   {
244     return UNIPROT_FTS_CACHE_KEY;
245   }
246
247   @Override
248   public String getAutosearchPreference()
249   {
250     return UNIPROT_AUTOSEARCH;
251   }
252
253   @Override
254   protected void showHelp()
255   {
256     try
257     {
258       Help.showHelpWindow(HelpId.UniprotFts);
259     } catch (HelpSetException e1)
260     {
261       e1.printStackTrace();
262     }
263   }
264 }