Skip to content

Commit

Permalink
MARS receipt with exception handling for isajson-biosamples
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedram-A-Keyvani committed Dec 4, 2024
1 parent 225f4dc commit f6b97a0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -78,7 +78,7 @@ public List<Study> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Study> studies, final String webinToken) {
final BiosampleAccessionsMap typeToBioSamplesAccessionMap = new BiosampleAccessionsMap();

Expand All @@ -39,7 +53,7 @@ public BiosampleAccessionsMap createBioSamples(final List<Study> studies, final
}
}

typeToBioSamplesAccessionMap.sourceAccessionsMap.keyName = Source.Fields.name;
typeToBioSamplesAccessionMap.sourceAccessionsMap.isaItemName = Source.Fields.name;
typeToBioSamplesAccessionMap.sourceAccessionsMap.accessionMap.put(
sourceBioSample.getName(),
sourceBioSample.getAccession());
Expand All @@ -52,36 +66,44 @@ public BiosampleAccessionsMap createBioSamples(final List<Study> 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<Characteristic> 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<Characteristic> 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;
Expand All @@ -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<BioSample> persistedSampleEntity = this.createSampleInBioSamples(bioSample, webinToken);
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -151,8 +173,8 @@ private BioSample createSourceBioSample(final List<Study> 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<BioSample> persistedParentSampleEntity = this.createSampleInBioSamples(sourceSample,
webinToken);
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -227,8 +249,8 @@ private EntityModel<BioSample> 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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <a href='https://github.com/elixir-europe/MARS/blob/main/repository-services/repository-api.md#response'>Repository API Specification</a>
* @param biosampleAccessionsMap {@link BiosampleAccessionsMap} Receipt from Biosample
* @param isaJson {@link IsaJson} Requested ISA-Json
*/
public void convertReceiptToMars(final BiosampleAccessionsMap biosampleAccessionsMap, final IsaJson isaJson) {
Expand Down

0 comments on commit f6b97a0

Please sign in to comment.