|
45 | 45 | "metadata": {}, |
46 | 46 | "outputs": [], |
47 | 47 | "source": [ |
48 | | - "def _load_valid_result_json(fpath:os.PathLike|str)->tuple[tuple|None, dict|None]:\n", |
| 48 | + "def _load_valid_result_json(\n", |
| 49 | + " fpath: os.PathLike | str,\n", |
| 50 | + ") -> tuple[tuple | None, dict | None]:\n", |
49 | 51 | " \"\"\"Load the data from a results JSON into a dict.\n", |
50 | 52 | "\n", |
51 | 53 | " Parameters\n", |
|
69 | 71 | " except (ValueError, IndexError):\n", |
70 | 72 | " print(f\"{fpath}: Missing ligand names. Skipping.\")\n", |
71 | 73 | " return None, None\n", |
72 | | - " if result['estimate'] is None:\n", |
| 74 | + " if result[\"estimate\"] is None:\n", |
73 | 75 | " errormsg = f\"{fpath}: No 'estimate' found, assuming to be a failed simulation.\"\n", |
74 | 76 | " raise ValueError(errormsg)\n", |
75 | 77 | " # return names, None\n", |
76 | | - " if result['uncertainty'] is None:\n", |
77 | | - " errormsg = f\"{fpath}: No 'uncertainty' found, assuming to be a failed simulation.\"\n", |
| 78 | + " if result[\"uncertainty\"] is None:\n", |
| 79 | + " errormsg = (\n", |
| 80 | + " f\"{fpath}: No 'uncertainty' found, assuming to be a failed simulation.\"\n", |
| 81 | + " )\n", |
78 | 82 | " raise ValueError(errormsg)\n", |
79 | | - " if all('exception' in u for u in result['unit_results'].values()):\n", |
| 83 | + " if all(\"exception\" in u for u in result[\"unit_results\"].values()):\n", |
80 | 84 | " errormsg = f\"{fpath}: Exception found in all 'unit_results', assuming to be a failed simulation.\"\n", |
81 | 85 | " raise ValueError(errormsg)\n", |
82 | 86 | "\n", |
|
91 | 95 | "outputs": [], |
92 | 96 | "source": [ |
93 | 97 | "def _get_legs_from_result_jsons(\n", |
94 | | - " result_fns: list[pathlib.Path]\n", |
| 98 | + " result_fns: list[pathlib.Path],\n", |
95 | 99 | ") -> dict[tuple[str, str], dict[str, list]]:\n", |
96 | 100 | " \"\"\"\n", |
97 | 101 | " Iterate over a list of result JSONs and populate a dict of dicts with all data needed\n", |
|
119 | 123 | " if names is None: # this means it couldn't find names and/or simtype\n", |
120 | 124 | " continue\n", |
121 | 125 | "\n", |
122 | | - " ddgs[names]['overall'].append([result[\"estimate\"], result[\"uncertainty\"]])\n", |
| 126 | + " ddgs[names][\"overall\"].append([result[\"estimate\"], result[\"uncertainty\"]])\n", |
123 | 127 | " proto_key = [\n", |
124 | | - " k\n", |
125 | | - " for k in result[\"unit_results\"].keys()\n", |
126 | | - " if k.startswith(\"ProtocolUnitResult\") \n", |
127 | | - " ]\n", |
| 128 | + " k\n", |
| 129 | + " for k in result[\"unit_results\"].keys()\n", |
| 130 | + " if k.startswith(\"ProtocolUnitResult\")\n", |
| 131 | + " ]\n", |
128 | 132 | " for p in proto_key:\n", |
129 | 133 | " if \"unit_estimate\" in result[\"unit_results\"][p][\"outputs\"]:\n", |
130 | 134 | " simtype = result[\"unit_results\"][p][\"outputs\"][\"simtype\"]\n", |
131 | 135 | " dg = result[\"unit_results\"][p][\"outputs\"][\"unit_estimate\"]\n", |
132 | 136 | " dg_error = result[\"unit_results\"][p][\"outputs\"][\"unit_estimate_error\"]\n", |
133 | | - " \n", |
| 137 | + "\n", |
134 | 138 | " ddgs[names][simtype].append([dg, dg_error])\n", |
135 | 139 | " elif \"standard_state_correction_A\" in result[\"unit_results\"][p][\"outputs\"]:\n", |
136 | | - " corr_A = result[\"unit_results\"][p][\"outputs\"][\"standard_state_correction_A\"]\n", |
137 | | - " corr_B = result[\"unit_results\"][p][\"outputs\"][\"standard_state_correction_B\"]\n", |
138 | | - " ddgs[names][\"standard_state_correction_A\"].append([corr_A, 0*unit.kilocalorie_per_mole])\n", |
139 | | - " ddgs[names][\"standard_state_correction_B\"].append([corr_B, 0*unit.kilocalorie_per_mole])\n", |
| 140 | + " corr_A = result[\"unit_results\"][p][\"outputs\"][\n", |
| 141 | + " \"standard_state_correction_A\"\n", |
| 142 | + " ]\n", |
| 143 | + " corr_B = result[\"unit_results\"][p][\"outputs\"][\n", |
| 144 | + " \"standard_state_correction_B\"\n", |
| 145 | + " ]\n", |
| 146 | + " ddgs[names][\"standard_state_correction_A\"].append(\n", |
| 147 | + " [corr_A, 0 * unit.kilocalorie_per_mole]\n", |
| 148 | + " )\n", |
| 149 | + " ddgs[names][\"standard_state_correction_B\"].append(\n", |
| 150 | + " [corr_B, 0 * unit.kilocalorie_per_mole]\n", |
| 151 | + " )\n", |
140 | 152 | " else:\n", |
141 | 153 | " continue\n", |
142 | 154 | "\n", |
|
150 | 162 | "metadata": {}, |
151 | 163 | "outputs": [], |
152 | 164 | "source": [ |
153 | | - "def _get_names(result:dict) -> tuple[str, str]:\n", |
| 165 | + "def _get_names(result: dict) -> tuple[str, str]:\n", |
154 | 166 | " \"\"\"Get the ligand names from a unit's results data.\n", |
155 | 167 | "\n", |
156 | 168 | " Parameters\n", |
|
164 | 176 | " Ligand names corresponding to the results.\n", |
165 | 177 | " \"\"\"\n", |
166 | 178 | " try:\n", |
167 | | - " nm = list(result['unit_results'].values())[0]['name']\n", |
| 179 | + " nm = list(result[\"unit_results\"].values())[0][\"name\"]\n", |
168 | 180 | "\n", |
169 | 181 | " except KeyError:\n", |
170 | 182 | " raise ValueError(\"Failed to guess names\")\n", |
171 | 183 | "\n", |
172 | 184 | " # TODO: make this more robust by pulling names from inputs.state[A/B].name\n", |
173 | 185 | "\n", |
174 | | - " toks = nm.split(',')\n", |
| 186 | + " toks = nm.split(\",\")\n", |
175 | 187 | " toks = toks[1].split()\n", |
176 | 188 | " return toks[1], toks[3]" |
177 | 189 | ] |
|
183 | 195 | "metadata": {}, |
184 | 196 | "outputs": [], |
185 | 197 | "source": [ |
186 | | - "def _generate_raw(legs:dict) -> None:\n", |
| 198 | + "def _generate_raw(legs: dict) -> None:\n", |
187 | 199 | " \"\"\"\n", |
188 | 200 | " Write out all legs found and their DG values, or indicate that they have failed.\n", |
189 | 201 | "\n", |
|
195 | 207 | " data = []\n", |
196 | 208 | " for ligpair, results in sorted(legs.items()):\n", |
197 | 209 | " for simtype, repeats in sorted(results.items()):\n", |
198 | | - " if simtype != 'overall':\n", |
| 210 | + " if simtype != \"overall\":\n", |
199 | 211 | " for repeat in repeats:\n", |
200 | | - " m, u = format_estimate_uncertainty(repeat[0].m, repeat[1].m, unc_prec=2)\n", |
| 212 | + " m, u = format_estimate_uncertainty(\n", |
| 213 | + " repeat[0].m, repeat[1].m, unc_prec=2\n", |
| 214 | + " )\n", |
201 | 215 | " data.append((simtype, ligpair[0], ligpair[1], m, u))\n", |
202 | 216 | "\n", |
203 | 217 | " df = pd.DataFrame(\n", |
|
226 | 240 | " \"\"\"\n", |
227 | 241 | " return np.std([v[0].m for v in r[\"overall\"]])\n", |
228 | 242 | "\n", |
| 243 | + "\n", |
229 | 244 | "def error_mbar(r):\n", |
230 | 245 | " \"\"\"\n", |
231 | 246 | " Calculate the error of the estimate using the reported MBAR errors.\n", |
|
234 | 249 | " \"\"\"\n", |
235 | 250 | " complex_errors = [x[1].m for x in r[\"complex\"]]\n", |
236 | 251 | " solvent_errors = [x[1].m for x in r[\"solvent\"]]\n", |
237 | | - " return math.sqrt(np.mean(complex_errors)**2 + np.mean(solvent_errors)**2)" |
| 252 | + " return math.sqrt(np.mean(complex_errors) ** 2 + np.mean(solvent_errors) ** 2)" |
238 | 253 | ] |
239 | 254 | }, |
240 | 255 | { |
|
244 | 259 | "metadata": {}, |
245 | 260 | "outputs": [], |
246 | 261 | "source": [ |
247 | | - "def _generate_ddg(legs:dict) -> None:\n", |
| 262 | + "def _generate_ddg(legs: dict) -> None:\n", |
248 | 263 | " \"\"\"Compute and write out DDG values for the given legs.\n", |
249 | 264 | "\n", |
250 | 265 | " Parameters\n", |
|
254 | 269 | " \"\"\"\n", |
255 | 270 | " data = []\n", |
256 | 271 | " # check the type of error which should be used based on the number of repeats\n", |
257 | | - " repeats = {len(v['overall']) for v in legs.values()}\n", |
| 272 | + " repeats = {len(v[\"overall\"]) for v in legs.values()}\n", |
258 | 273 | " error_func = error_mbar if 1 in repeats else error_std\n", |
259 | 274 | " for ligpair, results in sorted(legs.items()):\n", |
260 | 275 | " ddg = np.mean([v[0].m for v in results[\"overall\"]])\n", |
261 | 276 | " error = error_func(results)\n", |
262 | 277 | " m, u = format_estimate_uncertainty(ddg, error, unc_prec=2)\n", |
263 | 278 | " data.append((ligpair[0], ligpair[1], m, u))\n", |
264 | 279 | "\n", |
265 | | - " df = pd.DataFrame(data, columns=[\"ligand_i\", \"ligand_j\", \"DDG(i->j) (kcal/mol)\", \"uncertainty (kcal/mol)\"])\n", |
| 280 | + " df = pd.DataFrame(\n", |
| 281 | + " data,\n", |
| 282 | + " columns=[\n", |
| 283 | + " \"ligand_i\",\n", |
| 284 | + " \"ligand_j\",\n", |
| 285 | + " \"DDG(i->j) (kcal/mol)\",\n", |
| 286 | + " \"uncertainty (kcal/mol)\",\n", |
| 287 | + " ],\n", |
| 288 | + " )\n", |
266 | 289 | " return df" |
267 | 290 | ] |
268 | 291 | }, |
|
291 | 314 | " labelB=ligB,\n", |
292 | 315 | " DG=DDGbind * unit.kilocalorie_per_mole,\n", |
293 | 316 | " uncertainty=bind_unc * unit.kilocalorie_per_mole,\n", |
294 | | - " computational=True\n", |
| 317 | + " computational=True,\n", |
295 | 318 | " )\n", |
296 | 319 | " fe_results.append(m)\n", |
297 | 320 | "\n", |
|
304 | 327 | " femap.generate_absolute_values()\n", |
305 | 328 | " df = femap.get_absolute_dataframe()\n", |
306 | 329 | " df = df.iloc[:, :3]\n", |
307 | | - " df.rename({'label': 'ligand'}, axis='columns', inplace=True)\n", |
| 330 | + " df.rename({\"label\": \"ligand\"}, axis=\"columns\", inplace=True)\n", |
308 | 331 | "\n", |
309 | 332 | " return df" |
310 | 333 | ] |
|
317 | 340 | "outputs": [], |
318 | 341 | "source": [ |
319 | 342 | "def get_ddgs_dict(\n", |
320 | | - " results: List[os.PathLike | str]\n", |
| 343 | + " results: List[os.PathLike | str],\n", |
321 | 344 | ") -> dict[tuple[str, str], dict[str, list]]:\n", |
322 | 345 | " # find and filter result jsons\n", |
323 | 346 | " result_fns = _collect_result_jsons(results)\n", |
|
359 | 382 | ], |
360 | 383 | "source": [ |
361 | 384 | "# Specify paths to result directories\n", |
362 | | - "results_dir = [pathlib.Path('results_0'), pathlib.Path('results_1'), pathlib.Path('results_2')]\n", |
| 385 | + "results_dir = [\n", |
| 386 | + " pathlib.Path(\"results_0\"),\n", |
| 387 | + " pathlib.Path(\"results_1\"),\n", |
| 388 | + " pathlib.Path(\"results_2\"),\n", |
| 389 | + "]\n", |
363 | 390 | "ddgs = get_ddgs_dict(results_dir)" |
364 | 391 | ] |
365 | 392 | }, |
|
381 | 408 | "outputs": [], |
382 | 409 | "source": [ |
383 | 410 | "df_ddg = _generate_ddg(ddgs)\n", |
384 | | - "df_ddg.to_csv('ddg.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
| 411 | + "df_ddg.to_csv(\"ddg.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
385 | 412 | ] |
386 | 413 | }, |
387 | 414 | { |
|
485 | 512 | "outputs": [], |
486 | 513 | "source": [ |
487 | 514 | "df_dg = _generate_dg_mle(ddgs)\n", |
488 | | - "df_dg.to_csv('dg.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
| 515 | + "df_dg.to_csv(\"dg.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
489 | 516 | ] |
490 | 517 | }, |
491 | 518 | { |
|
582 | 609 | "outputs": [], |
583 | 610 | "source": [ |
584 | 611 | "df_raw = _generate_raw(ddgs)\n", |
585 | | - "df_raw.to_csv('ddg_raw.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
| 612 | + "df_raw.to_csv(\"ddg_raw.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)" |
586 | 613 | ] |
587 | 614 | }, |
588 | 615 | { |
|
0 commit comments