annotate poisson2test.py @ 1:21d265a4c17d draft default tip

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