Coverage for biobb_pdb_tools/pdb_tools/common.py: 95%

41 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 15:44 +0000

1"""Module containing the base classes shared by the pdb-tools building blocks.""" 

2 

3import glob 

4import shlex 

5import zipfile 

6from pathlib import Path 

7from typing import ClassVar, Optional 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.tools import file_utils as fu 

10 

11 

12class PdbToolsBase(BiobbObject): 

13 """Base of the pdb-tools blocks that go from the input file to the output file, the stdout of the tool.""" 

14 

15 #: Property whose value is the option of the tool (chain "A" gives ``-A``) 

16 option_property: ClassVar[Optional[str]] = None 

17 #: Boolean property that gives the option of its name (strict gives ``-strict``) 

18 flag_property: ClassVar[Optional[str]] = None 

19 #: The input can be a zip, whose PDB files the tool takes 

20 zip_input: ClassVar[bool] = False 

21 

22 def input_files(self) -> list[str]: 

23 """Go from the input file to the files of the command: the input file, or the PDB files of 

24 a zip input (extracted in the sandbox, sorted by name, without the macOS ``._*`` files).""" 

25 input_file = self.sandbox_file("input_file_path") 

26 if not (self.zip_input and input_file and zipfile.is_zipfile(input_file)): 

27 return [self.stage_io_dict["in"]["input_file_path"]] 

28 with zipfile.ZipFile(input_file) as zip_file: 

29 zip_file.extractall(self.stage_io_dict["unique_dir"]) 

30 names = zip_file.namelist() 

31 pdb_names = [name for name in names if name.lower().endswith(".pdb") and not Path(name).name.startswith("._")] 

32 return sorted(pdb_names, key=lambda name: Path(name).stem.upper()) 

33 

34 def run(self) -> None: 

35 """Run the tool with its option, the input files and the output file as its stdout. The 

36 values and paths are quoted for the shell (spaces, shell characters), not the redirection.""" 

37 self.cmd = [self.binary_path] 

38 value = getattr(self, self.option_property) if self.option_property else None 

39 if value is not None and value != "": 

40 self.cmd.append(shlex.quote(f"-{value}")) 

41 if self.flag_property and getattr(self, self.flag_property): 

42 self.cmd.append(f"-{self.flag_property}") 

43 self.cmd += [*map(shlex.quote, self.input_files()), ">", shlex.quote(self.stage_io_dict["out"]["output_file_path"])] 

44 self.run_biobb() 

45 

46 

47class PdbSplitBase(BiobbObject): 

48 """Base of the pdb-tools blocks that go from a PDB file to a zip with one PDB file per model or segment.""" 

49 

50 #: What the tool splits the input by ("model" or "segment"), for the log 

51 split_by: ClassVar[str] = "" 

52 

53 def run(self) -> None: 

54 """Run the tool, which writes <input name>_<model or segment>.pdb where it runs, and zip 

55 those files: the input file if there are none.""" 

56 input_file = self.stage_io_dict["in"]["input_file_path"] 

57 self.cmd = [self.binary_path, shlex.quote(input_file)] 

58 self.run_biobb() 

59 pdb_files = glob.glob(str(Path(self.stage_io_dict["unique_dir"]) / f"{Path(input_file).stem}_*.pdb")) 

60 if pdb_files: 

61 fu.log(f"Saving {len(pdb_files)} PDB files, one per {self.split_by}, in the zip", self.out_log, self.global_log) 

62 else: 

63 fu.log(f"The input file has no {self.split_by}s: saving the input file in the zip", self.out_log, self.global_log) 

64 pdb_files = [str(self.sandbox_file("input_file_path"))] 

65 fu.zip_list(str(self.sandbox_file("output_file_path")), pdb_files)