

'''simple docstring'''




from typing import Dict, Tuple, overload

import torch
import torch.types
from torch import nn

from . import residue_constants as rc
from .rigid_utils import Rigid, Rotation
from .tensor_utils import batched_gather


@overload
def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   None						):
  ...


@overload
def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor						):
  ...


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Union[str, Any]       ,   SCREAMING_SNAKE_CASE      :   Tuple       ,   SCREAMING_SNAKE_CASE      :   Union[str, Any]						):
  UpperCAmelCase													=	aatype == rc.restype_order['G']
  UpperCAmelCase													=	rc.atom_order['CA']
  UpperCAmelCase													=	rc.atom_order['CB']
  UpperCAmelCase													=	torch.where(
      is_gly[..., None].expand(*((-1,) * len(is_gly.shape						))       ,   3						)       ,   all_atom_positions[..., ca_idx, :]       ,   all_atom_positions[..., cb_idx, :]       ,   )

  if all_atom_masks is not None:
    UpperCAmelCase													=	torch.where(
        SCREAMING_SNAKE_CASE       ,   all_atom_masks[..., ca_idx]       ,   all_atom_masks[..., cb_idx]       ,   )
    return pseudo_beta, pseudo_beta_mask
  else:
    return pseudo_beta


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	batched_gather(
      SCREAMING_SNAKE_CASE       ,   batch['residx_atom37_to_atom14']       ,   dim=-2       ,   no_batch_dims=len(atomaa.shape[:-2]						)       ,   )

  UpperCAmelCase													=	atomaa_data * batch['atom37_atom_exists'][..., None]

  return atomaa_data


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	template_feats['template_aatype']
  UpperCAmelCase													=	template_feats['template_torsion_angles_sin_cos']
  UpperCAmelCase													=	template_feats['template_alt_torsion_angles_sin_cos']
  UpperCAmelCase													=	template_feats['template_torsion_angles_mask']
  UpperCAmelCase													=	torch.cat(
      [
          nn.functional.one_hot(SCREAMING_SNAKE_CASE       ,   22						),
          torsion_angles_sin_cos.reshape(*torsion_angles_sin_cos.shape[:-2]       ,   14						),
          alt_torsion_angles_sin_cos.reshape(*alt_torsion_angles_sin_cos.shape[:-2]       ,   14						),
          torsion_angles_mask,
      ]       ,   dim=-1       ,   )

  return template_angle_feat


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]       ,   SCREAMING_SNAKE_CASE      :   torch.types.Number       ,   SCREAMING_SNAKE_CASE      :   torch.types.Number       ,   SCREAMING_SNAKE_CASE      :   int       ,   SCREAMING_SNAKE_CASE      :   bool = False       ,   SCREAMING_SNAKE_CASE      :   float = 1E-20       ,   SCREAMING_SNAKE_CASE      :   float = 1E8       ,   ):
  UpperCAmelCase													=	batch['template_pseudo_beta_mask']
  UpperCAmelCase													=	template_mask[..., None] * template_mask[..., None, :]

  # Compute distogram (this seems to differ slightly from Alg. 5)
  UpperCAmelCase													=	batch['template_pseudo_beta']
  UpperCAmelCase													=	torch.sum((tpb[..., None, :] - tpb[..., None, :, :]) ** 2       ,   dim=-1       ,   keepdim=SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	torch.linspace(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   device=tpb.device						) ** 2
  UpperCAmelCase													=	torch.cat([lower[1:], lower.new_tensor([inf]						)]       ,   dim=-1						)
  UpperCAmelCase													=	((dgram > lower) * (dgram < upper)).type(dgram.dtype						)

  UpperCAmelCase													=	[dgram, template_mask_ad[..., None]]

  UpperCAmelCase													=	nn.functional.one_hot(
      batch['template_aatype']       ,   rc.restype_num + 2       ,   )

  UpperCAmelCase													=	batch['template_aatype'].shape[-1]
  to_concat.append(aatype_one_hot[..., None, :, :].expand(*aatype_one_hot.shape[:-2]       ,   SCREAMING_SNAKE_CASE       ,   -1       ,   -1						)						)
  to_concat.append(aatype_one_hot[..., None, :].expand(*aatype_one_hot.shape[:-2]       ,   -1       ,   SCREAMING_SNAKE_CASE       ,   -1						)						)

  UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	[rc.atom_order[a] for a in ['N', 'CA', 'C']]
  UpperCAmelCase													=	Rigid.make_transform_from_reference(
      n_xyz=batch['template_all_atom_positions'][..., n, :]       ,   ca_xyz=batch['template_all_atom_positions'][..., ca, :]       ,   c_xyz=batch['template_all_atom_positions'][..., c, :]       ,   eps=SCREAMING_SNAKE_CASE       ,   )
  UpperCAmelCase													=	rigids.get_trans()[..., None, :, :]
  UpperCAmelCase													=	rigids[..., None].invert_apply(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	torch.rsqrt(eps + torch.sum(rigid_vec**2       ,   dim=-1						)						)

  UpperCAmelCase													=	batch['template_all_atom_mask']
  UpperCAmelCase													=	t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
  UpperCAmelCase													=	template_mask[..., None] * template_mask[..., None, :]

  UpperCAmelCase													=	inv_distance_scalar * template_mask_ad
  UpperCAmelCase													=	rigid_vec * inv_distance_scalar[..., None]

  if not use_unit_vector:
    UpperCAmelCase													=	unit_vector * 0.0

  to_concat.extend(torch.unbind(unit_vector[..., None, :]       ,   dim=-1						)						)
  to_concat.append(template_mask_ad[..., None]						)

  UpperCAmelCase													=	torch.cat(SCREAMING_SNAKE_CASE       ,   dim=-1						)
  UpperCAmelCase													=	act * template_mask_ad[..., None]

  return act


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	nn.functional.one_hot(batch['extra_msa']       ,   23						)
  UpperCAmelCase													=	[
      msa_ahot,
      batch['extra_has_deletion'].unsqueeze(-1						),
      batch['extra_deletion_value'].unsqueeze(-1						),
  ]
  return torch.cat(SCREAMING_SNAKE_CASE       ,   dim=-1						)


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Rigid       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   ):
  # [*, N, 8, 4, 4]
  UpperCAmelCase													=	rrgdf[aatype, ...]

  # [*, N, 8] transformations, i.e.
  #   One [*, N, 8, 3, 3] rotation matrix and
  #   One [*, N, 8, 3]    translation matrix
  UpperCAmelCase													=	r.from_tensor_axa(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	alpha.new_zeros((*((1,) * len(alpha.shape[:-1]						)), 2)						)
  UpperCAmelCase													=	1

  # [*, N, 8, 2]
  UpperCAmelCase													=	torch.cat([bb_rot.expand(*alpha.shape[:-2]       ,   -1       ,   -1						), alpha]       ,   dim=-2						)

  # [*, N, 8, 3, 3]
  # Produces rotation matrices of the form:
  # [
  #   [1, 0  , 0  ],
  #   [0, a_2,-a_1],
  #   [0, a_1, a_2]
  # ]
  # This follows the original code rather than the supplement, which uses
  # different indices.

  UpperCAmelCase													=	alpha.new_zeros(default_r.get_rots().get_rot_mats().shape						)
  UpperCAmelCase													=	1
  UpperCAmelCase													=	alpha[..., 1]
  UpperCAmelCase													=	-alpha[..., 0]
  UpperCAmelCase													=	alpha

  UpperCAmelCase													=	default_r.compose(Rigid(Rotation(rot_mats=SCREAMING_SNAKE_CASE						)       ,   SCREAMING_SNAKE_CASE						)						)

  UpperCAmelCase													=	all_frames[..., 5]
  UpperCAmelCase													=	all_frames[..., 6]
  UpperCAmelCase													=	all_frames[..., 7]

  UpperCAmelCase													=	all_frames[..., 4]
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	Rigid.cat(
      [
          all_frames[..., :5],
          chia_frame_to_bb.unsqueeze(-1						),
          chia_frame_to_bb.unsqueeze(-1						),
          chia_frame_to_bb.unsqueeze(-1						),
      ]       ,   dim=-1       ,   )

  UpperCAmelCase													=	r[..., None].compose(SCREAMING_SNAKE_CASE						)

  return all_frames_to_global



def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Rigid       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   ):
  # [*, N, 14]
  UpperCAmelCase													=	group_idx[aatype, ...]

  # [*, N, 14, 8]
  UpperCAmelCase													=	nn.functional.one_hot(
      SCREAMING_SNAKE_CASE       ,   num_classes=default_frames.shape[-3]       ,   )

  # [*, N, 14, 8]
  UpperCAmelCase													=	r[..., None, :] * group_mask_one_hot

  # [*, N, 14]
  UpperCAmelCase													=	t_atoms_to_global.map_tensor_fn(lambda SCREAMING_SNAKE_CASE						: torch.sum(SCREAMING_SNAKE_CASE       ,   dim=-1						)						)

  # [*, N, 14, 1]
  UpperCAmelCase													=	atom_mask[aatype, ...].unsqueeze(-1						)

  # [*, N, 14, 3]
  UpperCAmelCase													=	lit_positions[aatype, ...]
  UpperCAmelCase													=	t_atoms_to_global.apply(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	pred_positions * atom_mask

  return pred_positions





'''simple docstring'''




from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
  import torch

_a				: List[Any] 							=						logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
  from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):


  def       lowerCamelCase__		(						cls      :   Dict						):
    return cls


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Union[Tuple, List]       ,   SCREAMING_SNAKE_CASE      :   Optional[Union[List["torch.Generator"], "torch.Generator"]] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.device"] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.dtype"] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.layout"] = None       ,   ):
  UpperCAmelCase													=	device
  UpperCAmelCase													=	shape[0]

  UpperCAmelCase													=	layout or torch.strided
  UpperCAmelCase													=	device or torch.device('cpu'						)

  if generator is not None:
    UpperCAmelCase													=	generator.device.type if not isinstance(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) else generator[0].device.type
    if gen_device_type != device.type and gen_device_type == "cpu":
      UpperCAmelCase													=	'cpu'
      if device != "mps":
        logger.info(
            f'''The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'''
            f''' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'''
            f''' slighly speed up this function by passing a generator that was created on the {device} device.'''						)
    elif gen_device_type != device.type and gen_device_type == "cuda":
      raise ValueError(f'''Cannot generate a {device} tensor from a generator of type {gen_device_type}.'''						)

  if isinstance(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						):
    UpperCAmelCase													=	(1,) + shape[1:]
    UpperCAmelCase													=	[
        torch.randn(SCREAMING_SNAKE_CASE       ,   generator=generator[i]       ,   device=SCREAMING_SNAKE_CASE       ,   dtype=SCREAMING_SNAKE_CASE       ,   layout=SCREAMING_SNAKE_CASE						)
        for i in range(SCREAMING_SNAKE_CASE						)
    ]
    UpperCAmelCase													=	torch.cat(SCREAMING_SNAKE_CASE       ,   dim=0						).to(SCREAMING_SNAKE_CASE						)
  else:
    UpperCAmelCase													=	torch.randn(SCREAMING_SNAKE_CASE       ,   generator=SCREAMING_SNAKE_CASE       ,   device=SCREAMING_SNAKE_CASE       ,   dtype=SCREAMING_SNAKE_CASE       ,   layout=SCREAMING_SNAKE_CASE						).to(SCREAMING_SNAKE_CASE						)

  return latents



def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Tuple						):
  if is_torch_version('<'       ,   '2.0.0'						) or not hasattr(SCREAMING_SNAKE_CASE       ,   '_dynamo'						):
    return False
  return isinstance(SCREAMING_SNAKE_CASE       ,   torch._dynamo.eval_frame.OptimizedModule						)


