-
Notifications
You must be signed in to change notification settings - Fork 0
/
biogeog_M1.Rev
269 lines (225 loc) · 10.7 KB
/
biogeog_M1.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
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
##
## Directories ========================
out_fn <- "output/"
range_fn = "data/chupon_biogeog.nex"
geo_fn = "data/hawaii"
times_fn = geo_fn + ".times.txt"
dist_fn = geo_fn + ".distances.txt"
output_name = "M1"
## Load alignments ========================
mol_filenames <- ["data/alignments/chupon_12S.fasta",
"data/alignments/chupon_16S.fasta",
"data/alignments/chupon_18S.fasta",
"data/alignments/chupon_Actin.fasta",
"data/alignments/chupon_COI.fasta",
"data/alignments/chupon_CytB.fasta",
"data/alignments/chupon_H3.fasta",
"data/alignments/chupon_SSU.fasta"]
n_data_subsets <- mol_filenames.size()
for (i in 1:n_data_subsets) {
data[i] = readDiscreteCharacterData(mol_filenames[i])
}
mvi = 0 # define move counter
mni = 0 # define monitor counter
## Import biogeographic ranges ========================
# read binary (01) presence-absence range data
dat_range_01 = readDiscreteCharacterData(range_fn)
n_areas <- dat_range_01.nchar()
# determine the number of states
max_areas <- 4
n_states <- 0
for (k in 0:max_areas) n_states += choose(n_areas, k)
# convert binary ranges into NaturalNumbers
dat_range_n = formatDiscreteCharacterData(dat_range_01, "DEC", n_states)
# epoch times
time_bounds <- readDataDelimitedFile(file=times_fn, delimiter=" ")
n_epochs <- time_bounds.size()
# epoch connectivity
for (i in 1:n_epochs) {
epoch_fn = geo_fn + ".connectivity." + i + ".txt"
connectivity[i] <- readDataDelimitedFile(file=epoch_fn, delimiter=" ")
}
# area distances
distances <- readDataDelimitedFile(file=dist_fn, delimiter=" ")
distance_sum <- 0.0
for (i in 1:n_areas) {
for (j in i:n_areas) {
if (i != j) {
distance_sum += distances[i][j]
}
}
}
num_distances = (n_areas * n_areas - n_areas) / 2
distance_mean <- distance_sum / num_distances
# Useful
taxa = data[1].taxa()
n_taxa = taxa.size()
n_branches = 2 * n_taxa - 2
# get the converted state descriptions
state_desc = dat_range_n.getStateDescriptions()
# write the state descriptions to file
state_desc_str = "state,range\n"
for (i in 1:state_desc.size()){
state_desc_str += (i-1) + "," + state_desc[i] + "\n"
}
write(state_desc_str, file=out_fn+"state_labels.txt")
# DEFINE SUBSTITUTION AND TREE MODEL ========================
print("Defining substitution and tree model")
# Define priors
for (i in 1:n_data_subsets){
# Substitution model parameters
ex[i] ~ dnDirichlet( v(1,1,1,1,1,1) )
pi[i] ~ dnDirichlet( v(1,1,1,1) )
moves[++mvi] = mvSimplexElementScale(ex[i], alpha = 10, tune = true, weight = 1)
moves[++mvi] = mvSimplexElementScale(pi[i], alpha = 10, tune = true, weight = 1)
# Define rate matrix
Q[i] := fnGTR(ex[i], pi[i])
# Gamma distributed rate variation
alpha[i] ~ dnUniform( 0, 1E8 )
gamma_rates[i] := fnDiscretizeGamma( alpha[i], alpha[i], 4, false ) # mean of 1, but shape can vary
moves[++mvi] = mvScale(alpha[i], weight = 2.0, tune = true)
alpha[i].setValue(0.5)
# Invariable sites
# pinvar[i] ~ dnBeta(1,1)
# moves[++mvi] = mvScale(pinvar[i], lambda=2.0, tune=true, weight=1.0)
# moves[++mvi] = mvSlide(pinvar[i], delta=1.0, tune=true, weight=1.0)
}
# specify a rate multiplier for each partition
part_rate_mult ~ dnDirichlet( rep(1.0, n_data_subsets) )
moves[++mvi] = mvBetaSimplex(part_rate_mult, alpha=1.0, tune=true, weight=n_data_subsets)
moves[++mvi] = mvDirichletSimplex(part_rate_mult, alpha=1.0, tune=true, weight=2.0)
# note that we use here a vector multiplication,
# i.e., multiplying each element of part_rate_mult by n_data_subsets
part_rate := part_rate_mult * n_data_subsets
# Clock model
branch_rate_mean_log ~ dnUniform(-10, 1)
branch_rate_mean_log.setValue(-1)
moves[++mvi] = mvSlide(branch_rate_mean_log, delta = 1.0, weight = 5.0, tune = true)
moves[++mvi] = mvScale(branch_rate_mean_log, lambda = 1.0, weight = 3.0, tune = true)
branch_rate_mean := 10^branch_rate_mean_log
branch_rate_sd ~ dnExponential(1)
moves[++mvi] = mvScaleBactrian(branch_rate_sd, lambda = 1, weight = 5.0, tune = true)
for(j in 1:n_branches) {
branch_rates[j] ~ dnLognormal( ln(branch_rate_mean) - (branch_rate_sd^2)/2, branch_rate_sd)
moves[++mvi] = mvScaleBactrian(branch_rates[j], lambda=0.5, weight=2.0)
}
## DIVERSIFICATION RATE FOR BIRTH-DEATH TREE MODEL ========================
print("Defining birth-death tree model")
#tertiariaFossil ~ dnUniform(22.8, 34.0)
originTime ~ dnUniform(0.1, 15)
originTime.setValue(7.0)
moves[++mvi] = mvSlide(originTime, delta = 1.0, weight = 1.0)
moves[++mvi] = mvSlide(originTime, delta = 2.0, weight = 1.0)
# Speciation and extinction rate priors (N0 = 2, Nt = 4,165)
net_diversification_rate := abs(ln( n_taxa ) / originTime)
net_diversification_rate_sd <- 0.587405
#speciation_rate ~ dnExponential( lambda = 1/net_diversification_rate )
net_div_rate ~ dnLognormal( ln(net_diversification_rate) - (net_diversification_rate_sd^2)/2, net_diversification_rate_sd )
relative_extinction_rate ~ dnUniform(0.0, 1.0)
speciation_rate := abs(net_div_rate / (1 - relative_extinction_rate) )
extinction_rate := speciation_rate * relative_extinction_rate
moves[++mvi] = mvScaleBactrian(net_div_rate, lambda=1.0, weight=4.0, tune = true)
moves[++mvi] = mvSlide(net_div_rate, delta=1.0, weight=2.0, tune = true)
moves[++mvi] = mvSlide(relative_extinction_rate, delta=0.1, weight=3.0, tune = true)
fbd_dist = dnBDP(rootAge = abs(originTime),
lambda = speciation_rate,
mu = extinction_rate,
rho = 1.0,
condition="nTaxa",
taxa = taxa,
sampling = "uniform")
outgroup_taxa = clade("Te_Ve_Ca_An_AshSus-12D", "Te_Vi_Me_Me_TPhy16-1B")
ingroup_taxa = clade("Te_Bj_Oa_Ka_TPhy16-12B",
"Te_Ef_Ka_Mw_AshSus-3G",
"RGG-008_EFKau08.JG8_12S_COI_n.sp_stelarobusta-ish__Kauai_Waiahuakua",
"RGG-014_EFAuwahi00014Y40116Sb2_16S_T.stelarobusta_Maui_Auwahi",
"RGS-070_EFHaMKGJ70_16S_COI_n.sp_stelarobusta-ish__Hawaii_MaunaKea",
"RGG-A10_EFHaPuHuHuC5A10_COI_n.sp_stelarobusta-ish_Hawaii_PuuHuluhulu",
"Te_Eo_Oa_Ka_TPhy16-3H",
"Te_El_Oa_Ka_GutPhy-11H",
"Te_Gd_Ha_Pu_GutPhy-4F",
"Te_Lc_Ha_Pu_TPhy16-8C",
"RGG-J33_PAOa16J33_COI_T.n.sp_pointedabdomen__Oahu_Ohikilolo",
"Te_Rd_Ha_Pu_GutPhy-4E",
"Te_Wv_Ka_Pi_AshSus-2H",
"Te_Wv_Ka_Wa_AshSus-3A",
"Te_Ac_Ma_Wa_GutPhy-11F",
"RGG-S03_PAWhMa05JS3_COI_T.albida_Maui_Auwahi",
"Te_An_Ha_Ol_TPhy16-5F",
"Te_Br_Ha_Wa_GutPhy-5G",
"Te_Br_Ma_Wa_TPhy16-1C",
"Te_Eu_Ma_Pd_TPhy16-10C",
"Te_Fi_Ma_Pd_TPhy16-10A",
"Te_Ha_Oa_Ka_TPhy16-5C",
"Te_Ka_Ma_Bc_TPhy16-11A",
"Te_Ka_Mo_Ka_AshSus-4G",
"Te_Ke_Ka_Aw_TPhy16-7D",
"Te_Ac_Ha_Hu_TPhy16-2H",
"Te_Ki_Ma_Lw_TPhy16-8H",
"RGS-023_SmSpnPWaaHA23Y11216SB2_16S_T.kukuhaa_Hawaii_PuuWaawaa",
"Te_Ku_Oa_Ph_TPhy16-11G",
"Te_Ef_Oa_Ta_TPhy16-4B",
"RGG-J26_OaK33J26_COI_T.limu_Oahu_Kaala",
"Te_Mc_Ma_Pd_TPhy16-9G",
"Te_Mk_Ka_Pi_AshSus-4A",
"Te_Mo_Ka_Mw_AshSus-3F",
"Te_Ob_Ha_Pu_GutPhy-5E",
"RGG-339_OPal31.N_COI_T.palikea_Oahu_Palikea",
"Te_Ps_Ca_Bo_AshSus-11H",
"Te_Pa_Ma_Bc_TPhy16-10H",
"Te_Pa_Mo_Ka_AshSus-4D",
"RGG-292_RGMaPKJG14_12S_T.paludicola_Maui_PuuKukui",
"Te_Pk_Ha_Pu_GutPhy-5D",
"Te_Pe_Oa_Ka_TPhy16-12D",
"Te_Pi_Ka_Pi_AshSus-2F",
"Te_Po_Oa_Ka_TPhy16-3B",
"Te_Qu_Ma_Wa_GutPhy-11G",
"Te_Re_Ma_Lw_TPhy16-9A",
"Te_St_Ma_Pd_TPhy16-11E",
"RGG-641_tantalus_2_COI_T.tantalus_Oahu_unk",
"Te_Tr_Ma_Wa_GutPhy-11A",
"RGS-J71_OaHa09J71_16S_COI_T.lena_Oahu_Halo.Waian",
"Te_Wa_Ma_Wa_GutPhy-11D")
# Create constrained tree
fbd_tree ~ dnConstrainedTopology(fbd_dist, constraints = v(outgroup_taxa, ingroup_taxa))
crownAgeInGroup := tmrca(fbd_tree, ingroup_taxa, stemAge = false)
moves[++mvi] = mvNNI(fbd_tree, weight=30.0)
moves[++mvi] = mvFNPR(fbd_tree, weight=10.0)
moves[++mvi] = mvNarrow(fbd_tree, weight = 5.0)
moves[++mvi] = mvRateAgeBetaShift(fbd_tree, branch_rates, weight=10.0)
moves[++mvi] = mvNodeTimeSlideUniform(fbd_tree, weight=20.0)
# Some special moves to help with mixing
up_down_scale_index = ++mvi
moves[up_down_scale_index] = mvUpDownScale(weight=8.0)
moves[up_down_scale_index].addVariable(originTime, up=true)
moves[up_down_scale_index].addVariable(net_div_rate, up=false)
moves[up_down_scale_index].addVariable(branch_rate_mean_log, up=false)
moves[up_down_scale_index].addVariable(branch_rates, up=false) # when branch rates are high, less time has elapsed
print("Defining phylogenetic CTMC model")
for(i in 1:n_data_subsets){
seq[i] ~ dnPhyloCTMC(tree = fbd_tree,
Q = Q[i],
branchRates = branch_rates * part_rate[i],
siteRates = gamma_rates[i],
type = "DNA")
#pInv = pinvar[i])
seq[i].clamp(data[i])
}
mymodel = model(seq)
print("Defining monitors")
monitors[++mni] = mnModel(filename=out_fn+output_name+".log", printgen=10)
monitors[++mni] = mnFile(fbd_tree, filename=out_fn+output_name+".trees", printgen=10)
monitors[++mni] = mnScreen(printgen=10, speciation_rate, extinction_rate)
#monitors[++mni] = mnStochasticCharacterMap(ctmc=m_bg, filename=out_fn+output_name+"stoch.log",printgen=10)
#monitors[++mni] = mnJointConditionalAncestralState(tree=fbd_tree,
# ctmc=m_bg,
# type="NaturalNumbers",
# withTips=true,
# withStartStates=true,
# filename=out_fn+output_name+"states.log",
# printgen=10)
## RUN ANALYSIS
mymcmc = mcmc(mymodel, moves, monitors, nruns = 20)
mymcmc.burnin(10000, tuningInterval = 100)
mymcmc.operatorSummary()
mymcmc.run(100000)