ó
È™xTc           @   s­   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	 m
 Z
 d  d l m Z d  d l Z d  d l m Z d Z d	 „  Z d
 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   adopt_init_args(   t   flexN(   t
   find_peaks(   t   xray(   t   boltzmann_constant_akmas7    tolerance = 1.0
    .type = float
  reset_all = False
    .type = bool
    .help = Removes all water atoms prior to re-picking using mFobs-DFmodel and 2mFo-DFmodel
  ordered_solvent_map_to_model = True
    .type = bool
  output_residue_name = HOH
    .type=str
    .input_size = 50
  output_chain_id = S
    .type=str
    .input_size = 50
  output_atom_name = O
    .type=str
    .input_size = 50
  scattering_type = O
    .type=str
    .help = Defines scattering factors for newly added waters
    .expert_level=2
    .input_size = 50
  primary_map_type = mFo-DFmodel
    .type=str
  primary_map_cutoff = 3.0
    .type=float
  secondary_map_type = 2mFo-DFmodel
    .type=str
  secondary_map_cutoff_keep = 2.5
    .type=float
  secondary_map_cutoff_find = 2.5
    .type=float
  h_bond_min_mac = 1.8
    .type = float
    .short_caption = H-bond minimum for solvent-model
    .expert_level = 1
  h_bond_min_sol = 1.8
    .type = float
    .short_caption = H-bond minimum for solvent-solvent
    .expert_level = 1
  h_bond_max = 3.2
    .type = float
    .short_caption = Maximum H-bond length
    .expert_level = 1
  new_solvent = *isotropic anisotropic
    .type = choice
    .help = Based on the choice, added solvent will have isotropic or             anisotropic b-factors
    .short_caption = New solvent ADP type
    .expert_level = 1
  b_iso_min = 0.0
    .type=float
    .help = Minimum B-factor value, waters with smaller value will be rejected
    .short_caption = Minimum B-factor
    .expert_level = 1
  b_iso_max = 100.0
    .type=float
    .help = Maximum B-factor value, waters with bigger value will be rejected
    .short_caption = Maximum B-factor
    .expert_level = 1
  anisotropy_min = 0.1
    .type = float
    .help = For solvent refined as anisotropic: remove is less than this value
    .short_caption = Minimum anisotropic B-factor
    .expert_level = 1
  b_iso = None
    .type=float
    .help = Initial B-factor value for newly added water
    .short_caption = Initial B-factor value
    .expert_level = 1
  refine_occupancies = False
    .type = bool
    .help = Refine solvent occupancies.
    .expert_level = 1
  occupancy_min = 0.1
    .type=float
    .help = Minimum occupancy value, waters with smaller value will be rejected
    .short_caption = Minimum occupancy
  occupancy_max = 1.0
    .type=float
    .help = Maximum occupancy value, waters with bigger value will be rejected
    .short_caption = Maximum occupancy
  occupancy = 1.0
    .type=float
    .help = Initial occupancy value for newly added water
    .short_caption = Initial occupancy value
  add_hydrogens = False
    .type = bool
    .help = Adds hydrogens to water molecules (except those on special positions)
  refilter = True
    .type = bool
  temperature = 300
    .type = float
    .help = Target temperature for random velocity assignment
  seed = 343534534
    .type = int
    .help = Fixes the random seed for velocity assignment
  find_peaks {
    use_sigma_scaled_maps = True
      .type=bool
      .help = Default is sigma scaled map, map in absolute scale is used               otherwise.
    resolution_factor = 1./4.
      .type=float
    map_next_to_model
      .expert_level=2
    {
      min_model_peak_dist = 1.8
        .type=float
      max_model_peak_dist = 6.0
        .type=float
      min_peak_peak_dist = 1.8
        .type=float
      use_hydrogens = True
        .type = bool
    }
    max_number_of_peaks = None
      .type=int
      .expert_level=1
    peak_search
      .expert_level=1
    {
      peak_search_level = 1
        .type=int
      max_peaks = 0
        .type=int
        .short_caption=Maximum peaks
      interpolate = True
        .type=bool
      min_distance_sym_equiv = None
        .type=float
        .short_caption=Minimum distance between symmetry-equivalent atoms
      general_positions_only = False
        .type=bool
      min_cross_distance = 1.8
        .type=float
        .short_caption=Minimum cross distance
      min_cubicle_edge = 5
        .type=float
        .short_caption=Minimum edge length of cubicles used for           fast neighbor search
        .expert_level=2
    }
  }
c           C   s   t  j j t ƒ S(   N(   t   iotbxt   philt   parset   master_params_str(    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyt   master_params�   s    t   managerc           B   s}   e  Z d  d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 e d	 „ Z d
 „  Z d d „ Z RS(   iÿÿÿÿc         C   s	  t  |  t ƒ  ƒ |  j d k r; t j j d d |  j ƒn  |  j d  k r_ t	 ƒ  j
 ƒ  |  _ n  |  j j |  _ |  j j |  j j k sŒ t ‚ |  j ƒ  |  j d k r¸ |  j d d ƒ n  |  j ƒ  sÓ t d ƒ ‚ n  |  j j ƒ  |  _ |  j j j |  j ƒ |  _ |  j d  k	 rC|  j j |  j ƒ |  _ |  j j |  j ƒ |  _ n  |  j ƒ  |  j j |  j j k skt ‚ |  j j d  k	 sƒt ‚ |  j d k r¢|  j d IJn  |  j j rž|  j j j d |  j j ƒ  d	 |  j j j  ƒ  d
 d ƒ } |  j j ƒ  } d }	 xc t! | j" ƒ  ƒ D]O }
 | |
 | j |
 k r|	 d 7}	 |  j d  k	 r_|  j# ƒ  |  j |
 <q_qqW|  j d k rˆ|  j d I|	 IJn  |  j j$ | j ƒ n  d  |  _% d  |  _& t' j( |  j j) ƒ  j" ƒ  t* ƒ |  _+ |  j j, j- } |  j j, j. } d |  j j, _- |  j j/ |  j j, _. |  j d |  j j d |  j j0 d t* ƒ j1 ƒ  } | d  k	 rn| j% | j& |  _% |  _& n  |  j j2 d  k	 r.|  j d |  j j2 d |  j j3 d t* ƒ j1 ƒ  } | d  k	 rô|  j% d  k	 rô|  j% j4 | j% ƒ |  j& j4 | j& ƒ q.| d  k	 r.|  j% d  k r.| j% | j& |  _% |  _& q.n  |  j% d  k	 rJ|  j5 ƒ  n  |  j j |  j+ ƒ } | j ƒ  |  _% |  j6 ƒ  |  j d  k	 r¯|  j j |  j+ ƒ } |  j j4 | ƒ n  |  j d k rÑ|  j d d ƒ n  |  j j |  j j k sït ‚ |  j ƒ  s
t d ƒ ‚ n  |  j d k r)|  j d IJn  d  |  _% d  |  _& | |  j j, _- | |  j j, _. |  j d |  j j d |  j j0 d t* ƒ j1 ƒ  } | d  k	 rX| j% d  k	 rXt' j7 | j% j" ƒ  t8 j9 d |  j j: d |  j j; d |  j j< d d ƒ ƒ } | j= | j% ƒ t8 j> d |  j j d | ƒ } |  j d |  j j2 d |  j j? d t* ƒ j1 ƒ  } | j d | j% d
 |  j j@ d	 d  ƒ } t' j( | j) ƒ  j" ƒ  t* ƒ } x* | jA D] }
 |
 d k rœtB | |
 <qœqœW|  j d k rí|  j d I| jC tB ƒ IJn  | j | ƒ } | j ƒ  |  _% |  j6 ƒ  |  j d k r7|  j d d ƒ n  |  j j |  j j k sXt ‚ n  |  j d  k	 rÚ|  j j j) ƒ  j" ƒ  t' jD |  j j j) ƒ  j" ƒ  |  j j" ƒ  d d d g ƒ |  _E |  jF ƒ  |  j j4 |  jE ƒ n  |  j d k r|  j d d d d  IJn  d  S(!   Ni    s*   Ensemble refinement ordered solvent updatet   outt   messages!   Number of waters in current models+   Water picking failed: solvent must be last.sK   
Cycle 1 - Evaluate Existing Solvent Atoms vs Primary Map and Secondary Mapt
   sites_fract   use_selectiont   distance_cutoffg      $@i   s*   Map solvent next to model (atoms moved) : gÍÌÌÌÌÌü?t   map_typet
   map_cutofft   use_kick_maps   Number of preserved waterssB   
Cycle 2 - Picking New Solvent Atoms from Fo-Fc Map and 2Fo-Fc Mapt	   occupancyt   bt   scattering_typet   labelt   HOHt   special_position_settingst
   scattererss*   Number of additional waters             : s   Total number of waterst   |t   -iM   s   |
(G   R   t   localst   verboset   mmtbxt   utilst   print_headert   logt   paramst   NoneR
   t   extractR   t   fppt   fmodelt   xray_structuret   modelt   AssertionErrort$   move_solvent_to_the_end_of_atom_listt   showt   is_water_lastt   RuntimeErrort   solvent_selectiont   existing_solvent_xrs_selectiont   selectt   existing_solvent_xrst
   velocitiest   existing_solvent_velocitiest   remove_solventt   primary_map_typet   ordered_solvent_map_to_modelt   closest_distancesR   t   hd_selectiont   xranget   sizet   randomize_velocityt   set_sites_fract   sitest   heightsR   t   boolR   t   Falset!   solvent_atoms_near_pick_selectiont   map_next_to_modelt   min_model_peak_distt   max_model_peak_distt"   preserved_solvent_minimum_distancet   primary_map_cutofft   peaks_mappedt   secondary_map_typet   secondary_map_cutoff_keept   extendt   compare_peaks_with_positionst   add_new_solventt   xray_scattererR   t	   scattererR   t   b_isoR   t	   set_sitest	   structuret   secondary_map_cutoff_findt	   tolerancet   i_seqst   Truet   countt   vec3_doublet   new_solvent_atom_velocitiest   randomize_new_velocities(   t   selfR'   R)   R   R#   R3   R"   R8   t   existing_sft   cntrt   xt   store_min_peak_distt   store_max_peak_distt   peakst   solvent_near_peakt   solvent_velocities_near_peakst   peaks_fo_fct   new_scattererst   new_solvent_xray_structuret   peaks_2fo_fct   atom_nearest_to_peakt%   new_solvent_atoms_near_pick_selectiont   new_solvent_near_peak(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyt   __init__¡   sò     

	
		$		
									
!=
c         C   s…   |  j  j d  k	 r( t j |  j  j ƒ n  t j } t j } t |  j  j } d } | | d } | ƒ  g  d D] } | d | ƒ ^ ql S(   Ng      0@g      à?i   i   i   i    (   i   i   i   (   R#   t   seedR$   t   randomt   gaussR   t   temperature(   R[   t   random_gausst   random_randomt   ktt   mass_oxygent   sigmat   i(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR<   3  s    		c         C   s²   |  j  j d  k	 r( t j |  j  j ƒ n  t j } t j } t |  j  j } d } | | d } | ƒ  xF t |  j j	 ƒ  ƒ D]/ } g  d D] } | d | ƒ ^ qˆ |  j | <q{ Wd  S(   Ng      0@g      à?i   i   i   i    (   i   i   i   (
   R#   Rl   R$   Rm   Rn   R   Ro   R:   RY   R;   (   R[   Rp   Rq   Rr   Rs   Rt   R^   Ru   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyRZ   >  s    		c         C   s^   |  j  j d |  j d |  j j d d  ƒ } x- | j D]" } | d k r4 t |  j | <q4 q4 Wd  S(   NR   R   R   i    (	   R2   R8   R>   R#   RT   R$   RU   RV   RB   (   R[   Rh   R^   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyRL   J  s    		c         C   sÄ   t  j |  j j ƒ  j t ƒ t ƒ } | j t  j |  j j ƒ  j t ƒ t ƒ ƒ |  j j j	 |  j j ƒ  ƒ } | j
 ƒ  j t ƒ d k r¡ |  j d | d | ƒ n  |  j j ƒ  |  j j |  j _ d  S(   Ni    R/   t   solvent_xray_structure(   R   R@   R)   R/   RW   RA   RK   RV   R(   R1   R9   t   reset_solventt   renumber_waterR'   (   R[   t   solselt   xrs_sol(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR+   S  s    $+	
c         C   s5   |  j  j ƒ  |  _  |  j j d |  j  j d t ƒ d  S(   NR(   t   update_f_calc(   R)   R5   R'   t   update_xray_structureR(   RA   (   R[   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR5   ^  s    c         C   sŒ   | j  t ƒ | j ƒ  j ƒ  k s' t ‚ |  j j ƒ  |  _ |  j j d | d |  j j	 d |  j j
 d |  j j d |  j j d |  j j ƒ d  S(   NRv   t   residue_namet	   atom_namet   chain_idt   refine_occupanciest
   refine_adp(   RW   RV   R   R;   R*   R)   R5   t   add_solventR#   t   output_residue_namet   output_atom_namet   output_chain_idR€   t   new_solvent(   R[   R/   Rv   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyRw   d  s    c         C   sŠ   t  } |  j j ƒ  } | j ƒ  } | j ƒ  } | j ƒ  d k r† | j ƒ  d k r† t j | d ƒ t j | d ƒ d k r† t } q† n  | S(   Ni    i   (	   RV   R)   R/   t
   iselectionR;   R   t   min_defaultt   max_defaultRA   (   R[   t   resultt   sol_selt	   i_sol_selt	   i_mac_sel(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR-   p  s    $(c         C   s†   |  j  j d |  j j d t ƒ |  j d k r7 t } n t } t j d |  j  d | d | d |  j	 d t d	 d  d
 t d | d |  j ƒ 	S(   NR(   R{   i    R'   R   R   R#   R   t   kick_map_paramst   use_all_datat   silentR"   (   R'   R|   R)   R(   RA   R   RV   R   R   R&   R$   R"   (   R[   R   R   R   R�   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR   z  s    		c         C   sƒ  |  j  j } t j |  j j ƒ  t j d |  j  j d | d |  j  j	 d d ƒ ƒ } | j
 |  j ƒ t j d |  j j d | ƒ } |  j j j |  j j ƒ  ƒ } |  j j j |  j j ƒ  ƒ } | j d | ƒ } t j | j ƒ  j ƒ  t ƒ } | j t j | j ƒ  j ƒ  t ƒ ƒ |  j j d	 | d
 |  j  j d |  j  j d |  j  j d |  j  j d |  j  j ƒ |  j j d |  j j d t ƒ d  S(   NR   R   R   R   R   R   R   t   otherRv   R}   R~   R   R€   R�   R(   R{   (   R#   RP   R   RN   R>   R;   R   RO   R   R   RQ   RR   R)   R(   R1   R/   t   concatenateR@   R   RA   RK   RV   R‚   Rƒ   R„   R…   R€   R†   R'   R|   (   R[   t   b_solvRe   Rv   Rz   t   xrs_macR‹   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyRM   Œ  s4    		%c         C   sg   | d  k	 rG d d t | ƒ } |  j | | d I|  j j ƒ  IJn |  j d I|  j j ƒ  IJd  S(   Nt    i(   s   : s*   Number of waters                        : (   R$   t   lenR"   R)   t#   number_of_ordered_solvent_molecules(   R[   R   t   spacer(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR,   ¨  s    'N(   t   __name__t
   __module__R$   Rk   R<   RZ   RL   R+   R5   Rw   R-   RA   R   RM   R,   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyR       s   �								
	(   t
   __future__R    t   libtbxR   t   cctbx.array_familyR   t
   iotbx.philR   t   mmtbx.utilsR   R   t   cctbxR   Rm   t   mmtbx.dynamics.constantsR   R	   R
   t   objectR   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/solvent/ensemble_ordered_solvent.pyt   <module>   s   ’	