Skip to content
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
2 changes: 1 addition & 1 deletion repository-services/isajson-biosamples/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builder stage
FROM gradle:jdk17 AS builder
ARG MARS_REPOSITORY_LIB_REF=v0.0.4
ARG MARS_REPOSITORY_LIB_REF=v0.0.5

WORKDIR /lib
USER root
Expand Down
2 changes: 1 addition & 1 deletion repository-services/isajson-biosamples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
implementation group: 'dom4j', name: 'dom4j', version: '1.6.1'

implementation group: 'com.elixir.mars', name: 'mars-repository', version: '0.0.4'
implementation group: 'com.elixir.mars', name: 'mars-repository', version: '0.0.5'

compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BiosampleAccessionsMap createBioSamples(
IsaJsonGraphLookup.findProcessByOutputId(study.getProcessSequence(), sample.getId());
final BioSample sourceBioSample =
findSourceBioSampleForSample(
sample, sampleCollectionProcess, study.getProcessSequence(), sourceBioSamplesById);
sample, study.getProcessSequence(), sourceBioSamplesById);
final BioSample persistedChildSample =
this.createAndUpdateChildSampleWithRelationship(
sample,
Expand Down Expand Up @@ -225,22 +225,18 @@ private List<ProcessSequence> findSampleCollectionProcesses(
* Resolves the BioSamples source record for a study sample.
*
* <p>ISA exports can express source-to-sample lineage either directly through
* {@code sample.derivesFrom} or indirectly through the sample collection process input. When a
* study has a single source, that source is used as a final fallback.
* {@code sample.derivesFrom} or indirectly through the sample collection process input (see
* {@link IsaJsonGraphLookup#findMaterialOriginIds(List, Sample)}). When a study has a single
* source, that source is used as a final fallback.
*/
private BioSample findSourceBioSampleForSample(
final Sample sample,
final ProcessSequence sampleCollectionProcess,
final List<ProcessSequence> studyProcesses,
final Map<String, BioSample> sourceBioSamplesById) {
// Prefer explicit material lineage, but fall back to the process input for ISA exports that
// only carry the source-to-sample relationship in processSequence.
final Optional<String> sourceId =
findSourceIdFromSampleDerivesFrom(sample, sourceBioSamplesById)
.or(
() ->
findSourceIdFromProcessInputs(
studyProcesses, sampleCollectionProcess, sourceBioSamplesById));
IsaJsonGraphLookup.findMaterialOriginIds(studyProcesses, sample).stream()
.filter(sourceBioSamplesById::containsKey)
.findFirst();
if (sourceId.isPresent()) {
return sourceBioSamplesById.get(sourceId.get());
}
Expand All @@ -253,42 +249,6 @@ private BioSample findSourceBioSampleForSample(
"Could not resolve source BioSample for sample " + sample.getId() + ".");
}

/**
* Finds a source ID from the sample's explicit ISA material lineage.
*
* <p>The {@code derivesFrom} entries store IDs, so they are resolved against the source
* BioSamples that were already created for the current study.
*/
private Optional<String> findSourceIdFromSampleDerivesFrom(
final Sample sample, final Map<String, BioSample> sourceBioSamplesById) {
if (sample == null || sample.getDerivesFrom() == null) {
return Optional.empty();
}

return sample.getDerivesFrom().stream()
.filter(Objects::nonNull)
.map(DerivesFrom::getId)
.filter(sourceBioSamplesById::containsKey)
.findFirst();
}

/**
* Finds a source ID from the inputs of the process that produced the sample.
*
* <p>Some ISA exports keep source-to-sample lineage in {@code processSequence} instead of
* {@code sample.derivesFrom}, so this is the fallback lookup for that representation. A sample
* collection process without declared inputs is resolved through its {@code previousProcess}
* links.
*/
private Optional<String> findSourceIdFromProcessInputs(
final List<ProcessSequence> studyProcesses,
final ProcessSequence sampleCollectionProcess,
final Map<String, BioSample> sourceBioSamplesById) {
return IsaJsonGraphLookup.findProcessInputIds(studyProcesses, sampleCollectionProcess).stream()
.filter(sourceBioSamplesById::containsKey)
.findFirst();
}

/** Converts ISA characteristics to BioSamples attributes using human-readable names. */
private SortedSet<Attribute> buildAttributesFromCharacteristics(
final List<Characteristic> characteristics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public MarsReceipt convertReceiptToMars(final BiosampleAccessionsMap biosampleAc
null,
null,
null,
null,
null,
isaJson);
}

Expand Down
2 changes: 1 addition & 1 deletion repository-services/isajson-ena/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builder stage
FROM gradle:jdk17 AS builder
ARG MARS_REPOSITORY_LIB_REF=v0.0.4
ARG MARS_REPOSITORY_LIB_REF=v0.0.5

WORKDIR /lib
USER root
Expand Down
2 changes: 1 addition & 1 deletion repository-services/isajson-ena/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
implementation group: 'dom4j', name: 'dom4j', version: '1.6.1'

implementation group: 'com.elixir.mars', name: 'mars-repository', version: '0.0.4'
implementation group: 'com.elixir.mars', name: 'mars-repository', version: '0.0.5'

compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public MarsReceipt convertReceiptToMars(final Receipt receipt, final IsaJson isa
getRunAliasAccessionPairs(receipt.getRuns(), isaJson),
receipt.getMessages().getInfoMessages(),
receipt.getMessages().getErrorMessages(),
null,
null,
isaJson);
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.elixir.mars.repository.models.isa.Characteristic;
import com.elixir.mars.repository.models.isa.CharacteristicCategory;
import com.elixir.mars.repository.models.isa.DataFile;
import com.elixir.mars.repository.models.isa.DerivesFrom;
import com.elixir.mars.repository.models.isa.Materials;
import com.elixir.mars.repository.models.isa.OtherMaterial;
import com.elixir.mars.repository.models.isa.Parameter;
Expand Down Expand Up @@ -177,7 +176,7 @@ private void mapExperimentForDataFile(
experimentSequence.put(library.getId(), experimentId);

final List<OtherMaterial> materialLineage =
findMaterialLineageToSample(library, assay.getMaterials());
findMaterialLineageToSample(assay.getProcessSequence(), library, assay.getMaterials());
final ProcessSequence libraryConstructionProcess =
IsaJsonGraphLookup.findProcessByOutputId(assay.getProcessSequence(), library.getId());
final List<ProcessSequence> experimentProcesses =
Expand Down Expand Up @@ -261,27 +260,33 @@ private void addExperimentProcess(
/**
* Returns the library and upstream other materials until the chain reaches a study sample.
*
* <p>ISA material lineage is represented as {@code derivesFrom} ID references. This helper keeps
* the ENA experiment metadata close to the full library lineage while stopping before biological
* samples, which are handled through BioSamples accessions.
* <p>ISA material lineage is represented as {@code derivesFrom} ID references or, when those are
* absent, by the inputs of the process that produced each material. This helper keeps the ENA
* experiment metadata close to the full library lineage while stopping before biological samples,
* which are handled through BioSamples accessions.
*/
private List<OtherMaterial> findMaterialLineageToSample(
final OtherMaterial material, final Materials materials) {
final List<ProcessSequence> assayProcesses,
final OtherMaterial material,
final Materials materials) {
final List<OtherMaterial> materialLineage = new ArrayList<>();
final Map<String, OtherMaterial> otherMaterialsById =
IsaJsonGraphLookup.buildOtherMaterialsById(materials);

collectMaterialLineage(material, otherMaterialsById, materialLineage, new HashSet<>());
collectMaterialLineage(
assayProcesses, material, otherMaterialsById, materialLineage, new HashSet<>());

return materialLineage;
}

/**
* Recursively follows {@code derivesFrom} links between other materials.
* Recursively follows the lineage between other materials (see {@link
* IsaJsonGraphLookup#findMaterialOriginIds(List, OtherMaterial)}).
*
* <p>The visited set prevents loops in malformed ISA graphs from causing unbounded recursion.
*/
private void collectMaterialLineage(
final List<ProcessSequence> assayProcesses,
final OtherMaterial material,
final Map<String, OtherMaterial> otherMaterialsById,
final List<OtherMaterial> materialLineage,
Expand All @@ -292,17 +297,11 @@ private void collectMaterialLineage(

materialLineage.add(material);

if (material.getDerivesFrom() == null) {
return;
}

for (final DerivesFrom derivesFrom : material.getDerivesFrom()) {
if (derivesFrom == null || derivesFrom.getId() == null) {
continue;
}

for (final String originId :
IsaJsonGraphLookup.findMaterialOriginIds(assayProcesses, material)) {
collectMaterialLineage(
otherMaterialsById.get(derivesFrom.getId()),
assayProcesses,
otherMaterialsById.get(originId),
otherMaterialsById,
materialLineage,
visitedIds);
Expand Down Expand Up @@ -379,7 +378,12 @@ private String resolveSampleAccessionForLibrary(
IsaJsonGraphLookup.buildOtherMaterialsById(assay.getMaterials());

final Sample sample =
findSampleForMaterialId(library.getId(), samplesById, otherMaterialsById, new HashSet<>());
findSampleForMaterialId(
assay.getProcessSequence(),
library.getId(),
samplesById,
otherMaterialsById,
new HashSet<>());
if (sample == null) {
return getBioSampleAccessionFallback(bioSampleAccessions, null);
}
Expand Down Expand Up @@ -418,10 +422,12 @@ private String getBioSampleAccessionByKey(
* Finds the biological sample at the start of an other-material lineage.
*
* <p>Libraries and other materials can derive from other materials before eventually reaching a
* study sample. This resolves those ID links recursively so the experiment can reference the
* correct BioSamples accession.
* study sample. This resolves that lineage recursively (declared {@code derivesFrom} or, when
* absent, the inputs of the producing process) so the experiment can reference the correct
* BioSamples accession.
*/
private Sample findSampleForMaterialId(
final List<ProcessSequence> assayProcesses,
final String materialId,
final Map<String, Sample> samplesById,
final Map<String, OtherMaterial> otherMaterialsById,
Expand All @@ -435,19 +441,12 @@ private Sample findSampleForMaterialId(
return sample;
}

final OtherMaterial otherMaterial = otherMaterialsById.get(materialId);
if (otherMaterial == null || otherMaterial.getDerivesFrom() == null) {
return null;
}

for (final DerivesFrom derivesFrom : otherMaterial.getDerivesFrom()) {
if (derivesFrom == null || derivesFrom.getId() == null) {
continue;
}

for (final String originId :
IsaJsonGraphLookup.findMaterialOriginIds(
assayProcesses, otherMaterialsById.get(materialId))) {
final Sample derivedSample =
findSampleForMaterialId(
derivesFrom.getId(), samplesById, otherMaterialsById, visitedIds);
assayProcesses, originId, samplesById, otherMaterialsById, visitedIds);
if (derivedSample != null) {
return derivedSample;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ private void assertLibraryAndPlatformMetadata(final Element webinElement) {
experiment.element("PLATFORM").element("OXFORD_NANOPORE").elementText("INSTRUMENT_MODEL"));
}

@Test
void testSampleAccessionResolvesThroughProcessLineageWithoutDerivesFrom() throws Exception {
// ena-minimal declares no derivesFrom: library 333 <- library construction <- extract 332
// <- nucleic acid extraction <- sample "leaf 1" is only known from the process inputs.
final List<Study> studies = isaJson.getStudies();
final Map<String, String> bioSampleAccessions = defaultBioSampleAccessions();
bioSampleAccessions.put("leaf 1", "SAMEA-leaf-1");

final Document document = DocumentHelper.createDocument();
final Element webinElement = document.addElement("WEBIN");
experimentXmlCreator.createENAExperimentSetElement(
bioSampleAccessions, webinElement, studies, "test-process-lineage");

@SuppressWarnings("unchecked")
final List<Element> experiments = webinElement.element("EXPERIMENT_SET").elements("EXPERIMENT");
Assertions.assertEquals(1, experiments.size());
Assertions.assertEquals(
"SAMEA-leaf-1",
experiments.get(0).element("DESIGN").element("SAMPLE_DESCRIPTOR").attributeValue("accession"),
"The sample should be found through the process inputs instead of the SOURCE fallback");
}

private static Map<String, String> defaultBioSampleAccessions() {
final Map<String, String> bioSampleAccessions = new HashMap<>();
bioSampleAccessions.put("SOURCE", "SAMEA130793922");
Expand Down
Loading