Package rdkit :: Package Chem :: Module FragmentCatalog

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.FragmentCatalog

     1  # $Id$
     2  #
     3  #  Copyright (C) 2003-2008 Greg Landrum and Rational Discovery LLC
     4  #
     5  #   @@ All Rights Reserved @@
     6  #  This file is part of the RDKit.
     7  #  The contents are covered by the terms of the BSD license
     8  #  which is included in the file license.txt, found at the root
     9  #  of the RDKit source tree.
    10  #
    11  import sys
    12
    13  from rdkit import Chem
    14  from rdkit.Chem.rdfragcatalog import *
    15
    16
    17 -def message(msg,dest=sys.stdout):
    18    dest.write(msg)
    19
    20
    21 -class BitGainsInfo(object):
    22    id=-1
    23    description=''
    24    gain=0.0
    25    nPerClass=None
    26
    27 -def ProcessGainsFile(fileName,nToDo=-1,delim=',',haveDescriptions=1):
    28    inFile = open(fileName,'r')
    29    nRead = 0
    30    res = []
    31    for line in inFile.xreadlines():
    32      nRead += 1
    33      splitL = [x.strip() for x in line.split(delim)]
    34      if nRead != 1 and len(splitL):
    35        bit = BitGainsInfo()
    36        bit.id = int(splitL[0])
    37        col = 1
    38        if haveDescriptions:
    39          bit.description = splitL[col]
    40          col += 1
    41        bit.gain = float(splitL[col])
    42        col += 1
    43        nPerClass = []
    44        for entry in splitL[col:]:
    45          nPerClass.append(int(entry))
    46        bit.nPerClass = nPerClass
    47        res.append(bit)
    48        if len(res)==nToDo:
    49          break
    50    return res
    51
    52 -def BuildAdjacencyList(catalog,bits,limitInclusion=1,orderLevels=0):
    53    adjs = {}
    54    levels = {}
    55    bitIds = [bit.id for bit in bits]
    56    for bitId in bitIds:
    57      entry = catalog.GetBitEntryId(bitId)
    58      tmp = []
    59      order = catalog.GetEntryOrder(entry)
    60      s = levels.get(order,set())
    61      s.add(bitId)
    62      levels[order] = s
    63      for down in catalog.GetEntryDownIds(entry):
    64        id = catalog.GetEntryBitId(down)
    65        if not limitInclusion or id in bitIds:
    66          tmp.append(id)
    67          order = catalog.GetEntryOrder(down)
    68          s = levels.get(order,set())
    69          s.add(id)
    70          levels[order] = s
    71      adjs[bitId] = tmp
    72    if orderLevels:
    73      # we'll play a little game and sort the indices in each level by
    74      #  the number of downlinks they have:
    75      for order in levels.keys():
    76        ids = levels[order]
    77        counts = [len(adjs[id]) for id in ids]
    78        countOrder = argsort(counts)
    79        l = [ids[x] for x in countOrder]
    80        l.reverse()
    81        levels[order] = l
    82    return adjs,levels
    83
    84 -def GetMolsMatchingBit(mols,bit,fps):
    85    res = []
    86    if isinstance(bit,BitGainsInfo):
    87      bitId = bit.id
    88    else:
    89      bitId = bit
    90    for i,mol in enumerate(mols):
    91      fp = fps[i]
    92      if fp[bitId]:
    93        res.append(mol)
    94    return res
    95