From f604f7c2efb76667b2116e0d6b4cbf4e0a5e810d Mon Sep 17 00:00:00 2001 From: Vedran Kasalica Date: Mon, 16 Dec 2024 12:39:12 +0100 Subject: [PATCH] Add categories to Design-time benchmarks --- .../models/benchmarks/BenchmarkBase.java | 3 + .../restape/ToolBenchmarkingAPIs.java | 110 +++++++++--------- .../PubmetricBenchmarksTest.java | 81 +++++++++++++ 3 files changed, 139 insertions(+), 55 deletions(-) create mode 100644 src/test/java/nl/esciencecenter/PubmetricBenchmarksTest.java diff --git a/src/main/java/nl/esciencecenter/models/benchmarks/BenchmarkBase.java b/src/main/java/nl/esciencecenter/models/benchmarks/BenchmarkBase.java index 0bb4ae0..b280d9f 100644 --- a/src/main/java/nl/esciencecenter/models/benchmarks/BenchmarkBase.java +++ b/src/main/java/nl/esciencecenter/models/benchmarks/BenchmarkBase.java @@ -15,6 +15,8 @@ public class BenchmarkBase { @NonNull private String benchmarkTitle; @NonNull + private String benchmarkCategory; + @NonNull private String benchmarkDescription; @NonNull private String unit; @@ -24,6 +26,7 @@ public class BenchmarkBase { public JSONObject getTitleJson() { JSONObject benchmarkJson = new JSONObject(); benchmarkJson.put("title", benchmarkTitle); + benchmarkJson.put("category", benchmarkCategory); benchmarkJson.put("description", benchmarkDescription); benchmarkJson.put("unit", unit); return benchmarkJson; diff --git a/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java b/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java index a15d19d..66247ef 100644 --- a/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java +++ b/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java @@ -96,63 +96,63 @@ static boolean computeBenchmarks(SolutionsList candidateSolutions, String runID) } /** - * Get the Pubmetric benchmarks for the workflow. - * @param workflow - * @return - */ - public static JSONObject getPubmetricBenchmarks(SolutionWorkflow workflow) { - // Generate the CWL file content + * Get the Pubmetric benchmarks for the workflow. + * @param workflow the SolutionWorkflow instance + * @return JSON response from Pubmetric API + */ + public static JSONObject getPubmetricBenchmarks(SolutionWorkflow workflow) { DefaultCWLCreator cwlCreator = new DefaultCWLCreator(workflow); String cwlFileContent = cwlCreator.generate(); - - // Convert the CWL content to a byte array byte[] cwlFileBytes = cwlFileContent.getBytes(); return sendPostToPubmetric(cwlFileBytes); - } - - - /** - * Send a POST request to the Pubmetric API to get the benchmarks for the CWL file. - * @param cwlFileBytes - * @return - */ - public static JSONObject sendPostToPubmetric(byte[] cwlFileBytes) { - // Create the HTTP client - CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpPost uploadFile = new HttpPost("http://pubmetric:8000/score_workflow/"); - - // Create a multipart entity with the CWL file + } + + /** + * Send a POST request to the Pubmetric API to get benchmarks. + * @param cwlFileBytes byte array of CWL file content + * @return JSON response from Pubmetric API + */ + public static JSONObject sendPostToPubmetric(byte[] cwlFileBytes) { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost uploadFile = createHttpPost(cwlFileBytes); + return executeRequest(httpClient, uploadFile); + } catch (IOException | JSONException e) { + log.error("Error while processing Pubmetric benchmarks", e); + return new JSONObject(); // return empty JSON if an error occurs + } + } + + /** + * Creates an HttpPost request with the provided CWL content. + * @param cwlFileBytes byte array of CWL file content + * @return configured HttpPost request + */ + private static HttpPost createHttpPost(byte[] cwlFileBytes) { + HttpPost uploadFile = new HttpPost("http://localhost:8000/score_workflow/"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addBinaryBody("cwl_file", cwlFileBytes, org.apache.http.entity.ContentType.DEFAULT_BINARY, - "workflow.cwl"); - - HttpEntity multipart = builder.build(); - uploadFile.setEntity(multipart); - - // Execute the request - CloseableHttpResponse response; - try { - response = httpClient.execute(uploadFile); - HttpEntity responseEntity = response.getEntity(); - - // Print the response - if (responseEntity != null) { - String responseString = EntityUtils.toString(responseEntity); - return new JSONObject(responseString); - } - - // Close resources - response.close(); - httpClient.close(); - } catch (IOException e) { - log.error("Error while fetching the Pubmetric benchmarks"); - } catch (JSONException e) { - log.error("Error while parsing the Pubmetric benchmarks"); + builder.addBinaryBody("cwl_file", cwlFileBytes, org.apache.http.entity.ContentType.DEFAULT_BINARY, "workflow.cwl"); + uploadFile.setEntity(builder.build()); + return uploadFile; + } + + /** + * Executes the HTTP request and returns the response as a JSONObject. + * @param httpClient the HTTP client + * @param uploadFile the configured HttpPost request + * @return JSON response from Pubmetric API + * @throws IOException if an error occurs during the HTTP call + */ + private static JSONObject executeRequest(CloseableHttpClient httpClient, HttpPost uploadFile) throws IOException { + try (CloseableHttpResponse response = httpClient.execute(uploadFile)) { + HttpEntity responseEntity = response.getEntity(); + if (responseEntity != null) { + String responseString = EntityUtils.toString(responseEntity); + return new JSONObject(responseString); + } + return new JSONObject(); // return empty JSON if no response entity } - - return new JSONObject(); - } + } /** * Compute the benchmarks (based on bio.tools and OpenEBench APIs) for the @@ -210,15 +210,15 @@ private static List computeBiotoolsBenchmark(SolutionWorkflow workflo List benchmarks = new ArrayList<>(); String unitOS = "supported / not supported"; - BenchmarkBase linuxBenchmark = new BenchmarkBase("Linux", "Linux (OS) supported tools", + BenchmarkBase linuxBenchmark = new BenchmarkBase("Linux", "OS", "Linux (OS) supported tools", unitOS, "operatingSystem", "Linux"); benchmarks.add(BioToolsBenchmarkProcessor.benchmarkOSSupport(biotoolsAnnotations, linuxBenchmark)); - BenchmarkBase macOSBenchmark = new BenchmarkBase("Mac OS", "Mac OS supported tools", + BenchmarkBase macOSBenchmark = new BenchmarkBase("Mac OS", "OS", "Mac OS supported tools", unitOS, "operatingSystem", "Mac"); benchmarks.add(BioToolsBenchmarkProcessor.benchmarkOSSupport(biotoolsAnnotations, macOSBenchmark)); - BenchmarkBase windowsBenchmark = new BenchmarkBase("Windows", "Windows (OS) supported tools", + BenchmarkBase windowsBenchmark = new BenchmarkBase("Windows", "OS", "Windows (OS) supported tools", unitOS, "operatingSystem", "Windows"); benchmarks.add(BioToolsBenchmarkProcessor.benchmarkOSSupport(biotoolsAnnotations, windowsBenchmark)); @@ -259,11 +259,11 @@ private static List computeOpenEBenchmarks(SolutionWorkflow workflow) List benchmarks = new ArrayList<>(); - BenchmarkBase licenseBenchmark = new BenchmarkBase("License", "License information available", + BenchmarkBase licenseBenchmark = new BenchmarkBase("License", "License", "License information available", "license type", "license", null); benchmarks.add(OpenEBenchBenchmarkProcessor.benchmarkLicenses(openEBenchBiotoolsMetrics, licenseBenchmark)); - BenchmarkBase citationsBenchmark = new BenchmarkBase("Citations", "Citations annotated per tool", + BenchmarkBase citationsBenchmark = new BenchmarkBase("Citations", "Bibliometrics", "Citations annotated per tool", "citation count", "citation", null); benchmarks.add(OpenEBenchBenchmarkProcessor.countCitationsBenchmark(openEBenchBiotoolsMetrics, citationsBenchmark)); diff --git a/src/test/java/nl/esciencecenter/PubmetricBenchmarksTest.java b/src/test/java/nl/esciencecenter/PubmetricBenchmarksTest.java new file mode 100644 index 0000000..f7a008c --- /dev/null +++ b/src/test/java/nl/esciencecenter/PubmetricBenchmarksTest.java @@ -0,0 +1,81 @@ +package nl.esciencecenter; + +import org.json.JSONObject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import java.io.IOException; + +import nl.esciencecenter.restape.ToolBenchmarkingAPIs; + +public class PubmetricBenchmarksTest { + + // @Test + // public void testSendPostToPubmetricWithFile() throws IOException { + // // Read CWL file content from the file system + // File cwlFile = new File("/Users/vedran/Downloads/candidate_workflow_4.cwl"); // Replace with the actual path + // byte[] fileContent = Files.readAllBytes(cwlFile.toPath()); + + // // Call the method to test + // JSONObject response = ToolBenchmarkingAPIs.sendPostToPubmetric(fileContent); + + // // Verify that the response is not null + // assertFalse(response.isEmpty()); + // } + + @Test + public void testSendPostToPubmetricWithString() throws IOException { + // Hardcoded CWL content string + String cwlFileContent = """ + # WorkflowNo_3 +# This workflow is generated by APE (https://github.com/sanctuuary/APE). +cwlVersion: v1.2 +class: Workflow + +label: WorkflowNo_3 +doc: A workflow including the tool(s) Comet, idconvert, ProteinProphet. + +inputs: + input_1: + type: File + format: "http://edamontology.org/format_3244" # mzML + input_2: + type: File + format: "http://edamontology.org/format_1929" # FASTA +steps: + Comet_01: + run: https://raw.githubusercontent.com/Workflomics/containers/main/cwl/tools/Comet/Comet.cwl + in: + Comet_in_1: input_1 + Comet_in_2: input_2 + out: [Comet_out_1, Comet_out_2, Comet_out_3] + idconvert_02: + run: https://raw.githubusercontent.com/Workflomics/containers/main/cwl/tools/idconvert/idconvert_to_pepXML.cwl + in: + idconvert_in_1: Comet_01/Comet_out_2 + out: [idconvert_out_1] + ProteinProphet_03: + run: https://raw.githubusercontent.com/Workflomics/containers/main/cwl/tools/ProteinProphet/ProteinProphet.cwl + in: + ProteinProphet_in_1: idconvert_02/idconvert_out_1 + ProteinProphet_in_2: input_2 + out: [ProteinProphet_out_1, ProteinProphet_out_2] +outputs: + output_1: + type: File + format: "http://edamontology.org/format_3747" # protXML + outputSource: ProteinProphet_03/ProteinProphet_out_1 + + """; + + // Convert the string to a byte array + byte[] cwlFileBytes = cwlFileContent.getBytes(); + + // Call the method to test + JSONObject response = ToolBenchmarkingAPIs.sendPostToPubmetric(cwlFileBytes); + + // Verify that the response is not null + assertFalse(response.isEmpty()); + } +} \ No newline at end of file