ó
Û™xTc           @   sj  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l Z d d l	 m
 Z
 d d l Z e j j d ƒ Z d d l m Z m Z d d	 l m Z d d l Z d d l Z e j j d
 ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d e  d e! d d d „ Z" d „  Z# e! e! e! d „ Z$ d „  Z% d S(   s—   
Methods and utilities for generating composite omit maps.  The actual
end-user application, phenix.composite_omit_map, is part of the phenix
sources.
iÿÿÿÿ(   t   divisionN(   t   miller(   t   maptbx(   t   flext   cctbx_asymmetric_map_ext(   t   slots_getstate_setstatet+   slots_getstate_setstate_default_initializer(   t   null_outs‚   
n_debias_cycles = 2
  .type = int
neutral_volume_box_cushion_width = 1
  .type = float
full_resolution_map = True
  .type = bool
t   runc           B   s2   e  Z d  Z d d d e e j d „ Z d „  Z RS(   s  
  Composite full-omit map: omit entire box in real-space corresponding to
  Fmodel, which includs atomic model and non-atomic model (bulk-solvent and
  scales).  This is much faster than the refinement-based method, and is used
  by default in phenix.composite_omit_map.
  g¸…ëQ¸ž?i   i   c	      
      s—  | ˆ  _  ˆ  j  j d  k	 s! t ‚ | j ƒ  j ƒ  j ƒ  ˆ  _ | j ƒ  j	 d t
 j | j ƒ  j ƒ  d ƒ ƒ ˆ  _ t ‡  f d † }	 | j ƒ  j d t j d ˆ  j  ƒ j ƒ  }
 |
 j ƒ  ˆ  _ t j ˆ  j |
 ƒ j ƒ  } | j ƒ  ˆ  _ | j ƒ  } t j d | j ƒ  d | d | ƒ } t
 j t
 j | j ƒ ƒ ˆ  _ | j ƒ  | j k sMt ‚ | j ˆ  j j ƒ  k skt ‚ | j } t
 j ƒ  ˆ  _  t! | j" ƒ } d } xõt# | j" | j$ ƒ D]Þ\ } } | d	 7} | d k rC| } g  d d	 d
 g D] } t% | | | d ƒ ^ qæ} g  d d	 d
 g D]! } t& | | | | | ƒ ^ q} n | | } } | j' ƒ  } x8 t( | ƒ D]* } ˆ  j) d | d | d | ƒ \ } } qiWt* j+ j, d | j ƒ  d | j- ƒ  d | d ˆ  j ƒ } | j. ƒ  } ˆ  j  j/ | ƒ |	 d | d | d ˆ  j  ƒ } | j ƒ  } | rF| d | | f I| I| Id | IJn  | j ƒ  ˆ  j j ƒ  k sgt ‚ t j0 d | d ˆ  j d | d | ƒ q®Wˆ  j j1 ˆ  j ƒ t j ˆ  j ˆ  j ˆ  j ƒ ˆ  _2 ˆ  j j	 d ˆ  j j3 ƒ  d ˆ  j2 j4 ˆ  j j3 ƒ  ƒ ƒ ˆ  _5 | r“ˆ  j j6 d ˆ  j j7 ƒ  ƒ } t j ˆ  j ˆ  j ˆ  j ƒ } ˆ  j j	 d | j3 ƒ  d | j4 | j3 ƒ  ƒ ƒ } ˆ  j5 j8 d | d t ƒ ˆ  _5 n  d  S(   Nt   datai    c      	      sx   |  j  ƒ  j d | d t d t d t ƒ } t j j d | d | ƒ } | rj t j ˆ  j	 | j
 ƒ  ƒ j ƒ  S| j
 ƒ  Sd  S(   Nt   map_typet
   isotropizet   exclude_free_r_reflectionst   fill_missingt   crystal_griddingt   fourier_coefficients(   t   electron_density_mapt   map_coefficientst   Truet   Falset   cctbxR   t   fft_mapt   asu_map_extt   asymmetric_mapt   sgtt   real_map_unpaddedR	   (   t   fmodelR
   R   t   asut   f_mapR   (   t   self(    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   get_map7   s    		 t   symmetry_flagsR   t   n_realt   fractiont   logi   i   t   st   et   md_asut   f_obst   r_free_flagst   f_calct   f_maskR   R
   s   box %2d of %2d:s   %6.4ft   map_data_fromt   map_data_tot   startt   endt   indicest   d_mint   othert   scale(9   R   t   _symmetry_flagst   Nonet   AssertionErrorR&   t   space_groupt   typeR   R(   t   customized_copyR   t   complex_doublet   sizet   zero_cmpl_maR   t   f_model_scaled_with_k1R   R   t   use_space_group_symmetryR   t   focusR    R   R   R	   t   accessort   acct   shift_origint   boxest   doublet   gridt   map_result_asut   rt   lent   startst   zipt   endst   maxt   mint	   deep_copyt   xranget   omit_boxt   mmtbxt   f_modelt   managerR'   t   r_workt   appendt   copy_boxt   reshapet   asu_map_omitR.   t   structure_factorsR   t   complete_setR/   t   complete_with(   R   R   R   R
   t   box_size_as_fractiont   n_debias_cyclest    neutral_volume_box_cushion_widtht   full_resolution_mapR"   R   t   f_model_map_datat   f_model_map_data_asut   bt
   n_real_asut   n_boxest   i_boxR#   R$   t   sht   it   sst   eet   f_model_map_data_asu_t   f_model_omitt   fmodel_t   rwt   f_map_data_asut   csRV   t   fill(    (   R   sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   __init__&   sŠ    
	$				"
3:-!		!c   
   	   C   sæ   t  j d d d | d | d | ƒ } | j |  j ƒ t j |  j | |  j ƒ } |  j j	 d |  j j
 ƒ  d | j |  j j
 ƒ  ƒ ƒ } t j j d |  j d	 | ƒ } | j ƒ  } t j |  j | ƒ }	 |	 j ƒ  } | j ƒ  } | | f S(
   Nt   valuei    R+   R,   R-   R.   R	   R   R   (   R   t   set_box_copyRU   R?   R   R   R   R    R:   R7   R.   RW   R   R   R   R   R   R	   R@   (
   R   R#   R$   R%   t   md_asu_omitRV   t   ma_omitR   t   mdt   asu_map(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRN   Œ   s    (   t   __name__t
   __module__t   __doc__R   t   syst   stdoutRo   RN   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyR      s   ]RN   c           B   sA   e  Z d  Z d d d d g Z e d „  ƒ Z e j d d „ Z RS(   sa   
  Defines a region in fractional coordinates containing a selection of atoms
  to be omitted.
  t   frac_mint   frac_maxt	   selectiont   serialc         C   s   t  |  j ƒ S(   N(   RF   R}   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   n_atoms°   s    t    c         C   sH   | | d t  |  j t |  j ƒ g t |  j ƒ t |  j ƒ ƒ IJd  S(   NsE   box %d: atoms: %6d  extents: (%.4f, %.4f, %.4f) to (%.4f, %.4f, %.4f)(   t   tupleR~   RF   R}   t   listR{   R|   (   R   t   outt   prefix(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   show´   s    (	   Rv   Rw   Rx   t	   __slots__t   propertyR   Ry   Rz   R…   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRN   ©   s   t   omit_regionsc           B   sk   e  Z d  Z d d d g Z d d „ Z e d „  ƒ Z e d „  ƒ Z d „  Z	 d „  Z
 e j d	 d
 „ Z RS(   sá   
  Groups together multiple omit_box objects (without any implicit spatial
  relationship); this is used to reduce the total number of bins of omitted
  atoms and to make the fraction omitted per bin approximately similar.
  RA   R}   R~   c         C   s=   | |  _  g  |  _ | |  _ | d  k r9 t j ƒ  |  _ n  d  S(   N(   R~   RA   R}   R3   R   t   size_t(   R   R~   R}   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRo   Á   s
    			c         C   s   t  |  j ƒ S(   N(   RF   RA   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRb   È   s    c         C   s   t  |  j ƒ S(   N(   RF   R}   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyR   Ì   s    c         C   s,   |  j  j | ƒ t | j |  j ƒ |  _ |  S(   N(   RA   RS   t   join_selectionsR}   (   R   t   box(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   add_boxÐ   s    c         C   s%   x | j  D] } |  j | ƒ q
 W|  S(   N(   RA   RŒ   (   R   R0   R‹   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   combine_withÕ   s    R€   c         C   st   t  |  j ƒ d k r- | | d |  j IJnC | | d |  j IJx+ |  j D]  } | j d | d | d ƒ qL Wd  S(   Ni    s   Region %d: emptys
   Region %d:Rƒ   R„   s     (   RF   RA   R~   R…   (   R   Rƒ   R„   R‹   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyR…   Ú   s
    N(   Rv   Rw   Rx   R†   R3   Ro   R‡   Rb   R   RŒ   R�   Ry   Rz   R…   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRˆ   º   s   		t   omit_region_resultsc           B   s   e  Z d  d d d g Z RS(   R~   t   map_coeffs_listRR   t   r_free(   Rv   Rw   R†   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRŽ   â   s   gš™™™™™©?g      @gñhãˆµøä>c   D   	   C   s„  | d k r t ƒ  } n  | d k rE t j |  j ƒ  j ƒ  t ƒ } n  | j ƒ  } g  }	 |  j ƒ  }
 |
 j	 ƒ  } |
 j
 d | ƒ } | j ƒ  } t j ƒ  } xr t | j ƒ  ƒ D]^ \ } } | | sÈ q¬ n  x? | D]7 } | j ƒ  } | GH| j d | ƒ } | j | ƒ PqÏ Wq¬ W| j d | g d ƒ \ } } } | j ƒ  \ } } } | j ƒ  \ } } } |  j ƒ  } | j | ƒ j t ƒ } | | } | d k s›t ‚ | rÚ| | | d }  | | | d }! | | | d }" n: | d }# |# | d }$ | j d |$ g d ƒ \ }  }! }" | j ƒ  }% t j t t | ƒ ƒ ƒ }& |% j |& d ƒ }' |% j |& d d ƒ }( |% j |& d d ƒ }) | }* d }+ x“|* | k  r| }, xp|, | k  r| }- xM|- | k  r|* |  }. |, |! }/ |- |" }0 |* | }1 |. | }2 |, | }3 |/ | }4 |- | }5 |0 | }6 |' |1 k |' |2 k  @|( |3 k @|( |4 k  @|) |5 k @|) |6 k  @}7 |7 j ƒ  }8 t |8 ƒ }9 |9 d k rôt |. d
 ƒ t |/ d
 ƒ t |0 d
 ƒ g }: t d |* |, |- g d |: d | j |8 ƒ d |+ ƒ }; |	 j |; ƒ |+ d 7}+ n  |- |" 7}- qµW|, |! 7}, q W|* |  7}* q‹Wg  }< x6 |	 D]. }= t d |= j ƒ j |= ƒ }> |< j |> ƒ q+W| rFt  |< d „  ƒ }< xÎ t rBd }? d }@ x¢ |@ t |< ƒ k  r.|@ d |< |@ _ d }A xh |A |@ k  r |< |@ j! |< |A j! }B |B | k r|< |A j" |< |@ ƒ |< |@ =|? d 7}? Pn  |A d 7}A q¹W|@ d 7}@ q�W|? d k rxPqxqxWn  t t# g  |< D] }C |C j ^ qSƒ ƒ t |< ƒ k s€t ‚ |< S(   s*  
  Divide the asymmetric unit into boxes of atoms to omit.  This will include
  a cushion around the actual omit region.  Although the step size is uniform,
  by default boxes will be grouped as needed to make the distribution roughly
  even and keep the total number of omit regions to a minimum.
  If a region does not contain any atoms, it will be skipped and filled in
  with the background map.

  Returns a list of omit_regions objects, which specify the selection of atoms
  to omit along with the boxes of interest (in fractional coordinates).
  t   buffer_thicknesst	   site_carti   i    i   i   g      "@i   g      @g      ð?R{   R|   R}   R~   c         S   s   t  | j |  j ƒ S(   N(   t   cmpR   (   t   aR`   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   <lambda>L  s    NgUUUUUUÕ?($   R3   R   R   t   boolt
   scatterersR9   R   t
   iselectiont   sites_mod_positivet	   unit_cellt   asu_mappingsR   t   vec3_doublet	   enumeratet   mappingst   mapped_sitet   fractionalizeRS   RK   RJ   t   hd_selectiont   selectt   countR4   t	   as_doubleR‰   t   rangeRF   RN   Rˆ   R~   RŒ   t   sortedR   R�   t   set(D   t   xray_structureR}   t   fraction_omitt   optimize_binningt   box_cushion_radiust   even_boxingt   asu_buffer_thicknessR"   R˜   RA   t   xrsRš   R›   R   t	   sites_asut   i_seqRž   t   mappingR’   t	   site_fract	   x_cushiont	   y_cushiont	   z_cushiont   x_mint   y_mint   z_mint   x_maxt   y_maxt   z_maxt
   non_hd_selt   n_omit_heavy_atomst   n_omit_per_boxt   x_stept   y_stept   z_stept   volumet   cube_lengtht   sites_1dt   sel_baset   sites_xt   sites_yt   sites_zt   x_startR~   t   y_startt   z_startt   x_endt   y_endt   z_endt	   x_min_boxt	   x_max_boxt	   y_min_boxt	   y_max_boxt	   z_min_boxt	   z_max_boxt   box_selectiont   box_iselectiont   n_atoms_current_boxR|   t   new_boxt   groupsR‹   t   groupt
   n_combinedt   i_groupt   j_groupt   n_atoms_combinedt   g(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   create_omit_regionså   sÀ     !
 		

"








>*		
:c         C   sd   |  j  | ƒ } t j t |  ƒ t ƒ } | j | d t ƒ |  j | ƒ }  |  j | ƒ t j	 |  ƒ S(   Ni    (
   t   intersection_i_seqsR   R–   RF   R   t   set_selectedR   R¢   t   extendR¦   (   t   sel1t   sel2t   intersectionst
   unique_sel(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRŠ   a  s    c         C   s  t  |  ƒ t  | ƒ k s t ‚ | j ƒ  } | j d t j d | ƒ j ƒ  } | r^ | j ƒ  n  | j ƒ  }	 | r§ t	 j
 |	 j ƒ  j ƒ  t ƒ }
 |	 j ƒ  j |
 d ƒ n  t j | j ƒ  |	 ƒ } | j ƒ  j ƒ  } |	 j ƒ  } | j ƒ  j ƒ  } | j ƒ  j ƒ  } | søt j | ƒ } d } xÙt | |  ƒ D]Å\ } } | j ƒ  } | j d | d t j ƒ j ƒ  } | rx| j ƒ  n  | j ƒ  } t j | j ƒ  | ƒ } | j ƒ  j ƒ  | k sºt ‚ x4| j D])} | rÖqÄn  t | | j ƒ ƒ } t | | j ƒ | ƒ } xç t | d | d d ƒ D]Ê } xÁ t | d | d d ƒ D]¤ } x› t | d | d d ƒ D]~ } y0 | j ƒ  | | | f | j ƒ  | | | f <Wqct k
 ràt d t | ƒ t | ƒ t | | | f ƒ f ƒ ‚ qcXqcWqAWqWqÄWq,Wn  | j  ƒ  S(   sÍ   
  For each box, FFT the corresponding omit map coefficients, extract the
  omit regions, and copy them to the combined map, using Marat's asymmetric
  map class to handle symmetry expansion internally.
  R   t   resolution_factori    i   i   s!   n_real: %s  origin: %s  index: %s(!   RF   R4   R5   R   R   R<   t   apply_volume_scalingt   apply_sigma_scalingR   R   R–   t   as_1dR9   R   Râ   R   R   R6   R	   t   originR=   t   allt	   frac2gridRH   RA   R�   R{   t   grid_maxR|   R¥   t
   IndexErrort   strt   map_for_fft(   t
   map_arrayst   omit_groupst   background_map_coeffsRè   t   flatten_backgroundt   sigma_scalingt   control_mapR5   R   t   background_mapt   sel_allt   asym_mapRì   t
   n_real_allRa   t
   m_real_asut   f2gt   nRÚ   t
   map_coeffst   omit_fft_mapt   omit_mapt   omit_asym_mapR‹   t
   grid_startt   grid_endt   ut   vt   w(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   combine_mapsi  sV    			 """0Ic         C   sL   t  |  d | d d ƒ t  |  d | d d ƒ t  |  d | d d ƒ f S(   Ni    i   i   (   RK   (   t   grid_coordsR    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyRï   ¦  s    0(&   Rx   t
   __future__R    t   mmtbx.f_modelRO   R   R   R   t   cctbx.millert   scitbx.array_familyR   t   boost.pythont   boostt   pythont
   import_extR   t   libtbxR   R   t   libtbx.utilsR   t   libtbx.philRy   t   philt   parset   omit_map_philt   objectR   RN   Rˆ   RŽ   R3   R   R   Rà   RŠ   R	  Rï   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/composite_omit_map.pyt   <module>   s<   		Š(u	6