Chem.FragmentCatalog

Package Chem :: Module FragmentCatalog |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.FragmentCatalog

      1  # $Id: FragmentCatalog.py 2 2006-05-06 22:54:39Z glandrum $
      2  #
      3  #  Copyright (C) 2003-2006 Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  import Chem
      8  from Numeric import *
      9  from rdfragcatalog import *
     10  import sys
     11  import sets
     12
     13 -def message(msg,dest=sys.stdout):
     14    dest.write(msg)
     15
     16
     17 -class BitGainsInfo(object):
     18    id=-1
     19    description=''
     20    gain=0.0
     21    nPerClass=None
     22
     23 -def ProcessGainsFile(fileName,nToDo=-1,delim=',',haveDescriptions=1):
     24    inFile = open(fileName,'r')
     25    nRead = 0
     26    res = []
     27    for line in inFile.xreadlines():
     28      nRead += 1
     29      splitL = [x.strip() for x in line.split(delim)]
     30      if nRead != 1 and len(splitL):
     31        bit = BitGainsInfo()
     32        bit.id = int(splitL[0])
     33        col = 1
     34        if haveDescriptions:
     35          bit.description = splitL[col]
     36          col += 1
     37        bit.gain = float(splitL[col])
     38        col += 1
     39        nPerClass = []
     40        for entry in splitL[col:]:
     41          nPerClass.append(int(entry))
     42        bit.nPerClass = nPerClass
     43        res.append(bit)
     44        if len(res)==nToDo:
     45          break
     46    return res
     47
     48 -def BuildAdjacencyList(catalog,bits,limitInclusion=1,orderLevels=0):
     49    adjs = {}
     50    levels = {}
     51    bitIds = [bit.id for bit in bits]
     52    for bitId in bitIds:
     53      entry = catalog.GetBitEntryId(bitId)
     54      tmp = []
     55      order = catalog.GetEntryOrder(entry)
     56      s = levels.get(order,sets.Set())
     57      s.add(bitId)
     58      levels[order] = s
     59      for down in catalog.GetEntryDownIds(entry):
     60        id = catalog.GetEntryBitId(down)
     61        if not limitInclusion or id in bitIds:
     62          tmp.append(id)
     63          order = catalog.GetEntryOrder(down)
     64          s = levels.get(order,sets.Set())
     65          s.add(id)
     66          levels[order] = s
     67      adjs[bitId] = tmp
     68    if orderLevels:
     69      # we'll play a little game and sort the indices in each level by
     70      #  the number of downlinks they have:
     71      for order in levels.keys():
     72        ids = levels[order]
     73        counts = [len(adjs[id]) for id in ids]
     74        countOrder = argsort(counts)
     75        l = [ids[x] for x in countOrder]
     76        l.reverse()
     77        levels[order] = l
     78    return adjs,levels
     79
     80 -def GetMolsMatchingBit(mols,bit,fps):
     81    res = []
     82    if isinstance(bit,BitGainsInfo):
     83      bitId = bit.id
     84    else:
     85      bitId = bit
     86    for i,mol in enumerate(mols):
     87      fp = fps[i]
     88      if fp[bitId]:
     89        res.append(mol)
     90    return res
     91
     92  xl = None
     93 -def ShowMolsMatchingBit(mols,bit,fps,actName="",wrapper=None,col=1,row=1):
     94    global xl
     95    from Excel import Molecules
     96    if wrapper is not None:
     97      xl = wrapper
     98    if xl is None:
     99      xl = Molecules.ExcelWrapper()
    100      xl.Workbooks.Add()
    101      xl.ChemdrawNewWorksheet()
    102    mols = GetMolsMatchingBit(mols,bit,fps)
    103    i = row
    104    for mol in mols:
    105      xl[i,col] = Chem.MolToSmiles(mol)
    106      if actName and mol.HasProp(actName):
    107        xl[i,col+1] = mol.GetProp(actName)
    108      i+=1
    109    i = row
    110    for mol in mols:
    111      try:
    112        xl.ChemdrawConvertCellsToMols(i,col)
    113        xl.ChemdrawShowPictures(i,col)
    114      except:
    115        pass
    116      i+=1
    117    return mols
    118