Coverage for biobb_amber/nab/nab_build_dna_structure.py: 23%

70 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 08:28 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.configuration import settings 

9from biobb_common.tools import file_utils as fu 

10from biobb_common.tools.file_utils import launchlogger 

11from biobb_amber.nab.common import check_output_path 

12 

13 

14class NabBuildDNAStructure(BiobbObject): 

15 """ 

16 | biobb_amber.nab.nab_build_dna_structure NabBuildDNAStructure 

17 | Wrapper of the `AmberTools (AMBER MD Package) nab tool <https://ambermd.org/AmberTools.php>`_ module. 

18 | Builds a 3D structure from a DNA sequence using nab (Nucleic Acid Builder) tool from the AmberTools MD package. 

19 

20 Args: 

21 output_pdb_path (str): DNA 3D structure PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/nab/ref_nab_build_dna_structure.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

23 * **sequence** (*str*) - ("GCGCGGCTGATAAACGAAAGC") Nucleotide sequence to convert to a 3D structure. Nucleotides should be written in 1-letter code, with no spaces between them. 

24 * **helix_type** (*str*) - ("lbdna") DNA/RNA helix type. Values: arna (Right Handed A-RNA - Arnott), aprna (Right Handed A’-RNA - Arnott), lbdna (Right Handed B-DNA - Langridge), abdna (Right Handed B-DNA - Arnott), sbdna (Left Handed B-DNA - Sasisekharan), adna (Right Handed A-DNA - Arnott). 

25 * **compiler** (*str*) - ("gcc") Alternative C compiler for nab. 

26 * **linker** (*str*) - ("gfortran") Alternative Fortran linker for nab. 

27 * **binary_path** (*str*) - ("nab") Path to the nab executable binary. 

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 * **container_path** (*str*) - (None) Container path definition. 

32 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

33 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

34 * **container_working_dir** (*str*) - (None) Container working directory definition. 

35 * **container_user_id** (*str*) - (None) Container user_id definition. 

36 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

37 

38 Examples: 

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

40 

41 from biobb_amber.nab.nab_build_dna_structure import nab_build_dna_structure 

42 prop = { 

43 'sequence': 'GCGCGGCTGATAAACGAAAGC' 

44 } 

45 nab_build_dna_structure(output_pdb_path='/path/to/newStructure.pdb', 

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: AmberTools nab 

51 * version: >20.9 

52 * license: LGPL 2.1 

53 * ontology: 

54 * name: EDAM 

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

56 

57 """ 

58 

59 def __init__(self, output_pdb_path, properties, **kwargs): 

60 

61 properties = properties or {} 

62 

63 # Call parent class constructor 

64 super().__init__(properties) 

65 self.locals_var_dict = locals().copy() 

66 

67 # Input/Output files 

68 self.io_dict = { 

69 'in': {}, 

70 'out': {'output_pdb_path': output_pdb_path} 

71 } 

72 

73 # Properties specific for BB 

74 self.properties = properties 

75 self.sequence = properties.get('sequence', "GCGCGGCTGATAAACGAAAGC") 

76 self.helix_type = properties.get('helix_type', "lbdna") 

77 self.compiler = properties.get('compiler', "gcc") 

78 self.linker = properties.get('linker', "gfortran") 

79 self.binary_path = properties.get('binary_path', 'nab') 

80 

81 # Check the properties 

82 self.check_properties(properties) 

83 self.check_arguments() 

84 

85 def check_data_params(self, out_log, err_log): 

86 """ Checks input/output paths correctness """ 

87 

88 # Check output(s) 

89 self.io_dict["out"]["output_pdb_path"] = check_output_path(self.io_dict["out"]["output_pdb_path"], "output_pdb_path", False, out_log, self.__class__.__name__) 

90 

91 @launchlogger 

92 def launch(self): 

93 """Launches the execution of the NabBuildDNAStructure module.""" 

94 

95 # check input/output paths and parameters 

96 self.check_data_params(self.out_log, self.err_log) 

97 

98 # Setup Biobb 

99 if self.check_restart(): 

100 return 0 

101 self.stage_files() 

102 

103 # Creating temporary folder 

104 # self.tmp_folder = fu.create_unique_dir() 

105 # fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

106 

107 # create .nab file 

108 # molecule m; 

109 # m = fd_helix( "abdna", "aaaaaaaaaa", "dna" ); 

110 # putpdb( "nuc.pdb", m, "-wwpdb"); 

111 

112 acid_type = 'dna' 

113 if ("rna" in self.helix_type): 

114 acid_type = 'rna' 

115 

116 # Creating temporary folder & Leap configuration (instructions) file 

117 if self.container_path: 

118 instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("nuc.nab")) 

119 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("nuc.nab")) 

120 self.tmp_folder = None 

121 else: 

122 self.tmp_folder = fu.create_unique_dir() 

123 instructions_file = str(PurePath(self.tmp_folder).joinpath("nuc.nab")) 

124 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

125 instructions_file_path = instructions_file 

126 

127 # instructions_file = str(PurePath(self.tmp_folder).joinpath("nuc.nab")) 

128 with open(instructions_file, 'w') as nabin: 

129 nabin.write("molecule m; \n") 

130 nabin.write("m = fd_helix( \"" + self.helix_type + "\", \"" + self.sequence + "\", \"" + acid_type + "\" ); \n") 

131 nabin.write("putpdb( \"" + self.stage_io_dict['out']['output_pdb_path'] + "\" , m, \"-wwpdb\");\n") 

132 

133 # Command line 

134 if self.container_path: 

135 nuc_path = self.container_volume_path 

136 self.cmd = [self.binary_path, 

137 '--compile', self.compiler, 

138 '-Xlinker', self.linker, 

139 instructions_file_path, 

140 ' ; ' + nuc_path + '/nuc' 

141 ] 

142 else: 

143 nuc_path = './' + str(self.tmp_folder) 

144 self.cmd = [self.binary_path, 

145 '--compiler', self.compiler, 

146 '--linker', self.linker, 

147 instructions_file_path, 

148 ' ; ' + nuc_path + '/nuc' 

149 ] 

150 

151 # Run Biobb block 

152 self.run_biobb() 

153 

154 # Copy files to host 

155 self.copy_to_host() 

156 

157 # remove temporary folder(s) 

158 self.tmp_files.extend([ 

159 # self.stage_io_dict.get("unique_dir", ""), 

160 str(self.tmp_folder), 

161 "nab.log", 

162 "tleap.out" 

163 ]) 

164 self.remove_tmp_files() 

165 

166 self.check_arguments(output_files_created=True, raise_exception=False) 

167 

168 return self.return_code 

169 

170 

171def nab_build_dna_structure(output_pdb_path: str, 

172 properties: Optional[dict] = None, **kwargs) -> int: 

173 """Create :class:`NabBuildDNAStructure <nab.nab_build_dna_structure.NabBuildDNAStructure>`nab.nab_build_dna_structure.NabBuildDNAStructure class and 

174 execute :meth:`launch() <nab.nab_build_dna_structure.NabBuildDNAStructure.launch>` method""" 

175 

176 return NabBuildDNAStructure(output_pdb_path=output_pdb_path, 

177 properties=properties).launch() 

178 

179 nab_build_dna_structure.__doc__ = NabBuildDNAStructure.__doc__ 

180 

181 

182def main(): 

183 parser = argparse.ArgumentParser(description='Building a 3D structure from a DNA sequence using nab.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

184 parser.add_argument('--config', required=False, help='Configuration file') 

185 

186 # Specific args 

187 required_args = parser.add_argument_group('required arguments') 

188 required_args.add_argument('--output_pdb_path', required=True, help='Linear (unfolded) 3D structure PDB file. Accepted formats: pdb.') 

189 

190 args = parser.parse_args() 

191 config = args.config if args.config else None 

192 properties = settings.ConfReader(config=config).get_prop_dic() 

193 

194 # Specific call 

195 nab_build_dna_structure(output_pdb_path=args.output_pdb_path, 

196 properties=properties) 

197 

198 

199if __name__ == '__main__': 

200 main()