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.
21 package jalview.fts.service.threedbeacons;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.net.HttpURLConnection;
27 import java.net.MalformedURLException;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import javax.swing.SwingUtilities;
35 import org.json.JSONArray;
36 import org.json.JSONObject;
38 import jalview.bin.Console;
39 import jalview.datamodel.AlignmentI;
40 import jalview.fts.api.FTSDataColumnI;
41 import jalview.fts.api.FTSRestClientI;
42 import jalview.fts.core.FTSRestRequest;
43 import jalview.fts.core.FTSRestResponse;
44 import jalview.fts.core.GFTSPanel;
45 import jalview.fts.service.pdb.PDBFTSRestClient;
46 import jalview.gui.SequenceFetcher;
47 import jalview.io.DataSourceType;
48 import jalview.io.FileFormat;
49 import jalview.io.FileFormatI;
50 import jalview.io.FileLoader;
51 import jalview.io.FormatAdapter;
52 import jalview.util.MessageManager;
54 @SuppressWarnings("serial")
55 public class TDBeaconsFTSPanel extends GFTSPanel
57 private static String defaultFTSFrameTitle = MessageManager
58 .getString("label.pdb_sequence_fetcher");
60 private static Map<String, Integer> tempUserPrefs = new HashMap<>();
62 private static final String THREEDB_FTS_CACHE_KEY = "CACHE.THREEDB_FTS";
64 private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH";
66 private static HttpURLConnection connection;
68 public TDBeaconsFTSPanel(SequenceFetcher fetcher)
70 // no ID retrieval option for TD Beacons just now
72 pageLimit = TDBeaconsFTSRestClient.getInstance()
73 .getDefaultResponsePageSize();
74 this.seqFetcher = fetcher;
75 this.progressIndicator = (fetcher == null) ? null
76 : fetcher.getProgressIndicator();
80 public void searchAction(boolean isFreshSearch)
82 mainFrame.requestFocusInWindow();
93 boolean allowEmptySequence = false;
94 if (getTypedText().length() > 0)
96 setSearchInProgress(true);
97 long startTime = System.currentTimeMillis();
99 String searchTarget = ((FTSDataColumnI) cmb_searchTarget
100 .getSelectedItem()).getCode();
101 wantedFields = TDBeaconsFTSRestClient.getInstance()
102 .getAllDefaultDisplayedFTSDataColumns();
103 String searchTerm = getTypedText(); // to add : decodeSearchTerm
105 FTSRestRequest request = new FTSRestRequest();
106 request.setAllowEmptySeq(allowEmptySequence);
107 request.setResponseSize(100);
108 // expect it to be uniprot accesssion
109 request.setSearchTerm(searchTerm + ".json");
110 request.setOffSet(offSet);
111 request.setWantedFields(wantedFields);
112 FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient
114 FTSRestResponse resultList;
117 resultList = tdbRestClient.executeRequest(request);
118 } catch (Exception e)
120 setErrorMessage(e.getMessage());
122 setSearchInProgress(false);
126 if (resultList.getSearchSummary() != null
127 && resultList.getSearchSummary().size() > 0)
129 getResultTable().setModel(FTSRestResponse.getTableModel(request,
130 resultList.getSearchSummary()));
131 FTSRestResponse.configureTableColumn(getResultTable(),
132 wantedFields, tempUserPrefs);
133 getResultTable().setVisible(true);
136 long endTime = System.currentTimeMillis();
137 totalResultSetCount = resultList.getNumberOfItemsFound();
138 resultSetCount = resultList.getSearchSummary() == null ? 0
139 : resultList.getSearchSummary().size();
140 String result = (resultSetCount > 0)
141 ? MessageManager.getString("label.results")
142 : MessageManager.getString("label.result");
144 if (isPaginationEnabled() && resultSetCount > 0)
146 String f1 = totalNumberformatter
147 .format(Integer.valueOf(offSet + 1));
148 String f2 = totalNumberformatter
149 .format(Integer.valueOf(offSet + resultSetCount));
150 String f3 = totalNumberformatter
151 .format(Integer.valueOf(totalResultSetCount));
152 updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
153 + " " + f1 + " to " + f2 + " of " + f3 + " " + " ("
154 + (endTime - startTime) + " milli secs)");
158 updateSearchFrameTitle(defaultFTSFrameTitle + " - "
159 + resultSetCount + " " + result + " ("
160 + (endTime - startTime) + " milli secs)");
163 setSearchInProgress(false);
164 refreshPaginatorState();
165 updateSummaryTableSelections();
167 txt_search.updateCache();
173 public void okAction()
175 // mainFrame.dispose();
176 disableActionButtons();
177 StringBuilder selectedIds = new StringBuilder();
178 final HashSet<String> selectedIdsSet = new HashSet<>();
179 int primaryKeyColIndex = 0;
182 primaryKeyColIndex = getFTSRestClient()
183 .getPrimaryKeyColumIndex(wantedFields, false);
184 } catch (Exception e)
188 int[] selectedRows = getResultTable().getSelectedRows();
189 String searchTerm = getTypedText();
190 for (int summaryRow : selectedRows)
192 String idStr = getResultTable()
193 .getValueAt(summaryRow, primaryKeyColIndex).toString();
194 selectedIdsSet.add(idStr);
197 for (String idStr : paginatorCart)
199 selectedIdsSet.add(idStr);
202 for (String selectedId : selectedIdsSet)
204 selectedIds.append(selectedId).append(";");
207 SwingUtilities.invokeLater(new Runnable()
212 AlignmentI allSeqs = null;
213 FormatAdapter fl = new jalview.io.FormatAdapter();
214 for (String tdbURL : selectedIdsSet)
218 // retrieve the structure via its URL
219 AlignmentI tdbAl = fl.readFile(tdbURL, DataSourceType.URL,
222 // TODO: pad structure according to its Uniprot Start so all line up
223 // w.r.t. the Uniprot reference sequence
224 // TODO: give the structure a sensible name (not the giant URL *:o)
230 allSeqs.append(tdbAl);
237 } catch (Exception x)
239 Console.warn("Couldn't retrieve 3d-beacons model for uniprot id"
240 + searchTerm + " : " + tdbURL, x);
243 seqFetcher.parseResult(allSeqs,
244 "3D-Beacons models for " + searchTerm, FileFormat.MMCif,
249 delayAndEnableActionButtons();
253 public FTSRestClientI getFTSRestClient()
255 return TDBeaconsFTSRestClient.getInstance();
259 public String getFTSFrameTitle()
261 return defaultFTSFrameTitle;
265 public boolean isPaginationEnabled()
271 public Map<String, Integer> getTempUserPrefs()
273 return tempUserPrefs;
277 public String getCacheKey()
279 return THREEDB_FTS_CACHE_KEY;
283 public String getAutosearchPreference()
285 return THREEDB_AUTOSEARCH;
289 protected void showHelp()
291 System.out.println("No help implemented yet.");
295 public static String decodeSearchTerm(String enteredText)
297 // no multiple query support yet