1 package jalview.fts.service.threedbeacons;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
9 import java.util.HashMap;
10 import java.util.HashSet;
13 import javax.swing.SwingUtilities;
15 import org.json.JSONArray;
16 import org.json.JSONObject;
18 import jalview.bin.Cache;
19 import jalview.datamodel.AlignmentI;
20 import jalview.fts.api.FTSDataColumnI;
21 import jalview.fts.api.FTSRestClientI;
22 import jalview.fts.core.FTSRestRequest;
23 import jalview.fts.core.FTSRestResponse;
24 import jalview.fts.core.GFTSPanel;
25 import jalview.fts.service.pdb.PDBFTSRestClient;
26 import jalview.gui.SequenceFetcher;
27 import jalview.io.DataSourceType;
28 import jalview.io.FileFormat;
29 import jalview.io.FileFormatI;
30 import jalview.io.FileLoader;
31 import jalview.io.FormatAdapter;
32 import jalview.util.MessageManager;
34 @SuppressWarnings("serial")
35 public class TDBeaconsFTSPanel extends GFTSPanel
37 private static String defaultFTSFrameTitle = MessageManager
38 .getString("label.pdb_sequence_fetcher");
40 private static Map<String, Integer> tempUserPrefs = new HashMap<>();
42 private static final String THREEDB_FTS_CACHE_KEY = "CACHE.THREEDB_FTS";
44 private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH";
46 private static HttpURLConnection connection;
48 public TDBeaconsFTSPanel(SequenceFetcher fetcher)
50 // no ID retrieval option for TD Beacons just now
52 pageLimit = TDBeaconsFTSRestClient.getInstance()
53 .getDefaultResponsePageSize();
54 this.seqFetcher = fetcher;
55 this.progressIndicator = (fetcher == null) ? null
56 : fetcher.getProgressIndicator();
60 public void searchAction(boolean isFreshSearch)
62 mainFrame.requestFocusInWindow();
73 boolean allowEmptySequence = false;
74 if (getTypedText().length() > 0)
76 setSearchInProgress(true);
77 long startTime = System.currentTimeMillis();
79 String searchTarget = ((FTSDataColumnI) cmb_searchTarget
80 .getSelectedItem()).getCode();
81 wantedFields = TDBeaconsFTSRestClient.getInstance()
82 .getAllDefaultDisplayedFTSDataColumns();
83 String searchTerm = getTypedText(); // to add : decodeSearchTerm
85 FTSRestRequest request = new FTSRestRequest();
86 request.setAllowEmptySeq(allowEmptySequence);
87 request.setResponseSize(100);
88 // expect it to be uniprot accesssion
89 request.setSearchTerm(searchTerm + ".json");
90 request.setOffSet(offSet);
91 request.setWantedFields(wantedFields);
92 FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient
94 FTSRestResponse resultList;
97 resultList = tdbRestClient.executeRequest(request);
100 setErrorMessage(e.getMessage());
102 setSearchInProgress(false);
106 if (resultList.getSearchSummary() != null
107 && resultList.getSearchSummary().size() > 0)
109 getResultTable().setModel(FTSRestResponse.getTableModel(request,
110 resultList.getSearchSummary()));
111 FTSRestResponse.configureTableColumn(getResultTable(),
112 wantedFields, tempUserPrefs);
113 getResultTable().setVisible(true);
116 long endTime = System.currentTimeMillis();
117 totalResultSetCount = resultList.getNumberOfItemsFound();
118 resultSetCount = resultList.getSearchSummary() == null ? 0
119 : resultList.getSearchSummary().size();
120 String result = (resultSetCount > 0)
121 ? MessageManager.getString("label.results")
122 : MessageManager.getString("label.result");
124 if (isPaginationEnabled() && resultSetCount > 0)
126 String f1 = totalNumberformatter
127 .format(Integer.valueOf(offSet + 1));
128 String f2 = totalNumberformatter
129 .format(Integer.valueOf(offSet + resultSetCount));
130 String f3 = totalNumberformatter
131 .format(Integer.valueOf(totalResultSetCount));
132 updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
133 + " " + f1 + " to " + f2 + " of " + f3 + " " + " ("
134 + (endTime - startTime) + " milli secs)");
138 updateSearchFrameTitle(defaultFTSFrameTitle + " - "
139 + resultSetCount + " " + result + " ("
140 + (endTime - startTime) + " milli secs)");
143 setSearchInProgress(false);
144 refreshPaginatorState();
145 updateSummaryTableSelections();
147 txt_search.updateCache();
153 public void okAction()
155 // mainFrame.dispose();
156 disableActionButtons();
157 StringBuilder selectedIds = new StringBuilder();
158 final HashSet<String> selectedIdsSet = new HashSet<>();
159 int primaryKeyColIndex = 0;
162 primaryKeyColIndex = getFTSRestClient()
163 .getPrimaryKeyColumIndex(wantedFields, false);
164 } catch (Exception e)
168 int[] selectedRows = getResultTable().getSelectedRows();
169 String searchTerm = getTypedText();
170 for (int summaryRow : selectedRows)
172 String idStr = getResultTable()
173 .getValueAt(summaryRow, primaryKeyColIndex).toString();
174 selectedIdsSet.add(idStr);
177 for (String idStr : paginatorCart)
179 selectedIdsSet.add(idStr);
182 for (String selectedId : selectedIdsSet)
184 selectedIds.append(selectedId).append(";");
187 SwingUtilities.invokeLater(new Runnable()
192 AlignmentI allSeqs = null;
193 FormatAdapter fl = new jalview.io.FormatAdapter();
194 for (String tdbURL : selectedIdsSet)
198 // retrieve the structure via its URL
199 AlignmentI tdbAl = fl.readFile(tdbURL, DataSourceType.URL,
202 // TODO: pad structure according to its Uniprot Start so all line up w.r.t. the Uniprot reference sequence
203 // TODO: give the structure a sensible name (not the giant URL *:o) )
208 allSeqs.append(tdbAl);
215 } catch (Exception x)
218 "Couldn't retrieve 3d-beacons model for uniprot id"
219 + searchTerm + " : " + tdbURL,
223 seqFetcher.parseResult(allSeqs,
224 "3D-Beacons models for " + searchTerm, FileFormat.MMCif,
229 delayAndEnableActionButtons();
233 public FTSRestClientI getFTSRestClient()
235 return TDBeaconsFTSRestClient.getInstance();
239 public String getFTSFrameTitle()
241 return defaultFTSFrameTitle;
245 public boolean isPaginationEnabled()
251 public Map<String, Integer> getTempUserPrefs()
253 return tempUserPrefs;
257 public String getCacheKey()
259 return THREEDB_FTS_CACHE_KEY;
263 public String getAutosearchPreference()
265 return THREEDB_AUTOSEARCH;
269 protected void showHelp()
271 System.out.println("No help implemented yet.");
275 public static String decodeSearchTerm(String enteredText)
277 // no multiple query support yet