-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathr1-from-nmut.py
executable file
·188 lines (155 loc) · 5.86 KB
/
r1-from-nmut.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
from sys import argv,stdin,stdout,stderr,exit
from math import ceil,log10
import kmer_mutation_formulas_thm5 as thm5
import mutation_model_simulator as mms
import hypergeometric_slicer as hgslicer
def usage(s=None):
message = """
Compute confidence interval for the mutation rate r1, given the observed number
of mutated k-mers.
usage: r1-from-nmut.py [options]
--nmut=<list> (N=) (cumulative) observed number of mutated
k-mers; <list> is a comma-separated list of
numbers
--length=<N> (l=) sequence length (number of NUCLEOTIDES in
the sequence)
(default is 1000 plus kmer size minus 1)
L=<N> (L=) sequence length (number of KMERS in
the sequence)
(default is 1000)
--k=<N> (K=) kmer size
(default is 21)
--confidence=<probability> (C=) size of confidence interval
(default is 95%)
--validate[=<N>] (V=) run simulations to validate the interval;
N is the number of simulation trials; if N is not
provided, 10,000 simulations are run
(by default, no simulation is performed)
--seed=<string> random seed for simulations
--progress=<number> periodically report how many simulations we've
performed"""
if (s == None): exit (message)
else: exit ("%s\n%s" % (s,message))
def main():
global reportProgress,debug
# parse the command line
nMutationObserved = []
ntSequenceLength = None
kmerSequenceLength = None
kmerSize = 21
confidence = 0.95
numSimulations = None
prngSeed = None
reportProgress = None
debug = []
for arg in argv[1:]:
if ("=" in arg):
argVal = arg.split("=",1)[1]
if (arg in ["--help","-help","--h","-h"]):
usage()
elif (arg.lower().startswith("--nmut=")) or (arg.upper().startswith("N=")):
nMutationObserved += list(map(int_with_unit,argVal.split(",")))
elif (arg.startswith("--length=")) or (arg.startswith("l=")):
ntSequenceLength = int_with_unit(argVal)
elif(arg.startswith("L=")):
kmerSequenceLength = int_with_unit(argVal)
elif (arg.startswith("--kmer=")) or (arg.upper().startswith("K=")):
kmerSize = int(argVal)
elif (arg.startswith("--confidence=")) or (arg.startswith("C=")):
confidence = parse_probability(argVal)
elif (arg.startswith("--validate=")) or (arg.startswith("V=")):
numSimulations = int_with_unit(argVal)
elif (arg.startswith("--seed=")):
prngSeed = argVal
elif (arg.startswith("--progress=")):
reportProgress = int_with_unit(argVal)
elif (arg == "--debug"):
debug += ["debug"]
elif (arg.startswith("--debug=")):
debug += argVal.split(",")
elif (arg.startswith("--")):
usage("unrecognized option: %s" % arg)
else:
usage("unrecognized option: %s" % arg)
if (nMutationObserved == []):
usage("you have to give me at least one nMut observation")
if (prngSeed != None) and (numSimulations == None):
print("WARNING, seed is ignored since --validate was not enabled",file=stderr)
if (ntSequenceLength != None) and (kmerSequenceLength != None):
if (kmerSequenceLength != ntSequenceLength + kmerSize-1):
usage("nucleotide and kmer sequence lengths are inconsistent\nyou only need to specify one of them")
elif (kmerSequenceLength != None):
ntSequenceLength = kmerSequenceLength + (kmerSize-1)
elif (ntSequenceLength == None):
ntSequenceLength = 1000 + (kmerSize-1)
if ("nocache" in debug):
hgslicer.useCache = False
if ("nojmonotonicity" in debug):
hgslicer.doJMonotonicityCheck = False
else:
hgslicer.doJMonotonicityCheck = True
if ("nsanity" in debug):
hgslicer.doNLowSanityCheck = True
hgslicer.doNHighSanityCheck = True
# compute the confidence interval(s)
L = ntSequenceLength - (kmerSize-1)
k = kmerSize
alpha = 1 - confidence
z = thm5.probit(1-alpha/2)
header = ["L","k","conf","nMut","r1Low","r1High"]
if (numSimulations != None):
header += ["r1Hat","validate"]
print("\t".join(header))
for (nMutIx,nMut) in enumerate(nMutationObserved):
q1 = thm5.q_for_n_mutated_high(L,k,nMut,z)
q2 = thm5.q_for_n_mutated_low (L,k,nMut,z)
r1Low = thm5.q_to_r1(k,q1)
r1High = thm5.q_to_r1(k,q2)
line = ["%d\t%d\t%.3f\t%d\t%.6f\t%.6f" % (L,k,confidence,nMut,r1Low,r1High)]
if (numSimulations != None):
numDigits = max(2,int(ceil(log10(numSimulations))))
r1Hat = hgslicer.q_to_r1(k,nMut/L)
prngSeedForNMut = ("%s_%d_%d" % (prngSeed,nMut,nMutIx)) if (prngSeed != None) else None
successRate = mms.r1_simulations(numSimulations,L,k,r1Hat,None,r1Low,r1High,
prngSeed=prngSeedForNMut,
reportProgress=reportProgress)
line += ["%.6f" % r1Hat]
line += ["%.*f" % (numDigits,successRate["no sketch"])]
print("\t".join(line))
# parse_probability--
# Parse a string as a probability
def parse_probability(s,strict=True):
scale = 1.0
if (s.endswith("%")):
scale = 0.01
s = s[:-1]
try:
p = float(s)
except:
try:
(numer,denom) = s.split("/",1)
p = float(numer)/float(denom)
except:
raise ValueError
p *= scale
if (strict) and (not 0.0 <= p <= 1.0):
raise ValueError
return p
# int_with_unit--
# Parse a string as an integer, allowing unit suffixes
def int_with_unit(s):
if (s.endswith("K")):
multiplier = 1000
s = s[:-1]
elif (s.endswith("M")):
multiplier = 1000 * 1000
s = s[:-1]
elif (s.endswith("G")):
multiplier = 1000 * 1000 * 1000
s = s[:-1]
else:
multiplier = 1
try: return int(s) * multiplier
except ValueError: return int(ceil(float(s) * multiplier))
if __name__ == "__main__": main()