Coverage for biobb_structure_utils/utils/extract_residues.py: 86%

21 statements  

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

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from Bio.PDB.PDBParser import PDBParser 

6from biobb_common.generic.biobb_object import BiobbObject, launcher 

7from biobb_common.tools import file_utils as fu 

8 

9from biobb_structure_utils.utils.common import ( 

10 create_biopython_residue, 

11 create_output_file, 

12 create_residues_list, 

13 select_residues, 

14) 

15 

16 

17class ExtractResidues(BiobbObject): 

18 """ 

19 | biobb_structure_utils ExtractResidues 

20 | Class to extract residues from a 3D structure using Biopython. 

21 | Extracts a list of residues from a 3D structure using Biopython. 

22 

23 Args: 

24 input_structure_path (str): Input structure file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/extract_heteroatom.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

25 output_residues_path (str): Output residues file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/ref_extract_residues.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

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

27 * **residues** (*list*) - (None) List of comma separated res_id (will extract all residues that match the res_id) or list of dictionaries with the name | res_id | chain | model of the residues to be extracted. Format: [{"name": "HIS", "res_id": "72", "chain": "A", "model": "1"}]. The res_id of a residue with an insertion code includes the code ("62A"). 

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

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

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

31 

32 Examples: 

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

34 

35 from biobb_structure_utils.utils.extract_residues import extract_residues 

36 prop = { 

37 'residues': [ 

38 { 

39 'name': 'HIS', 

40 'res_id': '72', 

41 'chain': 'A', 

42 'model': '1' 

43 } 

44 ] 

45 } 

46 extract_residues(input_structure_path='/path/to/myStructure.pdb', 

47 output_residues_path='/path/to/newResidues.pdb', 

48 properties=prop) 

49 

50 Info: 

51 * wrapped_software: 

52 * name: In house using Biopython 

53 * version: >=1.79 

54 * license: other 

55 * ontology: 

56 * name: EDAM 

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

58 

59 """ 

60 

61 def __init__( 

62 self, input_structure_path: str, output_residues_path: str, properties: Optional[dict] = None, **kwargs 

63 ) -> None: 

64 super().__init__(**locals()) 

65 

66 # Properties specific for BB 

67 self.residues = fu.str_to_list(self.residues) 

68 

69 def run(self) -> None: 

70 """Execute the :class:`ExtractResidues <utils.extract_residues.ExtractResidues>` utils.extract_residues.ExtractResidues object.""" 

71 

72 # Business code 

73 # get list of Residues from properties 

74 list_residues = create_residues_list(self.residues) 

75 

76 # load input into BioPython structure 

77 structure = PDBParser(QUIET=True).get_structure( 

78 "structure", self.sandbox_file("input_structure_path") 

79 ) 

80 

81 # get desired residues 

82 new_structure = [create_biopython_residue(residue) for residue in select_residues(structure, list_residues)] 

83 

84 # if not residues found in structure, raise exit 

85 if not new_structure: 

86 fu.log( 

87 self.__class__.__name__ + ": The residues given by user were not found in input structure", 

88 self.out_log, 

89 ) 

90 raise SystemExit( 

91 self.__class__.__name__ + ": The residues given by user were not found in input structure" 

92 ) 

93 

94 create_output_file( 

95 2, 

96 self.sandbox_file("input_structure_path"), 

97 new_structure, 

98 self.sandbox_file("output_residues_path"), 

99 self.out_log, 

100 ) 

101 

102 self.return_code = 0 

103 

104 

105extract_residues = launcher(ExtractResidues) 

106 

107if __name__ == "__main__": 

108 ExtractResidues.main()