
    RiI,                         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	e      Z G d
 de      Z G d de      Z G d de      Zy)z9Half-sphere exposure and coordination number calculation.    N)pi)AbstractPropertyMap)CaPPBuilder)is_aa)rotaxisc                   $    e Zd ZdZddZd Zd Zy)_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                    |dk\  sJ g | _         t               }|j                  |      }i }	g }
g }|D ]  }t        t	        |            D ]  }|dk(  rd}n||dz
     }||   }|t	        |      dz
  k(  rd}n||dz      }| j                  |||      }|K|\  }}d}d}|d   j                         }|D ]  }t        t	        |            D ]  }||u rt        ||z
        |k  r||   }t        |      r|j                  d      s;|d   j                         }||z
  }|j                         |k  sg|j                  |      t        dz  k  r|dz  }|dz  }  |j                         }|j                         j                         }|||f|	||f<   |
j                  ||||ff       |j                  ||f       ||j                   |<   ||j                   |<   |s||j                   |<     t#        j$                  | |	||
       y)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_ids                                 J/home/agent/.friday_env/lib/python3.12/site-packages/Bio/PDB/HSExposure.pyr   z_AbstractHSExposure.__init__   s6   . {{m  ' -	/C3s8_ ,/6BQUBVC1$BQUBb"b1>#
Uh))+ +C"3s8_ +#:#a!e**>$ V$Ry		$$ h113#I668f, wws|rAv6 %
 %
++ ==?113/4eU.C6*+eUE%: ;<6 23&+
#(-%).BGGI&Y,/-	/\ 	$$T7HhG    c                     t         S N)NotImplemented)r   r+   r,   r-   s       r:   r   z_AbstractHSExposure._get_cbj   s    r;   c                 @   	 |d   j                         }|d   j                         }|d   j                         }||z
  }||z
  }t        t         dz  dz  |      }|j	                  |      }||z   }| j
                  j                  ||f       |S # t        $ r Y yw xY w)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_vs           r:   _get_gly_cb_vectorz&_AbstractHSExposure._get_gly_cb_vectorm   s    	#,))+C#,))+C4=++-D DjDjrcEkE)3/**3/$d|,  		s   9B 	BBr=   )__name__
__module____qualname____doc__r   r   rK    r;   r:   r	   r	      s    MH^r;   r	   c                   &    e Zd ZdZddZd ZddZy)HSExposureCAzmClass to calculate HSE based on the approximate CA-CB vectors.

    Uses three consecutive CA positions.
    c           	      :    t         j                  | |||ddd       y)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    s       r:   r   zHSExposureCA.__init__   s'     	$$	
r;   c                    ||y	 |d   j                         }|d   j                         }|d   j                         }||z
  }||z
  }|j                          |j                          ||z   }	|	j                          | j                  j	                  ||	|z   f       |j                  d      r=|d   j                         }
|
|z
  }|j                          |j                  |	      }|	|fS |j                         dk(  r.| j                  |      }|d}|	|fS |j                  |	      }|	|fS d}|	|fS # t        $ r Y yw xY w)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   rB   	normalizer   r   r   r   get_resnamerK   )r   r+   r,   r-   ca1r2   ca3d1d3bcbcb_car   s                r:   r   zHSExposureCA._get_cb   sY    :	T(%%'CT(%%'CT(%%'C 3Y3Y

G	QW~.99T?D$$&BHEOOKKNE %x ^^&++B/E} %x	 A %x E%x5  		s   9D4 4	E ?E c                    | j                   st        j                  dt               yt	        |d      5 }|j                  d       |j                  d       |j                  d       |j                  d       |j                  dd	d
dd	d
dd	d
d       | j                   D ]i  \  }}|j                         \  }}}|j                  d|d
d|d
d|d
d       |j                         \  }}}|j                  d|d
d|d
d|d
d       k |j                  d       |j                  d       ddd       y# 1 sw Y   yxY w)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card   xyzs           r:   pcb_vectors_pymolzHSExposureCA.pcb_vectors_pymol   s<    MM,n=(C  	2BHH01HH./HHYHH&'HHws3ir#c"SISAB// BB,,.1a8Ac7"QsG2aWC@A,,.1a8Ac7"QsG2aWC@A	B
 HHXHH01	2 	2 	2s   C=D::EN   r   )z	hs_exp.py)rL   rM   rN   rO   r   r   rt   rP   r;   r:   rR   rR      s    

.,\2r;   rR   c                       e Zd ZdZddZd Zy)HSExposureCBz7Class to calculate HSE based on the real CA-CB vectors.c                 8    t         j                  | |||dd       y)rT   EXP_HSE_B_UEXP_HSE_B_DNrX   rY   s       r:   r   zHSExposureCB.__init__   s     	$$%	
r;   c                     |j                         dk(  r| j                  |      dfS |j                  d      r>|j                  d      r-|d   j                         }|d   j                         }||z
  dfS y)zCalculate CB-CA vector (PRIVATE).

        :param r1, r2, r3: three consecutive residues (only r2 is used)
        :type r1, r2, r3: L{Residue}
        r\   g        r[   r   N)r^   rK   r   r   )r   r+   r,   r-   vcbvcas         r:   r   zHSExposureCB._get_cb  st     >>u$**2.33yy299T?h))+h))+c	C''r;   Nru   )rL   rM   rN   rO   r   r   rP   r;   r:   rx   rx      s    A
"r;   rx   c                       e Zd ZdZddZy)
ExposureCNz:Residue exposure as number of CA atoms around its CA atom.c                    |dk\  sJ t               }|j                  |      }i }g }g }|D ]  }	t        t        |	            D ]  }
d}|	|
   }t	        |      r|j                  d      s(|d   }|D ]h  }t        t        |            D ]O  }|	|u rt        |
|z
        |k  r||   }t	        |      r|j                  d      s;|d   }||z
  }||k  sK|dz  }Q j |j                         }|j                         j                         }||||f<   |j                  ||f       |j                  ||f       ||j                  d<    ! t        j                  | |||       y)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+   r_   r3   r4   r,   r2   r7   r8   r9   s                        r:   r   zExposureCN.__init__  sz   $ {{m  ' 	'C3s8_ 'VRy		$h 
$C"3s8_ 	$#:#a!e**>$ V$Ry		$$ h#Iv:!GB	$
$ ==?113-/&)*Bx(&12$&!1'	'4 	$$T67GDr;   N)g      (@r   )rL   rM   rN   rO   r   rP   r;   r:   r   r     s    D2Er;   r   )rO   rh   mathr   Bio.PDB.AbstractPropertyMapr   Bio.PDB.Polypeptider   r   Bio.PDB.vectorsr   r	   rR   rx   r   rP   r;   r:   <module>r      sZ    @   ; + % #r- rjc2& c2L!& !H5E$ 5Er;   