diff --git a/src/main/java/org/mskcc/cbio/portal/dao/DaoGeneticAlteration.java b/src/main/java/org/mskcc/cbio/portal/dao/DaoGeneticAlteration.java index 5f705a62..8d82379b 100644 --- a/src/main/java/org/mskcc/cbio/portal/dao/DaoGeneticAlteration.java +++ b/src/main/java/org/mskcc/cbio/portal/dao/DaoGeneticAlteration.java @@ -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 internalSampleIdsToRemove) throws DaoException { - List geneticProfiles = DaoGeneticProfile.getAllGeneticProfiles(internalStudyId); - for (GeneticProfile geneticProfile : geneticProfiles) { - Set 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 removeSamplesInGeneticAlterationsForGeneticProfile(GeneticProfile geneticProfile, Set internalSampleIdsToRemove) throws DaoException { - int geneticProfileId = geneticProfile.getGeneticProfileId(); - List orderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(geneticProfileId); - Set actualInternalSampleIdsToRemove = orderedSampleList.stream() - .filter(internalSampleIdsToRemove::contains).collect(Collectors.toUnmodifiableSet()); - if (!actualInternalSampleIdsToRemove.isEmpty()) { - if (GeneticAlterationType.GENESET_SCORE.equals(geneticProfile.getGeneticAlterationType())) { - List 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> geneticAlterationMapForEntityIds = DaoGeneticAlteration.getInstance().getGeneticAlterationMapForEntityIds(geneticProfileId, null); - DaoGeneticAlteration.getInstance().deleteAllRecordsInGeneticProfile(geneticProfileId); - if (!orderedSampleList.isEmpty()) { - for (Map.Entry> 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; - } } diff --git a/src/main/java/org/mskcc/cbio/portal/dao/DaoPatient.java b/src/main/java/org/mskcc/cbio/portal/dao/DaoPatient.java index b09faa93..05675b4c 100644 --- a/src/main/java/org/mskcc/cbio/portal/dao/DaoPatient.java +++ b/src/main/java/org/mskcc/cbio/portal/dao/DaoPatient.java @@ -221,23 +221,26 @@ private static Patient extractPatient(ResultSet rs) throws SQLException } } - public static void removePatientsEverywhereInStudy(int internalStudyId, Set patientStableIds) throws DaoException { - log.info("Removing {} patients from study with internal id={} ...", patientStableIds, internalStudyId); - Set internalPatientIds = findInternalPatientIdsInStudy(internalStudyId, patientStableIds); - for (Integer internalPatientId : internalPatientIds) { - Set 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 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 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 internalPatientIds = findInternalPatientIdsInStudy(internalStudyId, patientStableIds); + Set 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 { @@ -257,6 +260,7 @@ public static void deletePatients(Collection 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 findInternalPatientIdsInStudy(Integer internalStudyId, Set patientStableIds) { diff --git a/src/main/java/org/mskcc/cbio/portal/dao/DaoSample.java b/src/main/java/org/mskcc/cbio/portal/dao/DaoSample.java index 09c23272..d67cbeeb 100644 --- a/src/main/java/org/mskcc/cbio/portal/dao/DaoSample.java +++ b/src/main/java/org/mskcc/cbio/portal/dao/DaoSample.java @@ -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; @@ -52,6 +54,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; +import java.util.stream.Collectors; /** * DAO to `sample`. @@ -279,11 +282,25 @@ public static void deleteAllRecords() throws DaoException clearCache(); } - public static void deleteSamples(Collection 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 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 internalSampleIds = findInternalSampleIdsInStudy(internalStudyId, sampleStableIds); + removeSamplesInGeneticAlterationsForStudy(internalStudyId, internalSampleIds); + Connection con = null; PreparedStatement pstmt = null; try { @@ -303,6 +320,7 @@ public static void deleteSamples(Collection 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 @@ -312,12 +330,59 @@ private static Sample extractSample(ResultSet rs) throws SQLException rs.getInt("PATIENT_ID")); } - public static void removeSamplesEverywhereInStudy(int internalStudyId, Set sampleStableIds) throws DaoException { - log.info("Removing {} samples from study with internal id={} ...", sampleStableIds, internalStudyId); - Set 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 internalSampleIdsToRemove) throws DaoException { + List geneticProfiles = DaoGeneticProfile.getAllGeneticProfiles(internalStudyId); + for (GeneticProfile geneticProfile : geneticProfiles) { + Set 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 removeSamplesInGeneticAlterationsForGeneticProfile(GeneticProfile geneticProfile, Set internalSampleIdsToRemove) throws DaoException { + int geneticProfileId = geneticProfile.getGeneticProfileId(); + List orderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(geneticProfileId); + Set actualInternalSampleIdsToRemove = orderedSampleList.stream() + .filter(internalSampleIdsToRemove::contains).collect(Collectors.toUnmodifiableSet()); + if (!actualInternalSampleIdsToRemove.isEmpty()) { + if (GeneticAlterationType.GENESET_SCORE.equals(geneticProfile.getGeneticAlterationType())) { + List 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> geneticAlterationMapForEntityIds = DaoGeneticAlteration.getInstance().getGeneticAlterationMapForEntityIds(geneticProfileId, null); + DaoGeneticAlteration.getInstance().deleteAllRecordsInGeneticProfile(geneticProfileId); + if (!orderedSampleList.isEmpty()) { + for (Map.Entry> 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 findInternalSampleIdsInStudy(Integer internalStudyId, Set sampleStableIds) { diff --git a/src/main/java/org/mskcc/cbio/portal/scripts/RemovePatients.java b/src/main/java/org/mskcc/cbio/portal/scripts/RemovePatients.java index 8d494b97..bec9fabc 100644 --- a/src/main/java/org/mskcc/cbio/portal/scripts/RemovePatients.java +++ b/src/main/java/org/mskcc/cbio/portal/scripts/RemovePatients.java @@ -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); diff --git a/src/main/java/org/mskcc/cbio/portal/scripts/RemoveSamples.java b/src/main/java/org/mskcc/cbio/portal/scripts/RemoveSamples.java index 66a1dd85..c0aa170d 100644 --- a/src/main/java/org/mskcc/cbio/portal/scripts/RemoveSamples.java +++ b/src/main/java/org/mskcc/cbio/portal/scripts/RemoveSamples.java @@ -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; @@ -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 */ @@ -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);