
    9i                        S SK JrJrJr  S SKrS\R
                  S\R
                  4S jrS\R
                  4S jrS\R
                  S	\R
                  S\\R
                  \R
                  4   4S
 jr  SS\R
                  S\	S\	S\\
\R
                  4   4S jjr      SS\R
                  S\\R
                     S\	S\	S\S\\R
                     S\S\R
                  4S jjrg)    )DictOptionalTupleNplddt_logitsreturnc                 |   U R                   S   n[        R                  R                  R	                  U R                  5       SS9nSU-  n[        R                  " SU-  SUU R                  S9n[        R                  " UUR                  " / S[        UR                   SS 5      -  QUR                   Q76 -  SS9nU$ )zComputes per-residue pLDDT from logits.
Args:
    logits: [num_res, num_bins] output from the PredictedLDDTHead.
Returns:
    plddt: [num_res] per-residue pLDDT.
dim      ?g      ?)startendstepdevice)   N)shapetorchnn
functionalsoftmaxfloataranger   sumviewlen)r   num_bins	bin_probs	bin_widthboundsplddts         t/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/science/unifold/modules/confidence.pypredicted_lddtr"   	   s     !!"%H##++L,>,>,@b+IIhI\\Io""	$F
 II
++
JIOOCR$8 99
JV\\
J	KE
 L    breaksc                     U S   U S   -
  nXS-  -   n[         R                  " X"S   U-   R                  S5      /SS9nU$ )zGets the bin centers from the bin edges.
Args:
    breaks: [num_bins - 1] the error bin edges.
Returns:
    bin_centers: [num_bins] the error bin centers.
r   r      r	   r
   )r   cat	unsqueeze)r$   r   
bin_valuess      r!   compute_bin_valuesr*       sR     !9vay D("JJB$)>(I(I"(MN "Jr#   	bin_edgesr   c                 F    [        U 5      n[        R                  " X-  SS9$ )a  Calculates expected aligned distance errors for every pair of residues.
Args:
    alignment_confidence_breaks: [num_bins - 1] the error bin edges.
    aligned_distance_error_probs: [num_res, num_res, num_bins] the predicted
    probs for each error bin, for each pair of residues.
Returns:
    predicted_aligned_error: [num_res, num_res] the expected aligned distance
    error for each pair of residues.
    max_predicted_aligned_error: The maximum predicted error possible.
r	   r
   )r*   r   r   )r+   r   r)   s      r!   compute_predicted_aligned_errorr-   .   s"     $I.J99Y+44r#   
pae_logitsmax_binr   c                     [         R                  R                  R                  U R	                  5       SS9n[         R
                  " SXS-
  U R                  S9n[        UUS9nUUS.$ )a  Computes aligned confidence metrics from logits.
Args:
    logits: [num_res, num_res, num_bins] the logits output from
    PredictedAlignedErrorHead.
    breaks: [num_bins - 1] the error bin edges.
Returns:
    aligned_confidence_probs: [num_res, num_res, num_bins] the predicted
    aligned error probabilities over bins for each residue pair.
    predicted_aligned_error: [num_res, num_res] the expected aligned distance
    error for each pair of residues.
    max_predicted_aligned_error: The maximum predicted error possible.
r	   r
   r   r   stepsr   )r+   r   )aligned_error_probs_per_binpredicted_aligned_error)r   r   r   r   r   linspacer   r-   )r.   r/   r   kwargsr   r+   r4   s          r!   r4   r4   @   sq    $ ##++J,<,<,>B+GI	7a<1B1BDI > (1#: r#   residue_weightsepsasym_id	interfacec                 
   U R                  5       n Uc  U R                  U R                  SS 5      n[        R                  " SX#S-
  U R
                  S9nS n	S n
[        U5      n[        R                  R                  R                  U SS	9nU	" U R                  S   S
9" U5      n[        R                  " X-  SS	9nUR                  UR                  5      nU(       a!  Uc   S5       eXSSS2S4   USSSS24   :g  -  nX-  nUUSSS24   USS2S4   -  -  nUUUR                  SSS9-   -  n[        R                  " UU-  SS	9nUU-  nUR                  SUR                  SSS9R                  S9R                  SS	9nU$ )a=  Computes predicted TM alignment or predicted interface TM alignment score.
Args:
    logits: [num_res, num_res, num_bins] the logits output from
    PredictedAlignedErrorHead.
    breaks: [num_bins] the error bins.
    residue_weights: [num_res] the per residue weights to use for the
    expectation.
    asym_id: [num_res] the asymmetric unit ID - the chain ID. Only needed for
    ipTM calculation, i.e. when interface=True.
    interface: If True, interface predicted TM score is computed.
Returns:
    ptm_score: The predicted TM alignment or the predicted iTM score.
Nr   r   r1   c                 D   ^ [        U S5      nSUS-
  S-  -  S-
  mU4S j$ )N   gףp=
?   gUUUUUU?g?c                     > SSU T-  S-  -   -  $ )Nr   r&    )xd0s    r!   <lambda>7predicted_tm_score.<locals>.tm_kernal.<locals>.<lambda>   s    q2vk 12r#   )max)nres	clipped_nrC   s     @r!   	tm_kernal%predicted_tm_score.<locals>.tm_kernal   s,    bM	Y^y11C722r#   c                    ^  U 4S j$ )Nc                    > SU T-   -  $ )Nr   rA   )rB   r8   s    r!   rD   9predicted_tm_score.<locals>.rmsd_kernal.<locals>.<lambda>   s    q3wr#   rA   )r8   s   `r!   rmsd_kernal'predicted_tm_score.<locals>.rmsd_kernal   s	    ''r#   r	   r
   )rG   z*must provide asym_id for iptm calculation..T)r   keepdim)r   index)r   new_onesr   r   r5   r   r*   r   r   r   r   gatherrF   indicessqueeze)r.   r7   r/   r   r8   r9   r:   r6   r$   rI   rN   bin_centersprobs
tm_per_binpredicted_tm_term	pair_maskpair_residue_weightsnormed_residue_maskper_alignmentweightedrets                        r!   predicted_tm_scorer`   a   s   . !!#J$--j.>.>s.CD^^	7a<1B1BDF3
( %V,KHH''
';E
 0 0 45kBJ 		%"4"=!**+<+B+BCI"P$PP"S!T\*gc4l.CCC	"$a ?1d7#;;=."&&2t&<<> II/2EE2NM.H


hllr+/ # 118  ::A'b'/  Jr#   )   @   )Nra   rb   g:0yE>NF)typingr   r   r   r   Tensorr"   r*   r-   intstrr4   r   boolr`   rA   r#   r!   <module>rh      s7   ) (  %,, .u|| 5||5||5 5<<%&5(  
 
#u||
F /3&*>>ell+> > 	>
 
> ell#> > \\>r#   