Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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