-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATAC_AJAX.py
133 lines (117 loc) · 4.18 KB
/
ATAC_AJAX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
import pymysql
import cgi
import json
import cgitb
# Enable error traceback
cgitb.enable()
# Establish a connection to the database
def connect_to_database():
try:
connection = pymysql.connect(
host='bioed.bu.edu',
user='jawa',
password='jawa',
database='Team_10',
port=4253)
return connection
except pymysql.Error as e:
print(e)
return None
# Query the database for pie chart and table data
def query_data(selector, gene_name=None, chromosome=None, start=None, end=None):
conditions = []
params = []
if gene_name:
conditions.append("`Entrez_ID` REGEXP %s")
params.append(gene_name)
if chromosome:
conditions.append("`Chr` = %s")
params.append(chromosome)
if start:
conditions.append("`Start` >= %s")
params.append(start)
if end:
conditions.append("`End` <= %s")
params.append(end)
if selector == "Pie":
sql = """
SELECT `Detailed_Annotation` as Type, COUNT(*) as Counts
FROM Brain_annotation
WHERE {}
GROUP BY `Detailed_Annotation`
""".format(" AND ".join(conditions))
elif selector == "Table":
sql = """
SELECT Chr, Start, End, `Peak_Score`, `Annotation`, `Detailed_Annotation`, `Distance_to_TSS`, `Nearest_PromoterID`, `Entrez_ID`, `Gene_Name`, `Gene_Type`
FROM Brain_annotation
WHERE {}
""".format(" AND ".join(conditions))
else:
return None
try:
connection = connect_to_database()
if connection:
with connection.cursor() as cursor:
cursor.execute(sql, params)
results = cursor.fetchall()
if not results:
return [("No data found",)] # Return None if no data is present
return results
except pymysql.Error as e:
print(e)
return None
finally:
if connection:
connection.close()
# Function to convert table data to text format
def convert_to_txt(headers, data):
if not data:
return ""
# Convert data to text format
txt_content = "\t".join(headers) + "\n" # Include headers
for row in data:
txt_content += "\t".join(map(str, row)) + "\n"
return txt_content
# Check for form data and process the request
def process_request():
form = cgi.FieldStorage()
print("Content-type: application/json\n")
if not form:
print(json.dumps({"error": "No form data received"}))
return
selector = form.getvalue("selector")
gene_name = form.getvalue("gene_name")
chromosome = form.getvalue("chromosome")
start = form.getvalue("start")
end = form.getvalue("end")
if selector == "Download":
# Handle download request
data = None
if gene_name and chromosome:
data = query_data("Table", gene_name, chromosome=chromosome, start=start, end=end)
elif gene_name:
data = query_data("Table", gene_name, start=start, end=end)
elif chromosome:
data = query_data("Table", chromosome=chromosome, start=start, end=end)
if data is not None:
headers = ["Chr", "Start", "End", "Peak_Score", "Annotation", "Detailed_Annotation", "Distance_to_TSS", "Nearest_PromoterID", "Entrez_ID", "Gene_Name", "Gene_Type"]
txt_content = convert_to_txt(headers, data)
print(json.dumps(txt_content))
else:
print(json.dumps({"message": "No data present"}))
else:
# Handle other requests
data = None
if gene_name and chromosome:
data = query_data(selector, gene_name, chromosome=chromosome, start=start, end=end)
elif gene_name:
data = query_data(selector, gene_name, start=start, end=end)
elif chromosome:
data = query_data(selector, chromosome=chromosome, start=start, end=end)
if data is not None:
print(json.dumps(data))
else:
print(json.dumps({"message": "No data present"}))
# Process the request
process_request()