ó
³™xTc           @   si  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 m Z d  d l	 m
 Z
 m 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  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 j j d ƒ Z d „  Z d „  Z  e! d k ree  e j" d ƒ n  d S(   iÿÿÿÿ(   t   divisionN(   t   crystal(   t   sgtbx(   t   xray(   t   flex(   t   Sorryt	   multi_out(   t   reflection_file_utils(   t   crystal_symmetry_from_any(   t   pdb(   t   absolute_scaling(   t   f_model(   t   StringIOt
   error_swapc           B   s,   e  Z d  d d d „ Z d „  Z d „  Z RS(   i   i   g      @c   
      C   s@  | |  _  | |  _ | |  _ |  j j |  j  ƒ |  _ |  j j |  j  ƒ |  _ t j |  j  d t ƒ} t j |  j d t ƒ} t j |  j d t ƒ}	 | j j ƒ  |  _	 | j j ƒ  |  _
 |	 j j ƒ  |  _ | j |  _ | j |  _ |	 j |  _ | |  _ | |  _ | |  _ |  j  j d |  j ƒ |  j j |  j  ƒ |  j j |  j  ƒ |  j	 j |  j  ƒ |  j
 j |  j ƒ |  j j |  j ƒ |  j	 j ƒ  j |  j	 ƒ |  _ d  |  _ |  j ƒ  |  j	 j d |  j j ƒ  |  j j ƒ  j ƒ  j ƒ  t j  |  j ƒ d |  j j! ƒ  |  j j ƒ  j ƒ  j ƒ  t j  |  j ƒ ƒ j |  j  ƒ |  _ d  S(   Nt   auto_kernelt   n_binst   datat   sigmas("   t
   miller_obst   miller_calct   miller_mockt
   common_setR
   t   kernel_normalisationt   Truet   normalised_miller_dev_epst	   f_sq_as_ft	   norma_obst
   norma_calct
   norma_mockt   normalizer_for_miller_arrayt   norma_obs_constt   norma_calc_constt   norma_mock_constt   threst   n_reso_binst   n_e_binst   setup_binnert   use_binner_oft	   deep_copyt   set_observation_typet   new_norma_obst   Nonet   new_obst   swap_itt   customized_copyR   t   epsilonst	   as_doubleR   t   sqrtR   (
   t   selfR   R   R   R"   R#   R!   t   norma_obs_objt   norma_calc_objt   norma_mock_obj(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyt   __init__   s<    							
8;c         C   só   xì |  j  j ƒ  j ƒ  D]Õ } |  j  j ƒ  j ƒ  | k } |  j  j | ƒ } |  j j | ƒ } |  j j | ƒ } |  j | j ƒ  | j	 ƒ  | j ƒ  | j ƒ  ƒ \ } } |  j  j
 d |  j  j ƒ  j | | ƒ d |  j  j	 ƒ  j | | ƒ ƒ |  _ q Wd  S(   NR   R   (   R   t   binnert
   range_usedt   bin_indicest   selectR   R   t   do_something_cleverR   R   R,   t   set_selectedR(   (   R0   t   ibint	   selectiont   tmp_norm_obst   tmp_norm_calct   tmp_norm_mockt   new_et   new_se(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyR+   F   s    		c          C   sî  t  j | ƒ } | j ƒ  } | j | ƒ } | j | ƒ } | j | ƒ }	 | j | ƒ }
 t  j |
 ƒ } t  j | ƒ t  j |	 ƒ } | j ƒ  } t | j ƒ  ƒ |  j } t	 | ƒ d } t  j	 ƒ  } d } xH t
 | j ƒ  ƒ D]4 } | | d k rÿ | d 7} n  | j | d ƒ qÜ Wx–t
 |  j ƒ D]…} t  j | | k ƒ } | j t ƒ } t  j t  j | ƒ ƒ } | j | ƒ } | j | ƒ } | j | ƒ } | j | ƒ } t  j | ƒ } t j t  j | | ƒ | | ƒ } t  j t  j | | ƒ |  j | k ƒ } | j t ƒ d k r…| j | ƒ } t  j t  j | j t ƒ ƒ ƒ } | j | ƒ } | d | j t ƒ !} | j | j ƒ  | ƒ } n  | j | | ƒ } | j | | ƒ } q$W| | } t  j | ƒ } | j | ƒ } | j | ƒ } | | f S(   Ni   i    (   R   t   sort_permutationt   inverse_permutationR8   t   logR&   t   floatt   sizeR#   t   intt   xranget   appendt   boolt   countR   t   random_doublet   meant   mathR/   t   absR!   R:   t
   iselectiont   exp(    R0   t   obst   sobst   calct   mockt
   sort_ordert   inverse_sort_ordert
   sorted_obst   sorted_sobst   sorted_calct   sorted_mockt   log_calct   deltast
   old_deltast   bin_sizet   ebinRK   t   iiR;   t   this_bin_selectiont   tmp_nt   permutet   selected_deltast   selected_sobst
   mean_deltat	   std_deltat   outlierst   non_out_deltat
   tmp_permutt	   tmp_deltat   new_fobst   new_sobs(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyR9   X   sR    $)"
(   t   __name__t
   __module__R4   R+   R9   (    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyR      s
   ,	c         C   s_  t  j d d  d d  ƒ } |  d  k	 r? | j d |  d t ƒ } n  | d  k	 rf | j d | d t ƒ } n  | j ƒ  d  k rÌ xQ | D]F } | j ƒ  } | d  k	 r t  j d | d | j ƒ  d t ƒ } Pq q Wn  x& | D] } | j d | d t ƒ } qÓ W| j ƒ  d  k r[xQ | D]F } | j ƒ  } | d  k	 rt  j d | j ƒ  d | d t ƒ } PqqWn  | S(   Nt	   unit_cellt   space_group_infot   other_symmetryt   forcet   assert_is_compatible_unit_cell(   R   t   symmetryR)   t   join_symmetryt   FalseRq   Rr   (   t   from_command_linet   from_parameter_filet   from_coordinate_filest   from_reflection_filest   resultt   crystal_symmetryRq   Rr   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyt   select_crystal_symmetry¡   s<    								s“  simul_utils{
  input{
    unit_cell=None
    .type=unit_cell
    space_group=None
    .type=space_group
    xray_data{
      file_name=None
      .type=path
      labels=None
      .type=str
    }
    model{
      file_name=None
      .type=path
    }
    mock_model{
      file_name=None
      .type=path
    }
  }
  output{
    logfile=simul.log
    .type=str
    hklout=simul.mtz
    .type=str
  }
}
c           C   s	   d GHd  S(   Ns¦  
mmtbx.simulate_data:

Allows one to quickly simulate data 'experimental' data with similar
Fobs-Fcalc distribution as in the given model/data pair.

The keywords are sumarized below and should be self explanatory:

simul_utils{
  input{
    unit_cell=None
    space_group=None
    xray_data{
      file_name=None
      labels=None
    }
    model{
      file_name=None
    }
    mock_model{
      file_name=None
    }
  }
  output{
    logfile=simul.log
    hklout=simul.mtz
  }
}

The main purpose of this file is to generate data with errors that look real.
This is what is done:
1. The pdb file 'model.file_name' is scaled to the observed data 'xray_data.file_name'
   (Fcalc and Fobs are now available and on the same scale)
2. Structure factors are computed for 'mock_model.file_name' with same bulk solvent
   parameters as 'model.file_name' (call this Fmock)
3a. For each resolution bin, generate about 20 E value bins.
3b. In each E value bin, compute ratio=Fobs/Fcalc (or log Fobs - log Fcalc so you will)
3c. make a random permuation of these ratios log differences (call this array random_ratio)
3d. Fmockobs = F_mock*random_ratio
4.  Write out Fmockobs

The data generated in this manner has similar F/sigF (the sigmas are permuted along with the ratios)
and R value properties.

If no mock model is supplied, the model will be the mock model.
The mock model is supposed to be in the same unit cell/spacegroup (this is enforced).

  (    (    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyt
   print_helpæ   s    0c         C   sn  t  |  ƒ d k r t ƒ  nNd |  k r2 t ƒ  n8d |  k rH t ƒ  n"d |  k r^ t ƒ  nt ƒ  } d |  k r� | j d d d t j ƒ n  t ƒ  } t ƒ  } | j d d	 d | ƒ g  } t j d
 d ƒ } | d IJ| d IJ| d IJ| d IJ| d IJ| Jx1|  D])} d  } t
 } t j j | ƒ r•y; t j j d | ƒ } | d  k	 rj| j | ƒ t } n  Wqôt k
 r�‚  qôt k
 r‘qôXn_ y8 | j d | ƒ } | d  k	 rÌ| j | ƒ t } n  Wn$ t k
 rã‚  n t k
 rón X| s| d IJ| d I| IJ| d IJ| Jt d | ƒ ‚ qqWt j d | ƒ }	 |	 j ƒ  }
 t j d |
 j j j j ƒ } t j d |
 j j j j ƒ } t  j! d |
 j j j" d |
 j j j# ƒ } t$ d  | | g | g ƒ } | j" ƒ  |
 j j _" t% j& d | j# ƒ  ƒ |
 j j _# | d IJt j' d |
 ƒ } | j( d | ƒ | d IJ|
 j j j" d  k rht d ƒ ‚ n  |
 j j j# d  k rŒt d ƒ ‚ n  |
 j j j j d  k r³t d ƒ ‚ n  |
 j j j j d  k rÚt d  ƒ ‚ n  t  j! d |
 j j j" d |
 j j j# ƒ } t) j* d! | d" t d# g  ƒ } d  } | j+ d |
 j j j j d$ |
 j j j j, d% t d& d' d( d$ ƒ } | j- ƒ  } | j. ƒ  } | j/ | j0 ƒ  d= k ƒ } | j/ | j1 ƒ  d k ƒ } | j2 ƒ  d  k	 rè| j/ | j2 ƒ  d k ƒ } n  | j3 ƒ  r| j4 ƒ  } n | j5 ƒ  rt6 | ƒ } n  | j7 | ƒ | J| d) IJ| d* IJ| j8 d+ | ƒ | J| j9 ƒ  } t: j d |
 j j j j ƒ j; d! | ƒ } | d, IJ| d- IJ| j8 ƒ  | J| d. IJ| d/ IJ| J| Jt< j= d0 | d1 | d2 | ƒ } | j> d | ƒ t6 | j< ƒ  ƒ j? | ƒ } d  } |
 j j j@ j d  k	 râ| d3 IJ| d4 IJ| J| Jt: j d |
 j j j@ j ƒ j; d! | ƒ } t< j= d0 | d1 | d2 | ƒ } | jA d5 | jB ƒ  d6 | jC ƒ  d7 | jD ƒ  ƒ t6 | j< ƒ  ƒ j? | ƒ } n | jE ƒ  } | d8 IJ| d9 IJ| J| JtF | | | ƒ } | jG } | d: IJ| d- IJ| jH d; d< ƒ } | jI ƒ  jJ d |
 j jK jL ƒ d  S(>   Ni    s   --helps   --hs   -hs   --quiett   labelt   stdoutt   file_objectt
   log_buffert
   home_scopet	   map_coefss   #phil __OFF__s   ======================s             SIMUL       s   A data simulation toolt	   file_namet   args2   ##----------------------------------------------##s   ## Unknown file or keyword:s   Unknown file or keyword: %st   sourcesRq   Rr   t   groups   #phil __ON__t   python_objectt   outs   #phil __END__s   unit cell not specifieds   space group not specifieds   Xray data not specifieds"   pdb file with  model not specifiedR~   t   force_symmetryt   reflection_filest   labelst   ignore_all_zerost   parameter_scopes   simul_utils.input.xray_datat   parameter_names   Summary info of observed datas   =============================t   fs   Atomic model summarys   ====================s   Performing bulk solvent scalings   ===============================t   f_obst   r_free_flagst   xray_structures   Reading in mock models   =====================t   k_solt   b_solt   b_carts   Making new datas   ===============s   Writing new data sett   column_root_labelt   FOBS(   i    i    i    (M   t   lenR€   R   t   registert   sysR‚   R   t   master_paramst!   command_line_argument_interpreterR)   Rx   t   ost   patht   isfilet   iotbxt   philt   parseRI   R   t   KeyboardInterruptt	   Exceptiont   processR   t   fetcht   extractR   t   extract_fromt   simul_utilst   inputt	   xray_dataR‡   t   modelR   Rv   Rq   t   space_groupR   R   Rr   t   formatt   showR   t   reflection_file_servert   get_xray_dataR�   t   infot
   map_to_asuR8   t   indicesR   R   t   is_xray_intensity_arrayR   t   is_complex_arrayRO   t   set_infot   show_summaryt   generate_r_free_flagsR	   t   xray_structure_simpleR   t   managert   update_solvent_and_scaleR'   t
   mock_modelt   updateR—   R˜   R™   R&   R   R*   t   as_mtz_datasett
   mtz_objectt   writet   outputt   hklout(   t   argsRD   t   string_buffert   string_buffer_plotst   phil_objectst   argument_interpreterRˆ   t   command_line_paramst   arg_is_processedt   effective_paramst   paramst   hkl_xst   pdb_xst   phil_xst   combined_xst
   new_paramst   xray_data_servert   miller_arrayR¶   t
   free_flagsR°   t   f_model_objectt   fmodelt
   mockfmodelRÁ   t   mock_f_modelt   new_data_buildert   new_datat   mtz_dataset(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyR­     s<   



					




    

			!

					

!	




		

!				

		

		t   __main__i   (#   t
   __future__R    RN   t   cctbxR   R   R   t   cctbx.array_familyR   t   libtbx.utilsR   R   t
   iotbx.philR¤   R   R   R	   t   mmtbx.scalingt   mmtbxR
   R   t	   cStringIOR   Rž   R¡   t   objectR   R   R¥   R¦   RŸ   R€   R­   Ro   t   argv(    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/examples/simulate_experimental_data.pyt   <module>   s.   Ž	&			3	à