-
Notifications
You must be signed in to change notification settings - Fork 8
/
modal_boltz.py
156 lines (126 loc) · 4.03 KB
/
modal_boltz.py
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
"""
Boltz-1 https://github.com/jwohlwend/boltz
TODO: adding a custom msa dir is not really supported
The user has to get the msa_dir onto modal so it requires a few more steps.
TODO: use yaml instead of fasta, and convert input fasta to yaml
## Example input:
```
>A|PROTEIN|
MAWTPLLLLLLSHCTGSLSQPVLTQPTSLSASPGASARFTCTLRSGINVGTYRIYWYQQK
PGSLPRYLLRYKSDSDKQQGSGVPSRFSGSKDASTNAGLLLISGLQSEDEADYYCAIWYS
STS
>B|DNA|
ACTGACTGGAAGATTTTTTTTTTTCCCCCGTAGTTTTTACCCGACG
>C|smiles
N[C@@H](Cc1ccc(O)cc1)C(=O)O
```
Then run
```
modal run modal_boltz.py --input-faa test_boltz.fasta
```
"""
import os
from pathlib import Path
import modal
from modal import App, Image
GPU = os.environ.get("GPU", modal.gpu.A100(size="80GB"))
TIMEOUT = int(os.environ.get("TIMEOUT", 60))
CACHE_DIR = "/root/.boltz"
ENTITY_TYPES = {"protein", "dna", "rna", "ccd", "smiles"}
ALLOWED_AAS = "ACDEFGHIKLMNPQRSTVWY"
def download_model():
"""Force download of the Boltz-1 model by running it once"""
from subprocess import run
Path(in_dir := "/tmp/tmp_in_boltz").mkdir(parents=True, exist_ok=True)
open(in_faa := Path(in_dir) / "tmp.fasta", "w").write(">A|PROTEIN|\nMAWTPLLLLLLSH\n")
run(
[
"boltz",
"predict",
str(in_faa),
"--out_dir",
"/tmp",
"--cache",
CACHE_DIR,
"--use_msa_server",
],
check=True,
)
image = (
Image.debian_slim(python_version="3.11")
.micromamba()
.apt_install("wget", "git")
.pip_install(
"colabfold[alphafold-minus-jax]@git+https://github.com/sokrypton/ColabFold"
)
.micromamba_install(
"kalign2=2.04", "hhsuite=3.3.0", channels=["conda-forge", "bioconda"]
)
.run_commands(
'pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html',
gpu="a100",
)
.run_commands("python -m colabfold.download")
.apt_install("build-essential")
.pip_install("boltz")
.run_function(download_model, gpu="a10g")
)
app = App("boltz", image=image)
def fasta_iter(fasta_name):
"""yield stripped seq_ids and seqs"""
from itertools import groupby
with open(fasta_name) as fh:
faiter = (x[1] for x in groupby(fh, lambda line: line.startswith(">")))
for header in faiter:
header = next(header)[1:].strip()
seq = "".join(s.strip() for s in next(faiter))
yield header, seq
@app.function(timeout=TIMEOUT * 60, gpu=GPU)
def boltz(input_faa_str: str, input_faa_name: str = "input.fasta"):
"""Runs Boltz on a fasta.
Fasta can contain protein, DNA, RNA, smiles, ccd
"""
from subprocess import run
Path(in_dir := "/tmp/in_boltz").mkdir(parents=True, exist_ok=True)
Path(out_dir := "/tmp/out_boltz").mkdir(parents=True, exist_ok=True)
in_faa = Path(in_dir) / input_faa_name
if in_faa.suffix == ".faa":
in_faa = in_faa.with_suffix(".fasta")
open(in_faa, "w").write(input_faa_str)
run(
[
"boltz",
"predict",
str(in_faa),
"--out_dir",
str(out_dir),
"--cache",
CACHE_DIR,
"--use_msa_server",
],
check=True,
)
return [
(out_file.relative_to(out_dir), open(out_file, "rb").read())
for out_file in Path(out_dir).glob("**/*")
if Path(out_file).is_file()
]
@app.local_entrypoint()
def main(
input_faa: str,
out_dir: str = "./out/boltz",
run_name: str = None,
):
from datetime import datetime
input_faa_str = open(input_faa).read()
outputs = boltz.remote(
input_faa_str,
Path(input_faa).name,
)
today = datetime.now().strftime("%Y%m%d%H%M")[2:]
out_dir_full = Path(out_dir) / (run_name or today)
for out_file, out_content in outputs:
(Path(out_dir_full) / Path(out_file)).parent.mkdir(parents=True, exist_ok=True)
if out_content:
with open((Path(out_dir_full) / Path(out_file)), "wb") as out:
out.write(out_content)