From f6b97a0da0573d1b3d2df2b601bb0310aa25f8bc Mon Sep 17 00:00:00 2001 From: "Pedram A. Keyvani" Date: Wed, 4 Dec 2024 09:28:06 +0100 Subject: [PATCH] MARS receipt with exception handling for isajson-biosamples --- .../BioSampleSubmissionController.java | 4 +- .../service/BioSamplesSubmitter.java | 102 +++++++++++------- .../service/MarsReceiptService.java | 11 +- 3 files changed, 71 insertions(+), 46 deletions(-) diff --git a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java index baf4ec5..bd9e27b 100644 --- a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java +++ b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java @@ -65,7 +65,7 @@ public String performSubmissionToBioSamplesAndEna( return marsReceiptService.convertMarsReceiptToJson(); } catch (final MarsReceiptException e) { log.error("Mars receipt excption", e); - marsReceiptService.setMarsReceiptErrors(e.getReceiptErrorMessage()); + marsReceiptService.setMarsReceiptErrors(e.getError()); return marsReceiptService.convertMarsReceiptToJson(); } catch (final Exception e) { log.error("Internal server error", e); @@ -78,7 +78,7 @@ public List getStudies(final IsaJson isaJson) { try { return isaJson.getInvestigation().getStudies(); } catch (final Exception e) { - throw new MarsReceiptException("Failed to parse ISA JSON and get studies", e); + throw new MarsReceiptException(e, "Failed to parse ISA JSON and get studies"); } } } diff --git a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/BioSamplesSubmitter.java b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/BioSamplesSubmitter.java index ced6377..4872c1f 100644 --- a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/BioSamplesSubmitter.java +++ b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/BioSamplesSubmitter.java @@ -1,18 +1,14 @@ /** Elixir BioHackathon 2022 */ package com.elixir.biohackaton.ISAToSRA.biosamples.service; -import com.elixir.biohackaton.ISAToSRA.biosamples.model.Attribute; -import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap; -import com.elixir.biohackaton.ISAToSRA.biosamples.model.Relationship; -import com.elixir.biohackaton.ISAToSRA.biosamples.model.BioSample; -import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException; -import com.elixir.biohackaton.ISAToSRA.receipt.ReceiptAccessionsMap; -import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*; - import java.time.Instant; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.ParameterizedTypeReference; import org.springframework.hateoas.EntityModel; import org.springframework.http.HttpEntity; @@ -22,10 +18,28 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import com.elixir.biohackaton.ISAToSRA.biosamples.model.Attribute; +import com.elixir.biohackaton.ISAToSRA.biosamples.model.BioSample; +import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap; +import com.elixir.biohackaton.ISAToSRA.biosamples.model.Relationship; +import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException; +import com.elixir.biohackaton.ISAToSRA.receipt.ReceiptAccessionsMap; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Category; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Characteristic; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Sample; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Source; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Study; +import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Value; + +import lombok.extern.slf4j.Slf4j; + @Service @Slf4j public class BioSamplesSubmitter { + @Autowired + private MarsReceiptService marsReceiptService; + public BiosampleAccessionsMap createBioSamples(final List studies, final String webinToken) { final BiosampleAccessionsMap typeToBioSamplesAccessionMap = new BiosampleAccessionsMap(); @@ -39,7 +53,7 @@ public BiosampleAccessionsMap createBioSamples(final List studies, final } } - typeToBioSamplesAccessionMap.sourceAccessionsMap.keyName = Source.Fields.name; + typeToBioSamplesAccessionMap.sourceAccessionsMap.isaItemName = Source.Fields.name; typeToBioSamplesAccessionMap.sourceAccessionsMap.accessionMap.put( sourceBioSample.getName(), sourceBioSample.getAccession()); @@ -52,36 +66,44 @@ public BiosampleAccessionsMap createBioSamples(final List studies, final typeToBioSamplesAccessionMap.studyAccessionsMap = new ReceiptAccessionsMap( Study.Fields.title, study.getTitle()); - study .getMaterials() .getSamples() .forEach( sample -> { - final BioSample persistedChildSample = this.createAndUpdateChildSampleWithRelationship( - sample, - sourceBioSample.getAccession(), - finalSourceBioSampleOrganismAttribute.getValue(), - webinToken); - - if (persistedChildSample != null) { - final Characteristic biosampleAccessionCharacteristic = getBioSampleAccessionCharacteristic( - new AtomicReference<>(persistedChildSample)); - final ArrayList sampleCharacteristics = sample.getCharacteristics() != null - ? sample.getCharacteristics() - : new ArrayList<>(); - sampleCharacteristics.add(biosampleAccessionCharacteristic); - - typeToBioSamplesAccessionMap.sampleAccessionsMap.keyName = Sample.Fields.name; - typeToBioSamplesAccessionMap.sampleAccessionsMap.accessionMap.put( - persistedChildSample.getName(), - persistedChildSample.getAccession()); + try { + final BioSample persistedChildSample = this.createAndUpdateChildSampleWithRelationship( + sample, + sourceBioSample.getAccession(), + finalSourceBioSampleOrganismAttribute.getValue(), + webinToken); + + if (persistedChildSample != null) { + final Characteristic biosampleAccessionCharacteristic = getBioSampleAccessionCharacteristic( + new AtomicReference<>(persistedChildSample)); + final ArrayList sampleCharacteristics = sample + .getCharacteristics() != null + ? sample.getCharacteristics() + : new ArrayList<>(); + sampleCharacteristics.add(biosampleAccessionCharacteristic); + + typeToBioSamplesAccessionMap.sampleAccessionsMap.isaItemName = Sample.Fields.name; + typeToBioSamplesAccessionMap.sampleAccessionsMap.accessionMap.put( + persistedChildSample.getName(), + persistedChildSample.getAccession()); + } + } catch (Exception e) { + throw new MarsReceiptException(e, + "Failed to parse ISA Json and create samples in BioSamples (SAMPLE)", + marsReceiptService.getSampleMarsPath( + Map.entry(Study.Fields.title, study.title), + Map.entry(Sample.Fields.id, sample.id))); } }); }); } } catch (final Exception e) { - throw new MarsReceiptException("Failed to parse ISA Json and create samples in BioSamples", e); + throw new MarsReceiptException(e, "Failed to parse ISA Json and create samples in BioSamples"); } return typeToBioSamplesAccessionMap; @@ -96,8 +118,8 @@ private BioSample createAndUpdateChildSampleWithRelationship( .withRelease(Instant.now()) .withAttributes( List.of(Attribute.build("organism", parentSampleOrganism), - Attribute.build("collection date", "not provided"), - Attribute.build("geographic location (country and/or sea)", "not provided"))) + Attribute.build("collection date", "not provided"), + Attribute.build("geographic location (country and/or sea)", "not provided"))) .build(); try { final EntityModel persistedSampleEntity = this.createSampleInBioSamples(bioSample, webinToken); @@ -123,7 +145,7 @@ private BioSample createAndUpdateChildSampleWithRelationship( return null; } } catch (final Exception e) { - throw new MarsReceiptException("Failed to handle child samples", e); + throw new MarsReceiptException(e, "Failed to handle child samples"); } } @@ -151,8 +173,8 @@ private BioSample createSourceBioSample(final List studies, final String final BioSample sourceSample = new BioSample.Builder(source.getName()) .withRelease(Instant.now()) .withAttributes(List.of(organismAttribute.get(), - Attribute.build("collection date", "not provided"), - Attribute.build("geographic location (country and/or sea)", "not provided"))) + Attribute.build("collection date", "not provided"), + Attribute.build("geographic location (country and/or sea)", "not provided"))) .build(); final EntityModel persistedParentSampleEntity = this.createSampleInBioSamples(sourceSample, webinToken); @@ -205,8 +227,8 @@ private BioSample updateSampleWithRelationshipsToBioSamples( new ParameterizedTypeReference<>() { }); return biosamplesResponse.getBody().getContent(); - } catch (final Exception ex) { - throw new MarsReceiptException("Failed to add relationships to child samples", ex); + } catch (final Exception e) { + throw new MarsReceiptException(e, "Failed to add relationships to child samples"); } } @@ -227,8 +249,8 @@ private EntityModel createSampleInBioSamples( }); return biosamplesResponse.getBody(); - } catch (final Exception ex) { - throw new MarsReceiptException("Failed to create samples in BioSamples", ex); + } catch (final Exception e) { + throw new MarsReceiptException(e, "Failed to create samples in BioSamples"); } } diff --git a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/MarsReceiptService.java b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/MarsReceiptService.java index 9403eca..8c9921b 100644 --- a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/MarsReceiptService.java +++ b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/service/MarsReceiptService.java @@ -10,6 +10,7 @@ import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap; import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptProvider; import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.IsaJson; +import com.elixir.biohackaton.ISAToSRA.receipt.marsmodel.MarsError; import com.elixir.biohackaton.ISAToSRA.receipt.marsmodel.MarsErrorType; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; @@ -51,13 +52,15 @@ public void setMarsReceiptErrors(String... errors) { super.setMarsReceiptErrors(MarsErrorType.INVALID_METADATA, errors); } + public void setMarsReceiptErrors(MarsError... errors) { + super.setMarsReceiptErrors(MarsErrorType.INVALID_METADATA, errors); + } + /** * Converting BioSample receipt to Mars data format * - * @see - * https://github.com/elixir-europe/MARS/blob/refactor/repository-services/repository-api.md#response - * @param biosampleAccessionsMap {@link BiosampleAccessionsMap} Receipt from - * Biosample + * @see Repository API Specification + * @param biosampleAccessionsMap {@link BiosampleAccessionsMap} Receipt from Biosample * @param isaJson {@link IsaJson} Requested ISA-Json */ public void convertReceiptToMars(final BiosampleAccessionsMap biosampleAccessionsMap, final IsaJson isaJson) {