corems.encapsulation.output.parameter_to_json
1import json 2 3import tomlkit 4from pathlib import Path 5 6from corems.encapsulation.output import parameter_to_dict 7from corems.encapsulation.output.parameter_to_dict import get_dict_data_lcms 8 9 10def _toml_array_item(item): 11 """Prepare one array element for TOML. 12 13 The previous encoder wrote a null array element as the string ``None``. 14 """ 15 if item is None: 16 return "None" 17 return _toml_value(item) 18 19 20def _toml_value(value): 21 """Prepare ``value`` for TOML. 22 23 TOML has no null. Table entries whose value is ``None`` are omitted, which 24 matches the encoder previously used for settings files. 25 """ 26 if isinstance(value, dict): 27 return { 28 key: _toml_value(item) for key, item in value.items() if item is not None 29 } 30 if isinstance(value, tuple): 31 return tuple(_toml_array_item(item) for item in value) 32 if isinstance(value, list): 33 return [_toml_array_item(item) for item in value] 34 return value 35 36 37def _dumps_toml(data): 38 """Serialize ``data`` to a TOML string.""" 39 return tomlkit.dumps(_toml_value(data)) 40 41 42def dump_all_settings_json(filename="SettingsCoreMS.json", file_path=None): 43 """ 44 Write JSON file into current directory with all the default settings for the CoreMS package. 45 46 Parameters: 47 ---------- 48 filename : str, optional 49 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 50 file_path : str or Path, optional 51 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 52 """ 53 54 data_dict_all = parameter_to_dict.get_dict_all_default_data() 55 56 if not file_path: 57 file_path = Path.cwd() / filename 58 59 with open( 60 file_path, 61 "w", 62 encoding="utf8", 63 ) as outfile: 64 import re 65 66 # pretty print 67 output = json.dumps( 68 data_dict_all, sort_keys=False, indent=4, separators=(",", ": ") 69 ) 70 output = re.sub(r'",\s+', '", ', output) 71 72 outfile.write(output) 73 74 75def dump_ms_settings_json(filename="SettingsCoreMS.json", file_path=None): 76 """ 77 Write JSON file into current directory with all the mass spectrum default settings for the CoreMS package. 78 79 Parameters 80 ---------- 81 filename : str, optional 82 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 83 file_path : str or Path, optional 84 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 85 86 """ 87 data_dict = parameter_to_dict.get_dict_ms_default_data() 88 if not file_path: 89 file_path = Path.cwd() / filename 90 91 with open( 92 file_path, 93 "w", 94 encoding="utf8", 95 ) as outfile: 96 import re 97 98 # pretty print 99 output = json.dumps( 100 data_dict, sort_keys=False, indent=4, separators=(",", ": ") 101 ) 102 output = re.sub(r'",\s+', '", ', output) 103 104 outfile.write(output) 105 106 107def dump_gcms_settings_json(filename="SettingsCoreMS.json", file_path=None): 108 """ 109 Write JSON file into current directory containing the default GCMS settings data. 110 111 Parameters 112 ---------- 113 filename : str, optional 114 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 115 file_path : str or Path-like object, optional 116 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 117 """ 118 119 from pathlib import Path 120 import json 121 122 data_dict = parameter_to_dict.get_dict_gcms_default_data() 123 124 if not file_path: 125 file_path = Path.cwd() / filename 126 127 with open( 128 file_path, 129 "w", 130 encoding="utf8", 131 ) as outfile: 132 import re 133 134 # pretty print 135 output = json.dumps( 136 data_dict, sort_keys=False, indent=4, separators=(",", ": ") 137 ) 138 output = re.sub(r'",\s+', '", ', output) 139 140 outfile.write(output) 141 142 143def dump_all_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 144 """ 145 Write TOML file into the specified file path or the current directory with all the default settings for the CoreMS package. 146 147 Parameters 148 ---------- 149 filename : str, optional 150 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 151 file_path : str or Path, optional 152 The path where the TOML file will be saved. If not provided, the file will be saved in the current directory. 153 154 """ 155 from pathlib import Path 156 157 data_dict_all = parameter_to_dict.get_dict_all_default_data() 158 159 if not file_path: 160 file_path = Path.cwd() / filename 161 162 with open( 163 file_path, 164 "w", 165 encoding="utf8", 166 ) as outfile: 167 168 output = _dumps_toml(data_dict_all) 169 outfile.write(output) 170 171 172def dump_ms_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 173 """ 174 Write TOML file into the current directory with all the mass spectrum default settings for the CoreMS package. 175 176 Parameters 177 ---------- 178 filename : str, optional 179 The name of the TOML file to be created. Default is 'SettingsCoreMS.toml'. 180 file_path : str or Path, optional 181 The path where the TOML file should be saved. If not provided, the file will be saved in the current working directory. 182 183 """ 184 data_dict = parameter_to_dict.get_dict_ms_default_data() 185 186 if not file_path: 187 file_path = Path.cwd() / filename 188 189 with open( 190 file_path, 191 "w", 192 encoding="utf8", 193 ) as outfile: 194 195 # pretty print 196 output = _dumps_toml(data_dict) 197 outfile.write(output) 198 199 200def dump_gcms_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 201 """ 202 Write TOML file into current directory containing the default GCMS settings data. 203 204 Parameters 205 ---------- 206 filename : str, optional 207 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 208 file_path : str or Path, optional 209 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 210 211 """ 212 213 data_dict = parameter_to_dict.get_dict_gcms_default_data() 214 215 if not file_path: 216 file_path = Path.cwd() / filename 217 218 with open( 219 file_path, 220 "w", 221 encoding="utf8", 222 ) as outfile: 223 output = _dumps_toml(data_dict) 224 outfile.write(output) 225 226 227def dump_lcms_settings_json( 228 filename="SettingsCoreMS.json", file_path=None, lcms_obj=None 229): 230 """ 231 Write JSON file into current directory with all the LCMS settings data for the CoreMS package. 232 233 Parameters 234 ---------- 235 filename : str, optional 236 The name of the JSON file. Defaults to 'SettingsCoreMS.json'. 237 file_path : str or Path, optional 238 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 239 lcms_obj : object, optional 240 The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 241 242 """ 243 244 if lcms_obj is None: 245 data_dict = parameter_to_dict.get_dict_lcms_default_data() 246 else: 247 data_dict = get_dict_data_lcms(lcms_obj) 248 249 if not file_path: 250 file_path = Path.cwd() / filename 251 252 with open( 253 file_path, 254 "w", 255 encoding="utf8", 256 ) as outfile: 257 outfile.write(json.dumps(data_dict, indent=4)) 258 259 260def dump_lcms_settings_toml( 261 filename="SettingsCoreMS.toml", file_path=None, lcms_obj=None 262): 263 """ 264 Write TOML file into current directory with all the LCMS settings data for the CoreMS package. 265 266 Parameters 267 ---------- 268 filename : str, optional 269 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 270 file_path : str or Path, optional 271 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 272 lcms_obj : object, optional 273 The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 274 275 """ 276 277 if lcms_obj is None: 278 data_dict = parameter_to_dict.get_dict_lcms_default_data() 279 else: 280 data_dict = get_dict_data_lcms(lcms_obj) 281 282 if not file_path: 283 file_path = Path.cwd() / filename 284 285 with open( 286 file_path, 287 "w", 288 encoding="utf8", 289 ) as outfile: 290 output = _dumps_toml(data_dict) 291 outfile.write(output) 292 293 294def dump_lcms_collection_settings_json( 295 filename="SettingsCoreMS.json", file_path=None, lcms_collection=None 296): 297 """Write JSON file with LCMS collection settings data. 298 299 Parameters 300 ---------- 301 filename : str, optional 302 The name of the JSON file. Defaults to 'SettingsCoreMS.json'. 303 file_path : str or Path, optional 304 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 305 lcms_collection : LCMSCollection, optional 306 The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 307 """ 308 from corems.encapsulation.output.parameter_to_dict import ( 309 get_dict_data_lcms_collection, 310 get_dict_lcms_collection_default_data, 311 ) 312 313 if lcms_collection is None: 314 data_dict = get_dict_lcms_collection_default_data() 315 else: 316 data_dict = get_dict_data_lcms_collection(lcms_collection) 317 318 if not file_path: 319 file_path = Path.cwd() / filename 320 321 with open( 322 file_path, 323 "w", 324 encoding="utf8", 325 ) as outfile: 326 outfile.write(json.dumps(data_dict, indent=4)) 327 328 329def dump_lcms_collection_settings_toml( 330 filename="SettingsCoreMS.toml", file_path=None, lcms_collection=None 331): 332 """Write TOML file with LCMS collection settings data. 333 334 Parameters 335 ---------- 336 filename : str, optional 337 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 338 file_path : str or Path, optional 339 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 340 lcms_collection : LCMSCollection, optional 341 The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 342 """ 343 from corems.encapsulation.output.parameter_to_dict import ( 344 get_dict_data_lcms_collection, 345 get_dict_lcms_collection_default_data, 346 ) 347 348 if lcms_collection is None: 349 data_dict = get_dict_lcms_collection_default_data() 350 else: 351 data_dict = get_dict_data_lcms_collection(lcms_collection) 352 353 if not file_path: 354 file_path = Path.cwd() / filename 355 356 with open( 357 file_path, 358 "w", 359 encoding="utf8", 360 ) as outfile: 361 output = _dumps_toml(data_dict) 362 outfile.write(output)
43def dump_all_settings_json(filename="SettingsCoreMS.json", file_path=None): 44 """ 45 Write JSON file into current directory with all the default settings for the CoreMS package. 46 47 Parameters: 48 ---------- 49 filename : str, optional 50 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 51 file_path : str or Path, optional 52 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 53 """ 54 55 data_dict_all = parameter_to_dict.get_dict_all_default_data() 56 57 if not file_path: 58 file_path = Path.cwd() / filename 59 60 with open( 61 file_path, 62 "w", 63 encoding="utf8", 64 ) as outfile: 65 import re 66 67 # pretty print 68 output = json.dumps( 69 data_dict_all, sort_keys=False, indent=4, separators=(",", ": ") 70 ) 71 output = re.sub(r'",\s+', '", ', output) 72 73 outfile.write(output)
Write JSON file into current directory with all the default settings for the CoreMS package.
Parameters:
filename : str, optional The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. file_path : str or Path, optional The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory.
76def dump_ms_settings_json(filename="SettingsCoreMS.json", file_path=None): 77 """ 78 Write JSON file into current directory with all the mass spectrum default settings for the CoreMS package. 79 80 Parameters 81 ---------- 82 filename : str, optional 83 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 84 file_path : str or Path, optional 85 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 86 87 """ 88 data_dict = parameter_to_dict.get_dict_ms_default_data() 89 if not file_path: 90 file_path = Path.cwd() / filename 91 92 with open( 93 file_path, 94 "w", 95 encoding="utf8", 96 ) as outfile: 97 import re 98 99 # pretty print 100 output = json.dumps( 101 data_dict, sort_keys=False, indent=4, separators=(",", ": ") 102 ) 103 output = re.sub(r'",\s+', '", ', output) 104 105 outfile.write(output)
Write JSON file into current directory with all the mass spectrum default settings for the CoreMS package.
Parameters
- filename (str, optional): The name of the JSON file to be created. Default is 'SettingsCoreMS.json'.
- file_path (str or Path, optional): The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory.
108def dump_gcms_settings_json(filename="SettingsCoreMS.json", file_path=None): 109 """ 110 Write JSON file into current directory containing the default GCMS settings data. 111 112 Parameters 113 ---------- 114 filename : str, optional 115 The name of the JSON file to be created. Default is 'SettingsCoreMS.json'. 116 file_path : str or Path-like object, optional 117 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 118 """ 119 120 from pathlib import Path 121 import json 122 123 data_dict = parameter_to_dict.get_dict_gcms_default_data() 124 125 if not file_path: 126 file_path = Path.cwd() / filename 127 128 with open( 129 file_path, 130 "w", 131 encoding="utf8", 132 ) as outfile: 133 import re 134 135 # pretty print 136 output = json.dumps( 137 data_dict, sort_keys=False, indent=4, separators=(",", ": ") 138 ) 139 output = re.sub(r'",\s+', '", ', output) 140 141 outfile.write(output)
Write JSON file into current directory containing the default GCMS settings data.
Parameters
- filename (str, optional): The name of the JSON file to be created. Default is 'SettingsCoreMS.json'.
- file_path (str or Path-like object, optional): The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory.
144def dump_all_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 145 """ 146 Write TOML file into the specified file path or the current directory with all the default settings for the CoreMS package. 147 148 Parameters 149 ---------- 150 filename : str, optional 151 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 152 file_path : str or Path, optional 153 The path where the TOML file will be saved. If not provided, the file will be saved in the current directory. 154 155 """ 156 from pathlib import Path 157 158 data_dict_all = parameter_to_dict.get_dict_all_default_data() 159 160 if not file_path: 161 file_path = Path.cwd() / filename 162 163 with open( 164 file_path, 165 "w", 166 encoding="utf8", 167 ) as outfile: 168 169 output = _dumps_toml(data_dict_all) 170 outfile.write(output)
Write TOML file into the specified file path or the current directory with all the default settings for the CoreMS package.
Parameters
- filename (str, optional): The name of the TOML file. Defaults to 'SettingsCoreMS.toml'.
- file_path (str or Path, optional): The path where the TOML file will be saved. If not provided, the file will be saved in the current directory.
173def dump_ms_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 174 """ 175 Write TOML file into the current directory with all the mass spectrum default settings for the CoreMS package. 176 177 Parameters 178 ---------- 179 filename : str, optional 180 The name of the TOML file to be created. Default is 'SettingsCoreMS.toml'. 181 file_path : str or Path, optional 182 The path where the TOML file should be saved. If not provided, the file will be saved in the current working directory. 183 184 """ 185 data_dict = parameter_to_dict.get_dict_ms_default_data() 186 187 if not file_path: 188 file_path = Path.cwd() / filename 189 190 with open( 191 file_path, 192 "w", 193 encoding="utf8", 194 ) as outfile: 195 196 # pretty print 197 output = _dumps_toml(data_dict) 198 outfile.write(output)
Write TOML file into the current directory with all the mass spectrum default settings for the CoreMS package.
Parameters
- filename (str, optional): The name of the TOML file to be created. Default is 'SettingsCoreMS.toml'.
- file_path (str or Path, optional): The path where the TOML file should be saved. If not provided, the file will be saved in the current working directory.
201def dump_gcms_settings_toml(filename="SettingsCoreMS.toml", file_path=None): 202 """ 203 Write TOML file into current directory containing the default GCMS settings data. 204 205 Parameters 206 ---------- 207 filename : str, optional 208 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 209 file_path : str or Path, optional 210 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 211 212 """ 213 214 data_dict = parameter_to_dict.get_dict_gcms_default_data() 215 216 if not file_path: 217 file_path = Path.cwd() / filename 218 219 with open( 220 file_path, 221 "w", 222 encoding="utf8", 223 ) as outfile: 224 output = _dumps_toml(data_dict) 225 outfile.write(output)
Write TOML file into current directory containing the default GCMS settings data.
Parameters
- filename (str, optional): The name of the TOML file. Defaults to 'SettingsCoreMS.toml'.
- file_path (str or Path, optional): The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory.
228def dump_lcms_settings_json( 229 filename="SettingsCoreMS.json", file_path=None, lcms_obj=None 230): 231 """ 232 Write JSON file into current directory with all the LCMS settings data for the CoreMS package. 233 234 Parameters 235 ---------- 236 filename : str, optional 237 The name of the JSON file. Defaults to 'SettingsCoreMS.json'. 238 file_path : str or Path, optional 239 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 240 lcms_obj : object, optional 241 The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 242 243 """ 244 245 if lcms_obj is None: 246 data_dict = parameter_to_dict.get_dict_lcms_default_data() 247 else: 248 data_dict = get_dict_data_lcms(lcms_obj) 249 250 if not file_path: 251 file_path = Path.cwd() / filename 252 253 with open( 254 file_path, 255 "w", 256 encoding="utf8", 257 ) as outfile: 258 outfile.write(json.dumps(data_dict, indent=4))
Write JSON file into current directory with all the LCMS settings data for the CoreMS package.
Parameters
- filename (str, optional): The name of the JSON file. Defaults to 'SettingsCoreMS.json'.
- file_path (str or Path, optional): The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory.
- lcms_obj (object, optional): The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings.
261def dump_lcms_settings_toml( 262 filename="SettingsCoreMS.toml", file_path=None, lcms_obj=None 263): 264 """ 265 Write TOML file into current directory with all the LCMS settings data for the CoreMS package. 266 267 Parameters 268 ---------- 269 filename : str, optional 270 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 271 file_path : str or Path, optional 272 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 273 lcms_obj : object, optional 274 The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 275 276 """ 277 278 if lcms_obj is None: 279 data_dict = parameter_to_dict.get_dict_lcms_default_data() 280 else: 281 data_dict = get_dict_data_lcms(lcms_obj) 282 283 if not file_path: 284 file_path = Path.cwd() / filename 285 286 with open( 287 file_path, 288 "w", 289 encoding="utf8", 290 ) as outfile: 291 output = _dumps_toml(data_dict) 292 outfile.write(output)
Write TOML file into current directory with all the LCMS settings data for the CoreMS package.
Parameters
- filename (str, optional): The name of the TOML file. Defaults to 'SettingsCoreMS.toml'.
- file_path (str or Path, optional): The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory.
- lcms_obj (object, optional): The LCMS object containing the settings data. If not provided, the settings data will be retrieved from the default settings.
295def dump_lcms_collection_settings_json( 296 filename="SettingsCoreMS.json", file_path=None, lcms_collection=None 297): 298 """Write JSON file with LCMS collection settings data. 299 300 Parameters 301 ---------- 302 filename : str, optional 303 The name of the JSON file. Defaults to 'SettingsCoreMS.json'. 304 file_path : str or Path, optional 305 The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory. 306 lcms_collection : LCMSCollection, optional 307 The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 308 """ 309 from corems.encapsulation.output.parameter_to_dict import ( 310 get_dict_data_lcms_collection, 311 get_dict_lcms_collection_default_data, 312 ) 313 314 if lcms_collection is None: 315 data_dict = get_dict_lcms_collection_default_data() 316 else: 317 data_dict = get_dict_data_lcms_collection(lcms_collection) 318 319 if not file_path: 320 file_path = Path.cwd() / filename 321 322 with open( 323 file_path, 324 "w", 325 encoding="utf8", 326 ) as outfile: 327 outfile.write(json.dumps(data_dict, indent=4))
Write JSON file with LCMS collection settings data.
Parameters
- filename (str, optional): The name of the JSON file. Defaults to 'SettingsCoreMS.json'.
- file_path (str or Path, optional): The path where the JSON file will be saved. If not provided, the file will be saved in the current working directory.
- lcms_collection (LCMSCollection, optional): The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings.
330def dump_lcms_collection_settings_toml( 331 filename="SettingsCoreMS.toml", file_path=None, lcms_collection=None 332): 333 """Write TOML file with LCMS collection settings data. 334 335 Parameters 336 ---------- 337 filename : str, optional 338 The name of the TOML file. Defaults to 'SettingsCoreMS.toml'. 339 file_path : str or Path, optional 340 The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory. 341 lcms_collection : LCMSCollection, optional 342 The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings. 343 """ 344 from corems.encapsulation.output.parameter_to_dict import ( 345 get_dict_data_lcms_collection, 346 get_dict_lcms_collection_default_data, 347 ) 348 349 if lcms_collection is None: 350 data_dict = get_dict_lcms_collection_default_data() 351 else: 352 data_dict = get_dict_data_lcms_collection(lcms_collection) 353 354 if not file_path: 355 file_path = Path.cwd() / filename 356 357 with open( 358 file_path, 359 "w", 360 encoding="utf8", 361 ) as outfile: 362 output = _dumps_toml(data_dict) 363 outfile.write(output)
Write TOML file with LCMS collection settings data.
Parameters
- filename (str, optional): The name of the TOML file. Defaults to 'SettingsCoreMS.toml'.
- file_path (str or Path, optional): The path where the TOML file will be saved. If not provided, the file will be saved in the current working directory.
- lcms_collection (LCMSCollection, optional): The LCMS collection object containing the settings data. If not provided, the settings data will be retrieved from the default settings.