Skip to content

Commit

Permalink
Merge pull request #2343 from opencb/TASK-4210
Browse files Browse the repository at this point in the history
TASK-4210 - Catalog delete operations may not check if data is used in Clinical Analyses
  • Loading branch information
pfurio authored Sep 27, 2023
2 parents 3c931ca + db992c5 commit 6d67420
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ private void updateClinicalAnalysisIndividualReferences(ClientSession clientSess
*/
private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document individual)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
String individualId = individual.getString(QueryParams.ID.key());
long individualUid = individual.getLong(PRIVATE_UID);
long studyUid = individual.getLong(PRIVATE_STUDY_UID);

Expand All @@ -653,8 +652,7 @@ private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document
.append(ClinicalAnalysisDBAdaptor.QueryParams.INDIVIDUAL.key(), individualUid);
OpenCGAResult<Long> count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query);
if (count.getNumMatches() > 0) {
throw new CatalogDBException("Could not delete individual '" + individualId + "'. Individual is in use in "
+ count.getNumMatches() + " cases");
throw new CatalogDBException("Individual is in use in " + count.getNumMatches() + " cases");
}
}

Expand Down Expand Up @@ -940,7 +938,7 @@ public OpenCGAResult delete(Individual individual) throws CatalogDBException, Ca
return runTransaction(clientSession -> privateDelete(clientSession, result.first()));
} catch (CatalogDBException e) {
logger.error("Could not delete individual {}: {}", individual.getId(), e.getMessage(), e);
throw new CatalogDBException("Could not delete individual " + individual.getId() + ": " + e.getMessage(), e.getCause());
throw new CatalogDBException("Could not delete individual " + individual.getId() + ": " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -976,7 +974,7 @@ OpenCGAResult<Object> privateDelete(ClientSession clientSession, Document indivi
checkInUseInClinicalAnalysis(clientSession, individualDocument);

logger.debug("Deleting individual {} ({})", individualId, individualUid);
// Look for all the different family versions
// Look for all the different individual versions
Query individualQuery = new Query()
.append(QueryParams.UID.key(), individualUid)
.append(QueryParams.STUDY_UID.key(), studyUid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ void updateIndividualIdFromSamples(ClientSession clientSession, long studyUid, S
*/
private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document sample)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
String sampleId = sample.getString(QueryParams.ID.key());
long sampleUid = sample.getLong(QueryParams.UID.key());
long studyUid = sample.getLong(QueryParams.STUDY_UID.key());

Expand All @@ -509,8 +508,7 @@ private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document
.append(ClinicalAnalysisDBAdaptor.QueryParams.SAMPLE.key(), sampleUid);
OpenCGAResult<Long> count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query);
if (count.getNumMatches() > 0) {
throw new CatalogDBException("Could not delete sample '" + sampleId + "'. Sample is in use in "
+ count.getNumMatches() + " cases");
throw new CatalogDBException("Sample is in use in " + count.getNumMatches() + " cases");
}
}

Expand Down Expand Up @@ -860,7 +858,7 @@ public OpenCGAResult delete(Sample sample) throws CatalogDBException, CatalogPar
return runTransaction(clientSession -> privateDelete(clientSession, result.first()));
} catch (CatalogDBException e) {
logger.error("Could not delete sample {}: {}", sample.getId(), e.getMessage(), e);
throw new CatalogDBException("Could not delete sample " + sample.getId() + ": " + e.getMessage(), e.getCause());
throw new CatalogDBException("Could not delete sample " + sample.getId() + ": " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ private void checkCanBeDeleted(Study study, Family family) throws CatalogExcepti
OpenCGAResult<ClinicalAnalysis> result = clinicalDBAdaptor.get(query, ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS);
if (result.getNumResults() > 0) {
String clinicalIds = result.getResults().stream().map(ClinicalAnalysis::getId).collect(Collectors.joining(", "));
throw new CatalogException("Family {" + family.getId() + "} in use in Clinical Analyses: {" + clinicalIds + "}");
throw new CatalogException("Could not delete family '" + family.getId() + "'. Family is in use in Clinical Analyses: '"
+ clinicalIds + "'");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,8 @@ private OpenCGAResult delete(Study study, Individual individual, ObjectMap param
// Check if the individual can be deleted
if (!params.getBoolean(Constants.FORCE, false)) {
if (familyDataResult.getNumResults() > 0) {
throw new CatalogException("Individual found in the families: " + familyDataResult.getResults()
.stream()
.map(Family::getId)
.collect(Collectors.joining(", ")));
throw new CatalogException("Could not delete individual '" + individual.getId() + "'. Individual found in the families: "
+ familyDataResult.getResults().stream().map(Family::getId).collect(Collectors.joining(", ")));
}
} else {
logger.info("Forcing deletion of individuals belonging to families");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ public OpenCGAResult<Sample> resetAnnotations(String studyStr, String sampleStr,
}

private void checkSampleCanBeDeleted(long studyId, Sample sample, boolean force) throws CatalogException {
String msg = "Could not delete sample '" + sample.getId() + "'. ";
// Look for files related with the sample
Query query = new Query()
.append(FileDBAdaptor.QueryParams.SAMPLE_IDS.key(), sample.getId())
Expand All @@ -834,9 +835,9 @@ private void checkSampleCanBeDeleted(long studyId, Sample sample, boolean force)
}
if (!errorFiles.isEmpty()) {
if (force) {
throw new CatalogException("Associated files are used in storage: " + StringUtils.join(errorFiles, ", "));
throw new CatalogException(msg + "Associated files are used in storage: " + StringUtils.join(errorFiles, ", "));
} else {
throw new CatalogException("Sample associated to the files: " + StringUtils.join(errorFiles, ", "));
throw new CatalogException(msg + "Sample associated to the files: " + StringUtils.join(errorFiles, ", "));
}
}

Expand Down Expand Up @@ -865,14 +866,14 @@ private void checkSampleCanBeDeleted(long studyId, Sample sample, boolean force)
}
}
if (associatedToDefaultCohort) {
throw new CatalogException("Sample in cohort " + StudyEntry.DEFAULT_COHORT);
throw new CatalogException(msg + "Sample in cohort " + StudyEntry.DEFAULT_COHORT);
}
if (!errorCohorts.isEmpty()) {
if (force) {
throw new CatalogException("Sample present in cohorts in the process of calculating the stats: "
throw new CatalogException(msg + "Sample present in cohorts in the process of calculating the stats: "
+ StringUtils.join(errorCohorts, ", "));
} else {
throw new CatalogException("Sample present in cohorts: " + StringUtils.join(errorCohorts, ", "));
throw new CatalogException(msg + "Sample present in cohorts: " + StringUtils.join(errorCohorts, ", "));
}
}

Expand All @@ -884,8 +885,7 @@ private void checkSampleCanBeDeleted(long studyId, Sample sample, boolean force)
OpenCGAResult<Individual> individualDataResult = individualDBAdaptor.get(query, new QueryOptions(QueryOptions.INCLUDE,
Arrays.asList(IndividualDBAdaptor.QueryParams.UID.key(), IndividualDBAdaptor.QueryParams.ID.key())));
if (individualDataResult.getNumResults() > 0) {
throw new CatalogException("Sample from individual " + individualDataResult.first().getName() + "("
+ individualDataResult.first().getUid() + ")");
throw new CatalogException(msg + "Sample is associated with individual '" + individualDataResult.first().getId() + "'.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2786,7 +2786,7 @@ public void deleteInUseInCATest() throws CatalogException {

try {
catalogManager.getSampleManager().delete(studyFqn, Collections.singletonList(case1.getProband().getSamples().get(0).getId()),
new QueryOptions(ParamConstants.FORCE, true), token);
new QueryOptions(ParamConstants.FORCE, false), token);
} catch (CatalogException e) {
assertTrue(e.getMessage().contains("in use in 3 cases"));
}
Expand Down

0 comments on commit 6d67420

Please sign in to comment.