B
    3Rc%              	   @   sd  d Z ddlZddlZddlmZ ejs8ejejdZ	ndZ	e	dddd	dfd
dZ
dd Zd!ddZedkr`eed eed eed eed dddddgZed x"eD ]Zeedeeddd qW ed x"eD ]Zeedeeddd qW ed x$eD ]Zeedeeddd qW ed  x$eD ]Zeedeeddd q@W dS )"z- utility functions with "chemical know-how"

    N)RDConfigz
atomdb.gdbz::RDDataZatomic_data ZsysdbaZ	masterkeyc             C   s  ddddg}ddl m}	 |	|||}
|
 }dd |D }d	|krN|d	 |rdd
|krd|d
 x|D ]}||krj|| qjW d|}d||f }y|| W n tk
r   t	d| dS X |
 }x|D ]}i }x,tt|D ]}|| }|| }|||< qW |d	 }|| |< |r| | d
 }t|| | d< t|dd| | d< t|dd| | d< t|ddd| | d< qW dS )a   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_

    ZNVALZNVAL_NO_FULL_FZNVAL_NO_FULL_DZNVAL_NO_FULLr   )DbModulec             S   s   g | ]}|  qS  )upper).0sr   r   4lib/python3.7/site-packages/rdkit/utils/chemutils.py
<listcomp>0   s    z!GetAtomicData.<locals>.<listcomp>NAMEZCONFIG,zselect %s from atomic_data %szProblems executing command:N   )ignoreFullF)ignoreFullD)r   r   )Zrdkit.Dbaser   ZconnectZcursorappendremovejoinZexecute	ExceptionprintZfetchallrangelenConfigToNumElectrons)ZatomDictZdescriptorsDesiredZdBasetablewhereuserZpasswordZincludeElCountsZextraFieldsr   ZcncZfieldZtoPullZcommandresZatomZtDictiZdescvalnameconfigr   r   r	   GetAtomicData   sF    





r!   c             C   sl   d}t |}|| }g }xJ|D ]B}t|d dkrR||d t|d f q"||d df q"W |S )a   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.

    z([A-Z][a-z]?)([0-9\.]*)r   r   )recompilefindallr   r   float)ZcompStrtargetZtheExprZmatchesr   matchr   r   r	   SplitCompositionQ   s    


r(   c             C   s   |  d}d}xtdt|D ]}||  d}t|d }|rj|dkrj|d ddkrjt|dkrjd}|r|d	kr|d d
dkrt|dkrd}|| }qW |S )aO   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

     r   r   ^   f   
   d)splitr   r   intfind)r    r   r   ZarrZnElr   lZincrr   r   r	   r   s   s    
**r   __main__ZFeZFe3AlZFe99PdAlZTiNiSiSO12Pz[Xe] 4f^12 6s^2z[Xe] 4f^14 5d^6 6s^2z[Xe] 4f^14 5d^10 6s^2z[Xe] 4f^14 5d^10 6s^2 6p^1z
[Xe] 5d^10z
ignore allz				r   )r   r   zignore dzignore fzignore None)r   r   )__doc__osr"   Zrdkitr   ZusePgSQLpathr   Z	RDDataDirZ_atomDbNamer!   r(   r   __name__r   Ztempentryr   r   r   r	   <module>   s:   <"





