ó
Ë™xTc           @   sÉ   d  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 Z
 y d d l Z d d l Z Wn e k
 r‡ n Xd Z
 d	 e
 Z d
 e d e e e d d „ Z d e d „ Z d S(   sM   
Deals with modifying a structure to include unbuilt and misidentified ions.
iÿÿÿÿ(   t   division(   t   make_sub_header(   t   null_out(   t   AutoNt    s»   
use_svm = False
  .type = bool
  .short_caption = Experimental feature: use SVM-based classification of     candidate ions.
  .expert_level  =3
include scope mmtbx.ions.svm.svm_phil_str
s
  
debug = False
  .type = bool
  .short_caption = Debugging mode (verbose)
ion_chain_id = X
  .type = str
  .input_size = 80
  .short_caption = Ion chain ID
initial_occupancy = 1.0
  .type = float(value_min=0.01)
  .input_size = 80
  .help = Occupancy for newly placed ions - if less than 1.0, the occupancy     may be refined automatically in future runs of phenix.refine.
initial_b_iso = Auto
  .type = float(value_min=1.0)
  .input_size = 80
  .short_caption = Initial B-iso
refine_ion_occupancies = True
  .type = bool
  .help = Toggles refinement of occupancies for newly placed ions.  This     will only happen if the occupancy refinement strategy is selected.
refine_ion_adp = *Auto isotropic anisotropic none
  .type = choice
  .short_caption = Refine ion B-factor
  .help = B-factor refinement type for newly placed ions.  At medium-to-high     resolution, anisotropic refinement may be preferrable for the heavier     elements.
refine_anomalous = True
  .type = bool
  .short_caption = Model anomalous scattering
  .help = If True and the wavelength is specified, any newly placed ions will     have anomalous scattering factors refined.  This is     unlikely to affect R-factors but should flatten the anomalous LLG map.
max_distance_between_like_charges = 3.5
  .type = float
%s
i   c   B         s   d d l  } d d l m } d d l m } d d l } d d l } d d l m } d d l	 m
 } d d l	 m } d d l	 m } d d	 l m } d d l } | d k rÁ t | d
 t ƒ } n  d | j k oÛ d k n sæ t ‚ | j ƒ  ‰  ˆ  j ƒ  j ƒ  } | d k rt j } n  ˆ  j | _ | j j ƒ  | j d t ƒ } | j ƒ  } | j ƒ  | j j  ƒ  } xs t! | ƒ D]e \ } } | j" ƒ  } | j# d k ry| j$ d k  ry| | d k sÅt ‚ | j% | | ƒ | _$ qyqyW|  d k r`d } | r| j& j' j( } n  | j& j) j* d | d | j+ j, d ˆ  d | d | d | d | j- d | d | ƒ 	}  nN | j+ j, } | j. d j/ ƒ  } |  j0 d | d ˆ  j d | d | ƒ |  j1 ƒ  | j2 d | ƒ t3 d d | ƒ|  j4 d | ƒ g  } t5 | t6 ƒ rt7 | ƒ d k r| d j8 d ƒ } n  |  j9 d | d | ƒ }  | j: ƒ  }! |  j; ƒ  }" g  }# | r„xp |  D] }$ |# j< |$ j= |$ j> f ƒ q^WnF xC |  D]; \ } }% }& t7 |% ƒ d k r‹|# j< | |% d f ƒ q‹q‹Wg  }' t7 |# ƒ d k ret3 d t7 |# ƒ | ƒ xit! |# ƒ D]X\ }( \ } }) |  j? | } t }* xæ |# |(  D]Ú \ }+ }, |+ |' k rTq6n  |, j@ d k rr|) j@ d k s�|, j@ d k  r6|) j@ d k  r6|  j? |+ }- | jA |- ƒ }. |. | jB k  r| d  | jC ƒ  |) jD |) j@ |. |- jC ƒ  |, jD |, j@ f IJ|' j< | ƒ t }* Pqq6q6W|* r qn  | d! | jC ƒ  |) jD |) j@ f IJ| jE }/ |/ d" k rÄˆ  j ƒ  jF ƒ  d# k ryd$ }/ qÄˆ  j ƒ  jF ƒ  d% k  rÄ| jG |) jD ƒ jH ƒ  }0 |0 d& k rÁd$ }/ qÁqÄn  | jI }1 |1 tJ k rë|  jK | ƒ }1 n  |) jD }2 |2 d' k r	d( }2 n  | jL d) | d* |) jM ƒ  d+ |2 d, |2 d- |) j@ d. |) jD d/ | j d0 |1 d1 | jN d2 d3 d4 |/ d5 t ƒ }3 | jO rQ| rQ| j jP ƒ  | }4 | d k	 ró| jG |) jD ƒ jQ | ƒ }5 |5 jR ƒ  |4 _R |5 jS ƒ  |4 _S | d6 |4 jR |4 jS f IJn  | jT d7 | j: | g ƒ d8 |4 jR d9 |4 jS d: d8 d9 g d; | |3 ƒ d< t ƒ }6 | j< |6 ƒ n  |! j< | ƒ qWn  t7 |! ƒ d k rü
| j jP ƒ  }7 | j jU ƒ  }8 xe |8 jV ƒ  D]W } | jW d= | j d> |8 jX | ƒ jY ƒ  d? |7 | jZ d@ |7 | j[ dA d ƒ |7 | _Z q¢W| j j\ dB |7 ƒ ‡  f dC †  }9 ˆ  j] d | j dD t dE t ƒ t7 | ƒ }: | ob| jO ob|: d k }; | j^ st|; o¢|	 p¢| j_ j` d k p¢t7 | j_ j` ƒ d k }< |; rÜ| j d k	 rÜ|
 rÜ| j ja | ƒ t }; qÜn  |< sè|; rv
| dF IJ| dG |9 ƒ  IJˆ  j jP ƒ  jb dH t ƒ ˆ  j jP ƒ  jc d7 |! ƒ | jd je dI dJ ƒ }= | jf jg jd dK d dL | dM | dN t dO |= ƒ }> ˆ  j jh dP d dQ d dR |! ƒ g  }? x@ |! D]8 } ˆ  j jP ƒ  | ji }@ |@ d k r¦	|? j< | ƒ q¦	q¦	Wˆ  j] dD t dE t ƒ | dS |9 ƒ  IJt7 |? ƒ d k ri
| dT IJ| j ƒ  j ƒ  }A x/ |? D]$ } | dU |A | jC dV t ƒ IJq>
Wn  | dF IJn  |; rü
ˆ  j ƒ  j ƒ  s”
t ‚ | dW |9 ƒ  IJˆ  jj dX dY ƒ | jk d ˆ  dZ | ƒ ˆ  jj dX d[ ƒ | dS |9 ƒ  IJ| dF IJqü
n  |  S(\   s  
  Analyzes the water molecules in a structure and re-labels them as ions if
  they scatter and bind environments that we expect of that ion.

  Parameters
  ----------
  manager : mmtbx.ions.identity.manager
  fmodels : mmtbx.fmodels
  model : mmtbx.model.manager
  wavelength : float
  params : libtbx.phil.scope_extract
  nproc : int, optional
  elements : list of str, optional
  out : file, optional
  run_ordered_solvent : bool, optional
  occupancy_strategy_enabled : bool, optional
  group_anomalous_strategy_enabled : bool, optional
  use_svm : bool, optional

  See Also
  --------
  mmtbx.ions.identify.manager.analyze_waters
  iÿÿÿÿN(   t    get_single_atom_selection_string(   t   anomalous_scatterer_groups(   t   sasaki(   t   crystal(   t   adptbx(   t   xray(   t   flext   use_svmg      ð?i    t   sync_with_xray_structuret   HOHt   pdb_hierarchyt   geometry_restraints_managert   fmodelt
   wavelengtht   paramst   nproct   verboset   logt   manager_classt   xray_structuret   connectivityt   outs   Analyzing water moleculesi   t   ,t
   candidatess   Adding %d ions to models<     %s (%s%+d) is only %.3fA from %s (%s%+d), skipping for nows     %s becomes %s%+dR   g      ø?t   anisotropicg      @i   t   IODt   It   i_seqt   scattering_typet	   atom_namet   elementt   charget   residue_namet   initial_occupancyt   initial_b_isot   chain_idt   segidt   IONt
   refine_adpt   refine_occupanciess       setting f'=%g, f''=%gt
   iselectiont   f_primet   f_double_primet   refinet   selection_stringt   update_from_selectiont   crystal_symmetryt
   special_opt	   site_fract
   site_labelt	   tolerancet
   scatterersc              s   d ˆ  j  ƒ  ˆ  j ƒ  f S(   Ns   r_work=%6.4f r_free=%6.4f(   t   r_workt   r_free(    (   R   (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/build.pyt   show_r_factors  s    t   update_f_calct   update_f_maskR   s0     occupancy refinement (new ions only): start %st   statet   max_iterationsi   t   restraints_managert   fmodelst   modelt   is_neutron_scat_tablet   lbfgs_termination_paramst   occ_maxt   occ_mint	   selections0                                           final %ss2     WARNING: occupancy dropped to zero for %d atoms:s       %st   suppress_segids0     anomalous refinement (new ions only): start %st   target_namet   lst   groupst   ml(l   t   mmtbx.refinement.minimizationt+   mmtbx.refinement.anomalous_scatterer_groupsR   t   mmtbx.refinementR   t   mmtbx.ions.identifyt   mmtbx.ions.svmt   cctbx.eltbxR   t   cctbxR   R	   R
   t   scitbx.array_familyR   t   scitbx.lbfgst   Nonet   getattrt   FalseR&   t   AssertionErrort   fmodel_xrayt   f_obst   anomalous_flagt   syst   stdoutR   t   tidy_usR   t   Truet   atomst   reset_i_seqt   extract_u_iso_or_u_equivt	   enumeratet   fetch_labelst   resnamet   bt   u_as_bt   ionst   svmt   managert   identifyt   create_managerR@   t   geometryt   debugt   shell_sym_tablest   full_simple_connectivityt   update_structuret   update_mapst   update_anomalous_groupsR   t"   show_current_scattering_statisticst
   isinstancet   listt   lent   splitt   analyze_waterst   size_tt   get_initial_b_isot   appendR    t   final_choicet	   pdb_atomsR$   t   distancet!   max_distance_between_like_chargest   id_strR#   t   refine_ion_adpt   d_mint   tablet   atomic_numberR'   R   t   guess_b_iso_realt   convert_atomR!   t   ion_chain_idt   refine_anomalousR8   t   at_angstromt   fpt   fdpt   anomalous_scatterer_groupt   site_symmetry_tablet   special_position_indicest   correct_special_positiont   getR4   t   sitet   labelt   replace_scattererst   update_xray_structuret   refine_ion_occupanciest   refinement_flagst   s_occupanciest   extendt   flags_set_gradst   flags_set_grad_occupancyt   lbfgst   termination_parameterst
   refinementt   minimizationt   adjust_occupancyt	   occupancyt   updatet	   minimizer(B   Rk   RA   RB   R   R   R   t   elementsR   t   run_ordered_solventt   occupancy_strategy_enabledt    group_anomalous_strategy_enabledR   t   mmtbxR   R   R   R   R	   R
   R   t   scitbxR\   R   R   t   u_isoR    t   atomt   labelsR   t   grmR   t   anomalous_groupst   water_ion_candidatest   modified_iselectiont   default_b_isot   for_buildingt   resultt   final_choicest   two_fofct   skippedt   kR~   t   skipt   other_i_seqt	   other_iont
   other_atomt   dxyzR+   R†   R'   R#   t   modified_atomt	   scatterert   fp_fdp_infot   groupR8   R�   R;   t   n_anomRŠ   R,   RD   t	   minimizedt   zero_occt   occRa   (    (   R   s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/build.pyt   find_and_build_ionsA   s¬   $% 
			
!		   																				

			

%
	
c         C   s›  | d k r t ƒ  } n  d d l } | j ƒ  j ƒ  j d ƒ } | j ƒ  } t | ƒ d k rk | d IJ| S| j j	 ƒ  j
 ƒ  } | j | ƒ }	 | j | ƒ }
 |
 j d t ƒ } | j ƒ  } |
 j } | j j j d | j ƒ  d |
 j ƒ } |
 j j j } |
 j j j } | j | ƒ } |	 j d	 | j | ƒ d
 g  | D] } | j ^ q9d g  | D] } | j ƒ  j ^ qUd | j | ƒ d | j | ƒ d | j d g  | D] } d ^ qžd | j d d d t ƒ 
|	 j j	 ƒ  j
 ƒ  } |	 j ƒ  } | j ƒ  j
 ƒ  } | | k o| k n st ‚ | j ƒ  j d ƒ } | j ƒ  j | ƒ } | r‡| d IJx" | D] } | d | j ƒ  IJq\W| d IJn  |  j |	 j ƒ |	 S(   s
  
  Parameters
  ----------
  fmodel : mmtbx.f_model.manager
  model : mmtbx.model.manager
  params : libtbx.phil.scope_extract
  log : file, optional
  verbose : bool, optional

  Returns
  -------
  mmtbx.model.manager
      An updated model with ions corrected.
  iÿÿÿÿNs	   segid IONi    s     No ions (segid=ION) found.R   R   R   t   new_xray_structuret
   atom_namest   residue_namest   nonbonded_typest   nonbonded_chargesR(   t   segidsR*   R,   R+   t	   isotropict   reset_labelss     Final list of ions:s       %sR   (   RV   R   t   mmtbx.ions.utilsR   t   atom_selection_cacheRG   R-   Rx   R   R8   t   sizet   selectR`   Ra   Ri   t   utilst   sort_atoms_permutationR@   Rn   RÊ   RË   t   append_single_atomst   nameRe   Rf   R‰   R—   RY   R‚   R–   (   R   RB   R   R   R   R©   t   ion_selectiont   ion_iselectiont   n_sites_startt	   new_modelt	   ion_modelt   ion_pdb_hierarchyt	   ion_atomst   ion_xrst   permRÊ   RË   R¬   t   n_sites_endt   new_hierarchyt   n_sites_pdbt   new_selection(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/build.pyt   clean_up_ionsC  sZ    	
		"		"
(   t   __doc__t
   __future__R    t   libtbx.str_utilsR   t   libtbx.utilsR   t   libtbxR   R]   t
   svm_paramsRj   t   svmutilt   ImportErrort   ion_building_params_strRV   RX   RÆ   R`   Rä   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/build.pyt   <module>   s*   	&
ö