Skip to content

Commit

Permalink
Refactor patient and sample removal code
Browse files Browse the repository at this point in the history
  • Loading branch information
forus committed Jun 27, 2024
1 parent 5753d1d commit bd54ed2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 81 deletions.
55 changes: 0 additions & 55 deletions src/main/java/org/mskcc/cbio/portal/dao/DaoGeneticAlteration.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,59 +493,4 @@ public void deleteAllRecords() throws DaoException {
JdbcUtil.closeAll(DaoGeneticAlteration.class, con, pstmt, rs);
}
}

/**
* Removes sample in genetic alterations' data for a study
* @param internalStudyId - internal id of study to remove samples in genetic alterations data
* @param internalSampleIdsToRemove - internal ids of samples to remove
* @throws DaoException
*/
public void removeSamplesInGeneticAlterationsForStudy(int internalStudyId, Set<Integer> internalSampleIdsToRemove) throws DaoException {
List<GeneticProfile> geneticProfiles = DaoGeneticProfile.getAllGeneticProfiles(internalStudyId);
for (GeneticProfile geneticProfile : geneticProfiles) {
Set<Integer> removedInternalSampleIds = removeSamplesInGeneticAlterationsForGeneticProfile(geneticProfile, internalSampleIdsToRemove);
log.debug("Genetic alterations data for {} sample ids ouf of {} requested have been removed for genetic profile with stable id={}",
removedInternalSampleIds, internalSampleIdsToRemove, geneticProfile.getStableId());
}
}

/**
* Removes sample in genetic alterations' data for a genetic profile
* @param geneticProfile - genetic profile to remove samples in genetic alteration data
* @param internalSampleIdsToRemove - internal ids of samples to remove
* @return set of sample internal ids that were actually removed
* @throws DaoException
*/
public Set<Integer> removeSamplesInGeneticAlterationsForGeneticProfile(GeneticProfile geneticProfile, Set<Integer> internalSampleIdsToRemove) throws DaoException {
int geneticProfileId = geneticProfile.getGeneticProfileId();
List<Integer> orderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(geneticProfileId);
Set<Integer> actualInternalSampleIdsToRemove = orderedSampleList.stream()
.filter(internalSampleIdsToRemove::contains).collect(Collectors.toUnmodifiableSet());
if (!actualInternalSampleIdsToRemove.isEmpty()) {
if (GeneticAlterationType.GENESET_SCORE.equals(geneticProfile.getGeneticAlterationType())) {
List<String> sampleStableIds = actualInternalSampleIdsToRemove.stream()
.map(internalSampleID ->
DaoSample.getSampleById(internalSampleID).getStableId())
.toList();
throw new RuntimeException("Sample(s) with stable id "
+ String.join(", ", sampleStableIds)
+ " can't be removed as it contains GSVA data." +
" Consider dropping and re-uploading the whole study.");
}
orderedSampleList.removeAll(actualInternalSampleIdsToRemove);
HashMap<Integer, HashMap<Integer, String>> geneticAlterationMapForEntityIds = DaoGeneticAlteration.getInstance().getGeneticAlterationMapForEntityIds(geneticProfileId, null);
DaoGeneticAlteration.getInstance().deleteAllRecordsInGeneticProfile(geneticProfileId);
if (!orderedSampleList.isEmpty()) {
for (Map.Entry<Integer, HashMap<Integer, String>> entry : geneticAlterationMapForEntityIds.entrySet()) {
String[] values = orderedSampleList.stream().map(isid -> entry.getValue().get(isid)).toArray(String[]::new);
DaoGeneticAlteration.getInstance().addGeneticAlterationsForGeneticEntity(geneticProfileId, entry.getKey(), values);
}
}
DaoGeneticProfileSamples.deleteAllSamplesInGeneticProfile(geneticProfileId);
if (!orderedSampleList.isEmpty()) {
DaoGeneticProfileSamples.addGeneticProfileSamples(geneticProfileId, orderedSampleList);
}
}
return actualInternalSampleIdsToRemove;
}
}
32 changes: 18 additions & 14 deletions src/main/java/org/mskcc/cbio/portal/dao/DaoPatient.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,26 @@ private static Patient extractPatient(ResultSet rs) throws SQLException
}
}

public static void removePatientsEverywhereInStudy(int internalStudyId, Set<String> patientStableIds) throws DaoException {
log.info("Removing {} patients from study with internal id={} ...", patientStableIds, internalStudyId);
Set<Integer> internalPatientIds = findInternalPatientIdsInStudy(internalStudyId, patientStableIds);
for (Integer internalPatientId : internalPatientIds) {
Set<Integer> patientInternalSampleIds = DaoSample.getSamplesByPatientId(internalPatientId).stream()
.map(Sample::getInternalId).collect(Collectors.toSet());
DaoGeneticAlteration.getInstance().removeSamplesInGeneticAlterationsForStudy(internalStudyId, patientInternalSampleIds);
}
DaoPatient.deletePatients(internalPatientIds);
log.info("Removing {} patients from study with internal id={} done.", patientStableIds, internalStudyId);
}

public static void deletePatients(Collection<Integer> internalPatientIds) throws DaoException
/**
* Removes patients information from the study
* @param internalStudyId - id of the study that contains the patients
* @param patientStableIds - patient stable ids to remove
* @throws DaoException
*/
public static void deletePatients(int internalStudyId, Set<String> patientStableIds) throws DaoException
{
if (internalPatientIds == null || internalPatientIds.isEmpty()) {
if (patientStableIds == null || patientStableIds.isEmpty()) {
log.info("No patients specified to remove for study with internal id={}. Skipping.", internalStudyId);
return;
}
log.info("Removing {} patients from study with internal id={} ...", patientStableIds, internalStudyId);

Set<Integer> internalPatientIds = findInternalPatientIdsInStudy(internalStudyId, patientStableIds);
Set<String> patientsSampleStableIds = internalPatientIds.stream().flatMap(internalPatientId ->
DaoSample.getSamplesByPatientId(internalPatientId).stream().map(Sample::getStableId))
.collect(Collectors.toSet());
DaoSample.deleteSamples(internalStudyId, patientsSampleStableIds);

Connection con = null;
PreparedStatement pstmt = null;
try {
Expand All @@ -257,6 +260,7 @@ public static void deletePatients(Collection<Integer> internalPatientIds) throws
finally {
JdbcUtil.closeAll(DaoPatient.class, con, pstmt, null);
}
log.info("Removing {} patients from study with internal id={} done.", patientStableIds, internalStudyId);
}

public static Set<Integer> findInternalPatientIdsInStudy(Integer internalStudyId, Set<String> patientStableIds) {
Expand Down
81 changes: 73 additions & 8 deletions src/main/java/org/mskcc/cbio/portal/dao/DaoSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package org.mskcc.cbio.portal.dao;

import org.mskcc.cbio.portal.model.GeneticAlterationType;
import org.mskcc.cbio.portal.model.GeneticProfile;
import org.mskcc.cbio.portal.model.Patient;
import org.mskcc.cbio.portal.model.Sample;
import org.mskcc.cbio.portal.util.ProgressMonitor;
Expand All @@ -52,6 +54,7 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;

/**
* DAO to `sample`.
Expand Down Expand Up @@ -279,11 +282,25 @@ public static void deleteAllRecords() throws DaoException

clearCache();
}
public static void deleteSamples(Collection<Integer> internalSampleIds) throws DaoException

/**
* Remove set of samples from the study
* @param internalStudyId - id of the study that contains the samples
* @param sampleStableIds - sample stable ids of samples to remove
* @throws DaoException
*/
public static void deleteSamples(int internalStudyId, Set<String> sampleStableIds) throws DaoException
{
if (internalSampleIds == null || internalSampleIds.isEmpty()) {
if (sampleStableIds == null || sampleStableIds.isEmpty()) {
log.info("No samples specified to remove for study with internal id={}. Skipping.", internalStudyId);
return;
}

log.info("Removing {} samples from study with internal id={} ...", sampleStableIds, internalStudyId);

Set<Integer> internalSampleIds = findInternalSampleIdsInStudy(internalStudyId, sampleStableIds);
removeSamplesInGeneticAlterationsForStudy(internalStudyId, internalSampleIds);

Connection con = null;
PreparedStatement pstmt = null;
try {
Expand All @@ -303,6 +320,7 @@ public static void deleteSamples(Collection<Integer> internalSampleIds) throws D
finally {
JdbcUtil.closeAll(DaoSample.class, con, pstmt, null);
}
log.info("Removing {} samples from study with internal id={} done.", sampleStableIds, internalStudyId);
}

private static Sample extractSample(ResultSet rs) throws SQLException
Expand All @@ -312,12 +330,59 @@ private static Sample extractSample(ResultSet rs) throws SQLException
rs.getInt("PATIENT_ID"));
}

public static void removeSamplesEverywhereInStudy(int internalStudyId, Set<String> sampleStableIds) throws DaoException {
log.info("Removing {} samples from study with internal id={} ...", sampleStableIds, internalStudyId);
Set<Integer> internalSampleIds = findInternalSampleIdsInStudy(internalStudyId, sampleStableIds);
DaoGeneticAlteration.getInstance().removeSamplesInGeneticAlterationsForStudy(internalStudyId, internalSampleIds);
DaoSample.deleteSamples(internalSampleIds);
log.info("Removing {} samples from study with internal id={} done.", sampleStableIds, internalStudyId);
/**
* Removes sample in genetic alterations' data for a study
* @param internalStudyId - internal id of study to remove samples in genetic alterations data
* @param internalSampleIdsToRemove - internal ids of samples to remove
* @throws DaoException
*/
private static void removeSamplesInGeneticAlterationsForStudy(int internalStudyId, Set<Integer> internalSampleIdsToRemove) throws DaoException {
List<GeneticProfile> geneticProfiles = DaoGeneticProfile.getAllGeneticProfiles(internalStudyId);
for (GeneticProfile geneticProfile : geneticProfiles) {
Set<Integer> removedInternalSampleIds = removeSamplesInGeneticAlterationsForGeneticProfile(geneticProfile, internalSampleIdsToRemove);
log.debug("Genetic alterations data for {} sample ids ouf of {} requested have been removed for genetic profile with stable id={}",
removedInternalSampleIds, internalSampleIdsToRemove, geneticProfile.getStableId());
}
}

/**
* Removes sample in genetic alterations' data for a genetic profile
* @param geneticProfile - genetic profile to remove samples in genetic alteration data
* @param internalSampleIdsToRemove - internal ids of samples to remove
* @return set of sample internal ids that were actually removed
* @throws DaoException
*/
private static Set<Integer> removeSamplesInGeneticAlterationsForGeneticProfile(GeneticProfile geneticProfile, Set<Integer> internalSampleIdsToRemove) throws DaoException {
int geneticProfileId = geneticProfile.getGeneticProfileId();
List<Integer> orderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(geneticProfileId);
Set<Integer> actualInternalSampleIdsToRemove = orderedSampleList.stream()
.filter(internalSampleIdsToRemove::contains).collect(Collectors.toUnmodifiableSet());
if (!actualInternalSampleIdsToRemove.isEmpty()) {
if (GeneticAlterationType.GENESET_SCORE.equals(geneticProfile.getGeneticAlterationType())) {
List<String> sampleStableIds = actualInternalSampleIdsToRemove.stream()
.map(internalSampleID ->
DaoSample.getSampleById(internalSampleID).getStableId())
.toList();
throw new RuntimeException("Sample(s) with stable id "
+ String.join(", ", sampleStableIds)
+ " can't be removed as it contains GSVA data." +
" Consider dropping and re-uploading the whole study.");
}
orderedSampleList.removeAll(actualInternalSampleIdsToRemove);
HashMap<Integer, HashMap<Integer, String>> geneticAlterationMapForEntityIds = DaoGeneticAlteration.getInstance().getGeneticAlterationMapForEntityIds(geneticProfileId, null);
DaoGeneticAlteration.getInstance().deleteAllRecordsInGeneticProfile(geneticProfileId);
if (!orderedSampleList.isEmpty()) {
for (Map.Entry<Integer, HashMap<Integer, String>> entry : geneticAlterationMapForEntityIds.entrySet()) {
String[] values = orderedSampleList.stream().map(isid -> entry.getValue().get(isid)).toArray(String[]::new);
DaoGeneticAlteration.getInstance().addGeneticAlterationsForGeneticEntity(geneticProfileId, entry.getKey(), values);
}
}
DaoGeneticProfileSamples.deleteAllSamplesInGeneticProfile(geneticProfileId);
if (!orderedSampleList.isEmpty()) {
DaoGeneticProfileSamples.addGeneticProfileSamples(geneticProfileId, orderedSampleList);
}
}
return actualInternalSampleIdsToRemove;
}

public static Set<Integer> findInternalSampleIdsInStudy(Integer internalStudyId, Set<String> sampleStableIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void doRun() {
ProgressMonitor.setCurrentMessage("Removing patient with stable id(s) ("
+ String.join(", ", patientIds)
+ ") from study with stable id=" + cancerStudy.getCancerStudyStableId() + " ...");
DaoPatient.removePatientsEverywhereInStudy(cancerStudy.getInternalId(), patientIds);
DaoPatient.deletePatients(cancerStudy.getInternalId(), patientIds);
}
} catch (DaoException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.NotNull;
import org.mskcc.cbio.portal.dao.DaoCancerStudy;
import org.mskcc.cbio.portal.dao.DaoException;
import org.mskcc.cbio.portal.dao.DaoSample;
import org.mskcc.cbio.portal.dao.JdbcUtil;
import org.mskcc.cbio.portal.model.CancerStudy;
import org.mskcc.cbio.portal.util.ProgressMonitor;
Expand All @@ -33,8 +34,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.mskcc.cbio.portal.dao.DaoSample.removeSamplesEverywhereInStudy;

/**
* Command Line Tool to Remove Samples in Cancer Studies
*/
Expand Down Expand Up @@ -76,7 +75,7 @@ private void doRun() {
ProgressMonitor.setCurrentMessage("Removing sample with stable id(s) ("
+ String.join(", ", sampleIds)
+ ") from study with stable id=" + cancerStudy.getCancerStudyStableId() + " ...");
removeSamplesEverywhereInStudy(cancerStudy.getInternalId(), sampleIds);
DaoSample.deleteSamples(cancerStudy.getInternalId(), sampleIds);
}
} catch (DaoException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit bd54ed2

Please sign in to comment.