ó
§™xTc        3   @   sF  d  d l  m Z d  d l m Z d  d l 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 d  d
 l m Z d  d l Z d  d l Z d Z d Z e j d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> g2 ƒ Z e j d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` da db dc dd de df dg dh di dj dk dl dm dn do dp g2 ƒ Z dq e j j f dr „  ƒ  YZ ds e j j f dt „  ƒ  YZ d S(u   iÿÿÿÿ(   t   division(   t   absolute_scalingN(   t
   data_plots(   t   flex(   t   scale_curves(   t   simplex(   t   chebyshev_polynome(   t   Sorry(   t   table_utilsgõ�4äÜgW?gl¦¢|‚Fí?g5&l÷K¶Û¿gê™ø�ÍdÕ?gÁ„££P×¿gU FÌd}Ô?g
„ ‹t=Õ¿g Nƒ9t„Ð?g<F1ÿGË¿gQï®‘E¢È?g&ZÕ�Å¿gÁ{F¸’Ã?g¤a¡\†À¿g(kˆ-rs¹?gßâ:k¶¶¿g/æ"ƒâ¸?g#tiÞ5»¿g²t<Œ¼?g¡{|i!¹¿g‘‘Z¦Î´?gÌê¨Î¢²¿gF	1ÓJ°?gû’YÈª¿g8{>°�¦?gÎ”${ªa¡¿gnqcœ¨™?gwútF’¿g ª1Û§Š?gW¹8œ‚¿gã¬HC˜u?gf~5‚�ìE¿gæO‡åLl\¿gS,y»ç_?gÓ¸)�âuI¿g1çÃä¿gUžùØõ¹b?gš¹éq¿g!ˆ‘˜Óés?g`jÚè3tt¿g/V0�^Èy?g‚†•Û}v¿g¾Ì½X§yp?gSûƒåk¿g§}ë‰vq?gÜ~ë®)\n¿gÁÊ/îe?goVn0’*a¿gÃ˜Xlkm?g0pÔ;£g¿g œy–½4a?g~ÂÛá"ÑW¿gd”&Œæc?g¯AÈðÌ?g¸Th2�'ª¿g<]Þ­ƒº¹?gzG|Æ�®¿ggñ¯io´?g/Wå®l¯¿gè„ír�§?gãÄÜà�ø§¿g>5!ç7£?g
:å¤S ¿g>ñ]„JáŸ?g .­ÑÐàš¿gðÇ÷ÚÕ™?gñíº©Š—¿gÜM^Õ“?g7ÿG1j/•¿g²ÿÊ¼´–?goð«^}�“¿g”‘‘_ª$“?gZñÙˆjY’¿g.É¨ J‘?gÂ•ö��¿gmìZ²‹?gµàíû‹‡¿gÿ -Ž)ª„?g½›^¹ª¿gþ€ÜD{“|?gtÈlä—�¿g�<ñvJB€?g§éW5Ùy¿g ÈÚ4ñ€}?gf?÷
Eï€¿g=‘82è~?gŠGæq¼ÿ¿g¡Y§ç…5}?gÜÑeÊ¬t¿g±Ïò!? r?gM“hW¹v¿gï¨]ð q?g'@þ�“j¿gÝ
 Mçl?gÝu„çe¿gx={ž4U?g�„î¥ð‹^¿gü±v�7`?g{Iø›)W¿gÁmÖ…H?g¦yo =M¿gÔ¨!<©2D?g–t�ÄÓõI¿t   relative_wilsonc           B   s_   e  Z d  d d d d „ Z d „  Z d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z	 RS(   g        g       @iÐ  g      @c      	   C   s¬  | j  ƒ  j | j  ƒ  ƒ s! t ‚ | j ƒ  r< | j ƒ  } n  | j ƒ  rW | j ƒ  } n  | j ƒ  |  _ | j ƒ  |  _ | |  _ | |  _	 | |  _
 d |  _ | |  _ t j d |  j d t d d d d ƒ } t j d |  j d t d d d d ƒ } | j }	 | j }
 | j j | j ƒ } | j j | j ƒ } |	 t k |	 t k  @| j d k @} | j | ƒ } |	 j | ƒ |  _ |
 j | ƒ j | ƒ |  _ | j j | ƒ |  _ | j j | ƒ j | ƒ |  _ | j j | ƒ |  _ | j j | ƒ j | ƒ |  _ t j |  j |  j	 |  j
 ƒ |  _ |  j j  |  j |  j ƒ \ } |  _ |  _! |  _" |  j j  |  j |  j ƒ \ |  _ |  _ |  _! |  _" |  j j  |  j |  j ƒ \ } |  _ |  _# |  _$ |  j j  |  j |  j ƒ \ |  _ |  _ |  _# |  _$ t% t& j' ƒ  t d	 t d	 t& ƒ |  _( t% t) j' ƒ  t d	 t d	 t) ƒ |  _* t+ j, d d g ƒ |  _- d
 d |  _. |  j |  j. k } | j/ t ƒ d k rƒt0 d d ƒ ‚ n  | j1 ƒ  d |  j |  _2 |  j2 j d ƒ s¶t ‚ t+ j, d
 t+ j3 |  j ƒ t+ j3 |  j ƒ d g ƒ |  _4 t+ j, d d g ƒ |  _5 d
 t+ j3 |  j2 |  j ƒ t+ j3 |  j2 |  j ƒ } d } t+ j, | | g ƒ t+ j, | d | d g ƒ t+ j, | d | d g ƒ g |  _6 t7 j8 d |  j6 |  ƒ |  _9 |  j9 j: ƒ  } t; | d ƒ |  _< | d |  _= |  j> ƒ  |  j2 j d ƒ |  _? |  j? s¨d
 t+ j3 |  j2 |  j ƒ t+ j3 |  j2 |  j ƒ } d } t+ j, | | g ƒ t+ j, | d | d g ƒ t+ j, | d | d g ƒ g |  _6 t7 j8 d |  j6 |  ƒ |  _9 n  d  S(   Ni   t   miller_arrayt   auto_kernelt   n_binsi-   t   n_termi   i    gü©ñÒMbP?g      ð?g      @s6   No reflections within required resolution range after s
   filtering.g       @g        g      à?gš™™™™™¹?gš™™™™™ñ?i   g      0@(@   t   indicest   all_eqt   AssertionErrort   is_xray_amplitude_arrayt	   f_as_f_sqt	   deep_copyt   obst   calct   mindt   maxdt   mt   nt   levelR   t   kernel_normalisationt   Truet   d_star_sq_arrayt   bin_selectiont   selectt   low_limt   high_limt   mean_I_arrayt   obs_d_star_sqt   calc_d_star_sqt   mean_obst	   mean_calct   var_I_arrayt   var_obst   var_calcR   t   curve_interpolatort   interpolt   interpolatet   obs_at   obs_bt   calc_at   calc_bR   t
   mean_coefst   sizet   mean_ratio_enginet	   std_coefst   std_ratio_engineR   t   doublet   xt   low_lim_for_scalingt   countR   t	   as_doublet   weight_arrayt   sumt   meant   sigmast   sart_simplexR   t   simplex_optt   optit   get_solutiont   abst   scalet   b_valuet   modify_weightst   all_bad_z_scores(   t   selft
   miller_obst   miller_calct   min_d_star_sqt   max_d_star_sqt   n_pointsR   t	   norma_obst
   norma_calcR#   R$   t   sel_calc_obst   sel_obs_calct   selt   tmp_obs_d_star_sqt   tmp_calc_d_star_sqt	   selectiont   st   bt   sol(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt   __init__   s˜    !															0303 ,#
	,#c         C   sâ   t  j |  j |  j ƒ |  j |  j } |  j d k j ƒ  } t  j | j	 | ƒ |  j j	 | ƒ ƒ } t  j
 |  j j ƒ  d ƒ } | j | | ƒ |  j | |  j |  j ƒ |  j |  j |  j ƒ g } t d | d |  j d |  j ƒ S(   Ni    t
   all_curvesR   RG   (   R   t   expR$   RE   R&   RD   R%   t
   iselectiont   logR   R6   R2   t   set_selectedt   curvet   get_z_scorest   summaryR   RG   (   RH   t   i_scaledRR   t   ratiot   ratio_t   curves(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRa   {   s    '(	i   c         C   sP   |  j  |  j |  j ƒ } t j t t j | | k  ƒ ƒ ƒ } |  j | |  _ d  S(   N(   R`   RD   RE   R   R6   t   listt   boolR;   (   RH   R   t   z_scoresRR   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRF   �   s    $c         C   s&  t  j |  j | ƒ |  j | } |  j d k | d k @j ƒ  } |  j j | ƒ | j | ƒ } |  j |  j ƒ j | ƒ } | j d ƒ s� t	 ‚ t  j
 | ƒ } |  j |  j ƒ j | ƒ } |  j j | ƒ } | j d ƒ sà t	 ‚ t  j | | ƒ | }	 t  j |  j j ƒ  d ƒ }
 |
 j | |	 ƒ |
 S(   Ni    iÿÿÿÿ(   R   R[   R$   R&   R%   R\   R   R_   t   all_gtR   R]   t   stdt   all_neRC   R6   R2   R^   (   RH   RD   RE   Rb   RR   Rc   R=   t   vart	   d_star_sqt   zt   z_(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyR`   ’   s    !c   
      C   si  d } t  | d ƒ } | d } | d k r5 d } n  | d k  rJ d } n  t j |  j | ƒ |  j | } | |  j } |  j |  j ƒ } | t j | ƒ } t j | ƒ t j	 t
 j j ƒ k rÐ t d ƒ ‚ n  | | } t j |  j ƒ }	 |	 d k sþ t ‚ |	 d k rIt j | ƒ t
 j j |	 k rIt d d t j | ƒ ƒ ‚ n  | |  j } t j | ƒ } | S(	   Ng      ð?i    i   g      i@g      iÀs*   Result array exceeds floating-point limit.s1   Weighted result array will exceed floating-point s	   limit: %e(   RC   R   R[   R$   R&   R%   R_   t   maxt   matht   sqrtt   syst
   float_infot   OverflowErrorR;   R   R<   (
   RH   t   vectort   vRD   RE   Rb   Rc   R_   t   resultt   wmax(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt   target¡   s,    
		!$
+c         C   s   |  j  j | ƒ } | S(   N(   R3   t   f(   RH   Rm   Rx   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyR_   ¹   s    c         C   s   |  j  j | ƒ } | S(   N(   R5   R{   (   RH   Rm   Rx   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRj   ½   s    c         C   s   |  j  ƒ  j d | ƒ S(   Nt   out(   Ra   t   show(   RH   R|   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt   show_summaryÁ   s    (
   t   __name__t
   __module__RY   Ra   RF   R`   Rz   R_   Rj   R~   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyR	      s   `					Ra   c           B   s2   e  Z d  e d „ Z d „  Z d „  Z d „  Z RS(   g      @c         C   s‰   t  j d d d d d d d g d d g d	 d g d t t d ƒ ƒ g d t d g  | D] } t | ƒ ^ qU ƒ |  _ | |  _ | |  _ d  S(   Nt   titles   Relative Wilson plott   column_labelss   Max. resolutions   log(I_exp/I_obs)s   Reference curves   Z-scoret   graph_namest   graph_labelss   High resolutiont    t   graph_columnsi   t   x_is_inverse_d_mint   data(   s   High resolutionR…   (   R   t
   table_dataRf   t   rangeR   t   tablet   cutoffRG   (   RH   RZ   R   RG   t   array(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRY   Å   s    					%	c         C   s4   |  j  ƒ  \ } } } } | |  j k } | j t ƒ S(   N(   t   data_as_flex_arraysRŒ   R9   R   (   RH   t   sst   rrt   iit   zzt   flagged(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt
   n_outliersÕ   s    c         C   s&   g  |  j  j D] } t j | ƒ ^ q S(   N(   R‹   Rˆ   R   R6   (   RH   t   column(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRŽ   Ú   s    c         C   s�  |  j  ƒ  \ } } } } | |  j k } | j | ƒ } | j | ƒ } | j | ƒ }	 | j | ƒ }
 | j d ƒ | j d ƒ | j |  j ƒ |  j r­ | j d |  j ƒ n | j d |  j ƒ | j	 ƒ  g  } t
 | ƒ d k rxe t | | |	 |
 ƒ D]N \ } } } } t j d | ƒ } | j d | d | d | d	 | g ƒ qù Wt j d
 d d d d g d | ƒ } | j | ƒ n | j d ƒ d  S(   Ns   Relative Wilson plotsë  The relative Wilson plot compares the mean intensity of the observed data with
the mean intensity computed from the model, as a function of resolution.  This
curve is expected to fall off at low resolution if no contribution for bulk
solvent is provided for the calculated intensities, because the presence of
bulk solvent reduces the observed intensities at low resolution by reducing
the contrast.  At high resolution, the curve should be a straight line with a
slope that reflects the difference in overall B-factor between the model and
the data.  Compared to the normal Wilson plot, the relative Wilson plot is
more linear because the influence of favored distances between atoms, caused
by bonding and secondary structure, is cancelled out.
sï   All resolution shells have Z-scores above %4.2f sigma.  This is indicative of
severe problems with the input data, including processing errors or ice rings.
We recommend checking the logs for data processing and inspecting the raw
images.
sB   All relative wilson plot outliers above %4.2f sigma are reported.
i    g      ð?s   %8.2fs   %9.3es   %5.2ft   column_headerss	   d-spacings   Obs. Log[ratio]s   Expected Log[ratio]s   Z-scoret
   table_rowss=   The Relative wilson plot doesn't indicate any serious errors.(   RŽ   RŒ   R   t   show_sub_headert	   show_textt	   show_plotR‹   RG   t   warnt   newlinet   lent   zipRq   Rr   t   appendR   t   simple_tablet
   show_tableR}   (   RH   R|   R�   R�   R‘   R’   R“   t   sel_sst   sel_zt   sel_rt   sel_it   rowsRV   Rn   t   rt   it   sssR‹   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt
   _show_implÝ   s6    	
(-				(   R   R€   t   FalseRY   R”   RŽ   Rª   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyRa   Ä   s
   		(   t
   __future__R    t   mmtbx.scalingR   t   mmtbxt   iotbxR   t   scitbx.array_familyR   t   scitbx.mathR   t   scitbxR   R   t   libtbx.utilsR   t   libtbxR   Rs   Rq   R    R!   R6   R1   R4   t   scalingt   xtriage_analysisR	   Ra   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/relative_wilson.pyt   <module>   s    ¥¥°