B
    ‰°b?,  ã               @   s„   d Z ddlZddlmZ ddlmZ ddlmZ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 )
Ú_AbstractHSExposurezçAbstract 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t ‚g | _tƒ }| |¡}i }	g }
g }x´|D ]ª}x¢tdt|ƒƒD ]Ž}|dkrbd}n||d  }|| }|t|ƒd krŒd}n||d  }|  |||¡}|dkr°qN|\}}d}d}|d  ¡ }x¦|D ]ž}x˜tdt|ƒƒD ]†}||krt|| ƒ|krqæ|| }t	|ƒræ| 
d¡s&qæ|d  ¡ }|| }| ¡ |k ræ| |¡td k rd|d7 }qæ|d7 }qæW qÒW | ¡ }| ¡  ¡ }|||f|	||f< |
 ||||ff¡ | ||f¡ ||j|< ||j|< |rN||j|< qNW q6W 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é   )ÚAssertionErrorÚ
ca_cb_listr   Úbuild_peptidesÚrangeÚlenÚ_get_cbÚ
get_vectorÚabsr   Úhas_idZnormÚangler   Úget_idÚ
get_parentÚappendÚxtrar   Ú__init__)ÚselfÚmodelÚradiusÚoffsetZ
hse_up_keyZhse_down_keyZ	angle_keyÚppbÚpplZhse_mapZhse_listZhse_keysÚpp1ÚiÚr1Úr2Úr3ÚresultZpcbr   Zhse_uZhse_dÚca2Úpp2ÚjZroZcaoÚdÚres_idÚchain_id© r,   ú1lib/python3.7/site-packages/Bio/PDB/HSExposure.pyr      s\    




z_AbstractHSExposure.__init__c             C   s   t S )N)ÚNotImplemented)r   r"   r#   r$   r,   r,   r-   r   j   s    z_AbstractHSExposure._get_cbc             C   sˆ   y(|d   ¡ }|d   ¡ }|d   ¡ }W n tk
r<   dS X || }|| }tt d d |ƒ}| |¡}|| }| j ||f¡ |S )zÈReturn 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   Zleft_multiplyr   r   )r   ZresidueZn_vZc_vZca_vZrotZcb_at_origin_vZcb_vr,   r,   r-   Ú_get_gly_cb_vectorm   s    
z&_AbstractHSExposure._get_gly_cb_vector)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r2   r,   r,   r,   r-   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 )az  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
        ZEXP_HSE_A_UZEXP_HSE_A_DZEXP_CB_PCB_ANGLEN)r   r   )r   r   r   r   r,   r,   r-   r   Ž   s    zHSExposureCA.__init__c             C   s  |dks|dkrdS y(|d   ¡ }|d   ¡ }|d   ¡ }W n tk
rP   dS X || }|| }| ¡  | ¡  || }	|	 ¡  | j ||	| f¡ | d¡rÈ|d   ¡ }
|
| }| ¡  | |	¡}n4| ¡ dkrø|  |¡}|dkrìd}n
| |	¡}n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   r1   Z	normalizer   r   r   r   Úget_resnamer2   )r   r"   r#   r$   Úca1r&   Zca3Zd1Zd3ÚbÚcbZcb_car   r,   r,   r-   r   ¥   s6    

zHSExposureCA._get_cbú	hs_exp.pyc          	   C   sÒ   | j st dt¡ dS t|dƒ¨}| d¡ | d¡ | d¡ | d¡ | dd	 ¡ xV| j D ]L\}}| ¡ \}}}| d
|||f ¡ | ¡ \}}}| d
|||f ¡ q`W | d¡ | d¡ W dQ R X dS )zòWrite 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, %.2f, %.2f, %.2f,
)g      ð?g      ð?g      ð?zVERTEX, %.2f, %.2f, %.2f,
zEND]
zcmd.load_cgo(obj, 'HS')
)r   ÚwarningsÚwarnÚRuntimeWarningÚopenÚwriteZ	get_array)r   ÚfilenameÚfpZcar>   ÚxÚyÚzr,   r,   r-   Úpcb_vectors_pymolÓ   s     	




zHSExposureCA.pcb_vectors_pymolN)r8   r   )r?   )r3   r4   r5   r6   r   r   rK   r,   r,   r,   r-   r7   ˆ   s   
.r7   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.r8   r   c             C   s   t  | |||dd¡ dS )az  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
        ZEXP_HSE_B_UZEXP_HSE_B_DN)r   r   )r   r   r   r   r,   r,   r-   r   ñ   s    zHSExposureCB.__init__c             C   sV   |  ¡ dkr|  |¡dfS | d¡rR| d¡rR|d  ¡ }|d  ¡ }|| dfS dS )z˜Calculate CB-CA vector (PRIVATE).

        :param r1, r2, r3: three consecutive residues (only r2 is used)
        :type r1, r2, r3: L{Residue}
        r:   g        r9   r	   N)r;   r2   r   r   )r   r"   r#   r$   ZvcbZvcar,   r,   r-   r     s    zHSExposureCB._get_cbN)r8   r   )r3   r4   r5   r6   r   r   r,   r,   r,   r-   rL   î   s   
rL   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   sR  |dkst ‚tƒ }| |¡}i }g }g }x|D ]}	x tdt|	ƒƒD ]î}
d}|	|
 }t|ƒrH| d¡slqH|d }xz|D ]r}xltdt|ƒƒD ]Z}|	|kr¬t|
| ƒ|kr¬qŽ|| }t|ƒrŽ| d¡sÈqŽ|d }|| }||k rŽ|d7 }qŽW qzW | ¡ }| 	¡  ¡ }||||f< | 
||f¡ | 
||f¡ ||jd< qHW q0W 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   ZEXP_CNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   Zfs_mapZfs_listZfs_keysr    r!   Zfsr"   r<   r'   r(   r#   r&   r)   r*   r+   r,   r,   r-   r     s>    

zExposureCN.__init__N)rN   r   )r3   r4   r5   r6   r   r,   r,   r,   r-   rM     s   rM   )r6   rA   Zmathr   ZBio.PDB.AbstractPropertyMapr   ZBio.PDB.Polypeptider   r   ZBio.PDB.vectorsr   r   r7   rL   rM   r,   r,   r,   r-   Ú<module>   s   uf$