Mercurial > repos > iuc > ngsutils_bam_filter
annotate ngsutils/support/stats.py @ 2:7a68005de299 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
author | iuc |
---|---|
date | Sun, 27 Nov 2016 15:01:21 -0500 |
parents | 4e4e4093d65d |
children |
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 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
5 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
6 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
|
7 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
8 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
9 def median(vals): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
10 ''' |
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]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
12 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
13 >>> 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
|
14 2.5 |
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 vals.sort() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
17 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
18 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
|
19 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
|
20 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 |
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 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
|
27 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
28 >>> 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
|
29 (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
|
30 >>> 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
|
31 (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
|
32 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
33 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
34 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
35 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
|
36 acc += el |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
37 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
38 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
|
39 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
40 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
|
41 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
|
42 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
43 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
|
44 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
|
45 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
46 stdev = 0.0 |
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 return (mean, stdev) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
49 |
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 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
|
52 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
53 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
|
54 >>> 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
|
55 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
56 >>> 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
|
57 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
58 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
59 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
60 count = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
61 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
|
62 count += d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
63 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
64 if count == 0: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
65 return 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
66 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
67 acc = 0.0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
68 last = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
69 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
|
70 if last: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
71 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
|
72 acc += d[k] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
73 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
|
74 last = k |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
75 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
|
76 return k |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
77 |
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 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
|
80 ''' |
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 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
|
83 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
84 Ex: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
85 { 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
|
86 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
87 >>> 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
|
88 (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
|
89 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
90 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
91 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
92 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
93 count = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
94 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
|
95 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
|
96 count += d[k] |
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 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
|
99 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
100 acc = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
101 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
|
102 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
|
103 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
104 if count > 2: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
105 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
|
106 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
107 stdev = 0.0 |
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 return (mean, stdev) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
110 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
111 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
112 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
113 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
|
114 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
115 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
|
116 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
|
117 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
118 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
|
119 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
120 >>> 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
|
121 0.1300960209527205 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
122 >>> 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
|
123 0.33277427882095645 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
124 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
125 acc = 0.0 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
126 for i in xrange(1, x + 1): |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
127 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
|
128 return acc |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
129 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
130 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
131 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
132 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
|
133 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
134 This is the Poisson distribution function |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
135 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
136 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
|
137 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
138 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
|
139 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
|
140 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
141 >>> 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
|
142 0.00045399929762484856 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
143 >>> 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
|
144 0.0022699964881242427 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
145 >>> 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
|
146 0.007566654960414142 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
147 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
148 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
|
149 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
150 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
151 @memoize |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
152 def _factorial(x): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
153 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
154 >>> _factorial(1) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
155 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
156 >>> _factorial(2) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
157 2 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
158 >>> _factorial(3) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
159 6 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
160 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
161 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
|
162 |
2
7a68005de299
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents:
0
diff
changeset
|
163 |
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
164 if __name__ == '__main__': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
165 import doctest |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
166 doctest.testmod() |