Skip to content

Commit

Permalink
optimise endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsPon committed Dec 11, 2024
1 parent 2b2868c commit bdc3c8f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
5 changes: 5 additions & 0 deletions dev/add_single_cell_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ CREATE TABLE IF NOT EXISTS single_cell_expression (
FOREIGN KEY(SAMPLE_ID) REFERENCES sample(INTERNAL_ID) ON DELETE CASCADE,
FOREIGN KEY(ENTREZ_GENE_ID) REFERENCES gene(ENTREZ_GENE_ID) ON DELETE CASCADE
);

CREATE INDEX single_cell_sortby ON single_cell_expression (GENETIC_PROFILE_ID, SAMPLE_ID, TISSUE, CELL_TYPE);
CREATE INDEX sample_index ON single_cell_expression (GENETIC_PROFILE_ID, SAMPLE_ID);
CREATE INDEX entrez_index ON single_cell_expression (ENTREZ_GENE_ID);

5 changes: 5 additions & 0 deletions dev/create_sc_expression_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
FOREIGN KEY(SAMPLE_ID) REFERENCES sample(INTERNAL_ID),
FOREIGN KEY(ENTREZ_GENE_ID) REFERENCES gene(ENTREZ_GENE_ID)
);
CREATE INDEX single_cell_sortby ON single_cell_expression (GENETIC_PROFILE_ID, SAMPLE_ID, TISSUE, CELL_TYPE);
CREATE INDEX sample_index ON single_cell_expression (GENETIC_PROFILE_ID, SAMPLE_ID);
CREATE INDEX entrez_index ON single_cell_expression (ENTREZ_GENE_ID);
"""

add_genetic_profile = f"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cbioportal.model;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -26,8 +28,8 @@ public SingleCellExpressionGroupedBySample(String sampleId, List<SingleCellExpre
this.patientId = singleCellExpressions.get(0).getPatientId();
this.studyId = singleCellExpressions.get(0).getStudyId();

Map<String, List<SingleCellExpression>> tissueMap = singleCellExpressions.stream()
.collect(Collectors.groupingBy(SingleCellExpression::getTissue));
Map<String, List<SingleCellExpression>> tissueMap = SingleCellExpressionGroupedBySample
.createSingleCellExpressionGroupedMap(singleCellExpressions, "tissue");
this.singleCellExpressionGroupedByTissues = tissueMap.entrySet().stream()
.map(e -> new SingleCellExpressionGroupedByTissue(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Expand Down Expand Up @@ -75,8 +77,9 @@ public SingleCellExpressionGroupedByTissue(String tissue,
List<SingleCellExpression> singleCellExpressions) {
this.tissue = tissue;

Map<String, List<SingleCellExpression>> cellTypeMap = singleCellExpressions.stream()
.collect(Collectors.groupingBy(SingleCellExpression::getCellType));
Map<String, List<SingleCellExpression>> cellTypeMap = SingleCellExpressionGroupedBySample
.createSingleCellExpressionGroupedMap(singleCellExpressions, "cellType");

this.singleCellExpressionGroupedByCellTypes = cellTypeMap.entrySet().stream()
.map(e -> new SingleCellExpressionGroupedByCellType(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Expand Down Expand Up @@ -147,4 +150,43 @@ public void setExpressionValue(Float expressionValue) {
}
}
}
}

public static Map<String, List<SingleCellExpression>> createSingleCellExpressionGroupedMap(List<SingleCellExpression> singleCellExpressionData, String sortBy) {
// singleCellExpressionData NEEDS TO BE SORTED in order for this to work correctly.
// This is done this way in order to improve efficiency. Sorting is done in the DB query,
// and indices on the relevant columns increase query speed
Map<String, List<SingleCellExpression>> singleCellExpressionGroupedBy = new LinkedHashMap<>();
String currentValue = "";
String forLoopValue = "";
List<SingleCellExpression> singleCellExpressionList = new LinkedList<>();
for (SingleCellExpression singleCellExpression : singleCellExpressionData) {
switch (sortBy) {
case "sampleId":
forLoopValue = singleCellExpression.getSampleId();
break;
case "tissue":
forLoopValue = singleCellExpression.getTissue();
break;
case "cellType":
forLoopValue = singleCellExpression.getCellType();
break;
default:
throw new RuntimeException("sortBy option " + sortBy + " not supported.");
}
if (currentValue.equals("")) {
currentValue = forLoopValue;
singleCellExpressionList = new LinkedList<>();
}
if (!currentValue.equals(forLoopValue)) {
singleCellExpressionGroupedBy.put(currentValue, singleCellExpressionList);
currentValue = forLoopValue;
singleCellExpressionList = new LinkedList<>();
}
singleCellExpressionList.add(singleCellExpression);
}
if (!currentValue.equals("")) {
singleCellExpressionGroupedBy.put(currentValue, singleCellExpressionList);
}
return singleCellExpressionGroupedBy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.cbioportal.model.SingleCellExpression;
import org.cbioportal.model.SingleCellExpressionGroupedBySample;
import org.cbioportal.persistence.SingleCellExpressionRepository;
Expand Down Expand Up @@ -37,14 +36,14 @@ public List<SingleCellExpressionGroupedBySample> fetchSingleCellExpressionInMole
throws MolecularProfileNotFoundException {

List<SingleCellExpression> singleCellExpressionData = fetchSingleCellExpressionInMolecularProfile(
molecularProfileId, sampleListId, entrezGeneIds, pageSize, pageNumber, "sampleId", "ASC");
molecularProfileId, sampleListId, entrezGeneIds, pageSize, pageNumber, "sampleId ASC, tissue ASC, cellType", "ASC");

Map<String, List<SingleCellExpression>> singleCellExpressionGroupedBySamples = singleCellExpressionData.stream()
.collect(Collectors.groupingBy(SingleCellExpression::getSampleId));
Map<String, List<SingleCellExpression>> singleCellExpressionGroupedBySamples =
SingleCellExpressionGroupedBySample.createSingleCellExpressionGroupedMap(singleCellExpressionData, "sampleId");

return singleCellExpressionGroupedBySamples.entrySet().stream()
.map(e -> new SingleCellExpressionGroupedBySample(e.getKey(), e.getValue()))
.collect(Collectors.toList());
}

}
}

0 comments on commit bdc3c8f

Please sign in to comment.