Skip to content

Commit

Permalink
Add categories to Design-time benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vedran-kasalica committed Dec 16, 2024
1 parent d8707c0 commit 5dce109
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class BenchmarkBase {
@NonNull
private String benchmarkTitle;
@NonNull
private String benchmarkCategory;
@NonNull
private String benchmarkDescription;
@NonNull
private String unit;
Expand All @@ -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;
Expand Down
110 changes: 55 additions & 55 deletions src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -210,15 +210,15 @@ private static List<Benchmark> computeBiotoolsBenchmark(SolutionWorkflow workflo
List<Benchmark> 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));

Expand Down Expand Up @@ -259,11 +259,11 @@ private static List<Benchmark> computeOpenEBenchmarks(SolutionWorkflow workflow)

List<Benchmark> 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));

Expand Down
81 changes: 81 additions & 0 deletions src/test/java/nl/esciencecenter/PubmetricBenchmarksTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 5dce109

Please sign in to comment.