run_pretrained_alphafold.py 3 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Copyright 2021 AlQuraishi Laboratory
# Copyright 2021 DeepMind Technologies Limited
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
17
18
19

# A hack to get OpenMM and PyTorch to peacefully coexist
os.environ["OPENMM_DEFAULT_PLATFORM"] = "OpenCL"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
20
21
22
23
24
25
26
27
28

import math
import pickle
import time
import torch
import torch.nn as nn
import numpy as np

from config import model_config
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
29
from openfold.model.model import AlphaFold
30
31
from openfold.np import residue_constants, protein

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
32
33
import openfold.np.relax.relax as relax
from openfold.utils.import_weights import (
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
34
35
    import_jax_weights_,
)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
36
from openfold.utils.tensor_utils import (
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
37
38
39
40
41
42
    tree_map,
    tensor_tree_map,
)


MODEL_NAME = "model_1"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
43
MODEL_DEVICE = "cuda:4"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
44
PARAM_PATH = "openfold/resources/params/params_model_1.npz"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
45
FEAT_PATH = "tests/test_data/sample_feats.pickle"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
46
47
48
49
50

config = model_config(MODEL_NAME)
model = AlphaFold(config.model)
model = model.eval()
import_jax_weights_(model, PARAM_PATH)
51
model = model.to(MODEL_DEVICE)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
52
53
54
55
56

with open(FEAT_PATH, "rb") as f:
    batch = pickle.load(f)

with torch.no_grad():
57
58
59
60
61
62
63
64
    batch = {k:torch.as_tensor(v, device=MODEL_DEVICE) for k,v in batch.items()}
    
    longs = [
        "aatype", 
        "template_aatype", 
        "extra_msa", 
        "residx_atom37_to_atom14",
        "residx_atom14_to_atom37",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
65
66
        "true_msa",
        "residue_index",
67
68
69
70
71
72
73
74
    ]
    for l in longs:
        batch[l] = batch[l].long()
    
    # Move the recycling dimension to the end
    move_dim = lambda t: t.permute(*range(len(t.shape))[1:], 0).contiguous()
    batch = tensor_tree_map(move_dim, batch)

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    t = time.time()
    out = model(batch)
    print(f"Inference time: {time.time() - t}")

# Toss out the recycling dimensions --- we don't need them anymore
batch = tensor_tree_map(lambda x: np.array(x[..., -1].cpu()), batch)
out = tensor_tree_map(lambda x: np.array(x.cpu()), out)

plddt = out["plddt"]
mean_plddt = np.mean(plddt)

plddt_b_factors = np.repeat(
    plddt[..., None], residue_constants.atom_type_num, axis=-1
)

unrelaxed_protein = protein.from_prediction(
    features=batch,
    result=out,
    b_factors=plddt_b_factors
)

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
96
97
os.environ["CUDA_VISIBLE_DEVICES"] = "7"

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
98
99
100
101
102
amber_relaxer = relax.AmberRelaxation(
    **config.relax
)

# Relax the prediction.
103
t = time.time()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
104
relaxed_pdb_str, _, _ = amber_relaxer.process(prot=unrelaxed_protein)
105
print(f"Relaxation time: {time.time() - t}")
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
106
107
108
109
110
111

# Save the relaxed PDB.
output_dir = '.'
relaxed_output_path = os.path.join(output_dir, f'relaxed_{MODEL_NAME}.pdb')
with open(relaxed_output_path, 'w') as f:
    f.write(relaxed_pdb_str)