ó
µ™xTc           @   sØ   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	 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 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   utils(   t   simulated_annealing(   t   tardy(   t   easy_mpt   Auto(   t   print_statistics(   t   adp_refinement(   t	   reference(   t   flex(   t   StringIONt   managerc           B   sY   e  Z d d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 d „ Z RS(	   c      	   C   sa  | d  k r t j } n  | |  _ | j j |  _ |  j t k rK d |  _ n  | j j |  _ | |  _	 | |  _
 | |  _ | |  _ | |  _ d  |  _ | |  _ d |  _ | j j |  _ | j j j j } t j d d |  j	 ƒ|  j j d t ƒ }	 t | j ƒ d k rRt j d d |  j	 ƒ| j d |	 ƒ | j ƒ  | j d |  j j  j! ƒ  ƒ n  | j j" rz| j" |  j j  j! ƒ  ƒ n  t j d	 d |  j	 ƒ|  j# d
 |  j j j$ ƒ | j% ƒ  j  j& ƒ  j' ƒ  |  _( t | j j j j) _ | j j* r| j+ ƒ  }
 | d | j j IJn | j j, | j j- f g }
 g  } g  } d | j j. k rC|  j	 d IJ| j j* r|  j t k s{|  j d k rt/ j0 d | j j d |  j1 d |
 d d ƒ } xˆ | D]M \ } } | d  k rât2 d d d | ƒ ‚ n  | j3 | ƒ | j3 | ƒ q¯Wn0 x- |
 D]% } |  j1 d | ƒ } | j3 | ƒ q
W|  j4 | ƒ nd | j j. k rT|  j	 d IJ| j j* r|  j t k sŒ|  j d k rt/ j0 d | j j d |  j5 d |
 d d ƒ } xˆ | D]M \ } } | d  k rót2 d d d | ƒ ‚ n  | j3 | ƒ | j3 | ƒ qÀWn0 x- |
 D]% } |  j5 d | ƒ } | j3 | ƒ qW|  j6 | ƒ n d ‚ d } d  } d  } d  } d  } d  } xg t7 | ƒ D]Y \ } } | d } | | k  r‹| } | d } | d } | d } | d } | } q‹q‹W| d  k	 sút8 ‚ | j j* rq|  j	 d | IJ|  j	 d | IJ| j j rqt | ƒ | d k rn|  j	 d  IJ|  j	 | | IJqnqqn  | j% ƒ  j  j9 | j: ƒ  ƒ | j; d! | j% ƒ  j  d" t ƒ t< j= d# | j% ƒ  j  d$ | j  ƒ | j j j j j> d% | ƒ |  j d  k	 rH|  j j? d& |  j j j d |  j j  j! ƒ  d |  j j d t ƒ d' |  j	 ƒ n  t@ | j j j j) _ d  S((   Ni   i    s   DEN refinementt   outt   sync_with_xray_structures   DEN restraint neworkt   pdb_hierarchyt
   sites_carts(   coordinate minimization before annealingt   ca_onlys4   Running DEN torsion optimization on %d processors...t   torsions#   Running torsion simulated annealingt	   processest
   fixed_funct   argst   func_wrappert   buffer_stdout_stderrs   DEN weight optimization failed:s   
%s
This is a s5   serious error; please contact bugs@phenix-online.org.t	   grid_pairt	   cartesians%   Running Cartesian simulated annealings   error in DEN annealing typeg      ð?i   i   i   s   
best gamma: %.1fs   best weight: %.1f
s   
Best annealing results:
t   xray_structuret   update_f_calct   x1t   x2t   eq_distancest   geometryt   log(A   t   Nonet   syst   stdoutt   ncs_managert   maint   nprocR   t   dent   verboseR   t   fmodelst   modelt   paramst   target_weightst   adp_refinement_managert   macro_cyclet   tan_b_iso_maxt   random_seedt   restraints_managerR   t   generic_restraints_managert   den_managerR   t   make_headerR   t   Truet   lent   den_proxiest   make_sub_headert   build_den_proxiest   build_den_restraintst   show_den_summaryR   R   t   output_kinemaget   minimizet   minimize_c_alpha_onlyt   fmodel_xrayt   deep_copy_scattererst
   scattererst   save_scatterers_localt   flagst   optimizet   get_optimization_gridt   gammat   weightt   annealing_typeR   t   pool_mapt   try_den_weight_torsiont   RuntimeErrort   appendt   show_den_opt_summary_torsiont   try_den_weight_cartesiant   show_den_opt_summary_cartesiant	   enumeratet   AssertionErrort   replace_scattererst	   deep_copyt   update_xray_structureR   t   assert_xray_structures_equalt   import_eq_distancest   update_dihedral_ncs_restraintst   False(   t   selfR(   R)   R*   R+   R-   R#   R   R2   R   t   gridt   grid_resultst   grid_sot   stdout_and_resultst   sot   rR   t   resultt
   low_r_freet   best_xray_structuret   best_eq_distancest
   best_gammat   best_weightt	   best_so_it   it
   cur_r_free(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyt   __init__   sú    										
											




		
c         C   sÁ  t  |  j | d ƒ } t j d | ƒ t j | ƒ |  j j ƒ  j j	 |  j
 j ƒ  ƒ |  j j d |  j j ƒ  j d t ƒ t j d |  j j ƒ  j d |  j j ƒ | d } | d } | |  j j j j j _ | |  j j j j j _ d } | d |  j j j j j _ |  j j j j j j } |  j j j rZ|  j t k rZ|  j d k rZt j } nH |  j j j r™|  j d k r™|  j  r�|  j! } q¢t" ƒ  } n	 |  j! } |  j! d | | f IJx¤| | k  r_| d	 | d IJ|  j j ƒ  j# ƒ  } | d
 | IJ| d |  j j$ j% j& IJt$ j' d |  j d |  j d |  j( d |  j j$ d | d t d t) ƒ |  j j j* r”|  j* d | ƒ |  j j |  j j ƒ  _ n  |  j j j+ r¶|  j, d | ƒ n  |  j- d  k	 r|  j- j/ d |  j j j d |  j j j0 ƒ  d |  j j1 d t ƒ d | ƒ n  | d 7} |  j j j j j j d 7_ |  j j ƒ  j# ƒ  } | d | IJq¼W|  j j ƒ  j# ƒ  } |  j j ƒ  j j2 ƒ  j3 ƒ  }	 |  j j j j j j4 ƒ  }
 | | | |	 |
 f S(   Ni   t   valueR   R   R   R   i    s#     ...trying gamma %.1f, weight %.1fs   DEN cycle %ds    rfree at start of SA cycle: %.4fs   k_rep = %.2fR(   R)   R+   R*   R   t   format_for_phenix_refinet   call_back_after_stepR   R   R   R   s   rfree at end of SA cycle: %f(5   t   intR/   R	   t   set_random_seedt   randomt   seedR(   R>   R   RQ   RA   RR   RS   R4   R   RT   R)   R0   R   R1   R2   RE   RF   t   current_cyclet
   num_cyclesR*   R&   RC   R%   R   R!   R"   R'   R   R
   t   r_freeR   t!   prolsq_repulsion_function_changest   k_rept   runR+   RW   t   bulk_solvent_and_scalet
   refine_adpR   R#   R    RV   R   R   R?   R@   t   get_current_eq_distances(   RX   R   t
   local_seedt   gamma_localt   weight_localt   cyclet   num_den_cyclest	   local_logRr   t   step_xray_structuret   step_eq_distances(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyRI       sˆ    	

		
				

c         C   s¡  t  |  j | d ƒ } t j d | ƒ t j | ƒ |  j j ƒ  j j	 |  j
 j ƒ  ƒ |  j j d |  j j ƒ  j d t ƒ t j d |  j j ƒ  j d |  j j ƒ | d } | d } | |  j j j j j _ | |  j j j j j _ d } | d |  j j j j j _ |  j j j j j j } |  j j j rZ|  j t k rZ|  j d k rZt j } nH |  j j j r™|  j d k r™|  j  r�|  j! } q¢t" ƒ  } n	 |  j! } |  j! d | | f IJx„| | k  r?| d	 | d IJ|  j j ƒ  j# ƒ  } | d
 | IJt$ j% d |  j j$ d |  j& d |  j' d |  j j( d |  j d |  j d |  j d | ƒ |  j j j) rt|  j) d | ƒ n  |  j j j* r–|  j+ d | ƒ n  |  j, d  k	 rñ|  j, j. d |  j j j d |  j j j/ ƒ  d |  j j0 d t ƒ d | ƒ n  | d 7} |  j j j j j j d 7_ |  j j ƒ  j# ƒ  } | d | IJq¼W|  j j ƒ  j# ƒ  } |  j j ƒ  j j1 ƒ  j2 ƒ  }	 |  j j j j j j3 ƒ  }
 | | | |	 |
 f S(   Ni   Ri   R   R   R   R   i    s     ...trying gamma %f, weight %fs   DEN cycle %ss   rfree at start of SA cycle: %fR*   R+   R-   t   h_paramsR(   R)   t
   all_paramsR   R   R   R   R   R   s   rfree at end of SA cycle: %f(4   Rl   R/   R	   Rm   Rn   Ro   R(   R>   R   RQ   RA   RR   RS   R4   R   RT   R)   R0   R   R1   R2   RE   RF   Rp   Rq   R*   R&   RC   R%   R   R!   R"   R'   R   R
   Rr   R   R   R+   R-   t	   hydrogensRv   Rw   R   R#   R    RV   R   R   R?   R@   Rx   (   RX   R   Ry   Rz   R{   R|   R}   R~   Rr   R   R€   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyRM   û   s„    	

		
						

c         C   s¿   t  j d d |  j ƒ|  j d d IJ|  j d d IJxo | D]g } | d  k rj t d t t ƒ ƒ ‚ n  | d } | d	 } | d
 } |  j d | | | f d IJq? W|  j d d IJd  S(   Ns'   DEN torsion weight optimization resultsR   s(   |---------------------------------------s'   --------------------------------------|s(   |  Gamma    Weight    R-free            s'                                         |s   Parallel DEN job failed: %si    i   i   s'   | %6.1f    %6.1f    %6.4f              s%                                       |(   R   R3   R   R    RJ   t   strR   (   RX   RZ   R_   t	   cur_gammat
   cur_weightRg   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyRL   J  s&    






c         C   s¿   t  j d d |  j ƒ|  j d d IJ|  j d d IJxo | D]g } | d  k rj t d t t ƒ ƒ ‚ n  | d } | d	 } | d
 } |  j d | | | f d IJq? W|  j d d IJd  S(   Ns)   DEN Cartesian weight optimization resultsR   s(   |---------------------------------------s'   --------------------------------------|s(   |  Gamma    Weight    R-free            s'                                         |s   Parallel DEN job failed: %si    i   i   s'   | %6.1f    %6.1f    %6.4f              s%                                       |(   R   R3   R   R    RJ   R„   R   (   RX   RZ   R_   R…   R†   Rg   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyRN   _  s&    






c         C   sD   |  j  j d t d |  j j d |  j j j d t d d d | ƒ d  S(   Nt   update_f_part1R*   t   optimize_maskt   force_update_f_maskR%   i   R   (   R(   t   update_all_scalesR4   R*   Rv   R$   Rˆ   (   RX   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyRv   t  s    c      )   C   sY  | d  k r t j } n  |  j j ƒ  j j ƒ  j ƒ  } |  j j	 ƒ  |  j j
 d |  j j d t ƒ t j d |  j d |  j d |  j j j d |  j j j d |  j j d |  j j j d	 |  j d
 |  j j d |  j j d |  j j d |  j j j d |  j j j d |  j j j d |  j d |  j j d |  j d |  j d | d |  j j d d ƒ |  _ d  S(   NR   R   R(   R)   t   group_adp_selectionst   group_adp_selections_ht   group_adp_paramst   tls_selectionsR‚   t
   tls_paramst   individual_adp_paramst   adp_restraints_paramst   refine_adp_individualt   refine_adp_groupt
   refine_tlsR.   R0   R-   R+   R   R�   R%   i   (   R    R!   R"   R(   R>   R   R?   R@   R)   t!   reset_adp_of_hd_sites_to_be_equalRS   R4   R   R   t   refinement_flagst	   adp_groupt   group_hR*   t   group_b_isot   adp_tlst   tlst   adpt   adp_restraintst   individual_adpt	   group_adpR.   R0   R-   R+   Rƒ   R,   (   RX   R   t   save_xray_structure(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyR   }  s8    							c         C   sÈ  |  j  j d t ƒ } | rQ | j ƒ  } |  j  j j ƒ  j ƒ  j | ƒ } | } n* |  j  j j ƒ  j ƒ  } | j ƒ  j	 ƒ  } |  j  j
 j j j j d | d | ƒ |  j  j
 j j j j t k sÄ t ‚ |  j  j
 j j j j d  k	 sè t ‚ t |  j  j
 j j j j ƒ t | ƒ k st ‚ |  j  j } |  j  j d d d d d | d t d	 t d
 t d t d t d t d t ƒ 
} t j d |  j j ƒ  j d |  j  j ƒ d  |  j  j
 j j _ t |  j  j
 j j j _ d  S(   NR   R   t	   selectiont   max_number_of_iterationsiô  t   number_of_macro_cyclesi   t   bondt	   nonbondedt   anglet   dihedralt	   chiralityt	   planarityt   generic_restraintsR   R   (   R)   R   R4   t   get_peptide_c_alpha_selectionR   R   RR   t   selectt   atomst   extract_i_seqR0   R   R1   t   reference_managert   add_coordinate_restraintsRB   R   RP   t   reference_coordinate_proxiesR    R5   t   selection_movingt   geometry_minimizationR   RT   R(   R>   RW   t   reference_coordinate(   RX   R   R   t   ca_selectiont   restraint_sites_cartt   restraint_selectionR¡   t	   minimized(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyR<   �  sD    				N(   t   __name__t
   __module__R    Rh   RI   RM   RL   RN   Rv   R   RW   R<   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyR      s   Š	[	O					 (   t
   __future__R    t   mmtbxR   t   mmtbx.dynamicsR   t   mmtbx.refinementR   t   libtbxR   R   R   R   t   mmtbx.geometry_restraintsR   t   cctbx.array_familyR	   t	   cStringIOR
   R!   Rn   t   objectR   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/refinement.pyt   <module>   s   