From aabba1a3c434fbf7d1436eaea10807f83defc907 Mon Sep 17 00:00:00 2001 From: Etienne Boileau Date: Sun, 4 Aug 2024 19:56:43 +0200 Subject: [PATCH] WIP delete selection --- server/src/scimodom/app.py | 9 ++++++--- server/src/scimodom/cli/dataset.py | 12 +++++++++--- server/src/scimodom/cli/project.py | 3 +++ server/src/scimodom/services/file.py | 9 +++++++++ server/tests/unit/services/test_dataset.py | 4 ++++ 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/server/src/scimodom/app.py b/server/src/scimodom/app.py index 316caa73..b96fa41b 100644 --- a/server/src/scimodom/app.py +++ b/server/src/scimodom/app.py @@ -435,13 +435,16 @@ def batch(input_directory, request_uuids, annotation): multiple=True, required=False, type=click.INT, - help="Selection ID(s) to delete. Repeat parameter to pass multiple selection IDs. Use at your own risk!", + help="Selection ID(s) to delete. Repeat parameter to pass multiple selection IDs.", ) @click.argument("smid", type=click.STRING) def delete(smid, selection): """Delete a project and all associated - data from the database. Deleting - selections at your own risk. + data from the database. If given, delete + selections (and gene cache) associated + with the project data. Selections + may be associated with other datasets, + delete at your own risk! SMID is the Sci-ModoM project ID. """ diff --git a/server/src/scimodom/cli/dataset.py b/server/src/scimodom/cli/dataset.py index 2724657c..21b91c30 100644 --- a/server/src/scimodom/cli/dataset.py +++ b/server/src/scimodom/cli/dataset.py @@ -20,6 +20,7 @@ from scimodom.services.annotation import AnnotationSource from scimodom.services.assembly import AssemblyNotFoundError, get_assembly_service from scimodom.services.dataset import get_dataset_service +from scimodom.services.file import get_file_service from scimodom.services.project import get_project_service from scimodom.utils.project_dto import ( ProjectMetaDataDto, @@ -295,9 +296,14 @@ def add_selection( def delete_selection(selection: Selection) -> None: session = get_session() - session.delete(selection) - session.commit() - # TODO delete gene cache + file_service = get_file_service() + try: + session.delete(selection) + file_service.delete_gene_cache(selection.id) + session.commit() + except Exception: + session.rollback() + raise def get_selection_from_dataset(dataset: Dataset) -> Iterator[Selection]: diff --git a/server/src/scimodom/cli/project.py b/server/src/scimodom/cli/project.py index 212a919b..35f64b4e 100644 --- a/server/src/scimodom/cli/project.py +++ b/server/src/scimodom/cli/project.py @@ -210,6 +210,9 @@ def delete_project(smid: str, selection_ids: list[int]) -> None: BAM files associated with a dataset belonging to this project are deleted from the file system. + If given, selection IDs are deleted and + gene cache files are removed. + :param smid: Project ID (SMID) :type smid: str :param selection_ids: List of selection IDs to delete, diff --git a/server/src/scimodom/services/file.py b/server/src/scimodom/services/file.py index 6c1f990c..6cb81415 100644 --- a/server/src/scimodom/services/file.py +++ b/server/src/scimodom/services/file.py @@ -196,6 +196,15 @@ def update_gene_cache(self, selection_id: int, genes: Iterable[str]) -> None: print(g, file=fh) flock(fh.fileno(), LOCK_UN) + def delete_gene_cache(self, selection_id: int) -> None: + """Remove a gene cache file for a given selection. + + :param selection_id: Selection ID + :type selection_id: int + """ + path = Path(self._get_gene_cache_dir(), str(selection_id)) + path.unlink() + def _get_gene_cache_dir(self) -> Path: return Path(self._data_path, self.CACHE_DEST) diff --git a/server/tests/unit/services/test_dataset.py b/server/tests/unit/services/test_dataset.py index ff5cd636..88a08350 100644 --- a/server/tests/unit/services/test_dataset.py +++ b/server/tests/unit/services/test_dataset.py @@ -28,6 +28,10 @@ class MockFileService: def __init__(self, session): self._session = session self.deleted_bam_files: list[str] = [] + self.deleted_gene_cache: list[int] = [] + + def delete_gene_cache(self, selection_id: int) -> None: + self.deleted_gene_cache.append(selection_id) def remove_bam_file(self, bam_file): self.deleted_bam_files.append(bam_file.original_file_name)