forked from tskit-dev/tsinfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.py
334 lines (276 loc) · 9.53 KB
/
dev.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import random
import os
import sys
import tqdm
import pprint
import numpy as np
import json
import tsinfer
import msprime
def make_errors(v, p):
"""
For each sample an error occurs with probability p. Errors are generated by
sampling values from the stationary distribution, that is, if we have an
allele frequency of f, a 1 is emitted with probability f and a
0 with probability 1 - f. Thus, there is a possibility that an 'error'
will in fact result in the same value.
"""
w = np.copy(v)
if p > 0:
m = v.shape[0]
frequency = np.sum(v) / m
# Randomly choose samples with probability p
samples = np.where(np.random.random(m) < p)[0]
# Generate observations from the stationary distribution.
errors = (np.random.random(samples.shape[0]) < frequency).astype(int)
w[samples] = errors
return w
def generate_samples(ts, error_p):
"""
Returns samples with a bits flipped with a specified probability.
Rejects any variants that result in a fixed column.
"""
S = np.zeros((ts.sample_size, ts.num_mutations), dtype=np.int8)
for variant in ts.variants():
done = False
# Reject any columns that have no 1s or no zeros
while not done:
S[:, variant.index] = make_errors(variant.genotypes, error_p)
s = np.sum(S[:, variant.index])
done = 0 < s < ts.sample_size
return S.T
def tsinfer_dev(
n,
L,
seed,
num_threads=1,
recombination_rate=1e-8,
error_rate=0,
engine="C",
log_level="WARNING",
precision=None,
debug=True,
progress=False,
path_compression=True,
):
np.random.seed(seed)
random.seed(seed)
L_megabases = int(L * 10 ** 6)
# daiquiri.setup(level=log_level)
ts = msprime.simulate(
n,
Ne=10 ** 4,
length=L_megabases,
recombination_rate=recombination_rate,
mutation_rate=1e-8,
random_seed=seed,
)
if debug:
print("num_sites = ", ts.num_sites)
assert ts.num_sites > 0
# ts = msprime.mutate(ts, rate=1e-8, random_seed=seed,
# model=msprime.InfiniteSites(msprime.NUCLEOTIDES))
samples = tsinfer.SampleData.from_tree_sequence(ts)
rho = recombination_rate
mu = 1e-3 # 1e-15
# num_alleles = samples.num_alleles(inference_sites=True)
# num_sites = samples.num_inference_sites
# with tsinfer.AncestorData(samples) as ancestor_data:
# t = np.sum(num_alleles) + 1
# for j in range(num_sites):
# for allele in range(num_alleles[j]):
# ancestor_data.add_ancestor(j, j + 1, t, [j], [allele])
# t -= 1
ancestor_data = tsinfer.generate_ancestors(
samples, engine=engine, num_threads=num_threads
)
ancestors_ts = tsinfer.match_ancestors(
samples,
ancestor_data,
engine=engine,
path_compression=True,
extended_checks=False,
precision=precision,
recombination_rate=rho,
mutation_rate=mu,
)
# print(ancestors_ts.tables)
# print("ancestors ts")
# for tree in ancestors_ts.trees():
# print(tree.draw_text())
# for site in tree.sites():
# if len(site.mutations) > 1:
# print(site.id)
# for mutation in site.mutations:
# print("\t", mutation.node, mutation.derived_state)
# for var in ancestors_ts.variants():
# print(var.genotypes)
# print(ancestors_ts.tables)
# ancestors_ts = tsinfer.augment_ancestors(samples, ancestors_ts,
# [5, 6, 7], engine=engine)
ts = tsinfer.match_samples(
samples,
ancestors_ts,
recombination_rate=rho,
mutation_rate=mu,
path_compression=False,
engine=engine,
precision=precision,
simplify=False,
)
print("num_edges = ", ts.num_edges)
# # print(ts.draw_text())
# for tree in ts.trees():
# print(tree.draw_text())
# for site in tree.sites():
# if len(site.mutations) > 1:
# print(site.id)
# for mutation in site.mutations:
# print("\t", mutation.node, mutation.derived_state)
# # print(ts.tables.edges)
# print(ts.dump_tables())
# simplified = ts.simplify()
# print("edges before = ", simplified.num_edges)
# new_ancestors_ts = insert_srb_ancestors(ts)
# ts = tsinfer.match_samples(samples, new_ancestors_ts,
# path_compression=False, engine=engine,
# simplify=True)
# for tree in ts.trees():
# print(tree.interval)
# print(tree.draw(format="unicode"))
# print(ts.tables.edges)
# for tree in ts.trees():
# print(tree.draw(format="unicode"))
tsinfer.verify(samples, ts)
# for node in ts.nodes():
# if tsinfer.is_synthetic(node.flags):
# print("Synthetic node", node.id, node.time)
# parent_edges = [edge for edge in ts.edges() if edge.parent == node.id]
# child_edges = [edge for edge in ts.edges() if edge.child == node.id]
# child_edges.sort(key=lambda e: e.left)
# print("parent edges")
# for edge in parent_edges:
# print("\t", edge)
# print("child edges")
# for edge in child_edges:
# print("\t", edge)
# # output_ts = tsinfer.match_samples(subset_samples, ancestors_ts, engine=engine)
# output_ts = tsinfer.match_samples(sample_data, ancestors_ts, engine=engine)
# # dump_provenance(output_ts)
def dump_provenance(ts):
print("dump provenance")
for p in ts.provenances():
print("-" * 50)
print(p.timestamp)
pprint.pprint(json.loads(p.record))
def build_profile_inputs(n, num_megabases):
L = num_megabases * 10 ** 6
input_file = "tmp__NOBACKUP__/profile-n={}-m={}.input.trees".format(
n, num_megabases
)
if os.path.exists(input_file):
ts = msprime.load(input_file)
else:
ts = msprime.simulate(
n,
length=L,
Ne=10 ** 4,
recombination_rate=1e-8,
mutation_rate=1e-8,
random_seed=10,
)
print(
"Ran simulation: n = ",
n,
" num_sites = ",
ts.num_sites,
"num_trees =",
ts.num_trees,
)
ts.dump(input_file)
filename = "tmp__NOBACKUP__/profile-n={}-m={}.samples".format(n, num_megabases)
if os.path.exists(filename):
os.unlink(filename)
# daiquiri.setup(level="DEBUG")
with tsinfer.SampleData(
sequence_length=ts.sequence_length, path=filename, num_flush_threads=4
) as sample_data:
# progress_monitor = tqdm.tqdm(total=ts.num_samples)
# for j in range(ts.num_samples):
# sample_data.add_sample(metadata={"name": "sample_{}".format(j)})
# progress_monitor.update()
# progress_monitor.close()
progress_monitor = tqdm.tqdm(total=ts.num_sites)
for variant in ts.variants():
sample_data.add_site(variant.site.position, variant.genotypes)
progress_monitor.update()
progress_monitor.close()
print(sample_data)
# filename = "tmp__NOBACKUP__/profile-n={}_m={}.ancestors".format(n, num_megabases)
# if os.path.exists(filename):
# os.unlink(filename)
# ancestor_data = tsinfer.AncestorData.initialise(sample_data, filename=filename)
# tsinfer.build_ancestors(sample_data, ancestor_data, progress=True)
# ancestor_data.finalise()
def copy_1kg():
source = "tmp__NOBACKUP__/1kg_chr22.samples"
sample_data = tsinfer.SampleData.load(source)
copy = sample_data.copy("tmp__NOBACKUP__/1kg_chr22_copy.samples")
copy.finalise()
print(sample_data)
print("copy = ")
print(copy)
def tutorial_samples():
import tqdm
import msprime
import tsinfer
ts = msprime.simulate(
sample_size=10000,
Ne=10 ** 4,
recombination_rate=1e-8,
mutation_rate=1e-8,
length=10 * 10 ** 6,
random_seed=42,
)
ts.dump("tmp__NOBACKUP__/simulation-source.trees")
print("simulation done:", ts.num_trees, "trees and", ts.num_sites, "sites")
progress = tqdm.tqdm(total=ts.num_sites)
with tsinfer.SampleData(
path="tmp__NOBACKUP__/simulation.samples",
sequence_length=ts.sequence_length,
num_flush_threads=2,
) as sample_data:
for var in ts.variants():
sample_data.add_site(var.site.position, var.genotypes, var.alleles)
progress.update()
progress.close()
def run_build():
sample_data = tsinfer.load(sys.argv[1])
ad = tsinfer.generate_ancestors(sample_data)
print(ad)
if __name__ == "__main__":
# run_build()
# np.set_printoptions(linewidth=20000)
# np.set_printoptions(threshold=20000000)
# tutorial_samples()
# build_profile_inputs(10, 10)
# build_profile_inputs(100, 10)
# build_profile_inputs(1000, 100)
# build_profile_inputs(10**4, 100)
# build_profile_inputs(10**5, 100)
# for j in range(1, 100):
# tsinfer_dev(15, 0.5, seed=j, num_threads=0, engine="P", recombination_rate=1e-8)
# copy_1kg()
tsinfer_dev(
118,
0.05,
seed=4,
num_threads=0,
engine="C",
recombination_rate=1e-8,
precision=0,
)
# minimise_dev()
# for seed in range(1, 10000):
# print(seed)
# # tsinfer_dev(40, 2.5, seed=seed, num_threads=1, genotype_quality=1e-3, engine="C")