#!/usr/local/bin/python3.3
import cgi
import cgitb
import pymysql
import datetime
import gviz_api
import DAPRfunctions as dapr
import DAPRfunctionsBED as daprBED

#dapr.loadConfiguration()

cookie = dapr.get_cookies()
db = dapr.DBConnection()

cgitb.enable()

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()
    
    bid = form.getvalue('bid')

    bed = daprBED.BEDfile(bid)
    
    
    #def get_peak_lengths(location):
    #    lengths = []
    #    with open(location) as f:
    #        for line in f:
    #            line_spl = line.split("\t")
    #            #the format of each entry is [peak_id,length]
    #            lengths.append([line_spl[3],int(line_spl[2]) - int(line_spl[1])])
    #    return lengths
    
    print("Content-type: text/plain")
    print()
        
    
    if bid != None:
        
        feature_lengths = bed.get_peak_lengths()
        
        table_desc = [("peakID"),("featureLength")]
        
        data_table = gviz_api.DataTable(table_desc)
        data_table.LoadData(feature_lengths)
        
        print(data_table.ToJSon()) 
