Attach a callback function to a given node of a phylo object aggregated by a PhyloSim object

Share:

Description

Attach a callback function to a given node of a phylo object aggregated by a PhyloSim object.

A "node hook" is a function which accepts a Sequence object through the named argument "seq" and returns a Sequence object. The node hook function must accept any object which inherits from the Sequence class!

After simulating the branch leading to the node, the resulting Sequence object is passed to the node hook and the returned object is used to simulate the downstream branches.

By using node hooks the attached processes can be replaced during simulation, hence enabling the simulation of non-homogeneous sequence evolution.

Usage

1
2
## S3 method for class 'PhyloSim'
attachHookToNode(this, node=NA, fun=NA, ...)

Arguments

this

A PhyloSim object.

node

Node identifier.

fun

A function (see above).

...

Not used.

Value

The PhyloSim object (invisible).

Author(s)

Botond Sipos, Gregory Jordan

See Also

For more information see PhyloSim.

Examples

 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
	# Create a PhyloSim object.
	# Provide the phylo object
	# and the root sequence.
	sim<-PhyloSim(
		name="TinySim",
		phylo=rcoal(3),
		root.seq=NucleotideSequence(string="ATGC",processes=list(list(JC69())))
	);
	# create a node hook function
	hook<-function(seq=NA){
		# replace the substitution process with F84
		if(inherits(seq,"NucleotideSequence")){
			cat("Replacing JC69 with F84.\n");
			seq$processes<-list(list(F84(rate.params=list("Kappa" = 2))));
		}
		return(seq);
	}
	# attach hook function to node 5
	attachHookToNode(sim,5,hook);
	# Run the simulation
	Simulate(sim);
	# Check if the processes have been truly replaced
	lapply(sim$sequences, getUniqueProcesses.Sequence)
	# Print the resulting alignment
	sim$alignment
 

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.