File size: 6,645 Bytes
6d19a18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | #!/usr/bin/env python3
"""Submit docking-analysis jobs for all configured method/mode combinations."""
from __future__ import annotations
import argparse
import shlex
import subprocess
from pathlib import Path
from typing import Any
import yaml
SCRIPT_DIR = Path(__file__).resolve().parent
REPO_ROOT = SCRIPT_DIR.parent
ANALYSIS_SCRIPT = SCRIPT_DIR / "analyse_docking.py"
DEFAULT_SOFTWARE_SETTINGS = REPO_ROOT / "configs" / "docking" / "software_settings.yaml"
def load_yaml(path: Path) -> dict[str, Any]:
"""Load a YAML file and require a mapping at the top level."""
if not path.is_file():
raise FileNotFoundError(f"YAML file not found: {path}")
with path.open("r", encoding="utf-8") as handle:
data = yaml.safe_load(handle)
if not isinstance(data, dict):
raise ValueError(f"YAML root must be a mapping: {path}")
return data
def configured_runs(config: dict[str, Any]) -> list[tuple[str, str]]:
"""Return all method/mode combinations declared by the docking config."""
modes = config.get("modes")
methods = config.get("methods")
if (
not isinstance(modes, list)
or not modes
or not all(isinstance(x, str) for x in modes)
):
raise ValueError("Docking config 'modes' must be a non-empty list of strings")
if not isinstance(methods, dict) or not methods:
raise ValueError("Docking config 'methods' must be a non-empty mapping")
return [(str(method), mode) for method in methods for mode in modes]
def cpu_profile(settings: dict[str, Any]) -> dict[str, Any]:
"""Return the CPU Slurm profile and validate its worker count."""
profiles = settings.get("profiles")
if not isinstance(profiles, dict) or not isinstance(profiles.get("cpu"), dict):
raise ValueError("Software settings must define profiles.cpu")
profile = profiles["cpu"]
cpus = profile.get("cpus-per-task")
if not isinstance(cpus, int) or cpus < 1:
raise ValueError("profiles.cpu.cpus-per-task must be a positive integer")
return profile
def sbatch_command(
profile: dict[str, Any],
job_name: str,
output_log: Path,
wrap_command: list[str],
) -> list[str]:
"""Build one generic ``sbatch`` command from a profile mapping."""
command = ["sbatch"]
for key, value in profile.items():
if value in (None, "", False):
continue
command.append(f"--{key}" if value is True else f"--{key}={value}")
command += [
f"--job-name={job_name}",
f"--output={output_log}",
f"--wrap={shlex.join(wrap_command)}",
]
return command
def build_parser() -> argparse.ArgumentParser:
"""Build the command-line parser."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"docking_dir",
type=Path,
help="Docking output directory containing method_mode run directories.",
)
parser.add_argument(
"--config", required=True, type=Path, help="Docking experiment YAML file."
)
parser.add_argument(
"--software-settings",
type=Path,
default=DEFAULT_SOFTWARE_SETTINGS,
help=f"Software and Slurm YAML (default: {DEFAULT_SOFTWARE_SETTINGS}).",
)
parser.add_argument(
"--output-dir",
type=Path,
default=None,
help="Analysis root (default: sibling analysis directory).",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Print sbatch commands without submitting.",
)
return parser
def main() -> None:
"""Submit one analysis job for every available configured docking run."""
args = build_parser().parse_args()
if not ANALYSIS_SCRIPT.is_file():
raise FileNotFoundError(f"Analysis script not found: {ANALYSIS_SCRIPT}")
docking_dir = args.docking_dir.expanduser().resolve()
if not docking_dir.is_dir():
raise FileNotFoundError(f"Docking directory not found: {docking_dir}")
config = load_yaml(args.config.expanduser().resolve())
settings = load_yaml(args.software_settings.expanduser().resolve())
profile = cpu_profile(settings)
workers = int(profile["cpus-per-task"])
analysis_dir = (
args.output_dir.expanduser().resolve()
if args.output_dir is not None
else docking_dir.parent / "analysis"
)
log_dir = analysis_dir / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
runs = configured_runs(config)
available_runs = []
missing_runs = []
for method, mode in runs:
run_dir = docking_dir / f"{method}_{mode}"
if run_dir.is_dir():
available_runs.append((method, mode, run_dir))
else:
missing_runs.append(run_dir)
if missing_runs:
print(
"Warning: configured docking run directories are missing "
"and will be skipped:",
flush=True,
)
for run_dir in missing_runs:
print(f" {run_dir}", flush=True)
if not available_runs:
raise FileNotFoundError("None of the configured docking run directories exist.")
submitted = 0
failed = 0
for method, mode, run_dir in available_runs:
job_name = f"ana_{method}_{mode}"
wrap = [
"python",
"-u",
str(ANALYSIS_SCRIPT),
str(run_dir),
"--method",
method,
"--mode",
mode,
"--output-dir",
str(analysis_dir),
"--num-workers",
str(workers),
]
command = sbatch_command(
profile=profile,
job_name=job_name,
output_log=log_dir / f"{job_name}-%j.out",
wrap_command=wrap,
)
if args.dry_run:
print(shlex.join(command))
submitted += 1
continue
try:
subprocess.run(command, check=True)
except subprocess.CalledProcessError as exc:
failed += 1
print(
f"Failed to submit {job_name}: {exc}",
flush=True,
)
else:
submitted += 1
print(f"Submitted {job_name}", flush=True)
print()
if args.dry_run:
print(f"Analysis jobs to submit: {submitted}")
else:
print(f"Successfully submitted analysis jobs: {submitted}")
if failed:
print(f"Failed submissions: {failed}")
if missing_runs:
print(f"Skipped missing runs: {len(missing_runs)}")
if failed:
raise SystemExit(1)
if __name__ == "__main__":
main()
|