Skip to content

Commit abd5fc7

Browse files
authored
Merge pull request #80 from revbayes/checked_snippet
Allow lines to match non-continuously, and require indentation.
2 parents 050d441 + 76b591f commit abd5fc7

File tree

4 files changed

+51
-36
lines changed

4 files changed

+51
-36
lines changed

_plugins/snippet.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
def lines_match_in_order?(snippet_lines, file_lines)
2+
i = 0
3+
snippet_lines.each do |snippet_line|
4+
while i < file_lines.length && file_lines[i].strip != snippet_line.strip
5+
i += 1
6+
end
7+
return false if i == file_lines.length
8+
i += 1
9+
end
10+
true
11+
end
12+
113
module Liquid
214
class Snippet < Block
315
def initialize(tag_name, markup, options)
@@ -6,7 +18,8 @@ def initialize(tag_name, markup, options)
618
end
719

820
def render(context)
9-
code_block = super.strip
21+
code_block = super
22+
1023
code_lines = code_block.lines.map{ |l| l.strip }
1124

1225
# Site source directory (so we don't need absolute paths)
@@ -18,32 +31,21 @@ def render(context)
1831
file_path = File.expand_path(File.join(site_source, page_dir, @filename))
1932

2033
unless File.exist?(file_path)
21-
# return "❌ Error: File '#{@filename}' not found at #{file_path}."
2234
raise IOError, "❌ Error: File '#{@filename}' not found at #{file_path}."
2335
end
2436

2537
file_lines = File.readlines(file_path).map{ |l| l.strip }
2638

2739
# Search for exact match of code_lines in file_lines
28-
match_found = false
29-
(0..(file_lines.length - code_lines.length)).each do |start_idx|
30-
if file_lines[start_idx, code_lines.length] == code_lines
31-
match_found = true
32-
break
33-
end
34-
end
40+
match_found = lines_match_in_order?(code_lines, file_lines)
3541

3642
unless match_found
37-
# return "❌ Error: Code block not found in file `#{@filename}`:\n\n#{code_block}"
3843
raise RuntimeError, "❌ Error: Code block not found in file `#{@filename}`:\n\n#{code_block}"
3944
end
4045

4146
<<~MARKDOWN
4247
43-
44-
```
4548
#{code_block}
46-
```
4749
4850
MARKDOWN
4951
end

tutorials/chromo/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ assumed known. In further examples we'll jointly estimate chromosome
283283
evolution and the phylogeny.
284284

285285
{% snippet scripts/ChromEvol_simple.Rev %}
286-
phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
286+
phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
287287
{% endsnippet %}
288288

289289
We need to limit the maximum number of chromosomes allowed in our model,
@@ -292,17 +292,17 @@ arbitrary limit on the size of the state space that could be increased
292292
if necessary.
293293

294294
{% snippet scripts/ChromEvol_simple.Rev %}
295-
max_chromo = 26
295+
max_chromo = 26
296296
{% endsnippet %}
297297

298298
Now we get the observed chromosome counts from a tab-delimited file.
299299
{% snippet scripts/ChromEvol_simple.Rev %}
300-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
300+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
301301
{% endsnippet %}
302302
Finally, we initialize a variable for our vector of moves and monitors.
303303
{% snippet scripts/ChromEvol_simple.Rev %}
304-
moves = VectorMoves()
305-
monitors = VectorMonitors()
304+
moves = VectorMoves()
305+
monitors = VectorMonitors()
306306
{% endsnippet %}
307307

308308
### The Chromosome Evolution Model

tutorials/clocks/index.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ age being younger than 38 Mya is equal to 0, using this value to offset
229229
a prior distribution on the root-age.
230230

231231
First specify the occurrence-time of the fossil.
232-
```
233-
tHesperocyon <- 38.0
234-
```
232+
233+
{% snippet scripts/m_BDP_bears.Rev %}
234+
tHesperocyon <- 38.0
235+
{% endsnippet %}
236+
235237
We will assume a lognormal prior on the root age that is offset by the
236238
observed age of *Hesperocyon gregarius*. We can use the previous
237239
analysis by {% cite DosReis2012 %} to parameterize the lognormal prior on the root
@@ -241,22 +243,28 @@ distribution to equal $49 - 38 = 11$ Mya. Given the expected value of
241243
the lognormal (`mean_ra`) and a standard deviation (`stdv_ra`), we can
242244
also compute the location parameter of the lognormal (`mu_ra`).
243245

246+
{% snippet scripts/m_BDP_bears.Rev %}
244247
mean_ra <- 11.0
245248
stdv_ra <- 0.25
246249
mu_ra <- ln(mean_ra) - ((stdv_ra*stdv_ra) * 0.5)
250+
{% endsnippet %}
247251

248252
With these parameters we can instantiate the root age stochastic node
249253
with the offset value.
250254

255+
{% snippet scripts/m_BDP_bears.Rev %}
251256
root_time ~ dnLognormal(mu_ra, stdv_ra, offset=tHesperocyon)
257+
{% endsnippet %}
252258

253259
### Time Tree Stochastic Node
254260

255261
Now that we have specified all of the parameters of the birth-death
256262
process, we can create our stochastic node representing the tree
257263
topology and divergence times.
258264

265+
{% snippet scripts/m_BDP_bears.Rev %}
259266
timetree ~ dnBDP(lambda=birth_rate, mu=death_rate, rho=rho, rootAge=root_time, samplingStrategy="uniform", condition="nTaxa", taxa=taxa)
267+
{% endsnippet %}
260268

261269
### Creating a Node-Age Variable
262270

@@ -267,27 +275,35 @@ taxa using the `clade()` function. This will not restrict this node to
267275
be monophyletic, but just create a node that is the MRCA of the taxa
268276
listed (even if that node has descendants that are not named).
269277

270-
clade_Ursidae <- clade("Ailuropoda_melanoleuca","Tremarctos_ornatus","Helarctos_malayanus", "Ursus_americanus","Ursus_thibetanus","Ursus_arctos","Ursus_maritimus","Melursus_ursinus")
278+
{% snippet scripts/m_BDP_bears.Rev %}
279+
clade_Ursidae <- clade("Ailuropoda_melanoleuca","Tremarctos_ornatus","Helarctos_malayanus","Ursus_americanus","Ursus_thibetanus","Ursus_arctos","Ursus_maritimus","Melursus_ursinus")
280+
{% endsnippet %}
271281

272282
Once we have defined the node, we can create a deterministic node to
273283
monitor its age.
274284

285+
{% snippet scripts/m_BDP_bears.Rev %}
275286
tmrca_Ursidae := tmrca(timetree,clade_Ursidae)
287+
{% endsnippet %}
276288

277289
### Proposals on the Time Tree
278290

279291
Next, create the vector of moves. These tree moves act on node ages:
280292

281-
moves.append( mvNodeTimeSlideUniform(timetree, weight=30.0) ) )
293+
{% snippet scripts/m_BDP_bears.Rev %}
294+
moves.append( mvNodeTimeSlideUniform(timetree, weight=30.0 ) )
282295
moves.append( mvSlide(root_time, delta=2.0, tune=true, weight=10.0) )
283296
moves.append( mvScale(root_time, lambda=2.0, tune=true, weight=10.0) )
284297
moves.append( mvTreeScale(tree=timetree, rootAge=root_time, delta=1.0, tune=true, weight=3.0) )
298+
{% endsnippet %}
285299

286300
Then, we will add moves that will propose changes to the tree topology.
287301

302+
{% snippet scripts/m_BDP_bears.Rev %}
288303
moves.append( mvNNI(timetree, weight=8.0) )
289304
moves.append( mvNarrow(timetree, weight=8.0) )
290305
moves.append( mvFNPR(timetree, weight=8.0) )
306+
{% endsnippet %}
291307

292308
Now save and close the file. This file, with all the model
293309
specifications will be loaded by other `Rev` files.

tutorials/clocks/scripts/m_BDP_bears.Rev

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ mean_ra <- 11.0
5252
stdv_ra <- 0.25
5353
### the lognormal distribution is parameterized by mu which is a function of the mean and standard deviation
5454
mu_ra <- ln(mean_ra) - ((stdv_ra*stdv_ra) * 0.5)
55-
root_time ~ dnLnorm(mu_ra, stdv_ra, offset=tHesperocyon)
55+
root_time ~ dnLognormal(mu_ra, stdv_ra, offset=tHesperocyon)
5656

5757

5858
### the time tree is a stochastic node modeled by the constant rate birth-death process (dnBDP)
59-
timetree ~ dnBDP(lambda=birth_rate, mu=death_rate, rho=rho, rootAge=root_time,
60-
samplingStrategy="uniform", condition="nTaxa", taxa=taxa)
59+
timetree ~ dnBDP(lambda=birth_rate, mu=death_rate, rho=rho, rootAge=root_time, samplingStrategy="uniform", condition="nTaxa", taxa=taxa)
6160

6261

6362
### If you would like to specify a starting tree, simply use the .setValue() method
@@ -70,9 +69,7 @@ timetree ~ dnBDP(lambda=birth_rate, mu=death_rate, rho=rho, rootAge=root_time,
7069
### then a deterministic node for the age. This does not create a monophyletic constraint.
7170

7271
### The clade() function specifies the node that is the MRCA of all listed taxa
73-
clade_Ursidae <- clade("Ailuropoda_melanoleuca","Tremarctos_ornatus","Helarctos_malayanus",
74-
"Ursus_americanus","Ursus_thibetanus","Ursus_arctos","Ursus_maritimus",
75-
"Melursus_ursinus")
72+
clade_Ursidae <- clade("Ailuropoda_melanoleuca","Tremarctos_ornatus","Helarctos_malayanus","Ursus_americanus","Ursus_thibetanus","Ursus_arctos","Ursus_maritimus","Melursus_ursinus")
7673

7774

7875
### A deterministic node for a given "clade" will report the age of that node (even if they are not
@@ -82,12 +79,12 @@ tmrca_Ursidae := tmrca(timetree,clade_Ursidae)
8279
####### Tree Moves #######
8380

8481
### add moves on the tree node times, including the root time, which is outside of the timetree
85-
moves.append(mvNodeTimeSlideUniform(timetree, weight=30.0))
86-
moves.append(mvTreeScale(tree=timetree, rootAge=root_time, delta=1.0, tune=true, weight=3.0))
87-
moves.append(mvSlide(root_time, delta=2.0, tune=true, weight=10.0))
88-
moves.append(mvScale(root_time, lambda=2.0, tune=true, weight=10.0))
82+
moves.append( mvNodeTimeSlideUniform(timetree, weight=30.0 ) )
83+
moves.append( mvSlide(root_time, delta=2.0, tune=true, weight=10.0) )
84+
moves.append( mvScale(root_time, lambda=2.0, tune=true, weight=10.0) )
85+
moves.append( mvTreeScale(tree=timetree, rootAge=root_time, delta=1.0, tune=true, weight=3.0) )
8986

9087
### and moves for the tree topology
91-
moves.append(mvNNI(timetree, weight=8.0))
92-
moves.append(mvNarrow(timetree, weight=8.0))
93-
moves.append(mvFNPR(timetree, weight=8.0))
88+
moves.append( mvNNI(timetree, weight=8.0) )
89+
moves.append( mvNarrow(timetree, weight=8.0) )
90+
moves.append( mvFNPR(timetree, weight=8.0) )

0 commit comments

Comments
 (0)