ó
±™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	 d  d l
 m Z d  d l m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z e j j d d	 e ƒZ e d
 „ Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d e d „ Z d „  Z i d d 6d d 6d d 6Z  d e d „ Z! e e" d „ Z# d „  Z$ d e f d „  ƒ  YZ% d  e f d! „  ƒ  YZ& d" e f d# „  ƒ  YZ' e( d$ k rÂe e j) d% ƒ n  d S(&   iÿÿÿÿ(   t   divisionN(   t   make_header(   t   ifloor(   t   Sorry(   t   easy_pickle(   t   adopt_init_argss   
simulate_data {
  pdb_file = None
    .type = path
    .help = Model file.  If no reflections file is given, data will be       generated starting from F(model).  Otherwise it will only be used       to report scaling statistics.
  hkl_file = None
    .type = path
    .help = Data file.  If defined, the extracted amplitudes will be       truncated.  Any R-free flags will be propagated to the output file.
  data_label = None
    .type = str
    .help = Label for amplitudes or intensities in hkl_file (optional).
  d_min = 3.0
    .type = float
    .help = Resolution cutoff of output data.
  random_seed = 90125714
    .type = int
  output_file = None
    .type = path
  write_modified_pdb = True
    .type = bool
  r_free_flags {
    file_name = None
      .type = path
      .help = File containing R-free flags.  Can be left blank if hkl_file is         defined and contains R-free flags.
    label = None
      .type = str
      .help = Label for R-free flags in r_free_file (or hkl_file).
    fraction = 0.05
      .type = float
      .help = Percent of reflections to flag for R-free (ignored if already         available).
    missing_flags = *extend discard
      .type = choice(multi=False)
      .help = Handling of reflections for which R-free flags are not present.
  }
  crystal_symmetry {
    space_group = None
      .type = space_group
      .help = Space group of output data.  Ignored if reflections are used         as input.
    unit_cell = None
      .type = unit_cell
      .help = Unit cell of output data.  Ignored if reflections are used as         input.
  }
  modify_pdb {
    remove_waters = True
      .type = bool
      .help = Strip waters from input model.
    remove_alt_confs = False
      .type = bool
      .help = Strip alternate sidechains (and reset occupancy to 1).
    convert_to_isotropic = False
      .type = bool
      .help = Convert all atoms to isotropic before calculating F(model).
    set_mean_b_iso = None
      .type = float
      .help = Scale atomic B-factors to have this mean value.
    set_wilson_b = False
      .type = bool
      .help = Scale atomic B-factors to have a mean equal to the mean         Wilson B-factor for this resolution (+/- 0.2A)
  }
  truncate
    .help = Options for data truncation at lower resolution.
  {
    add_b_iso = None
      .type = float
      .help = Isotropic B-factor to be added to data.
    add_b_aniso = 0 0 0 0 0 0
      .type = floats(size=6)
      .help = Anisotropic B-factor to be added to data.  Severely anisotropic         data might have an anisotropic B of 80,80,200,0,0,0.
    apply_b_to_sigmas = True
      .type = bool
      .help = If True, the B-factor scaling will be done on experimental         sigmas (if present) as well as amplitudes.
    add_random_error_percent = None
      .type = float
      .help = Adds random noise as a percentage of amplitude, evenly across         all resolutions.  This is probably inferior to the sigma-based         noise generation.
    remove_cone_around_axis = None
      .type = float
      .help = Radius in degrees of cone of missing data around the axis of         rotation (data collection pathology).  If specified, axis_of_rotation         must be defined.
    axis_of_rotation = None
      .type = ints(size=3)
      .help = Axis of rotation (as Miller indices, i.e. three integers) of         crystal during data collection.  Only used if remove_cone_around_axis         is defined.
    elliptical_truncation = False
      .type = bool
      .help = Truncate data anisotropically, using the overall anisotropic         B-factor plus ellipse_scale to set the ellipse dimensions.
    ellipse_scale = 1.0
      .type = float
    ellipse_target_completeness = None
      .type = float
      .help = Target completeness of data (as percent, max=100) after         elliptical truncation
  }
  generate_noise {
    add_noise = False
      .type = bool
    noise_profile_file = None
      .type = path
    profile_model_file = None
      .type = path
    profile_data_label = None
      .type = str
    scale_noise = 1.0
      .type = float
    n_resolution_bins = 20
      .type = int
    n_intensity_bins = 20
      .type = int
  }
  fake_data_from_fmodel
    .help = Options for generating model-based reflections using phenix.fmodel.
  {
    include scope mmtbx.command_line.fmodel.fmodel_from_xray_structure_params
  }
}
t   process_includesc      	   C   s=  | d  k r t j } n  t d d | ƒ| d IJt |  ƒ d k sP d |  k r‡ | d IJd |  k ry t j d d ƒ n
 t j ƒ  d  Sd	 d
 l m } t j	 d d ƒ } d  } d  } d  } g  } x|  D]} t
 j j | ƒ rŠ| j | ƒ }	 |	 j d k r*|	 j } | j | j d d |	 j ƒ ƒ qÏ|	 j d k re|	 j } | j | j d d |	 j ƒ ƒ qÏ|	 j d k rÏ| j |	 j ƒ qÏqÈ y | j d | ƒ }
 Wn t k
 rÁ| d | IJqÈ X| j |
 ƒ qÈ Wt j d | ƒ } t d d | ƒ| j d d ƒ | j ƒ  } | j } t d | d | d | d | ƒ d  S(   Ns   mmtbx.simulate_low_res_datat   outsO  
  For generation of realistic data (model-based, or using real
  high-resolution data) for methods development.

*********************************** WARNING: ***********************************
 this is an experimental program - definitely NOT bug-free.
                  Use at your own risk!

  Usage:
   mmtbx.simulate_low_res_data model.pdb [options...]
     (generate data from a PDB file)

   mmtbx.simulate_low_res_data highres.mtz [model.pdb] [options...]
     (truncate high-resolution data)

   mmtbx.simulate_low_res_data --help
     (print full parameters with additional info)
i    s   --helps   # full parameters:t   attributes_leveli   iÿÿÿÿ(   t   file_readert
   home_scopet   simulate_datat   pdbt   args   pdb_file=%st   hkls   hkl_file=%st   phils&   ignoring uninterpretable argument '%s't   sourcess   Working parameterst   prefixs     t   paramst   hkl_int   pdb_in(   t   Nonet   syst   stdoutR   t   lent   master_philt   showt   iotbxR	   t!   command_line_argument_interpretert   ost   patht   isfilet   any_filet	   file_typet   file_objectt   appendt   processt	   file_namet   RuntimeErrort   fetcht   extractR   t   prepare_data(   t   argsR   R	   t   interpreterR   t   pdb_hierarchyR   t	   user_philR   t   ft   arg_philt   working_philt   params_R   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyt   run‘   sX    

			#	#	R)   c           B   sJ   e  Z d d e j d  „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   c      	   C   sÖ  t  |  t ƒ  ƒ | |  _ | |  _ d  |  _ | j d  k rX | j d  k rX t d ƒ ‚ n  | j	 j
 r— | j d  k r— | j	 j d  k r— t d ƒ ‚ q— n  | d  k ræ | j d  k	 ræ t j | j d d ƒ} | j d ƒ | j |  _ n  |  j d  k r8| j d  k	 r8t j | j d d ƒ} | j d ƒ | j |  _ n  |  j d  k	 rY|  j j |  _ n  |  j d  k	 r�t d d t j ƒ|  j ƒ  \ } } n7 |  j d  k	 rÇt d d t j ƒ|  j ƒ  \ } } n  | j j d  k	 rñ|  j | ƒ \ } } n  | |  _ t d	 d t j ƒ| d
 | j IJd \ |  _ |  _ |  j d  k	 r]t  |  j ƒ \ |  _ |  _ n  |  j! | ƒ |  _" | j	 j
 rt d d t j ƒ|  j" j# ƒ  d  k r|  j d  k	 rÖt$ |  j" |  j |  j ƒ \ } }	 n  t% d |  j" d | j	 d | j& d t' ƒ }
 n  t( |
 ƒ |
 j) ƒ  |  _" n  t d d t j ƒ| d |  j" j* ƒ  d IJ| d |  j" j ƒ  IJ|  j d  k	 rÈt$ |  j" |  j |  j ƒ \ } }	 | d IJ| d IJt+ | |	 d | ƒ| d IJn  |  j, ƒ  d  S(   Ns   No PDB file specified.sJ   noise_profile_file required when add_noise=True and hkl_file is undefined.t
   force_typeR   R   s   Extracting experimental dataR   s'   Generating fake data with phenix.fmodels!   Applying low-resolution filterings     Target resolution: %.2f As    Adding noise using sigma profilet   f_obsR   t   wilson_bt   return_as_amplitudess   Done processings'     Completeness after processing: %.2f%%g      Y@s     Final resolution: %.2f At    s%     Scaling statistics for output data:(   NN(-   R   t   localsR   R   R   R,   t   pdb_filet   hkl_fileR   t   generate_noiset	   add_noiset   noise_profile_fileR	   R    t   assert_file_typeR"   R   R   t   hkl_Filet	   hierarchyR   R   R   t   from_hklt   from_pdbt   r_free_flagsR%   t   import_r_free_flagst   r_freet   d_mint
   n_residuest   n_basest
   get_countst   truncate_datat   f_outt   sigmast   wilson_scalingt   create_sigmast   b_wilsont   Falset   apply_sigma_noiset	   f_sq_as_ft   completenesst   show_b_factor_infot   write_output(   t   selfR   R   R   R   R.   t   f_rawRE   t	   iso_scalet   aniso_scalet   i_obs(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyt   __init__Ó   sv    							


c   
      C   sê  |  j  } |  j } |  j } | j ƒ  d  k	 rH | j d d d d ƒ } n | j d d d d ƒ } |  j d  k	 r  |  j j | ƒ } | j d | d d d d ƒ n  | j	 ƒ  } | j
 d  k r'| j d  k	 rò t j j t j j | j ƒ ƒ d } n% t j j t j j | j ƒ ƒ d } | d	 | _
 n  | j d
 | j
 ƒ | d | j
 IJ|  j d  k	 ræ| j ræt j j | j
 ƒ d d } t | d ƒ }	 |	 j d d j |  j j j ƒ  ƒ ƒ |	 j |  j j ƒ  ƒ |	 j ƒ  | d | IJn  d  S(   Nt   column_root_labelt   Ft   column_typest   FQt   miller_arrayt
   FreeR_flagt   Ii    s   _low_res.mtzR%   s
     Wrote %ss   .pdbt   ws   %s
s   
s     Wrote modified model to %s(   RK   R   R   RL   R   t   as_mtz_datasetRE   t
   common_sett   add_miller_arrayt
   mtz_objectt   output_fileR:   R   R   t   splitextt   basenameR9   t   writeR,   t   write_modified_pdbt   opent   joinR   t   inputt   crystallographic_sectiont   as_pdb_stringt   close(
   RV   RK   R   R   t   mtz_datasetRE   Rg   t	   base_namet   pdb_outR.   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRU     s>    							
(%&
c   !      C   sx  |  j  } |  j } |  j } d+ \ } } |  j j ƒ  } | d  k	 r] | j ƒ  } | j ƒ  } n  | } | } | j j d  k	 rŠ | j j } n | | j _ | j j d  k	 r· | j j } n | | j _ | d  k sÛ | d  k rî t	 d d ƒ ‚ n  d d l
 m }	 m }
 d d l m } |	 j d | d | ƒ } | j j r| d IJxÔ | j ƒ  D]Ã } xº | j ƒ  D]¬ } xx | j ƒ  D]j } x6 | j ƒ  D]( } | j d, k rˆ| j d | ƒ qˆqˆWt | j ƒ  ƒ d k ru| j d | ƒ ququWt | j ƒ  ƒ d k rb| j d | ƒ qbqbWqOWn  | j j rR| d IJd d l m } | j d | ƒ n  |  j j d | ƒ } | j  ƒ  j! ƒ  j" ƒ  } | j# ƒ  } d | k p d | k sÎ| d IJ| d IJ| d IJ| d IJn  | j j$ rç| j$ ƒ  n  d  } | j j% d  k	 rM| j j& st' ‚ | d | j j% IJ| j j% d k s>t' ‚ | j j% } n; | j j& rˆ| d IJt( d | j) d d ƒ } | d IJn  | d  k	 r| j* ƒ  } | j+ | ƒ } | j, | ƒ } |
 j- | ƒ } | | } | j. | ƒ | j ƒ  j/ d | j0 ƒ  d | j ƒ  ƒ n  d d  l1 } d d l m2 } | j3 j4 j5 j6 ƒ  } | j) | _7 | j8 } | j4 | _4 | j4 j9 d k rµ| d  IJ| d! IJ| d" IJ| d# IJd$ | j4 _9 | d IJn  | j4 j: d k r| d% IJ| d! IJ| d" IJ| d& IJd' | j4 _: | d IJn  | j; | _; | j< | _< | j= j> | _? t@ | _A d( | jB _C | jD d) | d* | ƒ } | jE } | j= }  | |  f S(-   Ns9   Incomplete symmetry information - please specify a space s'   group and unit cell for this structure.iÿÿÿÿ(   t   crystalt   adptbx(   t   flext	   unit_cellt   space_group_infos     Removing solvent atoms...t   HOHt   WATt
   atom_groupi    t   residue_groupt   chainsC     Removing all alternate conformations and resetting occupancies...(   t   pdbtoolsR@   t   crystal_symmetryt   Ht   Ds6     WARNING: this model does not contain hydrogen atoms!s9              strongly recommend running phenix.ready_set ors9              equivalent to ensure realistic simulated data.R7   s(     Scaling B-factors to have mean of %.2fs>     Scaling B-factors to match mean Wilson B for this resolutionRF   t	   stat_typeR5   t
   scatterers(   t   utilss8     b_sol is zero - will use mean value for d_min +/- 0.2As8      (this is not strongly correlated with resolution, buts<       it is preferrable to use a real value instead of leavings       it set to 0)g      G@s8     k_sol is zero - will use mean value for d_min +/- 0.2As        it set to 0)gffffffÖ?t   realt   xray_structureR   (   NN(   R{   R|   (F   R   R   R,   R   R   R�   Rz   Ry   t   space_groupR   t   cctbxRv   Rw   t   scitbx.array_familyRx   t   symmetryt
   modify_pdbt   remove_waterst   modelst   chainst   residue_groupst   atom_groupst   resnamet   remove_atom_groupR   t   remove_residue_groupt   atomst   remove_chaint   remove_alt_confst   mmtbxR€   t   xray_structure_simplet   scattering_type_registryt   type_count_dictt   keyst   hd_selectiont   convert_to_isotropict   set_mean_b_isot   set_wilson_bt   AssertionErrort!   get_mean_statistic_for_resolutionRF   t   extract_u_iso_or_u_equivt   selectt   meant   u_as_bt
   scale_adpst   set_adps_from_scatterersR…   t   mmtbx.command_line.fmodelR†   t   command_linet   fmodelt(   fmodel_from_xray_structure_master_paramsR(   t   high_resolutiont   fake_data_from_fmodelt   b_solt   k_solt   structure_factors_accuracyt   maskRC   t   fractiont   r_free_flags_fractionRP   t
   add_sigmast   outputt   typet   fmodel_from_xray_structuret   f_model(!   RV   R   R   R,   t   pdb_sgt   pdb_uct   pdb_symmt   apply_sgt   apply_ucRv   Rw   Rx   t
   apply_symmt   modelR   R~   R}   R€   Rˆ   t	   sctr_keysRž   t   set_bt   u_isot   u_meant   b_meant   scaleR™   R†   t   fmodel_paramst	   fake_datat   fmodel_Rº   RC   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRB   7  sÒ    					

	



		
	







					c         C   sÜ  |  j  } |  j } |  j j ƒ  } d  } d  } xo| D]g} | j d  k	 rv | j ƒ  j ƒ  | j k rþ | j ƒ  } qþ nˆ | j	 ƒ  r· | j ƒ  } | j ƒ  j ƒ  | _ | d | j IJnG | j
 ƒ  rþ | j ƒ  j ƒ  } | j ƒ  j ƒ  | _ | d | j IJn  | j j d  k	 rZ| j ƒ  j ƒ  | j j k r›| j ƒ  } | d | j ƒ  j ƒ  IJq›q4 | j ƒ  j ƒ  d k r4 | j ƒ  } | d | j ƒ  j ƒ  IJq4 q4 W| j d | j ƒ } | d  k	 rÒ| j | ƒ } n  | | f S(   Ns     F-obs: %ss     I-obs: %ss     R-free: %sRa   t   FREERF   (   s
   FreeR_flagRË   (   R   R   R   t   as_miller_arraysR   t
   data_labelt   infot   label_stringt
   map_to_asut   is_xray_amplitude_arrayt   is_xray_intensity_arrayRR   RC   t   labelt   resolution_filterRF   Re   (   RV   R   R   t   miller_arraysR4   RE   t   array(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRA   ¤  s8    		 !c         C   s   |  j  j } |  j } d d l m } | j | j ƒ } | j d ƒ | j } | j	 d d  d | j d d  d d d	 t ƒ \ } } | j d
 | j ƒ  | k ƒ }	 |	 j ƒ  j | ƒ }	 | d | j | j ƒ  j ƒ  f IJ| j ƒ  j ƒ  |	 j ƒ  j ƒ  k rÌ| j ƒ  j ƒ  |	 j ƒ  j ƒ  }
 |
 d k s.t ‚ | j d k r]| d |
 IJ| j |	 ƒ } qÌ| d |
 IJ| j |	 ƒ } | j d |	 j ƒ  j t ƒ |	 j ƒ  j ƒ  d d  d t ƒ } |	 j d | ƒ }	 n  | j ƒ  j ƒ  |	 j ƒ  j ƒ  k söt ‚ | |	 f S(   Niÿÿÿÿ(   R	   R   R%   RÓ   t   test_flag_valuet   parameter_scopes   simulate_data.r_free_flagst   disable_suitability_testt   datas     Using R-free flags from %s:%si    t   discards1       discarding %d amplitudes without R-free flagss6       generating missing R-free flags for %d reflectionsR´   t   max_freet   use_lattice_symmetryt   other(   R   RC   R   R   R	   R    R%   R>   t   file_servert   get_r_free_flagsR   RÓ   RP   t   customized_copyRÚ   RÐ   Re   RÎ   RÏ   t   sizeR¢   t   missing_flagst   lone_sett   generate_r_free_flagst   countt   Truet   concatenate(   RV   R]   R   R   R	   t   rfree_int
   hkl_servert
   r_free_rawt
   flag_valueRE   t	   n_missingt   missing_setRã   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRD   Â  sB    				$"	%	*c      	   C   se  |  j  } |  j } d d l m } | j j d  k	 ro | d | j j IJ| j d | j j d | j j ƒ } n  | j j	 d d d d d d g k rÄ | d IJ| j d | j j	 d | j j ƒ } n  | j j
 d  k	 rþ | d	 IJt d
 | d | j j
 ƒ } n  | j j d  k	 rŠ| d IJ| d | j j IJ| j j d  k	 sFt ‚ t d | d | j j d | j j ƒ \ } } | d | | f IJn  | j j ra| d IJ| j j } | j ƒ  d } | | k  ré| d IJ| d IJ| d IJqa| d IJt d | d |  j d |  j ƒ \ }	 }
 t d | d |
 j d | j j d | ƒ \ } } | d | | f IJn  | S(   Niÿÿÿÿ(   Rx   s)     Applying isotropic B-factor of %.2f A^2t   b_isot   apply_to_sigmasi    s     Adding anisotropy...t   b_carts0     Adding random error as percent of amplitude...R4   t   error_percents2     Removing cone of data around axis of rotation...s       radius = %.1f degreesRÖ   t   axist   cone_radiuss&       removed %d reflections (out of %d)s%     Truncating the data elliptically...g      Y@s3       completeness is already less than target value:s          %.2f versus %.2fs*       elliptical truncation will be skipped.s+       using overall anisotropic B as a guide.R]   RG   RH   t   scale_factort   target_completeness(   R   R   R‹   Rx   t   truncatet	   add_b_isoR   t   apply_debye_waller_factorst   apply_b_to_sigmast   add_b_anisot   add_random_error_percentt   add_random_errort   remove_cone_around_axist   axis_of_rotationR¢   t   elliptical_truncationt   ellipse_target_completenessRS   RM   RG   RH   Rñ   t   ellipse_scale(   RV   R]   R   R   Rx   t   n_hklt   delta_n_hklRö   t   completeness_startRX   RY   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRJ   æ  sd    			$
	






		
N(   t   __name__t
   __module__R   R   R   R[   RU   RB   RA   RD   RJ   (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR)   Ò   s   A	#	m		$c         C   s‘   d } d } xx |  j  ƒ  d j ƒ  D]` } | j ƒ  d } | j ƒ  r^ | t | j ƒ  ƒ 7} q# | j ƒ  r# | t | j ƒ  ƒ 7} q# q# W| | f S(   Ni    (   R�   R�   t
   conformerst
   is_proteinR   R‘   t   is_na(   R@   RG   RH   R   t	   main_conf(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRI     s    c         C   sV   d d l  m } | j d |  d | d | ƒ } | j d |  d | d | ƒ } | | f S(   Niÿÿÿÿ(   t   absolute_scalingR`   RG   RH   (   t   mmtbx.scalingR  t   ml_iso_absolute_scalingt   ml_aniso_absolute_scaling(   R]   RG   RH   R  RX   RY   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRM   &  s    				c         C   so   | j  } | d |  j IJ| d | d | d | d f IJ| d | d | d f IJ| d	 | d
 IJd  S(   Ns'       overall isotropic B-factor:   %6.2fs5       overall anisotropic B-factor: %6.2f, %6.2f, %6.2fi    i   i   s/                                     %14.2f, %6.2fi   i   s(                                     %22.2fi   (   Rñ   RO   (   RX   RY   R   Rñ   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRT   2  s    	c         C   sÍ   d d l  m } d | k o' d k n s2 t ‚ |  j ƒ  } t j ƒ  | d } | j ƒ  } xN t | j ƒ  ƒ D]: } t j	 d d ƒ } | d k rž d } n  | j
 | ƒ qq W| | | } |  j d | ƒ S(   Niÿÿÿÿ(   Rx   id   i    g      Y@i   RÚ   (   R‹   Rx   R¢   RÚ   R]   t   doublet   xrangeRâ   t   randomt   randintR#   Rá   (   R4   Rò   Rx   RÚ   t   frt   rit   trialt   r(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRý   ;  s    " 	g      ð?c         C   s^  d d l  m } d d l m } |  j ƒ  } d } t j } x< t | d d !ƒ D]' \ }	 }
 |
 | k  rO |
 } |	 } qO qO Wd | k o‘ d k n sœ t ‚ d d d g } x3 | D]+ } | | | | k r² | | | | <q² q² W| d d d g k sü t ‚ |  j	 ƒ  j
 d | d | ƒ | } |  j d | ƒ j ƒ  } | d  k	 r¹| d k rZ| d k s`t ‚ |  j ƒ  d	 } | | k s‚t ‚ t | ƒ } t t | ƒ | | d ƒ } | | } n  |  j ƒ  } |  j ƒ  } | j ƒ  } d } x^ | t | ƒ k  rC| | | k  r6| | =| | =| | =| d  k	 r@| | =q@qæ| d
 7} qæW| | j ƒ  } | | f S(   Niÿÿÿÿ(   Rw   (   Rx   i    i   t   miller_indexRñ   id   g      Y@i   (   RŠ   Rw   R‹   Rx   t   indicesR   t   maxintt	   enumerateR¢   Ry   t   debye_waller_factort   debye_waller_factorsRÚ   R   RS   t   sortedR   R   RL   Râ   (   RÖ   Rñ   Rõ   Rö   Rw   Rx   R  t
   axis_indext   min_b_directionalt   nt   b_indext   max_index_along_axisR   t   scale_cutoffRÇ   R  t	   scale_srtt   i_maxRÚ   RL   R  t   iR  (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR   H  sN    	 "c         C   sU  | d k  r | d k s t  ‚ d d l m } |  j ƒ  } |  j ƒ  } | j | ƒ } | | j t | ƒ ƒ d ƒ } | j ƒ  } |  j ƒ  }	 |  j	 ƒ  }
 d | } d } x‘ | t
 | ƒ k  r:| | | d	 ƒ } t j | j | ƒ ƒ } | | k sÿ | | k r-| | =| | =|	 | =|
 d  k	 r7|
 | =q7qª | d 7} qª W| | j ƒ  } | | f S(
   NiZ   i    iÿÿÿÿ(   t   reci   i   i´   (   i   i   (   i   i   (   R¢   t   scitbx.matrixR(  Ry   R  t   reciprocal_space_vectort   tupleRâ   RÚ   RL   R   t   matht   degreest   angleR   (   RÖ   Ró   Rô   R(  Ry   R  t
   rc_vectorst   v1R  RÚ   RL   t
   angle_highR'  t   v2R.  R  (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRþ   u  s.    
s   Wilson B-factorR5   s!   Bulk solvent scale factor (k_sol)R±   s   Bulk solvent B-factor (b_sol)R°   gš™™™™™É?c         C   s¼  | d  k r t j } n  d d l m } t j j d d d t j	 j
 ƒ } t j | ƒ } | d } | | } | j ƒ  }	 x€ t | | ƒ D]o \ }
 } y t |
 ƒ } t | ƒ } Wn t k
 rÆ qˆ qˆ X| |  | k rˆ | |  | k  rˆ |	 j | ƒ qˆ qˆ W| j |	 d d ƒ} | d	 t | |  | |  | f IJ| j |	 ƒ } | j |	 ƒ } | j |	 ƒ } | d
 |	 j ƒ  IJ| d | IJ| d | IJ| d | IJ| d IJ| j d d ƒ | S(   Niÿÿÿÿ(   Rx   t   relative_paths%   chem_data/polygon_data/all_mvd.picklet   testR®   t   n_slotsi
   s     %s for d_min = %.3f - %.3f As       count: %ds       min: %.2fs       max: %.2fs       mean: %.2fs       histogram of values:R   s         (   R   R   R   R‹   Rx   t   libtbxt   envt   find_in_repositoriesR   R   R   R   t   loadR  t   zipt   floatt
   ValueErrorR#   t	   histogramt
   stat_namest   mint   maxR¦   Râ   R   (   RF   R„   t   rangeR   Rx   t   pkl_filet   dbt	   all_d_mint   stat_valuest   values_for_ranget   d_t   v_t   dt   vt   hR?  R@  R¦   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR£   –  s>    

  
c         C   sg  |  j  ƒ  d  k s t ‚ d d l m } |  j ƒ  } | j ƒ  | j | j ƒ  ƒ } t d | j	 d | j
 d | d | j d | j d | j ƒ } | j | j ƒ  d	 ƒ } | j d
 | j ƒ x€ | j ƒ  j ƒ  D]l }	 | j ƒ  j |	 ƒ }
 | j |	 ƒ } x? |
 j ƒ  D]1 } | j | | ƒ } | j ƒ  | | | | <qWqÍ W| j d | ƒ } | r_| j ƒ  S| Sd  S(   Niÿÿÿÿ(   Rx   t   mtz_fileR9   R5   RÍ   t   n_resolution_binst   n_intensity_binsg        t   n_binsRL   (   RL   R   R¢   R‹   Rx   t	   f_as_f_sqRÚ   R¦   t   profile_sigma_generatorR=   t   profile_model_filet   profile_data_labelRM  RN  R  Râ   t   setup_binnert   binnert
   range_usedt	   selectiont   get_noise_profile_for_shellt
   iselectiont   get_i_over_sigmaRá   (   R4   R   R5   R6   Rx   RZ   t   i_normt   profilerRL   t   j_bint   bin_selt   shell_profilet   kt   i_over_sigmat   i_new(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRN   ·  s.    				 
c         C   sw   |  j  ƒ  s t ‚ |  j ƒ  d  k	 s* t ‚ |  j ƒ  } x: t |  j ƒ  ƒ D]& \ } } t j | | | ƒ | | <qI Wd  S(   N(   RÒ   R¢   RL   R   RÚ   R  R  t   gauss(   RZ   RÚ   R'  t   sigma(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRQ   Ñ  s
    RQ  c           B   s)   e  Z d d d  d  d d „ Z d „  Z RS(   i   c         C   s  | d  k r t j } n  | d  k s0 | d  k r= | d IJn  g  |  _ d d l m } d d l m }	 | | d d ƒ}
 |
 j d ƒ |
 j	 j
 } d  } d  } xw | D]o } | j ƒ  j ƒ  | k sÎ | d  k r¤ | j ƒ  rï | d  k rï | } q| j ƒ  r| d  k r| } qq¤ q¤ W| d  k rV| d  k	 rA| j ƒ  d  k	 sGt ‚ | j ƒ  } n  | j ƒ  d  k	 snt ‚ | d  k	 r,| d  k	 r,| d IJ| j ƒ  } | | ƒ j j } t | ƒ \ } } t d | d	 | d
 | ƒ \ } } | d IJt | | d | ƒ| | j } | j d | ƒ } | j ƒ  } n  |	 j | j ƒ  ƒ } | j d | j ƒ  | d | j ƒ  | ƒ } | j d d ƒ | j ƒ  | j ƒ  } xx | j ƒ  j ƒ  D]d } | j ƒ  j  | ƒ } | j! | ƒ } | j! | ƒ } t" d | d | d | ƒ } |  j j# | ƒ q¦Wd  S(   Ns    WARNING: missing desired Wilson B-factor and/or PDB file
           for noise profile data.  Without this information
           the intensity falloff with resolution will probably
           not be the same for your synthetic data and the
           data used to generate sigmas.
iÿÿÿÿ(   R    (   Rx   R3   R   s0     Correcting reference data intensity falloff...R]   RG   RH   s3     Scaling statistics for unmodified reference data:R   Rï   RÚ   RL   RO  i   R[  Ra  ($   R   R   R   t   _resolution_binst   iotbx.file_readerR    R‹   Rx   R>   Rß   RÕ   RÎ   RÏ   RÑ   RÒ   RL   R¢   RP  RR   R"   R@   RI   RM   RT   RO   Rù   R@  RÚ   Rá   RT  RU  RV  RW  R¥   t   shell_intensity_binsR#   (   RV   RL  R9   R5   RÍ   RM  RN  R   R    Rx   R.   RÕ   R4   RZ   RÖ   R,   RG   RH   RX   RY   t   delta_bt   i_meanR[  Ra  t   i_bint   selt   i_shellt   sn_shellt
   noise_bins(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR[   Ù  sh    		$	$

		c         C   s   |  j  | d S(   Ni   (   Re  (   RV   Rj  (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRX    s    N(   R  R  R   R[   RX  (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRQ  Ø  s   <Rg  c           B   s   e  Z d  d „ Z d „  Z RS(   i   c         C   s³   t  | j ƒ  | ƒ |  _ g  |  _ g  |  _ x‚ t | ƒ D]t } |  j j | ƒ } | j | ƒ } | j | ƒ } | j ƒ  | j ƒ  k s‹ t	 ‚ |  j j
 | ƒ |  j j
 | ƒ q7 Wd  S(   N(   t   bin_by_intensityRÚ   t   _binnert   _sn_binst   _i_binsRA  t   get_bin_selectionR¥   Râ   R¢   R#   (   RV   R[  Ra  RO  R!  R^  t   sn_binRj  (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR[      s    		c         C   s�   | d k s t  ‚ | d k r" d S|  j j | ƒ } |  j | } x5 t |  j | ƒ D]  \ } } | | k rU | | SqU W| d S(   Ni   i    iÿÿÿÿ(   R¢   Rp  t   get_binRq  R  Rr  (   RV   Rb   R`  t
   sn_profilet   jt   i_ref(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRZ  ,  s      (   R  R  R[   RZ  (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRg    s   Ro  c           B   s&   e  Z d  d „ Z d „  Z d „  Z RS(   i   c         C   sñ   d d l  m } g  |  _ | j | j ƒ  d ƒ |  _ t | ƒ } | j ƒ  | } xa t | ƒ D]S } | | d } | | j ƒ  k r– | j ƒ  d } n  | | } |  j j | ƒ q] Wx6 t	 | ƒ D]( \ }	 }
 |  j
 |
 ƒ } | |  j |	 <qÁ Wd  S(   Niÿÿÿÿ(   Rx   i   (   R‹   Rx   t   _bin_limitst   intRâ   t   _binsR  RA  R#   R  Ru  (   RV   RÚ   RO  Rx   t   sorted_datat   bin_sizeR!  t	   bin_limitR&  R`  Rb   t   bin(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyR[   7  s    	
c         C   sS   | d k s t  ‚ x- t |  j ƒ D] \ } } | | k  r" | Sq" Wt |  j ƒ d S(   Ni    i   (   R¢   R  Ry  R   (   RV   Rb   R`  t   limit(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRu  G  s
    c         C   sh   d d l  m } | j |  j j ƒ  t ƒ } x6 t |  j ƒ D]% \ } } | | k r; t | | <q; q; W| S(   Niÿÿÿÿ(   Rx   (   R‹   Rx   t   boolR{  Râ   RP   R  Rç   (   RV   R  Rx   Rk  R`  R!  (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRs  N  s    (   R  R  R[   Ru  Rs  (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyRo  6  s   	t   __main__i   (*   t
   __future__R    t
   iotbx.philR   t   libtbx.load_envR6  t   libtbx.str_utilsR   t   libtbx.math_utilsR   t   libtbx.utilsR   R   R   R  R,  R   R   R   t   parseRç   R   R   R2   t   objectR)   RI   RM   RT   Rý   R   Rþ   R>  R£   RP   RN   RQ   RQ  Rg  Ro  R  t   argv(    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/simulate_low_res_data.pyt   <module>   sF   	�Aÿ J					*	
!	G 