ó
Ö™xTc           @   sR  d  d l  m Z d  d l Z d  d l m Z e j j d ƒ Z d  d l	 Td  d l
 m Z m Z d  d l m Z d  d l Z d  d l Z d  d l 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 a 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 S(   iÿÿÿÿ(   t   divisionN(   t   flext   mmtbx_masks_ext(   t   *(   t   around_atomst   vdw_radii_from_xray_structure(   t   maptbx(   t   Sorry(   t   introspection(   t   adopt_init_args(   t   deepcopyi    s^    use_asu_masks = True
    .type = bool
  solvent_radius = 1.11
    .type = float
  shrink_truncation_radius = 0.9
    .type = float
  grid_step_factor = 4.0
    .type = float
    .help = The grid step for the mask calculation is determined as             highest_resolution divided by grid_step_factor. This is considered             as suggested value and may be adjusted internally based on the             resolution.
  verbose = 1
    .type = int
    .expert_level=3
  mean_shift_for_mask_update = 0.001
    .type = float
    .help = Value of overall model shift in refinement to updates the mask.
    .expert_level=2
  ignore_zero_occupancy_atoms = True
    .type = bool
    .short_caption = Ignore zero-occupancy atoms when calculating bulk solvent mask (recommended)
    .help = Include atoms with zero occupancy into mask calculation
  ignore_hydrogens = True
    .type = bool
    .help = Ignore H or D atoms in mask calculation
  n_radial_shells = 1
    .type = int
    .help = Number of shells in a radial shell bulk solvent model
  radial_shell_width = 1.3
    .type = float
    .help = Radial shell width
t   bulk_solventc           B   sD   e  Z e d d d d  „ Z d d „ Z d „  Z d „  Z d „  Z RS(   c	         C   s‡  t  d 7a  | | g j d  ƒ d k s+ t ‚ | |  _ | d  k rg t j d | j ƒ  d | ƒ j ƒ  } n  | d  k rˆ t	 d |  j ƒ } n  | j
 ƒ  }	 d |  _ t j | j ƒ  j ƒ  t ƒ }
 | rà |
 | j ƒ  j ƒ  d k M}
 n  | rú |
 | j ƒ  M}
 n  |	 j |
 ƒ }	 | j |
 ƒ } |
 j t ƒ |  _ t j |  d | j ƒ  d | j ƒ  j ƒ  d |	 d | d	 | d
 | d | ƒt j ƒ  j ƒ  d  S(   Ni   t	   unit_cellt   stept   xray_structurei    t   space_group_order_zt
   sites_fract
   atom_radiit   gridding_n_realt   solvent_radiust   shrink_truncation_radius(   t   number_of_mask_calculationst   countt   Nonet   AssertionErrorR   R   t   crystal_griddingR   t   n_realR   R   t   n_atoms_excludedR   t   boolt
   scattererst   sizet   Truet   extract_occupanciest   hd_selectiont   selectt   FalseR   t   __init__t   space_groupt   order_zR   t   virtual_memory_infot
   update_max(   t   selfR   t   ignore_zero_occupancy_atomsR   R   t   ignore_hydrogen_atomsR   t	   grid_stepR   R   t	   selection(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR$   9   s:    

!			c            sÁ  ˆ  d  k r t j ‰  n  ˆ  d IJd |  j } d |  j } d |  j j ƒ  j ƒ  } |  j j	 ƒ  j
 ƒ  } d t | ƒ } d d j g  t |  j j ƒ  j ƒ  d  | ƒ D] \ } } d	 | | ^ q¦ ƒ }	 d
 |  j d }
 t t | ƒ t | ƒ t | ƒ ƒ } | d | t | ƒ 7} | d | t | ƒ 7} | d | t | ƒ 7} ‡  f d †  } d } | d | d | | ƒ | d | d | |	 ƒ | d | d | |
 ƒ ˆ  d d d d IJd  S(   NsO   |- mask summary --------------------------------------------------------------|s"   solvent radius:            %4.2f As"   shrink truncation radius:  %4.2f As   number of atoms: %ds   gridding:   %ss   grid steps: (%s) As   , i   s   %.2fs"   estimated solvent content: %.1f %%id   t    c      	      s1   ˆ  d |  d t  d d t |  ƒ ƒ d IJd  S(   Ns   | R.   i    iK   s    |(   t   maxt   len(   t   s(   t   out(    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt	   show_lines   s    i   R1   t   |t   -iM   (   R   t   syst   stdoutR   R   R   R   R   t   datat   accessort   focust   strt   joint   zipR   t
   parameterst   contact_surface_fractionR/   R0   (   R)   R2   t   srt   stt   naR   t   grt   lt   nt   gst   scR3   t   gap(    (   R2   sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   show_summarya   s.     
+$c         C   s�   t  j j j d |  j j ƒ  d d d |  j j ƒ  ƒ } t  j j j d | d t d d g d	 |  j j	 ƒ  d
 | d |  j j
 ƒ  d d d d ƒ d  S(   NRE   t   firsti    t   lastt	   file_namet
   is_p1_cellt   title_liness    REMARKS Mask map""R   t   griddingR8   t   averageiÿÿÿÿt   standard_deviation(   i    i    i    (   t   iotbxt   xplort   mapRO   R8   R:   t   writerR   R   R   t	   as_double(   R)   RL   RO   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   mask_as_xplor_map{   s    	c      	   C   s=   | j  d |  j d t d t d t ƒ } | j d | j ƒ  ƒ S(   NRT   t	   use_scalet   anomalous_flagt   use_sgR8   (   t   structure_factors_from_mapR8   R   R#   t   array(   R)   t
   miller_sett   result(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   structure_factors‰   s    	c         C   s>   | j  ƒ  |  j j  ƒ  k s! t ‚ |  j j | d k d ƒ d  S(   Ni    (   R9   R8   R   t   set_selected(   R)   t   non_uniform_mask(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt,   subtract_non_uniform_solvent_region_in_placeŽ   s    !N(	   t   __name__t
   __module__R   R   R$   RI   RW   R_   Rb   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR   7   s    		t   asu_maskc           B   s#   e  Z d d d d  „ Z d „  Z RS(   c   	      C   sL  t  |  t ƒ  ƒ | | g j d  ƒ d k s1 t ‚ |  j d  k rR t j ƒ  |  _ n  t d |  j	 ƒ |  _
 | d  k	 rÍ t d |  j	 j ƒ  d |  j	 j ƒ  d |  j d |  j j d |  j j d |  j j ƒ |  _ nK t d |  j	 j ƒ  d	 |  j	 j ƒ  d
 |  j d |  j j d |  j j ƒ |  _ t j |  j	 j ƒ  j ƒ  t ƒ } |  j j rg| |  j	 j ƒ  j ƒ  d k M} n  |  j j rŠ| |  j	 j ƒ  M} n  |  j	 j ƒ  } | j | ƒ } |  j
 j | ƒ } |  j j d k r5|  j j  g |  j j d } | d c |  j j 8<| d d k  rd | d <n  |  j j! | | | ƒ n |  j j! | | ƒ d  S(   Ni   R   R   t   groupt
   resolutiont   grid_step_factorR   R   R%   R   i    g        ("   R	   t   localsR   R   R   t   mask_paramst   mask_master_paramst   extractR   R   R   t	   atom_maskR   R%   t   d_minRh   R   R   Re   R   R   R   R   R   R   R*   R    t   ignore_hydrogensR!   R   R"   t   n_radial_shellst   radial_shell_widtht   compute(	   R)   R   Rn   R   Rj   R-   R   R   t
   shell_rads(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR$   “   sH    !		!"c         C   sA   |  j  j ƒ  |  j j ƒ  j ƒ  } t j | t j | j	 ƒ  ƒ ƒ S(   N(
   Re   t   mask_data_whole_ucR   R%   R&   R   t   copyR   t   gridR:   (   R)   t	   mask_data(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyRt   ¾   s    N(   Rc   Rd   R   R$   Rt   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyRe   ’   s   +t   managerc           B   sk   e  Z d	 d	 d	 e d  „ Z d „  Z d „  Z d „  Z d	 e d „ Z	 d „  Z
 d „  Z d „  Z d „  Z RS(
   c         C   sB  t  |  t ƒ  ƒ |  j d  k	 r+ | |  _ n t j ƒ  |  _ |  j ƒ  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  |  _ d  |  _ | d  k	 rø t d |  j ƒ |  _ |  j j ƒ  |  _ |  j j ƒ  |  _ t } |  j d  k	 râ t } n  | rø |  j ƒ  qø n  |  j	 d  k	 r>|  j	 d j ƒ  j ƒ  |  j j ƒ  j ƒ  k s>t ‚ n  d  S(   NR   i    (   R	   Ri   Rj   R   Rk   Rl   t   _get_grid_stepR,   R   t   _f_maskt   _f_mask_twint   solvent_content_via_maskt   layer_volume_fractionst
   sites_cartR   R   t   deep_copy_scatterersR#   t   miller_array_twinR   t   compute_f_maskR8   R   t   miller_arrayt   indicesR   (   R)   R‚   R   R€   Rj   t   compute_maskt   twin(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR$   Ä   s.     						 	 c         C   s(   |  j  t j |  j j ƒ  j ƒ  t ƒ ƒ S(   N(   R"   R   R   R‚   Rƒ   R   R   (   R)   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt	   deep_copyÞ   s    c         C   sˆ  d  } |  j d  k	 r* |  j j | ƒ } n  t d |  j j | ƒ d | d |  j d t |  j ƒ d t ƒ } |  j	 d  k	 r-|  j	 d j
 ƒ  j ƒ  |  j j ƒ  j ƒ  k s¬ t ‚ g  | _	 xu |  j	 D]g } | j
 ƒ  j ƒ  | j ƒ  k s
t | j
 ƒ  j ƒ  d | j ƒ  f ƒ ‚ | j	 j | j d | ƒ ƒ q¿ Wn  |  j d  k	 rxg  | _ x0 |  j D]" } | j j | j d | ƒ ƒ qOWn  |  j | _ | S(	   NR‚   R€   R   Rj   R„   i    s   !=R-   (   R   R€   R"   Rx   R‚   R   R
   Rj   R#   Rz   R8   R   Rƒ   R   t   appendR{   R|   (   R)   R-   R€   t   new_managert   fm(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR"   á   s*    		4	E#	#c         C   s~   |  j  j d k s% t d d ƒ ‚ n  |  j  j d k s= t ‚ |  j j ƒ  |  j  j } | d k  rk d } n  t d | ƒ } | S(   Ni    s2   Inappropriate value for grid_step_factor: must be s   positive and non-zero.g333333Ã?gš™™™™™é?(   Rj   Rh   R   R   R‚   Rn   t   min(   R)   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyRy   ø   s     	c         C   sÊ   | d  k	 r˜ | s! |  j d  k rL | j ƒ  |  _ | j ƒ  |  _ |  j ƒ  qÃ |  j d | j ƒ  ƒ } | rÃ | j ƒ  |  _ | j ƒ  |  _ |  j ƒ  qÃ n+ |  j d  k rÃ |  j d  k	 rÃ |  j ƒ  n  |  j S(   Nt   sites_cart_new(   R   Rz   R   R   R~   R�   t   _need_update_mask(   R)   R   t   force_updatet   flag(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   shell_f_masks  s    	c         C   s   |  j  S(   N(   R{   (   R)   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   shell_f_masks_twin  s    c         C   s�   |  j  d  k	 r. |  j  j ƒ  | j ƒ  k r. t S|  j  d  k	 rˆ t j | |  j  j ƒ  ƒ } t j | d ƒ } | |  j j	 k r� t St
 Sn t Sd  S(   Ni    (   R~   R   R   R   R   t   sqrtt   dott   mean_defaultRj   t   mean_shift_for_mask_updateR#   (   R)   R‹   t   atom_atom_distancest
   mean_shift(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyRŒ     s     c         C   s  |  j  j s‡ |  j  j d k s$ t ‚ |  j ƒ  } | j d |  j ƒ g |  _ |  j d  k	 rx | j d |  j ƒ g |  _
 n  | j |  _ n]t d |  j d |  j j ƒ  d |  j  ƒ j } g  |  _ xa t d |  j  j ƒ D]J } | j |  j j ƒ  | d ƒ } |  j j |  j j ƒ  j d | ƒ ƒ qÓ W|  j d  k	 rÉ|  j j ƒ  j ƒ  |  j j ƒ  j ƒ  k s`t ‚ g  |  _
 x] t d |  j  j ƒ D]C } | j |  j j ƒ  ƒ } |  j
 j |  j j ƒ  j d | ƒ ƒ qWn  | j |  _ | j ƒ  |  _ |  j d j ƒ  j ƒ  |  j j ƒ  j ƒ  k st ‚ d  S(   Ni   R]   R   Rn   Rj   i    R8   (   Rj   t   use_asu_masksRp   R   t   bulk_solvent_maskR_   R‚   Rz   R€   R   R{   R?   R|   Re   R   Rn   t   rangeRƒ   R‡   t   setR\   R   R}   R8   (   R)   t   bulk_solvent_mask_objt   asu_mask_objt   it   fm_asu(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR�   #  s6    				)	,c         C   sF   |  j  } t d |  j d |  j d | j d | j d | j d | j ƒ S(   NR   R,   R*   R+   R   R   (   Rj   R   R   R,   R*   Ro   R   R   (   R)   t   mp(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR˜   B  s    						N(   Rc   Rd   R   R   R$   R†   R"   Ry   R#   R�   R�   RŒ   R�   R˜   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyRx   Ã   s   			
			t   mask_from_xray_structurec           B   s   e  Z d  „  Z RS(   c   	      C   sÀ   | } | r! | j  d t ƒ } n  t d | ƒ } t j j d | j ƒ  d | j ƒ  d | d | d | ƒ |  _ |  j j	 | j
 ƒ  | ƒ |  j j ƒ  |  _ | r¼ |  j | j ƒ  j ƒ  |  _ n  d  S(   Nt   sites_mod_positiveR   R   R%   R   R   R   (   t   expand_to_p1R   R   t   mmtbxt   masksRm   R   R%   Re   Rr   R   Rt   Rw   R&   (	   R)   R   t   p1R   R   t   for_structure_factorsR   t   xrsR   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR$   M  s     (   Rc   Rd   R$   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyR    L  s   (%   t
   __future__R    t   boost.pythont   boostt   cctbx.array_familyR   t   pythont
   import_extt   extR   t   cctbx.masksR   R   t   cctbxR   R6   t   iotbx.xplor.mapRR   t
   iotbx.philt   libtbx.utilsR   t   libtbxR   R	   Ru   R
   t   mmtbx.masksR£   R   t   philt   parseRk   R   t   objectRe   Rx   R    (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/masks/__init__.pyt   <module>   s*   
	!	[1‰