annotate tools/taxonomy/poisson2test.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/local/bin/python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 from math import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 if ((len(sys.argv)-1) != 6):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 print 'too few parameters'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 print 'usage: inputfile, col1, col2, d-value(not 0), p-val correction method(0 or 1)'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 lines_arr = open(sys.argv[1]).readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 except IOError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 print'cannot open',sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 i = int(sys.argv[2]) #first column to compare
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 j = int(sys.argv[3]) #second colum to compare
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 d = float(sys.argv[4]) #correction factor
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 k = int(sys.argv[5]) #p-val correction method
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 outfile = open(sys.argv[6],'w') # output data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if (i>j):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 print 'column order not correct col1 < col2'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 print 'usage: inputfile, col1, col2, d-value, p-val correction method'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 a = 1 / d
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 assert k in [0,1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 except ZeroDivisionError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 print 'd cannot be 0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 print 'usage: inputfile, col1, col2, d-value, p-val correction method'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 print ' p-val correction should be 0 or 1 (0 = "bonferroni", 1 = "fdr")'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 print 'usage: inputfile, col1, col2, d-value, p-val correction method'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 print 'parameters are not integers'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 print 'usage: inputfile, col1, col2, d-value, p-val correction method'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 fsize = len(lines_arr)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 z1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 z2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 pz1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 pz2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 field = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if d<1: # Z score calculation
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 for line in lines_arr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 field = line.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 x = int(field[j-1]) #input column 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 y = int(field[i-1]) #input column 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if y>x:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 z1.append(float((y - ((1/d)*x))/sqrt((1/d)*(x + y))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 z2.append(float((2*(sqrt(y+(3/8))-sqrt((1/d)*(x+(3/8)))))/sqrt(1+(1/d))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 tmp_var1 = x
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 x = y
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 y = tmp_var1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 z1.append(float((y - (d*x))/sqrt(d*(x + y))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 z2.append(float((2*(sqrt(y+(3/8))-sqrt(d*(x+(3/8)))))/sqrt(1+d)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 else: #d>1 Z score calculation
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 for line in lines_arr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 field = line.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 x = int(field[i-1]) #input column 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 y = int(field[j-1]) #input column 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 if y>x:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 z1.append(float((y - (d*x))/sqrt(d*(x + y))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 z2.append(float((2*(sqrt(y+(3/8))-sqrt(d*(x+(3/8)))))/sqrt(1+d)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 tmp_var2 = x
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 x = y
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 y = tmp_var2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 z1.append(float((y - ((1/d)*x))/sqrt((1/d)*(x + y))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 z2.append(float((2*(sqrt(y+(3/8))-sqrt((1/d)*(x+(3/8)))))/sqrt(1+(1/d))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 # P-value caluculation for z1 and z2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 for p in z1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 pz1.append(float(r.pnorm(-abs(float(p)))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 for q in z2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 pz2.append(float(r.pnorm(-abs(float(q)))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 # P-value correction for pz1 and pz2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 if k == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 corrz1 = r.p_adjust(pz1,"bonferroni",fsize)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 corrz2 = r.p_adjust(pz2,"bonferroni",fsize)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 corrz1 = r.p_adjust(pz1,"fdr",fsize)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 corrz2 = r.p_adjust(pz2,"fdr",fsize)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 #printing all columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 for n in range(fsize):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 print >> outfile, "%s\t%4.3f\t%4.3f\t%8.6f\t%8.6f\t%8.6f\t%8.6f" %(lines_arr[n].strip(),z1[n],z2[n],pz1[n],pz2[n],corrz1[n],corrz2[n])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124