Chem.Suppliers.DbMolSupplier

Package Chem :: Package Suppliers :: Module DbMolSupplier |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.Suppliers.DbMolSupplier

      1
      2  # $Id: DbMolSupplier.py 2 2006-05-06 22:54:39Z glandrum $
      3  #
      4  # Copyright (C) 2003-2006 greg Landrum and Rational Discovery LLC
      5  #
      6  #   @@ All Rights Reserved  @@
      7  #
      8  """ Supplies a class for working with molecules from databases
      9  #DOC
     10
     11  """
     12  import Chem
     13  from Chem.Suppliers.MolSupplier import MolSupplier
     14  import sys
     15 -def warning(msg,dest=sys.stderr):
     16    dest.write(msg)
     17
     18 -class DbMolSupplier(MolSupplier):
     19    """
     20      new molecules come back with all additional fields from the
     21      database set in a "_fieldsFromDb" data member
     22
     23    """
     24 -  def __init__(self,dbResults,
     25                 molColumnFormats={'SMILES':'SMI',
     26                                   'SMI':'SMI',
     27                                   'MOLPKL':'PKL'},
     28                 nameCol='',**kwargs):
     29      """
     30
     31        DbResults should be a subclass of Dbase.DbResultSet.DbResultBase
     32
     33      """
     34      self._data = dbResults
     35      self._colNames = [x.upper() for x in self._data.GetColumnNames()]
     36      nameCol = nameCol.upper()
     37      self.molCol = -1
     38      try:
     39        self.nameCol = self._colNames.index(nameCol)
     40      except ValueError:
     41        self.nameCol = -1
     42      for name in molColumnFormats.keys():
     43        name = name.upper()
     44        try:
     45          idx = self._colNames.index(name)
     46        except ValueError:
     47          pass
     48        else:
     49          self.molCol = idx
     50          self.molFmt = molColumnFormats[name]
     51          break
     52      if self.molCol < 0:
     53        raise ValueError,'DbResultSet has no recognizable molecule column'
     54      del self._colNames[self.molCol]
     55      self._colNames  = tuple(self._colNames)
     56      self._numProcessed=0
     57 -  def GetColumnNames(self):
     58      return self._colNames
     59
     60 -  def _BuildMol(self,data):
     61      data = list(data)
     62      molD = data[self.molCol]
     63      del data[self.molCol]
     64      self._numProcessed+=1;
     65      try:
     66        if self.molFmt =='SMI':
     67          newM = Chem.MolFromSmiles(molD)
     68          if not newM:
     69            warning('Problems processing mol %d, smiles: %s\n'%(self._numProcessed,molD))
     70        elif self.molFmt =='PKL':
     71          newM = Chem.Mol(str(molD))
     72      except:
     73        newM = None
     74      else:
     75        if newM:
     76          newM._fieldsFromDb = data
     77          nFields = len(data)
     78          for i in range(nFields):
     79            newM.SetProp(self._colNames[i],str(data[i]))
     80          if self.nameCol >=0 :
     81            newM.SetProp('_Name',str(data[self.nameCol]))
     82      return newM
     83
     84 -class ForwardDbMolSupplier(DbMolSupplier):
     85    """ DbMol supplier supporting only forward iteration
     86
     87
     88      new molecules come back with all additional fields from the
     89      database set in a "_fieldsFromDb" data member
     90
     91    """
     92 -  def __init__(self,dbResults,**kwargs):
     93      """
     94
     95        DbResults should be an iterator for Dbase.DbResultSet.DbResultBase
     96
     97      """
     98      DbMolSupplier.__init__(self,dbResults,**kwargs)
     99      self.Reset()
    100
    101 -  def Reset(self):
    102      self._dataIter = iter(self._data)
    103
    104 -  def NextMol(self):
    105      """
    106
    107        NOTE: this has side effects
    108
    109      """
    110      try:
    111        d = self._dataIter.next()
    112      except StopIteration:
    113        d = None
    114      if d is not None:
    115        newM = self._BuildMol(d)
    116      else:
    117        newM = None
    118
    119      return newM
    120
    121 -class RandomAccessDbMolSupplier(DbMolSupplier):
    122 -  def __init__(self,dbResults,**kwargs):
    123      """
    124
    125        DbResults should be a Dbase.DbResultSet.RandomAccessDbResultSet
    126
    127      """
    128      DbMolSupplier.__init__(self,dbResults,**kwargs)
    129      self._pos = -1
    130
    131 -  def __len__(self):
    132      return len(self._data)
    133
    134 -  def __getitem__(self,idx):
    135      newD = self._data[idx]
    136      return self._BuildMol(newD)
    137
    138 -  def Reset(self):
    139      self._pos = -1
    140 -  def NextMol(self):
    141      self._pos += 1
    142      res = None
    143      if self._pos < len(self):
    144        res = self[self._pos]
    145      return res
    146