Chem.FragmentCatalog

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

# Source Code for Module Chem.FragmentCatalog

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