o
    *j                     @   s   d dl mZmZmZ d dlZdejdejfddZdejfdd	Zd
ejdejdeejejf fddZ		ddejde	de	dee
ejf fddZ						ddejdeej de	de	dedeej dedejfddZdS )    )DictOptionalTupleNplddt_logitsreturnc              	   C   sz   | j d }tjjj|  dd}d| }tjd| d|| jd}tj||j	g dt
|j dd  |j R   dd}|S )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floatZaranger   sumviewlen)r   num_bins	bin_probsZ	bin_widthZboundsZplddt r   u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/science/unifold/modules/confidence.pypredicted_lddt	   s    
&r   breaksc                 C   s@   | d | d  }| |d  }t j||d | dgdd}|S )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   catZ	unsqueeze)r   r   
bin_valuesr   r   r   compute_bin_values    s   r"   	bin_edgesr   c                 C   s   t | }tj|| ddS )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!   r   r   r   compute_predicted_aligned_error.   s   r$      @   
pae_logitsmax_binr   c                 K   sD   t jjj|  dd}t jd||d | jd}t||d}||d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   Zstepsr   )r#   r   )Zaligned_error_probs_per_binpredicted_aligned_error)r   r   r   r   r   linspacer   r$   )r'   r(   r   kwargsr   r#   r*   r   r   r   r*   @   s   r*   :0yE>Fresidue_weightsepsasym_id	interfacec                 K   sV  |   } |du r| | jdd }tjd||d | jd}dd }	dd	 }
t|}tjjj	| d
d}|	| jd d|}tj
|| d
d}||j}|rj|dusVJ d||ddddf |ddddf k9 }||9 }||dddf |dddf   }|||j
d
dd  }tj
|| d
d}|| }|jd
|jd
ddjdjd
d}|S )am  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   r)   c                    s*   t | d}d|d d  d   fddS )N   gףp=
?   gUUUUUU?g?c                    s   dd|   d   S )Nr
   r   r   xZd0r   r   <lambda>   s    z7predicted_tm_score.<locals>.tm_kernal.<locals>.<lambda>)max)nresZ	clipped_nr   r7   r   	tm_kernal   s   
z%predicted_tm_score.<locals>.tm_kernalc                    s    fddS )Nc                    s   d|    S )Nr
   r   r5   r/   r   r   r8      s    z9predicted_tm_score.<locals>.rmsd_kernal.<locals>.<lambda>r   r<   r   r<   r   rmsd_kernal   s   z'predicted_tm_score.<locals>.rmsd_kernalr   r   )r:   z*must provide asym_id for iptm calculation..T)r	   Zkeepdim)r	   index)r   Znew_onesr   r   r+   r   r"   r   r   r   r   Zgatherr9   indicesZsqueeze)r'   r.   r(   r   r/   r0   r1   r,   r   r;   r=   Zbin_centersZprobsZ
tm_per_binZpredicted_tm_termZ	pair_maskZpair_residue_weightsZnormed_residue_maskZper_alignmentZweightedretr   r   r   predicted_tm_scorea   sD   (rA   )r%   r&   )Nr%   r&   r-   NF)typingr   r   r   r   ZTensorr   r"   r$   intstrr*   r   boolrA   r   r   r   r   <module>   sZ   

#	