Coverage for biobb_vs/utils/extract_model_pdbqt.py: 92%

40 statements  

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

1#!/usr/bin/env python3 

2 

3"""Module containing the ExtractModelPDBQT class and the command line interface.""" 

4import warnings 

5from pathlib import PurePath 

6from typing import Optional 

7from Bio import BiopythonDeprecationWarning 

8from biobb_common.generic.biobb_object import BiobbObject, launcher 

9from biobb_common.tools import file_utils as fu 

10 

11 

12with warnings.catch_warnings(): 

13 warnings.simplefilter("ignore", BiopythonDeprecationWarning) 

14 # try: 

15 # import Bio.SubsMat.MatrixInfo 

16 # except ImportError: 

17 import Bio.Align.substitution_matrices 

18 import Bio.pairwise2 

19 import Bio.PDB 

20 

21 

22class ExtractModelPDBQT(BiobbObject): 

23 """ 

24 | biobb_vs ExtractModelPDBQT 

25 | Extracts a model from a PDBQT file with several models. 

26 | Extracts a model from a PDBQT file with several models. The model number to extract is defined by the user. 

27 

28 Args: 

29 input_pdbqt_path (str): Input PDBQT file. File type: input. `Sample file <https://github.com/bioexcel/biobb_vs/raw/master/biobb_vs/test/data/utils/models.pdbqt>`_. Accepted formats: pdbqt (edam:format_1476). 

30 output_pdbqt_path (str): Output PDBQT file. File type: output. `Sample file <https://github.com/bioexcel/biobb_vs/raw/master/biobb_vs/test/reference/utils/ref_extract_model.pdbqt>`_. Accepted formats: pdbqt (edam:format_1476). 

31 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

32 * **model** (*int*) - (1) [0~1000|1] Model number to extract from input_pdbqt_path. 

33 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

34 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

35 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

36 

37 Examples: 

38 This is a use example of how to use the building block from Python:: 

39 

40 from biobb_vs.utils.extract_model_pdbqt import extract_model_pdbqt 

41 prop = { 

42 'model': 1 

43 } 

44 extract_model_pdbqt(input_pdbqt_path='/path/to/myStructure.pdbqt', 

45 output_pdbqt_path='/path/to/newStructure.pdbqt', 

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: In house using Biopython 

51 * version: >=1.76 

52 * license: Apache-2.0 

53 * ontology: 

54 * name: EDAM 

55 * schema: http://edamontology.org/EDAM.owl 

56 

57 """ 

58 

59 def __init__( 

60 self, input_pdbqt_path: str, output_pdbqt_path: str, properties: Optional[dict] = None, **kwargs 

61 ) -> None: 

62 super().__init__(**locals()) 

63 

64 def stage_files(self) -> None: 

65 """The block does not use the sandbox: there are no files to stage.""" 

66 

67 def run(self) -> None: 

68 """Execute the :class:`ExtractModelPDBQT <utils.extract_model_pdbqt.ExtractModelPDBQT>` utils.extract_model_pdbqt.ExtractModelPDBQT object.""" 

69 

70 structure_name = PurePath(self.io_dict["in"]["input_pdbqt_path"]).name 

71 parser = Bio.PDB.PDBParser(QUIET=True) 

72 structPDB = parser.get_structure( 

73 structure_name, self.io_dict["in"]["input_pdbqt_path"] 

74 ) 

75 

76 models = [] 

77 for model in structPDB.get_models(): 

78 models.append(model.id + 1) 

79 

80 if self.model not in models: 

81 fu.log( 

82 self.__class__.__name__ + ": Selected model %d not found in %s structure." 

83 % (self.model, self.io_dict["in"]["input_pdbqt_path"]), 

84 self.out_log, 

85 ) 

86 raise SystemExit( 

87 self.__class__.__name__ + ": Selected model %d not found in %s structure." 

88 % (self.model, self.io_dict["in"]["input_pdbqt_path"]) 

89 ) 

90 

91 save = False 

92 lines = 0 

93 with open(self.io_dict["in"]["input_pdbqt_path"], "r") as input_pdb, open( 

94 self.io_dict["out"]["output_pdbqt_path"], "w" 

95 ) as output_pdb: 

96 for line in input_pdb: 

97 if line.startswith("MODEL") and line.split()[1] == str(self.model): 

98 save = True 

99 if line.startswith("ENDMDL"): 

100 save = False 

101 if save and not line.startswith("MODEL"): 

102 lines = lines + 1 

103 output_pdb.write(line) 

104 

105 fu.log( 

106 "Saving model %d to %s" 

107 % (self.model, self.io_dict["out"]["output_pdbqt_path"]), 

108 self.out_log, 

109 ) 

110 

111 

112extract_model_pdbqt = launcher(ExtractModelPDBQT) 

113 

114 

115if __name__ == "__main__": 

116 ExtractModelPDBQT.main()