From: Arnaldo Date: Tue, 2 Mar 2021 15:24:01 +0000 (+0100) Subject: First commit of 3DB FTS Panel & Restclient X-Git-Tag: Release_2_11_2_0~39^2~62 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=00974ead1f9b7c9690f33b87bf31c6817446c8aa;p=jalview.git First commit of 3DB FTS Panel & Restclient --- diff --git a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSPanel.java b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSPanel.java new file mode 100644 index 0000000..21034ac --- /dev/null +++ b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSPanel.java @@ -0,0 +1,213 @@ +package jalview.fts.service.threedbeacons; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +import jalview.fts.api.FTSDataColumnI; +import jalview.fts.api.FTSRestClientI; +import jalview.fts.core.FTSRestRequest; +import jalview.fts.core.FTSRestResponse; +import jalview.fts.core.GFTSPanel; +import jalview.fts.service.pdb.PDBFTSRestClient; +import jalview.gui.SequenceFetcher; +import jalview.util.MessageManager; + +@SuppressWarnings("serial") +public class TDBeaconsFTSPanel extends GFTSPanel +{ + private static String defaultFTSFrameTitle = MessageManager + .getString("label.threedb_sequence_fetcher"); + + private static Map tempUserPrefs = new HashMap<>(); + + private static final String THREEDB_FTS_CACHE_KEY = "CACHE.THREEDB_FTS"; + + private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH"; + + public TDBeaconsFTSPanel(SequenceFetcher fetcher) + { + super(fetcher); + pageLimit = TDBeaconsFTSRestClient.getInstance().getDefaultResponsePageSize(); + this.seqFetcher = fetcher; + this.progressIndicator = (fetcher == null) ? null + : fetcher.getProgressIndicator(); + } + + @Override + public void searchAction(boolean isFreshSearch) + { + mainFrame.requestFocusInWindow(); + if (isFreshSearch) + { + offSet = 0; + } + new Thread() + { + @Override + public void run() + { + reset(); + boolean allowEmptySequence = false; + if (getTypedText().length() > 0) + { + setSearchInProgress(true); + long startTime = System.currentTimeMillis(); + + String searchTarget = ((FTSDataColumnI) cmb_searchTarget + .getSelectedItem()).getCode(); + wantedFields = TDBeaconsFTSRestClient.getInstance() + .getAllDefaultDisplayedFTSDataColumns(); + String searchTerm = getTypedText(); // to add : decodeSearchTerm + + FTSRestRequest request = new FTSRestRequest(); + request.setAllowEmptySeq(allowEmptySequence); + request.setResponseSize(100); + request.setFieldToSearchBy("(" + searchTarget + ":"); + request.setSearchTerm(searchTerm + ")"); + request.setOffSet(offSet); + request.setWantedFields(wantedFields); + FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient.getInstance(); + FTSRestResponse resultList; + try + { + resultList = tdbRestClient.executeRequest(request); + } catch (Exception e) + { + setErrorMessage(e.getMessage()); + checkForErrors(); + setSearchInProgress(false); + return; + } + + if (resultList.getSearchSummary() != null + && resultList.getSearchSummary().size() > 0) + { + getResultTable().setModel(FTSRestResponse.getTableModel(request, + resultList.getSearchSummary())); + FTSRestResponse.configureTableColumn(getResultTable(), + wantedFields, tempUserPrefs); + getResultTable().setVisible(true); + } + + long endTime = System.currentTimeMillis(); + totalResultSetCount = resultList.getNumberOfItemsFound(); + resultSetCount = resultList.getSearchSummary() == null ? 0 + : resultList.getSearchSummary().size(); + String result = (resultSetCount > 0) + ? MessageManager.getString("label.results") + : MessageManager.getString("label.result"); + + if (isPaginationEnabled() && resultSetCount > 0) + { + String f1 = totalNumberformatter.format(Integer.valueOf(offSet + 1)); + String f2 = totalNumberformatter + .format(Integer.valueOf(offSet + resultSetCount)); + String f3 = totalNumberformatter + .format(Integer.valueOf(totalResultSetCount)); + updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result + + " " + f1 + " to " + f2 + " of " + f3 + " " + " (" + + (endTime - startTime) + " milli secs)"); + } + else + { + updateSearchFrameTitle(defaultFTSFrameTitle + " - " + + resultSetCount + " " + result + " (" + + (endTime - startTime) + " milli secs)"); + } + + setSearchInProgress(false); + refreshPaginatorState(); + updateSummaryTableSelections(); + } + txt_search.updateCache(); + } + }.start(); + } + + @Override + public void okAction() + { + // mainFrame.dispose(); + disableActionButtons(); + StringBuilder selectedIds = new StringBuilder(); + HashSet selectedIdsSet = new HashSet<>(); + int primaryKeyColIndex = 0; + try + { + primaryKeyColIndex = getFTSRestClient() + .getPrimaryKeyColumIndex(wantedFields, false); + } catch (Exception e) + { + e.printStackTrace(); + } + int[] selectedRows = getResultTable().getSelectedRows(); + String searchTerm = getTypedText(); + for (int summaryRow : selectedRows) + { + String idStr = getResultTable() + .getValueAt(summaryRow, primaryKeyColIndex).toString(); + selectedIdsSet.add(searchTerm); + } + + for (String idStr : paginatorCart) + { + selectedIdsSet.add(searchTerm); + } + + for (String selectedId : selectedIdsSet) + { + selectedIds.append(selectedId).append(";"); + } + + String ids = selectedIds.toString(); + seqFetcher.setQuery(ids); + Thread worker = new Thread(seqFetcher); + worker.start(); + delayAndEnableActionButtons(); + } + + @Override + public FTSRestClientI getFTSRestClient() + { + return TDBeaconsFTSRestClient.getInstance(); + } + + @Override + public String getFTSFrameTitle() + { + return defaultFTSFrameTitle; + } + + @Override + public boolean isPaginationEnabled() + { + return true; + } + + @Override + public Map getTempUserPrefs() + { + return tempUserPrefs; + } + + @Override + public String getCacheKey() + { + return THREEDB_FTS_CACHE_KEY; + } + + @Override + public String getAutosearchPreference() + { + return THREEDB_AUTOSEARCH; + } + + @Override + protected void showHelp() + { + System.out.println("No help implemented yet."); + + } + +} diff --git a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java new file mode 100644 index 0000000..967fb2d --- /dev/null +++ b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java @@ -0,0 +1,240 @@ +package jalview.fts.service.threedbeacons; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.json.simple.parser.ParseException; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.DefaultClientConfig; + +import jalview.datamodel.SequenceI; +import jalview.fts.api.FTSData; +import jalview.fts.api.FTSDataColumnI; +import jalview.fts.api.FTSRestClientI; +import jalview.fts.core.FTSRestClient; +import jalview.fts.core.FTSRestRequest; +import jalview.fts.core.FTSRestResponse; +import jalview.fts.service.pdb.PDBFTSRestClient; +import jalview.util.JSONUtils; +import jalview.util.MessageManager; +import jalview.util.Platform; + +public class TDBeaconsFTSRestClient extends FTSRestClient +{ + private static final String DEFAULT_THREEDBEACONS_DOMAIN = + "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/"; + + private static FTSRestClientI instance = null; + + @SuppressWarnings("unchecked") + @Override + public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest) + throws Exception + { + try + { + // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient + + int responseSize = (tdbRestRequest.getResponseSize() == 0) + ? getDefaultResponsePageSize() + : tdbRestRequest.getResponseSize(); + + String query = tdbRestRequest.getFieldToSearchBy() + + tdbRestRequest.getSearchTerm(); + Client client; + Class clientResponseClass; + if (Platform.isJS()) + { + // JavaScript only + client = (Client) (Object) new jalview.javascript.web.Client(); + clientResponseClass = (Class) (Object) jalview.javascript.web.ClientResponse.class; + } + else + /** + * Java only + * @j2sIgnore + */ + { + client = Client.create(new DefaultClientConfig()); + clientResponseClass = ClientResponse.class; + } + WebResource webResource; + webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN); + URI uri = webResource.getURI(); + + System.out.println(uri); + + // Execute the REST request + ClientResponse clientResponse = webResource + .accept(MediaType.APPLICATION_JSON).get(clientResponseClass); + + // Get the JSON string from the response object or directly from the + // client (JavaScript) + Map jsonObj = null; + String responseString = null; + + System.out.println("query >>>>>>> " + tdbRestRequest.toString()); + + // Check the response status and report exception if one occurs + int responseStatus = clientResponse.getStatus(); + switch (responseStatus) + { + // if succesful + case 200: + if (Platform.isJS()) + { + jsonObj = clientResponse.getEntity(Map.class); + } + else + { + responseString = clientResponse.getEntity(String.class); + } + break; + case 400: + throw new Exception(parseJsonExceptionString(responseString)); + default: + throw new Exception( + getMessageByHTTPStatusCode(responseStatus, "3DBeacons")); + } + // Process the response and return the result to the caller. + return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest); + } catch (Exception e) + { + String exceptionMsg = e.getMessage(); + if (exceptionMsg.contains("SocketException")) + { + // No internet connection + throw new Exception(MessageManager.getString( + "exception.unable_to_detect_internet_connection")); + } + else if (exceptionMsg.contains("UnknownHostException")) + { + // The server is unreachable + throw new Exception(MessageManager.formatMessage( + "exception.fts_server_unreachable", "3DB Hub")); + } + else + { + throw e; + } + } + + } + + public static FTSRestResponse parseTDBeaconsJsonResponse( + String tdbJsonResponseString, FTSRestRequest tdbRestRequest) + { + return parseTDBeaconsJsonResponse(tdbJsonResponseString, + (Map) null, tdbRestRequest); + } + + @SuppressWarnings("unchecked") + public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString, + Map jsonObj, FTSRestRequest tdbRestRequest) + { + FTSRestResponse searchResult = new FTSRestResponse(); + //searchResult.setNumberOfItemsFound(1); + List result = null; + + try + { + if (jsonObj == null) + { + jsonObj = (Map) JSONUtils.parse(tdbJsonResponseString); + } + Map tdbEntry = (Map) jsonObj.get("uniprot_entry"); + //String seqLength = tdbEntry.get("sequence_length").toString(); + int seqLength = Integer.valueOf(tdbEntry.get("sequence_length").toString()); + + // tdbStructures is the list of found structures +// Map tdbStructures = (Map) jsonObj.get("structures"); + Object[] structures = (Object[]) jsonObj.get("structures"); + String accessName = ((Map) jsonObj.get("uniprot_entry")) + .get("ac").toString(); + // this is to be modified + int numFound = structures.length; +// if (numFound > 0) +// { +// +// result = new ArrayList<>(); +// List models; +// //= ((Map) jsonObj.get("structures")) +// //.get("model_identifier").toString(); +// for (Iterator modelsIter = models.iterator(); modelsIter.hasNext();) +// .hasNext();) +// { +// Map doc = (Map) docIter.next(); +// result.add(getFTSData(doc, tdbRestRequest)); +// } + searchResult.setNumberOfItemsFound(numFound); + // searchResult.setAccessionName(accessName); ?usefulness? +// searchResult.setSearchSummary(result); +// +// } + } catch (ParseException e) + { + e.printStackTrace(); + } + return searchResult; + } + +// private static FTSData getFTSData(Map doc, +// FTSRestRequest tdbRestRequest) +// { +// String primaryKey = null; +// +// Object[] summaryRowData; +// +// Collection displayFields = tdbRestRequest.getWantedFields(); +// int colCounter = 0; +// summaryRowData = new Object[displayFields.size() + 1]; +// +// return null; +// } + + private String parseJsonExceptionString(String jsonErrorString) + { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getColumnDataConfigFileName() + { + // TODO Auto-generated method stub + return null; + } + + public static FTSRestClientI getInstance() + { + if (instance == null) + { + instance = new TDBeaconsFTSRestClient(); + } + return instance; + } + + private Collection allDefaultDisplayedStructureDataColumns; + + public Collection getAllDefaultDisplayedStructureDataColumns() + { + if (allDefaultDisplayedStructureDataColumns == null + || allDefaultDisplayedStructureDataColumns.isEmpty()) + { + allDefaultDisplayedStructureDataColumns = new ArrayList<>(); + allDefaultDisplayedStructureDataColumns + .addAll(super.getAllDefaultDisplayedFTSDataColumns()); + } + return allDefaultDisplayedStructureDataColumns; + } + +}