o
    RŀgI,                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 G dd	 d	eZG d
d deZG dd deZG dd deZdS )z9Half-sphere exposure and coordination number calculation.    N)pi)AbstractPropertyMap)CaPPBuilder)is_aa)rotaxisc                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
_AbstractHSExposurezAbstract class to calculate Half-Sphere Exposure (HSE).

    The HSE can be calculated based on the CA-CB vector, or the pseudo CB-CA
    vector based on three consecutive CA atoms. This is done by two separate
    subclasses.
    Nc                 C   s  |dksJ g | _ t }||}i }	g }
g }|D ]}tt|D ]}|dkr*d}n||d  }|| }|t|d kr?d}n||d  }| |||}|du rQq!|\}}d}d}|d  }|D ]H}tt|D ]?}||u rxt|| |krxqi|| }t|r|	dsqi|d  }|| }|
 |k r||td k r|d7 }qi|d7 }qiqa| }|  }|||f|	||f< |
||||ff |||f ||j|< ||j|< |r||j|< q!qt| |	||
 dS )a  Initialize class.

        :param model: model
        :type model: L{Model}

        :param radius: HSE radius
        :type radius: float

        :param offset: number of flanking residues that are ignored in the
                       calculation of the number of neighbors
        :type offset: int

        :param hse_up_key: key used to store HSEup in the entity.xtra attribute
        :type hse_up_key: string

        :param hse_down_key: key used to store HSEdown in the entity.xtra attribute
        :type hse_down_key: string

        :param angle_key: key used to store the angle between CA-CB and CA-pCB in
                          the entity.xtra attribute
        :type angle_key: string
        r   N   CA   )
ca_cb_listr   build_peptidesrangelen_get_cb
get_vectorabsr   has_idnormangler   get_id
get_parentappendxtrar   __init__)selfmodelradiusoffset
hse_up_keyhse_down_key	angle_keyppbpplhse_maphse_listhse_keyspp1ir1r2r3resultpcbr   hse_uhse_dca2pp2jrocaodres_idchain_id r7   F/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/HSExposure.pyr      sd   




-z_AbstractHSExposure.__init__c                 C   s   t S N)NotImplemented)r   r(   r)   r*   r7   r7   r8   r   j   s   z_AbstractHSExposure._get_cbc                 C   s   z|d   }|d   }|d   }W n
 ty   Y dS w || }|| }tt d d |}||}|| }| j||f |S )zReturn a pseudo CB vector for a Gly residue (PRIVATE).

        The pseudoCB vector is centered at the origin.

        CB coord=N coord rotated over -120 degrees
        along the CA-C axis.
        NCr	   Ng      ^@g     f@)r   	Exceptionr   r   left_multiplyr   r   )r   residuen_vc_vca_vrotcb_at_origin_vcb_vr7   r7   r8   _get_gly_cb_vectorm   s   
z&_AbstractHSExposure._get_gly_cb_vectorr9   )__name__
__module____qualname____doc__r   r   rF   r7   r7   r7   r8   r      s
    
Or   c                   @   s,   e Zd ZdZdddZdd Zdd	d
ZdS )HSExposureCAzmClass to calculate HSE based on the approximate CA-CB vectors.

    Uses three consecutive CA positions.
       r   c              	   C   s   t | |||ddd dS )z  Initialize class.

        :param model: the model that contains the residues
        :type model: L{Model}

        :param radius: radius of the sphere (centred at the CA atom)
        :type radius: float

        :param offset: number of flanking residues that are ignored
                       in the calculation of the number of neighbors
        :type offset: int
        EXP_HSE_A_UEXP_HSE_A_DEXP_CB_PCB_ANGLENr   r   r   r   r   r   r7   r7   r8   r      s   zHSExposureCA.__init__c                 C   s  |du s|du r
dS z|d   }|d   }|d   }W n
 ty(   Y dS w || }|| }|  |  || }	|	  | j||	| f |drg|d   }
|
| }|  ||	}|	|fS | dkr| |}|du r|d}|	|fS ||	}|	|fS d}|	|fS )ap  Calculate approx CA-CB direction (PRIVATE).

        Calculate the approximate CA-CB direction for a central
        CA atom based on the two flanking CA positions, and the angle
        with the real CA-CB vector.

        The CA-CB vector is centered at the origin.

        :param r1, r2, r3: three consecutive residues
        :type r1, r2, r3: L{Residue}
        Nr	   CBGLY)	r   r=   	normalizer   r   r   r   get_resnamerF   )r   r(   r)   r*   ca1r/   ca3d1d3bcbcb_car   r7   r7   r8   r      s>   




zHSExposureCA._get_cb	hs_exp.pyc                 C   s  | j stdt dS t|ds}|d |d |d |d |dd	d
dd	d
dd	d
d | j D ]6\}}| \}}}|d|d
d|d
d|d
d | \}}}|d|d
d|d
d|d
d q:|d |d W d   dS 1 sw   Y  dS )zWrite PyMol script for visualization.

        Write a PyMol script that visualizes the pseudo CB-CA directions
        at the CA coordinates.

        :param filename: the name of the pymol script file
        :type filename: string
        zNothing to draw.Nwzfrom pymol.cgo import *
zfrom pymol import cmd
zobj=[
zBEGIN, LINES,
zCOLOR, g      ?z.2fz, z,
zVERTEX, zEND]
zcmd.load_cgo(obj, 'HS')
)r   warningswarnRuntimeWarningopenwrite	get_array)r   filenamefpcar\   xyzr7   r7   r8   pcb_vectors_pymol   s"   	



$$&
"zHSExposureCA.pcb_vectors_pymolNrL   r   )r^   )rG   rH   rI   rJ   r   r   rl   r7   r7   r7   r8   rK      s
    
.rK   c                   @   s"   e Zd ZdZd	ddZdd ZdS )
HSExposureCBz7Class to calculate HSE based on the real CA-CB vectors.rL   r   c                 C   s   t | |||dd dS )rM   EXP_HSE_B_UEXP_HSE_B_DNrQ   rR   r7   r7   r8   r      s   zHSExposureCB.__init__c                 C   sV   |  dkr| |dfS |dr)|dr)|d  }|d  }|| dfS dS )zCalculate CB-CA vector (PRIVATE).

        :param r1, r2, r3: three consecutive residues (only r2 is used)
        :type r1, r2, r3: L{Residue}
        rT   g        rS   r	   N)rV   rF   r   r   )r   r(   r)   r*   vcbvcar7   r7   r8   r     s   zHSExposureCB._get_cbNrm   )rG   rH   rI   rJ   r   r   r7   r7   r7   r8   rn      s    
rn   c                   @   s   e Zd ZdZdddZdS )
ExposureCNz:Residue exposure as number of CA atoms around its CA atom.      (@r   c                 C   s8  |dksJ t  }||}i }g }g }|D ]{}	tt|	D ]r}
d}|	|
 }t|r/|ds0q|d }|D ]6}tt|D ]-}|	|u rMt|
| |krMq>|| }t|rZ|ds[q>|d }|| }||k rk|d7 }q>q6| }|  }||||f< |	||f |	||f ||j
d< qqt| ||| dS )a]  Initialize class.

        A residue's exposure is defined as the number of CA atoms around
        that residue's CA atom. A dictionary is returned that uses a L{Residue}
        object as key, and the residue exposure as corresponding value.

        :param model: the model that contains the residues
        :type model: L{Model}

        :param radius: radius of the sphere (centred at the CA atom)
        :type radius: float

        :param offset: number of flanking residues that are ignored in
                       the calculation of the number of neighbors
        :type offset: int

        r   r	   r   EXP_CNN)r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r!   r"   fs_mapfs_listfs_keysr&   r'   fsr(   rW   r0   r1   r)   r/   r4   r5   r6   r7   r7   r8   r     sD   

zExposureCN.__init__N)rt   r   )rG   rH   rI   rJ   r   r7   r7   r7   r8   rs     s    rs   )rJ   r`   mathr   Bio.PDB.AbstractPropertyMapr   Bio.PDB.Polypeptider   r   Bio.PDB.vectorsr   r   rK   rn   rs   r7   r7   r7   r8   <module>   s   uf$