# 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."""Amber relaxation."""fromtypingimportAny,Dict,Sequence,Tupleimportnumpyasnpfromdeepfold.commonimportproteinfromdeepfold.relaximportamber_minimize,utils
[docs]classAmberRelaxation(object):"""Amber relaxation."""def__init__(self,*,max_iterations:int,tolerance:float,stiffness:float,exclude_residues:Sequence[int],max_outer_iterations:int,use_gpu:bool,):"""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. use_gpu: Whether to run on GPU """self._max_iterations=max_iterationsself._tolerance=toleranceself._stiffness=stiffnessself._exclude_residues=exclude_residuesself._max_outer_iterations=max_outer_iterationsself._use_gpu=use_gpu
[docs]defprocess(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,max_outer_iterations=self._max_outer_iterations,use_gpu=self._use_gpu,)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"]min_pdb=protein.add_pdb_headers(prot,min_pdb)output_str=min_pdbreturnoutput_str,debug_data,violations