Coverage for biobb_structure_utils/utils/cat_pdb.py: 79%
58 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-03 15:30 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-03 15:30 +0000
1#!/usr/bin/env python3
3"""Module containing the CatPDB class and the command line interface."""
4import argparse
5from biobb_common.configuration import settings
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.tools.file_utils import launchlogger
8from biobb_structure_utils.utils.common import check_input_path, check_output_path
11class CatPDB(BiobbObject):
12 """
13 | biobb_structure_utils CatPDB
14 | Class to concat two PDB structures in a single PDB file.
15 | Class to concat two PDB structures in a single PDB file.
17 Args:
18 input_structure1 (str): Input structure 1 file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/cat_protein.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476).
19 input_structure2 (str): Input structure 2 file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/cat_ligand.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476).
20 output_structure_path (str): Output protein file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/ref_cat_pdb.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476).
21 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
22 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
23 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
24 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
26 Examples:
27 This is a use example of how to use the building block from Python::
29 from biobb_structure_utils.utils.cat_pdb import cat_pdb
30 prop = { }
31 cat_pdb(input_structure1='/path/to/myInputStr1.pdb',
32 input_structure2='/path/to/myInputStr2.pdb',
33 output_structure_path='/path/to/newStructure.pdb',
34 properties=prop)
36 Info:
37 * wrapped_software:
38 * name: In house
39 * license: Apache-2.0
40 * ontology:
41 * name: EDAM
42 * schema: http://edamontology.org/EDAM.owl
44 """
46 def __init__(self, input_structure1, input_structure2, output_structure_path, properties=None, **kwargs) -> None:
47 properties = properties or {}
49 # Call parent class constructor
50 super().__init__(properties)
51 self.locals_var_dict = locals().copy()
53 # Input/Output files
54 self.io_dict = {
55 "in": {"input_structure1": input_structure1,
56 "input_structure2": input_structure2},
57 "out": {"output_structure_path": output_structure_path}
58 }
60 # Properties specific for BB
61 self.properties = properties
63 # Check the properties
64 self.check_properties(properties)
65 self.check_arguments()
67 @launchlogger
68 def launch(self) -> int:
69 """Execute the :class:`CatPDB <utils.cat_pdb.CatPDB>` utils.cat_pdb.CatPDB object."""
71 self.io_dict['in']['input_structure1'] = check_input_path(self.io_dict['in']['input_structure1'],
72 self.out_log, self.__class__.__name__)
73 self.io_dict['in']['input_structure2'] = check_input_path(self.io_dict['in']['input_structure2'],
74 self.out_log, self.__class__.__name__)
75 self.io_dict['out']['output_structure_path'] = check_output_path(self.io_dict['out']['output_structure_path'],
76 self.out_log, self.__class__.__name__)
78 # Setup Biobb
79 if self.check_restart():
80 return 0
81 self.stage_files()
83 # Business code
84 filenames = [self.io_dict['in']['input_structure1'], self.io_dict['in']['input_structure2']]
85 # check if self.input_structure1 and self.input_structure2 end with newline
86 newline = [False, False]
87 for idx, fname in enumerate(filenames):
88 with open(fname, 'rb') as fh:
89 fh.seek(-2, 2)
90 last = fh.readlines()[-1].decode()
91 newline[idx] = "\n" in last
93 # concat both input files and save them into output file
94 with open(self.io_dict['out']['output_structure_path'], 'w') as outfile:
95 for idx, fname in enumerate(filenames):
96 with open(fname) as infile:
97 for line in infile:
98 if not line.startswith("END"):
99 outfile.write(line)
100 # if not ends in newline, add it
101 if not newline[idx]:
102 outfile.write("\n")
103 self.return_code = 0
105 # Copy files to host
106 self.copy_to_host()
108 # Remove temporal files
109 self.tmp_files.append(self.stage_io_dict.get("unique_dir"))
110 self.remove_tmp_files()
112 self.check_arguments(output_files_created=True, raise_exception=False)
114 return self.return_code
117def cat_pdb(input_structure1: str, input_structure2: str, output_structure_path: str, properties: dict = None, **kwargs) -> int:
118 """Execute the :class:`CatPDB <utils.cat_pdb.CatPDB>` class and
119 execute the :meth:`launch() <utils.cat_pdb.CatPDB.launch>` method."""
121 return CatPDB(input_structure1=input_structure1,
122 input_structure2=input_structure2,
123 output_structure_path=output_structure_path,
124 properties=properties, **kwargs).launch()
127def main():
128 """Command line execution of this building block. Please check the command line documentation."""
129 parser = argparse.ArgumentParser(description="Concat two PDB structures in a single PDB file.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
130 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string")
132 # Specific args of each building block
133 required_args = parser.add_argument_group('required arguments')
134 required_args.add_argument('-i1', '--input_structure1', required=True, help="Input structure 1 file path. Accepted formats: pdb.")
135 required_args.add_argument('-i2', '--input_structure2', required=True, help="Input structure 2 file path. Accepted formats: pdb.")
136 required_args.add_argument('-o', '--output_structure_path', required=True, help="Output structure file path. Accepted formats: pdb.")
138 args = parser.parse_args()
139 config = args.config if args.config else None
140 properties = settings.ConfReader(config=config).get_prop_dic()
142 # Specific call of each building block
143 cat_pdb(input_structure1=args.input_structure1,
144 input_structure2=args.input_structure2,
145 output_structure_path=args.output_structure_path,
146 properties=properties)
149if __name__ == '__main__':
150 main()