From f640efee12150d1ae174c64c936f4630716cb7c4 Mon Sep 17 00:00:00 2001 From: Arnaldo Date: Thu, 25 Mar 2021 15:20:11 +0100 Subject: [PATCH] Created 3Dbeacons data_columns.txt Test passed for data columns --- resources/fts/tdbeacons_data_columns.txt | 44 ++++++ src/jalview/fts/core/FTSRestResponse.java | 10 ++ .../threedbeacons/TDBeaconsFTSRestClient.java | 146 ++++++++++++++++---- .../threedbeacons/TDBeaconsFTSRestClientTest.java | 69 ++++++++- 4 files changed, 236 insertions(+), 33 deletions(-) create mode 100644 resources/fts/tdbeacons_data_columns.txt diff --git a/resources/fts/tdbeacons_data_columns.txt b/resources/fts/tdbeacons_data_columns.txt new file mode 100644 index 0000000..2eb3d31 --- /dev/null +++ b/resources/fts/tdbeacons_data_columns.txt @@ -0,0 +1,44 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +tdbeacons_data_columns +# +_group.id +_group.name +_group.sort_order +g1;Miscellaneous;2 +g2;Name;1 +# +_data_column.primary_key;id +_data_column.default_response_page_size;100 +# +_data_column.name +_data_column.code +_data_column.group_id +_data_column.data_type +_data_column.min_col_width +_data_column.max_col_width +_data_column.preferred_col_width +_data_column.is_shown_by_default +_data_column.is_searchable +Uniprot Id;id;String;g2;80;150;85;true;true +UniProt Accession;uniprot_accession;String;g2;50;400;95;true;false +Entry Name;entry name;String;g2;100;150;105;true;true diff --git a/src/jalview/fts/core/FTSRestResponse.java b/src/jalview/fts/core/FTSRestResponse.java index 597bb89..a5c8705 100644 --- a/src/jalview/fts/core/FTSRestResponse.java +++ b/src/jalview/fts/core/FTSRestResponse.java @@ -41,6 +41,8 @@ import javax.swing.table.DefaultTableModel; public class FTSRestResponse { private int numberOfItemsFound; + + private int sequenceLength; private String responseTime; @@ -75,6 +77,14 @@ public class FTSRestResponse { this.searchSummary = searchSummary; } + + public int getSequenceLength() { + return sequenceLength; + } + + public void setSequenceLength(int sequenceLength) { + this.sequenceLength = sequenceLength; + } /** * Convenience method to obtain a Table model for a given summary List based diff --git a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java index 1ee7f37..e6d45e5 100644 --- a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java +++ b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java @@ -6,6 +6,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import javax.ws.rs.core.MediaType; @@ -35,6 +36,10 @@ public class TDBeaconsFTSRestClient extends FTSRestClient private static FTSRestClientI instance = null; + protected TDBeaconsFTSRestClient() + { + } + @SuppressWarnings("unchecked") @Override public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest) @@ -48,6 +53,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient int responseSize = (tdbRestRequest.getResponseSize() == 0) ? getDefaultResponsePageSize() : tdbRestRequest.getResponseSize(); + //int offSet = pdbRestRequest.getOffSet(); String query = tdbRestRequest.getFieldToSearchBy() + tdbRestRequest.getSearchTerm(); @@ -70,7 +76,9 @@ public class TDBeaconsFTSRestClient extends FTSRestClient } WebResource webResource; webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN) - .queryParam("P",query); + .queryParam("ac", query); +// .queryParam("rows", String.valueOf(responseSize)) +// .queryParam("P",query); URI uri = webResource.getURI(); System.out.println(uri); @@ -132,6 +140,10 @@ public class TDBeaconsFTSRestClient extends FTSRestClient } + public String setSearchTerm() { + return null; + } + public static FTSRestResponse parseTDBeaconsJsonResponse( String tdbJsonResponseString, FTSRestRequest tdbRestRequest) { @@ -153,35 +165,25 @@ public class TDBeaconsFTSRestClient extends FTSRestClient { jsonObj = (Map) JSONUtils.parse(tdbJsonResponseString); } - Map tdbEntry = (Map) jsonObj.get("uniprot_entry"); + //Map tdbResponse = (Map) jsonObj.get("structures"); //String seqLength = tdbEntry.get("sequence_length").toString(); - int seqLength = Integer.valueOf(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)); -// } + int seqLength = (int) ((Map) jsonObj.get("uniprot_entry")).get("sequence_length"); + result = new ArrayList<>(); + List structures = (List) jsonObj.get("structures"); + int numFound = 0; + for (Iterator strucIter = structures.iterator(); strucIter + .hasNext();) + { + Map structure = (Map) strucIter.next(); + result.add(getFTSData(structure, tdbRestRequest)); + numFound++; + } searchResult.setNumberOfItemsFound(numFound); - // searchResult.setAccessionName(accessName); ?usefulness? -// searchResult.setSearchSummary(result); -// -// } + searchResult.setSearchSummary(result); + searchResult.setSequenceLength(seqLength); + } catch (ParseException e) { e.printStackTrace(); @@ -189,6 +191,96 @@ public class TDBeaconsFTSRestClient extends FTSRestClient return searchResult; } +private static FTSData getFTSData(Map tdbJsonStructure, + FTSRestRequest tdbRequest) + { + // TODO Auto-generated method stub + String primaryKey = null; + Object[] summaryRowData; + Collection displayFields = tdbRequest.getWantedFields(); + int colCounter = 0; + summaryRowData = new Object[displayFields.size()]; + for (FTSDataColumnI field : displayFields) { + String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " " + : tdbJsonStructure.get(field.getCode()).toString(); + if (field.isPrimaryKeyColumn()) + { + primaryKey = fieldData; + summaryRowData[colCounter++] = primaryKey; + } + else if (fieldData == null || fieldData.isEmpty()) + { + summaryRowData[colCounter++] = null; + } + else + { + try + { + summaryRowData[colCounter++] = (field.getDataType() + .getDataTypeClass() == Integer.class) + ? Integer.valueOf(fieldData) + : (field.getDataType() + .getDataTypeClass() == Double.class) + ? Double.valueOf(fieldData) + : fieldData; + } catch (Exception e) + { + e.printStackTrace(); + System.out.println("offending value:" + fieldData); + } + } + } + final String primaryKey1 = primaryKey; + final Object[] summaryRowData1 = summaryRowData; + + return new FTSData() + { + + @Override + public Object[] getSummaryData() + { + return summaryRowData1; + } + + @Override + public Object getPrimaryKey() + { + return primaryKey1; + } + + /** + * Returns a string representation of this object; + */ + @Override + public String toString() + { + StringBuilder summaryFieldValues = new StringBuilder(); + for (Object summaryField : summaryRowData1) + { + summaryFieldValues.append( + summaryField == null ? " " : summaryField.toString()) + .append("\t"); + } + return summaryFieldValues.toString(); + } + + /** + * Returns hash code value for this object + */ + @Override + public int hashCode() + { + return Objects.hash(primaryKey1, this.toString()); + } + + @Override + public boolean equals(Object that) + { + return this.toString().equals(that.toString()); + } + }; + } + // private static FTSData getFTSData(Map doc, // FTSRestRequest tdbRestRequest) // { diff --git a/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java b/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java index be76222..3c0d616 100644 --- a/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java +++ b/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java @@ -3,6 +3,7 @@ package jalview.fts.threedbeacons; import static org.testng.AssertJUnit.assertTrue; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.testng.Assert; @@ -12,6 +13,7 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import jalview.fts.api.FTSDataColumnI; +import jalview.fts.core.FTSRestClient; import jalview.fts.core.FTSRestRequest; import jalview.fts.core.FTSRestResponse; import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient; @@ -25,37 +27,93 @@ public class TDBeaconsFTSRestClientTest JvOptionPane.setInteractiveMode(false); JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); } + + private FTSRestClient ftsRestClient; @BeforeMethod(alwaysRun = true) public void setUp() throws Exception { + ftsRestClient = new FTSRestClient() + { + + @Override + public String getColumnDataConfigFileName() + { + return "/fts/tdbeacons_data_columns.txt"; + } + + @Override + public FTSRestResponse executeRequest(FTSRestRequest ftsRequest) + throws Exception + { + return null; + } + }; } @AfterMethod(alwaysRun = true) public void tearDown() throws Exception { } - - @Test(groups = { "External", "Network" }) + @Test + public void getAllDefaulDisplayedDataColumns() + { + Assert.assertNotNull(ftsRestClient + .getAllDefaultDisplayedFTSDataColumns()); + System.out.println(ftsRestClient.getAllDefaultDisplayedFTSDataColumns()); + Assert.assertTrue(!ftsRestClient.getAllDefaultDisplayedFTSDataColumns() + .isEmpty()); + Assert.assertEquals(ftsRestClient + .getAllDefaultDisplayedFTSDataColumns().size(), 3); + } + + @Test(groups = { "Functional" }) + public void getPrimaryKeyColumIndexTest() + { + Collection wantedFields = ftsRestClient + .getAllDefaultDisplayedFTSDataColumns(); + int foundIndex = -1; + try + { + Assert.assertEquals(foundIndex, -1); + foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields, + false); + Assert.assertEquals(foundIndex, 0); + foundIndex = ftsRestClient + .getPrimaryKeyColumIndex(wantedFields, true); + Assert.assertEquals(foundIndex, 1); + } catch (Exception e) + { + e.printStackTrace(); + Assert.fail("Exception thrown while testing..."); + } + } + + @Test(groups = { "External", "Network" }) public void executeRequestTest() { List wantedFields = new ArrayList(); try { wantedFields.add(TDBeaconsFTSRestClient.getInstance() - .getDataColumnByNameOrCode("uniprot_entry")); + .getDataColumnByNameOrCode("model_category")); + wantedFields.add(TDBeaconsFTSRestClient.getInstance() + .getDataColumnByNameOrCode("provider")); + wantedFields.add(TDBeaconsFTSRestClient.getInstance() + .getDataColumnByNameOrCode("created")); } catch (Exception e1) { e1.printStackTrace(); } - System.out.println(wantedFields); + System.out.println("wantedFields >>" + wantedFields); FTSRestRequest request = new FTSRestRequest(); //request.setAllowEmptySeq(false); //request.setResponseSize(100); request.setSearchTerm("01308.json"); - //request.setWantedFields(wantedFields); + request.setWantedFields(wantedFields); + System.out.println("request : " + request.getFieldToSearchBy()); System.out.println(request.toString()); FTSRestResponse response; @@ -72,5 +130,4 @@ public class TDBeaconsFTSRestClientTest assertTrue(response.getSearchSummary() != null); assertTrue(response.getSearchSummary().size() > 99); } - } -- 1.7.10.2