- 1""" Common functions for package biobb_analysis.gromacs """
- 2from pathlib import Path, PurePath
+ 1"""Common functions for package biobb_analysis.gromacs"""
+ 2
3import re
4import shutil
- 5from biobb_common.tools import file_utils as fu
- 6from biobb_common.command_wrapper import cmd_wrapper
+ 5from pathlib import Path, PurePath
+ 6from typing import Optional, Union
7
- 8
- 9def gmx_check(file_a: str, file_b: str, gmx: str = 'gmx') -> bool:
- 10 print("Comparing GROMACS files:")
- 11 print("FILE_A: %s" % str(Path(file_a).resolve()))
- 12 print("FILE_B: %s" % str(Path(file_b).resolve()))
- 13 check_result = 'check_result.out'
- 14 cmd = [gmx, 'check']
- 15 if file_a.endswith(".tpr"):
- 16 cmd.append('-s1')
- 17 else:
- 18 cmd.append('-f')
- 19 cmd.append(file_a)
- 20 if file_b.endswith(".tpr"):
- 21 cmd.append('-s2')
- 22 else:
- 23 cmd.append('-f2')
- 24 cmd.append(file_b)
- 25 cmd.append('> check_result.out')
- 26 cmd_wrapper.CmdWrapper(cmd).launch()
- 27 print("Result file: %s" % str(Path(check_result).resolve()))
- 28 with open(check_result) as check_file:
- 29 for line_num, line in enumerate(check_file):
- 30 if not line.rstrip():
- 31 continue
- 32 if line.startswith("Both files read correctly"):
- 33 continue
- 34 if not line.startswith('comparing'):
- 35 print('Discrepance found in line %d: %s' % (line_num, line))
- 36 return False
- 37 return True
- 38
- 39
- 40def check_energy_path(path, out_log, classname):
- 41 """ Checks energy input file """
- 42 if not Path(path).exists():
- 43 fu.log(classname + ': Unexisting energy input file, exiting', out_log)
- 44 raise SystemExit(classname + ': Unexisting energy input file')
- 45 file_extension = PurePath(path).suffix
- 46 if not is_valid_energy(file_extension[1:]):
- 47 fu.log(classname + ': Format %s in energy input file is not compatible' % file_extension[1:], out_log)
- 48 raise SystemExit(classname + ': Format %s in energy input file is not compatible' % file_extension[1:])
- 49 # if file input has no path, add cwd because execution is launched on tmp folder
- 50 if (PurePath(path).name == path or not PurePath(path).is_absolute()):
- 51 path = str(PurePath(Path.cwd()).joinpath(path))
- 52 return path
- 53
- 54
- 55def check_input_path(path, out_log, classname):
- 56 """ Checks input structure file """
- 57 if not Path(path).exists():
- 58 fu.log(classname + ': Unexisting structure input file, exiting', out_log)
- 59 raise SystemExit(classname + ': Unexisting structure input file')
- 60 file_extension = PurePath(path).suffix
- 61 if not is_valid_structure(file_extension[1:]):
- 62 fu.log(classname + ': Format %s in structure input file is not compatible' % file_extension[1:], out_log)
- 63 raise SystemExit(classname + ': Format %s in structure input file is not compatible' % file_extension[1:])
- 64 # if file input has no path, add cwd because execution is launched on tmp folder
- 65 if (PurePath(path).name == path or not PurePath(path).is_absolute()):
- 66 path = str(PurePath(Path.cwd()).joinpath(path))
- 67 return path
- 68
- 69
- 70def check_index_path(path, out_log, classname):
- 71 """ Checks index input file """
- 72 if not path:
- 73 return None
- 74 file_extension = PurePath(path).suffix
- 75 if not is_valid_index(file_extension[1:]):
- 76 fu.log(classname + ': Format %s in index input file is not compatible' % file_extension[1:], out_log)
- 77 raise SystemExit(classname + ': Format %s in index input file is not compatible' % file_extension[1:])
- 78 # if file input has no path, add cwd because execution is launched on tmp folder
- 79 if (PurePath(path).name == path or not PurePath(path).is_absolute()):
- 80 path = str(PurePath(Path.cwd()).joinpath(path))
- 81 return path
- 82
- 83
- 84def check_traj_path(path, out_log, classname):
- 85 """ Checks input structure file """
- 86 if not Path(path).exists():
- 87 fu.log(classname + ': Unexisting trajectory input file, exiting', out_log)
- 88 raise SystemExit(classname + ': Unexisting trajectory input file')
- 89 file_extension = PurePath(path).suffix
- 90 if not is_valid_trajectory(file_extension[1:]):
- 91 fu.log(classname + ': Format %s in trajectory input file is not compatible' % file_extension[1:], out_log)
- 92 raise SystemExit(classname + ': Format %s in trajectory input file is not compatible' % file_extension[1:])
- 93 # if file input has no path, add cwd because execution is launched on tmp folder
- 94 if (PurePath(path).name == path or not PurePath(path).is_absolute()):
- 95 path = str(PurePath(Path.cwd()).joinpath(path))
- 96 return path
- 97
- 98
- 99def check_out_xvg_path(path, out_log, classname):
- 100 """ Checks if output folder exists and format is xvg """
- 101 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
- 102 fu.log(classname + ': Unexisting output folder, exiting', out_log)
- 103 raise SystemExit(classname + ': Unexisting output folder')
- 104 file_extension = PurePath(path).suffix
- 105 if not is_valid_xvg(file_extension[1:]):
- 106 fu.log(classname + ': Format %s in output file is not compatible' % file_extension[1:], out_log)
- 107 raise SystemExit(classname + ': Format %s in output file is not compatible' % file_extension[1:])
- 108 return path
+ 8from biobb_common.command_wrapper import cmd_wrapper
+ 9from biobb_common.tools import file_utils as fu
+ 10
+ 11
+ 12def gmx_check(file_a: str, file_b: str, gmx: str = "gmx") -> bool:
+ 13 print("Comparing GROMACS files:")
+ 14 print("FILE_A: %s" % str(Path(file_a).resolve()))
+ 15 print("FILE_B: %s" % str(Path(file_b).resolve()))
+ 16 check_result = "check_result.out"
+ 17 cmd = [gmx, "check"]
+ 18 if file_a.endswith(".tpr"):
+ 19 cmd.append("-s1")
+ 20 else:
+ 21 cmd.append("-f")
+ 22 cmd.append(file_a)
+ 23 if file_b.endswith(".tpr"):
+ 24 cmd.append("-s2")
+ 25 else:
+ 26 cmd.append("-f2")
+ 27 cmd.append(file_b)
+ 28 cmd.append("> check_result.out")
+ 29 cmd_wrapper.CmdWrapper(cmd).launch()
+ 30 print("Result file: %s" % str(Path(check_result).resolve()))
+ 31 with open(check_result) as check_file:
+ 32 for line_num, line in enumerate(check_file):
+ 33 if not line.rstrip():
+ 34 continue
+ 35 if line.startswith("Both files read correctly"):
+ 36 continue
+ 37 if not line.startswith("comparing"):
+ 38 print("Discrepance found in line %d: %s" % (line_num, line))
+ 39 return False
+ 40 return True
+ 41
+ 42
+ 43def check_energy_path(path, out_log, classname):
+ 44 """Checks energy input file"""
+ 45 if not Path(path).exists():
+ 46 fu.log(classname + ": Unexisting energy input file, exiting", out_log)
+ 47 raise SystemExit(classname + ": Unexisting energy input file")
+ 48 file_extension = PurePath(path).suffix
+ 49 if not is_valid_energy(file_extension[1:]):
+ 50 fu.log(
+ 51 classname
+ 52 + ": Format %s in energy input file is not compatible" % file_extension[1:],
+ 53 out_log,
+ 54 )
+ 55 raise SystemExit(
+ 56 classname
+ 57 + ": Format %s in energy input file is not compatible" % file_extension[1:]
+ 58 )
+ 59 # if file input has no path, add cwd because execution is launched on tmp folder
+ 60 if PurePath(path).name == path or not PurePath(path).is_absolute():
+ 61 path = str(PurePath(Path.cwd()).joinpath(path))
+ 62 return path
+ 63
+ 64
+ 65def check_input_path(path, out_log, classname):
+ 66 """Checks input structure file"""
+ 67 if not Path(path).exists():
+ 68 fu.log(classname + ": Unexisting structure input file, exiting", out_log)
+ 69 raise SystemExit(classname + ": Unexisting structure input file")
+ 70 file_extension = PurePath(path).suffix
+ 71 if not is_valid_structure(file_extension[1:]):
+ 72 fu.log(
+ 73 classname
+ 74 + ": Format %s in structure input file is not compatible"
+ 75 % file_extension[1:],
+ 76 out_log,
+ 77 )
+ 78 raise SystemExit(
+ 79 classname
+ 80 + ": Format %s in structure input file is not compatible"
+ 81 % file_extension[1:]
+ 82 )
+ 83 # if file input has no path, add cwd because execution is launched on tmp folder
+ 84 if PurePath(path).name == path or not PurePath(path).is_absolute():
+ 85 path = str(PurePath(Path.cwd()).joinpath(path))
+ 86 return path
+ 87
+ 88
+ 89def check_index_path(path, out_log, classname):
+ 90 """Checks index input file"""
+ 91 if not path:
+ 92 return None
+ 93 file_extension = PurePath(path).suffix
+ 94 if not is_valid_index(file_extension[1:]):
+ 95 fu.log(
+ 96 classname
+ 97 + ": Format %s in index input file is not compatible" % file_extension[1:],
+ 98 out_log,
+ 99 )
+ 100 raise SystemExit(
+ 101 classname
+ 102 + ": Format %s in index input file is not compatible" % file_extension[1:]
+ 103 )
+ 104 # if file input has no path, add cwd because execution is launched on tmp folder
+ 105 if PurePath(path).name == path or not PurePath(path).is_absolute():
+ 106 path = str(PurePath(Path.cwd()).joinpath(path))
+ 107 return path
+ 108
109
- 110
- 111def check_out_pdb_path(path, out_log, classname):
- 112 """ Checks if output folder exists and format is xvg """
- 113 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
- 114 fu.log(classname + ': Unexisting output folder, exiting', out_log)
- 115 raise SystemExit(classname + ': Unexisting output folder')
- 116 file_extension = PurePath(path).suffix
- 117 if not is_valid_structure(file_extension[1:]):
- 118 fu.log(classname + ': Format %s in output file is not compatible' % file_extension[1:], out_log)
- 119 raise SystemExit(classname + ': Format %s in output file is not compatible' % file_extension[1:])
- 120 return path
- 121
- 122
- 123def check_out_traj_path(path, out_log, classname):
- 124 """ Checks if output folder exists and format is correct """
- 125 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
- 126 fu.log(classname + ': Unexisting output folder, exiting', out_log)
- 127 raise SystemExit(classname + ': Unexisting output folder')
- 128 file_extension = PurePath(path).suffix
- 129 if not is_valid_trajectory_output(file_extension[1:]):
- 130 fu.log(classname + ': Format %s in output file is not compatible' % file_extension[1:], out_log)
- 131 raise SystemExit(classname + ': Format %s in output file is not compatible' % file_extension[1:])
- 132 return path
+ 110def check_traj_path(path, out_log, classname):
+ 111 """Checks input structure file"""
+ 112 if not Path(path).exists():
+ 113 fu.log(classname + ": Unexisting trajectory input file, exiting", out_log)
+ 114 raise SystemExit(classname + ": Unexisting trajectory input file")
+ 115 file_extension = PurePath(path).suffix
+ 116 if not is_valid_trajectory(file_extension[1:]):
+ 117 fu.log(
+ 118 classname
+ 119 + ": Format %s in trajectory input file is not compatible"
+ 120 % file_extension[1:],
+ 121 out_log,
+ 122 )
+ 123 raise SystemExit(
+ 124 classname
+ 125 + ": Format %s in trajectory input file is not compatible"
+ 126 % file_extension[1:]
+ 127 )
+ 128 # if file input has no path, add cwd because execution is launched on tmp folder
+ 129 if PurePath(path).name == path or not PurePath(path).is_absolute():
+ 130 path = str(PurePath(Path.cwd()).joinpath(path))
+ 131 return path
+ 132
133
- 134
- 135def check_out_str_ens_path(path, out_log, classname):
- 136 """ Checks if output folder exists and format is correct """
- 137 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
- 138 fu.log(classname + ': Unexisting output folder, exiting', out_log)
- 139 raise SystemExit(classname + ': Unexisting output folder')
- 140 file_extension = PurePath(path).suffix
- 141 if not is_valid_zip(file_extension[1:]):
- 142 fu.log(classname + ': Format %s in output file is not compatible' % file_extension[1:], out_log)
- 143 raise SystemExit(classname + ': Format %s in output file is not compatible' % file_extension[1:])
- 144 return path
- 145
- 146
- 147def get_default_value(key):
- 148 """ Gives default values according to the given key """
- 149
- 150 default_values = {"instructions_file": "instructions.in",
- 151 "binary_path": "gmx",
- 152 "terms": ["Potential"],
- 153 "selection": "System",
- 154 "xvg": "none",
- 155 "dista": False,
- 156 "method": "linkage",
- 157 "cutoff": 0.1,
- 158 "cluster_selection": "System",
- 159 "fit_selection": "System",
- 160 "center_selection": "System",
- 161 "output_selection": "System",
- 162 "pbc": "mol",
- 163 "center": True,
- 164 "fit": "none",
- 165 "ur": "compact",
- 166 "skip": 1,
- 167 "start": 0,
- 168 "end": 0,
- 169 "dt": 0,
- 170 "ot_str_ens": "pdb"}
+ 134def check_out_xvg_path(path, out_log, classname):
+ 135 """Checks if output folder exists and format is xvg"""
+ 136 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
+ 137 fu.log(classname + ": Unexisting output folder, exiting", out_log)
+ 138 raise SystemExit(classname + ": Unexisting output folder")
+ 139 file_extension = PurePath(path).suffix
+ 140 if not is_valid_xvg(file_extension[1:]):
+ 141 fu.log(
+ 142 classname
+ 143 + ": Format %s in output file is not compatible" % file_extension[1:],
+ 144 out_log,
+ 145 )
+ 146 raise SystemExit(
+ 147 classname
+ 148 + ": Format %s in output file is not compatible" % file_extension[1:]
+ 149 )
+ 150 return path
+ 151
+ 152
+ 153def check_out_pdb_path(path, out_log, classname):
+ 154 """Checks if output folder exists and format is xvg"""
+ 155 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
+ 156 fu.log(classname + ": Unexisting output folder, exiting", out_log)
+ 157 raise SystemExit(classname + ": Unexisting output folder")
+ 158 file_extension = PurePath(path).suffix
+ 159 if not is_valid_structure(file_extension[1:]):
+ 160 fu.log(
+ 161 classname
+ 162 + ": Format %s in output file is not compatible" % file_extension[1:],
+ 163 out_log,
+ 164 )
+ 165 raise SystemExit(
+ 166 classname
+ 167 + ": Format %s in output file is not compatible" % file_extension[1:]
+ 168 )
+ 169 return path
+ 170
171
- 172 return default_values[key]
- 173
- 174
- 175def get_binary_path(properties, type):
- 176 """ Gets binary path """
- 177 return properties.get(type, get_default_value(type))
- 178
- 179
- 180def get_terms(properties, out_log, classname):
- 181 """ Gets energy terms """
- 182 terms = properties.get('terms', dict())
- 183 if not terms or not isinstance(terms, list):
- 184 fu.log(classname + ': No terms provided or incorrect format, exiting', out_log)
- 185 raise SystemExit(classname + ': No terms provided or incorrect format')
- 186 if not is_valid_term(terms):
- 187 fu.log(classname + ': Incorrect terms provided, exiting', out_log)
- 188 raise SystemExit(classname + ': Incorrect terms provided')
- 189 return properties.get('terms', '')
+ 172def check_out_traj_path(path, out_log, classname):
+ 173 """Checks if output folder exists and format is correct"""
+ 174 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
+ 175 fu.log(classname + ": Unexisting output folder, exiting", out_log)
+ 176 raise SystemExit(classname + ": Unexisting output folder")
+ 177 file_extension = PurePath(path).suffix
+ 178 if not is_valid_trajectory_output(file_extension[1:]):
+ 179 fu.log(
+ 180 classname
+ 181 + ": Format %s in output file is not compatible" % file_extension[1:],
+ 182 out_log,
+ 183 )
+ 184 raise SystemExit(
+ 185 classname
+ 186 + ": Format %s in output file is not compatible" % file_extension[1:]
+ 187 )
+ 188 return path
+ 189
190
- 191
- 192def get_selection(properties, out_log, classname):
- 193 """ Gets selection items """
- 194 selection = properties.get('selection', get_default_value('selection'))
- 195 if not selection:
- 196 fu.log(classname + ': No selection provided or incorrect format, exiting', out_log)
- 197 raise SystemExit(classname + ': No selection provided or incorrect format')
- 198 if not is_valid_selection(selection):
- 199 fu.log(classname + ': Incorrect selection provided, exiting', out_log)
- 200 raise SystemExit(classname + ': Incorrect selection provided')
- 201 return selection
- 202
- 203
- 204def get_image_selection(properties, key, out_log, classname):
- 205 """ Gets selection items """
- 206 selection = properties.get(key, get_default_value(key))
- 207 if not selection:
- 208 fu.log(classname + ': No selection provided or incorrect format, exiting', out_log)
- 209 raise SystemExit(classname + ': No selection provided or incorrect format')
- 210 if not is_valid_selection(selection):
- 211 fu.log(classname + ': Incorrect selection provided, exiting', out_log)
- 212 raise SystemExit(classname + ': Incorrect selection provided')
- 213 return selection
- 214
- 215
- 216def get_selection_index_file(properties, index, key, out_log, classname):
- 217 """ Gets selection items from provided index file """
- 218 pattern = re.compile(r"\[.*\]")
- 219 selection = []
- 220 with open(index, "r") as ndx_file:
- 221 for i, line in enumerate(ndx_file):
- 222 for match in re.finditer(pattern, line):
- 223 selection.append(re.sub(r'[\[\] ]', '', match.group()))
- 224 sel = properties.get(key, get_default_value(key))
- 225 if sel not in selection:
- 226 fu.log(classname + ': Incorrect selection provided, exiting', out_log)
- 227 raise SystemExit(classname + ': Incorrect selection provided')
- 228 return sel
- 229
- 230
- 231def get_pbc(properties, out_log, classname):
- 232 """ Gets pbc """
- 233 pbc = properties.get('pbc', get_default_value('pbc'))
- 234 if not is_valid_pbc(pbc):
- 235 fu.log(classname + ': Incorrect pbc provided, exiting', out_log)
- 236 raise SystemExit(classname + ': Incorrect pbc provided')
- 237 return pbc
+ 191def check_out_str_ens_path(path, out_log, classname):
+ 192 """Checks if output folder exists and format is correct"""
+ 193 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
+ 194 fu.log(classname + ": Unexisting output folder, exiting", out_log)
+ 195 raise SystemExit(classname + ": Unexisting output folder")
+ 196 file_extension = PurePath(path).suffix
+ 197 if not is_valid_zip(file_extension[1:]):
+ 198 fu.log(
+ 199 classname
+ 200 + ": Format %s in output file is not compatible" % file_extension[1:],
+ 201 out_log,
+ 202 )
+ 203 raise SystemExit(
+ 204 classname
+ 205 + ": Format %s in output file is not compatible" % file_extension[1:]
+ 206 )
+ 207 return path
+ 208
+ 209
+ 210def get_default_value(key):
+ 211 """Gives default values according to the given key"""
+ 212
+ 213 default_values = {
+ 214 "instructions_file": "instructions.in",
+ 215 "binary_path": "gmx",
+ 216 "terms": ["Potential"],
+ 217 "selection": "System",
+ 218 "xvg": "none",
+ 219 "dista": False,
+ 220 "method": "linkage",
+ 221 "cutoff": 0.1,
+ 222 "cluster_selection": "System",
+ 223 "fit_selection": "System",
+ 224 "center_selection": "System",
+ 225 "output_selection": "System",
+ 226 "pbc": "mol",
+ 227 "center": True,
+ 228 "fit": "none",
+ 229 "ur": "compact",
+ 230 "skip": 1,
+ 231 "start": 0,
+ 232 "end": 0,
+ 233 "dt": 0,
+ 234 "ot_str_ens": "pdb",
+ 235 }
+ 236
+ 237 return default_values[key]
238
239
- 240def get_center(properties, out_log, classname):
- 241 """ Gets center """
- 242 center = properties.get('center', get_default_value('center'))
- 243 if not is_valid_boolean(center):
- 244 fu.log(classname + ': Incorrect center provided, exiting', out_log)
- 245 raise SystemExit(classname + ': Incorrect center provided')
- 246 return center
- 247
- 248
- 249def get_ur(properties, out_log, classname):
- 250 """ Gets ur """
- 251 ur = properties.get('ur', get_default_value('ur'))
- 252 if not is_valid_ur(ur):
- 253 fu.log(classname + ': Incorrect ur provided, exiting', out_log)
- 254 raise SystemExit(classname + ': Incorrect ur provided')
- 255 return ur
+ 240def get_binary_path(properties, type):
+ 241 """Gets binary path"""
+ 242 return properties.get(type, get_default_value(type))
+ 243
+ 244
+ 245def get_terms(properties, out_log, classname):
+ 246 """Gets energy terms"""
+ 247 terms = properties.get("terms", dict())
+ 248 if not terms or not isinstance(terms, list):
+ 249 fu.log(classname + ": No terms provided or incorrect format, exiting", out_log)
+ 250 raise SystemExit(classname + ": No terms provided or incorrect format")
+ 251 if not is_valid_term(terms):
+ 252 fu.log(classname + ": Incorrect terms provided, exiting", out_log)
+ 253 raise SystemExit(classname + ": Incorrect terms provided")
+ 254 return properties.get("terms", "")
+ 255
256
- 257
- 258def get_fit(properties, out_log, classname):
- 259 """ Gets fit """
- 260 fit = properties.get('fit', get_default_value('fit'))
- 261 if not is_valid_fit(fit):
- 262 fu.log(classname + ': Incorrect fit provided, exiting', out_log)
- 263 raise SystemExit(classname + ': Incorrect fit provided')
- 264 return fit
- 265
- 266
- 267def get_skip(properties, out_log, classname):
- 268 """ Gets skip """
- 269 skip = properties.get('skip', get_default_value('skip'))
- 270 if not is_valid_int(skip):
- 271 fu.log(classname + ': Incorrect skip provided, exiting', out_log)
- 272 raise SystemExit(classname + ': Incorrect start provided')
- 273 return str(skip)
- 274
- 275
- 276def get_start(properties, out_log, classname):
- 277 """ Gets start """
- 278 start = properties.get('start', get_default_value('start'))
- 279 if not is_valid_int(start):
- 280 fu.log(classname + ': Incorrect start provided, exiting', out_log)
- 281 raise SystemExit(classname + ': Incorrect start provided')
- 282 return str(start)
+ 257def get_selection(properties, out_log, classname):
+ 258 """Gets selection items"""
+ 259 selection = properties.get("selection", get_default_value("selection"))
+ 260 if not selection:
+ 261 fu.log(
+ 262 classname + ": No selection provided or incorrect format, exiting", out_log
+ 263 )
+ 264 raise SystemExit(classname + ": No selection provided or incorrect format")
+ 265 if not is_valid_selection(selection):
+ 266 fu.log(classname + ": Incorrect selection provided, exiting", out_log)
+ 267 raise SystemExit(classname + ": Incorrect selection provided")
+ 268 return selection
+ 269
+ 270
+ 271def get_image_selection(properties, key, out_log, classname):
+ 272 """Gets selection items"""
+ 273 selection = properties.get(key, get_default_value(key))
+ 274 if not selection:
+ 275 fu.log(
+ 276 classname + ": No selection provided or incorrect format, exiting", out_log
+ 277 )
+ 278 raise SystemExit(classname + ": No selection provided or incorrect format")
+ 279 if not is_valid_selection(selection):
+ 280 fu.log(classname + ": Incorrect selection provided, exiting", out_log)
+ 281 raise SystemExit(classname + ": Incorrect selection provided")
+ 282 return selection
283
284
- 285def get_end(properties, out_log, classname):
- 286 """ Gets end """
- 287 end = properties.get('end', get_default_value('end'))
- 288 if not is_valid_int(end):
- 289 fu.log(classname + ': Incorrect end provided, exiting', out_log)
- 290 raise SystemExit(classname + ': Incorrect end provided')
- 291 return str(end)
- 292
- 293
- 294def get_dt(properties, out_log, classname):
- 295 """ Gets dt """
- 296 dt = properties.get('dt', get_default_value('dt'))
- 297 if not is_valid_int(dt):
- 298 fu.log(classname + ': Incorrect dt provided, exiting', out_log)
- 299 raise SystemExit(classname + ': Incorrect dt provided')
- 300 return str(dt)
- 301
- 302
- 303def get_ot_str_ens(properties, out_log, classname):
- 304 """ Gets output type """
- 305 output_type = properties.get('output_type', get_default_value('ot_str_ens'))
- 306 if not is_valid_ot_str_ens(output_type):
- 307 fu.log(classname + ': Incorrect output_type provided, exiting', out_log)
- 308 raise SystemExit(classname + ': Incorrect output_type provided')
- 309 return str(output_type)
- 310
- 311
- 312def get_xvg(properties, out_log, classname):
- 313 """ Gets xvg """
- 314 xvg = properties.get('xvg', get_default_value('xvg'))
- 315 if not is_valid_xvg_param(xvg):
- 316 fu.log(classname + ': Incorrect xvg provided, exiting', out_log)
- 317 raise SystemExit(classname + ': Incorrect xvg provided')
- 318 return xvg
- 319
- 320
- 321def get_dista(properties, out_log, classname):
- 322 """ Gets dista """
- 323 dista = properties.get('dista', get_default_value('dista'))
- 324 if not is_valid_boolean(dista):
- 325 fu.log(classname + ': Incorrect dista provided, exiting', out_log)
- 326 raise SystemExit(classname + ': Incorrect dista provided')
- 327 return dista
- 328
- 329
- 330def get_method(properties, out_log, classname):
- 331 """ Gets method """
- 332 method = properties.get('method', get_default_value('method'))
- 333 if not is_valid_method_param(method):
- 334 fu.log(classname + ': Incorrect method provided, exiting', out_log)
- 335 raise SystemExit(classname + ': Incorrect method provided')
- 336 return method
- 337
- 338
- 339def get_cutoff(properties, out_log, classname):
- 340 """ Gets cutoff """
- 341 cutoff = properties.get('cutoff', get_default_value('cutoff'))
- 342 if not is_valid_float(cutoff):
- 343 fu.log(classname + ': Incorrect cutoff provided, exiting', out_log)
- 344 raise SystemExit(classname + ': Incorrect cutoff provided')
- 345 return str(cutoff)
- 346
- 347
- 348def is_valid_boolean(val):
- 349 """ Checks if given value is boolean """
- 350 values = [True, False]
- 351 return val in values
+ 285def get_selection_index_file(properties, index, key, out_log, classname):
+ 286 """Gets selection items from provided index file"""
+ 287 pattern = re.compile(r"\[.*\]")
+ 288 selection = []
+ 289 with open(index, "r") as ndx_file:
+ 290 for i, line in enumerate(ndx_file):
+ 291 for match in re.finditer(pattern, line):
+ 292 selection.append(re.sub(r"[\[\] ]", "", match.group()))
+ 293 sel = properties.get(key, get_default_value(key))
+ 294 if sel not in selection:
+ 295 fu.log(classname + ": Incorrect selection provided, exiting", out_log)
+ 296 raise SystemExit(classname + ": Incorrect selection provided")
+ 297 return sel
+ 298
+ 299
+ 300def get_pbc(properties, out_log, classname):
+ 301 """Gets pbc"""
+ 302 pbc = properties.get("pbc", get_default_value("pbc"))
+ 303 if not is_valid_pbc(pbc):
+ 304 fu.log(classname + ": Incorrect pbc provided, exiting", out_log)
+ 305 raise SystemExit(classname + ": Incorrect pbc provided")
+ 306 return pbc
+ 307
+ 308
+ 309def get_center(properties, out_log, classname):
+ 310 """Gets center"""
+ 311 center = properties.get("center", get_default_value("center"))
+ 312 if not is_valid_boolean(center):
+ 313 fu.log(classname + ": Incorrect center provided, exiting", out_log)
+ 314 raise SystemExit(classname + ": Incorrect center provided")
+ 315 return center
+ 316
+ 317
+ 318def get_ur(properties, out_log, classname):
+ 319 """Gets ur"""
+ 320 ur = properties.get("ur", get_default_value("ur"))
+ 321 if not is_valid_ur(ur):
+ 322 fu.log(classname + ": Incorrect ur provided, exiting", out_log)
+ 323 raise SystemExit(classname + ": Incorrect ur provided")
+ 324 return ur
+ 325
+ 326
+ 327def get_fit(properties, out_log, classname):
+ 328 """Gets fit"""
+ 329 fit = properties.get("fit", get_default_value("fit"))
+ 330 if not is_valid_fit(fit):
+ 331 fu.log(classname + ": Incorrect fit provided, exiting", out_log)
+ 332 raise SystemExit(classname + ": Incorrect fit provided")
+ 333 return fit
+ 334
+ 335
+ 336def get_skip(properties, out_log, classname):
+ 337 """Gets skip"""
+ 338 skip = properties.get("skip", get_default_value("skip"))
+ 339 if not is_valid_int(skip):
+ 340 fu.log(classname + ": Incorrect skip provided, exiting", out_log)
+ 341 raise SystemExit(classname + ": Incorrect start provided")
+ 342 return str(skip)
+ 343
+ 344
+ 345def get_start(properties, out_log, classname):
+ 346 """Gets start"""
+ 347 start = properties.get("start", get_default_value("start"))
+ 348 if not is_valid_int(start):
+ 349 fu.log(classname + ": Incorrect start provided, exiting", out_log)
+ 350 raise SystemExit(classname + ": Incorrect start provided")
+ 351 return str(start)
352
353
- 354def is_valid_float(val):
- 355 """ Checks if given value is float """
- 356 if val and not isinstance(val, float) and not isinstance(val, int):
- 357 return False
- 358 return True
- 359
- 360
- 361def is_valid_int(val):
- 362 """ Checks if given value is int """
- 363 if val and not isinstance(val, int):
- 364 return False
- 365 return True
- 366
- 367
- 368def is_valid_method_param(met):
- 369 """ Checks if method is compatible with GROMACS """
- 370 methods = ['linkage', 'jarvis-patrick', 'monte-carlo', 'diagonalization', 'gromos']
- 371 return met in methods
- 372
- 373
- 374def is_valid_structure(ext):
- 375 """ Checks if structure format is compatible with GROMACS """
- 376 formats = ['tpr', 'gro', 'g96', 'pdb', 'brk', 'ent']
- 377 return ext in formats
- 378
+ 354def get_end(properties, out_log, classname):
+ 355 """Gets end"""
+ 356 end = properties.get("end", get_default_value("end"))
+ 357 if not is_valid_int(end):
+ 358 fu.log(classname + ": Incorrect end provided, exiting", out_log)
+ 359 raise SystemExit(classname + ": Incorrect end provided")
+ 360 return str(end)
+ 361
+ 362
+ 363def get_dt(properties, out_log, classname):
+ 364 """Gets dt"""
+ 365 dt = properties.get("dt", get_default_value("dt"))
+ 366 if not is_valid_int(dt):
+ 367 fu.log(classname + ": Incorrect dt provided, exiting", out_log)
+ 368 raise SystemExit(classname + ": Incorrect dt provided")
+ 369 return str(dt)
+ 370
+ 371
+ 372def get_ot_str_ens(properties, out_log, classname):
+ 373 """Gets output type"""
+ 374 output_type = properties.get("output_type", get_default_value("ot_str_ens"))
+ 375 if not is_valid_ot_str_ens(output_type):
+ 376 fu.log(classname + ": Incorrect output_type provided, exiting", out_log)
+ 377 raise SystemExit(classname + ": Incorrect output_type provided")
+ 378 return str(output_type)
379
- 380def is_valid_index(ext):
- 381 """ Checks if structure format is compatible with GROMACS """
- 382 formats = ['ndx']
- 383 return ext in formats
- 384
- 385
- 386def is_valid_trajectory(ext):
- 387 """ Checks if trajectory format is compatible with GROMACS """
- 388 formats = ['xtc', 'trr', 'cpt', 'gro', 'g96', 'pdb', 'tng']
- 389 return ext in formats
- 390
- 391
- 392def is_valid_trajectory_output(ext):
- 393 """ Checks if trajectory format is compatible with GROMACS """
- 394 formats = ['xtc', 'trr', 'gro', 'g96', 'pdb', 'tng']
- 395 return ext in formats
- 396
+ 380
+ 381def get_xvg(properties, out_log, classname):
+ 382 """Gets xvg"""
+ 383 xvg = properties.get("xvg", get_default_value("xvg"))
+ 384 if not is_valid_xvg_param(xvg):
+ 385 fu.log(classname + ": Incorrect xvg provided, exiting", out_log)
+ 386 raise SystemExit(classname + ": Incorrect xvg provided")
+ 387 return xvg
+ 388
+ 389
+ 390def get_dista(properties, out_log, classname):
+ 391 """Gets dista"""
+ 392 dista = properties.get("dista", get_default_value("dista"))
+ 393 if not is_valid_boolean(dista):
+ 394 fu.log(classname + ": Incorrect dista provided, exiting", out_log)
+ 395 raise SystemExit(classname + ": Incorrect dista provided")
+ 396 return dista
397
- 398def is_valid_energy(ext):
- 399 """ Checks if energy format is compatible with GROMACS """
- 400 formats = ['edr']
- 401 return ext in formats
- 402
- 403
- 404def is_valid_xvg(ext):
- 405 """ Checks if file is XVG """
- 406 formats = ['xvg']
- 407 return ext in formats
- 408
- 409
- 410def is_valid_zip(ext):
- 411 """ Checks if file is ZIP """
- 412 formats = ['zip']
- 413 return ext in formats
- 414
+ 398
+ 399def get_method(properties, out_log, classname):
+ 400 """Gets method"""
+ 401 method = properties.get("method", get_default_value("method"))
+ 402 if not is_valid_method_param(method):
+ 403 fu.log(classname + ": Incorrect method provided, exiting", out_log)
+ 404 raise SystemExit(classname + ": Incorrect method provided")
+ 405 return method
+ 406
+ 407
+ 408def get_cutoff(properties, out_log, classname):
+ 409 """Gets cutoff"""
+ 410 cutoff = properties.get("cutoff", get_default_value("cutoff"))
+ 411 if not is_valid_float(cutoff):
+ 412 fu.log(classname + ": Incorrect cutoff provided, exiting", out_log)
+ 413 raise SystemExit(classname + ": Incorrect cutoff provided")
+ 414 return str(cutoff)
415
- 416def is_valid_xvg_param(ext):
- 417 """ Checks xvg parameter """
- 418 formats = ['xmgrace', 'xmgr', 'none']
- 419 return ext in formats
- 420
+ 416
+ 417def is_valid_boolean(val):
+ 418 """Checks if given value is boolean"""
+ 419 values = [True, False]
+ 420 return val in values
421
- 422def is_valid_ot_str_ens(ext):
- 423 """ Checks if output type for structure ensemble is correct """
- 424 formats = ['gro', 'g96', 'pdb']
- 425 return ext in formats
- 426
- 427
- 428def is_valid_pbc(pbc):
- 429 """ Checks pbc parameter """
- 430 values = ['none', 'mol', 'res', 'atom', 'nojump', 'cluster', 'whole']
- 431 return pbc in values
- 432
- 433
- 434def is_valid_ur(ur):
- 435 """ Checks ur parameter """
- 436 values = ['rect', 'tric', 'compact']
- 437 return ur in values
- 438
- 439
- 440def is_valid_fit(fit):
- 441 """ Checks fit parameter """
- 442 values = ['none', 'rot+trans', 'rotxy+transxy', 'translation', 'transxy', 'progressive']
- 443 return fit in values
- 444
- 445
- 446def is_valid_term(iterms):
- 447 """ Checks if term is correct """
- 448 cterms = ['Angle', 'Proper-Dih.', 'Improper-Dih.', 'LJ-14', 'Coulomb-14', 'LJ-(SR)', 'Coulomb-(SR)', 'Coul.-recip.', 'Position-Rest.', 'Potential', 'Kinetic-En.', 'Total-Energy', 'Temperature', 'Pressure', ' Constr.-rmsd', 'Box-X', 'Box-Y', ' Box-Z', 'Volume', 'Density', 'pV', 'Enthalpy', 'Vir-XX', 'Vir-XY', 'Vir-XZ', 'Vir-YX', 'Vir-YY', 'Vir-YZ', 'Vir-ZX', 'Vir-ZY', 'Vir-ZZ', 'Pres-XX', 'Pres-XY', 'Pres-XZ', 'Pres-YX', 'Pres-YY', 'Pres-YZ', 'Pres-ZX', 'Pres-ZY', 'Pres-ZZ', '#Surf*SurfTen', 'Box-Vel-XX', 'Box-Vel-YY', 'Box-Vel-ZZ', 'Mu-X', 'Mu-Y', 'Mu-Z', 'T-Protein', 'T-non-Protein', 'Lamb-Protein', 'Lamb-non-Protein']
- 449 return all(elem in cterms for elem in iterms)
- 450
- 451
- 452def is_valid_selection(ext):
- 453 """ Checks if selection is correct """
- 454 formats = ['System', 'Protein', 'Protein-H', 'C-alpha', 'Backbone', 'MainChain', 'MainChain+Cb', 'MainChain+H', 'SideChain', 'SideChain-H', 'Prot-Masses', 'non-Protein', 'Water', 'SOL', 'non-Water', 'Ion', 'NA', 'CL', 'Water_and_ions', 'DNA', 'RNA', 'Protein_DNA', 'Protein_RNA', 'Protein_DNA_RNA', 'DNA_RNA']
- 455 return ext in formats
- 456
- 457
- 458def copy_instructions_file_to_container(instructions_file, unique_dir):
- 459 shutil.copy2(instructions_file, unique_dir)
+ 422
+ 423def is_valid_float(val):
+ 424 """Checks if given value is float"""
+ 425 if val and not isinstance(val, float) and not isinstance(val, int):
+ 426 return False
+ 427 return True
+ 428
+ 429
+ 430def is_valid_int(val):
+ 431 """Checks if given value is int"""
+ 432 if val and not isinstance(val, int):
+ 433 return False
+ 434 return True
+ 435
+ 436
+ 437def is_valid_method_param(met):
+ 438 """Checks if method is compatible with GROMACS"""
+ 439 methods = ["linkage", "jarvis-patrick", "monte-carlo", "diagonalization", "gromos"]
+ 440 return met in methods
+ 441
+ 442
+ 443def is_valid_structure(ext):
+ 444 """Checks if structure format is compatible with GROMACS"""
+ 445 formats = ["tpr", "gro", "g96", "pdb", "brk", "ent"]
+ 446 return ext in formats
+ 447
+ 448
+ 449def is_valid_index(ext):
+ 450 """Checks if structure format is compatible with GROMACS"""
+ 451 formats = ["ndx"]
+ 452 return ext in formats
+ 453
+ 454
+ 455def is_valid_trajectory(ext):
+ 456 """Checks if trajectory format is compatible with GROMACS"""
+ 457 formats = ["xtc", "trr", "cpt", "gro", "g96", "pdb", "tng"]
+ 458 return ext in formats
+ 459
460
- 461
- 462def remove_tmp_files(list, remove_tmp, out_log):
- 463 """ Removes temporal files generated by the wrapper """
- 464 if remove_tmp:
- 465 tmp_files = list
- 466 removed_files = [f for f in tmp_files if fu.rm(f)]
- 467 fu.log('Removed: %s' % str(removed_files), out_log)
- 468
- 469
- 470def process_output_trjconv_str_ens(tmp_folder, output_file, output_dir, glob_pattern, out_log):
- 471 tmp_fl = list(Path(tmp_folder).glob(glob_pattern))
- 472 if not tmp_fl:
- 473 tmp_fl = list(Path(tmp_folder).glob("frame*.pdb"))
- 474
- 475 files_list = []
- 476 for file_name in tmp_fl:
- 477 files_list.append(file_name)
+ 461def is_valid_trajectory_output(ext):
+ 462 """Checks if trajectory format is compatible with GROMACS"""
+ 463 formats = ["xtc", "trr", "gro", "g96", "pdb", "tng"]
+ 464 return ext in formats
+ 465
+ 466
+ 467def is_valid_energy(ext):
+ 468 """Checks if energy format is compatible with GROMACS"""
+ 469 formats = ["edr"]
+ 470 return ext in formats
+ 471
+ 472
+ 473def is_valid_xvg(ext):
+ 474 """Checks if file is XVG"""
+ 475 formats = ["xvg"]
+ 476 return ext in formats
+ 477
478
- 479 # adding files from temporary folder to zip
- 480 fu.zip_list(output_file, files_list, out_log)
- 481
- 482 shutil.copy2(output_file, output_dir)
+ 479def is_valid_zip(ext):
+ 480 """Checks if file is ZIP"""
+ 481 formats = ["zip"]
+ 482 return ext in formats
+ 483
+ 484
+ 485def is_valid_xvg_param(ext):
+ 486 """Checks xvg parameter"""
+ 487 formats = ["xmgrace", "xmgr", "none"]
+ 488 return ext in formats
+ 489
+ 490
+ 491def is_valid_ot_str_ens(ext):
+ 492 """Checks if output type for structure ensemble is correct"""
+ 493 formats = ["gro", "g96", "pdb"]
+ 494 return ext in formats
+ 495
+ 496
+ 497def is_valid_pbc(pbc):
+ 498 """Checks pbc parameter"""
+ 499 values = ["none", "mol", "res", "atom", "nojump", "cluster", "whole"]
+ 500 return pbc in values
+ 501
+ 502
+ 503def is_valid_ur(ur):
+ 504 """Checks ur parameter"""
+ 505 values = ["rect", "tric", "compact"]
+ 506 return ur in values
+ 507
+ 508
+ 509def is_valid_fit(fit):
+ 510 """Checks fit parameter"""
+ 511 values = [
+ 512 "none",
+ 513 "rot+trans",
+ 514 "rotxy+transxy",
+ 515 "translation",
+ 516 "transxy",
+ 517 "progressive",
+ 518 ]
+ 519 return fit in values
+ 520
+ 521
+ 522def is_valid_term(iterms):
+ 523 """Checks if term is correct"""
+ 524 cterms = [
+ 525 "Angle",
+ 526 "Proper-Dih.",
+ 527 "Improper-Dih.",
+ 528 "LJ-14",
+ 529 "Coulomb-14",
+ 530 "LJ-(SR)",
+ 531 "Coulomb-(SR)",
+ 532 "Coul.-recip.",
+ 533 "Position-Rest.",
+ 534 "Potential",
+ 535 "Kinetic-En.",
+ 536 "Total-Energy",
+ 537 "Temperature",
+ 538 "Pressure",
+ 539 " Constr.-rmsd",
+ 540 "Box-X",
+ 541 "Box-Y",
+ 542 " Box-Z",
+ 543 "Volume",
+ 544 "Density",
+ 545 "pV",
+ 546 "Enthalpy",
+ 547 "Vir-XX",
+ 548 "Vir-XY",
+ 549 "Vir-XZ",
+ 550 "Vir-YX",
+ 551 "Vir-YY",
+ 552 "Vir-YZ",
+ 553 "Vir-ZX",
+ 554 "Vir-ZY",
+ 555 "Vir-ZZ",
+ 556 "Pres-XX",
+ 557 "Pres-XY",
+ 558 "Pres-XZ",
+ 559 "Pres-YX",
+ 560 "Pres-YY",
+ 561 "Pres-YZ",
+ 562 "Pres-ZX",
+ 563 "Pres-ZY",
+ 564 "Pres-ZZ",
+ 565 "#Surf*SurfTen",
+ 566 "Box-Vel-XX",
+ 567 "Box-Vel-YY",
+ 568 "Box-Vel-ZZ",
+ 569 "Mu-X",
+ 570 "Mu-Y",
+ 571 "Mu-Z",
+ 572 "T-Protein",
+ 573 "T-non-Protein",
+ 574 "Lamb-Protein",
+ 575 "Lamb-non-Protein",
+ 576 ]
+ 577 return all(elem in cterms for elem in iterms)
+ 578
+ 579
+ 580def is_valid_selection(ext):
+ 581 """Checks if selection is correct"""
+ 582 formats = [
+ 583 "System",
+ 584 "Protein",
+ 585 "Protein-H",
+ 586 "C-alpha",
+ 587 "Backbone",
+ 588 "MainChain",
+ 589 "MainChain+Cb",
+ 590 "MainChain+H",
+ 591 "SideChain",
+ 592 "SideChain-H",
+ 593 "Prot-Masses",
+ 594 "non-Protein",
+ 595 "Water",
+ 596 "SOL",
+ 597 "non-Water",
+ 598 "Ion",
+ 599 "NA",
+ 600 "CL",
+ 601 "Water_and_ions",
+ 602 "DNA",
+ 603 "RNA",
+ 604 "Protein_DNA",
+ 605 "Protein_RNA",
+ 606 "Protein_DNA_RNA",
+ 607 "DNA_RNA",
+ 608 ]
+ 609 return ext in formats
+ 610
+ 611
+ 612def copy_instructions_file_to_container(instructions_file, unique_dir):
+ 613 shutil.copy2(instructions_file, unique_dir)
+ 614
+ 615
+ 616def remove_tmp_files(list, remove_tmp, out_log):
+ 617 """Removes temporal files generated by the wrapper"""
+ 618 if remove_tmp:
+ 619 tmp_files = list
+ 620 removed_files = [f for f in tmp_files if fu.rm(f)]
+ 621 fu.log("Removed: %s" % str(removed_files), out_log)
+ 622
+ 623
+ 624def process_output_trjconv_str_ens(
+ 625 tmp_folder, output_file, output_dir, glob_pattern, out_log
+ 626):
+ 627 tmp_fl = list(Path(tmp_folder).glob(glob_pattern))
+ 628 if not tmp_fl:
+ 629 tmp_fl = list(Path(tmp_folder).glob("frame*.pdb"))
+ 630
+ 631 files_list = []
+ 632 for file_name in tmp_fl:
+ 633 files_list.append(file_name)
+ 634
+ 635 # adding files from temporary folder to zip
+ 636 fu.zip_list(output_file, files_list, out_log)
+ 637
+ 638 shutil.copy2(output_file, output_dir)
+ 639
+ 640
+ 641def _from_string_to_list(input_data: Optional[Union[str, list[str]]]) -> list[str]:
+ 642 """
+ 643 Converts a string to a list, splitting by commas or spaces. If the input is already a list, returns it as is.
+ 644 Returns an empty list if input_data is None.
+ 645
+ 646 Parameters:
+ 647 input_data (str, list, or None): The string, list, or None value to convert.
+ 648
+ 649 Returns:
+ 650 list: A list of string elements or an empty list if input_data is None.
+ 651 """
+ 652 if input_data is None:
+ 653 return []
+ 654
+ 655 if isinstance(input_data, list):
+ 656 # If input is already a list, return it
+ 657 return input_data
+ 658
+ 659 # If input is a string, determine the delimiter based on presence of commas
+ 660 delimiter = "," if "," in input_data else " "
+ 661 items = input_data.split(delimiter)
+ 662
+ 663 # Remove whitespace from each item and ignore empty strings
+ 664 processed_items = [item.strip() for item in items if item.strip()]
+ 665
+ 666 return processed_items