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

69 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 08:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from pathlib import PurePath 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_amber.nab.common import check_output_path 

11 

12 

13class NabBuildDNAStructure(BiobbObject): 

14 """ 

15 | biobb_amber.nab.nab_build_dna_structure NabBuildDNAStructure 

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

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

18 

19 Args: 

20 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). 

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

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

23 * **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). 

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

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

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

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

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

29 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_amber.nab.nab_build_dna_structure import nab_build_dna_structure 

40 prop = { 

41 'sequence': 'GCGCGGCTGATAAACGAAAGC' 

42 } 

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

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: AmberTools nab 

49 * version: >20.9 

50 * license: LGPL 2.1 

51 * ontology: 

52 * name: EDAM 

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

54 

55 """ 

56 

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

58 

59 properties = properties or {} 

60 

61 # Call parent class constructor 

62 super().__init__(properties) 

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

64 

65 # Input/Output files 

66 self.io_dict = { 

67 'in': {}, 

68 'out': {'output_pdb_path': output_pdb_path} 

69 } 

70 

71 # Properties specific for BB 

72 self.properties = properties 

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

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

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

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

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

78 

79 # Check the properties 

80 self.check_properties(properties) 

81 self.check_arguments() 

82 

83 def check_data_params(self, out_log, err_log): 

84 """ Checks input/output paths correctness """ 

85 

86 # Check output(s) 

87 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__) 

88 

89 @launchlogger 

90 def launch(self): 

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

92 

93 # check input/output paths and parameters 

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

95 

96 # Setup Biobb 

97 if self.check_restart(): 

98 return 0 

99 self.stage_files() 

100 

101 # Creating temporary folder 

102 # self.tmp_folder = fu.create_unique_dir() 

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

104 

105 # create .nab file 

106 # molecule m; 

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

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

109 

110 acid_type = 'dna' 

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

112 acid_type = 'rna' 

113 

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

115 if self.container_path: 

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

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

118 self.tmp_folder = None 

119 else: 

120 self.tmp_folder = fu.create_unique_dir() 

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

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

123 instructions_file_path = instructions_file 

124 

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

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

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

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

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

130 

131 # Command line 

132 if self.container_path: 

133 nuc_path = self.container_volume_path 

134 self.cmd = [self.binary_path, 

135 '--compile', self.compiler, 

136 '-Xlinker', self.linker, 

137 instructions_file_path, 

138 ' ; ' + nuc_path + '/nuc' 

139 ] 

140 else: 

141 nuc_path = './' + self.tmp_folder 

142 self.cmd = [self.binary_path, 

143 '--compiler', self.compiler, 

144 '--linker', self.linker, 

145 instructions_file_path, 

146 ' ; ' + nuc_path + '/nuc' 

147 ] 

148 

149 # Run Biobb block 

150 self.run_biobb() 

151 

152 # Copy files to host 

153 self.copy_to_host() 

154 

155 # remove temporary folder(s) 

156 self.tmp_files.extend([ 

157 self.stage_io_dict.get("unique_dir"), 

158 self.tmp_folder, 

159 "nab.log", 

160 "tleap.out" 

161 ]) 

162 self.remove_tmp_files() 

163 

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

165 

166 return self.return_code 

167 

168 

169def nab_build_dna_structure(output_pdb_path: str, 

170 properties: dict = None, **kwargs) -> int: 

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

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

173 

174 return NabBuildDNAStructure(output_pdb_path=output_pdb_path, 

175 properties=properties).launch() 

176 

177 

178def main(): 

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

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

181 

182 # Specific args 

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

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

185 

186 args = parser.parse_args() 

187 config = args.config if args.config else None 

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

189 

190 # Specific call 

191 nab_build_dna_structure(output_pdb_path=args.output_pdb_path, 

192 properties=properties) 

193 

194 

195if __name__ == '__main__': 

196 main()