Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
TDBeaconsFTSPanel | 44 | 86 | 28 |
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 | package jalview.fts.service.threedbeacons; | |
22 | ||
23 | import java.net.HttpURLConnection; | |
24 | import java.util.HashMap; | |
25 | import java.util.HashSet; | |
26 | import java.util.Map; | |
27 | ||
28 | import javax.swing.SwingUtilities; | |
29 | ||
30 | import jalview.bin.Console; | |
31 | import jalview.datamodel.AlignmentI; | |
32 | import jalview.fts.api.FTSDataColumnI; | |
33 | import jalview.fts.api.FTSRestClientI; | |
34 | import jalview.fts.core.FTSRestRequest; | |
35 | import jalview.fts.core.FTSRestResponse; | |
36 | import jalview.fts.core.GFTSPanel; | |
37 | import jalview.gui.SequenceFetcher; | |
38 | import jalview.io.DataSourceType; | |
39 | import jalview.io.FileFormat; | |
40 | import jalview.io.FormatAdapter; | |
41 | import jalview.util.MessageManager; | |
42 | ||
43 | @SuppressWarnings("serial") | |
44 | public class TDBeaconsFTSPanel extends GFTSPanel | |
45 | { | |
46 | private static String defaultFTSFrameTitle = MessageManager | |
47 | .getString("label.pdb_sequence_fetcher"); | |
48 | ||
49 | private static Map<String, Integer> tempUserPrefs = new HashMap<>(); | |
50 | ||
51 | private static final String THREEDB_FTS_CACHE_KEY = "CACHE.THREEDB_FTS"; | |
52 | ||
53 | private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH"; | |
54 | ||
55 | private static HttpURLConnection connection; | |
56 | ||
57 | 1 | public TDBeaconsFTSPanel(SequenceFetcher fetcher) |
58 | { | |
59 | // no ID retrieval option for TD Beacons just now | |
60 | 1 | super(null); |
61 | 1 | pageLimit = TDBeaconsFTSRestClient.getInstance() |
62 | .getDefaultResponsePageSize(); | |
63 | 1 | this.seqFetcher = fetcher; |
64 | 1 | this.progressIndicator = (fetcher == null) ? null |
65 | : fetcher.getProgressIndicator(); | |
66 | } | |
67 | ||
68 | 0 | @Override |
69 | public void searchAction(boolean isFreshSearch) | |
70 | { | |
71 | 0 | mainFrame.requestFocusInWindow(); |
72 | 0 | if (isFreshSearch) |
73 | { | |
74 | 0 | offSet = 0; |
75 | } | |
76 | 0 | new Thread() |
77 | { | |
78 | 0 | @Override |
79 | public void run() | |
80 | { | |
81 | 0 | reset(); |
82 | 0 | boolean allowEmptySequence = false; |
83 | 0 | if (getTypedText().length() > 0) |
84 | { | |
85 | 0 | setSearchInProgress(true); |
86 | 0 | long startTime = System.currentTimeMillis(); |
87 | ||
88 | 0 | String searchTarget = ((FTSDataColumnI) cmb_searchTarget |
89 | .getSelectedItem()).getCode(); | |
90 | 0 | wantedFields = TDBeaconsFTSRestClient.getInstance() |
91 | .getAllDefaultDisplayedFTSDataColumns(); | |
92 | 0 | String searchTerm = getTypedText(); // to add : decodeSearchTerm |
93 | ||
94 | 0 | FTSRestRequest request = new FTSRestRequest(); |
95 | 0 | request.setAllowEmptySeq(allowEmptySequence); |
96 | 0 | request.setResponseSize(100); |
97 | // expect it to be uniprot accesssion | |
98 | 0 | request.setSearchTerm(searchTerm + ".json"); |
99 | 0 | request.setOffSet(offSet); |
100 | 0 | request.setWantedFields(wantedFields); |
101 | 0 | FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient |
102 | .getInstance(); | |
103 | 0 | FTSRestResponse resultList; |
104 | 0 | try |
105 | { | |
106 | 0 | resultList = tdbRestClient.executeRequest(request); |
107 | } catch (Exception e) | |
108 | { | |
109 | 0 | setErrorMessage(e.getMessage()); |
110 | 0 | checkForErrors(); |
111 | 0 | setSearchInProgress(false); |
112 | 0 | return; |
113 | } | |
114 | ||
115 | 0 | if (resultList.getSearchSummary() != null |
116 | && resultList.getSearchSummary().size() > 0) | |
117 | { | |
118 | 0 | getResultTable().setModel(FTSRestResponse.getTableModel(request, |
119 | resultList.getSearchSummary())); | |
120 | 0 | FTSRestResponse.configureTableColumn(getResultTable(), |
121 | wantedFields, tempUserPrefs); | |
122 | 0 | getResultTable().setVisible(true); |
123 | } | |
124 | ||
125 | 0 | long endTime = System.currentTimeMillis(); |
126 | 0 | totalResultSetCount = resultList.getNumberOfItemsFound(); |
127 | 0 | resultSetCount = resultList.getSearchSummary() == null ? 0 |
128 | : resultList.getSearchSummary().size(); | |
129 | 0 | String result = (resultSetCount > 0) |
130 | ? MessageManager.getString("label.results") | |
131 | : MessageManager.getString("label.result"); | |
132 | ||
133 | 0 | if (isPaginationEnabled() && resultSetCount > 0) |
134 | { | |
135 | 0 | String f1 = totalNumberformatter |
136 | .format(Integer.valueOf(offSet + 1)); | |
137 | 0 | String f2 = totalNumberformatter |
138 | .format(Integer.valueOf(offSet + resultSetCount)); | |
139 | 0 | String f3 = totalNumberformatter |
140 | .format(Integer.valueOf(totalResultSetCount)); | |
141 | 0 | updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result |
142 | + " " + f1 + " to " + f2 + " of " + f3 + " " + " (" | |
143 | + (endTime - startTime) + " milli secs)"); | |
144 | } | |
145 | else | |
146 | { | |
147 | 0 | updateSearchFrameTitle(defaultFTSFrameTitle + " - " |
148 | + resultSetCount + " " + result + " (" | |
149 | + (endTime - startTime) + " milli secs)"); | |
150 | } | |
151 | ||
152 | 0 | setSearchInProgress(false); |
153 | 0 | refreshPaginatorState(); |
154 | 0 | updateSummaryTableSelections(); |
155 | } | |
156 | 0 | txt_search.updateCache(); |
157 | } | |
158 | }.start(); | |
159 | } | |
160 | ||
161 | 0 | @Override |
162 | public void okAction() | |
163 | { | |
164 | // mainFrame.dispose(); | |
165 | 0 | disableActionButtons(); |
166 | 0 | StringBuilder selectedIds = new StringBuilder(); |
167 | 0 | final HashSet<String> selectedIdsSet = new HashSet<>(); |
168 | 0 | int primaryKeyColIndex = 0; |
169 | 0 | try |
170 | { | |
171 | 0 | primaryKeyColIndex = getFTSRestClient() |
172 | .getPrimaryKeyColumIndex(wantedFields, false); | |
173 | } catch (Exception e) | |
174 | { | |
175 | 0 | e.printStackTrace(); |
176 | } | |
177 | 0 | int[] selectedRows = getResultTable().getSelectedRows(); |
178 | 0 | String searchTerm = getTypedText(); |
179 | 0 | for (int summaryRow : selectedRows) |
180 | { | |
181 | 0 | String idStr = getResultTable() |
182 | .getValueAt(summaryRow, primaryKeyColIndex).toString(); | |
183 | 0 | selectedIdsSet.add(idStr); |
184 | } | |
185 | ||
186 | 0 | for (String idStr : paginatorCart) |
187 | { | |
188 | 0 | selectedIdsSet.add(idStr); |
189 | } | |
190 | ||
191 | 0 | for (String selectedId : selectedIdsSet) |
192 | { | |
193 | 0 | selectedIds.append(selectedId).append(";"); |
194 | } | |
195 | ||
196 | 0 | SwingUtilities.invokeLater(new Runnable() |
197 | { | |
198 | 0 | @Override |
199 | public void run() | |
200 | { | |
201 | 0 | AlignmentI allSeqs = null; |
202 | 0 | FormatAdapter fl = new jalview.io.FormatAdapter(); |
203 | 0 | for (String tdbURL : selectedIdsSet) |
204 | { | |
205 | 0 | try |
206 | { | |
207 | // retrieve the structure via its URL | |
208 | 0 | AlignmentI tdbAl = fl.readFile(tdbURL, DataSourceType.URL, |
209 | FileFormat.MMCif); | |
210 | ||
211 | // TODO: pad structure according to its Uniprot Start so all line up | |
212 | // w.r.t. the Uniprot reference sequence | |
213 | // TODO: give the structure a sensible name (not the giant URL *:o) | |
214 | // ) | |
215 | 0 | if (tdbAl != null) |
216 | { | |
217 | 0 | if (allSeqs != null) |
218 | { | |
219 | 0 | allSeqs.append(tdbAl); |
220 | } | |
221 | else | |
222 | { | |
223 | 0 | allSeqs = tdbAl; |
224 | } | |
225 | } | |
226 | } catch (Exception x) | |
227 | { | |
228 | 0 | Console.warn("Couldn't retrieve 3d-beacons model for uniprot id" |
229 | + searchTerm + " : " + tdbURL, x); | |
230 | } | |
231 | } | |
232 | 0 | seqFetcher.parseResult(allSeqs, |
233 | "3D-Beacons models for " + searchTerm, FileFormat.MMCif, | |
234 | null); | |
235 | ||
236 | } | |
237 | }); | |
238 | 0 | delayAndEnableActionButtons(); |
239 | } | |
240 | ||
241 | 3 | @Override |
242 | public FTSRestClientI getFTSRestClient() | |
243 | { | |
244 | 3 | return TDBeaconsFTSRestClient.getInstance(); |
245 | } | |
246 | ||
247 | 2 | @Override |
248 | public String getFTSFrameTitle() | |
249 | { | |
250 | 2 | return defaultFTSFrameTitle; |
251 | } | |
252 | ||
253 | 1 | @Override |
254 | public boolean isPaginationEnabled() | |
255 | { | |
256 | 1 | return true; |
257 | } | |
258 | ||
259 | 4 | @Override |
260 | public Map<String, Integer> getTempUserPrefs() | |
261 | { | |
262 | 4 | return tempUserPrefs; |
263 | } | |
264 | ||
265 | 1 | @Override |
266 | public String getCacheKey() | |
267 | { | |
268 | 1 | return THREEDB_FTS_CACHE_KEY; |
269 | } | |
270 | ||
271 | 1 | @Override |
272 | public String getAutosearchPreference() | |
273 | { | |
274 | 1 | return THREEDB_AUTOSEARCH; |
275 | } | |
276 | ||
277 | 0 | @Override |
278 | protected void showHelp() | |
279 | { | |
280 | 0 | jalview.bin.Console.outPrintln("No help implemented yet."); |
281 | ||
282 | } | |
283 | ||
284 | 0 | public static String decodeSearchTerm(String enteredText) |
285 | { | |
286 | // no multiple query support yet | |
287 | 0 | return enteredText; |
288 | } | |
289 | ||
290 | 1 | public String getDbName() |
291 | { | |
292 | 1 | return "3D-Beacons"; |
293 | } | |
294 | } |