Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BI-1910 - Allow Breeder to Upload a Genotype Sample File #306

Merged
merged 13 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ AWS_REGION=<aws region>
AWS_ACCESS_KEY_ID=<access key>
AWS_SECRET_KEY=<secret>
AWS_GENO_BUCKET=<s3 bucket for genotypic data uploads>
AWS_S3_ENDPOINT=<s3 endpoint, default https://s3.us-east-1.amazonaws.com>
AWS_S3_ENDPOINT=<s3 endpoint, default https://s3.us-east-1.amazonaws.com>

BRAPI_VENDOR_SUBMISSION_ENABLED=false #can a submission be sent to a vendor via BrAPI
BRAPI_VENDOR_CHECK_FREQUENCY=1d #how often to check for vendor updates for sample submissions
1 change: 1 addition & 0 deletions localstack/startup_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"timestamp": "2022-11-17T17:01:36.401503", "localstack_version": "1.2.0", "localstack_ext_version": "1.2.0", "pro_activated": false}]
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Breeding Insight API</name>
<name>DeltaBreed API</name>
<description></description>
<url>https://breedinginsight.org</url>

Expand Down Expand Up @@ -57,6 +57,10 @@
<name>David Phillips</name>
<email>[email protected]</email>
</developer>
<developer>
<name>Matthew Mandych</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
Expand Down Expand Up @@ -149,6 +153,13 @@
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

<dependencyManagement>
Expand Down
12 changes: 0 additions & 12 deletions settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github-brapi</id>
<name>GitHub Breeding Insight Apache Maven Packages</name>
<url>https://maven.pkg.github.com/Breeding-Insight/brapi</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github-fannypack</id>
<name>FannyPack github repository</name>
Expand All @@ -73,11 +66,6 @@
<username>${GITHUB_ACTOR}</username>
<password>${GITHUB_TOKEN}</password>
</server>
<server>
<id>github-brapi</id>
<username>${GITHUB_ACTOR}</username>
<password>${GITHUB_TOKEN}</password>
</server>
<server>
<id>github-fannypack</id>
<username>${GITHUB_ACTOR}</username>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.breedinginsight.api.v1.controller.geno;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.*;
import io.micronaut.http.annotation.*;
import io.micronaut.http.server.types.files.StreamedFile;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.extern.slf4j.Slf4j;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.v2.model.geno.BrAPIVendorOrderSubmission;
import org.breedinginsight.api.auth.*;
import org.breedinginsight.api.model.v1.response.DataResponse;
import org.breedinginsight.api.model.v1.response.Response;
import org.breedinginsight.api.model.v1.response.metadata.Metadata;
import org.breedinginsight.api.model.v1.response.metadata.Pagination;
import org.breedinginsight.api.model.v1.response.metadata.Status;
import org.breedinginsight.api.model.v1.response.metadata.StatusCode;
import org.breedinginsight.model.*;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.SampleSubmissionService;
import org.breedinginsight.services.UserService;
import org.breedinginsight.utilities.Utilities;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Controller("/${micronaut.bi.api.version}")
@Secured(SecurityRule.IS_AUTHENTICATED)
public class SampleSubmissionController {

private final boolean brapiSubmissionEnabled;
private final SampleSubmissionService sampleSubmissionService;
private final ProgramService programService;
private final SecurityService securityService;
private final UserService userService;

private final Gson gson;

@Inject
public SampleSubmissionController(@Property(name = "brapi.vendor-submission-enabled") boolean brapiSubmissionEnabled, SampleSubmissionService sampleSubmissionService, ProgramService programService, SecurityService securityService, UserService userService) {
this.brapiSubmissionEnabled = brapiSubmissionEnabled;
this.sampleSubmissionService = sampleSubmissionService;
this.programService = programService;
this.securityService = securityService;
this.userService = userService;
this.gson = new Gson();
}

@Get("programs/{programId}/submissions")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = ProgramSecuredRoleGroup.ALL)
public HttpResponse<Response<DataResponse<SampleSubmission>>> getProgramSampleSubmissions(@PathVariable UUID programId) {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
log.info(String.format("programId not found: %s", programId.toString()));
return HttpResponse.notFound();
}

List<SampleSubmission> submissions = sampleSubmissionService.getProgramSubmissions(program.get());
Metadata metadata = new Metadata(new Pagination(submissions.size(), submissions.size(), 1, 0),
List.of(new Status(StatusCode.INFO, "Successful Query")));
Response<DataResponse<SampleSubmission>> response = new Response<>(metadata, new DataResponse<>(submissions));
return HttpResponse.ok(response);
}

@Get("programs/{programId}/submissions/{submissionId}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = ProgramSecuredRoleGroup.ALL)
public HttpResponse<Response<SampleSubmission>> getSubmissionById(@PathVariable UUID programId, @PathVariable UUID submissionId, @QueryValue(value = "details", defaultValue = "false") @Nullable Boolean fetchDetails) {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
log.info(String.format("programId not found: %s", programId.toString()));
return HttpResponse.notFound();
}

try {
Optional<SampleSubmission> submission = sampleSubmissionService.getSampleSubmission(program.get(), submissionId, fetchDetails);

if(submission.isEmpty()) {
return HttpResponse.notFound();
}

Metadata metadata = new Metadata(new Pagination(1, 1, 1, 0),
List.of(new Status(StatusCode.INFO, "Successful Query")));
Response<SampleSubmission> response = new Response<>(metadata, submission.get());
return HttpResponse.ok(response);
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}
}

@Put("programs/{programId}/submissions/{submissionId}/status")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roles = {ProgramSecuredRole.SYSTEM_ADMIN})
public HttpResponse<Response<SampleSubmission>> updateSubmissionStatus(@PathVariable UUID programId, @PathVariable UUID submissionId, @Body String body) {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
log.info(String.format("programId not found: %s", programId.toString()));
return HttpResponse.notFound();
}

AuthenticatedUser actingUser = securityService.getUser();
Optional<User> user = userService.getById(actingUser.getId());
if (user.isEmpty()) {
return HttpResponse.unauthorized();
}

SampleSubmission.Status status = SampleSubmission.Status.fromValue(gson.fromJson(body, JsonObject.class).get("status").getAsString());
if(status == null) {
HttpResponse response = HttpResponse.badRequest("Unrecognized status");
return response;
}

try {
Optional<SampleSubmission> submission = sampleSubmissionService.updateSubmissionStatus(program.get(), submissionId, status, user.get());

if(submission.isEmpty()) {
return HttpResponse.notFound();
}

Metadata metadata = new Metadata(new Pagination(1, 1, 1, 0),
List.of(new Status(StatusCode.INFO, "Successful Update")));
Response<SampleSubmission> response = new Response<>(metadata, submission.get());
return HttpResponse.ok(response);
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}
}


@Get("/programs/{programId}/submissions/{submissionId}/dart")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
@Produces(value={"text/csv", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/octet-stream"})
public HttpResponse<StreamedFile> generateDArTFile(@PathVariable UUID programId, @PathVariable UUID submissionId) {
try {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
return HttpResponse.notFound();
}
Optional<DownloadFile> downloadFile = sampleSubmissionService.generateDArTFile(program.get(), submissionId);
if(downloadFile.isEmpty()) {
return HttpResponse.notFound();
}
HttpResponse<StreamedFile> response = HttpResponse
.ok(downloadFile.get().getStreamedFile())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + downloadFile.get().getFileName());
return response;
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
} catch (IOException e) {
log.error("Error generating DArT file", e);
HttpResponse response = HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error generating DArT file").contentType(MediaType.TEXT_PLAIN).body("Error generating DArT file");
return response;
}
}

@Get("/programs/{programId}/submissions/{submissionId}/lookup")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
@Produces(value={"text/csv", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/octet-stream"})
public HttpResponse<StreamedFile> generateLookupFile(@PathVariable UUID programId, @PathVariable UUID submissionId) {
try {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
return HttpResponse.notFound();
}
Optional<DownloadFile> downloadFile = sampleSubmissionService.generateLookupFile(program.get(), submissionId);
if(downloadFile.isEmpty()) {
return HttpResponse.notFound();
}
HttpResponse<StreamedFile> response = HttpResponse
.ok(downloadFile.get().getStreamedFile())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + downloadFile.get().getFileName());
return response;
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
} catch (IOException e) {
log.error("Error generating lookup file", e);
HttpResponse response = HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error generating lookup file").contentType(MediaType.TEXT_PLAIN).body("Error generating lookup file");
return response;
}
}

@Post("programs/{programId}/submissions/{submissionId}/submit")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roles = {ProgramSecuredRole.SYSTEM_ADMIN})
public HttpResponse<Response<BrAPIVendorOrderSubmission>> submitOrder(@PathVariable UUID programId, @PathVariable UUID submissionId, @QueryValue(value = "vendor") @NotBlank String vendorName) {
if(!brapiSubmissionEnabled) {
return HttpResponse.notFound();
}

try {
GenotypeVendor vendor = GenotypeVendor.fromName(vendorName);
if(vendor != null) {
Optional<Program> program = programService.getById(programId);
if (program.isEmpty()) {
return HttpResponse.notFound();
}

AuthenticatedUser actingUser = securityService.getUser();
Optional<User> user = userService.getById(actingUser.getId());
if (user.isEmpty()) {
return HttpResponse.unauthorized();
}

Optional<BrAPIVendorOrderSubmission> order = sampleSubmissionService.submitOrder(program.get(), submissionId, user.get(), vendor);
if (order.isEmpty()) {
return HttpResponse.notFound();
}

Metadata metadata = new Metadata(new Pagination(1, 1, 1, 0),
List.of(new Status(StatusCode.INFO, "Successful submission")));
Response<BrAPIVendorOrderSubmission> response = new Response<>(metadata, order.get());
return HttpResponse.ok(response);
} else {
HttpResponse response = HttpResponse.badRequest("Unrecognized vendor");
return response;
}
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}
}

@Get("programs/{programId}/submissions/{submissionId}/status")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roles = {ProgramSecuredRole.SYSTEM_ADMIN})
public HttpResponse<Response<SampleSubmission>> checkVendorStatus(@PathVariable UUID programId, @PathVariable UUID submissionId) {
if(!brapiSubmissionEnabled) {
return HttpResponse.notFound();
}

Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
log.info(String.format("programId not found: %s", programId.toString()));
return HttpResponse.notFound();
}

try {
Optional<SampleSubmission> submission = sampleSubmissionService.checkVendorStatus(program.get(), submissionId);

if(submission.isEmpty()) {
return HttpResponse.notFound();
}

Metadata metadata = new Metadata(new Pagination(1, 1, 1, 0),
List.of(new Status(StatusCode.INFO, "Successful Query")));
Response<SampleSubmission> response = new Response<>(metadata, submission.get());
return HttpResponse.ok(response);
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,21 @@ public HttpResponse<StreamedFile> datasetExport(
@QueryValue @Valid ExperimentExportQuery queryParams) {
String downloadErrorMessage = "An error occurred while generating the download file. Contact the development team at [email protected].";
try {
Program program = programService.getById(programId).orElseThrow(() -> new DoesNotExistException("Program does not exist"));
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
return HttpResponse.notFound();
}

// if a list of environmentIds are sent, return multiple files (zipped),
// else if a single environmentId is sent, return single file (CSV/Excel),
// else (if no environmentIds are sent), return a single file (CSV/Excel) including all Environments.
DownloadFile downloadFile = experimentService.exportObservations(program, experimentId, queryParams);
DownloadFile downloadFile = experimentService.exportObservations(program.get(), experimentId, queryParams);

HttpResponse<StreamedFile> response = HttpResponse
return HttpResponse
.ok(downloadFile.getStreamedFile())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + downloadFile.getFileName());
return response;
} catch (Exception e) {
log.info(e.getMessage(), e);
log.info(downloadErrorMessage, e);
HttpResponse response = HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, downloadErrorMessage).contentType(MediaType.TEXT_PLAIN).body(downloadErrorMessage);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public final class BrAPIAdditionalInfoFields {
public static final String TREATMENTS = "treatments";
public static final String GID = "gid";
public static final String ENV_YEAR = "envYear";
public static final String GERMPLASM_UUID = "germplasmId";
public static final String SAMPLE_ORGANISM = "organism";
public static final String SAMPLE_SPECIES = "species";
public static final String OBS_UNIT_ID = "obsUnitID";
public static final String GERMPLASM_NAME = "germplasmName";
public static final String SUBMISSION_NAME = "submissionName";
}
Loading
Loading