ó
 ¤Ic           @   s´  d  d l  m Z d  d l Z d  d l Td  d l m Z d Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ	 e
 d k r°d  d l Z e ƒ  Z e j d e j d ƒ Z e d Z d Z d Z e e d e d e ƒZ x e D] Z e GHqWHe e d e d e ƒZ x e D] Z e GHq2WHe	 e d e d e ƒZ x e D] Z e GHqaWHx< e D]1 Z x( e D]  Z y e j d GHWq…q…Xq…WqxWn  d S(   iÿÿÿÿ(   t   piN(   t   *(   t   AbstractPropertyMaps9   Half sphere exposure and coordination number calculation.t   _AbstractHSExposurec           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sî   
    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. 
    c         C   s´  | d k s t  ‚ g  |  _ t ƒ  } | j | ƒ } i  }	 g  }
 g  } xR| D]J} xAt d t | ƒ ƒ D]*} | d k rƒ d } n | | d } | | } | t | ƒ d k rº d } n | | d } |  j | | | ƒ } | d k rï qh n  | \ } } d } d } | d j ƒ  } xâ | D]Ú } xÑ t d t | ƒ ƒ D]º } | | k rht	 | | ƒ | k rhq:n  | | } t
 | ƒ s:| j d ƒ r•q:n  | d j ƒ  } | | } | j ƒ  | k  r:| j | ƒ t d k  rç| d 7} qô| d 7} q:q:WqW| j ƒ  } | j ƒ  j ƒ  } | | | f |	 | | f <|
 j | | | | f f ƒ | j | | f ƒ | | j | <| | j | <| rh | | j | <qh qh WqL Wt j |  |	 | |
 ƒ d S(   s›  
        @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
        i    i   t   CAi   N(   t   AssertionErrort
   ca_cb_listt   CaPPBuildert   build_peptidest   ranget   lent   Nonet   _get_cbt
   get_vectort   abst   is_aat   has_idt   normt   angleR    t   get_idt
   get_parentt   appendt   xtraR   t   __init__(   t   selft   modelt   radiust   offsett
   hse_up_keyt   hse_down_keyt	   angle_keyt   ppbt   pplt   hse_mapt   hse_listt   hse_keyst   pp1t   it   r1t   r2t   r3t   resultt   pcbR   t   hse_ut   hse_dt   ca2t   pp2t   jt   rot   caot   dt   res_idt   chain_id(    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR      s\    			
	"

c         C   sž   y4 | d j  ƒ  } | d j  ƒ  } | d j  ƒ  } Wn d SX| | } | | } t t d d | ƒ } | j | ƒ } | | } |  j j | | f ƒ | S(   sÇ   
        Return a pseudo CB vector for a Gly residue.
        The pseudoCB vector is centered at the origin.

        CB coord=N coord rotated over -120 degrees 
        along the CA-C axis.
        t   Nt   CR   g      ^@g     €f@N(   R   R   t   rotaxisR    t   left_multiplyR   R   (   R   t   residuet   n_vt   c_vt   ca_vt   rott   cb_at_origin_vt   cb_v(    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyt   _get_gly_cb_vectorg   s    


N(   t   __name__t
   __module__t   __doc__R   R   R@   (    (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR      s   Nt   HSExposureCAc           B   s2   e  Z d  Z d d d „ Z d „  Z d d „ Z RS(   sr   
    Class to calculate HSE based on the approximate CA-CB vectors,
    using three consecutive CA positions.
    i   i    c         C   s#   t  j |  | | | d d d ƒ d S(   s\  
        @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
        t   EXP_HSE_A_Ut   EXP_HSE_A_Dt   EXP_CB_PCB_ANGLEN(   R   R   (   R   R   R   R   (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR   ˆ   s    c         C   sN  | d k s | d k r d Sy4 | d j ƒ  } | d j ƒ  } | d j ƒ  } Wn d SX| | } | | } | j ƒ  | j ƒ  | | }	 |	 j ƒ  |  j j | |	 | f ƒ | j d ƒ rö | d j ƒ  }
 |
 | } | j ƒ  | j |	 ƒ } nN | j ƒ  d k r>|  j | ƒ } | d k r,d } qD| j |	 ƒ } n d } |	 | f S(   sM  
        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}
        R   t   CBt   GLYN(	   R   R   t	   normalizeR   R   R   R   t   get_resnameR@   (   R   R&   R'   R(   t   ca1R-   t   ca3t   d1t   d3t   bt   cbt   cb_caR   (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR   –   s6    







	s	   hs_exp.pyc         C   s  t  |  j ƒ d k r) t j j d ƒ d St | d ƒ } | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d	 d ƒ xu |  j D]j \ } } | j ƒ  \ } } } | j d | | | f ƒ | j ƒ  \ } } } | j d | | | f ƒ q‡ W| j d ƒ | j d ƒ | j ƒ  d S(   sÍ   
        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
        i    s   Nothing to draw.
Nt   ws   from pymol.cgo import *
s   from pymol import cmd
s   obj=[
s   BEGIN, LINES,
s   COLOR, %.2f, %.2f, %.2f,
g      ð?s   VERTEX, %.2f, %.2f, %.2f,
s   END]
s   cmd.load_cgo(obj, 'HS')
(   g      ð?g      ð?g      ð?(   R
   R   t   syst   stderrt   writet   opent	   get_arrayt   close(   R   t   filenamet   fpt   caRQ   t   xt   yt   z(    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyt   pcb_vectors_pymolÃ   s"    (   RA   RB   RC   R   R   R`   (    (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyRD   ƒ   s   	-t   HSExposureCBc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   sA   
    Class to calculate HSE based on the real CA-CB vectors.
    i   i    c         C   s    t  j |  | | | d d ƒ d S(   s\  
        @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
        t   EXP_HSE_B_Ut   EXP_HSE_B_DN(   R   R   (   R   R   R   R   (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR   â   s    c         C   su   | j  ƒ  d k r% |  j | ƒ d f S| j d ƒ rq | j d ƒ rq | d j ƒ  } | d j ƒ  } | | d f Sd S(   s¡   
        Method to calculate CB-CA vector.

        @param r1, r2, r3: three consecutive residues (only r2 is used)
        @type r1, r2, r3: L{Residue}
        RI   g        RH   R   N(   RK   R@   R   R   R   (   R   R&   R'   R(   t   vcbt   vca(    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR   ð   s    (   RA   RB   RC   R   R   (    (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyRa   Þ   s   t
   ExposureCNc           B   s   e  Z d  d d „ Z RS(   g      (@i    c         C   sØ  | d k s t  ‚ t ƒ  } | j | ƒ } i  } g  } g  } x| D]w}	 xnt d t |	 ƒ ƒ D]W}
 d } |	 |
 } t | ƒ s_ | j d ƒ r˜ q_ n  | d } x° | D]¨ } xŸ t d t | ƒ ƒ D]ˆ } |	 | k ró t |
 | ƒ | k ró qÅ n  | | } t | ƒ sÅ | j d ƒ r qÅ n  | d } | | } | | k  rÅ | d 7} qÅ qÅ Wq© W| j ƒ  } | j	 ƒ  j ƒ  } | | | | f <| j
 | | f ƒ | j
 | | f ƒ | | j d <q_ WqC Wt j |  | | | ƒ d S(   s?  
        A residue's exposure is defined as the number of CA atoms around 
        that residues 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

        i    R   i   t   EXP_CNN(   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R    t   fs_mapt   fs_listt   fs_keysR$   R%   t   fsR&   RL   R.   R/   R'   R-   R2   R3   R4   (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyR     s>    	

"


(   RA   RB   R   (    (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyRf     s   t   __main__t   Xi   i    g      *@R   R   t   PCB_CB_ANGLE(   t   mathR    RT   t   Bio.PDBR   RC   R   RD   Ra   Rf   RA   t	   PDBParsert   pt   get_structuret   argvt   sR   t   RADIUSt   OFFSETt   hset   lt   ct   rR   (    (    (    s…   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/HSExposure.pyt   <module>   sB   
s[#4	
			