Mercurial > repos > iuc > ngsutils_bam_filter
annotate ngsutils/support/stats.py @ 0:4e4e4093d65d draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
author | iuc |
---|---|
date | Wed, 11 Nov 2015 13:04:07 -0500 |
parents | |
children | 7a68005de299 |
rev | line source |
---|---|
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
1 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
2 various statistical tests and methods... |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
3 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
4 import math |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
5 from ngsutils.support import memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
6 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
7 def median(vals): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
8 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
9 >>> median([1,2,3]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
10 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
11 >>> median([1,2,3,4]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
12 2.5 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
13 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
14 vals.sort() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
15 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
16 if len(vals) % 2 == 1: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
17 return vals[len(vals) / 2] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
18 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
19 a = vals[(len(vals) / 2) - 1] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
20 b = vals[(len(vals) / 2)] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
21 return float(a + b) / 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
22 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
23 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
24 def mean_stdev(l): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
25 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
26 >>> mean_stdev([1,2,2,2]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
27 (1.75, 0.5) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
28 >>> mean_stdev([2,2,2,2]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
29 (2.0, 0.0) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
30 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
31 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
32 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
33 for el in l: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
34 acc += el |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
35 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
36 mean = float(acc) / len(l) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
37 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
38 for el in l: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
39 acc += (el - mean) ** 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
40 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
41 if len(l) > 2: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
42 stdev = math.sqrt(float(acc) / (len(l) - 1)) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
43 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
44 stdev = 0.0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
45 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
46 return (mean, stdev) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
47 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
48 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
49 def counts_median(d): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
50 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
51 Calculate the median from counts stored in a dictionary |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
52 >>> counts_median({ 1: 4, 2: 1, 3: 4 }) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
53 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
54 >>> counts_median({ 1: 4, 3: 4 }) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
55 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
56 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
57 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
58 count = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
59 for k in d: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
60 count += d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
61 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
62 if count == 0: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
63 return 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
64 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
65 acc = 0.0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
66 last = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
67 for k in sorted(d): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
68 if last: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
69 return (last + k) / 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
70 acc += d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
71 if acc / count == 0.5: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
72 last = k |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
73 elif acc / count > 0.5: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
74 return k |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
75 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
76 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
77 def counts_mean_stdev(d): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
78 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
79 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
80 calc mean / stdev when data is stored as counts in a dictionary |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
81 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
82 Ex: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
83 { 1: 4, 2: 1, 3: 4 } = [1, 1, 1, 1, 2, 3, 3, 3, 3] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
84 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
85 >>> counts_mean_stdev({ 1: 4, 2: 1, 3: 4 }) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
86 (2.0, 1.0) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
87 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
88 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
89 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
90 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
91 count = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
92 for k in d: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
93 acc += k * d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
94 count += d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
95 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
96 mean = float(acc) / count |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
97 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
98 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
99 for k in d: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
100 acc += (((k - mean) ** 2) * d[k]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
101 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
102 if count > 2: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
103 stdev = math.sqrt(float(acc) / (count - 1)) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
104 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
105 stdev = 0.0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
106 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
107 return (mean, stdev) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
108 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
109 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
110 def poisson_prob(x, mean): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
111 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
112 Return the probability that you could get x counts in |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
113 a Poisson test with a mean value. |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
114 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
115 prob(x) = sum(i=1..x){poisson(i)} |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
116 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
117 >>> poisson_prob(6,10) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
118 0.1300960209527205 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
119 >>> poisson_prob(8,10) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
120 0.33277427882095645 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
121 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
122 acc = 0.0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
123 for i in xrange(1, x+1): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
124 acc += poisson_func(i, mean) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
125 return acc |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
126 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
127 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
128 def poisson_func(mu, lambd): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
129 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
130 This is the Poisson distribution function |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
131 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
132 p(mu) = (lambda^mu * e^(-lambda)) / (mu!) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
133 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
134 mu is a count |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
135 lambd is the mean |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
136 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
137 >>> poisson_func(1,10) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
138 0.00045399929762484856 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
139 >>> poisson_func(2,10) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
140 0.0022699964881242427 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
141 >>> poisson_func(3,10) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
142 0.007566654960414142 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
143 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
144 return (lambd ** mu) * (math.exp(-1 * lambd)) / _factorial(mu) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
145 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
146 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
147 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
148 def _factorial(x): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
149 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
150 >>> _factorial(1) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
151 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
152 >>> _factorial(2) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
153 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
154 >>> _factorial(3) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
155 6 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
156 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
157 return math.factorial(x) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
158 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
159 if __name__ == '__main__': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
160 import doctest |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
161 doctest.testmod() |