ó
Ê™xTc           @`  s¼  d  Z  d d l m Z 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 m Z m 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 m Z d d l  Z! d d l" m# Z# y( d d l$ Z% d d l& Z& d d l' Z' Wn# e( k
 rkd Z& d Z' d Z% n Xe j* j+ d d d e j, j- ƒ Z. d Z/ i  a0 i  a1 d d d d d d d d d d d d d  g Z2 d d! „ Z3 d" „  Z4 e5 e5 e5 e5 e5 e5 d e5 e5 e5 e6 e5 d# „ Z7 e5 d$ d% „ Z8 e6 e6 e6 d& „ Z9 d d' „ Z: d d( „ Z; d d) „ Z< d e5 e6 d* „ Z= d d d+ „ Z> d, „  Z? d- Z@ d. e f d/ „  ƒ  YZA d0 e! jB jC jD f d1 „  ƒ  YZD d S(2   st  
A set of functions to act upon ChemicalEnvironment and ScatteringEnvironment and
produce a single class and vector of features for use with a classifier.

This module relies on a SVM classifier generated by the module within
phenix_dev.ion_identification.nader_ml. See that module's description for more
details.

See Also
--------
phenix_dev.ion_identification.nader_ml
i    (   t   divisiont   absolute_import(   t   Iterable(   t   StringIO(   t   c_doubleN(   t   Autot+   slots_getstate_setstate_default_initializer(   t   load(   t   make_sub_header(   t   Sorry(   t   sasaki(   t   common_residue_names_water(   t   N_SUPPORTED_ENVIRONMENTS(   t   SUPPORTED_GEOMETRY_NAMES(   t   utilst   relative_paths   chem_data/classifierst   testt   merged_high_rest   HOHt   MNt   ZNt   FEt   NIt   CAt   NAt   MGt   CLt   CUt   COt   CDt   Kc         C`  s  t  d k	 s t ‚ |  s% |  t k r. t }  n  |  t k rå t j j t	 d j
 |  ƒ ƒ } t j j t	 d j
 |  ƒ ƒ } y t  j | ƒ t |  <WnB t k
 rÑ } | j t j k rº | ‚ qÒ d t |  <d t |  <n Xt | ƒ t |  <n  t |  \ } } } t |  | | | f S(   sy  
  If need be, initializes, and then returns a classifier trained to
  differentiate between different ions and water. Also returns of options for
  gathering features.

  To use the classifier, you will need to pass it to
  svm.libsvm.svm_predict_probability. Ion prediction is already encapsulated by
  predict_ion, so most users should just call that.

  Parameters
  ----------
  svm_name : str, optional
      The SVM to use for prediction. By default, the SVM trained on heavy atoms
      and calcium in the presence of anomalous data is used. See
      chem_data/classifiers for a full list of SVMs available.

  Returns
  -------
  svm.svm_model
      The libsvm classifier used to predict the identities of ion sites.
  dict of str, bool
      Options to pass to ion_vector when collecting features about these sites.
  tuple of ((tuple of numpy.array of float, numpy.array of float),
            tuple of float)
      The scaling parameters passed to scale_to.
  numpy.array of bool
      The features of the vector that were selected as important for
      classification. Useful for both asserting that ion_vector is returning
      something of the correct size as well as only selection features that
      actually affect classification.

  See Also
  --------
  svm.libsvm.svm_predict_probability
  mmtbx.ions.svm.predict_ion
  phenix_dev.ion_identification.nader_ml.ions_train_svms
  s   {}.models   {}_options.pklN(   NNN(   t   svmutilt   Nonet   AssertionErrorR   t   _DEFAULT_SVM_NAMEt   _CLASSIFIERt   ost   patht   joint   CLASSIFIERS_PATHt   formatt   svm_load_modelt   IOErrort   errnot   ENOENTt   _CLASSIFIER_OPTIONSR   (   t   svm_namet   svm_patht   options_patht   errt   vector_optionst   scalingt   features(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   _get_classifier;   s"    &		
c         C`  sp   t  |  j d ƒ r? |  j j j ƒ  r? |  j j j ƒ  j ƒ  } n |  j j j ƒ  j ƒ  } | t k rl d } n  | S(   s1  
  Returns the class name associated with the ion, analogous to the chemical
  ID.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      The object to extract the ion class from.

  Returns
  -------
  str
      The class associated with the ion (i.e. "HOH" or "ZN").
  t   segidR   (   t   hasattrt   atomR6   t   stript   uppert   resnamet   WATER_RES_NAMES(   t   chem_envt   name(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt	   ion_classx   s    $	c         C`  sÓ   t  d k	 s t ‚ t  j t | d | ƒ| rK t | d | d | d | ƒn g  | rf | rf t |  ƒ n g  | r{ t |  ƒ n g  | rœ |	 rœ t |  d | ƒn g  | rÉ |
 rÉ t	 | d | d | d | ƒn g  f ƒ S(	   s6  
  Creates a vector containing all of the useful properties contained
  within ion. Merges together the vectors from ion_*_vector().

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      A object containing information about the chemical environment at a site.
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment, optional
      An object containing information about the scattering environment at a
      site.
  use_scatter : bool, optional
      Include information derived from the scattering environment (b-factor,
      occupancy, FoFc peak, 2FoFc peak and width, f'' values)
  use_chem : bool, optional
      Include information derived from the chemical environment (Geometry,
      coordinating atom identities, valences).
  b_iso : bool, optional
      Include the atom's refined isotropic b-factor, divided by the mean
      b-factor of solvent molecules in the structure.
  occ : bool, optional
      Include the atom's refined occupancy.
  diff_peak : bool, optional
      Include the difference map peak height.
  geometry : bool, optional
      Include information about the presence of various geometries.
  elements : list of str, optional
      List of elements to include when calculating BVS, VECSUM, and f''_expected
       values. If unset, takes the list from mmtbx.ions.ALLOWED_IONS.
  valence : bool, optional
      Include BVS and VECSUM values.
  anom : bool, optional
      Include anomalous scattering information.
  ratios : bool, optional
      Use f'' / f'' expected, instead of raw f'' and f' values.
  anom_peak : bool, optional
      Whether to use the actual height of the anomalous map instead of the
      calculated f'' values.
  d_min : bool, optional
      Include the high resolution associated with the model as a feature.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.

  See Also
  --------
  ion_model_vector()
  ion_electron_density_vector()
  ion_geometry_vector()
  ion_nearby_atoms_vector()
  ion_valence_vector()
  ion_anomalous_vector()
  t   d_mint   b_isot   occt	   diff_peakt   elementst   ratiost	   anom_peakN(
   t   npR    R!   t   concatenatet   ion_model_vectort   ion_electron_density_vectort   ion_geometry_vectort   ion_nearby_atoms_vectort   ion_valence_vectort   ion_anomalous_vector(   R=   t   scatter_envt   use_scattert   use_chemRA   RB   RC   t   geometryRD   t   valencet   anomRE   RF   R@   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt
   ion_vector�   s    ;'!g      à?c         C`  se   | s t  j g  d t ƒS|  j } | d k	 rO d | } t | | ƒ | } n  t  j | g d t ƒS(   sÒ  
  Creates a vector containing information about the general properties of the
  model in which the site is found. Currently this only includes the minimum
  resolution of the data set.

  Parameters
  ----------
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment
      An object containing information about the scattering environment at a
      site.
  d_min : bool, optional
      Include the high resolution associated with the model as a feature.
  nearest_res : float, optional
      If not None, the nearest value to round d_min to. This value has no effect
      if nearest_res is False.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  t   dtypei   N(   RG   t   arrayt   floatR@   R    t   round(   RO   R@   t   nearest_rest   factor(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRI   Û   s    	
c         C`  s˜   |  j  d |  j  d g } | r7 | j |  j d ƒ n  | rl | j |  j |  j d k ra |  j n d ƒ n  | r… | j |  j ƒ n  t j | d t ƒS(   sI  
  Creates a vector containing information about the electron density around
  the site. Currently this only includes the site's peak in the 2FoFc map. May
  be expanded in the future to include information about the volume of density
  around the site.

  Parameters
  ----------
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment
      An object containing information about the scattering environment at a
      site.
  b_iso : bool, optional
      Include the atom's refined isotropic b-factor, divided by the mean
      b-factor of solvent molecules in the structure.
  occ : bool, optional
      Include the atom's refined occupancy.
  diff_peak : bool, optional
      Include the difference map peak height.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  i    i   i   RV   (	   t
   fo_densityt   appendt   fofc_densityRA   t
   b_mean_hohRB   RG   RW   RX   (   RO   RA   RB   RC   t   props(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRJ   û   s    
#c         `  sX   | d k r t } n  g  |  j D] } | d ^ q ‰  t j ‡  f d †  | Dƒ d t ƒS(   s  
  Creates a vector for a site's geometry. For each geometry in geometry_names
  the vector contains a 1 if that geometry is present at the site and 0
  otherwise.

  A single boolean was chosen after some trial and error with an SVM as
  differences in deviations < 15 degrees were not found to be significant in
  helping to diffentiate ions.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      A object containing information about the chemical environment at a site.
  geometry_names : list of str, optional
      A list of geometry names to check for. If unset, take names from
      mmtbx.ions.SUPPORTED_GEOMETRY_NAMES.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  i    c         3`  s   |  ] } | ˆ  k Vq d  S(   N(    (   t   .0t   i(   t   present_geometry_names(    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pys	   <genexpr>>  s    RV   N(   R    R   RR   RG   t   fromiterRX   (   R=   t   geometry_namesRb   (    (   Rc   sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRK   #  s
    	 c         `  s>   | d k r t t ƒ } n  t j ‡  f d †  | Dƒ d t ƒS(   sa  
  Creates a vector for the identities of the ions surrounding a site. Returns
  a vector with a count of coordinating nitrogens, oxygens, sulfurs, and
  chlorines.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      A object containing information about the chemical environment at a site.
  environments : list of int, optional
      A list of environments to check for. If unset, takes values from
      mmtbx.ions.environment.N_SUPPORTED_ENVIRONMENTS.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  c         3`  s   |  ] } ˆ  j  | Vq d  S(   N(   t	   chemistry(   Ra   Rb   (   R=   (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pys	   <genexpr>X  s    RV   N(   R    t   rangeR   RG   Rd   RX   (   R=   t   environments(    (   R=   sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRL   A  s    c         C`  sƒ   | d k r4 g  t D] } | t k r | ^ q } n  g  } x< | D]4 } | j |  j d | d t j j j | ƒ ƒ ƒ qA Wt	 | ƒ S(   s	  
  Calculate the BVS and VECSUM values for a variety of ion identities.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      A object containing information about the chemical environment at a site.
  elements : list of str, optional
      List of elements to include when calculating BVS and VECSUM values. If
      unset, takes the list from mmtbx.ions.ALLOWED_IONS.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  t   elementt   chargeN(
   R    t   ALLOWED_IONSR<   R]   t   get_valencet   mmtbxt   ionst   servert
   get_charget   _flatten_list(   R=   RD   Rb   t   retRi   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRM   [  s    (c         `  sÛ   | d k r4 g  t D] } | t k r | ^ q } n  ˆ j d k sR ˆ j d k r{ | rk t j t | ƒ ƒ St j d ƒ Sn  | r‘ ˆ j d ‰  n	 ˆ j ‰  | rÈ t j	 ‡  ‡ f d †  | Dƒ t
 ƒ } n t ˆ  g ƒ } | S(   s‡  
  Creates a vector of the anomalous features of a site. These can either include
  the f'' / f''_expected for a variety of ion identities or the exact anomalous
  peak height.

  Parameters
  ----------
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment
      An object containing information about the scattering environment at a
      site.
  elements : list of str, optional
      List of elements to include when calculating f''_expected values. If
      unset, takes the list from mmtbx.ions.ALLOWED_IONS.
  ratios : bool, optional
      If False, instead of calculating ratios, just return a vector of the
      wavelength, f', and f''.
  anom_peak : bool, optional
      Whether to use the actual height of the anomalous map instead of the
      calculated f'' values.

  Returns
  -------
  numpy.array of float
      A vector containing quantitative properties for classification.
  i   i    c         3`  s4   |  ]* } ˆ  t  j | ƒ j ˆ j ƒ j ƒ  Vq d  S(   N(   R
   t   tablet   at_angstromt
   wavelengtht   fdp(   Ra   Ri   (   t   heightRO   (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pys	   <genexpr>¥  s   N(   R    Rk   R<   t   fppRu   RG   t   zerost   lent   anom_densityRd   RX   Rq   (   RO   RD   RE   RF   Rb   Rr   (    (   Rw   RO   sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRN   y  s    (	c         C`  s  t  | ƒ \ } } } } | d k s0 | d k r4 d St |  | | � } t j | | d | d ƒ } t | ƒ t | ƒ k s� t ‚ | | } t j t	 | ƒ d | j
 j t j k ƒd }	 | j ƒ  }
 t |
 ƒ  } t j j | |	 | ƒ | |
  } g  | j ƒ  D] } t | ^ qÿ } t | | ƒ } | j d d „  ƒ | d k	 r x2 | D]* } | t k rJt d j | ƒ ƒ ‚ qJqJWg  g  } } x= | D]5 \ } } | | k rŒ| j | ƒ | j | ƒ qŒqŒWt | ƒ } g  | D] } | | ^ qØ} t | | ƒ } n  | S(   sô  
  Uses the trained classifier to predict the ions that most likely fit a given
  list of features about the site.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
      A object containing information about the chemical environment at a site.
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment, optional
      An object containing information about the scattering environment at a
      site.
  elements : list of str, optional
      A list of elements to include within the prediction. Must be a subset of
      mmtbx.ions.svm.ALLOWED_IONS. Note: Water is not added to elements by
      default.
  svm_name : str, optional
      The SVM to use for prediction. By default, the SVM trained on heavy atoms
      and calcium in the presence of anomalous data is used

  Returns
  -------
  list of tuple of str, float or None
      A sorted list of classes and the predicted probabilities associated with
      each or None if the trained classifier cannot be loaded.
  i    i   t   isKernelt   keyc         S`  s	   |  d S(   Niÿÿÿÿ(    (   t   x(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   <lambda>á  s    s   Unsupported element '{}'N(   R5   R    RU   R   t   scale_toRz   R!   t   svmt   gen_svm_nodearrayt   listt   paramt   kernel_typet   PRECOMPUTEDt   get_nr_classR   t   libsvmt   svm_predict_probabilityt
   get_labelsRk   t   zipt   sortR	   R(   R]   t   sum(   R=   RO   RD   R.   t
   classifierR2   R3   R4   t   vectort   xit   nr_classt   prob_estimatest   probsRb   t   labelst   lstRi   t   classest   probt   total(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   predict_ion«  s<    
!
#c         `  s2   ‡  f d †  ‰  t  j d „  ˆ  |  ƒ Dƒ d t ƒS(   sE  
  Turn a tree main out of lists into one flat numpy array. Converts all Nones
  into zeros and integers into floats in the process.

  Parameters
  ----------
  lst : list or  list of list or list of list of list or ...
      A list to be flattened

  Returns
  -------
  numpy.array of float
      A flat list of values.
  c         3`  sX   xQ |  D]I } t  | t ƒ rK t  | t ƒ rK x! ˆ  | ƒ D] } | Vq9 Wq | Vq Wd S(   sO   
    Returns a generator for each element in the flattened version of lst.
    N(   t
   isinstanceR   t
   basestring(   R•   t   itemt   sub(   t   _flatten(    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRž     s
    c         s`  s-   |  ]# } | d k	 r! t | ƒ n d  Vq d S(   g        N(   R    RX   (   Ra   Rb   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pys	   <genexpr>  s    RV   (   RG   Rd   RX   (   R•   (    (   Rž   sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRq   ö  s    sn  
svm
  .expert_level = 3
{
  svm_name = Auto
    .type = str
    .help = "Name of SVM classifier to use"
  filtered_outputs = True
    .type = bool
    .help = "Apply an filter to the outputs of the SVM, ensuring they are chemically sane"
  min_score = 0.2
    .type = float
  min_score_above = 0.1
    .type = float
  min_fraction_of_next = 2.0
    .type = float
}
t   svm_predictionc           B`  sM   e  Z d  Z d d d d d d d g Z e j d d	 „ Z e j d d
 „ Z RS(   s\  
  Contains information about a SVM's prediction of a site's identity.

  Attributes
  ----------
  i_seq : int
  pdb_id_str : str
  atom_info_str : str
  map_stats : group_args
  atom_types : list of str
  scores : list of float
      Probabilities associated with each element listed by atom_types.
  final_choice : mmtbx.ions.metal_parameters
  t   i_seqt
   pdb_id_strt   atom_info_strt	   map_statst
   atom_typest   scorest   final_choicet    c         C`  sº   x+ |  j  j ƒ  D] } | | | j ƒ  IJq W| | d IJt t |  j |  j ƒ d d „  ƒ} x, | D]$ \ } } | | d | | f IJqg W|  j d k	 r¶ | | d |  j IJn  d S(   s�   
    Shows information about a SVM's prediction of a site's identity.

    Parameters
    ----------
    out : file, optional
    prefix : str, optional
    s   SVM scores:R}   c         S`  s	   |  d S(   Ni   (    (   R~   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyR   I  s    s     %4s : %.3fs   Final choice: %sN(	   R¢   t
   splitlinest   rstript   sortedR‹   R¤   R¥   R¦   R    (   t   selft   outt   prefixt   lineR¥   t   elemt   score(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   show=  s    	$c      	   C`  sž   |  j  } | d k r$ d } d } nC x@ t |  j |  j ƒ D]) \ } } | | j k r: d | } Pq: q: W| | d |  j | j | |  j j |  j j	 f IJd S(   sÄ   
    Shows a brief description of a SVM's prediction of a site's identity, for
    use in output as a table.

    Parameters
    ----------
    out : file, optional
    prefix : str, optional
    s   ----s   %5.3fs   %s   %4s  %5s  %5.2f  %5.2fN(
   R¦   R    R‹   R¤   R¥   Ri   R¡   R£   t   two_fofct   fofc(   R«   R¬   R­   R¦   t
   best_scoret	   atom_typeR°   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt
   show_briefO  s    
		"

(   t   __name__t
   __module__t   __doc__t	   __slots__t   syst   stdoutR±   R¶   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyRŸ   +  s
   t   managerc           B`  s,   e  Z e e d  „ Z e j e e d „ Z RS(   c         C`  sþ  t  j j j | |  ƒ } | j d |  j j ƒ } | t  j j j k rI d S| t	 k } | rj t  j j
 } n9 t | t ƒ sˆ t | t ƒ r£ | j d d ƒ j ƒ  } n  g  | D] } | j ƒ  j ƒ  ^ qª } | d g k rà g  } n  | rï d } n | j d ƒ d d l m } m }	 |	 | | j |  ƒ }
 | d | d	 |  d
 |  j d | ƒ d |  j d | ƒ d |  j d | ƒ ƒ } t |
 | d | d |  j j j ƒ} | d k	 rúd } | j d d „  ƒ | d \ } } | d k rZt | ƒ d k rt  j j j | ƒ } qZ| d \ } } | |  j j j k rZ| | |  j j j  k rZt  j j j | ƒ } qZn  t! ƒ  } | j" d d d | ƒ t# d | d |  j$ | j% ƒ  d | j& ƒ  d |  j' | ƒ d g  | D] } | d ^ qºd g  | D] } | d ^ q×d | ƒ } | Sd S(   s  
    Analyzes a single water site using a SVM to decide whether to re-assign it
    as an ion.

    Parameters
    ----------
    i_seq : int
    debug : bool, optional
    candidates : list of str, optional

    Returns
    -------
    svm_prediction or None
    t   paramst   ,t    t   XR   i    (   t   ScatteringEnvironmentt   ChemicalEnvironmentR    R½   R\   t   mFoR^   s   mFo-DFcR{   RT   RD   R.   R}   c         S`  s	   |  d S(   Ni   (    (   R~   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyR   š  s    i   t   identityR¬   R¡   R¢   R£   R¤   R¥   R¦   N((   Rm   Rn   t   identifyt   AtomPropertiest   get_atom_typeR¾   t   watert
   WATER_POORR    R   t   DEFAULT_IONSRš   t   strt   unicodet   replacet   splitR9   R:   R]   t   mmtbx.ions.environmentRÂ   RÃ   t   nearby_atomst   get_map_gaussian_fitR™   R�   R.   RŒ   Rz   Ro   t   get_metal_parameterst	   min_scoret   min_fraction_of_nextR   t   show_propertiesRŸ   t	   pdb_atomst   id_strt   getvalueR£   (   R«   R    t   debugt
   candidatest
   atom_propst   expected_atom_typet   auto_candidatesRb   RÂ   RÃ   R=   RO   t   predictionsR¦   t
   best_guessR´   t
   next_guesst
   next_scoret   atom_info_outt   predt   result(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   analyze_waterg  sj    	%						c   
      C`  sT  |  j  ƒ  } | d t | ƒ IJ| d IJt | ƒ d k r@ d Sg  } xH | D]@ } |  j d | d | d | ƒ } | d k	 rM | j | ƒ qM qM Wg  } xV | D]N }	 | rÍ |	 j d | d	 d
 ƒ | d IJn  |	 j d k	 rž | j |	 ƒ qž qž Wt | ƒ d k r| d IJ| d IJn7 t d d | ƒx$ | D] }	 |	 j d | d	 d
 ƒ q0W| S(   s  
    Uses a SVM to analyze all of a model's water sites and decide whether to
    re-assign them as ions.

    Parameters
    ----------
    out : file, optional
    debug : bool, optional
    candidates : list of str, optional

    Returns
    -------
    list of svm_prediction
    s     %d waters to analyzeR§   i    NR    RÚ   RÛ   R¬   R­   s     s1     No waters could be classified as possible ions.s   Predicted ions(	   t   _extract_watersRz   Ræ   R    R]   R±   R¦   R   R¶   (
   R«   R¬   RÚ   RÛ   t   watersRß   R    t
   predictiont   filteredRå   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   analyze_waters±  s6    
 		
(   R·   R¸   t   TrueR   Ræ   R»   R¼   Rë   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyR½   f  s   J(E   R¹   t
   __future__R    R   t   collectionsR   t	   cStringIOR   t   ctypesR   R+   R$   R»   t   libtbxR   R   t   libtbx.easy_pickleR   t   libtbx.str_utilsR   t   libtbx.utilsR	   t   libtbx.load_envt   cctbx.eltbxR
   t	   iotbx.pdbR   R<   RÐ   R   t   mmtbx.ions.geometryR   t   mmtbx.ions.identifyRm   t   mmtbx.ions.svmR   t   numpyRG   R�   R   t   ImportErrorR    t   envt   find_in_repositoriesR%   t   isdirR'   R"   R#   R-   Rk   R5   R?   Rì   t   FalseRU   RI   RJ   RK   RL   RM   RN   R™   Rq   t   svm_phil_strRŸ   Rn   RÆ   R½   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/__init__.pyt   <module>   sh   
-=	I '1K	3;