ó
Ò™xTc           @   s)  d  d l  m Z d  d l m Z d  d l Z d  d l m Z d  d l m Z m	 Z	 d  d l
 m Z m Z d  d l Z d  d l Z d  d l
 m Z d  d l m Z m Z d  d	 l
 m Z d  d
 l m Z d  d l m Z d  d l Z d  d l
 m Z d  d l m Z e	 d ƒ Z d a d d „ Z d „  Z  d d „ Z! d d „ Z" d d „ Z# d „  Z$ i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d d! 6d" d# 6d$ d$ 6d% d% 6d& d' 6Z% d( e& f d) „  ƒ  YZ' d* e f d+ „  ƒ  YZ( d, e& f d- „  ƒ  YZ) d. e& f d/ „  ƒ  YZ* d0 e& f d1 „  ƒ  YZ+ d S(2   iÿÿÿÿ(   t   division(   t   flexN(   t   matrix(   t   format_cpu_timest   getenv_bool(   t   adopt_init_argst   slots_getstate_setstate(   t	   str_utils(   t   prefix_each_line_suffixt   format_value(   t   introspection(   t   math(   t   xray(   t   easy_pickle(   t   countt/   MMTBX_PRINT_STATISTICS_ENABLE_SHOW_PROCESS_INFOg        c         C   s?   |  d  k r t j }  n  t } | d k r; |  d t IJn  | S(   Ng{®Gáz„?s1   Collect and process                      = %-7.2f(   t   Nonet   syst   stdoutt   time_collect_and_process(   t   outt   total(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt
   show_times   s     c         C   si   |  d d IJt  j ƒ  j d |  d t ƒ t j j j d |  ƒ |  t ƒ  IJ|  d d IJ|  j	 ƒ  d  S(   Ns   \/i'   R   t   show_maxs   /\(
   R
   t   virtual_memory_infot   show_if_availablet   TrueR   t   structure_factorst   global_counterst   showR   t   flush(   R   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   show_process_info   s    c         C   sK   | d  k r t j } n  t r. t d | ƒ n  t j |  d | d d ƒd  S(   NR   t
   header_leniP   (   R   R   R   t   enable_show_process_infoR   R   t   make_header(   t   lineR   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR"   %   s
     c         C   s5   | d  k r t j } n  t j |  d | d d ƒd  S(   NR   R    iP   (   R   R   R   R   t   make_sub_header(   t   textR   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR$   +   s     c         C   sý   | d  k r t j } n  d } t |  ƒ }  t | ƒ } t |  ƒ } t | ƒ } t d |  d | ƒ d } | | } | d } | }	 | }
 | d | k r§ |	 d 7}	 n  d d |
 d d |  d } | d d |	 d } | | } | | IJ| j ƒ  d  S(	   NiP   s    REFINEMENT MACRO_CYCLE s    OF i   i   s   
t   *t    (   R   R   R   t   strt   lenR   (   t   macro_cyclet   number_of_macro_cyclesR   R    t   macro_cycle_strt   number_of_macro_cycles_strt   line_lent   fill_lent   fill_rlt   fill_rt   fill_lt   str1t   str2t
   out_string(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   macro_cycle_header/   s(     

 

c      	   C   s)  | d k s t  ‚ t t j j | d ƒ } |  t d | | d d | d | ƒ IJxÆ t t d ƒ | | ƒ D]¬ \ } } }	 t | ƒ } | j	 ƒ  t
 t j j d	 | | Œ  ƒ j d
 t ƒ ƒ }
 t
 t j |	 ƒ ƒ } |  | | d t | g | |
 g t |	 ƒ | g ƒ | j ƒ  IJqk W|  j ƒ  d  S(   Nt   xyzt   zyzt   _matrixt   prefixt   lines_as_one_stringsš                               rotation (deg)                 translation (A)   
                         %s              total           xyz          total t   suffixi   t   rt   degs=    group %4d: %8.3f %8.3f %8.3f %7.2f  %6.2f %6.2f %6.2f %6.2f (   s   xyzR8   (   t   AssertionErrort   getattrt   scitbxR   t   euler_anglesR   t   zipR   t   listt   reverset   abst&   r3_rotation_axis_and_angle_from_matrixt   angleR   R   t   colt   tuplet   rstripR   (   R   R:   t   framet   euler_angle_conventiont	   rotationst   translationst   euler_angles_as_matrixt   iR=   t   tt   r_totalt   t_total(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt*   show_rigid_body_rotations_and_translationsD   s     

(
Et   bsst   solt   iont   rbrt   rsrlt   realsrlt   rsrgt   realsrgt   dent   SAt   tardyt   sacartR7   t   xyzrect   adpt   occt   anomt   fp_fdpt   refinement_monitorc        	   B   s†   e  Z d  d d d d d d d d g	 Z d d d e d	 „ Z d
 „  Z d d d „ Z d d „ Z d d d „ Z	 d „  Z
 d d „ Z RS(   t   stepst   r_workst   r_freest   geomt   bs_iso_max_at   bs_iso_min_at   bs_iso_ave_at   n_solvt   shiftsc         C   s•   t  |  t ƒ  ƒ |  j d  k r. t j |  _ n  d  |  _ d  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  |  _ x! |  j D] } t |  | g  ƒ qw Wd  S(   N(   R   t   localsR   R   R   R   t   wilson_bt
   bond_startt   angle_startt
   bond_finalt   angle_finalt   rigid_body_shift_accumulatort   sites_cart_startt
   __arrays__t   setattr(   t   selft   paramsR   t   neutron_refinementt   call_back_handlert   is_neutron_monitort   name(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   __init__~   s     							c         C   sA   i  } x$ |  j  D] } t |  | ƒ | | <q Wt j | | ƒ d  S(   N(   Ry   R@   R   t   dump(   R{   t	   file_namet   statsR€   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   dump_statistics‘   s    c         C   sm  t  j  ƒ  } |  j d  k r0 | j j ƒ  |  _ n  | j j ƒ  } | j ƒ  |  j j ƒ  k rŒ |  j j t j	 t j
 |  j | j ƒ  ƒ ƒ ƒ n |  j j d ƒ | d  k	 r´ | |  _ n  |  j j | ƒ |  j j | j ƒ  ƒ |  j j | j ƒ  ƒ t } t |  j d ƒ r|  j j j } n  t }	 |  j s/| r8t }	 n  t }
 t |  j d ƒ rb|  j j j }
 n  d  } |
 r®d d l m } | j | j | j j ƒ  | j j ƒ  |	 ƒ } n  | j d |	 d |  j j  j! d | ƒ } | d  k	 rô|  j" j | ƒ n  d  } |  j r |  j# r | j j ƒ  } n  | j j$ ƒ  t% j& d	 d
 } | d  k	 r]| j' | ƒ } n  |  j( j t j) | d ƒ ƒ |  j* j t j+ | d ƒ ƒ |  j, j t j- | d ƒ ƒ |  j. j | j/ ƒ  ƒ t0 |  j" ƒ d k r!t1 |  j" d d d  ƒ r!t1 |  j" d d d  ƒ r!|  j2 |  j3 g j4 d  ƒ d	 k rxt0 |  j" ƒ d k rx|  j" d j5 d	 |  _2 |  j" d j6 d	 |  _3 qxn  t0 |  j" ƒ d k rØ|  j" t0 |  j" ƒ d j5 d	 |  _7 |  j" t0 |  j" ƒ d j6 d	 |  _8 q!t0 |  j" ƒ d k r!|  j" d j5 d	 |  _7 |  j" d j6 d	 |  _8 q!n  | d  k	 r9| |  _9 n  t  j  ƒ  } t: | | 7a: |  j; | | d | ƒd  S(   Ns   n/at   ambert   afittiÿÿÿÿ(   R‡   t	   ignore_hdt   cdl_restraintst   general_selectioni   i   i    t   bt   ai   t   method(<   t   timeRx   R   t   xray_structuret
   sites_cartt   sizeRp   t   appendR   t   meant   sqrtt   dotRr   Rh   Ri   t   r_workRj   t   r_freet   Falset   hasattrR|   R†   t	   use_amberR   R}   R‡   t	   use_afittt   mmtbx.geometry_restraintst   get_non_afitt_selectiont   restraints_managert   hd_selectiont   geometry_statisticst   pdb_interpretationt   cdlRk   R   t   extract_u_iso_or_u_equivR   t   pit   selectRl   t   max_defaultRm   t   min_defaultRn   t   mean_defaultRo   t#   number_of_ordered_solvent_moleculesR)   R@   Rs   Rt   R   R‹   RŒ   Ru   Rv   Rw   R   t	   call_back(   R{   t   modelt   fmodelt   stepRr   Rw   t   t1t   sites_cart_currRš   Rˆ   R›   RŠ   R‡   Rk   t   hd_selt   b_isost   t2(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   collect—   s€    	) 			  !$'t   monitor_collectc         C   s;   |  j  d  k	 r7 t |  j  ƒ r7 |  j  |  | | | ƒ n  d  S(   N(   R~   R   t   callable(   R{   R«   R¬   R�   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyRª   ç   s    t    c         C   sÒ  t  j  ƒ  } t g  |  j D] } t | ƒ ^ q ƒ } | d  k rL |  j } n  d d } |  j d  k	 r³ | | d IJt d | d | d d d |  j j d	 |  j j	 d
 |  j j
 ƒ n  | | d IJ| | d IJ| | d IJ|  j j j rý | | d IJn  d |  j j j k r#| | d IJn  d |  j j j k rI| | d IJn  |  j j j ri| | d IJn  |  j j j r‰| | d IJn  d |  j j j k r¯| | d IJn  d |  j j j k rÕ| | d IJn  d |  j j j k rû| | d IJn  | | | IJt } t |  j ƒ r@|  j d j d  k r@t } q@n  | ro| | d IJ| d d | f d } n& | | d  IJ| d d | f d! } xt |  j |  j |  j |  j |  j |  j |  j |  j |  j ƒ	 D]Ã \	 }	 }
 } } } } } } } t d" ƒ t | ƒ k r%d# t d$ | ƒ } n
 d% | } | rr| | |	 |
 | | j  d& | j d& | | | | | f
 IJqÕ| | |	 |
 | | | | | | f IJqÕW| | | IJ| j! ƒ  t  j  ƒ  } t" | | 7a" d  S('   Nt   -iH   s<   Information about total rigid body shift of selected groups:R   R:   RL   R'   RM   RN   RO   sH   ****************** REFINEMENT STATISTICS STEP BY STEP ******************sH   leading digit, like 1_, means number of macro-cycle                     sH   0    : statistics at the very beginning when nothing is done yet        sH   1_bss: bulk solvent correction and/or (anisotropic) scaling             t   individual_sitessH   1_xyz: refinement of coordinates                                        t   individual_adpsH   1_adp: refinement of ADPs (Atomic Displacement Parameters)              sH   1_sar: simulated annealing refinement of x,y,z                          sH   1_wat: ordered solvent update (add / remove)                            t
   rigid_bodysH   1_rbr: rigid body refinement                                            t	   group_adpsH   1_gbr: group B-factor refinement                                        t   occupanciessH   1_occ: refinement of occupancies                                        i    sG    stage       r-work r-free bonds angles b_min b_max b_ave n_water shifts   %s%dst   %s1    %6.4f %6.4f %5.3f %5.2f %5.1f %5.1f %5.1f %3d %ss:    stage       r-work r-free b_min b_max b_ave n_water shifts%    %6.4f %6.4f %5.1f %5.1f %5.1f %3d %sg      ð?s        s   %5.3fs   %9si   (#   RŽ   t   maxRh   R)   R   R   Rw   RU   RM   RN   RO   R|   t   maint   bulk_solvent_and_scalet   refinet   strategyt   simulated_annealingt   ordered_solventR   Rk   RŒ   R˜   RC   Ri   Rj   Rm   Rl   Rn   Ro   Rp   t   typeR(   R‹   R   R   (   R{   R   t   remarkR®   t   st   max_step_lent	   separatort   has_bonds_anglest   formatRŒ   R‹   t   ct   dt   et   ft   gt   hRQ   R²   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR   ë   s„    ( 

+ 
=*
c         C   s¤  g  } g  } g  } g  } g  } xUt  |  j ƒ D]D\ } } | j d d ƒ } | j d ƒ } t | ƒ d k  r} | j | ƒ nV | d }	 d j | d ƒ }
 t j |
 d  ƒ } | d  k r¾ q. n  | j |	 d | ƒ | j |  j
 | ƒ | j |  j | ƒ |  j d  k	 rXt |  j ƒ d k rX| j |  j | j d ƒ | j |  j | j d ƒ q. | j d  ƒ | j d  ƒ q. Wt d | d | d	 | d
 | d | d |  j ƒ S(   Nt   :R¶   t   _i   i    i   Rh   Ri   Rj   t   as_avet   bs_avet   neutron_flag(   t	   enumerateRh   t   replacet   splitR)   R’   t   joint   show_actionst   getR   Ri   Rj   Rk   RŒ   R‹   t   stats_tableR   (   R{   Rh   Ri   Rj   RÔ   RÕ   t   i_stept   labelt   fieldst   cyclet   actiont   action_label(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   format_stats_for_phenix_gui4  s<    
 $c      
   C   s  t  |  j ƒ d k r d  S|  j d j d d ƒ } | j d ƒ } d  } t  | ƒ d k r© | d } d j | d ƒ } t j | d  ƒ } | d  k r˜ d  S| d | } n  | d  k rÜ t  |  j ƒ d k rÓ d } qÜ d	 } n  | d
 | | t d |  j	 d ƒ t d |  j
 d ƒ f IJd  S(   Ni    iÿÿÿÿRÒ   R¶   RÓ   i   i   t   startt   ends   %s%-6s  r_work=%s  r_free=%ss   %.4f(   R)   Rh   RØ   RÙ   R   RÚ   RÛ   RÜ   R	   Ri   Rj   (   R{   R   R:   t	   last_stepRà   Rã   Rá   Râ   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   show_current_r_factors_summaryU  s(    
		
N(   t   __name__t
   __module__Ry   R   R˜   R�   R…   R³   Rª   R   Rä   Rè   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyRg   q   s(   			LI	!RÝ   c           B   s)   e  Z d  d d d d d g Z d „  Z RS(   Rh   Ri   Rj   RÔ   RÕ   RÖ   c         K   s1   x* |  j  D] } t |  | | j | ƒ ƒ q
 Wd  S(   N(   t	   __slots__Rz   RÜ   (   R{   t   kwdst   attr(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR�   u  s    (   Ré   Rê   Rë   R�   (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyRÝ   l  s   	t   coordinate_shiftsc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c   
      C   s×   d d l  m } | j ƒ  |  _ |  j j ƒ  } i  } x3 | j ƒ  D]% } | j ƒ  j ƒ  } | j | | <qA Wd „  } x] t | ƒ D]O \ }	 } | j ƒ  j ƒ  } | | k rÆ | | | | j ƒ | _	 q€ d | _	 q€ Wd  S(   Niÿÿÿÿ(   R   c         S   sK   |  \ } } } | \ } } } t  j | | d | | d | | d ƒ S(   Ni   (   R   R”   (   t   xyz1t   xyz2t   x1t   y1t   z1t   x2t   y2t   z2(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   get_distanceƒ  s    g      ð¿(
   t   scitbx.array_familyR   t	   deep_copyt   hierarchy_shiftedt   atomst   fetch_labelst   id_strR7   R×   R‹   (
   R{   t   hierarchy_startt   hierarchy_endR   t   atoms_shiftedt   coords_startt   atomRý   R÷   t   i_seq(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR�   {  s    	c         C   s   |  j  j ƒ  j ƒ  S(   N(   Rú   Rû   t	   extract_b(   R{   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt
   get_shiftsŽ  s    c         C   s4   |  j  j ƒ  j ƒ  } | j | d k ƒ } | j ƒ  S(   Ni    (   Rú   Rû   R  R¥   t   min_max_mean(   R{   Rp   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR  ‘  s    c         C   s3   t  | d ƒ } | j |  j j ƒ  ƒ | j ƒ  d  S(   Nt   w(   t   opent   writeRú   t   as_pdb_stringt   close(   R{   Rƒ   RÏ   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   save_pdb_file–  s    (   Ré   Rê   R�   R  R  R  (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyRî   z  s   			t   trajectory_outputc           B   s2   e  Z d  Z d e e j e d „ Z d d „ Z RS(   sè   
  Callback object for saving the intermediate results of refinement as a stack
  of PDB and MTZ files.  Equivalent to the interactivity with Coot in the
  Phenix GUI, but intended for command-line use and demonstrative purposes.
  RÁ   c         C   s   t  |  t ƒ  ƒ d |  _ d  S(   Ni    (   R   Rq   t   _i_trajectory(   R{   t	   file_baset   filled_mapst   logt   verbose(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR�   ¡  s    R´   c         C   sò   d d  l  } |  j d 7_ d |  j |  j f } | j d t ƒ } | j d d d |  j ƒ } | j d d ƒ }	 | j j d	 | d
 |	 d | d ƒ t	 | d d ƒ }
 |
 j
 | j d | j ƒ ƒ |
 j ƒ  |  j d | IJ|  j d | IJd  S(   Niÿÿÿÿi   s
   %s_traj_%dt   sync_with_xray_structuret   map_types   2mFo-DFct   fill_missings   mFo-DFct
   fwt_coeffst   delfwt_coeffsRƒ   s   .mtzs   .pdbR  t   crystal_symmetrys   wrote model to %s.pdbs    wrote map coefficients to %s.mtz(   t   iotbx.map_toolsR  R  t   pdb_hierarchyR   t   map_coefficientsR  t	   map_toolst   write_map_coeffsR  R	  R
  R�   R  R  (   R{   t   monitorR«   R¬   R�   t   iotbxR  R  t   two_fofc_map_coeffst   fofc_map_coeffsRÏ   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   __call__¦  s"    
(   Ré   Rê   t   __doc__R   R   R   R�   R"  (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR  ›  s   t   annealing_callbackc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   R«   R  (   R{   R«   R  (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR�   º  s    	c         C   s&   |  j  j d |  j d | d d ƒ d  S(   NR«   R¬   R�   t   anneal(   R  Rª   R«   (   R{   R¬   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR"  ¾  s    (   Ré   Rê   R�   R"  (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyR$  ¹  s   	(,   t
   __future__R    t   cctbx.array_familyR   t   scitbx.math.euler_anglesRA   R   t   libtbx.utilsR   R   t   libtbxR   R   R   RŽ   R   t   libtbx.str_utilsR   R	   R
   t   stdlibR   t   cctbxR   t,   cctbx.xray.structure_factors.global_countersR   t	   itertoolsR   R!   R   R   R   R   R"   R$   R6   RU   RÛ   t   objectRg   RÝ   Rî   R  R$  (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/print_statistics.pyt   <module>   sT   			
û!