ó
•™xTc           @   s   d  Z  d d l m Z d d l m Z d d l m Z m Z d d l m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d d l Z d d l Z d	 Z d
 Z d e Z d e f d „  ƒ  YZ e d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e e d „ Z d S(   se   
Routines for calculating common metrics of data quality based on merging of
redundant observations.
iÿÿÿÿ(   t   division(   t
   data_plots(   t   make_sub_headert   format_value(   t   Sorryt   null_out(   t
   group_argst   Auto(   t   sqrtNsá     Diederichs K & Karplus PA (1997) Nature Structural Biology 4:269-275
    (with erratum in: Nat Struct Biol 1997 Jul;4(7):592)
  Weiss MS (2001) J Appl Cryst 34:130-135.
  Karplus PA & Diederichs K (2012) Science 336:1030-3.s�  
sigma_filtering = *auto xds scala scalepack
  .type = choice
  .short_caption = Sigma(I) filtering convention
  .help = Determines how data are filtered by SigmaI and I/SigmaI.  XDS     discards reflections whose intensity after merging is less than -3*sigma,     Scalepack uses the same cutoff before merging, and SCALA does not do any     filtering.  Reflections with negative SigmaI will always be discarded.
sˆ  
high_resolution = None
  .type = float
  .input_size = 64
low_resolution = None
  .type = float
  .input_size = 64
n_bins = 10
  .type = int
  .short_caption = Number of resolution bins
  .input_size = 64
  .style = spinner
extend_d_max_min = False
  .type = bool
  .expert_level = 2
anomalous = False
  .type = bool
  .short_caption = Keep anomalous pairs separate in merging statistics
%s
t   model_based_arraysc           B   s    e  Z d  Z d „  Z d „  Z RS(   sD  
  Container for observed and calculated intensities, along with the selections
  for work and free sets; these should be provided by mmtbx.f_model.  It is
  assumed (or hoped) that the resolution range of these arrays will be
  the same as that of the unmerged data, but the current implementation does
  not force this.
  c         C   sô   | j  ƒ  j ƒ  | j  ƒ  j ƒ  k oR | j  ƒ  j ƒ  k oR | j  ƒ  j ƒ  k n s] t ‚ t | j  ƒ  ƒ t | j  ƒ  ƒ k s‡ t ‚ | |  _ | j d |  j ƒ |  _ | j d |  j ƒ |  _ | j d |  j ƒ |  _ | j d |  j ƒ |  _	 d  S(   Nt   other(
   t   datat   sizet   AssertionErrort   lent   f_obst
   common_sett   i_obst   i_calct   work_selt   free_sel(   t   selfR   R   R   R   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   __init__=   s    &7*	c         C   sï  |  j  j | ƒ s t ‚ |  j  j d | ƒ } |  j j d | ƒ } |  j j d | ƒ } |  j j d | ƒ } |  j j d | ƒ } t | j	 ƒ  ƒ d k r¤ d g d S| j | j	 ƒ  ƒ } | j | j	 ƒ  ƒ } | j | j	 ƒ  ƒ }	 | j | j	 ƒ  ƒ }
 | j | j	 ƒ  ƒ } | j | j	 ƒ  ƒ } t | j	 ƒ  ƒ d k rät | j	 ƒ  ƒ d k räd d l m } | j | j	 ƒ  | j	 ƒ  ƒ j ƒ  } | j |	 j	 ƒ  |
 j	 ƒ  ƒ j ƒ  } | j | j ƒ  ƒ } | j |
 j ƒ  ƒ } | | | | f Sd g d S(   s   
    Given a unique array of arbitrary resolution range, extract the equivalent
    reflections from the observed and calculated intensities, and calculate
    CC and R-factor for work and free sets.  Currently, these statistics will
    be None if there are no matching reflections.
    R
   i    i   iÿÿÿÿ(   t   flexN(   R   t   is_similar_symmetryR   R   R   R   R   R   R   R   t   Nonet   selectt   scitbx.array_familyR   t   linear_correlationt   coefficientt	   r1_factort	   f_sq_as_f(   R   R
   t	   i_obs_selt	   f_obs_selt
   i_calc_selR   R   t
   i_obs_workt   i_calc_workt
   i_obs_freet   i_calc_freet
   f_obs_workt
   f_obs_freeR   t   cc_workt   cc_freet   r_workt   r_free(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   cc_work_and_freeG   s0    0(   t   __name__t
   __module__t   __doc__R   R-   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR	   5   s   	
c         C   ss   |  j  ƒ  } | t d g k ro | j d k r6 d } qo | j d k rN d } qo | j d k rf d } qo d } n  | S(   Nt   autot	   xds_asciit   xdst   ccp4_mtzt   scalat!   scalepack_no_merge_original_indext	   scalepack(   t   infoR   t   source_type(   R   t   sigma_filteringR8   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   get_filtering_conventiong   s    				t   filter_intensities_by_sigmac           B   s   e  Z d  Z e d „ Z RS(   sÅ  
  Wrapper for filtering intensities based on one of several different
  conventions:

    - in XDS, reflections where I < -3*sigmaI after merging are deleted from
      both the merged and unmerged arrays
    - in Scalepack, the filtering is done before merging
    - SCALA and AIMLESS do not do any filtering

  note that ctruncate and cctbx.french_wilson (any others?) do their own
  filtering, e.g. discarding I < -4*sigma in cctbx.french_wilson.
  c         C   s¡  t  | | ƒ } | d	 k s! t ‚ d |  _ |  _ | j d t ƒ } | j ƒ  } d  } d  |  _ | d k rã d |  _ | j	 ƒ  d | j
 ƒ  k  } | j t ƒ |  _ | j | ƒ } | j d | ƒ } | j d t ƒ } | j ƒ  } nŸ | d k rWd |  _ | j	 ƒ  d | j
 ƒ  k  } | j t ƒ |  _ | j | ƒ } | j d t ƒ } | j ƒ  } n+ | d k s‚| d  k rrn t d | ƒ ‚ | |  _ | |  _ | |  _ d  S(
   NR5   R7   R3   i    t   use_internal_varianceiýÿÿÿR
   s.   Unrecognized sigmaI filtering convention '%s'.(   s   scalas	   scalepacks   xdsN(   R;   R   R   t   n_rejected_before_merget   n_rejected_after_merget   merge_equivalentst   Falset   arrayt   observed_criterion_sigma_IR   t   sigmast   countt   TrueR   t   delete_indicest
   ValueErrort   merget   array_merged(   R   RB   R:   RI   RJ   t
   reject_selt   bad_data(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR   �   s:    			
		(   R.   R/   R0   R   R   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR<   t   s   t   merging_statsc           B   st   e  Z d  Z d d e d d d „ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z e j d	 d
 „ Z RS(   sk  
  Calculate standard merging statistics for (scaled) unmerged data.  Usually
  these statistics will consider I(+) and I(-) as observations of the same
  reflection, but these can be kept separate instead if desired.

  Reflections with negative sigmas will be discarded, and depending on the
  program we're trying to mimic, excessively negative intensities.
  R5   c         C   sÐ  d d  l  } d d l m } | j ƒ  d  k	 s4 t ‚ | j ƒ  } | j ƒ  d k }	 |	 j t ƒ |  _	 | j ƒ  d k }
 |
 j t ƒ |  j	 |  _
 | j |
 ƒ } | j ƒ  |  _ | j d | ƒ j ƒ  } | j d ƒ } t d | d | ƒ } | d  k r| j ƒ  } n  | \ |  _ |  _ | j |  _ | j } | j } | j } | j |  _ | j |  _ | j ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ | j ƒ  j  d |  j d	 |  j ƒ } |  j d k rñ| j! j" d
 | d | d |  j ƒ j  d |  j d	 |  j ƒ } n  t# | j ƒ  ƒ } | d k r2t$ d d |  j |  j f ƒ ‚ n  t% |  j | d ƒ |  _& d  |  _' | rl| j( ƒ  |  _' n  | j) ƒ  j* ƒ  } i  |  _) d |  _+ d |  _, d |  _- d |  _. d |  _/ d |  _0 d |  _1 d  |  _2 |  _3 |  _4 x0 t5 t6 | ƒ ƒ D] } | j | ƒ |  j) | <qðW|  j d k ri| j7 | j8 ƒ  ƒ |  _+ | j7 | j* ƒ  ƒ |  _, | j7 | j ƒ  ƒ |  _- | j | j ƒ  d k ƒ } | j* ƒ  | j ƒ  } | j7 | ƒ |  _. |  j, |  j- |  _/ | j2 ƒ  |  _2 | j3 ƒ  |  _3 | j4 ƒ  |  _4 | j! j9 d | ƒ |  _0 |  j0 d k rd |  _1 qid } |  j0 d k  r;d } n  | t: d t; |  j0 ƒ d |  j0 ƒ |  _1 n  d  |  _< |  _= |  _> |  _? | d  k	 rÌ|  j d k rÌ| j@ | ƒ \ |  _< |  _= |  _> |  _? n  d  S(   Niÿÿÿÿ(   R   i    t   anomalous_flagt   packed_indicesRB   R:   t   d_mint   d_maxt   crystal_symmetrys1   No reflections within specified resolution range s	   (%g - %g)g      ð?t   unmergedg      ð¿i   i   (A   t   cctbx.millerR   R   RD   R   R   t   eliminate_sys_absentRE   RA   t   n_neg_sigmast   n_zero_sigmasR   t"   half_dataset_anomalous_correlationt   anom_half_corrt   customized_copyt
   map_to_asut   sortR<   t	   d_max_minRQ   RP   RC   RB   RI   RJ   R>   R?   t   indicesR   t   n_obst   n_uniqt   complete_sett   resolution_filtert   millert	   build_setR   t   RuntimeErrort   mint   completenesst   anom_completenesst   anomalous_completenesst   redundanciesR   t   mean_redundancyt   i_meant	   sigi_meant   i_over_sigma_meant   i_mean_over_sigi_meant   cc_one_halft   cc_start   r_merget   r_meast   r_pimt   sortedt   sett   meant	   as_doublet   compute_cc_one_halfR   t   absR)   R*   R+   R,   R-   (   R   RB   R]   t   model_arrayst	   anomaloust   debugR:   t   cctbxR   t   non_negative_selt   positive_selt   filterRI   RJ   Ra   t
   n_expectedRj   t   xt   nonzero_arrayt   i_over_sigmat   mult(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR   «   s˜    				'										c         C   s   t  |  d d  ƒ S(   NRY   (   t   getattrR   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   cc_anom  s    c         C   su   d |  j  |  j |  j |  j |  j |  j d |  j |  j t d |  j	 ƒ t d |  j
 ƒ t d |  j ƒ |  j |  j f S(   NsI   %6.2f %6.2f %6d %6d   %5.2f %6.2f  %8.1f  %6.1f  %s  %s  %s  %5.3f  %5.3fid   s   %5.3f(   RQ   RP   R_   R`   Rk   Rg   Rl   Rn   R   Rr   Rs   Rt   Rp   RY   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   format
  s    
c         C   sr   d |  j  |  j |  j |  j d |  j |  j |  j t d |  j ƒ t d |  j	 ƒ t d |  j
 ƒ t d |  j ƒ f S(   Ns?   %6.2f  %6.2f  %6d  %6.2f  %6.2f  %5.3f  %5.3f   %s   %s  %s  %sid   s   %5.3f(   RQ   RP   R`   Rg   Rn   Rp   Rq   R   R)   R*   R+   R,   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   format_for_model_cc  s    c         C   sy   d |  j  |  j f t |  j ƒ t |  j ƒ d |  j d |  j d d |  j d |  j d |  j	 d |  j
 d |  j g
 S(   Ns   %.2f - %.2fs   %.1fs   %.1f %%id   s   %.3f(   RQ   RP   t   strR_   R`   Rk   Rg   Rn   Rr   Rs   Rt   Rp   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   format_for_gui"  s    




c         C   s‹   d |  j  |  j f t |  j ƒ d |  j d d |  j d |  j d |  j t d |  j	 ƒ t d |  j
 ƒ t d |  j ƒ t d |  j ƒ g
 S(   Ns   %.2f - %.2fs   %.1f %%id   s   %.1fs   %.3fs   %5.3f(   RQ   RP   R‹   R`   Rg   Rn   Rp   Rq   R   R)   R*   R+   R,   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   format_for_cc_star_gui.  s    


c         C   s›   d |  j  d |  j |  j |  j |  j d |  j |  j |  j |  j |  j	 |  j
 |  j g } |  j d  k	 r— | j |  j |  j |  j |  j |  j g ƒ n  | S(   Ni   i   id   (   RP   R_   R`   Rk   Rg   Rl   Rn   Rr   Rs   Rt   Rp   RY   R)   R   t   extendRq   R*   R+   R,   (   R   t   table(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt
   table_data:  s     t    c         C   sX  | | d |  j  |  j f IJ| | d |  j IJ| | d |  j IJ| | d |  j IJ| | d |  j d IJ| | d |  j IJ| | d |  j IJ|  j d	 k rÇ | | d
 |  j IJn  |  j	 d	 k rî | | d |  j	 IJn  |  j
 d	 k r| | d |  j
 IJn  | | d |  j IJ| | d |  j IJ| | d |  j IJd  S(   Ns   Resolution: %.2f - %.2fs   Observations: %ds   Unique reflections: %ds   Redundancy: %.1fs   Completeness: %.2f%%id   s   Mean intensity: %.1fs   Mean I/sigma(I): %.1fi    s$   SigI < 0 (rejected): %d observationss'   I < -3*SigI (rejected): %d observationss&   I < -3*SigI (rejected): %d reflectionss   R-merge: %5.3fs   R-meas:  %5.3fs   R-pim:   %5.3f(   RQ   RP   R_   R`   Rk   Rg   Rl   Rn   RV   R>   R?   Rr   Rs   Rt   (   R   t   outt   prefix(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show_summaryD  s&    


N(   R.   R/   R0   R   RA   R   t   propertyRˆ   R‰   RŠ   RŒ   R�   R�   t   syst   stdoutR”   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyRM   ¢   s   U					
t   dataset_statisticsc           B   sò   e  Z d  Z d d d e d e d d e d e d d „ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 d d „ Z d e d „ Z d d	 „ Z d
 „  Z d d „ Z d d „ Z d d d „ Z d d e j e j d d d „ Z e j d d „ Z RS(   s—   
  Container for overall and by-shell merging statistics, plus a table_data
  object suitable for displaying graphs (or outputting loggraph format).
  i
   g�íµ ÷Æ°>c         C   s@  | |  _  | d  k r! t ƒ  } n  | j ƒ  d  k	 s9 t ‚ | j ƒ  } t | |
 ƒ }
 | d  k r‡ | j ƒ  d  k	 sx t ‚ | j ƒ  } n  | |  _ | j	 d | ƒ j
 | ƒ } | j ƒ  rÚ t d d | j ƒ  j ƒ  ƒ ‚ n  | } | } | d  k	 r$| d | 9} | d  k	 r$| | k s!t ‚ q$n  | d  k	 rA| d | 9} n  | j d | d | ƒ j
 | ƒ } | j ƒ  d k rƒt d ƒ ‚ n  | j d	 | ƒ d
 |  _ | sÉ| j	 d t ƒ j
 | ƒ } d |  _ n  d  } | r | j d | d | d | ƒ | | f } n | j d | ƒ | j d t ƒ } t | d | d |	 d | d | d |
 ƒ|  _ g  |  _ d } d d d d d d d d d d d d  g } d! d d d d d" d d  g } d d d# g d d$ g d d% g d d& g d d' g d d( d) d* g d d+ g d d, g g } |	 d  k	 rid- } | j d. d/ d0 d1 d2 g ƒ | j d. d3 g ƒ | j d d, d4 d5 g d d6 d7 g g ƒ n  t j d8 | d9 | d: | d; | d< t d= t ƒ |  _ d  } x— | j ƒ  j ƒ  D]ƒ } | j ƒ  j | ƒ } t | j  | ƒ d | j ƒ  j! | ƒ d |	 d | d | d |
 ƒ} |  j j" | ƒ |  j j# | j ƒ  ƒ qµWd  S(>   NRR   s7   The data in %s are already merged.  Only unmerged (but s)   scaled) data may be used in this program.i   RP   RQ   i    s6   No reflections left after applying resolution cutoffs.t   fR‘   RN   s    (non-anomalous)t   n_binsR=   R]   R{   R|   R}   R:   s   Intensity merging statisticss   1/d**2s   N(obs)s	   N(unique)t
   Redundancyt   Completenesss   Mean(I)s   Mean(I/sigma)s   R-merges   R-meass   R-pims   CC1/2s   CC(anom)s   Reflection countss	   R-factorsi   i   i   i   i   i   i   i	   i
   i   s.   Model quality and intensity merging statisticss   CC*s   CC(work)s   CC(free)s   R-works   R-frees   Model R-factorsi   i   i   i   t   titlet   column_labelst   graph_namest   graph_columnst   x_is_inverse_d_mint   force_exact_x_labels($   t	   file_nameR   R   RD   R   R8   R;   t   space_groupRR   RZ   t   set_infot   is_unique_set_under_symmetryR   t   label_stringRb   R   R”   t
   anom_extraRA   t   setup_binnerR@   RM   t   overallt   binsRŽ   R   R�   RF   R�   t   binnert
   range_usedt	   selectionR   t   bin_d_ranget   appendt   add_row(   R   R   RR   RP   RQ   R|   Rš   R}   R£   R{   R:   t   d_min_tolerancet   extend_d_max_mint   logR8   t   d_min_cutofft   d_max_cutofft   overall_d_max_minRI   R�   Rž   RŸ   R    t   last_bint   bint   sele_unmergedt	   bin_stats(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR   a  s     	 									W+		c         C   sé   d d d d d d d g } d d d d d g } d	 d
 d g d	 d g d	 d g d	 d g d	 d g g } t  j d d d | d | d | d d d d d d d d g d t d t ƒ } x1 |  j D]& } | j ƒ  } | j | d	 d !ƒ q» W| S(   Ns   1/d**2s   N(obs)s	   N(unique)R›   Rœ   s   Mean(I)s   Mean(I/sigma)s   Reflection countsi    i   i   i   i   i   i   R�   s3   Statistics for redundancy, completeness, and signalRž   RŸ   R    t   column_formatss   %6.2fs   %6ds   %5.2fs   %8.1fs   %6.1fR¡   R¢   i   (   R   R�   RF   R«   R±   (   R   Rž   RŸ   R    R�   R¹   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   signal_tableÂ  s"    	6		c         C   sÙ   d d d d d d g } d d d	 d
 g d d g d d g g } d d d g } t  j d d d | d d d d d d d g d | d | d t d t ƒ } x< |  j D]1 } | j ƒ  } | j | d g | d d !ƒ q  W| S(   Ns   1/d**2s   R-merges   R-meass   R-pims   CC1/2s   CC(anom)i    i   i   i   i   i   s	   R-factorsR�   s"   Statistics for dataset consistencyRž   R¼   s   %6.2fs   %5.3fRŸ   R    R¡   R¢   i   i   (   R   R�   RF   R«   R±   (   R   Rž   R    RŸ   R�   R¹   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   quality_tableÖ  s     	'		#c         C   s·   d d g } d d g g } d g } t  j d d d d d g d d	 d
 g d d g d d d g g d t d t ƒ } x> |  j D]3 } | j ƒ  } | j d | j d | j g ƒ q| W| S(   Ns   1/d**2s   CC(anom)i    i   R�   s"   Half-dataset anomalous correlationRž   R¼   s   %6.2fs   %5.3fRŸ   R    R¡   R¢   i   (   R   R�   RF   R«   R±   RP   RY   (   R   Rž   R    RŸ   R�   R¹   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   cc_anom_tableé  s    				%c         C   sC   | d  k r t j } n  | d IJ| |  j j ƒ  IJ| d IJd  S(   NR‘   (   R   R–   R—   R�   t   format_loggraph(   R   R’   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show_loggraphû  s
     
c         C   sò   | d  k r t j } n  | r1 t d d | ƒn  |  j j | ƒ | d IJ| d |  j IJt |  j j j	 ƒ  ƒ } x, | D]$ } | d | |  j j | f IJq{ W| d IJ| d IJx! |  j
 D] } | | j ƒ  IJqÁ W| |  j j ƒ  IJd  S(   Ns   Merging statisticsR’   R‘   s   Redundancies%s:s	     %d : %ds€     Statistics by resolution bin:
 d_max  d_min   #obs  #uniq   mult.  %comp       <I>  <I/sI>  r_mrg r_meas  r_pim  cc1/2  cc_ano(   R   R–   R—   R   Rª   R”   R¨   Ru   Rj   t   keysR«   R‰   (   R   R’   t   headerR_   Rƒ   R»   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show  s      
"
c         C   sa   t  d d | ƒ| d IJx- t |  j ƒ D] \ } } | | j ƒ  IJq* W| |  j j ƒ  IJd  S(   Ns   CC* and related statisticsR’   sR    d_max   d_min  n_uniq  compl. <I/sI>  cc_1/2    cc* cc_work cc_free r_work r_free(   R   t	   enumerateR«   RŠ   Rª   (   R   R’   t   kR¹   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show_cc_star  s    c         C   sw   |  j  d } t d | j | j f d | j d | j d | j d | j d | j d | j	 d	 | j
 d
 | j d | j ƒ 
S(   sr   
    For compatibility with iotbx.logfiles (which should probably now be
    deprecated) and phenix.table_one
    iÿÿÿÿR]   t   n_reflt
   n_refl_allRg   t   multiplicityt   r_symRs   Rp   Rq   R…   (   R«   R   RQ   RP   R`   R_   Rg   Rk   Rr   Rs   Rp   Rq   Rn   (   R   t   shell(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   extract_outer_shell_stats  s    								c         C   sØ  d d  l  } | d  k r- | j j j ƒ  } n  |  j j } | d  k rN d } n  |  j j | d <|  j j | d <|  j j	 d | d <|  j j
 | d <|  j j | d <|  j j | d	 <|  j j | d
 <|  j j | d <|  j j | d <|  j j | d <|  j j | d <|  j j | d <|  j j | d <| | d <d | d <| j j j d d" ƒ } xv |  j D]k } | j | j | j | j
 | j | j | j	 d | j | j | j | j | j | j | j | j f ƒ qXW| j | ƒ | S(#   Niÿÿÿÿt   ?s   _reflns.d_resolution_lows   _reflns.d_resolution_highid   s   _reflns.percent_possible_obss    _reflns.pdbx_number_measured_alls   _reflns.number_obss   _reflns.pdbx_redundancys   _reflns.phenix_mean_Is   _reflns.pdbx_netI_over_sigmaIs   _reflns.pdbx_Rmerge_I_obss   _reflns.pdbx_Rrim_I_obss   _reflns.pdbx_Rpim_I_obss   _reflns.phenix_cc_stars   _reflns.phenix_cc_1/2s"   _reflns.observed_criterion_sigma_Is"   _reflns.observed_criterion_sigma_FRÃ   s   _reflns_shell.d_res_highs   _reflns_shell.d_res_lows!   _reflns_shell.number_measured_obss   _reflns_shell.number_unique_obss   _reflns_shell.pdbx_redundancys"   _reflns_shell.percent_possible_obss   _reflns_shell.phenix_mean_Is'   _reflns_shell.pdbx_netI_over_sigmaI_obss!   _reflns_shell.meanI_over_sigI_obss   _reflns_shell.Rmerge_I_obss   _reflns_shell.pdbx_Rrim_I_obss   _reflns_shell.pdbx_Rpim_I_obss   _reflns_shell.phenix_cc_stars   _reflns_shell.phenix_cc_1/2(   s   _reflns_shell.d_res_highs   _reflns_shell.d_res_lows!   _reflns_shell.number_measured_obss   _reflns_shell.number_unique_obss   _reflns_shell.pdbx_redundancys"   _reflns_shell.percent_possible_obss   _reflns_shell.phenix_mean_Is'   _reflns_shell.pdbx_netI_over_sigmaI_obss!   _reflns_shell.meanI_over_sigI_obss   _reflns_shell.Rmerge_I_obss   _reflns_shell.pdbx_Rrim_I_obss   _reflns_shell.pdbx_Rpim_I_obss   _reflns_shell.phenix_cc_stars   _reflns_shell.phenix_cc_1/2(   t   iotbx.cif.modelR   t   cift   modelt   blockRª   RC   RQ   RP   Rg   R_   R`   Rk   Rl   Rn   Rr   Rs   Rt   Rq   Rp   t   loopR«   R±   Ro   t   add_loop(   R   t	   cif_blockt   iotbxRC   t   reflns_shell_loopR»   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   as_cif_block-  sl    	

             	
c         C   s¥  d d l  m } d } } | d  k	 r† t j ƒ  } | | d d d d | ƒrs | | d d d d | ƒrs d	 } n d
 } d | } n  g  } | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d | ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ t d  |  j j d! ƒ } t d  |  j j	 ƒ } t d" |  j j
 ƒ }	 t d |  j j ƒ }
 | j d# | ƒ | j d$ | ƒ | j d% |	 ƒ | j d& ƒ | j d' |
 ƒ | j d ƒ | j d( ƒ |  j d } t d) | j ƒ } t d) | j ƒ } t d  | j d! ƒ } t d  | j	 ƒ } t d" | j
 ƒ } t d | j ƒ } | j d* | ƒ | j d+ | ƒ | j d, | ƒ | j d- | ƒ | j d. | ƒ | j d/ ƒ | j d0 | ƒ | j d ƒ g  | D] } d1 | ^ q‚} d2 j | ƒ S(3   Niÿÿÿÿ(   t   approx_equalt   NULLg—�z6«ø?t   epsg{®Gáz„?R’   gçŒ(í¾æ?t   Yt   Ns   %.4fR‘   s   EXPERIMENTAL DETAILSs3    EXPERIMENT TYPE                : X-RAY DIFFRACTIONs&    DATE OF DATA COLLECTION        : NULLs&    TEMPERATURE           (KELVIN) : NULLs&    PH                             : NULLs&    NUMBER OF CRYSTALS USED        : NULLs&    SYNCHROTRON              (Y/N) : NULLs&    RADIATION SOURCE               : NULLs&    BEAMLINE                       : NULLs&    X-RAY GENERATOR MODEL          : NULLs#    MONOCHROMATIC OR LAUE    (M/L) : Ms$    WAVELENGTH OR RANGE        (A) : %ss&    MONOCHROMATOR                  : NULLs&    OPTICS                         : NULLs&    DETECTOR TYPE                  : NULLs&    DETECTOR MANUFACTURER          : NULLs&    INTENSITY-INTEGRATION SOFTWARE : NULLs&    DATA SCALING SOFTWARE          : NULLs   OVERALL.s   %.1fid   s   %.5fs%    COMPLETENESS FOR RANGE     (%%) : %ss$    DATA REDUNDANCY                : %ss$    R MERGE                    (I) : %ss&    R SYM                      (I) : NULLs$    <I/SIGMA(I)> FOR THE DATA SET  : %ss    IN THE HIGHEST RESOLUTION SHELL.s   %.2fs.    HIGHEST RESOLUTION SHELL, RANGE HIGH (A) : %ss.    HIGHEST RESOLUTION SHELL, RANGE LOW  (A) : %ss%    COMPLETENESS FOR SHELL     (%%) : %ss$    DATA REDUNDANCY IN SHELL       : %ss$    R MERGE FOR SHELL          (I) : %ss&    R SYM FOR SHELL            (I) : NULLs$    <I/SIGMA(I)> FOR SHELL         : %ss   REMARK 200 %ss   
(   t   libtbx.test_utilsRÙ   R   t	   cStringIOt   StringIOR°   R   Rª   Rg   Rk   Rr   Rn   R«   RQ   RP   t   join(   R   t
   wavelengthRÙ   t   synchrotront   wlR’   t   linest   comp_overallt   mult_overallt   rmerg_overallt   s2n_overallR»   RQ   RP   t   comp_lastbint   mult_lastbint   rmerg_lastbint   s2n_lastbint   linet   remark_lines(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   as_remark_200i  sz    
	R‘   c         C   sË  |  j  j d  k	 s t ‚ | d  k r0 t j } n  |  j d } | | d IJ| | d |  j  j |  j  j | j | j f IJ| | d |  j  j	 | j	 f IJ| | d |  j  j
 | j
 f IJ| | d |  j  j | j f IJ| | d |  j  j | j f IJ| | d |  j  j | j f IJ| | d	 |  j  j | j f IJ| | d
 |  j  j | j f IJ| | d |  j  j | j f IJ|  j  j d  k	 r¹| | d |  j  j | j f IJn | | d IJd  S(   Niÿÿÿÿs   Merging statistics and CC*:s-     Resolution      : %.3f - %.3f (%.3f - %.3f)s      Mean(I/sigmaI)  : %6.3f (%.3f)s"     Redundancy      :  %4.2f  (%.2f)s!     R-merge         :  %5.3f (%.3f)s!     R-meas          :  %5.3f (%.3f)s!     R-pim           :  %5.3f (%.3f)s!     CC1/2           :  %5.3f (%.3f)s!     CC*             :  %5.3f (%.3f)s!     CC(work)        :  %6.4f (%.4f)s!     CC(free)        :  %6.4f (%.4f)s"     CC(free)        :  not available(   Rª   R)   R   R   R–   R—   R«   RQ   RP   Rn   Rk   Rr   Rs   Rt   Rp   Rq   R*   (   R   R’   R“   t   outer_shell(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show_model_vs_dataª  s8     









i    iÿÿÿÿc   
      C   sû   | | | | | | g j  d ƒ d k r+ d S| d k rD | d } n  d } d } x� |  j D]‚ }	 |	 j | k  sÒ |	 j | k  sÒ | d k	 r™ |	 j | k sÒ | d k	 r´ |	 j | k sÒ |	 j | k  sÒ |	 j | k  rÖ Pn  |	 } qZ W| d k rð d S| j	 Sd S(   sî  
    Determine approximate resolution cutoffs based on a variety of metrics.
    Numbers are assumed to be fractional, not percentage values, except for
    the completeness which will be treated as a percent if the cutoff is
    greater than 1.

    :param min_i_over_sigma: minimum Mean(I/sigmaI) for outer shell
    :param min_cc_one_half: minimum CC1/2 for outer shell
    :param max_r_merge: maximum R-merge for outer shell
    :param max_r_meas: maximum R-meas for outer shell
    :param min_cc_anom: minimum CC(anom) for outer shell
    :param min_completeness: minimum completeness for outer shell
    :returns: Python float representing d_min for the outermost acceptable
      resolution bin, or None if no bins meet the given criteria
    i   i   g      Y@N(
   RE   R   R«   Rn   Rp   Rr   Rs   Rˆ   Rg   RP   (
   R   t   min_i_over_sigmat   min_cc_one_halft   max_r_merget
   max_r_meast   min_cc_anomt   min_completenessRP   R¸   R¹   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   estimate_d_minÈ  s&    
c         C   su  | d IJ| d IJd „  } t  d d | ƒ| | d |  j j IJ|  j d d ƒ } |  j d d	 ƒ } |  j d
 d ƒ } |  j d d ƒ } |  j d d ƒ } |  j d d ƒ }	 |  j d d ƒ }
 | | d | | ƒ IJ| | d | | ƒ IJ| | d | | ƒ IJ| | d | | ƒ IJ| | d | |	 ƒ IJ| | d | |
 ƒ IJ| d IJ| d IJ| d IJd  S(   NR‘   c         S   s   |  d  k r d Sd |  S(   Ns   (use all data)s   %7.3f(   R   (   t   value(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   format_d_minö  s    s   Resolution cutoff estimatesR’   s)     resolution of all data          : %7.3fRô   g…ëQ¸Õ?Ró   g       @Rõ   g      à?Rö   R÷   g333333Ó?Rø   gÍÌÌÌÌÌì?s&     based on CC(1/2) >= 0.33        : %ss&     based on mean(I/sigma) >= 2.0   : %ss&     based on R-merge < 0.5          : %ss&     based on R-meas < 0.5           : %ss'     based on completeness >= 90%%    : %ss'     based on completeness >= 50%%    : %ss:   NOTE: we recommend using all data out to the CC(1/2) limits   for refinement.(   R   Rª   RP   Rù   (   R   R’   R“   Rû   t   cc_one_half_cutt   i_over_sigma_cutt   r_merge_cutt
   r_meas_cutt   cc_anom_cutt   completeness_cut_conservativet   completeness_cut_permissive(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   show_estimated_cutoffsó  s8    

	








N(   R.   R/   R0   R   RA   R   R   R•   R½   R¾   R¿   RÁ   RF   RÄ   RÇ   RÍ   RØ   Rð   Rò   R–   t   maxintRù   R—   R  (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyR˜   \  s<   T	<A%c         C   s*  | d  k r t ƒ  } n  d d l m } | j |  ƒ } | d I| j ƒ  IJ| j d t d | ƒ } | j ƒ  d k rœ d |  k rœ | d  k rœ | d IJn  d  } g  }	 xR | D]J }
 |
 j ƒ  j	 ƒ  } | | k rÝ |
 } Pq¯ |
 j
 ƒ  r¯ |	 j |
 ƒ q¯ q¯ W| j ƒ  d k s| rl| d  k rlt |	 ƒ d	 k rlx3 | D]( }
 |
 j ƒ  r:|	 j |
 j ƒ  ƒ q:q:Wqln  | d  k rût |	 ƒ d	 k r�t d
 |  ƒ ‚ qût |	 ƒ d k rît d d j g  |	 D] } d | j ƒ  j	 ƒ  ^ qÂƒ ƒ ‚ qû|	 d	 } n  | j
 ƒ  s&t d | j ƒ  j	 ƒ  ƒ ‚ n  | S(   Niÿÿÿÿ(   t   reflection_file_readers   Format:R@   t    assume_shelx_observation_type_ist
   shelx_hklft   =s5   WARNING: SHELX file is assumed to contain intensitiesi    s   No intensities found in %s.i   s2   Multiple intensity arrays - please specify one:
%ss   
s     labels=%ss   %s is not an intensity array.(   R   R   RÖ   R  t   any_reflection_filet	   file_typet   as_miller_arraysRA   R8   R§   t   is_xray_intensity_arrayR°   R   t   is_xray_amplitude_arrayt	   f_as_f_sqR   Rá   (   R£   t   data_labelsR´   R  t   allow_amplitudesR  t   hkl_int   miller_arraysR   t	   all_i_obsRB   t   labelst   a(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   select_data  sD     	 9(   R0   t
   __future__R    RÖ   R   t   libtbx.str_utilsR   R   t   libtbx.utilsR   R   t   libtbxR   R   t   mathR   Rß   R–   t   citations_strt   sigma_filtering_phil_strt   merging_params_strt   objectR	   R;   R<   RM   R˜   R   R  (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/merging_statistics.pyt   <module>   s&   

2.ºÿ ¹