示例#1
0
def ScreenInDb(details, mol):
  try:
    probeFp = FingerprintMols.FingerprintMol(mol, **details.__dict__)
  except Exception:
    import traceback
    FingerprintMols.error('Error: problems fingerprinting molecule.\n')
    traceback.print_exc()
    return []
  
  if details.metric not in (DataStructs.TanimotoSimilarity, DataStructs.DiceSimilarity,
                            DataStructs.CosineSimilarity):
    return ScreenFingerprints(details, data=GetFingerprints(details), mol=mol)
  
  conn: DbConnect = _ConnectToDatabase(details)
  if details.metric == DataStructs.TanimotoSimilarity:
    func = 'rd_tanimoto'
  elif details.metric == DataStructs.DiceSimilarity:
    func = 'rd_dice'
  elif details.metric == DataStructs.CosineSimilarity:
    func = 'rd_cosine'
  pkl = probeFp.ToBitString()
  extraFields = f"{func}({DbModule.placeHolder},{details.fpColName}) as tani"
  cmd = _ConstructSQL(details, extraFields=extraFields)

  if details.doThreshold:
    # we need to do a subquery here:
    cmd = f"select * from ({cmd}) tmp where tani>{details.screenThresh}"
  cmd += " order by tani desc"
  if not details.doThreshold and details.topN > 0:
    cmd += f" limit {details.topN}"
  
  curs = conn.GetCursor()
  curs.execute(cmd, (pkl, ))
  return curs.fetchall()
示例#2
0
def GetFingerprints(details):
    """ returns an iterable sequence of fingerprints
  each fingerprint will have a _fieldsFromDb member whose first entry is
  the id.

  """
    if details.dbName and details.tableName:
        try:
            conn = DbConnect(details.dbName, details.tableName)
            if hasattr(details, 'dbUser'):
                conn.user = details.dbUser
            if hasattr(details, 'dbPassword'):
                conn.password = details.dbPassword
        except:
            import traceback
            FingerprintMols.error(
                'Error: Problems establishing connection to database: %s|%s\n'
                % (details.dbName, details.tableName))
            traceback.print_exc()
        cmd = _ConstructSQL(details, extraFields=details.fpColName)
        curs = conn.GetCursor()
        #curs.execute(cmd)
        #print 'CURSOR:',curs,curs.closed
        if _dataSeq:
            suppl = _dataSeq(curs,
                             cmd,
                             depickle=not details.noPickle,
                             klass=DataStructs.ExplicitBitVect)
            _dataSeq._conn = conn
        else:
            suppl = DbFpSupplier.ForwardDbFpSupplier(
                data, fpColName=details.fpColName)
    elif details.inFileName:
        conn = None
        try:
            inF = open(details.inFileName, 'r')
        except IOError:
            import traceback
            FingerprintMols.error('Error: Problems reading from file %s\n' %
                                  (details.inFileName))
            traceback.print_exc()

        supple = []
        done = 0
        while not done:
            try:
                id, fp = cPickle.load(inF)
            except:
                done = 1
            else:
                fp._fieldsFromDb = [id]
                suppl.append(fp)
    else:
        suppl = None

    return suppl
示例#3
0
def ScreenInDb(details, mol):
    try:
        probeFp = apply(FingerprintMols.FingerprintMol, (mol, ),
                        details.__dict__)
    except:
        import traceback
        FingerprintMols.error('Error: problems fingerprinting molecule.\n')
        traceback.print_exc()
        return []
    if details.dbName and details.tableName:
        try:
            conn = DbConnect(details.dbName, details.tableName)
            if hasattr(details, 'dbUser'):
                conn.user = details.dbUser
            if hasattr(details, 'dbPassword'):
                conn.password = details.dbPassword
        except:
            import traceback
            FingerprintMols.error(
                'Error: Problems establishing connection to database: %s|%s\n'
                % (details.dbName, details.tableName))
            traceback.print_exc()

    if details.metric not in (DataStructs.TanimotoSimilarity,
                              DataStructs.DiceSimilarity,
                              DataStructs.CosineSimilarity):
        data = GetFingerprints(details)
        res = ScreenFingerprints(details, data, mol)
    else:
        res = []
        if details.metric == DataStructs.TanimotoSimilarity:
            func = 'rd_tanimoto'
            pkl = probeFp.ToBitString()
        elif details.metric == DataStructs.DiceSimilarity:
            func = 'rd_dice'
            pkl = probeFp.ToBitString()
        elif details.metric == DataStructs.CosineSimilarity:
            func = 'rd_cosine'
            pkl = probeFp.ToBitString()
        extraFields = "%s(%s,%s) as tani" % (func, DbModule.placeHolder,
                                             details.fpColName)
        cmd = _ConstructSQL(details, extraFields=extraFields)

        if details.doThreshold:
            # we need to do a subquery here:
            cmd = "select * from (%s) tmp where tani>%f" % (
                cmd, details.screenThresh)
        cmd += " order by tani desc"
        if not details.doThreshold and details.topN > 0:
            cmd += " limit %d" % details.topN
        curs = conn.GetCursor()
        curs.execute(cmd, (pkl, ))
        res = curs.fetchall()

    return res
示例#4
0
def GetFingerprints(details):
    """ returns an iterable sequence of fingerprints
  each fingerprint will have a _fieldsFromDb member whose first entry is
  the id.

  """
    if details.dbName and details.tableName:
        try:
            conn = DbConnect(details.dbName, details.tableName)
            if hasattr(details, "dbUser"):
                conn.user = details.dbUser
            if hasattr(details, "dbPassword"):
                conn.password = details.dbPassword
        except Exception:
            import traceback

            FingerprintMols.error(
                "Error: Problems establishing connection to database: %s|%s\n" % (details.dbName, details.tableName)
            )
            traceback.print_exc()
        cmd = _ConstructSQL(details, extraFields=details.fpColName)
        curs = conn.GetCursor()
        # curs.execute(cmd)
        # print 'CURSOR:',curs,curs.closed
        if _dataSeq:
            suppl = _dataSeq(curs, cmd, depickle=not details.noPickle, klass=DataStructs.ExplicitBitVect)
            _dataSeq._conn = conn
        else:
            suppl = DbFpSupplier.ForwardDbFpSupplier(data, fpColName=details.fpColName)
    elif details.inFileName:
        conn = None
        try:
            inF = open(details.inFileName, "r")
        except IOError:
            import traceback

            FingerprintMols.error("Error: Problems reading from file %s\n" % (details.inFileName))
            traceback.print_exc()

        suppl = []
        done = 0
        while not done:
            try:
                ID, fp = cPickle.load(inF)
            except Exception:
                done = 1
            else:
                fp._fieldsFromDb = [ID]
                suppl.append(fp)
    else:
        suppl = None

    return suppl
示例#5
0
def ScreenInDb(details, mol):
    try:
        probeFp = apply(FingerprintMols.FingerprintMol, (mol,), details.__dict__)
    except Exception:
        import traceback

        FingerprintMols.error("Error: problems fingerprinting molecule.\n")
        traceback.print_exc()
        return []
    if details.dbName and details.tableName:
        try:
            conn = DbConnect(details.dbName, details.tableName)
            if hasattr(details, "dbUser"):
                conn.user = details.dbUser
            if hasattr(details, "dbPassword"):
                conn.password = details.dbPassword
        except Exception:
            import traceback

            FingerprintMols.error(
                "Error: Problems establishing connection to database: %s|%s\n" % (details.dbName, details.tableName)
            )
            traceback.print_exc()

    if details.metric not in (DataStructs.TanimotoSimilarity, DataStructs.DiceSimilarity, DataStructs.CosineSimilarity):
        data = GetFingerprints(details)
        res = ScreenFingerprints(details, data, mol)
    else:
        res = []
        if details.metric == DataStructs.TanimotoSimilarity:
            func = "rd_tanimoto"
            pkl = probeFp.ToBitString()
        elif details.metric == DataStructs.DiceSimilarity:
            func = "rd_dice"
            pkl = probeFp.ToBitString()
        elif details.metric == DataStructs.CosineSimilarity:
            func = "rd_cosine"
            pkl = probeFp.ToBitString()
        extraFields = "%s(%s,%s) as tani" % (func, DbModule.placeHolder, details.fpColName)
        cmd = _ConstructSQL(details, extraFields=extraFields)

        if details.doThreshold:
            # we need to do a subquery here:
            cmd = "select * from (%s) tmp where tani>%f" % (cmd, details.screenThresh)
        cmd += " order by tani desc"
        if not details.doThreshold and details.topN > 0:
            cmd += " limit %d" % details.topN
        curs = conn.GetCursor()
        curs.execute(cmd, (pkl,))
        res = curs.fetchall()

    return res
示例#6
0
def _ConnectToDatabase(details) -> DbConnect:
  if details.dbName and details.tableName:
    try:
      conn = DbConnect(details.dbName, details.tableName)
      if hasattr(details, 'dbUser'):
        conn.user = details.dbUser
      if hasattr(details, 'dbPassword'):
        conn.password = details.dbPassword
      return conn
    except Exception:
      import traceback
      FingerprintMols.error(f'Error: Problems establishing connection to '
                            f'database:{details.dbName}|{details.tableName}\n')
      traceback.print_exc()
  return None
示例#7
0
def ScreenFingerprints(details, data, mol=None, probeFp=None):
    """ Returns a list of results

  """
    if probeFp is None:
        try:
            probeFp = apply(FingerprintMols.FingerprintMol, (mol, ),
                            details.__dict__)
        except:
            import traceback
            FingerprintMols.error('Error: problems fingerprinting molecule.\n')
            traceback.print_exc()
            return []
    if not probeFp:
        return []

    res = []
    if not details.doThreshold and details.topN > 0:
        topN = TopNContainer(details.topN)
    else:
        topN = []
    res = []
    count = 0
    for pt in data:
        fp1 = probeFp
        if not details.noPickle:
            if type(pt) in (types.TupleType, types.ListType):
                id, fp = pt
            else:
                fp = pt
                id = pt._fieldsFromDb[0]
            score = DataStructs.FingerprintSimilarity(fp1, fp, details.metric)
        else:
            id, pkl = pt
            score = details.metric(fp1, str(pkl))
        if topN:
            topN.Insert(score, id)
        elif not details.doThreshold or \
                 (details.doThreshold and score>=details.screenThresh):
            res.append((id, score))
        count += 1
        if hasattr(details, 'stopAfter') and count >= details.stopAfter:
            break
    for score, id in topN:
        res.append((id, score))

    return res
示例#8
0
def ScreenFingerprints(details,data,mol=None,probeFp=None):
  """ Returns a list of results

  """
  if probeFp is None:
    try:
      probeFp = apply(FingerprintMols.FingerprintMol,(mol,),details.__dict__)
    except:
      import traceback
      FingerprintMols.error('Error: problems fingerprinting molecule.\n')
      traceback.print_exc()
      return []
  if not probeFp:
    return []

  res = []
  if not details.doThreshold and details.topN>0:
    topN = TopNContainer(details.topN)
  else:
    topN = []
  res = []
  count = 0
  for pt in data:
    fp1 = probeFp
    if not details.noPickle:
      if type(pt) in (types.TupleType,types.ListType):
        id,fp = pt
      else:
        fp = pt
        id = pt._fieldsFromDb[0]
      score = DataStructs.FingerprintSimilarity(fp1,fp,details.metric)
    else:
      id,pkl = pt
      score = details.metric(fp1,str(pkl))
    if topN:
      topN.Insert(score,id)
    elif not details.doThreshold or \
             (details.doThreshold and score>=details.screenThresh):
      res.append((id,score))
    count += 1
    if hasattr(details,'stopAfter') and count >= details.stopAfter:
      break
  for score,id in topN:
    res.append((id,score))

  return res
示例#9
0
def ScreenFingerprints(details, data, mol=None, probeFp=None):
  """ Returns a list of results

  """
  if probeFp is None:
    try:
      probeFp = FingerprintMols.FingerprintMol(mol, **details.__dict__)
    except Exception:
      import traceback
      FingerprintMols.error('Error: problems fingerprinting molecule.\n')
      traceback.print_exc()
      return []
  if not probeFp:
    return []

  if not details.doThreshold and details.topN > 0:
    topN = TopNContainer(details.topN)
  else:
    topN = []
  res = []
  count = 0
  for pt in data:
    fp1 = probeFp
    if not details.noPickle:
      if isinstance(pt, (tuple, list)):
        ID, fp = pt
      else:
        fp = pt
        ID = pt._fieldsFromDb[0]
      score = DataStructs.FingerprintSimilarity(fp1, fp, details.metric)
    else:
      ID, pkl = pt
      score = details.metric(fp1, str(pkl))
    if topN:
      topN.Insert(score, ID)
    elif not details.doThreshold or (details.doThreshold and score >= details.screenThresh):
      res.append((ID, score))
    count += 1
    if hasattr(details, 'stopAfter') and count >= details.stopAfter:
      break

  for score, ID in topN:
    res.append((ID, score))
  return res
示例#10
0
def ScreenFromDetails(details, mol=None):
    """ Returns a list of results

  """
    if not mol:
        if not details.probeMol:
            smi = details.probeSmiles
            try:
                mol = Chem.MolFromSmiles(smi)
            except:
                import traceback
                FingerprintMols.error(
                    'Error: problems generating molecule for smiles: %s\n' %
                    (smi))
                traceback.print_exc()
                return
        else:
            mol = details.probeMol
    if not mol:
        return

    if details.outFileName:
        try:
            outF = open(details.outFileName, 'w+')
        except IOError:
            FingerprintMols.error(
                "Error: could not open output file %s for writing\n" %
                (details.outFileName))
            return None
    else:
        outF = None

    if not hasattr(details, 'useDbSimilarity') or not details.useDbSimilarity:
        data = GetFingerprints(details)
        res = ScreenFingerprints(details, data, mol)
    else:
        res = ScreenInDb(details, mol)
    if outF:
        for pt in res:
            outF.write(','.join([str(x) for x in pt]))
            outF.write('\n')
    return res
示例#11
0
def GetFingerprints(details):
  """ returns an iterable sequence of fingerprints
  each fingerprint will have a _fieldsFromDb member whose first entry is
  the id.

  """
  if details.dbName and details.tableName:
    conn: DbConnect = _ConnectToDatabase(details)
    cmd = _ConstructSQL(details, extraFields=details.fpColName)
    curs = conn.GetCursor()
    # curs.execute(cmd)
    # print 'CURSOR:',curs,curs.closed
    
    if _dataSeq:
      suppl = _dataSeq(curs, cmd, depickle=not details.noPickle, klass=DataStructs.ExplicitBitVect)
      _dataSeq._conn = conn
      return suppl
    return DbFpSupplier.ForwardDbFpSupplier(data, fpColName=details.fpColName)
  
  if details.inFileName:
    try:
      inF = open(details.inFileName, 'r')
    except IOError:
      import traceback
      FingerprintMols.error(f'Error: Problems reading from file {details.inFileName}\n')
      traceback.print_exc()

    suppl = []
    done = 0
    while not done:
      try:
        ID, fp = pickle.load(inF)
      except Exception:
        done = 1
      else:
        fp._fieldsFromDb = [ID]
        suppl.append(fp)
    return suppl

  return None
示例#12
0
def ScreenFromDetails(details, mol=None):
  """ Returns a list of results

  """
  if not mol:
    if not details.probeMol:
      smi = details.probeSmiles
      try:
        mol = Chem.MolFromSmiles(smi)
      except Exception:
        import traceback
        FingerprintMols.error('Error: problems generating molecule for smiles: %s\n' % (smi))
        traceback.print_exc()
        return
    else:
      mol = details.probeMol
  if not mol:
    return

  if details.outFileName:
    try:
      outF = open(details.outFileName, 'w+')
    except IOError:
      FingerprintMols.error("Error: could not open output file %s for writing\n" %
                            (details.outFileName))
      return None
  else:
    outF = None

  if not hasattr(details, 'useDbSimilarity') or not details.useDbSimilarity:
    data = GetFingerprints(details)
    res = ScreenFingerprints(details, data, mol)
  else:
    res = ScreenInDb(details, mol)
  if outF:
    for pt in res:
      outF.write(','.join([str(x) for x in pt]))
      outF.write('\n')
  return res