


import dataclasses
import re
import string
from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple

import numpy as np

from . import residue_constants


UpperCAmelCase__   	=							Mapping[str, np.ndarray]
UpperCAmelCase__   	=							Mapping[str, Any]  # Is a nested dict.
UpperCAmelCase__   	=							0.01







@dataclasses.dataclass(frozen=lowerCAmelCase)
class       lowerCamelCase__   :
     SCREAMING_SNAKE_CASE__      			=			42  # [num_res, num_atom_type, 3]

     # Amino-acid type for each residue represented as an integer between 0 and
     # 20, where 20 is 'X'.
     SCREAMING_SNAKE_CASE__      			=			42  # [num_res]

     # Binary float mask to indicate presence of a particular atom. 1.0 if an atom
     # is present and 0.0 if not. This should be used for loss masking.
     SCREAMING_SNAKE_CASE__      			=			42  # [num_res, num_atom_type]

     # Residue index as used in PDB. It is not necessarily continuous or 0-indexed.
     SCREAMING_SNAKE_CASE__      			=			42  # [num_res]

     # B-factors, or temperature factors, of each residue (in sq. angstroms units),
     # representing the displacement of the residue from its ground truth mean
     # value.
     SCREAMING_SNAKE_CASE__      			=			42  # [num_res, num_atom_type]

     # Chain indices for multi-chain predictions
     SCREAMING_SNAKE_CASE__      			=			None

     # Optional remark about the protein. Included as a comment in output PDB
     # files
     SCREAMING_SNAKE_CASE__      			=			None

     # Templates used to generate this protein (prediction-only)
     SCREAMING_SNAKE_CASE__      			=			None

     # Chain corresponding to each parent
     SCREAMING_SNAKE_CASE__      			=			None



def 				UpperCAmelCase_					(							__snake_case						)     ->			Protein:







 """simple docstring"""


 _lowercase							      =r'''(\[[A-Z]+\]\n)'''
 _lowercase							      =[tag.strip() for tag in re.split(__snake_case     ,						__snake_case						) if len(__snake_case						) > 0]
 _lowercase							      =zip(tags[0::2]     ,						[l.split('''\n'''						) for l in tags[1::2]]						)

 _lowercase							      =["N", "CA", "C"]
 _lowercase							      =None
 _lowercase							      =None
 _lowercase							      =None
 for g in groups:
  if "[PRIMARY]" == g[0]:
   _lowercase							      =g[1][0].strip()
   for i in range(len(__snake_case						)						):
    if seq[i] not in residue_constants.restypes:
     _lowercase							      ='''X'''  # FIXME: strings are immutable
   _lowercase							      =np.array(
       [residue_constants.restype_order.get(__snake_case     ,						residue_constants.restype_num						) for res_symbol in seq]						)
  elif "[TERTIARY]" == g[0]:
   _lowercase							      =[]
   for axis in range(3						):
    tertiary.append(list(map(__snake_case     ,						g[1][axis].split()						)						)						)
   _lowercase							      =np.array(__snake_case						)
   _lowercase							      =np.zeros((len(tertiary[0]						) // 3, residue_constants.atom_type_num, 3)						).astype(np.floataa						)
   for i, atom in enumerate(__snake_case						):
    _lowercase							      =np.transpose(tertiary_np[:, i::3]						)
   atom_positions *= PICO_TO_ANGSTROM
  elif "[MASK]" == g[0]:
   _lowercase							      =np.array(list(map({'''-''': 0, '''+''': 1}.get     ,						g[1][0].strip()						)						)						)
   _lowercase							      =np.zeros(
       (
           len(__snake_case						),
           residue_constants.atom_type_num,
       )						).astype(np.floataa						)
   for i, atom in enumerate(__snake_case						):
    _lowercase							      =1
   atom_mask *= mask[..., None]

 assert aatype is not None

 return Protein(
     atom_positions=__snake_case     ,						atom_mask=__snake_case     ,						aatype=__snake_case     ,						residue_index=np.arange(len(__snake_case						)						)     ,						b_factors=__snake_case     ,						)
def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case = 0						)     ->			List[str]:







 """simple docstring"""


 _lowercase							      =[]

 _lowercase							      =prot.remark
 if remark is not None:
  pdb_headers.append(F"REMARK {remark}"						)

 _lowercase							      =prot.parents
 _lowercase							      =prot.parents_chain_index
 if parents is not None and parents_chain_index is not None:
  _lowercase							      =[p for i, p in zip(__snake_case     ,						__snake_case						) if i == chain_id]

 if parents is None or len(__snake_case						) == 0:
  _lowercase							      =['''N/A''']

 pdb_headers.append(F"PARENT {' '.join(__snake_case						)}"						)

 return pdb_headers
def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case						)     ->			str:







 """simple docstring"""


 _lowercase							      =[]
 _lowercase							      =pdb_str.split('''\n'''						)

 _lowercase							      =prot.remark
 if remark is not None:
  out_pdb_lines.append(F"REMARK {remark}"						)

 _lowercase							      =42
 if prot.parents is not None and len(prot.parents						) > 0:
  _lowercase							      =[]
  if prot.parents_chain_index is not None:
   _lowercase							      ={}
   for p, i in zip(prot.parents     ,						prot.parents_chain_index						):
    parent_dict.setdefault(str(__snake_case						)     ,						[]						)
    parent_dict[str(__snake_case						)].append(__snake_case						)

   _lowercase							      =max([int(__snake_case						) for chain_idx in parent_dict]						)
   for i in range(max_idx + 1						):
    _lowercase							      =parent_dict.get(str(__snake_case						)     ,						['''N/A''']						)
    parents_per_chain.append(__snake_case						)
  else:
   parents_per_chain.append(list(prot.parents						)						)
 else:
  _lowercase							      =[['''N/A''']]

 def make_parent_line(__snake_case						) -> str:
  return F"PARENT {' '.join(__snake_case						)}"

 out_pdb_lines.append(make_parent_line(parents_per_chain[0]						)						)

 _lowercase							      =0
 for i, l in enumerate(__snake_case						):
  if "PARENT" not in l and "REMARK" not in l:
   out_pdb_lines.append(__snake_case						)
  if "TER" in l and "END" not in lines[i + 1]:
   chain_counter += 1
   if not chain_counter >= len(__snake_case						):
    _lowercase							      =parents_per_chain[chain_counter]
   else:
    _lowercase							      =['''N/A''']

   out_pdb_lines.append(make_parent_line(__snake_case						)						)

 return "\n".join(__snake_case						)
def 				UpperCAmelCase_					(							__snake_case						)     ->			str:







 """simple docstring"""


 _lowercase							      =residue_constants.restypes + ['''X''']

 def res_atoa(__snake_case						) -> str:
  return residue_constants.restype_atoa.get(restypes[r]     ,						'''UNK'''						)

 _lowercase							      =residue_constants.atom_types

 _lowercase							      =[]

 _lowercase							      =prot.atom_mask
 _lowercase							      =prot.aatype
 _lowercase							      =prot.atom_positions
 _lowercase							      =prot.residue_index.astype(np.intaa						)
 _lowercase							      =prot.b_factors
 _lowercase							      =prot.chain_index

 if np.any(aatype > residue_constants.restype_num						):
  raise ValueError('''Invalid aatypes.'''						)

 _lowercase							      =get_pdb_headers(__snake_case						)
 if len(__snake_case						) > 0:
  pdb_lines.extend(__snake_case						)

 _lowercase							      =aatype.shape[0]
 _lowercase							      =1
 _lowercase							      =0
 _lowercase							      =string.ascii_uppercase
 _lowercase							      =None
 # Add all atom sites.
 for i in range(__snake_case						):
  _lowercase							      =res_atoa(aatype[i]						)
  for atom_name, pos, mask, b_factor in zip(__snake_case     ,						atom_positions[i]     ,						atom_mask[i]     ,						b_factors[i]						):
   if mask < 0.5:
    continue

   _lowercase							      ='''ATOM'''
   _lowercase							      =atom_name if len(__snake_case						) == 4 else F" {atom_name}"
   _lowercase							      =''''''
   _lowercase							      =''''''
   _lowercase							      =1.00
   _lowercase							      =atom_name[0]  # Protein supports only C, N, O, S, this works.
   _lowercase							      =''''''

   _lowercase							      ='''A'''
   if chain_index is not None:
    _lowercase							      =chain_tags[chain_index[i]]

   # PDB is a columnar format, every space matters here!
   _lowercase							      =(
       F"{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}"
       F"{res_name_a:>3} {chain_tag:>1}"
       F"{residue_index[i]:>4}{insertion_code:>1}   "
       F"{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}"
       F"{occupancy:>6.2f}{b_factor:>6.2f}          "
       F"{element:>2}{charge:>2}"
   )
   pdb_lines.append(__snake_case						)
   atom_index += 1

  _lowercase							      =i == n - 1
  if chain_index is not None:
   if i != n - 1 and chain_index[i + 1] != prev_chain_index:
    _lowercase							      =True
    _lowercase							      =chain_index[i + 1]

  if should_terminate:
   # Close the chain.
   _lowercase							      ='''TER'''
   _lowercase							      =(
       F"{chain_end:<6}{atom_index:>5}      {res_atoa(aatype[i]						):>3} {chain_tag:>1}{residue_index[i]:>4}"
   )
   pdb_lines.append(__snake_case						)
   atom_index += 1

   if i != n - 1:
    # "prev" is a misnomer here. This happens at the beginning of
    # each new chain.
    pdb_lines.extend(get_pdb_headers(__snake_case     ,						__snake_case						)						)

 pdb_lines.append('''END'''						)
 pdb_lines.append(''''''						)
 return "\n".join(__snake_case						)
def 				UpperCAmelCase_					(							__snake_case						)     ->			np.ndarray:







 """simple docstring"""


 return residue_constants.STANDARD_ATOM_MASK[prot.aatype]



def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case     ,						__snake_case = None     ,						__snake_case = None     ,						__snake_case = None     ,						__snake_case = None     ,						__snake_case = None     ,						)     ->			Protein:







 """simple docstring"""


 return Protein(
     aatype=features['''aatype''']     ,						atom_positions=result['''final_atom_positions''']     ,						atom_mask=result['''final_atom_mask''']     ,						residue_index=features['''residue_index'''] + 1     ,						b_factors=b_factors if b_factors is not None else np.zeros_like(result['''final_atom_mask''']						)     ,						chain_index=__snake_case     ,						remark=__snake_case     ,						parents=__snake_case     ,						parents_chain_index=__snake_case     ,						)





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__   	=							logging.get_logger(__name__)

UpperCAmelCase__   	=							{
    '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''',
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}







class       lowerCamelCase__   (       lowerCAmelCase):
     SCREAMING_SNAKE_CASE__      			=			'''wavlm'''





     def __init__(self		,     UpperCAmelCase=3_2		,     UpperCAmelCase=7_6_8		,     UpperCAmelCase=1_2		,     UpperCAmelCase=1_2		,     UpperCAmelCase=3_0_7_2		,     UpperCAmelCase="gelu"		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.0		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.02		,     UpperCAmelCase=1e-5		,     UpperCAmelCase="group"		,     UpperCAmelCase="gelu"		,     UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)		,     UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2)		,     UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2)		,     UpperCAmelCase=False		,     UpperCAmelCase=1_2_8		,     UpperCAmelCase=1_6		,     UpperCAmelCase=3_2_0		,     UpperCAmelCase=8_0_0		,     UpperCAmelCase=False		,     UpperCAmelCase=True		,     UpperCAmelCase=0.05		,     UpperCAmelCase=1_0		,     UpperCAmelCase=2		,     UpperCAmelCase=0.0		,     UpperCAmelCase=1_0		,     UpperCAmelCase=3_2_0		,     UpperCAmelCase=2		,     UpperCAmelCase=0.1		,     UpperCAmelCase=1_0_0		,     UpperCAmelCase=2_5_6		,     UpperCAmelCase=2_5_6		,     UpperCAmelCase=0.1		,     UpperCAmelCase="mean"		,     UpperCAmelCase=False		,     UpperCAmelCase=False		,     UpperCAmelCase=2_5_6		,     UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)		,     UpperCAmelCase=(5, 3, 3, 1, 1)		,     UpperCAmelCase=(1, 2, 3, 1, 1)		,     UpperCAmelCase=5_1_2		,     UpperCAmelCase=8_0		,     UpperCAmelCase=0		,     UpperCAmelCase=1		,     UpperCAmelCase=2		,     UpperCAmelCase=False		,     UpperCAmelCase=3		,     UpperCAmelCase=2		,     UpperCAmelCase=3		,     UpperCAmelCase=None		,     **UpperCAmelCase		,     )     ->   Optional[Any]:
      super().__init__(**UpperCAmelCase		,     pad_token_id=UpperCAmelCase		,     bos_token_id=UpperCAmelCase		,     eos_token_id=UpperCAmelCase       )
      _lowercase							      =hidden_size
      _lowercase							      =feat_extract_norm
      _lowercase							      =feat_extract_activation
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =conv_bias
      _lowercase							      =num_buckets
      _lowercase							      =max_bucket_distance
      _lowercase							      =num_conv_pos_embeddings
      _lowercase							      =num_conv_pos_embedding_groups
      _lowercase							      =len(self.conv_dim       )
      _lowercase							      =num_hidden_layers
      _lowercase							      =intermediate_size
      _lowercase							      =hidden_act
      _lowercase							      =num_attention_heads
      _lowercase							      =hidden_dropout
      _lowercase							      =attention_dropout
      _lowercase							      =activation_dropout
      _lowercase							      =feat_proj_dropout
      _lowercase							      =final_dropout
      _lowercase							      =layerdrop
      _lowercase							      =layer_norm_eps
      _lowercase							      =initializer_range
      _lowercase							      =num_ctc_classes
      _lowercase							      =vocab_size
      _lowercase							      =do_stable_layer_norm
      _lowercase							      =use_weighted_layer_sum
      _lowercase							      =classifier_proj_size

      if (
          (len(self.conv_stride       ) != self.num_feat_extract_layers)
          or (len(self.conv_kernel       ) != self.num_feat_extract_layers)
          or (len(self.conv_dim       ) != self.num_feat_extract_layers)
      ):
       raise ValueError(
           '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
           ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
           f" {len(self.conv_dim       )}`, `len(config.conv_stride) = {len(self.conv_stride       )}`,"
           f" `len(config.conv_kernel) = {len(self.conv_kernel       )}`."       )

      # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
      _lowercase							      =apply_spec_augment
      _lowercase							      =mask_time_prob
      _lowercase							      =mask_time_length
      _lowercase							      =mask_time_min_masks
      _lowercase							      =mask_feature_prob
      _lowercase							      =mask_feature_length

      # parameters for pretraining with codevector quantized representations
      _lowercase							      =num_codevectors_per_group
      _lowercase							      =num_codevector_groups
      _lowercase							      =contrastive_logits_temperature
      _lowercase							      =num_negatives
      _lowercase							      =codevector_dim
      _lowercase							      =proj_codevector_dim
      _lowercase							      =diversity_loss_weight

      # ctc loss
      _lowercase							      =ctc_loss_reduction
      _lowercase							      =ctc_zero_infinity

      # adapter
      _lowercase							      =add_adapter
      _lowercase							      =adapter_kernel_size
      _lowercase							      =adapter_stride
      _lowercase							      =num_adapter_layers
      _lowercase							      =output_hidden_size or hidden_size

      # SequenceClassification-specific parameter. Feel free to ignore for other classes.
      _lowercase							      =classifier_proj_size

      # XVector-specific parameters. Feel free to ignore for other classes.
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =xvector_output_dim





     @property
     def      __A					(self       )     ->   int:
      return functools.reduce(operator.mul		,     self.conv_stride		,     1       )

