ó
Ï™xTc           @   s  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 d  d l m Z d  d l j j Z d  d l m Z d  d	 l m Z d  d l Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   real_space_target_and_gradients(   t   adopt_init_argsN(   t   maptbx(   t   flex(   t   utils(   t   approx_equal(   t   crystalt   easyc           B   s)   e  Z d  Z d d d d d d d „ Z RS(   s~   
  Simplest interface to most automated and fast real-space refinement.
  To keep it simple not all parameters are exposed.
  g¸…ëQ¸Ž?g       @c         C   s‡  t  |  t ƒ  ƒ | j j d | j ƒ  ƒ } | j ƒ  d |  _ | j ƒ  d |  _ | |  _	 |  j	 d  k rÄ d d l m } | j d | d |  j d |  j d | d	 | d
 | ƒ |  _ |  j j |  _	 n  | d  k rô t j |  j j ƒ  j ƒ  t ƒ } n  t d | d | d | j d |	 ƒ } | j d |  j	 d |  j ƒ | j ƒ  \ |  _ |  _ |  j j d | j ƒ  d d  ƒ |  _ |  j j |  j ƒ d  S(   Nt
   sites_carti   iÿÿÿÿ(   t   weightt   map_datat   xray_structuret   pdb_hierarchyt   geometry_restraints_managert   rms_bonds_limitt   rms_angles_limitt
   target_mapt	   selectiont   states_accumulatorR
   t	   new_sites(   R   t   localst   geometryt   energies_sitesR	   t   angle_deviationst   rmsd_angles_startt   bond_deviationst   rmsd_bonds_startt   wt   Nonet   mmtbx.refinement.real_spaceR
   t   runR   R   R   t   boolt
   scattererst   sizet   Truet   simplet   refinet   rmsdst   rmsd_bonds_finalt   rmsd_angles_finalt   replace_sites_cartt   adopt_xray_structure(   t   selfR   R   R   R   R   R   R   R   R   t   logt   esR
   t   refine_object(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyt   __init__   s8    				$		N(   t   __name__t
   __module__t   __doc__R   R/   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR      s   R$   c           B   s;   e  Z d d d d d d „ Z d „  Z d „  Z d „  Z RS(	   g      ð?i   i–   c	   	      C   sk   t  |  t ƒ  ƒ t j j d | ƒ |  _ t j j d t d t d t ƒ |  _ d  |  _
 d  |  _ d  |  _ d  S(   Nt   max_iterationst-   ignore_line_search_failed_step_at_lower_boundt-   ignore_line_search_failed_step_at_upper_boundt    ignore_line_search_failed_maxfev(   R   R   t   scitbxt   lbfgst   termination_parameterst   lbfgs_termination_paramst   exception_handling_parametersR#   t   lbfgs_exception_handling_paramsR   t   refinedt   crystal_symmetryt   site_symmetry_table(	   R+   R   R   R   t   real_space_gradients_deltat   selection_real_spaceR3   R   t
   ncs_groups(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR/   =   s    
		c         C   sr  |  j  d  k rœ | j ƒ  |  _ | j ƒ  |  _ t j j d |  j d |  j d | j	 ƒ  d |  j
 d |  j d | d |  j d |  j d	 |  j d
 |  j ƒ 
|  _ nÒ t j | j ƒ  j ƒ  t ƒ j ƒ  } t j j j d |  j
 d | j ƒ  d |  j  d | d |  j d |  j d | d t d t ƒ 	|  _ t j j j d |  j d | j ƒ  d |  j  d | d t d |  j d t d t ƒ } d  S(   Nt   selection_variablet   selection_variable_real_spaceR	   t   density_mapR   t   real_space_target_weightR@   R:   R<   t   states_collectorR   R   t   ncs_restraints_group_listt   refine_selectiont   restraints_managert   data_weightt   refine_sitest   refine_transformationst   target_and_grads_objectt   finite_grad_differences_testR3   (   RB   R   R>   R?   R   t   real_space_refinement_simpleR8   R   RA   R	   R   R   R@   R:   R<   R   R=   R   R    R!   R"   R#   t
   iselectiont   mmtbxt
   refinementt   minimization_ncs_constraintst$   target_function_and_grads_real_spacet   deep_copy_scattererst   FalseR3   (   R+   R
   R   RI   t	   minimized(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR%   S   sF    														c         C   sî   |  j  d  k	 s t ‚ |  j d  k r3 |  j  j } n |  j  j j ƒ  } | S|  j rn | j |  j |  j  j ƒ n  |  j	 j
 ƒ  } | j ƒ  d k rê xX | D]M } t j d |  j d |  j	 j | ƒ j ƒ  d | | d d  d d ƒ | | <q– Wn  | S(   Ni    R>   t
   special_opt	   site_cartt
   site_labelt	   tolerancei   (   R=   R   t   AssertionErrorRB   R	   R   R   t   set_selectedt   sites_cart_variableR?   t   special_position_indicesR"   R   t   correct_special_positionR>   t   getRY   (   R+   R	   R`   t   i_seq(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR	   z   s"    			
c         C   sQ   d \ } } |  j j d |  j ƒ  ƒ } | j ƒ  d } | j ƒ  d } | | f S(   NR	   i   (   NN(   R   R   R   R	   R   R   (   R+   t   bt   aR-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR&   Ž   s    g      Ð?N(   R0   R1   R   R/   R%   R	   R&   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR$   <   s   	'	t   diff_mapc           B   s2   e  Z d  d d d „ Z d d d „ Z d „  Z RS(   i   iô  c         C   s0   t  |  t ƒ  ƒ | j ƒ  d |  _ d  |  _ d  S(   Ng      @(   R   R   t   d_mint   stepR   R=   (   R+   t   miller_arrayt   crystal_griddingt
   map_targetR   t   restraints_target_weightR3   t   min_iterations(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR/   —   s    c         C   sš   | d  k	 r' | | g j d  ƒ d k s- t ‚ t j d | d |  j d |  j d |  j d |  j d |  j	 d |  j
 d	 | d
 |  j d |  j d d ƒ |  _ d  S(   Ni   R   Ri   Rj   Rk   R3   Rm   Rh   RF   Rl   R   t   target_typeRf   (   R   t   countR]   R   t   minimizationRi   Rj   Rk   R3   Rm   Rh   Rl   R   R=   (   R+   R
   R	   R   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR%   £   s    -									c         C   s%   |  j  d  k	 s t ‚ |  j  j j ƒ  S(   N(   R=   R   R]   R   R	   (   R+   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR	   ²   s    N(   R0   R1   R/   R   R%   R	   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyRf   –   s
   t   refineryc           B   s2   e  Z d  d d d e d „ Z d „  Z d „  Z RS(   g      I@i
   g¸…ëQ¸ž?g      @c         C   s¢  d  |  _ d  |  _ | |  _ | |  _ | j ƒ  } |  j d | j ƒ  ƒ \ |  _ |  _ | |  _ d  |  _ d  |  _	 d	 \ |  _
 |  _ i  }	 t j ƒ  }
 t j ƒ  } t j ƒ  } | } | } |  j d | ƒ | rFxžt rB|  j d | ƒ |  j d | ƒ | j d | j ƒ  d | ƒ | j ƒ  } |  j d | ƒ \ } } |
 j | ƒ | j | ƒ | j | ƒ |	 j | g  ƒ j | j ƒ  | | g ƒ | j d  k r˜Pn  | } | | k s¶| | k rÆ| |  j 8} n | |  j 7} | d k  sñt | ƒ d k  r|  j d | ƒ | } | |  j 8} n  | d k  s;| d k s;| | k rÐ PqÐ qÐ Wn( | j d | j ƒ  d | ƒ | j ƒ  } | r‰|
 | } | t j | j | d k ƒ t j | ƒ ƒ k j ƒ  d } | | |  _ |	 |  j d d |  _	 |  j d |  j	 ƒ \ |  _
 |  _ t |	 |  j d d | | ƒ s&t ‚ t |	 |  j d d |
 | ƒ sNt ‚ t |  j | | ƒ sjt ‚ t |  j
 |
 | ƒ sžt ‚ n |  j |  _ | |  _	 d  S(
   NR	   R
   R   i    g�íµ ÷Æ°>iè  i   i   (   NN(   R   t   rms_angles_startt   rms_bonds_startt   refinert   weight_startR	   R&   t   weight_sample_ratet   weight_finalt   sites_cart_resultt   rms_bonds_finalt   rms_angles_finalR   t   doublet   adjust_weight_sample_rateR#   R%   RV   t   appendt
   setdefaultt	   deep_copyR   t   abst   max_defaultt   selectt   minRQ   R   R]   (   R+   Rt   R   t   start_trial_weight_valueRv   R   R   t   optimize_weightt   sites_cart_startt   poolt   bondst   anglest   weightsR
   t   weight_lastRx   t   bdt   adt   deltat   ind(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR/   ·   sv    				$					( $ 	
!((c         C   sc   d \ } } |  j j d  k	 rY |  j j j d | ƒ } | j ƒ  d } | j ƒ  d } n  | | f S(   NR	   i   (   NN(   R   Rt   R   R   R   R   (   R+   R	   Rd   Re   R-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR&     s    	c         C   s”   | d k r d |  _  nx | d k r0 d |  _  n` | d k rH d |  _  nH | d k r` d |  _  n0 | d k rx d |  _  n | d k r� d |  _  n  d  S(   Ng{®Gáz„?gü©ñÒMbP?gš™™™™™¹?g      ð?g      $@g      Y@g     @�@(   Rv   (   R+   R
   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR|     s          (   R0   R1   R#   R/   R&   R|   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyRq   ¶   s   F		t   box_refinement_managerc           B   sG   e  Z d d d d „ Z d „  Z d „  Z e d d d d d	 d
 „ Z RS(   g      ð?i   i2   c         C   sR   | |  _  | j ƒ  |  _ | |  _ | |  _ | |  _ | |  _ d  |  _ | |  _ d  S(   N(	   R   R	   R   R   R3   R@   R   t   weight_optimalRB   (   R+   R   R   R   R@   R3   RB   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR/     s    						c         C   s   | |  _  d  S(   N(   R   (   R+   t   new_xray_structure(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyt   update_xray_structure&  s    c         C   s   | |  _  d  S(   N(   R   (   R+   t   new_target_map(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyt   update_target_map)  s    i   i   g¸…ëQ¸ž?g      @c         C   s£  |  j  d  k r¾|  j } |  j j d | d | ƒ }	 | j |	 ƒ }
 t j ƒ  } x0 t | ƒ D]" \ } } | r[ | j	 | ƒ q[ q[ Wt
 j d |  j d |  j d |	 d | ƒ } | j j ƒ  } |  j j | j ƒ } | j d | ƒ } | j } | j j ƒ  } t d | d |
 d |  j d	 |  j d
 | ƒ } t d | d | j d | d | d | d | ƒ } | j |  _ | j } | | j } | j |
 ƒ } | j | | ƒ } |  j j | ƒ |  j j ƒ  |  _ ná |  j j | ƒ } |  j j | ƒ } t j | j ƒ  j ƒ  t ƒ }
 |  j j ƒ  j ƒ  } t  j! d |  j  d | ƒ } t d |  j d |
 d |  j d	 |  j d
 | d | ƒ } t d | d | d | d | d | d | ƒ } | j |  _ d  S(   Nt   radiusR   R   R   t   box_cushiont   new_unit_cellR   R@   R3   R   Rt   R…   R„   R   R   RH   RB   ("   RB   R   R	   R   t   selection_withinR‚   R   t   size_tt	   enumerateR}   R   t    extract_box_around_model_and_mapR   t   xray_structure_boxt	   unit_cellR   t   discard_symmetryt   map_boxR$   R@   R3   Rq   Rw   R‘   Rx   t   shift_to_map_boxed_sites_backR^   t   set_sites_cartR    R!   R"   R#   t	   ncs_utilst   ncs_groups_selection(   R+   R   R…   R„   t   selection_buffer_radiusR—   R   R   t   sites_cart_movingR™   t   selRQ   t   it   statet   boxR˜   t   geo_boxR    t   sites_cart_boxt   rsr_simple_refinert   real_space_resultt   sites_cart_box_refinedt#   sites_cart_box_refined_shifted_backt   sites_cart_refinedt   grmt   xrsR"   t   ncs_groups_(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR%   ,  s~    																					g      Ð?N(   R0   R1   R   R/   R“   R•   R#   R%   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyR�     s   
		(   t
   __future__R    t   cctbx.maptbxR   t   libtbxR   t   scitbx.lbfgsR7   t   cctbxR   t   cctbx.array_familyR   RR   R   t   mmtbx.ncs.ncs_utilst   ncsR£   t   libtbx.test_utilsR   R   t-   mmtbx.refinement.minimization_ncs_constraintst   objectR   R$   Rf   Rq   R�   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/individual_sites.pyt   <module>   s   .Z _