-
Notifications
You must be signed in to change notification settings - Fork 0
/
binome_test.py
56 lines (44 loc) · 1.66 KB
/
binome_test.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
import random
import operator
import argparse
import math
import sys
from bisect import bisect
from scipy import stats
def read_scores(path):
with open(path, 'r') as file:
values = [float(line.strip()) for line in file]
file.close()
return(values)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('score1', action='store', help='path to file with score of first model')
parser.add_argument('score2', action='store', help='path to file with score of second model')
parser.add_argument('thr1', action='store', type=float, help='threshold for first model')
parser.add_argument('thr2', action='store', type=float, help='threshold for second model')
parser.add_argument('out', action='store', help='path to write results')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
return(parser.parse_args())
def write_results(p_val, wpath):
#Real Rand SD Zscore Log10Pval
with open(wpath, 'a') as file:
file.write('{:.4e}\n'.format(p_val))
file.close()
def main():
args = parse_args()
path1 = args.score1
path2 = args.score2
thr1 = args.thr1
thr2 = args.thr2
wpath = args.out
scores1 = read_scores(path1)
scores2 = read_scores(path2)
meet = sum(1 if s1 > thr1 and s2 > thr2 else 0 for s1, s2 in zip(scores1, scores2))
upper1 = sum(1 if s1 > thr1 else 0 for s1 in scores1)
upper2 = sum(1 if s2 > thr2 else 0 for s2 in scores2)
p_val = stats.binom_test(x=meet, n=len(scores1), p=(upper1 / len(scores1)) * (upper2 / len(scores2)), alternative='greater')
write_results(p_val, wpath)
if __name__=='__main__':
main()