ó
Ò™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	 d  d l
 m Z d  d l Z d  d l Z d e f d	 „  ƒ  YZ d
 „  Z e d d „ Z d d e e d d d e e e e j d „ Z d S(   iÿÿÿÿ(   t   division(   t   flex(   t   lbfgs(   t   make_sub_header(   t   approx_equal(   t   adopt_init_argsNt	   minimizerc           B   s/   e  Z d d  d d d „ Z d „  Z d „  Z RS(   i   i   i    c      
   C   sÄ  t  |  t ƒ  ƒ t j ƒ  |  _ xL | D]D } | j rK |  j j | j ƒ n  | j r& |  j j | j	 ƒ q& q& W| j
 j ƒ  j d t ƒ x^ | D]V } | j r¼ | j
 j ƒ  j d | j ƒ n  | j rŽ | j
 j ƒ  j d | j ƒ qŽ qŽ W| j ƒ  |  _ |  j j ƒ  x| t | ƒ D]n |  _ t j d |  d t j d | ƒ d t j d t ƒ ƒ |  _ | d  k	 r|  j ƒ  | d |  ƒ sPqqqW| d  k rœ|  j ƒ  n  |  ` |  ` |  ` |  ` |  ` |  ` d  S(	   Nt   statet
   iselectiont   target_evaluatort   termination_paramst   max_iterationst   exception_handling_paramst-   ignore_line_search_failed_step_at_lower_boundR   (   R   t   localsR   t   doublet   xt   refine_f_primet   appendt   f_primet   refine_f_double_primet   f_double_primet   xray_structuret
   scattererst   flags_set_gradst   Falset   flags_set_grad_fpR   t   flags_set_grad_fdpt   target_functort   prepare_for_minimizationt   xranget   i_cycleR   t   runt   termination_parameterst   exception_handling_parameterst   Truet   Nonet   unpackt   fmodelt   groups(   t   selfR&   R'   t   call_back_after_minimizer_cyclet   number_of_minimizer_cyclest   lbfgs_max_iterationst!   number_of_finite_difference_testst   group(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt   __init__   sH    	 	 						
c         C   s    t  |  j ƒ } xG |  j D]< } | j r: | j ƒ  | _ n  | j r | j ƒ  | _ q q Wx- |  j D]" } | j d |  j	 j
 j ƒ  ƒ qc W|  j	 j d t ƒ d  S(   NR   t   update_f_calc(   t   iterR   R'   R   t   nextR   R   R   t   copy_to_scatterers_in_placeR&   R   R   t   update_xray_structureR#   (   R(   t   xiR-   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyR%   6   s    	 	 	c         C   s  |  j  ƒ  |  j d t ƒ } |  j } | j ƒ  } | j ƒ  } | j d d  d | j ƒ  d | j	 d d d | d | j
 j ƒ } | j ƒ  } | j ƒ  } ~ t j ƒ  } xm |  j D]b }	 |	 j rá | j t j | j |	 j ƒ ƒ ƒ n  |	 j r­ | j t j | j |	 j ƒ ƒ ƒ q­ q­ W|  j d k r|  j d	 8_ g  }
 d
 } |  j } xª t | j ƒ  ƒ D]– } g  } | | } x\ | | g D]M } | | | | <|  j  ƒ  | | | <|  j d t ƒ } | j | j ƒ  ƒ q}W|
 j | d | d	 d | ƒ qYW|  j  ƒ  t |
 | ƒ st ‚ n  | | f S(   Nt   compute_gradientst   u_iso_refinable_paramst   d_target_d_f_calcR   t   n_parametersi    t
   miller_sett	   algorithmi   gñhãˆµøä>i   (   R%   R   R#   R&   t   target_workt   d_target_d_f_calc_workt   structure_factor_gradients_wR$   t   dataR   t
   sfg_paramsR:   t   d_target_d_fpt   d_target_d_fdpR   R   R'   R   R   t   sumt   selectR   R   R,   R   R   t   sizeR   R   t   AssertionError(   R(   t   t_rR&   t   fR7   t   sfgt   d_t_d_fpt	   d_t_d_fdpt   gR-   t   g_fint   epsR   t   it   fst   xi0t
   signed_eps(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt    compute_functional_and_gradients@   sN    
				%	)	


%
N(   t   __name__t
   __module__R$   R.   R%   RR   (    (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyR   
   s   $	
c         C   sV   |  j  ƒ  } | j } | d k r* d } n  d | j | j | j | | j ƒ  f } | S(   Nt    t    sD   chain '%s' and resname %s and name '%s' and altloc '%s' and resid %s(   t   fetch_labelst   altloct   chain_idt   resnamet   namet   resid(   t   atomt   labelsRX   t   sele(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt    get_single_atom_selection_stringi   s    	 	%c         C   sM  d d l  m } d d l m } | d k r8 t j } n  i  } g  } | d k r\ t ƒ  } n  | j ƒ  } xSt	 | j
 ƒ  ƒ D]?\ }	 }
 | |	 r— q{ n  |
 j ƒ  j ƒ  } y | j | ƒ j ƒ  } WnB t k
 r} | d |  |	 j ƒ  IJ| d t | ƒ IJq{ n X| d k r{ | rL| | k r8t j ƒ  | | <n  | | j |	 ƒ qº| d |  |	 j ƒ  IJ| j d t j |	 g ƒ d	 d
 d d
 d d	 d g d t |  |	 ƒ ƒ } | j | ƒ q{ q{ W| rIx‚ t | j ƒ  ƒ D]k } | | } | d | t | ƒ f IJ| j d | d	 d
 d d
 d d	 d g d d | ƒ } | j | ƒ q×Wn  | S(   sŸ   
  Automatic setup of anomalously scattering atoms, defined here as anything
  with atomic number 15 (P) or greater.  Not yet accessible from phenix.refine.
  iÿÿÿÿ(   t   sasaki(   t   xrays   Error for %ss     i   s!     creating anomalous group for %sR   R   i    R   t   refinet   selection_strings7     creating anomalous group for element %s with %d atomss
   element %sN(   t   cctbx.eltbxRa   t   cctbxRb   R$   t   syst   stdoutt   null_outt   hd_selectiont	   enumerateR   t   element_symbolt   stript   tablet   atomic_numbert   RuntimeErrort   id_strt   strR   t   size_tR   t   anomalous_scatterer_groupR`   t   sortedt   keyst   len(   t	   pdb_atomsR   t   group_same_elementt   outRa   Rb   t   element_i_seqsR'   Rj   t   i_seqt	   scatterert   elementRo   t   et   asgt   elemR   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt   find_anomalous_scatterer_groupsr   s^    	  
	
	t   anom_residualg      @R   R   c   +      C   sH  d d l  m } |  j ƒ  j ƒ  s( t ‚ | d/ k s: t ‚ t d d | ƒ|  j d d ƒ | j ƒ  } | j ƒ  j	 d	 ƒ } |  j
 j ƒ  } |  j
 j ƒ  } t j |  j
 j ƒ  j | ƒ ƒ } t j ƒ  } g  } t j ƒ  } d
 } x!| d0 k sù | | k  r| d 7} d
 } t j ƒ  } | d | IJ|  j d | ƒ j d d ƒ j ƒ  j ƒ  } t t j | j ƒ  ƒ ƒ } t j | j ƒ  ƒ } | d | | f IJt j ƒ  } x­t | ƒ D]Ÿ\ } } | j ƒ  j } | j j  ƒ  }  | | k sª| rò| d k sª|  d1 k r| d k r| rqªn  | | }! | | }" | j! |" ƒ }# |# | k sZ|! j" d
 k rª|
 rª| r | d
 k rƒ| d IJ| d IJn  | d | j# ƒ  |# f IJn  | j$ | ƒ t% | ƒ }$ | j& d t j | g ƒ d d
 d d
 d t' | ƒ d |$ ƒ }% | j$ |% ƒ | d 7} | d k rI|	 rI|! j( }& |& | k  rF| j$ | ƒ qFqIqªqªW| d
 k rq| d IJ| d IJPn | sˆ| d  | IJn  x* | D]" } | | }' d
 |' _) d
 |' _" q�W| rø| d IJ| d! IJ|  j* ƒ  j+ d" d# d | ƒ | d IJn  |  j
 j ƒ  }( |( j, | | ƒ |  j
 j- d$ |( ƒ |  j. d% t/ ƒ t0 d& |  d' | ƒ | rÍ|  j* ƒ  j+ d" d( d | ƒ | d IJ| d) IJxG t1 | | ƒ D]3 \ } }% | d* | | j# ƒ  |% j2 |% j3 f IJq“Wn  t j ƒ  }) | d IJ| rá | d+ |) | IJqá qá W|  j d d, ƒ | d- t4 | ƒ IJt j ƒ  }* | d. |* | IJ| S(2   s  
  Crude mimic of Phaser's substructure completion, with two essential
  differences: only the existing real scatterers in the input model will be
  used (with the assumption that the model is already more or less complete),
  and the anomalous refinement will be performed in Phenix, yielding both
  f-prime and f-double-prime.  The refined f-prime provides us with an
  orthogonal estimate of the number of electrons missing from an incorrectly
  labeled scatterer.

  :param wavelength: X-ray wavelenth in Angstroms
  :param exclude_waters: Don't refine anomalous scattering for water oxygens
  :param exclude_non_water_light_elements: Don't refine anomalous scattering
    for light atoms other than water (CHNO).
  :param n_cycles_max: Maximum number of refinement cycles
  :param map_sigma_min: Sigma cutoff for identify anomalous scatterers
  :param reset_water_u_iso: Reset B-factors for water atoms prior to f'
    refinement
  :param use_all_anomalous: include any scatterers which are already modeled
    as anomalous in the refinement
  iÿÿÿÿ(   Rb   t   llgRƒ   s+   Iterative anomalous substructure refinementRz   t   target_namet   lss5   (not element H and not element D and not resname HOH)i    i   s   Cycle %dt   map_typet   resolution_factorg      Ð?s&     map range: -%.2f sigma to %.2f sigmat   HOHt   Ht   Dt   Nt   Ct   ORU   s     new anomalous scatterers:s"       %-34s  map height: %6.2f sigmaR   R   R   Rc   Rd   s6   No new groups - anomalous scatterer search terminated.s     %d new groupss   Anomalous refinement:t   texts   before minimizationt   valuesR/   R&   R'   s   after minimizations     Refined sites:s"       %-34s  f' = %6.3f  f'' = %6.3fs     time for this cycle: %.1fst   mls%   %d anomalous scatterer groups refineds   overall time: %.1fs(   R„   Rƒ   N(   RŠ   R‹   RŒ   R�   RŽ   (5   Rf   Rb   t   f_obst   anomalous_flagRE   R   t   updatet   atomst   atom_selection_cachet	   selectionR   t
   sites_fracR   R   t   meant   extract_u_iso_or_u_equivRC   Rs   t   timeR$   t   map_coefficientst   fft_mapt   apply_sigma_scalingt   real_map_unpaddedt   abst   mint   as_1dt   maxRk   t   parentRZ   R~   Rm   t   tricubic_interpolationt   fdpRq   R   R`   Rt   t   listt   u_isot   fpt   infot   show_targetst   set_selectedt	   set_u_isoR3   R#   R   t   zipR   R   Rw   (+   R&   t   pdb_hierarchyt
   wavelengthR‡   t   exclude_waterst    exclude_non_water_light_elementst   n_cycles_maxt   map_sigma_minRc   t   reset_water_u_isot   use_all_anomaloust   verboseRz   Rb   Rx   t   non_water_non_hd_selectionR˜   R   t
   u_iso_meant   anomalous_iselectiont   anomalous_groupst   t_startt   n_cyclet   n_new_groupst   t_start_cyclet   anom_mapt   map_mint   map_maxt   reset_u_iso_selectionR|   R]   RZ   R�   R}   t	   site_fract   anom_map_valueRd   R-   t   water_u_isot   scR¨   t   t_end_cyclet   t_end(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt   refine_anomalous_substructureª   sÂ    "	



		
	


	



'
(   s   f_primes   f_double_prime(   t
   __future__R    t   cctbx.array_familyR   t   scitbxR   t   libtbx.str_utilsR   t   libtbx.test_utilsR   t   libtbxR   R›   Rg   t   objectR   R`   R#   R$   R‚   R   Rh   RÊ   (    (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/anomalous_scatterer_groups.pyt   <module>   s,   _	7