forked from tskit-dev/msprime-1.0-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty_pictures.py
99 lines (79 loc) · 2.55 KB
/
pretty_pictures.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
import click
import tskit
import msprime
@click.command()
def mutated_tree():
"""
Make a figure with (a) a tree and (b) some mutations added to it.
"""
ts = msprime.sim_ancestry(
50,
population_size=1e4,
recombination_rate=0.5e-8,
sequence_length=1000,
random_seed=14,
)
model = msprime.F84(kappa=2)
mts = msprime.sim_mutations(ts, rate=5e-8, model=model, random_seed=5)
def do_svg(ts, **kwargs):
# Sizes are probably all wrong here
style = """\
@media print {
@page { margin: 0; size: 3in 2in}
body { margin: 1.6cm; }
}
"""
ts.draw_svg(
size=(300, 200),
node_labels={},
mutation_labels={m.id: m.derived_state for m in ts.mutations()},
symbol_size=5,
force_root_branch=True,
style=style,
**kwargs
)
do_svg(ts, path="illustrations/unmutated_tree.svg")
do_svg(mts, path="illustrations/mutated_tree.svg")
@click.command()
def arg_ts():
tables = tskit.TableCollection(1.0)
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
tables.nodes.add_row(flags=msprime.NODE_IS_RE_EVENT, time=0.5)
# tables.nodes.add_row(flags=msprime.NODE_IS_RE_EVENT, time=0.5)
tables.nodes.add_row(flags=0, time=1.0)
tables.nodes.add_row(flags=0, time=1.5)
tables.nodes.add_row(flags=0, time=2.0)
tables.edges.add_row(0, 1, 5, 0)
tables.edges.add_row(0, 1, 3, 1)
tables.edges.add_row(0.0, 0.3, 5, 3)
tables.edges.add_row(0.3, 1, 4, 3)
tables.edges.add_row(0, 1, 4, 2)
tables.edges.add_row(0, 1, 6, 5)
tables.edges.add_row(0, 1, 6, 4)
tables.sort()
print(tables)
ts = tables.tree_sequence()
# https://stackoverflow.com/questions/46077392/additional-options-in-chrome-headless-print-to-pdf
# Sizes are probably all wrong here
style = """\
@media print {
@page { margin: 0; size: 4in 3in}
body { margin: 1.6cm; }
}
"""
svgfile = "illustrations/arg-ts.svg"
ts.draw_svg(svgfile, size=(400, 300), style=style)
print(ts.draw_text())
ts = ts.simplify()
svgfile = "illustrations/arg-ts-simplified.svg"
ts.draw_svg(svgfile, size=(400, 300), style=style)
print(ts.draw_text())
@click.group()
def cli():
pass
cli.add_command(mutated_tree)
cli.add_command(arg_ts)
if __name__ == "__main__":
cli()