-
Notifications
You must be signed in to change notification settings - Fork 13
/
FDR.py
58 lines (51 loc) · 1.32 KB
/
FDR.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
#This script calculate FDR for the P values
import sys,re,os,numpy
def myorder(p,reverse):
p_dict={};
#print('len(p)');print(len(p));
for index in range(len(p)):
if not(p[index] in p_dict):
p_dict[p[index]]=[];
p_dict[p[index]].append(index+1);
#print('p_dict');print(p_dict);
o_index=sorted(p_dict,reverse=reverse);o=[];
#print('o_index');print(o_index);
for index in o_index:
for this_p in p_dict[index]:
o.append(this_p);
return(o);
def mycummin(p):
res=[];
for i in range(len(p)):
res.append(min(p[:(i+1)]));
return(res);
def myFDR(p):
lp=len(p);
i=range(lp,0,-1);
o=myorder(p,True);
ro=myorder(o,False);
p_new=[];
for index in range(len(o)):
p_new.append(p[o[index]-1]*lp/i[index]);
p_mycummin=mycummin(p_new);
p_mycummin_new=[];
for index in range(len(p_mycummin)):
p_mycummin_new.append(min(p_mycummin[index],1));
res=[];
for index in range(len(p_mycummin_new)):
res.append(p_mycummin_new[ro[index]-1]);
return(res);
#input P values
ifile=open(sys.argv[1]);title=ifile.readline();
ilines=ifile.readlines();
P=[];
for i in ilines:
element=re.findall('[^\t\n]+',i);
P.append(float(element[-1]));
#Convert FDR
FDR=myFDR(P)
#output FDR
ofile=open(sys.argv[2],'w');ofile.write(title[:-1]+'\tFDR\n');
for i in range(len(ilines)):
ofile.write(ilines[i][:-1]+'\t'+str(FDR[i])+'\n');
ofile.close();