ó
a#nSc           @   s  d  Z  d d l m Z d d l m Z m Z d „  Z d d l m Z d d l	 Z	 d d l
 Z
 d d l Z d d l m Z d d	 d
 d d „ Z d d
 d d	 d d
 d d d d d d „ Z d d
 d
 d d	 d d d „ Z d d „ Z d „  Z d d „ Z e d „ Z d d d d „ Z d d d	 d d d d „ Z d d
 d
 d d	 d d d „ Z e d k rýd d l Z e j ƒ  Z e j d ƒ e j d ƒ e j d ƒ e j d  ƒ e j d ƒ d d l m Z d d l Z e j  j! e j" d! d" ƒ Z# e e# d# e ƒ n  d S($   s‡    a set of functions for interacting with databases

 When possible, it's probably preferable to use a _DbConnection.DbConnect_ object

iÿÿÿÿ(   t   RDConfig(   t   DbResultSett   RandomAccessDbResultSetc         C   s   t  |  d „ | ƒ S(   Nc         S   s   | |  S(   N(    (   t   xt   y(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   <lambda>   s    (   t   map(   t   fromLt   what(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   _take   s    (   t   DbModuleN(   t   DbInfot   sysdbat	   masterkeyt    c   	      C   s�   | s t  j |  | | ƒ } n  | j ƒ  } d | | f } | ry | j ƒ  j d ƒ d k rh d | } n  | d | 7} n  | j | ƒ | j ƒ  S(   sp   gets a set of data from a table

    **Arguments**

     - dBase: database name

     - table: table name
     
     - fieldString: a string with the names of the fields to be extracted,
        this should be a comma delimited list

     - user and  password:

     - join: a join clause (omit the verb 'join')
       

    **Returns**

     - a list of the data

  s   select %s from %st   joini    s   join %st    (   R
   t   connectt   cursort   stript   findt   executet   fetchall(	   t   dBaset   tablet   fieldStringt   usert   passwordR   t   cnt   ct   cmd(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt
   GetColumns   s    t   *i    i   c      
   C   së  | s t  j |  | | ƒ } n  | j ƒ  } d | | f } | ry | j ƒ  j d ƒ d k rh d | } n  | d | 7} n  | r¸ | j ƒ  j d ƒ d k r§ d | } n  | d | 7} n  | r®y* | s× | j | ƒ n | j | | ƒ Wn2 t j j d | ƒ d	 d
 l	 } | j
 ƒ  d
 SX|	 d
 k	 r5t d ‚ n  |
 sGt d ‚ n  | j ƒ  } | d k rçg  } xC | D]7 } | | | k r“| j | ƒ qm| j | | ƒ qmWqçn9 |
 r½t } n t } | | | | d | d |	 d | ƒ} | S(   s#   a more flexible method to get a set of data from a table

    **Arguments**

     - fields: a string with the names of the fields to be extracted,
          this should be a comma delimited list

     - where: the SQL where clause to be used with the DB query

     - removeDups indicates the column which should be used to screen
        out duplicates.  Only the first appearance of a duplicate will
        be left in the dataset.

    **Returns**

      - a list of the data


    **Notes**

      - EFF: this isn't particularly efficient

  s   select %s from %sR   i    s   join %sR   t   wheres   where %ss#   the command "%s" generated errors:
iÿÿÿÿNs4   forceList and transform arguments are not compatibles5   when forceList is set, randomAccess must also be usedt
   removeDupst	   transformt   extras(   R
   R   R   R   R   R   t   syst   stderrt   writet	   tracebackt	   print_exct   Nonet
   ValueErrorR   t   removet   appendR   R   (   R   R   R   t   whereStringR   R   R"   R   t	   forceListR#   t   randomAccessR$   R   R   R   R(   t   datat   seent   entryt   klass(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   GetData;   sL    
	$t   ,c	         C   s–  t  | ƒ r4 | j ƒ  j d ƒ d k r4 d | } n  t  | ƒ rh | j ƒ  j d ƒ d k rh d | } n  d | | | | f }	 | sœ t j |  | | ƒ } n  | j ƒ  }
 |
 j |	 ƒ g  } g  } x^ t t  |
 j ƒ ƒ D]G } |
 j | } | d t	 j
 k r× | j | ƒ | j | d ƒ q× q× Wg  } | j | j | ƒ ƒ |
 j ƒ  } x< | D]4 } t | | ƒ } | j | j t t | ƒ ƒ ƒ qQWd	 j | ƒ S(
   sW   Pulls the contents of a database and makes a deliminted text file from them

    **Arguments**
      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - fields: the fields to select with the SQL query

      - join: the join clause of the SQL query
        (e.g. 'join foo on foo.bar=base.bar')

      - where: the where clause of the SQL query
        (e.g. 'where foo = 2' or 'where bar > 17.6')

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - the CSV data (as text) 

  R!   iÿÿÿÿs   where %sR   s   join %ss   select %s from %s %s %si   i    s   
(   t   lenR   R   R
   R   R   R   t   ranget   descriptionR   t   sqlBinTypesR-   R   R   R	   R   t   str(   R   R   t   fieldsR   R!   R   R   t   delimR   t
   sqlCommandR   t   headerst
   colsToTaket   it   itemt   linest   resultst   rest   d(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   DatabaseToText‚   s.    ''#c         C   s  i d t  j 6d t  j 6d t  j 6d d 6} d g | } xàt | ƒ D]Ò} d d g } x³t | ƒ D]¥} |  | | }	 |	 d k	 rd t |	 ƒ }
 |
 t  j k r|
 t  j k rt  j }
 y t |	 ƒ }	 Wq.t k
 r} d | d | f GHd t	 |	 ƒ GHt | ‚ q.Xn# t
 | d t t |	 ƒ ƒ ƒ | d <|
 t  j k rƒ| d k sU|	 | k rt
 t |	 ƒ | d ƒ } t  j | g } qq	y t t |	 ƒ ƒ } Wn t k
 rµt  j }
 n X| |	 k rÎt  j }
 n  | d t  j k r	| |
 | | d k r	|
 | d <q	qd qd W| | | <qE W| S(	   sÒ    

    finds the types of the columns in _data_

    if nullMarker is not None, elements of the data table which are
      equal to nullMarker will not count towards setting the type of
      their columns.

  i   i   i   iÿÿÿÿs2   cannot convert text from row %d col %d to a strings   	>%si    N(   t   typest	   FloatTypet   IntTypet
   StringTypeR*   t   xranget   typeR;   t   UnicodeErrort   reprt   maxR7   t   floatt   intt   OverflowError(   R1   t   nRowst   nColst
   nullMarkert
   prioritiesRE   t   colt   typeHeret   rowRF   t   locTypet   msgt   lt   fD(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt
   TypeFinder»   s@    
+	#c         C   sè   xá t  t |  ƒ ƒ D]Í } t j |  | ƒ |  | <t j |  | d d ƒ |  | <t j |  | d d ƒ |  | <t j |  | d d ƒ |  | <t |  | ƒ | k r t j |  | d d ƒ } | |  } d |  | | f GH| |  | <q q W|  S(   s|    *For Internal Use*

    removes illegal characters from column headings
    and truncates those which are too long.
    
  R   t   _t   -t   .R   s#   	Heading %s too long, changed to %s(   RL   R7   t   stringR   t   replace(   t   colHeadingst   maxColLabelLenRA   t   newHead(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   _AdjustColHeadingsé   s    
c         C   sË   g  } x¾ t  t | ƒ ƒ D]ª } | | } | d t j k rT | j d |  | ƒ nJ | d t j k r | j d |  | ƒ n | j d |  | | d f ƒ |  | | k r d | d | d <q q W| S(   s'     returns a list of SQL type strings
  i    s   %s double precisions
   %s integers   %s varchar(%d)i   s   %s not null primary keyiÿÿÿÿ(   RL   R7   RH   RI   R-   RJ   (   Re   t   colTypest   keyColt   typeStrsRA   RM   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   GetTypeStringsÿ   s    
c         C   sÊ   y |  j  ƒ  j | | ƒ Wn� d } |  j ƒ  x“ | D]{ } y* |  j  ƒ  j | t | ƒ ƒ | d 7} Wn> | s² d d  l } | j ƒ  d G| GHd Gt | ƒ GHq² q7 X|  j ƒ  q7 Wn Xt | ƒ } | S(   Ni    i   iÿÿÿÿs   insert failed:s   	(	   R   t   executemanyt   commitR   t   tupleR(   R)   RO   R7   (   t   connt   sqlStrt   blockt   silentRE   RZ   R(   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   _insertBlock  s$    

	id   c
         C   sœ  |	 s t  j |  | | ƒ }	 n  |	 j ƒ  }
 y |
 j d | ƒ Wn d | GHn Xy! d | | f } |
 j | ƒ Wn, d G| GHd GHd d l } | j ƒ  d SX|	 j ƒ  d }
 g  } t  j g t	 | d ƒ } d	 j
 | ƒ } d
 | | f } d } xR| D]J} d g t	 | ƒ } xÃ t t	 | ƒ ƒ D]¯ } | | d k	 rÏ| d k s\| | | k rÏ| | d t j k rŠt | | ƒ | | <qÙ| | d t j k r¸t | | ƒ | | <qÙt | | ƒ | | <q*d | | <q*W| j t | ƒ ƒ t	 | ƒ | k rþ | t |	 | | ƒ 7} t |	 d ƒ s2|	 j r?|	 j ƒ  n  g  } qþ qþ Wt	 | ƒ rq| t |	 | | ƒ 7} n  t |	 d ƒ s‹|	 j r˜|	 j ƒ  n  d S(   sT    *For Internal Use*

    (drops and) creates a table and then inserts the values

  s   drop table %ss   cannot drop table %ss   create table %s (%s)s   create table failed: s   here is the exception:iÿÿÿÿNi    R6   s   insert into %s values (%s)t
   autocommit(   R
   R   R   R   R(   R)   Rn   R*   t   placeHolderR7   R   RL   RH   RI   RQ   RJ   RR   R;   R-   Ro   Rt   t   hasattrRu   (   R   R   R   R   t   colDefsRi   R1   RV   t	   blockSizeR   R   Rq   R(   Rr   t   entryTxtt   dStrt   nDoneRZ   t   entriesRX   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   _AddDataToDb%  sX    	

i   c	      
   C   sÇ  | j  d d ƒ | j  d d ƒ | j ƒ  j | ƒ }	 t |	 | ƒ t |	 ƒ }
 g  } | j ƒ  } xö | rX| j  d d ƒ } | j  d d ƒ } | j | ƒ } t | ƒ |
 k rÞ d Gt | ƒ GHt | ƒ |
 k sÞ t d ƒ ‚ n  g  } xU | D]M } y t | ƒ } Wn' y t | ƒ } Wq+| } q+Xn X| j	 | ƒ që W| j	 | ƒ | j ƒ  } qc Wt | ƒ } t
 | | |
 d	 | ƒ} t |	 | d
 | ƒ} d j | ƒ } t |  | | | | | | d	 | ƒd S(   s8  loads the contents of the text file into a database.

    **Arguments**

      - dBase: the name of the DB to use

      - table: the name of the table to create/overwrite

      - inF: the file like object from which the data should
        be pulled (must support readline())

      - delim: the delimiter used to separate fields  

      - user: the user name to use in connecting to the DB

      - password: the password to use in connecting to the DB
      
      - maxColLabelLen: the maximum length a column label should be
        allowed to have (truncation otherwise)

      - keyCol: the column to be used as an index for the db

    **Notes**

      - if _table_ already exists, it is destroyed before we write
        the new data

      - we assume that the first row of the file contains the column names

  Ra   R`   R   s   R   s   
s   >>>s   unequal lengthRV   Rj   R6   N(   Rd   t   readlinet   splitRh   R7   RO   t   AssertionErrorRR   RQ   R-   R_   Rl   R   R~   (   R   R   t   inFR=   R   R   Rf   Rj   RV   Re   RU   R1   t   inLt   splitLt   tmpVectR3   t   valRT   Ri   Rk   Rx   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   TextFileToDatabase\  s@    !	!R*   c         C   s…   d d l  } | j ƒ  } | j t |  | d | d | d | d | d | ƒƒ | j d ƒ t | | | d | d | d |	 d	 |
 ƒd S(
   s-   

   FIX: at the moment this is a hack
   
  iÿÿÿÿNR<   R   R!   R   R   Rj   RV   (   t	   cStringIOt   StringIOR'   RG   t   seekR‡   (   t   fromDbt   fromTblt   toDbt   toTblR<   R   R!   R   R   Rj   RV   Rˆ   t   io(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   DatabaseToDatabase£  s    $!t   __main__s   foo,bar,baz
s   1,2,3
s   1.1,4,5
s   4,foo,6
t   Dbases   TEST.GDBt   fromtext($   t   __doc__t   rdkitR    t   rdkit.Dbase.DbResultSetR   R   R	   t   rdkit.DbaseR
   R%   RH   Rc   R   R*   R   R5   RG   R_   Rh   Rl   t   FalseRt   R~   R‡   R�   t   __name__Rˆ   R‰   R�   R'   RŠ   t   ost   pathR   t	   RDCodeDirt   dirLoc(    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbUtils.pyt   <module>   sD   	$!F	8.	6F	