Coverage for src/python/ensembl/vep/runner/corpus/save.py: 88%

38 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"""Registered corpus export.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from typing import ClassVar 

20 

21from ensembl.vep.config import Config 

22from ensembl.vep.corpus.exporter import get_exporter 

23from ensembl.vep.corpus.registry import CorpusOptionsError, build_corpus_from_options 

24from ensembl.vep.runner.base import ExitCode 

25from ensembl.vep.runner.registry import register_runner 

26 

27__all__ = ["Runner"] 

28 

29logger = logging.getLogger(__name__) 

30 

31 

32@register_runner("corpus.save") 

33class Runner: 

34 """Runner for exporting a corpus as FASTA, GFF3 and VCF. 

35 

36 Attributes: 

37 name (ClassVar[str]): The registry name. 

38 sections (ClassVar[tuple[str, ...]]): The configuration sections read. 

39 description (ClassVar[str]): One-line description for the subcommand list. 

40 """ 

41 

42 name: ClassVar[str] = "corpus.save" 

43 sections: ClassVar[tuple[str, ...]] = ("logging", "corpus", "corpus_save") 

44 description: ClassVar[str] = "write a registered corpus as FASTA, GFF3 and VCF" 

45 

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

47 """Hold the configuration.""" 

48 self.config = config 

49 

50 def run(self) -> ExitCode: 

51 """Write the configured corpus and report what was written.""" 

52 corpus = self.config.corpus 

53 if corpus.corpus_dir is None: 

54 logger.error("give --corpus_dir DIR to say where the corpus should be written") 

55 return ExitCode.USAGE 

56 

57 try: 

58 built_corpus = build_corpus_from_options(corpus) 

59 except CorpusOptionsError as unusable: 

60 logger.error("%s", unusable) 

61 return ExitCode.USAGE 

62 try: 

63 manifest = get_exporter(self.config.corpus_save.exporter).export( 

64 built_corpus, 

65 corpus.corpus_dir, 

66 force=self.config.corpus_save.force, 

67 ) 

68 except KeyError as unknown_exporter: 

69 logger.error("%s", unknown_exporter.args[0]) 

70 return ExitCode.USAGE 

71 except FileExistsError as present: 

72 logger.error("%s", present) 

73 return ExitCode.FAILURE 

74 finally: 

75 built_corpus.close() 

76 

77 logger.info( 

78 "wrote %s: %d transcripts, %d variants, %d overlaps on %s (identity %s)", 

79 corpus.corpus_dir, 

80 manifest.transcripts, 

81 manifest.variants, 

82 manifest.overlaps, 

83 ", ".join(manifest.chromosomes), 

84 manifest.identity, 

85 ) 

86 return ExitCode.OK