ó
a#nSc           @   s¿  d  Z  d d l m Z d d l Z d d l Z d d l Z e j s^ e j j e j	 d ƒ Z
 n d Z
 e
 d d d d	 d
 d „ Z d „  Z d
 d
 d „ Z e d k r»e d ƒ GHe d ƒ GHe d ƒ GHe d ƒ GHd d d d d g Z d GHx- e D]% Z e Gd Ge e d d d d ƒGHqð Wd GHx- e D]% Z e Gd Ge e d d d d
 ƒGHq%Wd GHx- e D]% Z e Gd Ge e d d
 d d ƒGHqZWd GHx0 e D]% Z e Gd Ge e d d
 d d
 ƒGHq�Wn  d S(    s-    utility functions with "chemical know-how"

iÿÿÿÿ(   t   RDConfigNs
   atomdb.gdbs   ::RDDatat   atomic_datat    t   sysdbat	   masterkeyi    c         C   sü  d d d d g } d d l  m }	 |	 j | | | ƒ }
 |
 j ƒ  } t t j | ƒ } d | k rq | j d ƒ n  | r“ d | k r“ | j d ƒ n  x* | D]" } | | k rš | j | ƒ qš qš Wt j	 | d	 ƒ } d
 | | f } y | j
 | ƒ Wn d G| GHd SX| j ƒ  } xâ | D]Ú } i  } x8 t t | ƒ ƒ D]$ } | | } | | } | | | <q9W| d } | |  | <| r|  | d } t | ƒ |  | d <t | d d ƒ|  | d <t | d d ƒ|  | d <t | d d d d ƒ|  | d <qqWd S(   sÝ   pulls atomic data from a database

    **Arguments**

      - atomDict: the dictionary to populate

      - descriptorsDesired: the descriptors to pull for each atom

      - dBase: the DB to use

      - table: the DB table to use

      - where: the SQL where clause

      - user: the user name to use with the DB

      - password: the password to use with the DB

      - includeElCounts: if nonzero, valence electron count fields are added to
         the _atomDict_

  t   NVALt   NVAL_NO_FULL_Ft   NVAL_NO_FULL_Dt   NVAL_NO_FULLiÿÿÿÿ(   t   DbModulet   NAMEt   CONFIGt   ,s   select %s from atomic_data %ss   Problems executing command:Nt   ignoreFullFi   t   ignoreFullD(   t   rdkit.DbaseR	   t   connectt   cursort   mapt   stringt   uppert   appendt   removet   joint   executet   fetchallt   xranget   lent   ConfigToNumElectrons(   t   atomDictt   descriptorsDesiredt   dBaset   tablet   wheret   usert   passwordt   includeElCountst   extraFieldsR	   t   cnt   ct   fieldt   toPullt   commandt   rest   atomt   tDictt   it   desct   valt   namet   config(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/chemutils.pyt   GetAtomicData   sF    	



c         C   s�   d } t  j | ƒ } | j |  ƒ } g  } x_ | D]W } t | d ƒ d k rq | j | d t | d ƒ f ƒ q1 | j | d d f ƒ q1 W| S(   s•   Takes a simple chemical composition and turns into a list of element,# pairs.

      i.e. 'Fe3Al' -> [('Fe',3),('Al',1)]

      **Arguments**

       - compStr: the composition string to be processed

      **Returns**

       - the *composVect* corresponding to _compStr_

      **Note**

        -this isn't smart enough by half to deal with anything even
            remotely subtle, so be gentle.

  s   ([A-Z][a-z]?)([0-9\.]*)i   i    (   t   ret   compilet   findallR   R   t   float(   t   compStrt   targett   theExprt   matchesR+   t   match(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/chemutils.pyt   SplitCompositionM   s    $c         C   sþ   t  j |  d ƒ } d } xß t d t | ƒ ƒ D]È } t  j | | d ƒ } t | d ƒ } | r£ | d k r£ t  j | d d ƒ d k r£ t | ƒ d k r£ d } n  | rì | d	 k rì t  j | d d
 ƒ d k rì t | ƒ d k rì d } n  | | } q. W| S(   sG   counts the number of electrons appearing in a configuration string

    **Arguments**

      - config: the configuration string (e.g. '2s^2 2p^4')

      - ignoreFullD: toggles not counting full d shells

      - ignoreFullF: toggles not counting full f shells

    **Returns**

      the number of valence electrons
      
  t    i    i   t   ^i   t   fiÿÿÿÿi   i
   t   d(   R   t   splitt   rangeR   t   intt   find(   R2   R   R   t   arrt   nElR.   t   lt   incr(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/chemutils.pyR   o   s    @	@	t   __main__t   Fet   Fe3Alt   Fe99PdAlt   TiNiSiSO12Ps   [Xe] 4f^12 6s^2s   [Xe] 4f^14 5d^6 6s^2s   [Xe] 4f^14 5d^10 6s^2s   [Xe] 4f^14 5d^10 6s^2 6p^1s
   [Xe] 5d^10s
   ignore alls   				R   i   R   s   ignore ds   ignore fs   ignore None(   t   __doc__t   rdkitR    R   R4   t   ost   usePgSQLt   pathR   t	   RDDataDirt   _atomDbNameR3   R=   R   t   __name__t   tempt   entry(    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/chemutils.pyt   <module>   s<   $	;	"	###