Skip to content

Commit

Permalink
Improve treatment data api performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kalletlak committed Jul 10, 2023
1 parent 249d4ca commit 719e5d2
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion db-scripts/src/main/resources/cgds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -755,5 +755,5 @@ CREATE TABLE `resource_study` (
);

-- THIS MUST BE KEPT IN SYNC WITH db.version PROPERTY IN pom.xml
INSERT INTO info VALUES ('2.13.0', NULL);
INSERT INTO info VALUES ('2.13.1', NULL);

6 changes: 6 additions & 0 deletions db-scripts/src/main/resources/migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,9 @@ ALTER TABLE `mutation_event` CHANGE COLUMN `ONCOTATOR_PROTEIN_POS_START` `PROTEI
ALTER TABLE `mutation_event` CHANGE COLUMN `ONCOTATOR_PROTEIN_POS_END` `PROTEIN_POS_END` int(11);
UPDATE `info` SET `DB_SCHEMA_VERSION`="2.13.0";


##version: 2.13.1
ALTER TABLE `clinical_event_data` MODIFY COLUMN `VALUE` varchar(3000) NOT NULL;
CREATE INDEX idx_clinical_event_key ON clinical_event_data (`KEY`);
CREATE INDEX idx_clinical_event_value ON clinical_event_data (`VALUE`);
UPDATE `info` SET `DB_SCHEMA_VERSION`="2.13.1";
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public interface TreatmentRepository {
public Set<String> getAllUniqueTreatments(List<String> sampleIds, List<String> studyIds, ClinicalEventKeyCode key);

@Cacheable(cacheResolver = "generalRepositoryCacheResolver", condition = "@cacheEnabledConfig.getEnabled()")
public Integer getTreatmentCount(List<String> studies, String key);
public Boolean hasTreatmentData(List<String> studies, String key);

@Cacheable(cacheResolver = "generalRepositoryCacheResolver", condition = "@cacheEnabledConfig.getEnabled()")
public Integer getSampleCount(List<String> studies);
public Boolean hasSampleTimelineData(List<String> studies);

@Cacheable(cacheResolver = "generalRepositoryCacheResolver", condition = "@cacheEnabledConfig.getEnabled()")
public Boolean studyIdHasTreatments(String studyId, ClinicalEventKeyCode key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface TreatmentMapper {

Set<String> getAllUniqueTreatments(List<String> sampleIds, List<String> studyIds, String key);

Integer getTreatmentCount(List<String> sampleIds, List<String> studyIds, String key);
Boolean hasTreatmentData(List<String> sampleIds, List<String> studyIds, String key);

Integer getSampleCount(List<String> sampleIds, List<String> studyIds);
Boolean hasSampleTimelineData(List<String> sampleIds, List<String> studyIds);

Boolean studyIdHasTreatments(String studyId, String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public Set<String> getAllUniqueTreatments(List<String> sampleIds, List<String> s
}

@Override
public Integer getTreatmentCount(List<String> studies, String key) {
return treatmentMapper.getTreatmentCount(null, studies, key);
public Boolean hasTreatmentData(List<String> studies, String key) {
return treatmentMapper.hasTreatmentData(null, studies, key);
}

@Override
public Integer getSampleCount(List<String> studies) {
return treatmentMapper.getSampleCount(null, studies);
public Boolean hasSampleTimelineData(List<String> studies) {
return treatmentMapper.hasSampleTimelineData(null, studies);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,23 @@
AND clinical_event_data.KEY = #{key}
</select>

<select id="getTreatmentCount" resultType="java.lang.Integer">
SELECT
COUNT(clinical_event.PATIENT_ID)
<select id="hasTreatmentData" resultType="java.lang.Boolean">
SELECT EXISTS(SELECT
*
FROM
clinical_event
INNER JOIN clinical_event_data ON clinical_event.CLINICAL_EVENT_ID = clinical_event_data.CLINICAL_EVENT_ID
INNER JOIN patient ON clinical_event.PATIENT_ID = patient.INTERNAL_ID
INNER JOIN sample ON patient.INTERNAL_ID = sample.PATIENT_ID
INNER JOIN cancer_study ON patient.CANCER_STUDY_ID = cancer_study.CANCER_STUDY_ID
<include refid="where"/>
AND clinical_event_data.KEY = #{key}
AND clinical_event_data.KEY = #{key} LIMIT 1
)
</select>

<select id="getSampleCount" resultType="java.lang.Integer">
SELECT
COUNT(sample.STABLE_ID)
<select id="hasSampleTimelineData" resultType="java.lang.Boolean">
SELECT EXISTS(SELECT
*
FROM
clinical_event
INNER JOIN clinical_event_data ON clinical_event.CLINICAL_EVENT_ID = clinical_event_data.CLINICAL_EVENT_ID
Expand All @@ -85,20 +86,21 @@
INNER JOIN cancer_study ON patient.CANCER_STUDY_ID = cancer_study.CANCER_STUDY_ID
<include refid="where"/>
AND clinical_event_data.KEY = 'SAMPLE_ID'
AND (clinical_event.EVENT_TYPE LIKE 'Sample Acquisition' OR clinical_event.EVENT_TYPE LIKE 'SPECIMEN')
AND (clinical_event.EVENT_TYPE LIKE 'Sample Acquisition' OR clinical_event.EVENT_TYPE LIKE 'SPECIMEN') LIMIT 1)
</select>


<select id="studyIdHasTreatments" resultType="java.lang.Boolean">
SELECT
count(cancer_study.CANCER_STUDY_IDENTIFIER) > 0
SELECT EXISTS(SELECT
*
FROM
clinical_event
INNER JOIN clinical_event_data ON clinical_event.CLINICAL_EVENT_ID = clinical_event_data.CLINICAL_EVENT_ID
INNER JOIN patient ON clinical_event.PATIENT_ID = patient.INTERNAL_ID
INNER JOIN cancer_study ON patient.CANCER_STUDY_ID = cancer_study.CANCER_STUDY_ID
WHERE
clinical_event_data.KEY = #{key}
AND cancer_study.CANCER_STUDY_IDENTIFIER = #{studyId}
AND cancer_study.CANCER_STUDY_IDENTIFIER = #{studyId} LIMIT 1)
</select>

<sql id="where">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,17 @@ public void getAllUniqueTreatments() {
}

@Test
public void getTreatmentCount() {
Integer expected = 1;
Integer actual = treatmentRepository.getTreatmentCount(Collections.singletonList("study_tcga_pub"), ClinicalEventKeyCode.Agent.getKey());
public void hasTreatmentData() {
Boolean actual = treatmentRepository.hasTreatmentData(Collections.singletonList("study_tcga_pub"), ClinicalEventKeyCode.Agent.getKey());

Assert.assertEquals(actual, expected);
Assert.assertEquals(actual, true);
}

@Test
public void getSampleCount() {
Integer expected = 2;
Integer actual = treatmentRepository.getSampleCount(Collections.singletonList("study_tcga_pub"));
Boolean actual = treatmentRepository.hasSampleTimelineData(Collections.singletonList("study_tcga_pub"));

Assert.assertEquals(actual, expected);
Assert.assertEquals(actual, true);
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
<tomcat.session.timeout>720</tomcat.session.timeout>

<!-- THIS SHOULD BE KEPT IN SYNC TO VERSION IN CGDS.SQL -->
<db.version>2.13.0</db.version>
<db.version>2.13.1</db.version>
</properties>

<modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.*;

@Service
public class TreatmentServiceImpl implements TreatmentService {
Expand Down Expand Up @@ -40,16 +40,16 @@ private Pair<List<String>, List<String>> filterIds(List<String> sampleIds, List<
}
return new ImmutablePair<>(filteredSampleIds, filteredStudyIds);
}

@Override
public List<SampleTreatmentRow> getAllSampleTreatmentRows(List<String> sampleIds, List<String> studyIds, ClinicalEventKeyCode key) {
Pair<List<String>, List<String>> filteredIds = filterIds(sampleIds, studyIds, key);
sampleIds = filteredIds.getLeft();
studyIds = filteredIds.getRight();

Map<String, List<ClinicalEventSample>> samplesByPatient =
Map<String, List<ClinicalEventSample>> samplesByPatient =
treatmentRepository.getSamplesByPatientId(sampleIds, studyIds);
Map<String, List<Treatment>> treatmentsByPatient =
Map<String, List<Treatment>> treatmentsByPatient =
treatmentRepository.getTreatmentsByPatientId(sampleIds, studyIds, key);

Stream<SampleTreatmentRow> rows = samplesByPatient.keySet().stream()
Expand Down Expand Up @@ -150,16 +150,15 @@ Stream<SampleTreatmentRow> toRows() {
);
}
}

@Override
public List<PatientTreatmentRow> getAllPatientTreatmentRows(
List<String> sampleIds, List<String> studyIds, ClinicalEventKeyCode key
) {
Pair<List<String>, List<String>> filteredIds = filterIds(sampleIds, studyIds, key);
sampleIds = filteredIds.getLeft();
studyIds = filteredIds.getRight();
Map<String, List<Treatment>> treatmentsByPatient =

Map<String, List<Treatment>> treatmentsByPatient =
treatmentRepository.getTreatmentsByPatientId(sampleIds, studyIds, key);
Map<String, List<ClinicalEventSample>> samplesByPatient = treatmentRepository
.getShallowSamplesByPatientId(sampleIds, studyIds)
Expand Down Expand Up @@ -204,17 +203,17 @@ private List<Map.Entry<String, List<Treatment>>> matchingPatients(

@Override
public Boolean containsTreatmentData(List<String> studies, ClinicalEventKeyCode key) {
return treatmentRepository.getTreatmentCount(studies, key.getKey()) > 0;
return treatmentRepository.hasTreatmentData(studies, key.getKey());
}

@Override
public Boolean containsSampleTreatmentData(List<String> studyIds, ClinicalEventKeyCode key) {
studyIds = studyIds.stream()
.filter(studyId -> treatmentRepository.studyIdHasTreatments(studyId, key))
.collect(Collectors.toList());
Integer sampleCount = treatmentRepository.getSampleCount(studyIds);
Integer treatmentCount = treatmentRepository.getTreatmentCount(studyIds, key.getKey());

return sampleCount * treatmentCount > 0;
if(studyIds.size() > 0 && treatmentRepository.hasSampleTimelineData(studyIds)) {
return treatmentRepository.hasTreatmentData(studyIds, key.getKey());
}
return false;
}
}

0 comments on commit 719e5d2

Please sign in to comment.