Coverage for biobb_chemistry/acpype/common.py: 86%

108 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 09:28 +0000

1""" Common functions for package biobb_chemistry.acpype """ 

2from pathlib import Path, PurePath 

3import glob 

4import shutil 

5import string 

6import random 

7import fileinput 

8from biobb_common.tools import file_utils as fu 

9 

10 

11def check_input_path(path, out_log, classname): 

12 """ Checks input file """ 

13 if not Path(path).exists(): 

14 fu.log(classname + ': Unexisting input file, exiting', out_log) 

15 raise SystemExit(classname + ': Unexisting input file') 

16 file_extension = PurePath(path).suffix 

17 if not is_valid_input(file_extension[1:]): 

18 fu.log(classname + ': Format %s in input file is not compatible' % file_extension[1:], out_log) 

19 raise SystemExit(classname + ': Format %s in input file is not compatible' % file_extension[1:]) 

20 if (PurePath(path).name == path or not PurePath(path).is_absolute()): 

21 path = str(PurePath(Path.cwd()).joinpath(path)) 

22 

23 return path 

24 

25 

26def check_output_path(path, type, out_log, classname): 

27 """ Checks output path """ 

28 if PurePath(path).parent and not Path(PurePath(path).parent).exists(): 

29 fu.log(classname + ': Unexisting output %s output folder, exiting' % type, out_log) 

30 raise SystemExit(classname + ': Unexisting %s output folder' % type) 

31 file_extension = PurePath(path).suffix 

32 if type != file_extension[1:]: 

33 fu.log(classname + ': Format %s in %s input file is not compatible' % (file_extension[1:], type), out_log) 

34 raise SystemExit(classname + ': Format %s in %s input file is not compatible' % (file_extension[1:], type)) 

35 

36 return path 

37 

38 

39def get_binary_path(properties, type): 

40 """ Gets binary path """ 

41 return properties.get(type, get_default_value(type)) 

42 

43 

44def get_basename(basename, out_log): 

45 """ Checks if provided basename value is correct """ 

46 bsn = str(basename) 

47 if basename == '': 

48 fu.log('No basename provided, default value %s assigned' % get_default_value('basename'), out_log) 

49 bsn = get_default_value('basename') 

50 

51 return bsn 

52 

53 

54def get_charge(charge, out_log): 

55 """ Checks if provided charge value is correct """ 

56 ch = charge 

57 if ch is None or ch == '': 

58 fu.log('Charge will be guessed by acpype.', out_log) 

59 return ch 

60 

61 if not isinstance(ch, (int, None)): # type: ignore 

62 fu.log('Value %s is not compatible as a charge value, default value %d assigned' % (ch, get_default_value('charge')), out_log) 

63 ch = get_default_value('charge') 

64 

65 return str(ch) 

66 

67 

68def create_unique_name(length=10, char=string.ascii_uppercase + string.digits + string.ascii_lowercase): 

69 return ''.join(random.choice(char) for x in range(length)) 

70 

71 

72def get_default_value(key): 

73 """ Gives default values according to the given key """ 

74 default_values = { 

75 "charge": 0, 

76 "basename": "BBB", 

77 "binary_path": "acpype", 

78 "AcpypeParamsGMX": { 

79 "topology": "GROMACS", 

80 "suffix": "_GMX." 

81 }, 

82 "AcpypeParamsAC": { 

83 "topology": "Antechamber", 

84 "suffix": "_AC." 

85 }, 

86 "AcpypeParamsGMXOPLS": { 

87 "topology": "OPLS/AA", 

88 "suffix": "_GMX_OPLS." 

89 }, 

90 "AcpypeParamsCNS": { 

91 "topology": "GROMACS", 

92 "suffix": "_CNS." 

93 }, 

94 "AcpypeConvertAMBERtoGMX": { 

95 "topology": "GMX", 

96 "suffix": "_GMX" 

97 } 

98 } 

99 

100 return default_values[key] 

101 

102 

103def is_valid_input(ext): 

104 """ Checks if input file format is compatible with Acpype """ 

105 formats = ["pdb", "mdl", "mol2"] 

106 

107 return ext in formats 

108 

109 

110def process_output(unique_name, files_folder, remove_tmp, basename, class_params, output_files, out_log): 

111 """ Moves and removes temporal files generated by the wrapper """ 

112 path = files_folder 

113 suffix = class_params['suffix'] 

114 src_files = glob.glob(path + '/' + basename + '.' + unique_name + suffix + '*') 

115 

116 # copy files for the requested topology to the output_path 

117 for file_name in src_files: 

118 # replace random name by original name in all files 

119 with fileinput.FileInput(file_name, inplace=True) as file: 

120 for line in file: 

121 print(line.replace(basename + '.' + unique_name, basename), end='') 

122 

123 if (Path(file_name).is_file()): 

124 file_extension = PurePath(file_name).suffix 

125 shutil.copy(file_name, output_files[file_extension[1:]]) 

126 fu.log('File %s succesfully created' % output_files[file_extension[1:]], out_log) 

127 

128 if remove_tmp: 

129 # remove temporary folder 

130 fu.rm(files_folder) 

131 fu.log('Removed temporary folder: %s' % files_folder, out_log) 

132 

133 

134def process_output_gmx(unique_name, files_folder, remove_tmp, basename, class_params, output_files, out_log): 

135 """ Moves and removes temporal files generated by the wrapper """ 

136 path = files_folder 

137 suffix = class_params['suffix'] 

138 src_files = glob.glob(path + '/' + basename + '.' + unique_name + suffix + '*') 

139 

140 # copy files for the requested topology to the output_path 

141 for file_name in src_files: 

142 # replace random name by original name in all files 

143 with fileinput.FileInput(file_name, inplace=True) as file: 

144 for line in file: 

145 print(line.replace(basename + '.' + unique_name, basename), end='') 

146 

147 if (Path(file_name).is_file()): 

148 file_extension = PurePath(file_name).suffix 

149 # in top files for gromacs, replace file.itp by name given by user 

150 if (file_extension[1:] == 'top') and ('itp' in output_files): 

151 with open(file_name) as f: 

152 newText = f.read().replace(basename + '_GMX.itp', PurePath(output_files['itp']).name) 

153 with open(file_name, "w") as f: 

154 f.write(newText) 

155 shutil.copy(file_name, output_files[file_extension[1:]]) 

156 fu.log('File %s succesfully created' % output_files[file_extension[1:]], out_log) 

157 

158 

159def process_output_cns(unique_name, files_folder, remove_tmp, basename, class_params, output_files, out_log): 

160 """ Moves and removes temporal files generated by the wrapper """ 

161 path = files_folder 

162 suffix = class_params['suffix'] 

163 src_files = glob.glob(path + '/' + basename + '.' + unique_name + suffix + '*') 

164 

165 # copy files for the requested topology to the output_path 

166 for file_name in src_files: 

167 # replace random name by original name in all files 

168 with fileinput.FileInput(file_name, inplace=True) as file: 

169 for line in file: 

170 print(line.replace(basename + '.' + unique_name, basename), end='') 

171 

172 if (Path(file_name).is_file()): 

173 file_extension = PurePath(file_name).suffix 

174 fu.log('Files: %s' % str(file_name), out_log) 

175 shutil.copy(file_name, output_files[file_extension[1:]]) 

176 fu.log('File %s succesfully created' % output_files[file_extension[1:]], out_log) 

177 

178 file_extension = ".pdb" 

179 file_name = path + '/' + basename + '.' + unique_name + "_NEW.pdb" 

180 with open(file_name) as f: 

181 newText = f.read().replace(basename + '_NEW.pdb', PurePath(output_files['pdb']).name) 

182 with open(file_name, "w") as f: 

183 f.write(newText) 

184 shutil.copy(file_name, output_files[file_extension[1:]]) 

185 fu.log('File %s succesfully created' % output_files[file_extension[1:]], out_log) 

186 

187 if remove_tmp: 

188 # remove temporary folder 

189 fu.rm(files_folder) 

190 fu.log('Removed temporary folder: %s' % files_folder, out_log)