#!/usr/local/bin/python3.3 import cgi import cgitb import pymysql import datetime from functions import get_cookies, phead, ptail, BEDintersect, BEDcopy cgitb.enable() #this is a test cookie = get_cookies() if cookie: print(cookie) fname = cookie["fname"].value lname = cookie["lname"].value name = " ".join((fname, lname)) admin = cookie["admin"].value uid = cookie["uid"].value uname = cookie["uname"].value form = cgi.FieldStorage() eid = form.getvalue('eid') qexname = "select title from Experiment where eid = '%s'" % eid data = [] #data should be in the order: user, passwd, host, db with open('./dbinfo.txt') as f: for line in f: line = line.strip('\n') data.append(line) info = [] for item in data: item = item.split(':') info.append(item) user=info[0][1] passwd=info[1][1] host=info[2][1] db=info[3][1] def runQuery(query, user, passwd): connection = pymysql.connect(host=host,db=db,user=user,passwd=passwd) cursor = connection.cursor() cursor.execute(query) res = cursor.fetchall() cursor.close() connection.close() return res def Copy(bid,name, location, des1, des2, des3, command, uname, cmid, wmid,stringency, source, eid): connection = pymysql.connect(host=host, user=user, db=db, passwd=passwd) cursor = connection.cursor() query1 = """INSERT INTO BEDfile (bid,name, location, description1, description2, description3, upload_date, bedtoolcommand, uploader, cmid, wmid,stringency, source) VALUES (%s, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", %s, %s,%s,%s) """%(int(bid),name, location, des1, des2, des3, str(datetime.datetime.now())[:-7], command, uname, int(cmid), int(wmid),stringency, source) query2 = """INSERT INTO ExperimentBEDfile (eid,bid) VALUES(%s,%s)"""%(bid,eid) cursor.execute(query1) cursor.execute(query2) connection.commit() cursor.close() connection.close() print("Content-type: text/html\n") phead(name, admin) print("""
""") #BEDintersect() print("""

Bedtools Intersect:

Note: You must be in a experiment to use this function!

File 1: File 2:


""" % eid) if admin == "1" and eid == None: print('

All BED files:

') q1 = """ select bid, location, b.name, w.name, c.name, description1, description2, description3, uploader, upload_date, bedtoolcommand from BEDfile b join WetlabMethod w using(wmid) join ComputationalMethod c using(cmid) order by upload_date desc, b.name asc; """ elif admin == "1" and eid: exname = runQuery(qexname, user, passwd)[0][0] print('

All BED files in experiment %s:

' % exname) q1 = """ select b.bid, location, b.name, w.name, c.name, description1, description2, description3, uploader, upload_date, bedtoolcommand from BEDfile b join ExperimentBEDfile using(bid) join WetlabMethod w using(wmid) join ComputationalMethod c using(cmid) where eid = "%s" order by upload_date desc, b.name asc; """ % eid elif admin == "0" and eid == None: print('

All BED files:

') q1 = """ select bid, location, b.name, w.name, c.name, description1, description2, description3, uploader, upload_date, bedtoolcommand from BEDfile b join WetlabMethod w using(wmid) join ComputationalMethod c using(cmid) where uploader = "%s" order by upload_date desc, b.name asc; """ % uname else: exname = runQuery(qexname, user, passwd)[0][0] print('

All BED files in experiment %s:

' % exname) q1 = """ select b.bid, location, b.name, w.name, c.name, description1, description2, description3, uploader, upload_date, bedtoolcommand from BEDfile b join ExperimentBEDfile using(bid) join WetlabMethod w using(wmid) join ComputationalMethod c using(cmid) where eid = "%s" and uploader = "%s" order by upload_date desc, b.name asc; """ % (eid,uname) res1 = runQuery(q1, user, passwd) if len(res1) > 0: print("""
""" ) n1 = len(res1) for i in range(n1): row = res1[i] print("""""") for j in range(len(row)): if j == 0 or j == 2: print("""""" % (str(row[0]),str(row[j]))) elif j == 1: pass else: print("" % str(row[j])) print("""""" % row[1]) print("") print("
ID BED file Wetlab method Computational method Info1 Info2 Info3 Uploader Upload time bedtools command Download
%s%sDownload


") nuname = form.getvalue("uname") nbid = form.getvalue("bid") #BEDcopy() print("""

Copy the BEDfile:






""") if nbid != None: q2 = """select wmid,cmid,name,location,description1,description2,description3,bedtoolcommand, stringency, source from BEDfile where bid = "%s";"""%bid q3 = """select bid from BEDfile order by bid DESC limit 1;""" q4 = """select eid from Experiment where title = "%s";"""%exp add_new = runQuery(q2,user,passwd) nbid = runQuery(q3,user,passwd) neid = runQuery(q4,user,passwd) name = add_new[0][2] + "_" + str(nbid) Copy(nbid,name, add_new[0][3], add_new[0][4], add_new[0][5], add_new[0][6], add_new[0][7], nuname, add_new[0][1], add_new[0][0],add_new[0][8], add_new[0][9],neid) ptail() else: print("Location: login.py") print("Content-type: text/html\n")