ó
¦™xTc           @   sÜ   d  Z  d d l m Z d d l m Z m Z m Z m Z d d l m	 Z	 d d l
 Z
 d e	 f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s4   
Analysis of model properties, independent of data.
iÿÿÿÿ(   t   division(   t   atomt   residuet
   validationt   dummy_validation(   t   slots_getstate_setstateNt   model_statisticsc           B   s‰   e  Z d  Z d d d d d d d d d	 d
 d d g Z d e d d „ Z e j d d „ Z	 e
 d „  ƒ Z e
 d „  ƒ Z e
 d „  ƒ Z RS(   sÝ   
  Atom statistics for the overall model, and various selections within.
  This does not actually contain individual outliers, which are instead held
  in the xray_structure_statistics objects for subsets of the model.
  t   allt   _macromoleculest   _ligandst   _watert   n_modelst   n_atomst   n_hydrogenst   n_waterst	   n_polymert	   n_proteint   n_nuct	   ignore_hdc         C   sE  x! |  j  D] } t |  | d  ƒ q
 Wt | t ƒ ri | d  k	 rQ | j | ƒ } qi | j ƒ  j | ƒ } n  | |  _ | j ƒ  j	 ƒ  |  _
 | j ƒ  j t ƒ |  _ t | j ƒ  ƒ |  _ | j ƒ  } | j } | j d d ƒ | j d d ƒ |  _ d |  _ d |  _ |  _ | }	 | d  k	 r,| j | ƒ }	 n  |	 j ƒ  d }
 xª |
 j ƒ  D]œ } | j ƒ  } t } d } | s‚| j ƒ  } d } n d } | j ƒ  } | s | rIt | ƒ } |  j | 7_ | rÓ|  j | 7_ qå|  j | 7_ qIqIWt d | d | d	 | ƒ |  _  | d  k	 rí| j d
 ƒ } | j d ƒ } | d  k rO| j d ƒ } n  | | M} | j t ƒ d k r¢t d | j | ƒ d | j | ƒ d	 | ƒ |  _! n  | j t ƒ d k rít d | j | ƒ d | j | ƒ d	 | ƒ |  _" qín  | d  k	 rA| j t ƒ d k rAt d | j | ƒ d | j | ƒ d	 | ƒ |  _# n  d  S(   Nt   HOHi    t   WATt   others   nucleic acidt   proteint   pdb_hierarchyt   xray_structureR   s   protein or rna or dnat   waters$   not (protein or dna or rna or water)($   t	   __slots__t   setattrt   Nonet
   isinstancet
   basestringt	   selectiont   atom_selection_cacheR   t
   scattererst   sizeR   t   hd_selectiont   countt   TrueR   t   lent   modelsR   t   overall_countst   resnamest   getR   R   R   R   t   selectt   chainst
   is_proteint   Falset   is_nat   residue_groupst   xray_structure_statisticsR   R   R
   R	   (   t   selfR   R   t   all_chain_proxiesR   t   ligand_selectiont   namet   countsR)   t   hierarchy_tmpt   first_modelt   chainR-   R/   t
   chain_typeR0   t   n_rest	   macro_selt	   water_sel(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   __init__   sz    		%		!t    c         C   sú   | | d IJ|  j  j d | d | d ƒ |  j  j d | d | d ƒ |  j sZ |  j rÍ xp t d d d g |  j |  j |  j g ƒ D]A \ } } | r… | | d | IJ| j d | d | d ƒ q… q… Wn  |  j rö |  j d	 k rö | | d
 IJn  d  S(   Ns   Overall:t   outt   prefixs     t   Macromoleculest   Ligandst   Waterss   %s:i    s0   (Hydrogen atoms not included in overall counts.)(	   R   t   show_summaryt   show_bad_occupancyt   ligandsR   t   zipt   macromoleculesR   R   (   R2   R@   RA   t   labelt   props(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   showk   s    %$c         C   s   |  j  d  k r t ƒ  S|  j  S(   N(   R   R   R   (   R2   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRI   x   s    c         C   s   |  j  d  k r t ƒ  S|  j  S(   N(   R
   R   R   (   R2   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR   ~   s    c         C   s   |  j  d  k r t ƒ  S|  j  S(   N(   R	   R   R   (   R2   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRG   „   s    N(   t   __name__t
   __module__t   __doc__R   R   R%   R>   t   syst   stdoutRL   t   propertyRI   R   RG   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR      s(   	Ht   residue_occupancyc           B   s0   e  Z e j d  d g Z d d „ Z d „  Z RS(   R:   t   b_isoR?   c         C   s    d |  j  |  j |  j |  j f S(   Ns    %3s%2s%5s (all)  occ=%.2f(   t   resnamet   chain_idt   residt	   occupancy(   R2   RA   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt	   as_string�   s    c         C   s   |  j  ƒ  d |  j |  j g S(   NR   (   t   id_strRX   RT   (   R2   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   as_table_row_phenix‘   s    (   RM   RN   R   R   RY   R[   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRS   Š   s   t   atom_occupancyc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sY   
  Container for single-atom occupancy outliers (usually atoms with zero
  occupancy).
  R?   c         C   s   d |  j  ƒ  |  j |  j f S(   Ns   %s %4s   occ=%.2f(   t   atom_group_id_strR5   RX   (   R2   RA   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRY   ™   s    c         C   s   |  j  ƒ  d |  j |  j g S(   NR   (   RZ   RX   RT   (   R2   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR[   �   s    (   RM   RN   RO   RY   R[   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR\   ”   s   t   residue_bfactorc           B   s   e  Z d  d „ Z RS(   R?   c         C   s   d |  j  ƒ  |  j |  j f S(   Ns   %s %4s   b_iso=%.2f(   R]   R5   RT   (   R2   RA   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRY   ¢   s    (   RM   RN   RY   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR^       s   t   atom_bfactorc           B   s   e  Z RS(    (   RM   RN   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR_   ¦   s   R1   c           B   sð   e  Z d  Z e j d d d d d d d d d	 d
 d d d d d d d d d g Z d d d d g Z d d d d g Z d d d d g Z e e d „ Z	 e
 j d d „ Z e
 j d d  „ Z e
 j d d! „ Z d$ e d" „ Z d$ e e d# „ Z RS(%   s(   
  Occupancy and B-factor statistics.
  t   n_allR   t   n_non_hdt   n_hdt   n_anisot	   n_aniso_ht   n_zero_bt
   n_zero_occt   n_npdt   b_meant   b_mint   b_maxt   o_meant   o_mint   o_maxt   zero_occt   partial_occt   bad_adpst   b_histograms   Atom(s)t   Typet	   Occupancys   Isotropic B-factors   %ss   %.2fi,  id   iÈ   c   (         s³  x! |  j  D] } t |  | d  ƒ q
 Wt j |  ƒ t | j ƒ  ƒ d k sO t ‚ d d l m	 } d d l
 m } | } | j ƒ  j ƒ  |  _ d  |  _ | j ƒ  }	 |	 j ƒ  | j ƒ  }
 t } |
 j ƒ  |  _ |
 j t ƒ |  _ | rd |  j k  oÿ |  j k  n r| j |
 ƒ } t } n  | j ƒ  } | j ƒ  j ƒ  } | j ƒ  j ƒ  |  _ |  j |  j |  _ | j ƒ  j t ƒ |  _ | j ƒ  |
 @j t ƒ |  _ | j ƒ  j t ƒ |  _  | d k j t ƒ |  _! | d k j t ƒ |  _" t# j$ ‰  d ‰ | j | d k ƒ } t | ƒ d k r[| j% | ƒ } | j& ƒ  } | j' ƒ  d | ‰  | j' ƒ  d | ‰ n  | j( | j' | ƒ ƒ |  _) | j( | j* | ƒ ƒ |  _+ | j( | j, | ƒ ƒ |  _- | j' | ƒ |  _. | j* | ƒ |  _/ | j, | ƒ |  _0 |  j |  j  |  _1 g  |  _2 g  |  _3 g  |  _4 d  |  _5 ‡  ‡ f d †  } | j ƒ  j ƒ  } | j ƒ  } | j6 | j ƒ  t ƒ } | r¯x�t7 | ƒ D]\ } } |
 | r�| st| | r qtn  |	 | } | j8 ƒ  } | d k rD| j ƒ  } | j9 ƒ  } t | ƒ d k rº| j: ƒ  j; d ƒ rº| j< ƒ  } | j( | j' | j | ƒ ƒ ƒ } t= d | j> d	 | j? d
 | j@ d | jA d | jB d | d t d | jC ƒ  j' ƒ  d | ƒ 	} |  j2 jD | ƒ |  j1 d 7_1 | jE | t ƒ qó| jF | k sët d | jG ƒ  | jF | f ƒ ‚ tH d | d | d | j( | | ƒ d | jI d t ƒ } |  j2 jD | ƒ |  j1 d 7_1 qt| | | ƒ rt| | d k r½tJ d | d | d | j( | | ƒ d | jI d t ƒ } |  j4 jD | ƒ |  j1 d 7_1 qó| j ƒ  } | j< ƒ  } | j' | j | ƒ ƒ } | | ƒ rš| j9 ƒ  } tK d | j> d	 | j? d
 | j@ d | jA d | jB d | d t d | jC ƒ  j' ƒ  d | j( | ƒ ƒ 	} |  j4 jD | ƒ |  j1 d 7_1 | jE | t ƒ qótJ d | d | d | j( | | ƒ d | jI d t ƒ } |  j4 jD | ƒ |  j1 d 7_1 qtqtW| jL ƒ  d } x¥| jM ƒ  D]”} | jN ƒ  } t }  d }! | sM| jO ƒ  }  d }! n d }! | jP ƒ  }" xF| jP ƒ  D]8}# |# jQ ƒ  }$ t |$ ƒ d k rlt |$ d j ƒ  ƒ d k rlt tR g  |$ D] }% |% jB ^ q¹ƒ ƒ d k rltS g  |$ D] }% |% j ƒ  d jF ^ qäƒ }& |& d k r¤|& d k r¤|# j ƒ  jC ƒ  j' ƒ  }' t= d | jT d	 |# j? d
 |# j@ d |$ d jB d |& d |! d t d |' ƒ } |  j3 jD | ƒ |  j1 d 7_1 q¤qlqlWqWn  d  S(   Ni    iÿÿÿÿ(   t   adptbx(   t   flexi   g      @c            s"   |  ˆ k  p! |  ˆ  k p! |  d k S(   Ni    (    (   t   u(   t   u_cutoff_hight   u_cutoff_low(    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   is_u_iso_outlier÷   s    RV   t   resseqt   icodet   altlocRU   RX   t   outliert   xyzRT   s   %s: %s <--> %st   pdb_atomR   s   nucleic acidR   R:   (U   R   R   R   R   R>   R&   R!   t   AssertionErrort   cctbxRt   t   scitbx.array_familyRu   R"   t   n_totalt   resultst   atomst   reset_i_seqR#   R%   R`   R$   Rb   R+   R.   t   extract_u_iso_or_u_equivt   extract_occupanciesR   Ra   t   use_u_anisoRc   Rd   t   is_positive_definite_uRg   Re   Rf   RP   t   maxintt   mean_and_variancet$   unweighted_sample_standard_deviationt   meant   u_as_bRh   t   minRi   t   maxRj   Rk   Rl   Rm   t
   n_outliersRn   Ro   Rp   Rq   t   boolt	   enumeratet   parentt   fetch_labelst   extract_occt   all_eqt   extract_i_seqRS   RV   Rz   R{   R|   RU   t   extract_xyzt   appendt   set_selectedt   occRZ   R\   R~   R_   R^   R'   R,   R-   R/   R0   t   atom_groupst   sett   sumt   id((   R2   R   R   R   t   collect_outliersR5   Rt   Ru   t   xrst	   pdb_atomsR#   t   subtract_hdt   u_isosR�   t
   u_non_zerot   mvt   sigmaRy   t   occupanciest	   collectedt   i_seqR   R•   t   group_atomst   labelst   i_seqsRh   R}   t   u_meanR8   R9   R-   R/   R:   R0   t   residue_groupRž   t   agt	   total_occR~   (    (   Rw   Rx   sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR>   Ç   s*   	
(						
'!																		..,				R?   c         C   sñ   | | d |  j  |  j f IJ| | d |  j |  j |  j f IJ|  j d k ri | | d |  j IJn  |  j d k rÆ | | d |  j |  j |  j f IJ|  j	 d k rÆ | | d |  j	 IJqÆ n  |  j
 d k rí | | d |  j
 IJn  d  S(	   Ns(   Number of atoms = %d  (anisotropic = %d)s-   B_iso: mean = %5.1f  max = %5.1f  min = %5.1fi    s(     warning: %d anisotropic hydrogen atomsg      ð?s1   Occupancy: mean = %4.2f  max = %4.2f  min = %4.2fs'     warning: %d atoms with zero occupancys2   %d total B-factor or occupancy problem(s) detected(   R   Rc   Rh   Rj   Ri   Rd   Rl   Rk   Rm   Rf   R’   (   R2   R@   RA   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRE   j  s$    



c         C   sR   t  |  j ƒ d k rN | | d IJx( |  j D] } | | t | ƒ IJq- Wn  d  S(   Ni    s&   Atoms or residues with zero occupancy:(   R&   Rn   t   str(   R2   R@   RA   R}   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyRF   ~  s    c         C   s(   | | d |  j  |  j |  j f IJd  S(   Ns-   B_iso: mean = %5.1f  max = %5.1f  min = %5.1f(   Rh   Rj   Ri   (   R2   R@   RA   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   show_bfactors„  s    
c         c   s�   | d  k r& |  j |  j |  j } nG | d k rE |  j |  j } n( | d k r] |  j } n t d | ƒ ‚ x) | D]! } | j ƒ  s� | rt | Vqt qt Wd  S(   NRX   t   b_factors   Unknown property type '%s'(   R   Rn   Ro   Rp   t   RuntimeErrort
   is_outlier(   R2   t   property_typet   outliers_onlyt   outlierst   result(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   iter_resultsˆ  s    c         C   sŸ   g  } x’ |  j  d | d | ƒ D]x } g  } | r@ | j ƒ  } n  | j ƒ  } t | ƒ t |  j ƒ k o{ t |  j ƒ k n s† t ‚ | j | | ƒ q W| S(   NR¹   Rº   (   R½   t	   zoom_infoR[   R&   t   gui_list_headerst   gui_formatsR€   R›   (   R2   R¹   Rº   t   include_zoomt   tableR¼   t   extrat   row(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   as_gui_table_data•  s    :N(   RM   RN   RO   R   R   R¿   RÀ   t   wx_column_widthsR%   R>   RP   RQ   RE   RF   Rµ   R   R½   R.   RÅ   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyR1   «   s@   
¢(   RO   t
   __future__R    t   mmtbx.validationR   R   R   R   t   libtbxR   RP   R   RS   R\   R^   R_   R1   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/model_properties.pyt   <module>   s   "
