Coverage for src/python/ensembl/vep/runner/catalogue/remove.py: 81%

45 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-18 14:41 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14"""Consequence catalogue term removal.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from pathlib import Path 

20from typing import ClassVar 

21 

22from ensembl.vep.annotators.consequence.rules.editing import apply_remove 

23from ensembl.vep.annotators.consequence.rules.validation import ValidationReport 

24from ensembl.vep.config import Config 

25from ensembl.vep.runner.base import ExitCode 

26from ensembl.vep.runner.catalogue.common import ( 

27 destination_for, 

28 log_diff, 

29 log_validation_report, 

30 read_payload, 

31 write_payload, 

32) 

33from ensembl.vep.runner.registry import register_runner 

34 

35__all__ = ["Runner"] 

36 

37logger = logging.getLogger(__name__) 

38 

39 

40@register_runner("catalogue.remove") 

41class Runner: 

42 """Runner for removing a consequence term and its references.""" 

43 

44 name: ClassVar[str] = "catalogue.remove" 

45 sections: ClassVar[tuple[str, ...]] = ("logging", "catalogue", "catalogue_write", "catalogue_remove") 

46 description: ClassVar[str] = "remove a term from the consequence catalogue" 

47 

48 def __init__(self, config: Config) -> None: 

49 """Hold the configuration.""" 

50 self.config = config 

51 

52 def run(self) -> ExitCode: 

53 """Remove the term, or report why it was refused.""" 

54 entry = self.config.catalogue_remove 

55 if entry.term is None: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true

56 logger.error("nothing to remove: give --term") 

57 return ExitCode.USAGE 

58 

59 baseline, payload = read_payload(self.config) 

60 try: 

61 destination = destination_for(self.config) if not self.config.catalogue_write.dry_run else None 

62 updated, messages = apply_remove(payload, term=entry.term) 

63 except ValueError as error: 

64 logger.error("%s", error) 

65 return ExitCode.USAGE 

66 

67 for message in messages: 

68 logger.info("%s", message) 

69 

70 report = ValidationReport.for_catalogue_entries(updated["consequences"], baseline["consequences"]) 

71 log_validation_report(report) 

72 if report.has_errors: 72 ↛ 73line 72 didn't jump to line 73 because the condition on line 72 was never true

73 logger.error("refusing to write: validation reported errors") 

74 return ExitCode.FAILURE 

75 

76 if destination is None: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true

77 log_diff(baseline, updated, self.config.catalogue.catalogue_file or Path("catalogue.json")) 

78 logger.info("dry run: nothing written") 

79 return ExitCode.OK 

80 

81 log_diff(baseline, updated, destination) 

82 write_payload(updated, destination) 

83 return ExitCode.OK