


"""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     lowercase__	(  lowerCamelCase     :  str				,	lowerCamelCase     :  List[str]				,	lowerCamelCase     :  Optional[int]				)					->     torch.Tensor:
  ...







@overload
def     lowercase__	(  lowerCamelCase     :  Optional[Any]				,	lowerCamelCase     :  Tuple				,	lowerCamelCase     :  Any				)					->     Tuple[torch.Tensor, torch.Tensor]:
  ...







def     lowercase__	(  lowerCamelCase     :  List[Any]				,	lowerCamelCase     :  Any				,	lowerCamelCase     :  Dict				)					->     Any:
  lowerCAmelCase__    :    Union[str, Any]								=					aatype == rc.restype_order["G"]
  lowerCAmelCase__    :    List[str]								=					rc.atom_order["CA"]
  lowerCAmelCase__    :    Optional[int]								=					rc.atom_order["CB"]
  lowerCAmelCase__    :    List[Any]								=					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:
    lowerCAmelCase__    :    str								=					torch.where(
        lowerCamelCase				,	all_atom_masks[..., ca_idx]				,	all_atom_masks[..., cb_idx]				,	)
    return pseudo_beta, pseudo_beta_mask
  else:
    return pseudo_beta







def     lowercase__	(  lowerCamelCase     :  Any				,	lowerCamelCase     :  int				)					->     torch.Tensor:
  lowerCAmelCase__    :    Dict								=					batched_gather(
      lowerCamelCase				,	batch["residx_atom37_to_atom14"]				,	dim=-2				,	no_batch_dims=len(atomaa.shape[:-2]				)				,	)

  lowerCAmelCase__    :    int								=					atomaa_data * batch["atom37_atom_exists"][..., None]

  return atomaa_data







def     lowercase__	(  lowerCamelCase     :  Dict				)					->     torch.Tensor:
  lowerCAmelCase__    :    Union[str, Any]								=					template_feats["template_aatype"]
  lowerCAmelCase__    :    Any								=					template_feats["template_torsion_angles_sin_cos"]
  lowerCAmelCase__    :    int								=					template_feats["template_alt_torsion_angles_sin_cos"]
  lowerCAmelCase__    :    Dict								=					template_feats["template_torsion_angles_mask"]
  lowerCAmelCase__    :    str								=					torch.cat(
      [
          nn.functional.one_hot(lowerCamelCase				,	2_2				),
          torsion_angles_sin_cos.reshape(*torsion_angles_sin_cos.shape[:-2]				,	1_4				),
          alt_torsion_angles_sin_cos.reshape(*alt_torsion_angles_sin_cos.shape[:-2]				,	1_4				),
          torsion_angles_mask,
      ]				,	dim=-1				,	)

  return template_angle_feat







def     lowercase__	(  lowerCamelCase     :  str				,	lowerCamelCase     :  Any				,	lowerCamelCase     :  List[Any]				,	lowerCamelCase     :  Any				,	lowerCamelCase     :  Optional[Any] = False				,	lowerCamelCase     :  Dict = 1e-20				,	lowerCamelCase     :  Tuple = 1e8				,	)					->     torch.Tensor:
  lowerCAmelCase__    :    List[str]								=					batch["template_pseudo_beta_mask"]
  lowerCAmelCase__    :    Any								=					template_mask[..., None] * template_mask[..., None, :]

  # Compute distogram (this seems to differ slightly from Alg. 5)
  lowerCAmelCase__    :    Dict								=					batch["template_pseudo_beta"]
  lowerCAmelCase__    :    Any								=					torch.sum((tpb[..., None, :] - tpb[..., None, :, :]) ** 2				,	dim=-1				,	keepdim=lowerCamelCase				)
  lowerCAmelCase__    :    int								=					torch.linspace(lowerCamelCase				,	lowerCamelCase				,	lowerCamelCase				,	device=tpb.device				) ** 2
  lowerCAmelCase__    :    List[str]								=					torch.cat([lower[1:], lower.new_tensor([inf]				)]				,	dim=-1				)
  lowerCAmelCase__    :    List[str]								=					((dgram > lower) * (dgram < upper)).type(dgram.dtype				)

  lowerCAmelCase__    :    Optional[Any]								=					[dgram, template_mask_ad[..., None]]

  lowerCAmelCase__    :    Dict								=					nn.functional.one_hot(
      batch["template_aatype"]				,	rc.restype_num + 2				,	)

  lowerCAmelCase__    :    str								=					batch["template_aatype"].shape[-1]
  to_concat.append(aatype_one_hot[..., None, :, :].expand(*aatype_one_hot.shape[:-2]				,	lowerCamelCase				,	-1				,	-1				)				)
  to_concat.append(aatype_one_hot[..., None, :].expand(*aatype_one_hot.shape[:-2]				,	-1				,	lowerCamelCase				,	-1				)				)

  lowerCAmelCase__				,		lowerCAmelCase__				,		lowerCAmelCase__    :    List[str]								=					[rc.atom_order[a] for a in ["N", "CA", "C"]]
  lowerCAmelCase__    :    Any								=					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=lowerCamelCase				,	)
  lowerCAmelCase__    :    Tuple								=					rigids.get_trans()[..., None, :, :]
  lowerCAmelCase__    :    int								=					rigids[..., None].invert_apply(lowerCamelCase				)

  lowerCAmelCase__    :    Union[str, Any]								=					torch.rsqrt(eps + torch.sum(rigid_vec**2				,	dim=-1				)				)

  lowerCAmelCase__    :    str								=					batch["template_all_atom_mask"]
  lowerCAmelCase__    :    Tuple								=					t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
  lowerCAmelCase__    :    Optional[int]								=					template_mask[..., None] * template_mask[..., None, :]

  lowerCAmelCase__    :    str								=					inv_distance_scalar * template_mask_ad
  lowerCAmelCase__    :    Union[str, Any]								=					rigid_vec * inv_distance_scalar[..., None]

  if not use_unit_vector:
    lowerCAmelCase__    :    Dict								=					unit_vector * 0.0

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

  lowerCAmelCase__    :    int								=					torch.cat(lowerCamelCase				,	dim=-1				)
  lowerCAmelCase__    :    str								=					act * template_mask_ad[..., None]

  return act







def     lowercase__	(  lowerCamelCase     :  Dict				)					->     torch.Tensor:
  lowerCAmelCase__    :    str								=					nn.functional.one_hot(batch["extra_msa"]				,	2_3				)
  lowerCAmelCase__    :    Dict								=					[
      msa_ahot,
      batch["extra_has_deletion"].unsqueeze(-1				),
      batch["extra_deletion_value"].unsqueeze(-1				),
  ]
  return torch.cat(lowerCamelCase				,	dim=-1				)







def     lowercase__	(  lowerCamelCase     :  List[Any]				,	lowerCamelCase     :  Union[str, Any]				,	lowerCamelCase     :  Tuple				,	lowerCamelCase     :  int				,	)					->     Rigid:
  lowerCAmelCase__    :    Optional[int]								=					rrgdf[aatype, ...]

  # [*, N, 8] transformations, i.e.
  #   One [*, N, 8, 3, 3] rotation matrix and
  #   One [*, N, 8, 3]    translation matrix
  lowerCAmelCase__    :    List[Any]								=					r.from_tensor_axa(lowerCamelCase				)

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

  # [*, N, 8, 2]
  lowerCAmelCase__    :    List[Any]								=					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.

  lowerCAmelCase__    :    List[Any]								=					alpha.new_zeros(default_r.get_rots().get_rot_mats().shape				)
  lowerCAmelCase__    :    Optional[int]								=					1
  lowerCAmelCase__    :    Optional[int]								=					alpha[..., 1]
  lowerCAmelCase__    :    Optional[Any]								=					-alpha[..., 0]
  lowerCAmelCase__    :    Optional[Any]								=					alpha

  lowerCAmelCase__    :    Optional[int]								=					default_r.compose(Rigid(Rotation(rot_mats=lowerCamelCase				)				,	lowerCamelCase				)				)

  lowerCAmelCase__    :    Optional[Any]								=					all_frames[..., 5]
  lowerCAmelCase__    :    Optional[Any]								=					all_frames[..., 6]
  lowerCAmelCase__    :    List[Any]								=					all_frames[..., 7]

  lowerCAmelCase__    :    int								=					all_frames[..., 4]
  lowerCAmelCase__    :    List[Any]								=					chia_frame_to_bb.compose(lowerCamelCase				)
  lowerCAmelCase__    :    str								=					chia_frame_to_bb.compose(lowerCamelCase				)
  lowerCAmelCase__    :    Union[str, Any]								=					chia_frame_to_bb.compose(lowerCamelCase				)

  lowerCAmelCase__    :    Dict								=					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				,	)

  lowerCAmelCase__    :    Dict								=					r[..., None].compose(lowerCamelCase				)

  return all_frames_to_global







def     lowercase__	(  lowerCamelCase     :  Optional[Any]				,	lowerCamelCase     :  Dict				,	lowerCamelCase     :  str				,	lowerCamelCase     :  Any				,	lowerCamelCase     :  Dict				,	lowerCamelCase     :  Any				,	)					->     torch.Tensor:
  lowerCAmelCase__    :    Any								=					group_idx[aatype, ...]

  # [*, N, 14, 8]
  lowerCAmelCase__    :    Any								=					nn.functional.one_hot(
      lowerCamelCase				,	num_classes=default_frames.shape[-3]				,	)

  # [*, N, 14, 8]
  lowerCAmelCase__    :    Union[str, Any]								=					r[..., None, :] * group_mask_one_hot

  # [*, N, 14]
  lowerCAmelCase__    :    Optional[Any]								=					t_atoms_to_global.map_tensor_fn(lambda lowerCamelCase				: torch.sum(lowerCamelCase				,	dim=-1				)				)

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

  # [*, N, 14, 3]
  lowerCAmelCase__    :    List[Any]								=					lit_positions[aatype, ...]
  lowerCAmelCase__    :    List[Any]								=					t_atoms_to_global.apply(lowerCamelCase				)
  lowerCAmelCase__    :    Any								=					pred_positions * atom_mask

  return pred_positions




"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCAmelCase    				=      {"configuration_sew_d": ["SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWDConfig"]}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __UpperCAmelCase    				=      [
       "SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST",
       "SEWDForCTC",
       "SEWDForSequenceClassification",
       "SEWDModel",
       "SEWDPreTrainedModel",
   ]

if TYPE_CHECKING:
   from .configuration_sew_d import SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWDConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_sew_d import (
          SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST,
          SEWDForCTC,
          SEWDForSequenceClassification,
          SEWDModel,
          SEWDPreTrainedModel,
      )


else:
   import sys

   __UpperCAmelCase    				=      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
