ó
¥™xTc           @   sä   d  Z  d d l m Z d d l m Z m Z m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z m Z d d l Z d d	 d
 d g Z d e f d „  ƒ  YZ d e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d e e d „ Z d S(   sÑ   
Model validation against experimental data, in both real and reciprocal space.
This does not actually handle any of the scaling and fmodel calculations,
which are performed approximately as in model_vs_data.
iÿÿÿÿ(   t   division(   t   residuet   atomt
   validation(   t   Autot   slots_getstate_setstate(   t   format_value(   t   null_outt   SorryNt   b_isot   fofct   two_fofct   fmodelt   residue_real_spacec           B   s<   e  Z e j e Z e d  „  ƒ Z e d „  ƒ Z d „  Z	 RS(   c         C   s   |  j  S(   N(   t   score(   t   self(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   cc   s    c           C   s   d d S(	   Ns   %-20s  %6s  %4s  %6s  %6s  %5sR   R	   t   occs   2Fo-Fct   Fmodelt   CC(   s   atoms   b_isoR   s   2Fo-FcR   s   CC(    (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   header   s    c         C   s/   d |  j  ƒ  |  j |  j |  j |  j |  j f S(   Ns(   %-20s  %6.2f  %4.2f  %6.2f  %6.2f  %5.3f(   t   id_strR	   t	   occupancyR   R   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt	   as_string#   s    (
   t   __name__t
   __module__R   t	   __slots__t   __real_space_attr__t   propertyR   t   staticmethodR   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyR      s   t   data_statisticsc           B   sƒ   e  Z d  d d d d d d d d d	 d
 d d d d d d d d d d g Z d d e d „ Z e j d d „ Z e j d d „ Z	 RS(   t   d_maxt   d_mint   infot
   wavelengtht   n_reflt   n_refl_refinet   n_freet   r_workt   r_freet   twin_lawt   wilson_bt   completenesst   r_work_outert   r_free_outert   d_max_outert   d_min_outert   completeness_outert   n_refl_outert   n_refl_refine_outert   n_free_outert   anomalous_flagi
   c   
      C   sþ  | j  ƒ  j ƒ  } | j ƒ  j ƒ  } | j ƒ  |  _ | j ƒ  d |  _ | j ƒ  |  _ | j d | ƒ |  _ d  |  _	 | d  k	 rò t
 | j ƒ  d d  ƒ |  _	 | j ƒ  j ƒ  } | j d |  j d |  j ƒ } | j ƒ  rø |  j rø | j ƒ  } qø n | } | r/|  j r/| j ƒ  } | j ƒ  } | j ƒ  } n  | j d | ƒ | j ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ | j ƒ  j t ƒ |  _ |  j j |  _ |  j j |  _ | j |  _ | j ƒ  |  _ | j ƒ  |  _ | j ƒ  j | ƒ } d  | k sðt ‚ | d |  _  | d |  _! | j d |  j  d |  j! ƒ |  _" | j d |  j  d |  j! ƒ |  _# | j d |  j  ƒ |  _$ | j d |  j  d |  j! ƒ j ƒ  j ƒ  |  _% | j d |  j  d |  j! ƒ } | j ƒ  j ƒ  |  _& | j d |  j  d |  j! ƒ }	 |	 j ƒ  j t ƒ |  _' d  S(   Ni    t   n_binsR"   R   R    i   ((   t   f_obst	   deep_copyt   r_free_flagsR3   t	   d_max_minR   R    R!   t   NoneR"   t   getattrt
   map_to_asut   eliminate_sys_absentt   resolution_filtert   average_bijvoet_matest   setup_binnert   indicest   sizeR#   R$   t   datat   countt   TrueR%   R'   R&   R(   R)   R*   t   binnert   bin_d_ranget   AssertionErrorR-   R.   R,   R+   R/   R0   R1   R2   (
   R   R   t   raw_dataR4   t    count_anomalous_pairs_separatelyR5   R7   t   d_max_min_outert   f_obs_outert   r_free_flags_outer(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   __init__?   sX    	t    c         C   sI   | d | |  j  f IJ| d | |  j f IJ| d | |  j f IJd  S(   Ns   %sHigh resolution       = %7.3fs   %sR-work                = %8.4fs   %sR-free                = %8.4f(   R    R&   R'   (   R   t   outt   prefix(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   show_summaryq   s    c         C   s,  d „  } |  j  d k r2 | d | |  j  f IJn  | d | |  j |  j |  j |  j f IJ| d | |  j |  j f IJ| d | |  j |  j	 f IJ| d | |  j
 j d |  j d f IJ| d	 | |  j | d
 |  j ƒ f IJ| d | |  j | d
 |  j ƒ f IJ|  j
 j d | d | d d ƒ d  S(   Nc         S   s   t  |  | d d ƒS(   Nt   replace_none_withs   ----(   R   (   t   fst   val(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   fvw   s    i    s#   %sWavelength                 = %.4gs9   %sResolution range           = %7.3f - %.3f (%.3f - %.3f)s'   %sNumber of reflections      = %8d (%d)s'   %s   after outlier rejection = %8d (%d)s/   %sCompleteness               = %6.2f%% (%.2f%%)id   s)   %sR-work                     = %8.4f (%s)s   %.4fs)   %sR-free                     = %8.4f (%s)RP   RO   t   titles   By resolution bin:(   Ni    (   R"   R9   R   R    R-   R.   R#   R0   R$   R1   R!   t   completeness_in_rangeR/   R&   R+   R'   R,   t$   show_rwork_rfree_number_completeness(   R   RO   RP   RU   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   showv   s$    	
"




N(
   R   R   R   R9   t   FalseRM   t   syst   stdoutRQ   RY   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyR   '   s2   	1t
   real_spacec           B   sJ   e  Z d  Z d „  Z d d „ Z e j d d „ Z e j d e d „ Z	 RS(   sN   
  Real-space correlation calculation for residues with at least two atoms.
  c         C   s   t  S(   N(   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   get_result_class�   s    gš™™™™™é?c         C   s¯  t  j |  ƒ d d l m } y^ | j ƒ  j ƒ  } d | _ t | j _	 t | j
 _	 | j d | d | d | d t ƒ  ƒ } Wn" t k
 rŸ } d t | ƒ GHnXg  } xt | ƒ D]ô \ }	 }
 |
 j d	 k s³ |
 j j d
 k ræ q³ n  t d |
 j d |
 j j d |
 j j d |
 j j d d d |
 j d |
 j d |
 j d |
 j d |
 j d |
 j | k  d |
 j j ƒ  j ƒ  j ƒ  ƒ } | j ƒ  r—|  j  d	 7_  n  |  j! j" | ƒ q³ Wd  S(   Niÿÿÿÿ(   t   real_space_correlationR   R   t   pdb_hierarchyt   paramst   logs	   Error: %si   t   HOHt   chain_idt   resnamet   resseqt   icodet   altlocRN   R   R	   R   R   t   outliert   xyz(#   R   RM   t   mmtbxR_   t   master_paramst   extractt   detailRZ   t   map_1t   fill_missing_reflectionst   map_2t   simpleR   t	   Exceptiont   strt	   enumeratet   n_atomsR   Re   R   Rd   Rf   Rg   R   t   bR   t   map_value_1t   map_value_2t   atomst   extract_xyzt   meant
   is_outliert
   n_outlierst   resultst   append(   R   R   R`   t   cc_minR_   t
   rsc_paramst   rsct   et
   rsc_by_rest   it   result_t   result(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyRM   ‘   sD    		!						RN   c         C   s   | | d |  j  IJd  S(   Ns&   %d residues with CC(Fc,2mFo-DFc) < 0.8(   R~   (   R   RO   RP   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyRQ   »   s    
s     c         C   s}   |  j  d k rc | | |  j ƒ  j ƒ  IJx7 |  j D]) } | j ƒ  r3 | | t | ƒ IJq3 q3 Wn  |  j d | d | ƒ d  S(   Ni    RO   RP   (   R~   R^   R   R   R}   Rt   RQ   (   R   RO   RP   t   verboseRˆ   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyRY   ¿   s    (
   R   R   t   __doc__R^   RM   R[   R\   RQ   RD   RY   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyR]   ‹   s
   	*i   c         C   sc  d d l  m } | } | j ƒ  d k	 s. t ‚ | j ƒ  }	 |	 d k	 sL t ‚ | j d |  ƒ } | j | j ƒ  d k ƒ j |	 ƒ } | j	 d | d | ƒ }
 |
 j
 } |
 j } | j ƒ  rÎ | rÎ | j ƒ  } n  |  j ƒ  rð | rð |  j ƒ  }  n  | j ƒ  r| r| j ƒ  } n  | j ƒ  r4| r4| j ƒ  } n  | j ƒ  j t ƒ d k rbt d d ƒ ‚ n  | j d	 | j ƒ  ƒ } | j d
 | ƒ \ } } | j d
 |  ƒ \ } }  | j d
 | ƒ \ } } | j d
 | ƒ \ } } t | ƒ j ƒ  } | j ƒ  \ } } | j d |  d | d | d | d | ƒ } | j d | d | d | d | d | d | d | d d ƒ S(   sÁ   
  Compute merging statistics - CC* in particular - together with measures of
  model quality using reciprocal-space data (R-factors and CCs).  See Karplus
  & Diederichs 2012 for rationale.
  iÿÿÿÿ(   t   merging_statisticst   crystal_symmetryi    t   arrayt   sigma_filterings<   R-free array does not select any reflections.  To calculate sE   CC* and related statistics, a valid set of R-free flags must be used.RB   t   otherR5   t   i_obst   i_calct   work_selt   free_selR    R   R4   t   model_arrayst	   anomalousN(   t   iotbxR‹   t   sigmasR9   RG   R!   t   customized_copyt   selectt   set_infot   filter_intensities_by_sigmat   array_mergedR�   R3   R>   RB   RC   RD   R   t   common_setst   abst	   f_as_f_sqR8   t   model_based_arrayst   dataset_statistics(   R5   t   f_modelR7   t   unmerged_i_obsR4   RŽ   R•   R‹   R“   R!   t   filterR�   R’   R‘   R   R    R”   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   merging_and_model_statisticsÇ   s\    							(   RŠ   t
   __future__R    t   mmtbx.validationR   R   R   t   libtbxR   R   t   libtbx.str_utilsR   t   libtbx.utilsR   R   R[   R   R   R   R]   RZ   R¥   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/experimental.pyt   <module>   s    	dA