Skip to content

Commit

Permalink
Added plate deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpalladino committed Feb 4, 2025
1 parent bc2b09c commit a35eb5f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,45 @@ public void deleteSamples(Program program, List<String> sampleDbIds) throws ApiE
String batchDbId = postSamplesBatch(programBrAPIBaseUrl, sampleDbIds);

// delete samples specified in batch
deleteSamplesBatch(programBrAPIBaseUrl, batchDbId);
deleteBatch(programBrAPIBaseUrl, batchDbId);
}

/**
* Deletes all plates specified in the brapi server
* @param program
* @param plateDbIds
* @throws ApiException
*/
public void deletePlates(Program program, List<String> plateDbIds) throws ApiException {
// create batch of samples, not yet included in brapi client TODO: switch to brapi client when available
String programBrAPIBaseUrl = brAPIDAOUtil.getProgramBrAPIBaseUrl(program.getId());
String batchDbId = postPlatesBatch(programBrAPIBaseUrl, plateDbIds);

// TODO: delete plates associated with submission, could potentially only require brapi server side change if deleting a sample cascades
// delete plates specified in batch
deleteBatch(programBrAPIBaseUrl, batchDbId);
}


private String postSamplesBatch(String programBrAPIBaseUrl, List<String> sampleDbIds) throws ApiException {
HttpUrl.Builder requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/batchDeletes").newBuilder();
//requestUrl.addQueryParameter("hardDelete", Boolean.toString(hard));

BatchDeleteRequest requestBody = new BatchDeleteRequest(sampleDbIds);
SampleBatchDeleteRequest requestBody = new SampleBatchDeleteRequest(sampleDbIds);
String json = gson.toJson(requestBody);
RequestBody body = RequestBody.create(json, MediaType.get("application/json"));
HttpUrl url = requestUrl.build();
return postBatch(url, body, programBrAPIBaseUrl);
}

private String postPlatesBatch(String programBrAPIBaseUrl, List<String> plateDbIds) throws ApiException {
HttpUrl.Builder requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/batchDeletes").newBuilder();
PlateBatchDeleteRequest requestBody = new PlateBatchDeleteRequest(plateDbIds);
String json = gson.toJson(requestBody);
RequestBody body = RequestBody.create(json, MediaType.get("application/json"));
HttpUrl url = requestUrl.build();
return postBatch(url, body, programBrAPIBaseUrl);
}

private String postBatch(HttpUrl url, RequestBody body, String programBrAPIBaseUrl) throws ApiException {

Request brapiRequest = new Request.Builder()
.url(url)
.post(body)
Expand All @@ -153,12 +178,11 @@ private String postSamplesBatch(String programBrAPIBaseUrl, List<String> sampleD
return resultObject.get("batchDeleteDbId").getAsString();
} else if (resultObject.has("searchResultsDbId")) {
// TODO: once api stuff is in client use BrAPIDAOUtil::search to handle retries, for now just request once
// could maybe be an issue for large number of samples
// could be an issue for large number of samples
return getBatchDeleteDbIdFromSearchResult(programBrAPIBaseUrl, resultObject.get("searchResultsDbId").getAsString());
} else {
throw new InternalServerException("Expected batchDeleteDbId or searchResultsDbId but got " + resultObject);
}

}

private String getBatchDeleteDbIdFromSearchResult(String programBrAPIBaseUrl, String searchResultDbId) throws ApiException {
Expand All @@ -178,7 +202,7 @@ private String getBatchDeleteDbIdFromSearchResult(String programBrAPIBaseUrl, St
return resultObject.get("batchDeleteDbId").getAsString();
}

private void deleteSamplesBatch(String programBrAPIBaseUrl, String batchDbId) throws ApiException {
private void deleteBatch(String programBrAPIBaseUrl, String batchDbId) throws ApiException {
HttpUrl.Builder requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/batchDeletes/" + batchDbId).newBuilder();
requestUrl.addQueryParameter("hardDelete", "true");

Expand All @@ -195,11 +219,11 @@ private void deleteSamplesBatch(String programBrAPIBaseUrl, String batchDbId) th
/**
* TODO: temporary minimal model here until brapi client is updated with delete models
*/
public class BatchDeleteRequest {
public class SampleBatchDeleteRequest {
private String batchDeleteType;
private Search search;

public BatchDeleteRequest(List<String> sampleDbIds) {
public SampleBatchDeleteRequest(List<String> sampleDbIds) {
this.batchDeleteType = "samples";
this.search = new Search(sampleDbIds);
}
Expand All @@ -213,4 +237,22 @@ public Search(List<String> sampleDbIds) {
}
}

public class PlateBatchDeleteRequest {
private String batchDeleteType;
private Search search;

public PlateBatchDeleteRequest(List<String> plateDbIds) {
this.batchDeleteType = "plates";
this.search = new Search(plateDbIds);
}

private class Search {
private List<String> plateDbIds;

public Search(List<String> plateDbIds) {
this.plateDbIds = plateDbIds;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,19 @@ public void deleteSampleSubmission(Program program, UUID submissionId) throws Ap
// delete samples
// delete plates

// create a batch of sampleIds to delete
// create a batch of sampleIds and plateIds to delete

// get samples with the samble submission xref
// get samples with the sample submission xref
List<BrAPISample> samples = sampleDAO.readSamplesBySubmissionIds(program, List.of(submissionId.toString()));

// extract sampleDbIds to include in batch
// extract sampleDbIds and plateDbIds to include in batches
List<String> sampleDbIds = samples.stream().map(BrAPISample::getSampleDbId).collect(Collectors.toList());
List<String> platesDbIds = samples.stream().map(BrAPISample::getPlateDbId).collect(Collectors.toList());

// create batch of samples, not yet included in brapi client TODO: switch to brapi client when available
// TODO: uncomment when brapi server is working
sampleDAO.deleteSamples(program, sampleDbIds);

// TODO: delete plates, not yet supported in brapi server
sampleDAO.deletePlates(program, platesDbIds);

// delete sample submission record from bidb
submissionDAO.deleteById(submissionId);
Expand Down

0 comments on commit a35eb5f

Please sign in to comment.