relax.py 3.26 KB
Newer Older
Augustin-Zidek's avatar
Augustin-Zidek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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.

"""Amber relaxation."""
from typing import Any, Dict, Sequence, Tuple
from alphafold.common import protein
from alphafold.relax import amber_minimize
from alphafold.relax import utils
Tom Ward's avatar
Tom Ward committed
20
import numpy as np
Augustin-Zidek's avatar
Augustin-Zidek committed
21
22
23
24
25
26
27
28
29
30
31


class AmberRelaxation(object):
  """Amber relaxation."""

  def __init__(self,
               *,
               max_iterations: int,
               tolerance: float,
               stiffness: float,
               exclude_residues: Sequence[int],
Augustin Zidek's avatar
Augustin Zidek committed
32
33
               max_outer_iterations: int,
               use_gpu: bool):
Augustin-Zidek's avatar
Augustin-Zidek committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    """Initialize Amber Relaxer.

    Args:
      max_iterations: Maximum number of L-BFGS iterations. 0 means no max.
      tolerance: kcal/mol, the energy tolerance of L-BFGS.
      stiffness: kcal/mol A**2, spring constant of heavy atom restraining
        potential.
      exclude_residues: Residues to exclude from per-atom restraining.
        Zero-indexed.
      max_outer_iterations: Maximum number of violation-informed relax
       iterations. A value of 1 will run the non-iterative procedure used in
       CASP14. Use 20 so that >95% of the bad cases are relaxed. Relax finishes
       as soon as there are no violations, hence in most cases this causes no
       slowdown. In the worst case we do 20 outer iterations.
Augustin Zidek's avatar
Augustin Zidek committed
48
      use_gpu: Whether to run on GPU.
Augustin-Zidek's avatar
Augustin-Zidek committed
49
50
51
52
53
54
55
    """

    self._max_iterations = max_iterations
    self._tolerance = tolerance
    self._stiffness = stiffness
    self._exclude_residues = exclude_residues
    self._max_outer_iterations = max_outer_iterations
Augustin Zidek's avatar
Augustin Zidek committed
56
    self._use_gpu = use_gpu
Augustin-Zidek's avatar
Augustin-Zidek committed
57
58
59
60
61
62
63
64

  def process(self, *,
              prot: protein.Protein) -> Tuple[str, Dict[str, Any], np.ndarray]:
    """Runs Amber relax on a prediction, adds hydrogens, returns PDB string."""
    out = amber_minimize.run_pipeline(
        prot=prot, max_iterations=self._max_iterations,
        tolerance=self._tolerance, stiffness=self._stiffness,
        exclude_residues=self._exclude_residues,
Augustin Zidek's avatar
Augustin Zidek committed
65
66
        max_outer_iterations=self._max_outer_iterations,
        use_gpu=self._use_gpu)
Augustin-Zidek's avatar
Augustin-Zidek committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    min_pos = out['pos']
    start_pos = out['posinit']
    rmsd = np.sqrt(np.sum((start_pos - min_pos)**2) / start_pos.shape[0])
    debug_data = {
        'initial_energy': out['einit'],
        'final_energy': out['efinal'],
        'attempts': out['min_attempts'],
        'rmsd': rmsd
    }
    pdb_str = amber_minimize.clean_protein(prot)
    min_pdb = utils.overwrite_pdb_coordinates(pdb_str, min_pos)
    min_pdb = utils.overwrite_b_factors(min_pdb, prot.b_factors)
    utils.assert_equal_nonterminal_atom_types(
        protein.from_pdb_string(min_pdb).atom_mask,
        prot.atom_mask)
    violations = out['structural_violations'][
        'total_per_residue_violations_mask']
    return min_pdb, debug_data, violations