ó
º™xTc           @   s¬   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 d  d l	 m
 Z
 d  d l m Z m Z d  d l m Z d  d	 l Z d
 e f d „  ƒ  YZ d	 S(   iÿÿÿÿ(   t   division(   t   eigensystem(   t   flex(   t   Sorry(   t   null_out(   t   matrix(   t   acost   pi(   t   pdbNt   fab_elbow_anglec           B   s_   e  Z d  d d d d „ Z d „  Z d „  Z e d „ Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 RS(   t   Lt   Hik   iq   c      	   C   sÃ  |  j  d | d | d | d | ƒ | |  _ |  j ƒ  |  j j ƒ  d j } |  j j ƒ  d j } |  j d | d | ƒ } |  j d |  j	 d	 |  j
 ƒ }	 |  j d |  j d	 |  j ƒ }
 t j |	 j j ƒ  ƒ } t j |
 j j ƒ  ƒ } |  j | ƒ } |  j | ƒ } | j | ƒ d
 k r"| } n  |  j d | d | ƒ } |  j | | ƒ } |  j | | ƒ } | j | ƒ d
 k r€d | } n  | |  _ | j | ƒ |  _ | j | ƒ |  _ | j | ƒ |  _ d S(   s3  
    Get elbow angle for Fragment antigen-binding (Fab)

    - Default heavy and light chains IDs are: H : heavy,  L : light
    - Default limit (cutoff) between variable and constant parts
      are residue number 107/113 for light/heavy chains
    - Variable domain is from residue 1 to limit.
      Constant domain form limit+1 to end.

    Reference:
    ----------
    Stanfield, et al., JMB 2006

    Usage example:
    --------------
    >>>fab = fab_elbow_angle(pdb_hierarchy=ph,limit_light=114,limit_heavy=118)
    >>>fab_angle = fab.fab_elbow_angle
    t
   chain_ID_Ht   limit_Ht
   chain_ID_Lt   limit_Liÿÿÿÿt   startt   endt   fixed_selectiont   moving_selectioni    t   vec1t   vec2ih  N(   t
   select_strt   pdb_hierarchyt   get_pdb_chainst	   pdb_var_Ht   atomst   xyzt	   pdb_var_Lt   norm_vect   get_transformationt   pdb_const_Ht   pdb_const_LR   t   real_symmetrict   rt   as_sym_mat3t   get_eigenvectort   dott	   get_anglet   cross_product_as_unit_axisR	   t   cos_H_to_L_with_xaxist   cos_H_to_L_with_yaxist   cos_H_to_L_with_zaxis(   t   selfR   t   chain_id_lightt   chain_id_heavyt   limit_lightt   limit_heavyt   vh_endt   vl_endt
   mid_H_to_Lt   tranformation_constt   tranformation_vart   eigen_constt	   eigen_vart   eigenvectors_ct   eigenvectors_vt   anglet   zaxist   xaxis(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyt   __init__   s>    		
				
	c         C   sE   t  j | ƒ t  j | ƒ } | j ƒ  } | d k rA | | } n  | S(   sA   retruns normalized vector that starts at "stat" and ends at "end"i    (   R   t   doublet   norm(   R+   R   R   t   xt   l(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR   Q   s
    c         C   s]   t  | ƒ } t  | ƒ } t j t j | ƒ | ƒ } | j ƒ  } | d k rY | | } n  | S(   Ni    (   t   tupleR   R=   R   t   cross_product_matrixR>   (   R+   t   at   bR?   R@   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR'   Y   s    c         C   s“   | rJ | rJ | j  | ƒ } t | ƒ } | d k s9 t ‚ d t | } n d } | d k  ro | ro d | } n  | d k r� | r� d | } n  | S(   s-   retrun the larger angle between vec1 and vec2i    i´   iZ   (   R%   R   t   AssertionErrorR   (   R+   R   R   t   largert	   angle_cost   acos_angle_cosR9   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR&   b   s      c         C   sÌ   | j  ƒ  } | j ƒ  } d } xL t | ƒ D]> \ } } t | t ƒ s+ t d | ƒ d k  ri | } Pqi q+ q+ W| sz t ‚ | | | d !} | | j | ƒ } | j	 t
 j d d d g ƒ ƒ rÈ d } n  | S(   sA   
    Get the eigen vector for eigen value 1 and normalize it
    i   g�íµ ÷Æ°>i   N(   t   vectorst   valuest   Nonet	   enumeratet
   isinstancet   complext   absRE   R%   t   all_eqR   R=   (   R+   t   eigent   vt   et   indxt   iR?   t   eigenvector(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR$   o   s    !	c         C   sâ   |  j  } | j ƒ  j } | j | |  j ƒ ƒ |  _ | j | |  j ƒ ƒ |  _ | j | |  j ƒ ƒ |  _	 | j | |  j
 ƒ ƒ |  _ | |  j ƒ j t ƒ d k r± t d ƒ ‚ n  | |  j ƒ j t ƒ d k rÞ t d ƒ ‚ n  d S(   sE   Create seperate pdb hierarchy for each on the chains we want to aligni    s)   No atoms in light chain. Check chain names)   No atoms in heavy chain. Check chain nameN(   R   t   atom_selection_cachet	   selectiont   selectt   select_var_str_HR   t   select_const_str_HR   t   select_var_str_LR   t   select_const_str_LR    t   countt   TrueR   (   R+   t   pht   test(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR   …   s    	c         C   sV   d d l  m } | j j ƒ  } | j | d t ƒ  d t d t d | d | ƒ} | j S(   Niÿÿÿÿ(   t   superpose_pdbst   logt   write_outputt   save_lsq_fit_objt   pdb_hierarchy_fixedt   pdb_hierarchy_moving(	   t   phenix.command_lineRb   t   master_paramst   extractt   managerR   t   FalseR_   t   lsq_fit_obj(   R+   R   R   Rb   t   paramsR?   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR   ”   s    		c         C   s„   d } d } | j  | d | | ƒ |  _ | j  | | d d | ƒ |  _ | j  | d | | ƒ |  _ | j  | | d d | ƒ |  _ d S(   st   create selection strings for the heavy and light chains
    separating the variable and constant parts of the chainss9   pepnames and (name ca or name n or name c) and altloc " "s$   chain {0} and resseq {1}:{2} and {3}i   R   N(   t   formatRZ   R[   R\   R]   (   R+   R   R   R   R   t   s1t   s2(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR   ¤   s    (   t   __name__t
   __module__R<   R   R'   R_   R&   R$   R   R   R   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyR	      s   ?						(   t
   __future__R    t   scitbx.linalgR   t   scitbx.array_familyR   t   libtbx.utilsR   R   t   scitbxR   t   mathR   R   t   iotbxR   t	   iotbx.pdbt   objectR	   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/utils/fab_elbow_angle.pyt   <module>   s   