o
    ߥi                     @   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floataranger   sumviewlen)r   num_bins	bin_probs	bin_widthboundsplddt r   h/home/ubuntu/.local/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   cat	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   stepsr   )r(   r   )aligned_error_probs_per_binpredicted_aligned_error)r   r   r   r   r   linspacer   r)   )r,   r-   r   kwargsr   r(   r1   r   r   r    r1   @   s   r1   :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d0r   r    <lambda>   s    z7predicted_tm_score.<locals>.tm_kernal.<locals>.<lambda>)max)nres	clipped_nr   r>   r    	tm_kernal   s   
z%predicted_tm_score.<locals>.tm_kernalc                    s    fddS )Nc                    s   d|    S )Nr
   r   r<   r6   r   r    r@      s    z9predicted_tm_score.<locals>.rmsd_kernal.<locals>.<lambda>r   rE   r   rE   r    rmsd_kernal   s   z'predicted_tm_score.<locals>.rmsd_kernalr   r   )rB   z*must provide asym_id for iptm calculation..T)r	   keepdim)r	   index)r   new_onesr   r   r2   r   r'   r   r   r   r   gatherrA   indicessqueeze)r,   r5   r-   r   r6   r7   r8   r3   r"   rD   rF   bin_centersprobs
tm_per_binpredicted_tm_term	pair_maskpair_residue_weightsnormed_residue_maskper_alignmentweightedretr   r   r    predicted_tm_scorea   sD   (rW   )r*   r+   )Nr*   r+   r4   NF)typingr   r   r   r   Tensorr!   r'   r)   intstrr1   r   boolrW   r   r   r   r    <module>   sZ   

#	