bio_tools 0.1.1

Install, run, and inspect computational biology and chemistry tools, e.g. AlphaFold, Boltz, RFdiffusion, and ProteinMPNN
Documentation
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Get the status of each tool by running it with a simple command like `--version`, and confirming
//! the application and exists and is healthy, or if there's a problem. On a tool-by-tool basis,
//! provies additional data like computation devices configured for.

use std::{env, fs, path::PathBuf, time::Duration};

pub use crate::install::{StatusKind, ToolStatus};
use crate::{
    install::{InstallError, Installer},
    run::{CaptureLimits, CommandOutput, CommandSpec, ExitPolicy, RunError},
    tool_definitions::Tool,
};

const STATUS_DIRECTORY: &str = ".bio_tools";

pub(crate) fn record_install(installer: &Installer, tool: Tool) -> Result<(), InstallError> {
    let directory = installer
        .config
        .layout
        .environments_root
        .join(STATUS_DIRECTORY);

    fs::create_dir_all(&directory).map_err(|error| {
        InstallError::io(
            format!("unable to create status directory {}", directory.display()),
            error,
        )
    })?;

    let marker = directory.join(format!("{}.installed", tool.slug()));
    fs::write(&marker, format!("{}\n", tool.slug())).map_err(|error| {
        InstallError::io(
            format!("unable to write installation marker {}", marker.display()),
            error,
        )
    })
}

pub(crate) fn forget_install(installer: &Installer, tool: Tool) -> Result<(), InstallError> {
    let marker = marker_path(installer, tool);
    match fs::remove_file(&marker) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(InstallError::io(
            format!(
                "unable to remove the installation marker {}",
                marker.display()
            ),
            error,
        )),
    }
}

fn marker_path(installer: &Installer, tool: Tool) -> PathBuf {
    installer
        .config
        .layout
        .environments_root
        .join(STATUS_DIRECTORY)
        .join(format!("{}.installed", tool.slug()))
}

/// Quickly inspect every tool installed by this crate without launching its application code.
///
/// The returned order is the stable, user-facing [`Tool::ALL`] order.
pub fn list_quick(installer: &Installer) -> Vec<(Tool, ToolStatus)> {
    Tool::ALL
        .into_iter()
        .filter(|tool| marker_path(installer, *tool).is_file())
        .map(|tool| (tool, status_quick(installer, tool)))
        .collect()
}

/// Fully probe every tool installed by this crate using the supplied installation layout.
///
/// Unlike [`list_quick`], this launches each application and, where applicable, imports its
/// compute framework to determine whether its environment can use a GPU.
pub fn list_full(installer: &Installer) -> Vec<(Tool, ToolStatus)> {
    Tool::ALL
        .into_iter()
        .filter(|tool| marker_path(installer, *tool).is_file())
        .map(|tool| (tool, status_full(installer, tool)))
        .collect()
}

/// Backwards-compatible alias for [`list_full`].
pub fn list(installer: &Installer) -> Vec<(Tool, ToolStatus)> {
    list_full(installer)
}

/// Check installation markers, required assets, and the probe executable without launching the
/// tool. This is intended for interactive status pages and other latency-sensitive callers.
pub fn status_quick(installer: &Installer, tool: Tool) -> ToolStatus {
    let was_installed = match preliminary_check(installer, tool) {
        Ok(was_installed) => was_installed,
        Err(status) => return status,
    };

    let command = if tool == Tool::AlphaFold3 {
        match alphafold3_command(installer, was_installed) {
            Ok(command) => Some(command),
            Err(status) => return status,
        }
    } else {
        probe_command(installer, tool)
    };

    let Some(command) = command else {
        return if was_installed {
            pass(
                "The bio_tools installation recipe completed successfully.",
                None,
            )
        } else {
            not_found(format!(
                "No bio_tools installation marker was found for {}.",
                tool.name()
            ))
        };
    };

    let executable = PathBuf::from(command.program.as_os_str());
    if !executable.is_file() {
        return missing_or_broken(
            was_installed,
            format!("Missing status executable at {}.", executable.display()),
        );
    }

    // A named Conda probe starts the shared Conda executable, not a file inside the tool's
    // environment. Its install marker and required assets are the quick evidence that the
    // environment recipe completed; without that marker, finding Conda says nothing about the
    // particular tool.
    if (matches!(tool, Tool::BindCraft | Tool::Genie3)
        || (tool == Tool::Germinal && !installer.venv_python(tool.slug()).is_file()))
        && !was_installed
    {
        return not_found(format!(
            "No bio_tools installation marker was found for {}.",
            tool.name()
        ));
    }

    pass(
        format!(
            "Found {}'s executable and required installation files.",
            tool.name()
        ),
        None,
    )
}

/// Launch a tool with its help/version probe and inspect its compute device where applicable.
pub fn status_full(installer: &Installer, tool: Tool) -> ToolStatus {
    let was_installed = match preliminary_check(installer, tool) {
        Ok(was_installed) => was_installed,
        Err(status) => return status,
    };

    if tool == Tool::AlphaFold3 {
        return check_alphafold3(installer, was_installed);
    }

    let command = match probe_command(installer, tool) {
        Some(command) => command,
        None if was_installed => {
            return pass(
                "The bio_tools installation recipe completed successfully.",
                None,
            );
        }
        None => {
            return not_found(format!(
                "No bio_tools installation marker was found for {}.",
                tool.name()
            ));
        }
    };

    let output = match run_probe(command) {
        Ok(output) => output,
        Err(cause) if cause.io_error_kind() == Some(std::io::ErrorKind::NotFound) => {
            return missing_or_broken(was_installed, cause.to_string());
        }
        Err(cause) => return error(format!("Could not probe {}: {cause}", tool.name())),
    };
    let detail = output_detail(&output);
    if !output.status.success()
        && (detail.is_empty()
            || detail
                .trim_start()
                .starts_with("Traceback (most recent call last):"))
    {
        return error(if detail.is_empty() {
            format!("The {} status probe exited unsuccessfully.", tool.name())
        } else {
            detail
        });
    }

    let detail = if detail.is_empty() {
        format!("{} answered its status probe.", tool.name())
    } else {
        detail
            .lines()
            .next()
            .unwrap_or_default()
            .chars()
            .take(200)
            .collect()
    };
    pass(detail, probe_device(installer, tool))
}

fn preliminary_check(installer: &Installer, tool: Tool) -> Result<bool, ToolStatus> {
    if !tool.is_supported() {
        return Err(error(format!(
            "{} is not supported on this operating system.",
            tool.name()
        )));
    }

    let was_installed = marker_path(installer, tool).is_file();

    for (path, description) in required_paths(installer, tool) {
        if !path.exists() {
            return Err(missing_or_broken(
                was_installed,
                format!("Missing {description} at {}.", path.display()),
            ));
        }
    }
    Ok(was_installed)
}

fn check_alphafold3(installer: &Installer, was_installed: bool) -> ToolStatus {
    let command = match alphafold3_command(installer, was_installed) {
        Ok(command) => command,
        Err(status) => return status,
    };
    match run_probe(command) {
        Ok(output) if output.status.success() || !output_detail(&output).is_empty() => pass(
            output_detail(&output)
                .lines()
                .next()
                .unwrap_or("AlphaFold 3 answered its status probe."),
            probe_device(installer, Tool::AlphaFold3),
        ),
        Ok(output) => error(format!(
            "AlphaFold 3 did not answer its status probe: {}",
            output_detail(&output)
        )),
        Err(cause) => missing_or_broken(was_installed, cause.to_string()),
    }
}

fn alphafold3_command(
    installer: &Installer,
    was_installed: bool,
) -> Result<CommandSpec, ToolStatus> {
    let Some(runner) = env::var_os("ALPHAFOLD3_RUNNER").map(PathBuf::from) else {
        return Err(missing_or_broken(
            was_installed,
            "The Python environment is prepared, but ALPHAFOLD3_RUNNER is not configured."
                .to_owned(),
        ));
    };
    for (name, variable) in [
        ("model directory", "ALPHAFOLD3_MODEL_DIR"),
        ("database directory", "ALPHAFOLD3_DATABASE_DIR"),
    ] {
        let Some(path) = env::var_os(variable).map(PathBuf::from) else {
            return Err(missing_or_broken(
                was_installed,
                format!("{variable} is not configured."),
            ));
        };
        if !path.exists() {
            return Err(missing_or_broken(
                was_installed,
                format!(
                    "The configured {name} does not exist at {}.",
                    path.display()
                ),
            ));
        }
    }
    if !runner.is_file() {
        return Err(missing_or_broken(
            was_installed,
            format!("ALPHAFOLD3_RUNNER does not exist at {}.", runner.display()),
        ));
    }
    Ok(installer
        .tool_python_command(Tool::AlphaFold3)
        .arg(&runner)
        .arg("--help"))
}

/// For commands directly runnable from CLI, launch them with a "help" or "version" command as
/// a coarse status check.
fn probe_command(installer: &Installer, tool: Tool) -> Option<CommandSpec> {
    // The executable's name comes from `Tool::console_script`, which is not always the slug; only
    // the probe argument is decided here.
    let arguments: &[&str] = match tool {
        Tool::OpenDde => &["--version"],
        Tool::Boltz2
        | Tool::Chai1
        | Tool::Protenix
        | Tool::EsmFold2
        | Tool::ImmuneBuilder
        | Tool::BoltzGen
        | Tool::BioPhi
        | Tool::ProteinMpnnDdg
        | Tool::Anarcii
        | Tool::AggreScan3d
        | Tool::Mber => &["--help"],
        Tool::IgBlast => {
            let executable = installer.tools_root().join("igblast/bin/igblastn");
            return Some(CommandSpec::new(executable).arg("-version"));
        }
        Tool::Gromacs => {
            let prefix = installer
                .config
                .gromacs_prefix
                .clone()
                .unwrap_or_else(|| installer.tools_root().join("gromacs"));
            return Some(CommandSpec::new(prefix.join("bin/gmx")).arg("--version"));
        }
        Tool::BindCraft | Tool::Genie3 => {
            return named_conda_probe(installer, tool);
        }
        Tool::Germinal => {
            // The micromamba branch of the recipe builds a prefix environment; only the legacy
            // install.sh branch leaves a named Conda environment behind.
            let python = installer.venv_python(tool.slug());
            if python.is_file() {
                return Some(installer.tool_python_command(tool).arg("--version"));
            }
            return named_conda_probe(installer, tool);
        }
        _ => {
            return Some(installer.tool_python_command(tool).arg("--version"));
        }
    };
    Some(installer.tool_command(tool).args(arguments))
}

fn named_conda_probe(installer: &Installer, tool: Tool) -> Option<CommandSpec> {
    let executable = installer.conda_executable_path();
    let environment = tool.conda_environment()?;
    Some(CommandSpec::new(executable).args(["run", "--name", environment, "python", "--version"]))
}

fn run_probe(command: CommandSpec) -> Result<CommandOutput, RunError> {
    crate::run::run(
        &command
            .exit_policy(ExitPolicy::AllowFailure)
            .capture_limits(CaptureLimits::new(4_000, 4_000)),
    )
}

fn required_paths(installer: &Installer, tool: Tool) -> Vec<(PathBuf, &'static str)> {
    let root = installer.tools_root();
    let relative: &[(&str, &str)] = match tool {
        Tool::BindCraft => &[(
            "BindCraft/params/params_model_5_ptm.npz",
            "BindCraft weights",
        )],
        Tool::IgBlast => &[("igblast/internal_data", "IgBLAST internal data")],
        Tool::ProteinMpnn => &[
            ("ProteinMPNN/protein_mpnn_run.py", "ProteinMPNN runner"),
            (
                "ProteinMPNN/vanilla_model_weights/v_48_020.pt",
                "ProteinMPNN weights",
            ),
            ("ProteinMPNN/abmpnn_weights/v_48_020.pt", "ABMPNN weights"),
        ],
        Tool::LigandMpnn => &[
            ("LigandMPNN/run.py", "LigandMPNN runner"),
            (
                "LigandMPNN/model_params/ligandmpnn_v_32_010_25.pt",
                "LigandMPNN weights",
            ),
        ],
        Tool::RfDiffusion => &[
            ("RFdiffusion/scripts/run_inference.py", "RFdiffusion runner"),
            ("RFdiffusion/models/Base_ckpt.pt", "RFdiffusion weights"),
        ],
        Tool::RfAntibody => &[("RFantibody/weights/RFdiffusion_Ab.pt", "RFantibody weights")],
        Tool::Germinal => &[("germinal/run_germinal.py", "Germinal runner")],
        Tool::Mber => &[("mber-open", "mBER checkout")],
        Tool::IgDesign => &[("igdesign/predict.py", "IgDesign runner")],
        Tool::ThermoMpnn => &[(
            "ThermoMPNN/analysis/custom_inference.py",
            "ThermoMPNN runner",
        )],
        Tool::Genie3 => &[("genie3/pretrained", "Genie 3 weights")],
        Tool::DeepSp => &[("DeepSP/deepsp_cli.py", "DeepSP runner")],
        Tool::DeepImmuno => &[("DeepImmuno/deepimmuno-cnn.py", "DeepImmuno runner")],
        Tool::TlImmuno2 => &[("TLimmuno2/Python/TLimmuno2.py", "TLimmuno2 runner")],
        Tool::NetSolP => &[("NetSolP-1.0/PredictionServer", "NetSolP checkout")],
        Tool::DeepStabP => &[("deepStabP/src/Api/deepstabp_cli.py", "DeepSTABp runner")],
        Tool::DlkCat => &[(
            "DLKcat/DeeplearningApproach/Code/example/prediction_for_input.py",
            "DLKcat runner",
        )],
        Tool::CatPred => &[("CatPred/predict.py", "CatPred runner")],
        Tool::Placer => &[("PLACER/run_PLACER.py", "PLACER runner")],
        Tool::HighFold => &[("HighFold", "HighFold checkout")],
        Tool::AntiFold => &[("AntiFold", "AntiFold checkout")],
        _ => &[],
    };
    relative
        .iter()
        .map(|(path, description)| (root.join(path), *description))
        .collect()
}

fn probe_device(installer: &Installer, tool: Tool) -> Option<String> {
    let framework = if matches!(
        tool,
        Tool::HighFold | Tool::ProteinMpnnDdg | Tool::Germinal | Tool::Mber
    ) {
        "jax"
    } else if matches!(
        tool,
        Tool::OpenDde
            | Tool::Boltz2
            | Tool::Chai1
            | Tool::Protenix
            | Tool::EsmFold2
            | Tool::ImmuneBuilder
            | Tool::BoltzGen
            | Tool::ProteinMpnn
            | Tool::LigandMpnn
            | Tool::RfDiffusion
            | Tool::RfAntibody
            | Tool::IgDesign
            | Tool::ThermoMpnn
            | Tool::Genie3
            | Tool::DeepSp
            | Tool::NetSolP
            | Tool::DeepStabP
            | Tool::DlkCat
            | Tool::CatPred
            | Tool::Anarcii
            | Tool::Placer
    ) {
        "torch"
    } else {
        return None;
    };
    let python = installer.venv_python(tool.slug());
    if !python.is_file() {
        return None;
    }
    let snippet = if framework == "jax" {
        "import jax; print('GPU' if any(d.platform == 'gpu' for d in jax.devices()) else 'CPU')"
    } else {
        "import torch; print('GPU' if torch.cuda.is_available() else 'CPU')"
    };
    let command = installer
        .tool_python_command(tool)
        .args(["-c", snippet])
        .timeout(Duration::from_secs(300))
        .capture_limits(CaptureLimits::new(4_000, 4_000));
    let output = crate::run::run(&command).ok()?;
    if !output.status.success() {
        return None;
    }
    let value = output.stdout_lossy().trim().to_owned();
    matches!(value.as_str(), "GPU" | "CPU").then_some(value)
}

fn output_detail(output: &CommandOutput) -> String {
    let stdout = output.stdout_lossy();
    let stderr = output.stderr_lossy();
    format!("{stdout}{stderr}")
        .trim()
        .chars()
        .rev()
        .take(4_000)
        .collect::<String>()
        .chars()
        .rev()
        .collect()
}

fn pass(detail: impl Into<String>, device: Option<String>) -> ToolStatus {
    ToolStatus {
        result: StatusKind::Pass,
        detail: detail.into(),
        device,
    }
}

fn not_found(detail: impl Into<String>) -> ToolStatus {
    ToolStatus {
        result: StatusKind::NotFound,
        detail: detail.into(),
        device: None,
    }
}

fn error(detail: impl Into<String>) -> ToolStatus {
    ToolStatus {
        result: StatusKind::Error,
        detail: detail.into(),
        device: None,
    }
}

fn missing_or_broken(was_installed: bool, detail: String) -> ToolStatus {
    if was_installed {
        error(detail)
    } else {
        not_found(detail)
    }
}