ó
Ê™xTc           @   s±   d  Z  d d l m Z y d d l Z Wn e k
 r? d Z n Xd d l m Z d d l	 m
 Z
 d d l	 m Z d d l m Z e d d „ Z e d	 „ Z d
 „  Z d „  Z d S(   s,   
Utility functions used within this module.
iÿÿÿÿ(   t   divisionN(   t   Auto(   t   server(   t   halides(   t   colc         c   s	  x|  j  ƒ  D]ô } xë | j ƒ  D]Ý } xÔ | j ƒ  D]Æ } x½ | j ƒ  D]¯ } | j j ƒ  j ƒ  } | j j ƒ  d k rƒ | rƒ qF n  | d k s› | | k rF | j	 ƒ  } t
 | ƒ d k rõ | d }	 |	 j j ƒ  j ƒ  }
 |
 d k rê qF n  |	 Vqõ qF qF Wq3 Wq  Wq Wd S(   s!  
  Returns a generator iterating over all atoms in pdb_hierarchy. Optionally
  skips sites with alternate conformations and can filter by residue name.

  Parameters
  ----------
  pdb_hierarchy: iotbx.pdb.hierarchy.root
  split_sites: bool, optional
      Indicates whether to iterate over sites with alternate conformations, by
      default they are not included.
  res_filter: list of str, optional
      List of residue names to include, by default, all residues are examined.

  Returns
  -------
  generator of iotbx.pdb.hierarchy.atom
  t    i   i    t   Ht   DN(   R   R   (   t   modelst   chainst   residue_groupst   atom_groupst   resnamet   stript   uppert   altloct   Nonet   atomst   lent   element(   t   pdb_hierarchyt   split_sitest
   res_filtert   modelt   chaint   residue_groupt
   atom_groupR   R   t   atomR   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pyt   iterate_sites   s    
c         C   sÍ  t  } |  j } t } t } t } t } t | j ƒ } d  }	 |	 }
 xê|  j D]ß} | j } | j ƒ  } | j ƒ  } | j	 } t
 | ƒ } | d k  r˜ t S| d k rt j | ƒ } | d k  rÏ | d k rÏ t S| d k r+| d k r+t  } |	 d  k s| |	 k  r| }	 qq+qL | d k rb| d k rb| d k rbt  } |	 d  k sV| |	 k  r+| }	 q+qL | d k rÎ| d  k rÎ| sŒ| d k rÎ| d k rÎt  } | d k r+|	 d  k sÂ| |	 k  r+| }	 q+qL | d! k r| d" k r| d k rt  } | |
 k  r+| }
 q+qL | d# k rL | rL t  } qL qL Wx‹ |  j D]€ } | j ƒ  d$ k rWq9n  | j ƒ  } | j ƒ  } t
 | ƒ } | d k  r9|	 d  k	 r9| |	 d k  r9t j | | ƒ r9t Sq9W| pÌ| pÌ| S(%   Ng      @t   Ct   NR   t   Ot   Si    g      @t   NZt   LYSt   NH1t   NH2t   ND2t   NE2t   ARGt   ASNt   GLNt   OG1t   OG2t   OH1t   SERt   THRt   TYRR   t   Agš™™™™™	@gš™™™™™É?(   R   R   R   R   R    (   R!   (   R"   (   R!   R#   R$   R%   R&   (   R'   R(   R)   (   R*   R+   R,   (   R-   R.   R/   (   R   (   R   R0   (   t   TrueR   t   FalseR   t   xyzR   t   contactsR   t	   atom_nameR   t   absR   t
   get_chargeR   R   t   is_negatively_charged_oxygen(   t   chem_envt   scatter_envt   assume_hydrogens_all_missingR   t   binds_amide_hydrogent   near_cationt   near_lyst   near_hydroxylR3   t   min_distance_to_cationt   min_distance_to_hydroxylt   contactt   otherR   R5   R   t   distancet   charge(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pyt    _is_favorable_halide_environment4   s|    			c   
      C   s=  d } d } g  } x$| D]} | d k r(| j  d d k  rD q n  | d k rl | j d d k  rl q ql n  |  j | ƒ \ } } | d k rß t j | ƒ }	 | d | t |	 ƒ k r¼ q n  | d | t |	 ƒ k rô q qô n t |  | ƒ sô q n  | | k rq n  t d „  |  j	 Dƒ ƒ r(q q(n  | j
 | ƒ q W| S(   s|  
  Applies a simple set of filters to the accepted ions that might match a given
  chemical and scattering environment to help catch corner cases where the SVM
  might fail.

  Parameters
  ----------
  chem_env : mmtbx.ions.environment.ChemicalEnvironment
  scatter_env : mmtbx.ions.environment.ScatteringEnvironment
  elements : list of str

  Returns
  -------
  list of str
  g      à?g333333ã?t   HOHi    i   t   NAt   MGt   Ft   CLt   BRt   Ic         s   s$   |  ] } t  | j ƒ d  k  Vq d S(   gÍÌÌÌÌÌü?N(   R6   t   vector(   t   .0t   i(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pys	   <genexpr>Ä   s    (   RH   RI   (   RJ   RK   RL   RM   (   t
   fo_densityt   fofc_densityt   get_valenceR   t   get_chargest   mint   maxRF   t   anyR4   t   append(
   R9   R:   t   elementst	   bvs_ratiot   vecsum_cutofft   ok_elementsR   t   bvst   vecsumt   charges(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pyt   filter_svm_outputs—   s2    			c         C   s‰   t  j |  ƒ }  | d | d k } |  d d … | f }  | d | | d | f } |  | d | d | d | d | d | d S(   s›  
  Given an upper and lower bound for each row of matrix, scales the values to be
  within the range specified by target.

  Parameters
  ----------
  matrix : numpy.array of float
      The matrix to be scaled.
  source : tuple of numpy.array of float
      The upper and lower bound on the values of each row in the original
      matrix.
  target : tuple of float
      The target range to scale to.

  Returns
  -------
  matrix : numpy.array of float
      The matrix with scaled values.

  Examples
  --------
  >>> from mmtbx.ions.svm.utils import scale_to
  >>> import numpy as np
  >>> matrix = np.array([[0, 1, 2],
                         [2, 3, 4],
                         [1, 2, 3]])
  >>> source = (np.array([2, 3, 4]),
                np.array([0, 1, 2]))
  >>> target = (0, 1)
  >>> scale_to(matrix, source, target)
  array([[ 1. ,  1. ,  1. ],
         [ 0. ,  0. ,  0. ],
         [ 0.5,  0.5,  0.5]])
  i    i   N(   t   npt   array(   t   matrixt   sourcet   targett	   keep_rows(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pyt   scale_toÉ   s    #(   t   __doc__t
   __future__R    t   numpyRa   t   ImportErrorR   t   libtbxR   t
   mmtbx.ionsR   R   t   scitbx.matrixR   R2   R   RF   R`   Rg   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/svm/utils.pyt   <module>   s   
$b	2