|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import urllib.request |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from phi.api import _request |
| 8 | +from phi.config import _save_state, _ssl_context |
| 9 | +from phi.display import _C_BLUE, _C_SAND, _die, console |
| 10 | + |
| 11 | + |
| 12 | +def cmd_tutorial(args: argparse.Namespace) -> None: |
| 13 | + out = Path(args.out) |
| 14 | + |
| 15 | + # ── 1. Fetch manifest (standard Clerk JWT auth, same as all endpoints) ─── |
| 16 | + console.print("[dim]Fetching tutorial dataset …[/]") |
| 17 | + try: |
| 18 | + manifest = _request("GET", "/tutorial") |
| 19 | + except Exception as exc: |
| 20 | + _die( |
| 21 | + f"Could not reach the tutorial endpoint: {exc}\n" |
| 22 | + " Check your connection and API key, then try again." |
| 23 | + ) |
| 24 | + |
| 25 | + files: list[dict] = manifest.get("files", []) |
| 26 | + dataset_id: str | None = manifest.get("dataset_id") |
| 27 | + message: str | None = manifest.get("message") |
| 28 | + |
| 29 | + if not files: |
| 30 | + _die("No tutorial files returned by the API.") |
| 31 | + |
| 32 | + # ── 2. Download each file (plain HTTP — signed URLs are self-authenticating) |
| 33 | + out.mkdir(parents=True, exist_ok=True) |
| 34 | + console.print(f" Downloading {len(files)} file(s) to [{_C_BLUE}]{out}/[/] …\n") |
| 35 | + |
| 36 | + for entry in files: |
| 37 | + filename: str = entry["filename"] |
| 38 | + url: str = entry["url"] |
| 39 | + dest = out / filename |
| 40 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 41 | + try: |
| 42 | + req = urllib.request.Request(url) |
| 43 | + with urllib.request.urlopen(req, context=_ssl_context()) as resp: |
| 44 | + dest.write_bytes(resp.read()) |
| 45 | + console.print(f" [bold {_C_SAND}]✓[/] {filename}") |
| 46 | + except Exception as exc: |
| 47 | + _die(f"Failed to download {filename}: {exc}") |
| 48 | + |
| 49 | + # ── 3. Cache dataset_id so phi filter needs zero extra flags ───────────── |
| 50 | + if dataset_id: |
| 51 | + _save_state({"dataset_id": dataset_id}) |
| 52 | + console.print( |
| 53 | + f"\n[dim]dataset_id [{_C_BLUE}]{dataset_id}[/] cached — " |
| 54 | + f"run [bold]phi filter[/] to start scoring.[/]" |
| 55 | + ) |
| 56 | + |
| 57 | + # ── 4. Print step-by-step guide ────────────────────────────────────────── |
| 58 | + if message: |
| 59 | + console.print(f"\n[dim]{message}[/]") |
| 60 | + |
| 61 | + if dataset_id: |
| 62 | + upload_step = "[dim] (skipped — dataset already ready)[/]" |
| 63 | + else: |
| 64 | + upload_step = f" [{_C_SAND}]phi upload {out}/[/]" |
| 65 | + |
| 66 | + console.print(f""" |
| 67 | +[bold]── Tutorial: PD-L1 binder scoring pipeline ──────────────────[/] |
| 68 | +
|
| 69 | +You have {len(files)} example binder structures in [{_C_BLUE}]{out}/[/]. |
| 70 | +
|
| 71 | +[bold]Step 1 — Upload[/] |
| 72 | +{upload_step} |
| 73 | +
|
| 74 | +[bold]Step 2 — Run the filter pipeline[/] |
| 75 | + [{_C_SAND}]phi filter --preset default --wait[/] |
| 76 | +
|
| 77 | + Runs: ProteinMPNN → ESMFold → AlphaFold2 → score |
| 78 | + Typical runtime: 10–30 min for {len(files)} structures. |
| 79 | +
|
| 80 | +[bold]Step 3 — View scores[/] |
| 81 | + [{_C_SAND}]phi scores[/] |
| 82 | +
|
| 83 | +[bold]Step 4 — Download results[/] |
| 84 | + [{_C_SAND}]phi download --out ./results[/] |
| 85 | +
|
| 86 | +[bold]Dashboard[/] |
| 87 | + [{_C_BLUE}]https://design.dynotx.com/dashboard[/] |
| 88 | +""") |
0 commit comments