ó
a#nSc           @   s•  d  Z  d d l Z d d l 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 d d l Z d Z e j d	 ƒ d
 Z e j d ƒ Z d e e e !Z e j d „ Z d d d d d d „ Z d d d d d d „ Z d d d d d d „ Z d d d d d d d d „ Z d d d d d d d „ Z d d „ Z d d d
 d „ Z d e j d d
 d d „ Z d „  Z  d „  Z! d e" f d „  ƒ  YZ# d  „  Z$ e% d! k r‘d d l& Z& e# ƒ  Z' e$ e' ƒ d d" l( m) Z) e  e' ƒ Z* d Z+ d Z, e' j- rÑe* sAe d# ƒ e j. d$ ƒ n  e d% ƒ e& j& ƒ  Z/ e e* d& e' j0 d' e' j1 d( e' j2 ƒZ+ e& j& ƒ  Z3 e d) e3 e/ ƒ e' j4 ree d* ƒ e j5 e+ e6 e' j4 d+ ƒ ƒ qen” e' j4 ree d, ƒ e j7 e6 e' j4 d- ƒ ƒ Z+ e' j8 rey e j7 e6 e' j8 d- ƒ ƒ Z, Wn d Z, qbXe9 e, ƒ e: j; d. ƒ d
 k  rbd Z, qbqen  d Z< e' j= r�e* s”e d/ ƒ e j. d$ ƒ n  e+ s´e d0 ƒ e j. d$ ƒ n  e d1 ƒ e, sÚe9 e, ƒ e' j0 k  r]e e* e+ d& e' j0 d2 e' j> d3 e' j? ƒ\ Z< Z, e' j@ r2e j5 e< e6 e' j@ d+ ƒ ƒ n  e' j8 rŠe j5 e, e6 e' j8 d+ ƒ ƒ qŠq´e e, e* e+ d& e' j0 d2 e' j> d3 e' j? ƒZ< n' e' j@ r´e j7 e6 e' j@ d- ƒ ƒ Z< n  e' jA re jB jC e' jA ƒ re' jD re d4 ƒ e j7 e6 e' jA d- ƒ ƒ ZE n g  ZE d ZF e' jG r¥e* s9e d5 ƒ e j. d$ ƒ n  e+ pBeE s_e d6 ƒ e j. d$ ƒ n  e d7 ƒ e& j& ƒ  Z/ e' jA r‡d
 ZH n d ZH eE se e* e+ d8 e' jI d2 e' j> d3 e' j? d9 e' jJ d: eH ƒ\ ZF ZE e' jA rMe d; ƒ e6 e' jA d+ ƒ ZK e j5 eE eK d
 ƒ eK jL ƒ  qMn3 e e* eE d8 e' jI d2 e' j> d3 e' j? d9 e' jJ ƒZF e& j& ƒ  Z3 e d) e3 e/ ƒ e' jM rÏe6 e' jM d< ƒ ZN e eN eF e+ d3 e' j? ƒqÏn* e' jM rÏe6 e' jM d= ƒ ZO e eO ƒ ZF n  e' jP r‘e+ søe d> ƒ e j. d$ ƒ n  eF se d? ƒ e j. d$ ƒ n  e) ƒ  ZQ eQ jR d@ ƒ e e+ eF dA e' jS dB eQ ƒe' jT rue6 e' jT d< ƒ jR eQ jU ƒ  ƒ qŽe jV jR eQ jU ƒ  ƒ q‘n  d S(C   se    command line utility for working with FragmentCatalogs (CASE-type analysis)

**Usage**

  BuildFragmentCatalog [optional args] <filename>

 filename, the name of a delimited text file containing InData, is required
 for some modes of operation (see below)

**Command Line Arguments**

 - -n *maxNumMols*:  specify the maximum number of molecules to be processed

 - -b: build the catalog and OnBitLists
    *requires InData*

 - -s: score compounds
    *requires InData and a Catalog, can use OnBitLists*

 - -g: calculate info gains
    *requires Scores*

 - -d: show details about high-ranking fragments
    *requires a Catalog and Gains*

 - --catalog=*filename*: filename with the pickled catalog.
    If -b is provided, this file will be overwritten.

 - --onbits=*filename*: filename to hold the pickled OnBitLists.
   If -b is provided, this file will be overwritten
  
 - --scores=*filename*: filename to hold the text score data.
   If -s is provided, this file will be overwritten

 - --gains=*filename*: filename to hold the text gains data.
   If -g is provided, this file will be overwritten

 - --details=*filename*: filename to hold the text details data.
   If -d is provided, this file will be overwritten.

 - --minPath=2: specify the minimum length for a path

 - --maxPath=6: specify the maximum length for a path

 - --smiCol=1: specify which column in the input data file contains
     SMILES

 - --actCol=-1: specify which column in the input data file contains
     activities

 - --nActs=2: specify the number of possible activity values

 - --nBits=-1: specify the maximum number of bits to show details for

iÿÿÿÿN(   t   Chem(   t   RDConfig(   t   FragmentCatalog(   t	   DbConnect(   t
   InfoTheorys
   $Revision$t   :i   t   $s   %sc         C   s   | j  |  ƒ d  S(   N(   t   write(   t   msgt   dest(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   messageP   s    i   i   i
   c         C   s+  | d k r' t j j t j d ƒ } n  t j | | | ƒ } t j | ƒ } t j	 ƒ  } | d k rl | }	 n$ t
 |  d ƒ rŠ t |  ƒ }	 n d }	 x” t |  ƒ D]† \ }
 } |
 |	 k r¹ Pn  |
 r|
 | r|	 d k rö t d |
 |	 | j ƒ  f ƒ qt d |
 | j ƒ  f ƒ n  | j | | ƒ q� W| S(   sD   builds a fragment catalog from a set of molecules in a delimited text block

    **Arguments**

      - suppl: a mol supplier

      - maxPts: (optional) if provided, this will set an upper bound on the
        number of points to be considered

      - groupFileName: (optional) name of the file containing functional group
        information

      - minPath, maxPath: (optional) names of the minimum and maximum path lengths
        to be considered

      - reportFreq: (optional) how often to display status information  

    **Returns**

      a FragmentCatalog
      
  s   FunctionalGroups.txti    t   __len__iÿÿÿÿs   Done %d of %d, %d paths
s   Done %d, %d paths
N(   t   Nonet   ost   patht   joinR   t	   RDDataDirR   t   FragCatParamst   FragCatalogt   FragCatGeneratort   hasattrt   lent	   enumerateR
   t   GetFPLengtht   AddFragsFromMol(   t   supplt   maxPtst   groupFileNamet   minPatht   maxPatht
   reportFreqt   fpParamst   catalogt   fgent   nPtst   it   mol(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   BuildCatalogS   s&    	 t    c         C   sµ  | j  ƒ  } t j | d | f t j ƒ } g  }	 | rU | rU |  d j ƒ  d } n  t j ƒ  }
 |  j ƒ  d } x7|  D]/} | r  | | r  t d | ƒ n  | r�| sÄ t | j	 | ƒ ƒ } n | | d } |
 j
 | | ƒ } |	 j g  | j ƒ  D] } | ^ q÷ ƒ x- t | ƒ D] } | | d | f c d 7<qWx` |	 | d D]@ } | | d d | f c d 8<| | d d | f c d 7<qIWn |	 j g  ƒ | d 7} qx W| |	 f S(   s9   scores the compounds in a supplier using a catalog

    **Arguments**

      - suppl: a mol supplier

      - catalog: the FragmentCatalog

      - maxPts: (optional) the maximum number of molecules to be
        considered

      - actName: (optional) the name of the molecule's activity property.
        If this is not provided, the molecule's last property will be used.

      - acts: (optional) a sequence of activity values (integers).
        If not provided, the activities will be read from the molecules.

      - nActs: (optional) number of possible activity values

      - reportFreq: (optional) how often to display status information  

    **Returns**

      a 2-tuple:

        1) the results table (a 3D array of ints nBits x 2 x nActs)

        2) a list containing the on bit lists for each molecule

  i   i    iÿÿÿÿi   s	   Done %d.
(   R   t   numpyt   zerost   intt   GetPropNamesR   t   FragFPGeneratort   resetR
   t   GetPropt   GetFPForMolt   appendt	   GetOnBitst   range(   R   R    R   t   actNamet   actst   nActsR   t   nBitst   resTblt   oblst   fpgenR#   R$   t   actt   fpt   xt   jt   id(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   ScoreMoleculesƒ   s2     
&$c         C   s¢  | j  ƒ  } | d k r! | }	 n t |  ƒ }	 t j | d | f t j ƒ }
 | rp | rp | d j ƒ  d } n  | j ƒ  x!t d |	 d ƒ D]} | j ƒ  } | s¾ t | j	 | ƒ ƒ } n | | d } | rô | | rô t
 d | |	 f ƒ n  t ƒ  } x' |  | d D] } | j | d ƒ qWx- t | ƒ D] } |
 | d | f c d 7<q4Wx@ | D]8 } |
 | d | f c d 8<|
 | d | f c d 7<q^WqŽ W|
 S(   s    similar to _ScoreMolecules()_, but uses pre-calculated bit lists
    for the molecules (this speeds things up a lot)

    
    **Arguments**

      - bitLists: sequence of on bit sequences for the input molecules

      - suppl: the input supplier (we read activities from here)

      - catalog: the FragmentCatalog

      - maxPts: (optional) the maximum number of molecules to be
        considered

      - actName: (optional) the name of the molecule's activity property.
        If this is not provided, the molecule's last property will be used.

      - nActs: (optional) number of possible activity values

      - reportFreq: (optional) how often to display status information  

    **Returns**

       the results table (a 3D array of ints nBits x 2 x nActs)

  i    i   iÿÿÿÿi   s   Done %d of %d
(   R   R   R'   R(   R)   R*   R,   R1   t   nextR-   R
   t   sett   add(   t   bitListsR   R    R   R2   R3   R4   R   R5   R"   R6   R#   R$   R9   t   idsR=   R<   (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   ScoreFromListsÂ   s0    	
	!i    c	         C   sý  | j  ƒ  }	 | d k  r! |	 } n  | rF | rF |  d j ƒ  d } n  d g |	 }
 t |  d ƒ rq t |  ƒ } n d } t j ƒ  } | r´ t j |	 | t j j	 ƒ } | j
 | ƒ n t j |	 | t j j ƒ } d } g  } x|  D]þ } | sOy t | j | ƒ ƒ } WqYt k
 rKt d | ƒ t d t | j ƒ  ƒ ƒ t | ‚ qYXn
 | | } | rž| | rž| d k r�t d | | f ƒ qžt d | ƒ n  | j | | ƒ } | j | | ƒ | d 7} | râ | j | ƒ qâ qâ W| j | ƒ }
 |
 | f S(	   s“    calculates info gains by constructing fingerprints
    *DOC*

    Returns a 2-tuple:
       1) gains matrix
       2) list of fingerprints
    
  i    iÿÿÿÿR   s$   ERROR: Molecule has no property: %s
s   	Available properties are: %s
s   Done %d of %d.
s	   Done %d.
i   (   R   R*   R   R   R   R+   R   t   InfoBitRankert   InfoTypet   BIASENTROPYt   SetBiasListt   ENTROPYR)   R-   t   KeyErrorR
   t   strR.   t   AccumulateVotesR/   t   GetTopN(   R   R    t   topNR2   R3   R4   R   t   biasListt
   collectFpsR5   t   gainst   nMolsR8   t   rankerR#   t   fpsR$   R9   R:   (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt	   CalcGainsú   sH    
	

c         C   sÇ  t  | d ƒ } | d k  r% | } n  | rJ | rJ |  d j ƒ  d } n  d g | }	 t |  d ƒ ru t  |  ƒ }
 n d }
 | r¬ t j | | t j j ƒ } | j | ƒ n t j | | t j j ƒ } xê t	 |  ƒ D]Ü \ } } | sGy t
 | j | ƒ ƒ } WqQt k
 rCt d | ƒ t d t | j ƒ  ƒ ƒ t | ‚ qQXn
 | | } | r–| | r–|
 d k r…t d | |
 f ƒ q–t d | ƒ n  | | } | j | | ƒ qÔ W| j | ƒ }	 |	 S(   sD    calculates info gains from a set of fingerprints

    *DOC*
    
  i    iÿÿÿÿR   s$   ERROR: Molecule has no property: %s
s   	Available properties are: %s
s   Done %d of %d.
s	   Done %d.
(   R   R*   R   R   RE   RF   RG   RH   RI   R   R)   R-   RJ   R
   RK   RL   RM   (   R   RT   RN   R2   R3   R4   R   RO   R5   RQ   RR   RS   R#   R$   R9   R:   (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   CalcGainsFromFps/  s<    	

c   
      C   s  g  t  | ƒ D] } d | ^ q } | rF |  j d d j | ƒ ƒ n |  j d d j | ƒ ƒ x¯ | D]§ } t | d ƒ } t | ƒ g } | r± | j | ƒ }	 | j |	 ƒ n  | j d | d ƒ | g  | d D] } d	 | ^ qÔ 7} |  j d j | ƒ ƒ |  j d
 ƒ qg Wd  S(   Ns   Act-%ds   id,Description,Gain,%s
t   ,s   id,Gain,%s
i    s   %.6fi   i   s   %ds   
(   R1   R   R   R)   RK   t   GetBitDescriptionR/   (
   t   outFRQ   t   catR4   R;   t
   actHeaderst   entryR=   t   outLt   descr(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   OutputGainsDataZ  s    #%RW   c         C   sc   g  } |  j  ƒ  } xJ |  j ƒ  D]< } | j ƒ  j | ƒ } | j | | t | | ƒ f ƒ q W| S(   s<    reads a list of ids and info gains out of an input file

  (   t   readlinet
   xreadlinest   stript   splitR/   t   float(   t   inFt   delimt   idColt   gainColt   rest   inLt   linet   splitL(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   ProcessGainsDatal  s    %c         C   s¥   | d k  r t  | ƒ } n  xƒ t | ƒ D]u } t | | | ƒ } t | | | ƒ }	 |  j | ƒ }
 |
 r( | j d | j t | ƒ |
 t |	 ƒ f ƒ ƒ q( q( Wd S(   s£   
   gains should be a sequence of sequences.  The idCol entry of each
   sub-sequence should be a catalog ID.  _ProcessGainsData()_ provides
   suitable input.

  i    s   %s
N(   R   R1   R)   Rd   t   GetFragDescriptionR   R   RK   (   R    RQ   t   nToDoRY   Rg   Rh   t   outDelimR#   R=   t   gainR^   (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   ShowDetailsx  s    c      
   C   sÒ  d d l  m } d d l m } |  j rS t |  j |  j ƒ } | | j ƒ  ƒ } n× | |  j d |  j	 d |  j
 d |  j d |  j ƒ} t |  j ƒ t j k rÓ | j ƒ  | j ƒ  } | j ƒ  |  j } | |  _ n  t |  j
 ƒ t j k r*| j ƒ  | j ƒ  } | j ƒ  |  j
 } | |  _
 | j ƒ  n  t |  j ƒ t j k rw| j ƒ  | j ƒ  } | j ƒ  |  j } | |  _ n  t |  j
 ƒ t j k rÎ| j ƒ  | j ƒ  } | j ƒ  |  j
 } | |  _
 | j ƒ  n  | S(   Niÿÿÿÿ(   t   DbMolSupplyNode(   t   SmilesSupplyNodeRf   t
   nameColumnt   smilesColumnt	   titleLine(   t   rdkit.VLib.NodeLib.DbMolSupplyRs   t   rdkit.VLib.NodeLib.SmilesSupplyRt   t   dbNameR   t	   tableNamet   GetDatat
   inFileNameRf   t   nameColt   smiColt   hasTitlet   typet   actColt   typest   IntTypeR,   R?   R*   (   t   detailsRs   Rt   t   connR   t   mR2   t   nameName(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   SupplierFromDetails‰  s@    			

	

	c           C   s   d t  GHd GHt j d ƒ d  S(   Ns'   This is BuildFragmentCatalog version %ss   usage erroriÿÿÿÿ(   t   __VERSION_STRINGt   syst   exit(    (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   Usage®  s    	t
   RunDetailsc           B   s¤   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d  Z d  Z d Z d Z d  Z d Z d Z d  Z RS(   iÿÿÿÿi    R&   i   i   i   RW   N(   t   __name__t
   __module__t   numMolst   doBuildt   doSigst   doScoret   doGainst	   doDetailsR   t   catalogNamet
   onBitsNamet
   scoresNamet	   gainsNameRz   R{   t   detailsNameR}   t   fpNameR   R   R   R‚   R~   R€   R4   R5   Rf   RO   RN   (    (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyRŽ   ´  s4   c         C   s>  d d  l  } ye | j  t j d d d d d d d d	 d
 d d d d d d d d d d d d d d g ƒ \ } } Wn4 t j j d ƒ d d  l } | j ƒ  t ƒ  n XxS| D]K\ } } | d k rÙ t | ƒ |  _	 q¯ | d k rñ d |  _
 q¯ | d k r	d |  _
 q¯ | d k r!d  |  _
 q¯ | d! k r9| |  _ q¯ | d" k rQd |  _ q¯ | d# k rid |  _ q¯ | d$ k r�d |  _ q¯ | d% k r™d |  _ q¯ | d& k r±d |  _ q¯ | d' k rÉ| |  _ q¯ | d( k rá| |  _ q¯ | d) k rù| |  _ q¯ | d* k r| |  _ q¯ | d+ k r)| |  _ q¯ | d, k rA| |  _ q¯ | d- k r_t | ƒ |  _ q¯ | d. k r}t | ƒ |  _ q¯ | d/ k r¼y t | ƒ |  _ Wqút k
 r¸| |  _ qúXq¯ | d0 k rûy t | ƒ |  _ Wqút k
 r÷| |  _ qúXq¯ | d1 k r:y t | ƒ |  _ Wqút k
 r6| |  _ qúXq¯ | d2 k rXt | ƒ |  _ q¯ | d3 k rvt | ƒ |  _ q¯ | d4 k rŽd5 |  _ q¯ | d6 k r²t  t! | ƒ ƒ |  _" q¯ | d7 k rÐt | ƒ |  _# q¯ | d8 k rót ƒ  t j$ d5 ƒ q¯ t ƒ  q¯ Wt% | ƒ r3|  j r#| d5 |  _& q:| d5 |  _' n t ƒ  d  S(9   Niÿÿÿÿi   s   n:d:csts   catalog=s   onbits=s   scoresFile=s
   gainsFile=s   detailsFile=s   fpFile=s   minPath=s   maxPath=s   smiCol=s   actCol=s   nameCol=s   nActs=s   nBits=s	   biasList=s   topN=t   buildt   sigsRQ   R…   t   scoret   noTitles   Error parsing command line:
s   -ns   -cRW   s   -st    s   -ts   	s   -ds   --builds   --scores   --gainss   --sigss   -detailss	   --catalogs   --onbitss   --scoresFiles   --gainsFiles   --detailsFiles   --fpFiles	   --minPaths	   --maxPaths   --smiCols   --actCols	   --nameCols   --nActss   --nBitss	   --noTitlei    s
   --biasLists   --topNs   -h((   t   getoptR‹   t   argvt   stderrR   t	   tracebackt	   print_excR�   R)   R‘   Rf   Rz   R’   R”   R•   R“   R–   R—   R˜   R™   Rš   R›   Rœ   R   R   R   t
   ValueErrorR‚   R~   R4   R5   R€   t   tuplet   evalRO   RN   RŒ   R   R{   R}   (   R…   R¢   t   argst   extrasR¥   t   argt   val(    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt	   ParseArgsÐ  s¨    	%
	t   __main__(   t   StringIOs(   We require inData to generate a catalog
iþÿÿÿs   Building catalog
R   R   R   s   	That took %.2f seconds.
s   Dumping catalog data
s   wb+s   Loading catalog
t   rbs   
s%   We require inData to score molecules
s(   We require a catalog to score molecules
s   Scoring compounds
R2   R4   s    Reading fingerprints from file.
s%   We require inData to calculate gains
s?   We require either a catalog or fingerprints to calculate gains
s   Calculating Gains
RN   RO   RP   s   Writing fingerprint file.
s   w+t   rs$   We require a catalog to get details
s%   We require gains data to get details
s   id,SMILES,gain
Ro   RY   (W   t   __doc__R‹   R   t   cPicklet   rdkitR    R   t
   rdkit.ChemR   t   rdkit.Dbase.DbConnectionR   R'   t   rdkit.MLR   Rƒ   t   _cvsVersiont   findt   idx1t   rfindt   idx2RŠ   t   stdoutR
   R   R%   R>   RD   RU   RV   R_   Rm   Rr   R‰   R�   t   objectRŽ   R®   R�   t   timeR…   t	   cStringIOR°   R   RZ   R7   R’   RŒ   t   t1R‘   R   R   t   t2R—   t   dumpt   opent   loadR˜   R   t   inDt   countt   scoresR”   R‚   R4   R™   Rœ   R   t   existsR“   RT   RQ   R•   RP   RN   RO   t   tmpFt   closeRš   RY   Re   R–   t   ioR   R5   R›   t   getvalueR¤   (    (    (    sa   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/BuildFragmentCatalog.pyt   <module>A   s
  $/	>	7	4	*	%		X	
	

	
"	
	
	


			"		(
	


			
			

		"