# postgresql_util.py
import sys
import psycopg2

def quote(s):
    s = s.replace("'","''") # escape any single quote
    s = "'"+s+"'"
    return s

def getMax(csr,col,table):
    csr.execute("SELECT MAX("+col+") FROM "+table)
    _ = csr.fetchall(); assert len(_)==1
    maxv = _[0][0]
    return maxv

def doesExist(csr,table,col,val):
    csr.execute("SELECT * FROM "+table+" WHERE "+col+"="+"'"+val+"'")
    _ = csr.fetchall();
    return len(_)!=0

def main(argv):
    assert len(argv)==6

    db = argv[1]
    user = argv[2]; passwd = argv[3]
    host = argv[4]; port = argv[5]

    conn = psycopg2.connect(database=db, user=user, password=passwd,
                            host=host, port=port)
    csr = conn.cursor()

    ###
    # print getMax(csr,'com_id','compound')
    # print getMax(csr,'pla_id','plant')

    ###
    # print doesExist(csr,'compound','com_drugbank_id','DB08427')

    ###
    q = "select com_similarity_simcomp from compound where com_kegg_id='C07619';"
    q = "select pro_similarity_smithwaterman from protein where pro_uniprot_id='Q53R12'"
    csr.execute(q)
    resp = csr.fetchall()
    print len(resp)
    print resp[0]

if __name__ == '__main__':
    main(sys.argv)

@@ -42,16 +42,1269 @@
 ycopg2%0A%0A
+sys.path.append('../config')%0Afrom database_config import databaseConfig as dcfg%0A%0AdbConn = psycopg2.connect(database=dcfg%5B'name'%5D,user=dcfg%5B'user'%5D,password=dcfg%5B'passwd'%5D,%0A                               host=dcfg%5B'host'%5D,port=dcfg%5B'port'%5D)%0AdbCsr = dbConn.cursor()%0A%0Adef drawKernel(idList):%0A    prefix = idList%5B0%5D%5B0:3%5D%0A    tbl = None; col = None;%0A    if prefix=='COM':%0A        tbl = 'compound'%0A        col = 'com_similarity_simcomp'%0A    elif prefix=='PRO':%0A        tbl = 'protein'%0A        col = 'pro_similarity_smithwaterman'%0A    else:%0A        assert False, 'Unknown type'%0A%0A    cond = ''%0A    for i,ii in enumerate(idList):%0A        if i%3E0:%0A            cond += %22 OR %22%0A        cond += prefix.lower()+%22_id=%22+quote(ii)%0A%0A    q  = %22SELECT %22+col+%22 FROM %22+tbl%0A    q += %22 WHERE %22+cond%0A    dbCsr.execute(q)%0A    rows = dbCsr.fetchall()%0A%0A    kernelDict = dict()%0A    for i,row in enumerate(rows):%0A        id1 = idList%5Bi%5D%0A%0A        row = row%5B0%5D # row initially is a 1-element tuple: (x,)%0A        if row!=None:%0A            comps = row.split(',')%0A            for comp in comps:%0A                subcomps = comp.split('=')%0A                id2 = subcomps%5B0%5D.split(':')%5B0%5D%0A                sim = subcomps%5B1%5D%0A                kernelDict%5B (id1,id2) %5D = sim%0A%0A    return kernelDict%0A%0A
 def quot

add drawKernel()