2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
22 package jalview.fts.service.uniprot;
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;
34 import java.util.HashMap;
35 import java.util.HashSet;
38 import javax.help.HelpSetException;
40 @SuppressWarnings("serial")
41 public class UniprotFTSPanel extends GFTSPanel
44 private static String defaultFTSFrameTitle = MessageManager
45 .getString("label.uniprot_sequence_fetcher");
47 private static Map<String, Integer> tempUserPrefs = new HashMap<>();
49 private static final String UNIPROT_FTS_CACHE_KEY = "CACHE.UNIPROT_FTS";
51 private static final String UNIPROT_AUTOSEARCH = "FTS.UNIPROT.AUTOSEARCH";
54 * Constructor given an (optional) sequence fetcher panel to revert to on
55 * clicking the 'Back' button
59 public UniprotFTSPanel(SequenceFetcher fetcher)
62 pageLimit = UniProtFTSRestClient.getInstance()
63 .getDefaultResponsePageSize();
64 this.seqFetcher = fetcher;
65 this.progressIndicator = (fetcher == null) ? null
66 : fetcher.getProgressIndicator();
70 public void searchAction(boolean isFreshSearch)
72 mainFrame.requestFocusInWindow();
83 String searchInput = getTypedText();
84 if (searchInput.length() > 0)
86 setSearchInProgress(true);
87 long startTime = System.currentTimeMillis();
88 searchInput = getTypedText();
89 String searchTarget = ((FTSDataColumnI) cmb_searchTarget
90 .getSelectedItem()).getAltCode();
91 wantedFields = UniProtFTSRestClient.getInstance()
92 .getAllDefaultDisplayedFTSDataColumns();
93 String searchTerm = decodeSearchTerm(searchInput, searchTarget);
95 FTSRestRequest request = new FTSRestRequest();
96 request.setFieldToSearchBy(searchTarget);
97 request.setSearchTerm(searchTerm);
98 request.setOffSet(offSet);
99 request.setWantedFields(wantedFields);
100 FTSRestClientI uniProtRestClient = UniProtFTSRestClient
102 FTSRestResponse resultList;
105 resultList = uniProtRestClient.executeRequest(request);
106 } catch (Exception e)
108 setErrorMessage(e.getMessage());
110 setSearchInProgress(false);
114 if (resultList.getSearchSummary() != null
115 && resultList.getSearchSummary().size() > 0)
117 getResultTable().setModel(FTSRestResponse.getTableModel(request,
118 resultList.getSearchSummary()));
119 FTSRestResponse.configureTableColumn(getResultTable(),
120 wantedFields, tempUserPrefs);
121 getResultTable().setVisible(true);
124 long endTime = System.currentTimeMillis();
125 totalResultSetCount = resultList.getNumberOfItemsFound();
126 resultSetCount = resultList.getSearchSummary() == null ? 0
127 : resultList.getSearchSummary().size();
128 String result = (resultSetCount > 0)
129 ? MessageManager.getString("label.results")
130 : MessageManager.getString("label.result");
131 if (isPaginationEnabled() && resultSetCount > 0)
133 updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
135 + totalNumberformatter
136 .format(Integer.valueOf(offSet + 1))
138 + totalNumberformatter
140 .valueOf(offSet + resultSetCount))
142 + totalNumberformatter
143 .format(Integer.valueOf(totalResultSetCount))
144 + " " + " (" + (endTime - startTime) + " milli secs)");
148 updateSearchFrameTitle(defaultFTSFrameTitle + " - "
149 + resultSetCount + " " + result + " ("
150 + (endTime - startTime) + " milli secs)");
152 setSearchInProgress(false);
153 refreshPaginatorState();
154 updateSummaryTableSelections();
156 txt_search.updateCache();
162 public String decodeSearchTerm(String enteredText, String targetField)
164 int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
165 : targetField.length() + 1;
166 String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
168 String foundSearchTerms = enteredText;
169 StringBuilder foundSearchTermsBuilder = new StringBuilder();
170 if (enteredText.contains(";"))
172 String[] searchTerms = enteredText.split(";");
173 for (String searchTerm : searchTerms)
175 foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
178 int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
179 foundSearchTerms = foundSearchTermsBuilder.toString();
180 if (foundSearchTerms.contains(" OR "))
182 foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
186 return foundSearchTerms;
190 public boolean isPaginationEnabled()
196 public void okAction()
198 disableActionButtons();
199 StringBuilder selectedIds = new StringBuilder();
200 HashSet<String> selectedIdsSet = new HashSet<>();
201 int primaryKeyColIndex = 0;
204 primaryKeyColIndex = getFTSRestClient()
205 .getPrimaryKeyColumIndex(wantedFields, false);
206 } catch (Exception e)
210 int[] selectedRows = getResultTable().getSelectedRows();
211 for (int summaryRow : selectedRows)
213 String idStr = getResultTable()
214 .getValueAt(summaryRow, primaryKeyColIndex).toString();
215 selectedIdsSet.add(idStr);
217 selectedIdsSet.addAll(paginatorCart);
218 for (String selectedId : selectedIdsSet)
220 selectedIds.append(selectedId).append(";");
223 String ids = selectedIds.toString();
224 seqFetcher.setQuery(ids);
225 Thread worker = new Thread(seqFetcher);
227 delayAndEnableActionButtons();
231 public FTSRestClientI getFTSRestClient()
233 return UniProtFTSRestClient.getInstance();
237 public String getFTSFrameTitle()
239 return defaultFTSFrameTitle;
243 public Map<String, Integer> getTempUserPrefs()
245 return tempUserPrefs;
249 public String getCacheKey()
251 return UNIPROT_FTS_CACHE_KEY;
255 public String getAutosearchPreference()
257 return UNIPROT_AUTOSEARCH;
261 protected void showHelp()
265 Help.showHelpWindow(HelpId.UniprotFts);
266 } catch (HelpSetException e1)
268 e1.printStackTrace();