Skip to content

Commit

Permalink
[Chore] Bulk - Fix experiment design's memory issue (#203)
Browse files Browse the repository at this point in the history
* Remove experiment design field from Experiment

Temporary leave experimentDesign field on Experiment entity to pass the test. I am going to remove it in the last commit

* Refactor ExperimentsConditionsDetailsController

* Refactor BaselineExperimentAssayGroupsLines

* Refactor BaselineExperimentPageService

* Refactor HeatmapGroupingsService

* Refactor DifferentialExperimentPageService

* Refactor EvidenceService

* Refactor EvidenceService - left over

* Refactor ContrastSummaryController

* Refactor ExperimentPageContentService

* update with the latest atlas-web-core module changes

* Remove experimentDesign field from Experiment entity (latest core changes)

* Update to the latest atlas-web-core module changes

* Refactor getExperimentDesign method usage
  • Loading branch information
ke4 authored Jun 26, 2024
1 parent 81671de commit c21eeba
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ public String getDifferentialJsonResults(
SetMultimap<String, String> factorValuesByType = HashMultimap.create();
for (DiffAnalytics dbe : diffAnalyticsList) {
AssayGroup testAssayGroup = dbe.getContrastTestAssayGroup();
Experiment experiment = experimentTrader.getPublicExperiment(dbe.getExperimentAccession());

FactorSet factorsForAssayGroup =
FactorSet.create(experiment.getExperimentDesign()
FactorSet.create(experimentTrader.getExperimentDesign(dbe.getExperimentAccession())
.getFactorValues(testAssayGroup.getFirstAssayId()));
for (Factor factor : factorsForAssayGroup) {
factorValuesByType.put(factor.getType(), factor.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import uk.ac.ebi.atlas.model.experiment.ExperimentDesign;
import uk.ac.ebi.atlas.model.experiment.sample.AssayGroup;
import uk.ac.ebi.atlas.model.OntologyTerm;
import uk.ac.ebi.atlas.model.experiment.sdrf.FactorSet;
import uk.ac.ebi.atlas.model.experiment.sdrf.SampleCharacteristic;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.model.experiment.sdrf.Factor;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -18,9 +21,12 @@
public class BaselineExperimentAssayGroupsLines implements Iterable<String[]> {
private final LinkedHashSet<ImmutableList<String>> result = new LinkedHashSet<>();
private final LinkedHashSet<ImmutableList<String>> assayGroupsDetails;
private final ExperimentDesign experimentDesign;

public BaselineExperimentAssayGroupsLines(BaselineExperiment experiment) {
assayGroupsDetails = buildAssayGroupsDetails(experiment);
public BaselineExperimentAssayGroupsLines(BaselineExperiment experiment,
ExperimentDesign experimentDesign) {
this.experimentDesign = experimentDesign;
this.assayGroupsDetails = buildAssayGroupsDetails(experiment);
}

private LinkedHashSet<ImmutableList<String>> buildAssayGroupsDetails(BaselineExperiment experiment) {
Expand All @@ -35,7 +41,9 @@ private LinkedHashSet<ImmutableList<String>> buildAssayGroupsDetails(BaselineExp
}

private void populateSamples(BaselineExperiment experiment, String assayAccession, AssayGroup assayGroup) {
for (SampleCharacteristic sample : experiment.getExperimentDesign().getSampleCharacteristics(assayAccession)) {
final String experimentAccession = experiment.getAccession();

for (SampleCharacteristic sample : getSampleCharacteristics(assayAccession, experimentAccession)) {
ImmutableList<String> line =
ImmutableList.of(
experiment.getAccession(),
Expand All @@ -49,16 +57,26 @@ private void populateSamples(BaselineExperiment experiment, String assayAccessio
}

private void populateFactors(BaselineExperiment experiment, String assayAccession, AssayGroup assayGroup) {
FactorSet factorSet = experiment.getExperimentDesign().getFactors(assayAccession);
final String experimentAccession = experiment.getAccession();

FactorSet factorSet = getFactors(assayAccession);
if (factorSet != null) {
for (Factor factor : factorSet) {
ImmutableList<String> line = ImmutableList.of(experiment.getAccession(), assayGroup.getId(), "factor",
ImmutableList<String> line = ImmutableList.of(experimentAccession, assayGroup.getId(), "factor",
factor.getHeader(), factor.getValue(), joinURIs(factor.getValueOntologyTerms()));
result.add(line);
}
}
}

private Collection<SampleCharacteristic> getSampleCharacteristics(String assayAccession, String experimentAccession) {
return experimentDesign.getSampleCharacteristics(assayAccession);
}

private @Nullable FactorSet getFactors(String assayAccession) {
return experimentDesign.getFactors(assayAccession);
}

private static String joinURIs(Set<OntologyTerm> ontologyTerms) {
return ontologyTerms.stream().map(OntologyTerm::uri).collect(joining(" "));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;
import uk.ac.ebi.atlas.model.OntologyTerm;
import uk.ac.ebi.atlas.model.experiment.ExperimentDesign;
import uk.ac.ebi.atlas.model.experiment.sdrf.FactorSet;
import uk.ac.ebi.atlas.model.experiment.sdrf.SampleCharacteristic;
import uk.ac.ebi.atlas.model.experiment.sdrf.Factor;
Expand All @@ -18,9 +19,11 @@
public class DifferentialExperimentContrastLines implements Iterable<String[]> {
private final LinkedHashSet<ImmutableList<String>> contrastDetails;
private final LinkedHashSet<ImmutableList<String>> result = new LinkedHashSet<>();
private final ExperimentDesign experimentDesign;

public DifferentialExperimentContrastLines(DifferentialExperiment experiment) {
public DifferentialExperimentContrastLines(DifferentialExperiment experiment, ExperimentDesign experimentDesign) {
this.contrastDetails = buildContrastDetails(experiment);
this.experimentDesign = experimentDesign;
}

private LinkedHashSet<ImmutableList<String>> buildContrastDetails(DifferentialExperiment experiment) {
Expand All @@ -43,10 +46,11 @@ private void populateSamples(DifferentialExperiment experiment,
String assayAccession,
Contrast contrast,
String value) {
for (SampleCharacteristic sample : experiment.getExperimentDesign().getSampleCharacteristics(assayAccession)) {
final String experimentAccession = experiment.getAccession();
for (SampleCharacteristic sample : experimentDesign.getSampleCharacteristics(assayAccession)) {
ImmutableList<String> line =
ImmutableList.of(
experiment.getAccession(),
experimentAccession,
contrast.getId(),
value,
"characteristic",
Expand All @@ -61,12 +65,13 @@ private void populateFactors(DifferentialExperiment experiment,
String assayAccession,
Contrast contrast,
String value) {
FactorSet factorSet = experiment.getExperimentDesign().getFactors(assayAccession);
final String experimentAccession = experiment.getAccession();
FactorSet factorSet = experimentDesign.getFactors(assayAccession);
if (factorSet != null) {
for (Factor factor : factorSet) {
ImmutableList<String> line =
ImmutableList.of(
experiment.getAccession(),
experimentAccession,
contrast.getId(),
value,
"factor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import uk.ac.ebi.atlas.model.experiment.Experiment;
import uk.ac.ebi.atlas.model.experiment.ExperimentDesign;
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.model.experiment.differential.DifferentialExperiment;
Expand All @@ -26,7 +27,7 @@
@Controller
@Scope("request")
public class ExperimentsConditionsDetailsController {
private ExperimentTrader experimentTrader;
private final ExperimentTrader experimentTrader;

public ExperimentsConditionsDetailsController(ExperimentTrader experimentTrader) {
this.experimentTrader = experimentTrader;
Expand All @@ -36,7 +37,8 @@ public ExperimentsConditionsDetailsController(ExperimentTrader experimentTrader)
public void generateTsvFormatBaseline(HttpServletResponse response) {
writeTsvLinesToResponse(
response,
experiment -> new BaselineExperimentAssayGroupsLines((BaselineExperiment) experiment),
experiment -> new BaselineExperimentAssayGroupsLines(
(BaselineExperiment) experiment, getExperimentDesign(experiment.getAccession())),
RNASEQ_MRNA_BASELINE,
PROTEOMICS_BASELINE,
PROTEOMICS_BASELINE_DIA);
Expand All @@ -46,13 +48,18 @@ public void generateTsvFormatBaseline(HttpServletResponse response) {
public void generateTsvFormatDifferential(HttpServletResponse response) {
writeTsvLinesToResponse(
response,
experiment -> new DifferentialExperimentContrastLines((DifferentialExperiment) experiment),
experiment -> new DifferentialExperimentContrastLines(
(DifferentialExperiment) experiment, getExperimentDesign(experiment.getAccession())),
MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL,
MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL,
MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL,
RNASEQ_MRNA_DIFFERENTIAL);
}

private ExperimentDesign getExperimentDesign(String experimentAccession) {
return experimentTrader.getExperimentDesign(experimentAccession);
}

private void writeTsvLinesToResponse(HttpServletResponse response,
Function<Experiment, Iterable<String[]>> linesIteratorProducer,
ExperimentType... experimentTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uk.ac.ebi.atlas.experimentpage.baseline.profiles.BaselineExperimentProfilesListSerializer;
import uk.ac.ebi.atlas.experimentpage.baseline.profiles.BaselineExperimentProfilesService;
import uk.ac.ebi.atlas.experimentpage.context.BaselineRequestContext;
import uk.ac.ebi.atlas.model.experiment.ExperimentDesign;
import uk.ac.ebi.atlas.model.experiment.sample.AssayGroup;
import uk.ac.ebi.atlas.model.ExpressionUnit;
import uk.ac.ebi.atlas.model.GeneProfilesList;
Expand Down Expand Up @@ -38,12 +39,13 @@ public BaselineExperimentPageService(BaselineExperimentProfilesService baselineE
}

public <U extends ExpressionUnit.Absolute> JsonObject getResultsForExperiment(
BaselineExperiment experiment, String accessKey, BaselineRequestPreferences<U> preferences) {
BaselineExperiment experiment, ExperimentDesign experimentDesign, String accessKey,
BaselineRequestPreferences<U> preferences) {

BaselineRequestContext<U> requestContext = new BaselineRequestContext<>(preferences, experiment);

JsonObject result = new JsonObject();
result.add("columnHeaders", constructColumnHeaders(requestContext, experiment));
result.add("columnHeaders", constructColumnHeaders(requestContext, experiment, experimentDesign));
result.add("columnGroupings", new JsonArray());

GeneProfilesList<BaselineProfile> baselineProfilesList = fetchProfiles(experiment, preferences);
Expand Down Expand Up @@ -88,7 +90,8 @@ private GeneProfilesList<BaselineProfile> fetchProfiles(BaselineExperiment exper
return baselineProfilesList;
}

private JsonArray constructColumnHeaders(BaselineRequestContext<?> requestContext, BaselineExperiment experiment) {
private JsonArray constructColumnHeaders(BaselineRequestContext<?> requestContext, BaselineExperiment experiment,
ExperimentDesign experimentDesign) {
JsonArray result = new JsonArray();

for (AssayGroup dataColumnDescriptor : requestContext.getDataColumnsToReturn()) {
Expand All @@ -101,7 +104,7 @@ private JsonArray constructColumnHeaders(BaselineRequestContext<?> requestContex
o.add("assayGroupSummary",
new AssayGroupSummaryBuilder()
.forAssayGroup(experiment.getDataColumnDescriptor(dataColumnDescriptor.getId()))
.withExperimentDesign(experiment.getExperimentDesign())
.withExperimentDesign(experimentDesign)
.build().toJson());
result.add(o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.model.experiment.sample.ReportsGeneExpression;
import uk.ac.ebi.atlas.resource.DataFileHub;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.utils.GsonProvider;

import java.util.List;
Expand All @@ -39,10 +40,14 @@ public class ExperimentPageContentService {

private final DataFileHub dataFileHub;
private final ExpressionAtlasContentService expressionAtlasContentService;
private final ExperimentTrader experimentTrader;

public ExperimentPageContentService(DataFileHub dataFileHub, ExpressionAtlasContentService expressionAtlasContentService) {
public ExperimentPageContentService(DataFileHub dataFileHub,
ExpressionAtlasContentService expressionAtlasContentService,
ExperimentTrader experimentTrader) {
this.dataFileHub = dataFileHub;
this.expressionAtlasContentService = expressionAtlasContentService;
this.experimentTrader = experimentTrader;
}

@Cacheable(cacheNames = "experimentContent", key = "#experiment.getAccession()")
Expand All @@ -66,7 +71,8 @@ private JsonObject experimentPageContentForExperiment(final Experiment<? extends
// everything wants to have a heatmap
availableTabs.add(
heatmapTab(
GSON.toJsonTree(getExperimentVariablesAsHeatmapFilterGroups(experiment)).getAsJsonArray(),
GSON.toJsonTree(getExperimentVariablesAsHeatmapFilterGroups(experiment,
experimentTrader.getExperimentDesign(experiment.getAccession()))).getAsJsonArray(),
JsonBaselineExperimentController.geneDistributionUrl(
experiment.getAccession(),
accessKey,
Expand All @@ -90,7 +96,7 @@ private JsonObject experimentPageContentForExperiment(final Experiment<? extends

if (dataFileHub.getExperimentFiles(experiment.getAccession()).experimentDesign.exists()) {
availableTabs.add(
experimentDesignTab(new ExperimentDesignTable(experiment).asJson(),
experimentDesignTab(new ExperimentDesignTable(experimentTrader, experiment).asJson(),
ExperimentDesignFile.makeUrl(experiment.getAccession(), accessKey)));
}

Expand Down
Loading

0 comments on commit c21eeba

Please sign in to comment.