#!/usr/local/bin/python3.3
import cgi
import cgitb
import pymysql
import re
import subprocess
#from subprocess import call
#from subprocess import Popen
import datetime
import json
import DAPRfunctions as dapr
cgitb.enable()
dapr.loadConfiguration()
cookie = dapr.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
db=dapr.DBConnection()
#define functions that connect to the db
def insert_bedtools_output(bid,name, location, command, uname,peak_count):
getcmid = """
select cmid from ComputationalMethod
where name ="bedtools intersect";
"""
getwmid = """
select wmid from WetlabMethod
where name ="derived computationally";
"""
(wmid,)=db.runQueryFetchOne(getwmid)
(cmid,)=db.runQueryFetchOne(getcmid)
date_str=str(datetime.datetime.now())[:-7]
query1 = """INSERT INTO BEDfile (bid,name, location,
upload_date, bedtoolcommand, uploader, cmid, wmid,peaknumber)
VALUES (%s,"%s", "%s", "%s", "%s", "%s", "%s", "%s", %s);
"""%(bid,name, location, date_str,
command, uname,cmid,wmid,peak_count)
db.runInsert(query1)
def update_exp(bid,eid,uid):
query2 = """INSERT INTO ExperimentBEDfile(eid,bid)
VALUES (%s,%s);"""%(eid,bid)
query5 = """ INSERT INTO UserBEDfile (uid, bid)
VALUES (%s, %s)"""%(uid, bid)
db.runInsert(query2)
db.runInsert(query5)
#this should return only one value
def query_bedname(bid):
query6 = """
select name from BEDfile
where bid = "%s"
""" % bid
res=db.runQueryFetchOne(query6)
return res
dire = "/var/www/dapr_test/bedfiles/"
locpre = "/dapr_test/bedfiles/"
#get values from the HTTP request
form = cgi.FieldStorage()
eid = int(form.getvalue("eid"))
bedtools_cmd = form.getvalue("tool")
cmd_template = form.getvalue("cmd")
bedtools_cmd_label = form.getvalue("cmd_label")
cb = form.getvalue("checkboxes")
# example of the values these variables have
#bedtools_cmd_label="Intersection"
#bedtools_cmd="intersect"
#cmd_template="bedtools %s -a %s.bed -b %s.bed"
j = json.loads(cb)
f1_bid = j[0]
f2_bid = j[1]
#get names with .bed extension
(f1,) = query_bedname(f1_bid)
(f2,) = query_bedname(f2_bid)
#get names without .bed extension
f1_no_bed = re.sub("\.bed$","",f1)
f2_no_bed = re.sub("\.bed$","",f2)
#get names without _bid suffix, using regular expressions ### might not use these names
f1_no_id = re.sub("_[0-9]+$","",f1)
f2_no_id = re.sub("_[0-9]+$","",f2)
if f1 and f2 and eid:
#get the next available bid from the database, and use that for the name of the new file
getbid="""SHOW TABLE STATUS where Name='BEDfile'"""
bid = db.runQueryFetchOne(getbid)[10]
new_bedname_in_db ="%s %s and %s_%s"%(bedtools_cmd_label,f1_no_bed,f2_no_bed,str(bid))
new_bedfile_name="%s%s.bed"%(dire,new_bedname_in_db)
f1_w_path=dire+f1
f2_w_path=dire+f2
getename = """
select title from Experiment
where eid = "%s"
""" % eid
# ename = runQuery(getename, user, passwd)[0][0]
command_for_desc = cmd_template%(f1,f2)
f1_w_path=re.sub(" ","#",f1_w_path)#need to deal with spaces in names
f2_w_path=re.sub(" ","#",f2_w_path)#need to deal w spaces
command_for_bedtools_sp_call=cmd_template%(f1_w_path,f2_w_path)
#example of what it's supposed to look like
#command_for_bedtools_sp_call =["bedtools", "intersect", "-a", f1_w_path,"-b",f2_w_path]
ff=open(dire+"BEDfuncions_logfile","w")
with open(new_bedfile_name, "w") as f:
#call(command_for_bedtools_sp_call.split("\s"), stdout=f)
#can't split by space because file names have spaces, so the file names would get split too
#converted the spaces in fnames to # then split by space, then converted # back to escaped spaces
subprocess.call([cp.replace("#"," ") for cp in command_for_bedtools_sp_call.split(" ")], stdout=f) #can't split by space
ff.close()
with open(new_bedfile_name) as f:
peak_count=sum(1 for line in f)
#insert new bedfile entry to db table
insert_bedtools_output(str(bid),new_bedname_in_db, new_bedfile_name, command_for_desc, uname,str(peak_count))
#insert link between bedfile and experiment and user tables
update_exp(bid, eid, uid)
print("Content-type: text/html\n")
print(""""""%eid)
else:
print("Content-type: text/html\n")
print("""
""" %eid)
else:
print("Location: login.py")
print("Content-type: text/html\n")