run_pretrained_alphafold.py 3.06 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
17
18
#import sys
#sys.path.append("lib/conda/lib/python3.9/site-packages")
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
19
20
21
22
23
24
25
26
27

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
28
from openfold.model.model import AlphaFold
29
30
31
32
33
34
from openfold.np import residue_constants, protein

#os.environ["OPENMM_DEFAULT_PLATFORM"] = "CPU"
os.environ["OPENMM_DEFAULT_PLATFORM"] = "OpenCL"
#os.environ["OPENMM_CPU_THREADS"] = "16"

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


MODEL_NAME = "model_1"
MODEL_DEVICE = "cuda:1"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
47
PARAM_PATH = "openfold/resources/params/params_model_1.npz"
48
49
#FEAT_PATH = "tests/test_data/sample_feats.pickle"
FEAT_PATH = "prediction/1OJN_feats.pickle"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
50
51
52
53
54

config = model_config(MODEL_NAME)
model = AlphaFold(config.model)
model = model.eval()
import_jax_weights_(model, PARAM_PATH)
55
model = model.to(MODEL_DEVICE)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
56
57
58
59
60

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

with torch.no_grad():
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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",
    ]
    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
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
    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
)

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)