ó
¦™xTc           @   s¸   d  Z  d d l m Z d d l m Z m Z d d l m Z m Z m	 Z	 d d l
 Z
 d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d „  Z d d „ Z d „  Z d S(   sõ  
Validation of protein geometry by analysis of the positions of C-beta sidechain
atoms.  Significant deviations from ideality often indicate misfit rotamers
and/or necessity of mainchain movement, especially alternate conformations.

Reference:

Lovell SC, Davis IW, Arendall WB 3rd, de Bakker PI, Word JM, Prisant MG,
Richardson JS, Richardson DC.  Structure validation by Calpha geometry: phi,psi
and Cbeta deviation.  Proteins. 2003 Feb 15;50(3):437-50.
http://www.ncbi.nlm.nih.gov/pubmed/12557186
iÿÿÿÿ(   t   division(   t   residuet
   validation(   t   colt   dihedral_anglet   rotate_point_around_axisNt   cbetac           B   s]   e  Z d  Z d d d g Z e j e Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   sK   
  Result class for protein C-beta deviation analysis (phenix.cbetadev).
  t	   deviationt   dihedral_NABBt	   ideal_xyzc           C   s   d d S(   Ns
   %-20s  %5st   Residues   Dev.(   R
   s   Dev.(    (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   header   s    c         C   s   d |  j  ƒ  |  j f S(   Ns   %-20s  %5.2f(   t   id_strR   (   t   self(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt	   as_string#   s    c      
   C   sD   d |  j  |  j j ƒ  |  j |  j |  j |  j |  j |  j |  j  f	 S(   Ns&   %s:%s:%2s:%4s%1s:%7.3f:%7.2f:%7.2f:%s:(	   t   altloct   resnamet   lowert   chain_idt   resseqt   icodeR   R   t	   occupancy(   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt
   format_old'   s    	c         C   sc   d |  j  j ƒ  |  j |  j |  j |  j |  j f } d | |  j |  j d |  j d |  j d f S(   Ns   cb %3s%2s%4s%1s  %.3f %.2fs&   {%s} r=%.3f magenta  %.3f, %.3f, %.3f
i    i   i   (   R   R   R   R   R   R   R   R	   (   R   t   key(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   as_kinemage,   s
    c         C   s/   |  j  d |  j |  j f |  j |  j |  j g S(   Ns   %s %s(   R   R   t   residR   R   R   (   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   as_table_row_phenix3   s    (   t   __name__t
   __module__t   __doc__t   __cbeta_attr__R   t	   __slots__t   staticmethodR   R   R   R   R   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR      s   				t   cbetadevc           B   sÏ   e  Z e j d  g Z d Z d Z d d d d d g Z d d d d	 d
 g Z d d d d d g Z d „  Z	 e
 e j e
 e
 d „ Z e
 d d „ Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z RS(   t
   beta_ideals*   Analyze protein sidechain C-beta deviations4   pdb:alt:res:chainID:resnum:dev:dihedralNABB:Occ:ALT:t   ChainR
   t   Altloct	   Deviationt   Angles   %ss   %.3fs   %.2fid   iÈ   c         C   s   t  S(   N(   R   (   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   get_result_class?   s    c         C   s$  t  j |  ƒ i  |  _ i d  d 6d  d 6d  d 6d  d 6} g  } d d l m } | j d | ƒ }	 x½| j ƒ  D]¯}
 x¦|
 j ƒ  D]˜} |	 r¡ | j	 d | ƒ } n	 | j
 } xk| j ƒ  D]]} xTt | j ƒ  ƒ D]@\ } } x1t | j ƒ  ƒ D]\ } } | j d	 k rqï n  | d
 k } t } i  } xW | j ƒ  D]I } | j | k r5| | | j <t | j ƒ  j ƒ d
 k r~t } q~q5q5W| sŽ| rï t | ƒ d k rï t d | d | j ƒ } | j } | j } | j } | d  k råqï n  | d k sý| t k r| d k r|  j d 7_ n  | r-| j } n d } | j j ƒ  } | j
 } t | ƒ d k rjd | } n  | d } t d | d | j d | j d | j d | d | j  d | j! d | d | d | d | d k ƒ } |  j" j# | ƒ | j$ ƒ  } | r	| |  j | <q	qqï qï WqÐ Wq· Wq€ Wqm Wd  S(   Ns    CA s    N  s    C  s    CB iÿÿÿÿ(   t   utilst	   hierarchyt   chaint   GLYi    i   t   relevant_atomsR   g      Ð?i   t    R   R   R   R   t   xyzR   R   R   R	   t   outlier(%   R   t   __init__R"   t   Nonet   mmtbx.validationR(   t   use_segids_in_place_of_chainidst   modelst   chainst   get_segid_as_chainidt   idt   residue_groupst	   enumeratet
   conformerst   residuesR   t   Falset   atomst   namet   lent   parentR   t   Truet   calculate_ideal_and_deviationR   t   dihedralt   idealt
   n_outliersR   R   R   R   R.   t   occt   resultst   appendR   (   R   t   pdb_hierarchyt   outliers_onlyt   outt   collect_idealt   quiett   relevant_atom_namest   output_listR(   t
   use_segidst   modelR*   R   t   rgt   i_cft   cft	   i_residueR   t   is_firstt   is_alt_confR,   t   atomt   resultt   devt   dihedralNABBt   betaxyzt   altchart   rest   subt   resCBR   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR0   A   s|    							 	
					t   pdbc         C   s\   | r | |  j  IJn  x) |  j D] } | | d | j ƒ  IJq  W| rX |  j | ƒ n  d  S(   Ns    :(   t   output_headerRG   R   t   show_summary(   R   RK   t   verboset   prefixRY   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   show_old_output†   s    t    c         C   s   | | d |  j  IJd  S(   Ns8   SUMMARY: %d C-beta deviations >= 0.25 Angstrom (Goal: 0)(   RE   (   R   RK   Re   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyRc   Ž   s    c         C   s   |  j  S(   N(   RE   (   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   get_outlier_count“   s    c         C   s   d S(   Ni    (    (   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   get_expected_count–   s    c         C   s   |  j  S(   N(   R"   (   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   get_beta_ideal™   s    c         C   si   d } | d 7} xR |  j  D]G } | j ƒ  r | d  k sG | | j k ra | | j ƒ  d 7} qa q q W| S(   Ns   @subgroup {CB dev} dominant
sI   @balllist {CB dev Ball} color= gold radius= 0.0020   master= {Cbeta dev}
s   
(   RG   t
   is_outlierR1   R   R   (   R   R   t	   cbeta_outRY   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR   œ   s    
c         C   s^   g  } xQ |  j  D]F } | j ƒ  r | j | j | j | j | j | j | j f ƒ q q W| S(   N(	   RG   Rk   RH   R   R   R   R   R   R.   (   R   t   dataRY   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   as_coot_data¥   s     N(   R   R   R   R   t   program_descriptionRb   t   gui_list_headerst   gui_formatst   wx_column_widthsR'   R<   t   syst   stdoutR0   Rf   Rc   Rh   Ri   Rj   R1   R   Rn   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR!   7   s$   	A				RB   c           B   s    e  Z d  d d g Z d „  Z RS(   R   RD   RC   c      	   C   s¥  | d k s t  ‚ d  |  _ d  |  _ d  |  _ | d } | d } | d } | d } t | ƒ \ } } }	 }
 } } t | | | | | |	 d d ƒ} t | | | | |
 | d d ƒ} d  | | g k r¡t | ƒ t | ƒ d	 } t t | j	 ƒ | ƒ } | d
 k r¡| | k rC| t | j	 ƒ } t | j	 ƒ | | | } n  t t | j	 ƒ | ƒ |  _ t
 d | j	 | j	 | j | j	 g d t ƒ |  _ | j |  _ q¡n  d  S(   NR+   s    CA s    N  s    C  s    CB t   methodt   NCABt   CNABi   i    t   sitest   deg(   t   AssertionErrorR1   R   RD   RC   t   idealized_calpha_anglest   construct_fourthR   t   absR.   R   t   elemsRA   (   R   R,   R   t   resCAt   resNt   resCR`   t   distt   angleCABt   dihedralNCABt   angleNABt   dihedralCNABt
   angleidealt   betaNCABt   betaCNABR\   t   betadistt   distTemp(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR0   ¯   sD    			



		*(   R   R   R   R0   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyRB   ­   s   c         C   s  |  d k r3 d } d } d } d } d } d } n½ |  d k rf d	 } d
 } d } d } d } d } nŠ |  d k r™ d } d } d } d } d } d } nW |  d k rÌ d } d } d } d } d } d } n$ d	 } d } d } d } d } d } | | | | | | f S(    Nt   ALAgú~j¼t“ø?gfffff†[@gš™™™™¹^@gfffff¦[@gfffff¦^ÀgÍÌÌÌÌÌ[@t   PROg{®Gázø?gÍÌÌÌÌ\@gfffffÆ\@g     ÀY@gÍÌÌÌÌ,^Àg33333ó[@t   VALt   THRt   ILEg¤p=
×£ø?gfffffF[@gš™™™™Ù^@g     à[@g     €^ÀR+   gš™™™™™ñ?g33333S[@gffffff^@gffffff^Àg      \@g33333³^@g      [@(   RŽ   R�   R�   (    (   R   R‚   Rƒ   R„   R…   R†   R‡   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR{   Ô   sF    				Rv   c         C   s   d  |  | | g k rü| d k r6 |  } | } | }	 n! | d k rW | } |  } | }	 n  t |	 j ƒ t | j ƒ }
 t | j ƒ t | j ƒ } |
 j | ƒ } t | ƒ } | d k rÇ | | | 9} n  | t |	 j ƒ 7} | } | d } t | j ƒ }
 t |	 j ƒ } t t d | j d |	 j d | j d | d	 t ƒ ƒ } | t |	 j ƒ }
 t | j ƒ t |	 j ƒ } |
 j | ƒ } t | ƒ } | d k r¥| | | 9} n  d | } t |	 j ƒ }
 | |
 7} | } t d |
 j d | j d | j d | d	 t ƒ Sd  S(
   NRv   Rw   g�íµ ÷Æ°>iZ   t   axis_point_1t   axis_point_2t   pointt   angleRy   (   R1   R   R.   t   crossR}   R   R~   RA   (   R€   R   R�   R‚   R”   RC   Ru   t   res0t   res1t   res2t   at   bt   ct   cmagt   dt
   angledhdrlt   newD(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyR|   ú   sV    		
				

			c   	      C   s§  |  j  ƒ  } t | ƒ d k rS i  } x% | d j ƒ  D] } | | | j <q5 W| g Sg  } d d d d g } i  } x| D]þ } | j j ƒ  d k rÌ xà | j ƒ  D]% } | j | k r  | | | j <q  q  Wqx i  } x3 | j ƒ  D]% } | j | k rß | | | j <qß qß Wt | ƒ d k r qx n  x1 | j ƒ  D]# } | | k r-| | | | <q-q-Wt | ƒ d k rx | j | ƒ qx qx Wt | ƒ d k rŸ| j d | ƒ n  | Sd	 S(
   s¨  
  Given a residue_group object, which may or may not have multiple
  conformations, extract the relevant atoms for each conformer, taking into
  account any atoms shared between conformers.  This is implemented
  separately from the main validation routine, which accesses the hierarchy
  object via the chain->conformer->residue API.  Returns a list of hashes,
  each suitable for calling calculate_ideal_and_deviation.
  i   i    s    CA s    N  s    CB s    C  Rg   i   N(	   t   atom_groupsR?   R=   R>   R   t   stript   keysRH   t   insert(	   t   residue_groupR    R,   RX   t   all_relevant_atomst   expected_namest	   main_conft
   atom_groupt	   atom_name(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt    extract_atoms_from_residue_group(  s8    	 (   R   t
   __future__R    R2   R   R   t   scitbx.matrixR   R   R   Rs   R   R!   t   objectRB   R{   R|   Rª   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/cbetadev.pyt   <module>   s   #v'	&.