ó
‚™xTc           @   s2  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  d	 l m Z d  d
 l m Z d  d l m Z d  d l m Z d  d l Z d „  Z d e f d „  ƒ  YZ d „  Z d g  d „ Z e d k r.e d e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   reflection_file_reader(   t   option_parser(   t   maptbx(   t   miller(   t   crystal(   t   sgtbxN(   t   reindex(   t   flex(   t   Sorry(   t   countc         C   s¯   t  |  d j j d ƒ ƒ } d | d Gd Gx{ |  D]s } t j ƒ  } x4 | j d d !D]" } | d  k	 rV | j | ƒ qV qV W| j ƒ  d k r• d	 Gq3 d t j	 | ƒ Gq3 WHd  S(
   Ni    t    i	   s   average:i   iÿÿÿÿi   s   %7.4fs          (
   t   lent   binnert
   bin_legendR   t   doublet   datat   Nonet   appendt   sizet   mean(   t   binned_data_listt   lt   binned_dataR   t   d(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   show_average_of_binned_data   s      t   array_cachec           B   s�   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d d „ Z	 d d	 „ Z
 d d
 „ Z d „  Z d „  Z e d d d d „ Z RS(   c         C   sI  | j  d t d t j ƒ |  _ | |  _ |  j j ƒ  sÃ d GH|  j j ƒ  } | j d d ƒ H| j	 ƒ  |  _ ~ | j
 ƒ  d  k	 r¤ |  j j | j
 ƒ  j d t ƒ ƒ qÃ |  j j t j d t ƒ ƒ n  |  j j ƒ  H|  j j d | ƒ |  j j ƒ  |  _ |  j j ƒ  |  _ |  j j d |  j ƒ j ƒ  j ƒ  |  _ |  j j ƒ  rtt |  j j ƒ  ƒ j d |  j ƒ j ƒ  j ƒ  |  _ n	 d  |  _ t j j |  j d |  j ƒ|  _ |  j j d	 |  j j ƒ  ƒ |  _  t! j" j# |  j j$ ƒ  d
 |  j ƒ|  _% |  j% j& t! j' d ƒ ƒ |  j% j( ƒ  t j d |  j j$ ƒ  d t! j) d |  j% ƒ d t* ƒ |  _" d  S(   Nt   integral_onlyt   logs(   Merging symmetry-equivalent reflections:t   prefixs     t   mergedt   n_binst   cb_opt   anomalous_flagt	   max_deltai    t	   unit_cellt   space_group_infot   groupt   assert_is_compatible_unit_cell(   i    i    i    (+   t   eliminate_sys_absentt   Truet   syst   stdoutt   inputt   lattice_symmetry_max_deltat   is_unique_set_under_symmetryt   merge_equivalentst   show_summaryt   arrayt   infoR   t   set_infot   customized_copyR   t
   array_infot   show_comprehensive_summaryt   setup_binnert   resolution_ranget"   change_of_basis_op_to_minimum_cellt   change_basist   expand_to_p1t
   map_to_asut   observationsR!   t   abst   anomalous_differencest
   anom_diffsR   t   symmetryt   minimum_cell_symmetryt   reflection_intensity_symmetryt   intensity_symmetryR   t   lattice_symmetryR%   R#   t   lattice_groupt
   expand_invt   tr_vect	   make_tidyR$   t   False(   t   selfR+   R   R,   R   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   __init__   sJ    	%					c         C   sR   d t  |  j j ƒ  ƒ GH|  j j ƒ  } | j |  j ƒ | j d t ƒ j ƒ  Hd  S(   Ns   Completeness of %s:t   use_binning(   t   strR+   R1   R'   t   use_binning_oft   completenessR(   t   show(   RJ   t
   no_sys_abs(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   show_completenessN   s
    c         C   s"   |  j  j ƒ  j d |  j j ƒ  ƒ S(   NR    (   RD   R#   R9   R8   t   inverse(   RJ   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   idealized_input_unit_cellU   s    c         C   sX   t  j j d |  j d |  j j ƒ  j ƒ  j ƒ  ƒ } | j d |  j	 j
 ƒ  d t d t ƒ S(   Nt   gt   hR    t   omit_first_partitiont   omit_negative_determinants(   R   t   cosetst$   left_decomposition_point_groups_onlyRE   RC   t   space_groupt   build_derived_acentric_groupRH   t   best_partition_representativesR8   RS   R(   (   RJ   RY   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   possible_twin_lawsY   s    		c   
      C   sá  d G|  j  j ƒ  j ƒ  GHd G|  j j ƒ  j ƒ  GHd |  j } | d k rV | d 7} n
 | d 7} d GHd G| GH|  j ƒ  } t | ƒ d	 k r” d
 GHnH|  j ƒ  } t | ƒ } | t |  j	 j
 ƒ  ƒ k rÓ d G| GHn  d GHxí | D]å } | j ƒ  j ƒ  } t j | ƒ } | j d | ƒ j | ƒ s't ‚ d } y |  j	 j ƒ  j d | ƒ } Wn: t k
 rˆ}	 t |	 ƒ j d ƒ d	 k  r‚  n  d } n/ X| j ƒ  |  j	 j ƒ  k r·d t | ƒ } n  d G| | GHqß Wd GHd GHd GHd GHHd  S(   Ns   Space group of the intensities:s   Space group of the metric:     s   %.6gt   1s    degrees    degreess,     Tolerance used in the determination of thes     lattice symmetry:i    s   Possible twin laws: Nones   Idealized unit cell:s   Possible twin laws:R    t    s   Unsuitable value for rationals:    # Info: this changes the setting of the input space groups    # Info: new setting: %sR   s     Note:s;       phenix.xtriage provides comprehensive twinning analysiss-       facilities for macromolecular structures.s5       For more information enter: phenix.xtriage --help(   RC   R$   t   as_reference_settingRD   R,   R^   R   RT   RM   R+   R#   t   rt   as_hklR   t   change_of_basis_opR9   t   is_similar_tot   AssertionErrort   RuntimeErrort   findR%   R[   (
   RJ   R   t	   twin_lawst   idealized_cellt   st   hkl_strR    R1   t   cb_sgt   e(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   show_possible_twin_lawsd   sL     
		 
gš™™™™™¹?i   c      	   C   s_  d t  |  j j ƒ  ƒ GH|  j } | j ƒ  r< | j ƒ  } n  | j d t j ƒ } | j ƒ  | j	 ƒ  j
 d | j ƒ  d t j ƒ  ƒ } | j ƒ  d } t j d | j ƒ  d | j ƒ  d d ƒ } d	 GHx“ t | j ƒ  ƒ D] } | j ƒ  | }	 |	 | | k  r| | k rPn  | j ƒ  | }
 t j | |
 ƒ } d
 | j ƒ  |
 Gd |	 | j ƒ  f GHq× WHd  S(   Ns   Patterson peaks for %s:t   symmetry_flagst   mapt
   parametersi    R#   R[   t   original_sites=         Fractional coordinates     Height  Distance from origins     %8.4f %8.4f %8.4fs     %8.3f  %8.3f(   i    i    i    (   RM   R+   R1   R!   t   average_bijvoet_matest   patterson_mapR   t   use_space_group_symmetryt   apply_sigma_scalingt   tagst   peak_searcht   real_mapt   peak_search_parameterst   heightsR   t   sym_equiv_sitesR#   R[   t   xrangeR   t   sitest   min_sym_equiv_distance_infot   sym_opt   dist(   RJ   t   min_relative_peak_heightt   show_at_leastt   reciprocal_mapRu   t	   peak_listt
   max_heightt   sym_equiv_origint   i_peakt   heightt   sitet	   dist_info(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   show_patterson_peaksŽ   s4    		
		 c   	      C   s·  |  j  j ƒ  j ƒ  s t ‚ d t |  j  j ƒ  ƒ GH|  j  j ƒ  j ƒ  } | j d | j	 ƒ  | j
 ƒ  j	 ƒ  j ƒ  ƒ } | j ƒ  | d  k	 r  | j d | ƒ n; | j d t ƒ | j ƒ  j ƒ  d k rÛ | j d d ƒ n  | j ƒ  j d t ƒ | j d t ƒ } | j d t ƒ } | j j GH| j j GHd GHxo t t ƒ  | j	 | j	 ƒ D]R \ } } } | j j | ƒ Gx/ | | f D]! } | d  k r‘d Gqxd | GqxWHqLWt | | g ƒ Hd  S(   Ns(   Perfect merohedral twinning test for %s:R   R   t   auto_binningi   t   include_centricRL   sB   See also: http://www.doe-mbi.ucla.edu/Services/Twinning/intro.htmlR   i   s   %7.4fs          (   R+   R[   t
   is_centricRf   RM   R1   t   select_acentrict   as_intensity_arrayR0   R   t   epsilonst	   as_doublet#   set_observation_type_xray_intensityR   R6   R(   R   t   n_bins_usedt   counts_completeRI   t   second_moment_of_intensitiest   wilson_ratiot   __doc__t   zipR
   R   R   (	   RJ   R   t   acentrict   smt   wrt   i_binRk   t   wt   v(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt%   show_perfect_merohedral_twinning_test«   s4    	%
+ c         C   sü   d t  |  j j ƒ  ƒ GH|  j j ƒ  } | j d | j ƒ  | j ƒ  j ƒ  j ƒ  ƒ } | j ƒ  | d  k	 r~ | j
 d | ƒ n; | j
 d t ƒ | j ƒ  j ƒ  d k r¹ | j
 d d ƒ n  | j j j ƒ  d GH| j d t ƒ } | j ƒ  t | g ƒ Hd  S(   Ns%   Second moments of intensities for %s:R   R   RŽ   i   i    RL   (   RM   R+   R1   R’   R0   R   R“   R”   R•   R   R6   R(   R   R–   R˜   Rš   t   splitRP   R   (   RJ   R   t   fR�   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt"   show_second_moments_of_intensitiesÇ   s    .

c         C   s½   |  j  j ƒ  d  k	 r¹ |  j  j |  j  j ƒ  d k ƒ } | j ƒ  d k rµ d t |  j  j ƒ  ƒ GH|  j  j j j	 d t | ƒ ƒ GH| j
 |  j  ƒ | j d t d | ƒ } | j ƒ  n  Hn  d  S(   Ni    s   Observed measurabilities of %s:t   cutoffRL   (   R+   t   sigmasR   t   selectR   RM   R1   t   measurabilityRš   t   replaceRN   R(   RP   (   RJ   R¦   t
   work_arrayt   meas_obs(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   show_measurabilityØ   s    ! c         C   s¬   t  j d |  j j ƒ  d |  j j ƒ  ƒ } t  j d | j j ƒ  d | j j ƒ  ƒ } | j |  j ƒ } | j | j ƒ } t j | | | | |  j j	 ƒ  ƒ } | j
 ƒ  } | S(   NR#   R[   (   R   R@   R+   R#   R[   R9   R8   R   t   reindexing_operatorsR!   t   combined_cb_ops(   RJ   t   othert   relative_length_tolerancet   absolute_angle_tolerancet   self_xst   other_xst   double_cosetst   result(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   unique_reindexing_operatorsã   s     c         C   s   |  j  } | j  } | j | ƒ } d  } d  } xÚ |  j j ƒ  D]É } t j t j | j ƒ  ƒ ƒ j | ƒ } x™ | j j ƒ  D]ˆ } t j t j | j ƒ  ƒ ƒ j | ƒ } | j	 ƒ  | | | | }	 |	 j
 ƒ  }
 | d  k só t j | |
 ƒ d k rz |	 } |
 } qz qz Wq= W| d  k	 st ‚ | S(   Ni    (   R8   t   new_denominatorsR   RA   R[   R   Rd   t   rt_mxRb   RS   Rc   t   compare_cb_op_as_hklRf   (   RJ   R°   R    t   sct   oct   best_choicet   best_choice_as_hklt   s_symopt   o_symopt   possible_choicet   possible_choice_as_hkl(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   combined_cb_opû   s(    		i    g�íµ ÷Æ°>c         C   sü   t  |  j d | j d ƒ } t |  j d | j d ƒ } | | k re | | d 7} | | d 8} n | d | 9} | d | 9} |  j j d | d | d | d | d | ƒ |  j d  k	 rø | j d  k	 rø |  j j d | d | d | d | d | ƒ n  d  S(	   Ni    i   g      à?t   d_maxt   d_minRŽ   t   reflections_per_binR   (   t   minR7   t   maxR<   R6   R?   R   (   RJ   R°   RŽ   RÆ   R   t   d_toleranceRÄ   RÅ   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   setup_common_binner  s(    N(   t   __name__t
   __module__RK   RR   RT   R^   Ro   R�   R   R¢   R¥   R­   R·   RÃ   RI   RÊ   (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyR      s    	/				+		c         C   s¹  d } x¬| D]¤} | j  ƒ  } | j | j ƒ  d k ƒ } | j ƒ  j ƒ  d k rX q n  | j ƒ  r} | j ƒ  } | d 7} n | j ƒ  r˜ t | ƒ } n  | j ƒ  r | j	 ƒ  d  k rï Hd d GHd G| j  ƒ  GHd GHd d GHH|  j j ƒ  d S| j ƒ  d  k r:Hd d GHd G| j  ƒ  GHd	 GHd d GHH|  j j ƒ  d S|  j j d  k	 s^|  j j d  k	 r…| j d
 |  j j d |  j j ƒ } n  | j ƒ  } | j d | ƒ | j | ƒ q q W| S(   Ni    i   t   *iO   s   Unknown unit cell parameters:s2   Use --symmetry or --unit_cell to define unit cell:iÿÿÿÿs   Unknown space group:s6   Use --symmetry or --space_group to define space group:RÄ   RÅ   R1   (   i    i    i    (   R1   R¨   t   indicesR   t   is_xray_intensity_arrayt	   f_sq_as_ft   is_complex_arrayR=   t   is_real_arrayR#   R   t   parsert	   show_helpR$   t   optionst
   resolutiont   low_resolutiont   resolution_filterR;   R2   R   (   t   command_linet   input_miller_arrayst   active_miller_arrayst   n_f_sq_as_ft   miller_arrayR1   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   _process_miller_arrays.  sP     					s   phenix.reflection_statisticsc          C   sF  d Gx |  D]
 } | Gq WHHt  d | d d d | ƒ j ƒ  j d  d d d d	 t d
 d ƒj d  d d d d
 d ƒj ƒ  j d  d d d d d d d d	 d d
 d d d ƒj d  d d d d d d d d	 d  d
 d d d ƒj d  d d d d d d d d	 d  d
 d d d ƒj d  d d d d d d	 d  d
 d! ƒj d" |  ƒ } t | j ƒ d# k rrt | ƒ d# k rr| j	 j
 ƒ  d  Sg  } d# } xþ | j D]ó } t j d$ | ƒ } d  }	 | j ƒ  d  k	 ry, | j d% | j d& | j j d' t ƒ }	 Wqt k
 rü}
 ‚  qt k
 rqXn  |	 d  k rFt j d( I| IJt j Jt j j ƒ  qˆt d) | d* |	 d+ | ƒ } | d# k  rqd  S| | 7} qˆW| d  k	 rÃt d) | d* | d+ | ƒ } | d# k  r¶d  S| | 7} n  | d# k rì| d, k rãd- GHn d. GHHn  t | ƒ d/ k rb| j j rbd0 GHx1 t | ƒ D]# \ } } d1 | d, G| j ƒ  GHqWHd2 GHd3 GHd4 GHd5 GHd6 GHHn  | j j } g  } xËt | ƒ D]½\ } } d7 G| d, GHHt d8 | d | d9 | j j ƒ } | j ƒ  | j ƒ  | j  ƒ  | j! j" ƒ  j# ƒ  s| j$ d | j j% ƒ n | j& d | j j' ƒ | j! j( ƒ  r€d: t) | j! j ƒ  ƒ GH| j! j* j+ GH| j! j* d; t, ƒ } | j- ƒ  H| j. ƒ  n  x•t | ƒ D]‡\ } } | j/ d< | d= d> d? d@ ƒ } t | ƒ d# k rdA GHd1 | d, G| j! j ƒ  GHd1 | d, G| j! j ƒ  GHdB GHHq�t0 j1 ƒ  } xN | D]F } | j2 j3 | ƒ j4 ƒ  } | j5 | j2 j6 d< | dC t ƒ j7 ƒ  ƒ qWt0 j8 | dD t, ƒ} | j9 | ƒ } t0 j9 | dE | ƒ} xpt: t; ƒ  | | ƒ D]Y\ } } } | j< d< | dF | ƒ } | j= ƒ  j> ƒ  sdG | d, | j? ƒ  f } n dH } dI | d, | d, | | j? ƒ  f GHdJ GHd1 | d, G| j! j ƒ  GHd1 | d, G| j! j ƒ  GHdK | | f GHt } | d# k s°| dL k r| | d# dM k rt, } | j2 j3 | ƒ j4 ƒ  } | j@ | d | ƒ| j2 j6 d< | d; t, dC t ƒ } | j- ƒ  n  H| jA d  k	 r·| jA d  k	 r·| jA j3 | ƒ j4 ƒ  } | jA j6 d< | dC t ƒ } dN | d, | d, | j7 ƒ  | j? ƒ  f GHdO GHd1 | d, G| j! j ƒ  GHd1 | d, G| j! j ƒ  GHdP | j7 ƒ  | f GH| r| jA j6 d< | d; t, dC t ƒ } | j- ƒ  n  Hq·q·Wq�W| j j s4| j5 | ƒ n  dQ dR GHHq�Wd  S(S   Ns   Command line arguments:t   usages     [options] reflection_file [...]t   descriptions   Example: %s data1.mtz data2.scas   --weak_symmetryt   actiont
   store_truet   defaultt   helps?   symmetry on command line is weaker than symmetry found in filess   --quicks6   Do not compute statistics between pairs of data arrayss   --binst   storet   typet   intt   destR   i
   s   Number of binst   metavart   INTs   --bins_twinning_testt   n_bins_twinning_tests    Number of bins for twinning tests   --bins_second_momentst   n_bins_second_momentss0   Number of bins for second moments of intensitiess   --lattice_symmetry_max_deltat   floatg      @sN   angular tolerance in degrees used in the determination of the lattice symmetryt   argsi    t	   file_namet   crystal_symmetryt   force_symmetryR.   s   Warning: unknown file format:RÙ   RÚ   RÛ   i   s?   Note: Intensity array has been converted to an amplitude array.s?   Note: Intensity arrays have been converted to amplitude arrays.i   s$   Array indices (for quick searching):s     %2d:s   Useful search patterns are:s       Summary is       CC Obs i js       CC Ano i js&     i and j are the indices shown above.t   SummaryR+   R,   s   Anomalous signal of %s:RL   R°   R±   gš™™™™™©?R²   i   s   Incompatible unit cells:s   No comparison.t   assert_is_similar_symmetryt   reverset   permutationR    s     after reindexing %d using %sR`   s   CC Obs %d %d %6.3f  %ss   Correlation of:s   Overall correlation: %6.3f%sg333333Ó?gš™™™™™É?s   CC Ano %d %d %6.3f  %ss$   Anomalous difference correlation of:s1   Overall anomalous difference correlation: %6.3f%st   =iO   (B   R   t   enable_symmetry_comprehensivet   optionR   RI   t   enable_resolutionst   processR   Rî   RÓ   RÔ   R   t   any_reflection_filet	   file_typet   as_miller_arraysR@   RÕ   t   weak_symmetryR	   t	   ExceptionR)   t   stderrt   flushRÞ   t   quickt	   enumerateR1   R   R   R,   Ro   RR   R�   R+   R[   R�   R¢   Rë   R¥   Rì   R!   RM   t   anomalous_signalRš   R(   RP   R­   R·   R   R   R<   R9   R;   R   t   correlationt   coefficientt   sort_permutationR¨   R›   R
   RÃ   t   ct
   is_unit_mxRc   RÊ   R?   (    Rî   t   command_namet   additional_miller_arrayst   argRÙ   RÛ   RÜ   Rï   t   reflection_filet   miller_arrayst   KeyboardInterruptt   nt   i_0t   input_0R   t   array_cachest   cache_0t   anom_signalt   i_1t   cache_1R·   t   ccsR    t   similar_array_0Rõ   t   i_cb_opt   ccRÃ   t   reindexing_notet   show_in_binsR  t   similar_anom_diffs_0(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   runZ  s‚    
											'				  	 	 


		
			 ,				&		t   __main__Rî   i   (   t
   __future__R    t   iotbxR   t   iotbx.option_parserR   t   cctbxR   R   R   R   t   cctbx.sgtbx.lattice_symmetryt   cctbx.sgtbx.cosetst   cctbx.crystalR   t   cctbx.array_familyR   t   libtbx.utilsR	   t	   itertoolsR
   R)   R   t   objectR   RÞ   R  RË   t   argv(    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/reflection_statistics.pyt   <module>   s*   	ÿ 	.É