ó
Û™xTc           @   s  d  d l  m Z d  d l Z d  d l 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 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 Z d Z d Z e d Z e e Z d Z e d Z e e Z  d e e f Z! d e e  f Z" d „  Z# d e j$ j% e j$ j& e e f Z' e' Z( d „  Z) d „  Z* d d d „  ƒ  YZ+ d e, f d „  ƒ  YZ- d „  Z. d d d d d „ Z0 d d d d d d „ Z1 d e, f d „  ƒ  YZ2 e3 d e4 d „ Z5 d S(   iÿÿÿÿ(   t   divisionN(   t   flex(   t   Sorryt   date_and_time(   t   adopt_init_args(   t   show_string(   t   ifloort   iceil(   t	   map_tools(   t   miller(   t   maptbxsÿ    map_coefficients
    .multiple = True
    .short_caption = Map coefficients
    .style = auto_align
  {
    map_type = None
      .type = str
      .style = bold renderer:draw_map_type_widget
    format = *mtz phs
      .type = choice(multi=True)
    mtz_label_amplitudes = None
      .type = str
      .short_caption = MTZ label for amplitudes
      .style = bold
    mtz_label_phases = None
      .type = str
      .short_caption = MTZ label for phases
      .style = bold
    kicked = False
      .type = bool
      .short_caption = Kicked map
    fill_missing_f_obs = False
      .type = bool
      .short_caption = Fill missing F(obs) with F(calc)
    acentrics_scale = 2.0
      .type = float
      .help = Scale terms corresponding to acentric reflections (residual maps only: k==n)
      .expert_level = 2
    centrics_pre_scale = 1.0
      .type = float
      .help = Centric reflections, k!=n and k*n != 0:               max(k-centrics_pre_scale,0)*Fo-max(n-centrics_pre_scale,0)*Fc
      .expert_level = 2
    sharpening = False
      .type = bool
      .help = Apply B-factor sharpening
      .short_caption = Apply B-factor sharpening
      .style = bold
    sharpening_b_factor = None
      .type = float
      .help = Optional sharpening B-factor value
      .short_caption = Sharpening B-factor value (optional)
    exclude_free_r_reflections = False
      .type = bool
      .help = Exclude free-R selected reflections from output map coefficients
      .short_caption = Exclude R-free set from map coefficients
    isotropize = True
      .type = bool
    dev
      .expert_level=3
    {
      complete_set_up_to_d_min = False
        .type = bool
      aply_same_incompleteness_to_complete_set_at = randomly low high
        .type = choice(multi=False)
    }
    %s
  }
sÚ   
ncs_average = False
  .type = bool
  .expert_level = 2
  .help = Perform NCS averaging on map using RESOLVE (without density       modification).  Will be ignored if NCS is not present.
  .short_caption = NCS average
t    s    map
    .short_caption = XPLOR or CCP4 map
    .multiple = True
    .style = auto_align
  {
    map_type = None
      .type = str
      .expert_level=0
      .style = bold renderer:draw_map_type_widget
    format = xplor *ccp4
      .type = choice
      .short_caption = File format
      .caption = XPLOR CCP4
      .style = bold
    file_name = None
      .type = path
      .style = bold new_file
    kicked = False
      .type = bool
      .expert_level=0
    fill_missing_f_obs = False
      .type = bool
      .expert_level=0
    grid_resolution_factor = 1/4.
      .type = float
      .expert_level=0
    scale = *sigma volume
      .type = choice(multi=False)
      .expert_level=2
    region = *selection cell
      .type = choice
      .caption = Atom_selection Unit_cell
      .short_caption=Map region
    atom_selection = None
      .type = atom_selection
      .short_caption = Atom selection
    atom_selection_buffer = 3
      .type = float
    acentrics_scale = 2.0
      .type = float
      .help = Scale terms corresponding to acentric reflections (residual maps only: k==n)
      .expert_level=2
    centrics_pre_scale = 1.0
      .type = float
      .help = Centric reflections, k!=n and k*n != 0:               max(k-centrics_pre_scale,0)*Fo-max(n-centrics_pre_scale,0)*Fc
      .expert_level=2
    sharpening = False
      .type = bool
      .help = Apply B-factor sharpening
      .short_caption = Apply B-factor sharpening
      .style = bold
    sharpening_b_factor = None
      .type = float
      .help = Optional sharpening B-factor value
      .short_caption = Sharpening B-factor value (optional)
    exclude_free_r_reflections = False
      .type = bool
      .help = Exclude free-R selected reflections from map calculation
    isotropize = True
      .type = bool
    %s
  }
s   %s
%s
c           C   s   t  j j t d t ƒS(   Nt   process_includes(   t   iotbxt   philt   parset   map_and_map_coeff_params_strt   False(    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   map_and_map_coeff_master_params®   s    sð  maps {
  input {
    pdb_file_name = None
      .type = path
      .optional = False
      .short_caption = Model file
      .style = bold file_type:pdb input_file
    reflection_data {
      %s
      r_free_flags {
        %s
      }
    }
  }
  output {
    directory = None
      .type = path
      .short_caption = Output directory
      .help = For GUI only.
      .style = bold output_dir noauto
    prefix = None
      .type = str
      .input_size = 100
      .short_caption = Output prefix
      .style = bold noauto
    title = None
      .type = str
      .short_caption = Job title
      .input_size = 400
      .style = noauto
    fmodel_data_file_format = mtz
      .optional=True
      .type=choice
      .help=Write Fobs, Fmodel, various scales and more to MTZ file
    include_r_free_flags = False
      .type = bool
      .short_caption = Include R-free flags in output MTZ file
  }
  scattering_table = wk1995  it1992  *n_gaussian  neutron electron
    .type = choice
    .help = Choices of scattering table for structure factors calculations
  wavelength = None
    .type = float(value_min=0.2, value_max=10.)
    .input_size = 80
    .help = Optional X-ray wavelength (in Angstroms), which will be used to       set the appropriate anomalous scattering factors for the model.  This       will only affect the LLG map from Phaser.
  bulk_solvent_correction = True
    .type = bool
  anisotropic_scaling = True
    .type = bool
  skip_twin_detection = False
    .type = bool
    .short_caption = Skip automatic twinning detection
    .help = Skip automatic twinning detection
  omit {
    method = *simple
      .type = choice(multi=False)
    selection = None
      .type = str
      .short_caption = Omit selection
      .input_size = 400
  }
  %s
  %s
}
c           C   s   t  j j t d t ƒS(   NR   (   R   R   R   t   maps_including_IO_params_strt   True(    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   maps_including_IO_master_paramsü   s    c         C   sG   d |  j  ƒ  |  j  ƒ  |  j  ƒ  |  j |  j f } t j j | d t ƒS(   Nsº       map_coefficients
    {
      format = *mtz phs
      mtz_label_amplitudes = %s
      mtz_label_phases = P%s
      map_type = %s
      kicked = %s
      fill_missing_f_obs = %s
    }
R   (   t   formatt   kickedt   f_obs_filledR   R   R   R   (   t   map_type_objt   map_coeff_params_str(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   cast_map_coeff_paramsÿ   s    t   map_coeffs_mtz_label_managerc           B   s&   e  Z d  „  Z d „  Z d d „ Z RS(   c         C   sh   | j  |  _ | j |  _ |  j d  k r< t | j ƒ |  _ n  |  j d  k rd d t | j ƒ |  _ n  d  S(   Nt   PH(   t   mtz_label_amplitudest   _amplitudest   mtz_label_phasest   _phasest   Nonet   strt   map_type(   t   selft
   map_params(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   __init__  s      c         C   s   |  j  S(   N(   R   (   R%   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt
   amplitudes  s    c         C   s    | d  k s | s t ‚ |  j S(   N(   R"   t   AssertionErrorR!   (   R%   t
   root_labelt   anomalous_sign(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   phases  s    N(   t   __name__t
   __module__R'   R(   R"   R,   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyR     s   		t   write_xplor_map_filec           B   s)   e  Z d d d  „ Z d „  Z d „  Z RS(   c      	   C   s½  t  |  t ƒ  ƒ | j d |  j j ƒ } |  j j d k rG | j ƒ  n% |  j j d k rf | j ƒ  n t ‚ d t	 t
 j j |  j j ƒ ƒ g } | j d t	 t
 j j |  j j ƒ ƒ ƒ | j d t ƒ  ƒ |  j j d k sæ t ‚ |  j j d k r>| d  k	 r>d  } | d  k	 r%|  j ƒ  } n  |  j d	 | d
 |  j j ƒ \ } }	 | j ƒ  }
 g  t | |
 ƒ D] \ } } t | | ƒ ^ qe} g  t |	 |
 ƒ D] \ } } t | | ƒ ^ q—} | j d ƒ | j d t	 |  j j ƒ ƒ | j d |  j j ƒ | d  k r|  j j ƒ  j ƒ  } n | j ƒ  } | j d | ƒ n d  } d  } | j d ƒ | j d k r‘| j  d |  j j d | d | d | ƒ n( | j! d |  j j d | d | d | ƒ d  S(   Nt   resolution_factort   volumet   sigmas   REMARK file: %ss   REMARK directory: %ss	   REMARK %st	   selectiont   cellt
   iselectiont   buffers   REMARK map around selections   REMARK   atom_selection=%ss#   REMARK   atom_selection_buffer=%.6gs%   REMARK   number of atoms selected: %ds!   REMARK map covering the unit cellt   xplort	   file_namet   title_linest   gridding_firstt   gridding_lastt   labels(   s	   selectionR4   ("   R   t   localst   fft_mapt   paramst   grid_resolution_factort   scalet   apply_volume_scalingt   apply_sigma_scalingt   RuntimeErrorR   t   ost   patht   basenameR8   t   appendt   dirnameR   t   regionR)   R"   t   atom_iselectiont   box_around_selectiont   atom_selection_buffert   n_realt   zipR   R   t   atom_selectiont   xray_structuret
   scattererst   sizeR   t   as_xplor_mapt   as_ccp4_map(   R%   R?   t   coeffst   atom_selection_managerRQ   R>   R9   t   map_iselectiont   frac_mint   frac_maxRN   t   ft   nR:   R;   t   sel_size(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyR'     s^    	  "	 	22			
	c         C   sL   |  j  j ƒ  } | d  k	 r- | j | ƒ } n  |  j  j ƒ  j d | d | ƒ S(   Nt
   sites_cartR6   (   RQ   R^   R"   t   selectt	   unit_cellt   box_frac_around_sites(   R%   R5   R6   R^   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyRL   R  s
    c         C   s¹   |  j  j d k s$ |  j  j d  k r( d  Sy% |  j j d |  j  j ƒ j ƒ  } Wn: t k
 rc ‚  n' t k
 r‰ t	 d |  j  j ƒ ‚ n X| j
 ƒ  d k rµ t	 d |  j  j ƒ ‚ n  | S(   NR3   t   strings   Invalid atom selection: %si    s   Empty atom selection: %s(   R?   RJ   RP   R"   RW   R3   R5   t   KeyboardInterruptt	   ExceptionR   RS   (   R%   t   result(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyRK   Y  s    $ N(   R-   R.   R"   R'   RL   RK   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyR/     s   2	c         C   sê  d d l  m } |  j ƒ  j d |  j ƒ j ƒ  } t | d ƒ rà| j j rà|  j j	 d |  j ƒ  j
 ƒ  ƒ j ƒ  } d | j ƒ  j ƒ  | j ƒ  j ƒ  } | j ƒ  j ƒ  | j ƒ  j ƒ  } | j j d k rý t j | j ƒ  j ƒ  | ƒ } | j | ƒ } qæ| j j d k rj| j ƒ  } | j d	 | d
 | j ƒ  | d d | j ƒ  ƒ j d | j ƒ  | d ƒ } qæ| j j d k ræ| j d t ƒ } | j d	 | d
 | j ƒ  | d d | j ƒ  ƒ j d | j ƒ  | d ƒ } qæn | } | S(   Niÿÿÿÿ(   R	   RQ   t   devt   d_ming      ð?t   randomlyt   lowt   crystal_symmetryt   indicesi   t   anomalous_flagt   datat   hight   reverse(   t   cctbxR	   t   f_obst!   structure_factors_from_scatterersRQ   t   f_calct   hasattrRf   t   complete_set_up_to_d_mint   structure_factorsRg   Rm   RS   t+   aply_same_incompleteness_to_complete_set_atR   t   random_boolR_   t   sortt   setRk   Rl   t   arrayR   (   t   fmodelR?   R	   t   coeffs_partial_setRV   t   frac_inct   n_misst   sel(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   compute_f_calcf  s6    &"		c         C   sÏ  | | g j  d  ƒ d k s! t ‚ d d l m } d d  l } d d l m } | j d |  j ƒ } | j	 d k r« t
 | j ƒ d k r« | d  k rž | j } n  t | |  ƒ S| d  k	 rÐ | j ƒ  rÐ | j rÐ d  S| d  k	 rô | j ƒ  }	 | j }
 n | }	 | j j }
 d  } |  j s×|	 j d |  j d |  j d	 |  j d
 |  j d |  j d |  j d t |  d t ƒ d | d | d t ƒ 
} | d  k r‹d  S|  j rjd d l m } | j d |
 j ƒ  d | d |  j ƒ \ } } qjn“ |  j j  d ƒ d k rj| d  k r| j } n  | j  ƒ  j! d | j  ƒ  j" ƒ  d t# j$ d d ƒ } | j% j& j' d |	 j d | d |  j ƒ j( } n  | d  k r°t) d |  j |  j |  j |  j | j  ƒ  j* ƒ  f ƒ ‚ n  | j* ƒ  rË| j+ ƒ  } n  | S(   Ni   iÿÿÿÿ(   R   (   R	   t   map_name_stringi    R$   t   acentrics_scalet   centrics_pre_scalet   fill_missingt
   isotropizet   exclude_free_r_reflectionst   ncs_averaget   post_processing_callbackt   pdb_hierarchyt   merge_anomaloust
   sites_fract
   map_coeffst   b_sharpt   anomRg   t   symmetry_flagsR0   g      Ð?R|   t   crystal_griddingsf   Map coefficient generation failed (map_type=%s, kicked=%s, sharpening=%s, isotropize=%s, anomalous=%s.(,   t   countR"   R)   t   mmtbxR   Rp   R	   t	   map_namesR$   t   kt   absR\   R|   R�   t   is_twin_fmodel_managert   phaser_sad_llgt   electron_density_mapRQ   R   t   map_coefficientsRƒ   R„   t   fill_missing_f_obsR†   R‡   t   getattrR   R   t
   sharpeningt	   sharp_mapRŒ   t   sharpening_b_factorRq   R‘   Rg   R
   t   use_space_group_symmetryt   mapst   kickt   runt	   mc_resultRD   Rl   t   average_bijvoet_mates(   R?   R|   t   map_calculation_serverR‰   RŠ   R   R“   R	   t   mnmt	   e_map_objt   xrsRV   RŽ   R‘   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   map_coefficients_from_fmodel„  sr    !$										 					c      
   C   sl  | d  k s! t | d ƒ s! t ‚ g  } x>| D]6} | j d  k	 r. t d |  d | d | d | ƒ }	 |	 d  k r† t d | j ƒ ‚ n  | j d  k r/d }
 | d  k	 r° | }
 n  | d  k	 rì t |
 ƒ d k rß |
 d	 | }
 qì |
 | }
 n  | j d
 k rd } n d } |
 d	 | j d | }
 |
 | _ n  t	 d | d |	 d | d |  j
 ƒ | j | j ƒ q. q. W| S(   Nt   __call__R|   R?   R‰   RŠ   s    Couldn't generate map type '%s'.R   i    t   _R7   s   .xplors   .ccp4t   _mapRV   RW   RQ   (   R"   Rt   R)   R$   Rª   R   R8   t   lenR   R/   RQ   RH   (   R|   R?   RW   t   file_name_prefixt   file_name_baseR‰   RŠ   t   output_filest   mpRV   t   output_file_namet   ext(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   compute_xplor_mapsÈ  s:    	 		
t   compute_map_coefficientsc           B   s/   e  Z d d d e j d  „ Z d d d „ Z RS(   c      
   C   sÞ  | d  k s! t | d ƒ s! t ‚ | |  _ d  } | j ƒ  } g  |  _ x’| D]Š}	 |	 j d  k	 rL | j ƒ  r‘ |	 j r‘ t	 d d d d ƒ ‚ n  | j ƒ  r² |	 j
 r² t |	 _
 n  t d | d |	 d | d	 | ƒ } d
 |	 j k rƒ| d  k	 rƒ| j ƒ  r	| j ƒ  } n  t d |	 ƒ }
 |  j d  k rK| j d |
 j ƒ  d |
 ƒ |  _ n% |  j j d | d |
 j ƒ  d |
 ƒ |  j j | ƒ qÖ| d  k rÖ|	 j d k r´| j ƒ  j ƒ  r´qÓt j d d d |	 j ƒ qÖqL qL Wd  S(   NR«   s9   Kicked maps are not supported when twinning is present.  s@   You can disable the automatic twin law detection by setting the s9   parameter maps.skip_twin_detection to True (or check the s%   corresponding box in the Phenix GUI).R¦   R?   R‰   RŠ   t   mtzR&   t   column_root_labelt   label_decoratort   miller_arrayt	   anomalouss2   Map coefficients not available for map type '%s'; s?   usually means you have requested an anomalous map but supplied s=   merged data, or indicates a twinning-related incompatibility.(   R"   Rt   R)   t   mtz_datasetR™   R�   R$   R—   R   R   R†   R   Rª   R   Rl   R¥   R   t   as_mtz_datasetR(   t   add_miller_arrayRH   Rq   t   libtbxt   warn(   R%   R|   R?   R¼   R‰   RŠ   t   logRV   R¦   t   mcpt   lbl_mgr(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyR'   ð  sP    				c         C   sÀ   d d l  m } |  j d  k	 r¼ | d  k	 rD |  j j | d d ƒn  | d  k r_ | j ƒ  } n  | j t ƒ  ƒ | j d t j	 j
 | ƒ ƒ |  j j ƒ  } | j | ƒ | j d | ƒ t St S(   Niÿÿÿÿ(   R   R¸   t
   FreeR_flags   > file name: %sR8   (   t   cctbx.array_familyR   R¼   R"   R¾   t
   std_stringRH   R   RE   RF   RG   t
   mtz_objectt   add_historyt   writeR   R   (   R%   R8   t   mtz_history_buffert   r_free_flagsR   RÇ   (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   write_mtz_file&  s    
N(   R-   R.   R"   t   syst   stdoutR'   RÌ   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyR¶   î  s   0c         C   sl  d t  j |  j ƒ  j ƒ  ƒ d } | d  k rd  } d } xÜ t d d d ƒ D]Å } d t  j | | ƒ } |  j ƒ  j d |  j ƒ  | ƒ } | j	 d d	 ƒ }	 |	 j
 ƒ  |	 j ƒ  }
 t j |
 ƒ } | j ƒ  } | | k rì | } | } n  | rN d
 | | j ƒ  | j ƒ  f GHqN qN Wn  | r,d G| GHn  d t  j | | ƒ } | rN| S|  j d |  j ƒ  | ƒ Sd  S(   Ng      ð?g      @iüÿÿiœÿÿÿid   i   Rm   R0   g      Ð?s.   b_sharp: %6.1f skewness: %6.4f kurtosis: %6.4fs   Best sharpening B-factor:(   R   t   pow2t
   d_spacingsRm   R"   t   ranget   expt	   deep_copyt   customized_copyR>   RC   t   real_map_unpaddedR
   t   more_statisticst   kurtosist   skewness(   R�   t   showt   b_sharp_bestt   b_onlyt   sst   kurtRŽ   t   k_sharpt   map_coeffs_R>   t   map_datat   ot   kurt_(    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt0   b_factor_sharpening_by_map_kurtosis_maximization7  s2    #
	"  (    (6   t
   __future__R    t   mmtbx.utilsR“   t
   iotbx.philR   t   scitbx.array_familyR   t   libtbx.utilsR   R   R¿   R   t   libtbx.str_utilsR   t   libtbx.math_utilsR   R   t   libtbx.callbacksRE   RÍ   R   Rp   R	   R
   t   mmtbx.maps.kickt   map_coeff_params_base_strt   ncs_average_param_strR   t   map_coeff_params_ncs_strt   map_params_base_strt   map_params_strt   map_params_ncs_strR   t    map_and_map_coeff_params_ncs_strR   t   utilst   data_and_flags_str_part1t   data_and_flags_str_part2R   t   master_paramsR   R   R   t   objectR/   R�   R"   Rª   Rµ   R¶   R   R   Rã   (    (    (    sA   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/__init__.pyt   <module>   s\   =	

B

	F	
		I	 BI