-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChromoSSE.Rev
214 lines (169 loc) · 8.04 KB
/
ChromoSSE.Rev
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
#
# ChromoSSE: Cladogenetic and Anagenetic Phylogenetic Models of Chromosome Number Evolution
#
# Will Freyman, UC Berkeley
# http://willfreyman.org
#
#
# This script specifies the ChromoSSE model and MCMC analysis.
#
setOption("useScaling","true")
# set an index for the MCMC moves
mi = 0
####### Chromosome Model
# We'll use exponential priors to model the rates of polyploidy and
# dysploidy events along the branches of the phylogeny
# calculate tree length
t_len <- psi.treeLength()
# calculate the inverse of the expected rate for 2 anagenetic events
rate_pr <- t_len / ana_events
# anagenetic rate of chromosome gains
gamma ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
# anagenetic rate of chromosome losses
delta ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
# anagenetic rate of polyploidization
rho ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
# anagenetic rate of demi-polyploidization
eta ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
# linear component of rate of gain based on current number of chromosomes
gamma_l ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnUniform(-3/max_chromo, 3/max_chromo),
p=0.5)
gamma_l.setValue(0.0)
# linear component of rate of losss based on current number of chromosomes
delta_l ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnUniform(-3/max_chromo, 3/max_chromo),
p=0.5)
delta_l.setValue(0.0)
# Create the rate matrix for chromosome number transitions.
R := fnChromosomes(max_chromo, gamma, delta, rho, eta, gamma_l, delta_l)
root_frequencies_prior <- rep(1, max_chromo + 1)
root_frequencies ~ dnDirichlet(root_frequencies_prior)
moves[mi++] = mvBetaSimplex(root_frequencies, alpha=0.5, weight=10)
moves[mi++] = mvElementSwapSimplex(root_frequencies, weight=20)
# Add moves for each of the rates.
moves[mi++] = mvScale(gamma, lambda=1.0, weight=2)
moves[mi++] = mvScale(delta, lambda=1.0, weight=2)
moves[mi++] = mvScale(rho, lambda=1.0, weight=2)
moves[mi++] = mvScale(eta, lambda=1.0, weight=2)
moves[mi++] = mvSlide(gamma_l, delta=0.1, weight=1)
moves[mi++] = mvSlide(delta_l, delta=0.1, weight=1)
moves[mi++] = mvSlide(gamma_l, delta=0.001, weight=1)
moves[mi++] = mvSlide(delta_l, delta=0.001, weight=1)
moves[mi++] = mvRJSwitch(gamma, weight=2)
moves[mi++] = mvRJSwitch(delta, weight=2)
moves[mi++] = mvRJSwitch(rho, weight=2)
moves[mi++] = mvRJSwitch(eta, weight=2)
moves[mi++] = mvRJSwitch(gamma_l, weight=2)
moves[mi++] = mvRJSwitch(delta_l, weight=2)
# speciation rates for each cladogenetic event type
# expected num species at time t under constant birth-death is
# E(N_t) = N_0 e^{rt}
# therefore net diversification r is
# r = ( ln N_t - ln N_0 ) / t
# we use r as the mean for the speciation prior
taxa <- psi.taxa()
speciation_mean <- ln( taxa.size() ) / psi.rootAge()
speciation_pr <- 1 / speciation_mean
clado_no_change ~ dnExponential(speciation_pr)
clado_fission ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
clado_fusion ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
clado_polyploid ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
clado_demipoly ~ dnReversibleJumpMixture(constantValue=0.0,
baseDistribution=dnExponential(rate_pr),
p=0.5)
clado_no_change.setValue(speciation_mean)
clado_fission.setValue(1/rate_pr)
clado_fusion.setValue(1/rate_pr)
clado_polyploid.setValue(1/rate_pr)
clado_demipoly.setValue(1/rate_pr)
# MCMC moves for speciation rates
moves[mi++] = mvScale(clado_no_change, lambda=5.0, weight=2)
moves[mi++] = mvScale(clado_fission, lambda=5.0, weight=2)
moves[mi++] = mvScale(clado_fusion, lambda=5.0, weight=2)
moves[mi++] = mvScale(clado_polyploid, lambda=5.0, weight=2)
moves[mi++] = mvScale(clado_demipoly, lambda=5.0, weight=2)
up_down_scale_mv = mvUpDownScale(lambda=0.5, weight=2)
up_down_scale_mv.addVariable( clado_no_change, TRUE )
up_down_scale_mv.addVariable( clado_fission, FALSE )
up_down_scale_mv.addVariable( clado_fusion, FALSE )
up_down_scale_mv.addVariable( clado_polyploid, FALSE )
up_down_scale_mv.addVariable( clado_demipoly, FALSE )
moves[mi++] = up_down_scale_mv
moves[mi++] = mvRJSwitch(clado_fission, weight=2)
moves[mi++] = mvRJSwitch(clado_fusion, weight=2)
moves[mi++] = mvRJSwitch(clado_polyploid, weight=2)
moves[mi++] = mvRJSwitch(clado_demipoly, weight=2)
# set a vector to hold the speciation rates
speciation_rates := [clado_no_change, clado_fission, clado_fusion, clado_polyploid, clado_demipoly]
total_speciation := sum(speciation_rates)
# map the speciation rates to chromosome cladogenetic events
clado_matrix := fnChromosomesCladoEventsBD(speciation_rates, max_chromo)
# a stochastic variable to represent the turnover.
turnover ~ dnUniform(0, 1.0)
turnover.setValue(0.4)
moves[mi++] = mvScale(turnover, lambda=5.0, weight=3.0)
up_down_scale_mv2 = mvUpDownScale(lambda=0.5, weight=2)
up_down_scale_mv2.addVariable( clado_no_change, FALSE )
up_down_scale_mv2.addVariable( clado_fission, FALSE )
up_down_scale_mv2.addVariable( clado_fusion, FALSE )
up_down_scale_mv2.addVariable( clado_polyploid, FALSE )
up_down_scale_mv2.addVariable( clado_demipoly, FALSE )
up_down_scale_mv2.addVariable( turnover, FALSE )
moves[mi++] = up_down_scale_mv2
# make a vector of extinction rates for each state
for (i in 1:(max_chromo + 1)) {
extinction[i] := turnover * total_speciation
}
# construct a variable for the tree and chromosomes drawn from a
# cladogenetic character state dependent birth death process
chromotree ~ dnCDBDP(rootAge = psi.rootAge(),
speciation = clado_matrix,
extinction = extinction,
Q = R,
delta = 1.0,
pi = root_frequencies,
rho = rho_bd)
# and clamp tree and the chromosome counts data
chromotree.clamp(psi)
chromotree.clampCharData(chromo_data)
### set up the monitors that will output parameter values to file and screen
monitors[1] = mnScreen(printgen=1, rho, clado_polyploid, total_speciation)
monitors[2] = mnModel(filename="output/chromosse.log", printgen=sample_freq)
monitors[3] = mnJointConditionalAncestralState(tree=psi,
cdbdp=chromotree,
type="NaturalNumbers",
printgen=sample_freq,
withTips=true,
withStartStates=true,
filename="output/chromosse-anc.log")
### finish model
mymodel = model(chromotree)
### workspace MCMC object ###
mymcmc = mcmc(mymodel, monitors, moves)
### run the MCMC ###
mymcmc.run(generations=mcmc_iterations)
### summarize ancestral states ###
anc_states = readAncestralStateTrace("output/chromosse-anc.log")
# make summary tree
psi_anc <- ancestralStateTree(psi,
anc_states,
include_start_states=true,
"output/ancestral_states_results.tree",
burnin=burnin)
q()