-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·81 lines (73 loc) · 3.1 KB
/
server.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
#!/usr/bin/python
from flask import Flask, render_template, request
import scipy.stats
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/results', methods=['GET', 'POST'])
def results():
foreground_organisms = set()
if 'foreground' in request.form:
for organism in request.form['foreground'].split('\n'):
organism = organism.strip()
if organism:
foreground_organisms.add(organism)
background_organisms = set()
if 'background' in request.form:
for organism in request.form['background'].split('\n'):
organism = organism.strip()
if organism:
background_organisms.add(organism)
dictionary = 'disease'
if 'dictionary' in request.form:
dictionary = request.form['dictionary']
z_cutoff = 5.0
if 'zcutoff' in request.form:
z_cutoff = float(request.form['zcutoff'])
p_cutoff = 1.0
if 'pcutoff' in request.form:
p_cutoff = float(request.form['pcutoff'])
# Read and count associations above cutoff for foreground and background.
foreground_counts = {}
background_counts = {}
term_names = {}
for line in open('/data/download/organism_%s_textmining_full.tsv' % dictionary, 'r'):
fields = line.strip().split('\t')
z_score = fields[4]
if float(z_score) >= z_cutoff:
organism = fields[0]
term = fields[2]
name = fields[3]
if organism in foreground_organisms:
if term in foreground_counts:
foreground_counts[term] += 1
else:
foreground_counts[term] = 1
if term not in term_names:
term_names[term] = name
if organism in background_organisms:
if term in background_counts:
background_counts[term] += 1
else:
background_counts[term] = 1
# Loop over counted terms, calculate statistics, and print results.
results = []
for term in foreground_counts.keys():
name = term_names[term]
foreground_count = foreground_counts[term]
if background_organisms:
background_count = 0
if term in background_counts:
background_count = background_counts[term]-foreground_count
foreground_size = len(foreground_organisms)
background_size = len(background_organisms)-foreground_size
table = [[foreground_count,foreground_size-foreground_count],[background_count,background_size-background_count]]
ratio, p_value = scipy.stats.fisher_exact(table, alternative='greater')
if p_value < p_cutoff:
results.append((term, name, foreground_count, background_count, '%.9f' % p_value))
else:
results.append((term, name, foreground_count))
return render_template('results.html', results=results, enrichment=bool(background_counts))
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=9001)