table 1 by Minin and SuchardΒΆ
This example reproduces Table 1 of “Counting labeled transitions in continuous-time Markov models of evolution” by Minin and Suchard, up to sampling error.
In the paper they sampled nucleotide sequences of length 1000 and computed labeled transition count expectations conditional on those sequences, but here I’ve computed the expectations in the limit as the sequence length increases. The discrepancies in the reported conditional expectations seem to be within the sampling error.
Original table published by Minin and Suchard:
| k | Transitions | Transversions | ||||
|---|---|---|---|---|---|---|
| Prior | iid Post. | CP Post. | Prior | iid Post. | CP Post. | |
| 1 | 233.3 | 333.7 | 313.0 | 466.7 | 342.3 | 316.4 |
| 2 | 350.0 | 395.6 | 373.6 | 350.0 | 281.1 | 257.3 |
| 4 | 466.7 | 455.4 | 433.0 | 233.3 | 244.8 | 221.4 |
Reconstructed table from the output of the Python script below:
| k | Transitions | Transversions | ||||
|---|---|---|---|---|---|---|
| Prior | iid Post. | CP Post. | Prior | iid Post. | CP Post. | |
| 1 | 233.3 | 332.6 | 316.3 | 466.7 | 343.2 | 333.2 |
| 2 | 350.0 | 399.5 | 381.7 | 350.0 | 274.8 | 267.2 |
| 4 | 466.7 | 464.5 | 446.3 | 233.3 | 232.8 | 226.3 |
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 270 271 272 273 274 275 276 277 278 279 280 281 | """
This is really a K80 model because the nucleotide distribution is uniform.
Reproduce an example from Minin and Suchard
"Counting labeled transitions in continuous-time Markov models of evolution."
"""
from __future__ import print_function, division
import itertools
import copy
import numpy as np
from numpy.testing import assert_allclose
from jsonctmctree.interface import process_json_in
_template = """\
+------+-------------------------------+-------------------------------+
| k | Transitions | Transversions |
+------+--------+-----------+----------+--------+-----------+----------+
| | Prior | iid Post. | CP Post. | Prior | iid Post. | CP Post. |
+======+========+===========+==========+========+===========+==========+
| 1 | {0000} | {0001} | {0002} | {0003} | {0004} | {0005} |
+------+--------+-----------+----------+--------+-----------+----------+
| 2 | {0006} | {0007} | {0008} | {0009} | {0010} | {0011} |
+------+--------+-----------+----------+--------+-----------+----------+
| 4 | {0012} | {0013} | {0014} | {0015} | {0016} | {0017} |
+------+--------+-----------+----------+--------+-----------+----------+\
"""
def gen_K80():
# Use the nucleotide order from the Minin and Suchard paper.
# A, G, C, T
transitions = ((0, 1), (1, 0), (2, 3), (3, 2))
for i in range(4):
for j in range(4):
if i != j:
if (i, j) in transitions:
ts, tv = 1, 0
else:
ts, tv = 0, 1
yield i, j, ts, tv
def get_K80_process_definition(kappa):
exit_rates = np.zeros(4)
row_states = []
column_states = []
unnormalized_rates = []
for i, j, ts, tv in gen_K80():
row_states.append([i])
column_states.append([j])
rate = kappa * ts + tv
exit_rates[i] += rate
unnormalized_rates.append(rate)
# In the K80 model all exit rates are equal,
# and each is equal to the expected rate.
expected_rate = exit_rates.mean()
assert_allclose(exit_rates, expected_rate)
transition_rates = [r / expected_rate for r in unnormalized_rates]
return dict(
row_states = row_states,
column_states = column_states,
transition_rates = transition_rates)
def get_observation_reduction(likelihoods):
npatterns = len(likelihoods)
return dict(
observation_indices = range(npatterns),
weights = likelihoods)
def get_ts_reduction():
return dict(
row_states = [[i] for i, j, ts, tv in gen_K80() if ts],
column_states = [[j] for i, j, ts, tv in gen_K80() if ts],
weights = [1 for i, j, ts, tv in gen_K80() if ts])
def get_tv_reduction():
return dict(
row_states = [[i] for i, j, ts, tv in gen_K80() if tv],
column_states = [[j] for i, j, ts, tv in gen_K80() if tv],
weights = [1 for i, j, ts, tv in gen_K80() if tv])
def run_partitioned_analysis(scene, partitioned_likelihoods, kappa):
# Copy the scene because we are going to do some surgery.
scene = copy.deepcopy(scene)
scene['tree'] = get_analysis_tree()
analysis_process = get_K80_process_definition(kappa)
scene['process_definitions'] = [analysis_process]
# Request nucleotide transition count expectations.
ts_requests = []
for likelihoods in partitioned_likelihoods:
ts_request = dict(
property = 'WSNTRAN',
observation_reduction = get_observation_reduction(likelihoods),
transition_reduction = get_ts_reduction())
ts_requests.append(ts_request)
# Request nucleotide transversion count expectations.
tv_requests = []
for likelihoods in partitioned_likelihoods:
tv_request = dict(
property = 'WSNTRAN',
observation_reduction = get_observation_reduction(likelihoods),
transition_reduction = get_tv_reduction())
tv_requests.append(tv_request)
# Run the analysis.
j_in = dict(
scene = scene,
requests = ts_requests + tv_requests)
j_out = process_json_in(j_in)
ts_responses = j_out['responses'][:3]
tv_responses = j_out['responses'][3:]
partitioned_ts = np.mean(ts_responses)
partitioned_tv = np.mean(tv_responses)
return partitioned_ts, partitioned_tv
def run_analysis(scene, likelihoods, kappa):
# Copy the scene because we are going to do some surgery.
scene = copy.deepcopy(scene)
# Change the edge rate scaling factors.
scene['tree'] = get_analysis_tree()
# Define the model to be used for the analysis.
analysis_process = get_K80_process_definition(kappa)
scene['process_definitions'] = [analysis_process]
# Request nucleotide transition count expectations.
ts_transition_request = dict(
property = 'WSNTRAN',
observation_reduction = get_observation_reduction(likelihoods),
transition_reduction = get_ts_reduction())
# Request nucleotide transversion count expectations.
tv_transition_request = dict(
property = 'WSNTRAN',
observation_reduction = get_observation_reduction(likelihoods),
transition_reduction = get_tv_reduction())
# Define the requests.
requests = [ts_transition_request, tv_transition_request]
# Run the analysis.
j_in = dict(
scene = scene,
requests = requests)
j_out = process_json_in(j_in)
posterior_ts, posterior_tv = j_out['responses']
# Re-run the analysis without any observations.
scene['observed_data'] = dict(
nodes = [],
variables = [],
iid_observations = [[] for p in likelihoods])
j_in = dict(
scene = scene,
requests = requests)
j_out = process_json_in(j_in)
prior_ts, prior_tv = j_out['responses']
return prior_ts, prior_tv, posterior_ts, posterior_tv
def get_analysis_tree():
return dict(
row_nodes = [0, 0, 1, 1],
column_nodes = [2, 1, 3, 4],
edge_rate_scaling_factors = [0.28, 0.21, 0.12, 0.09],
edge_processes = [0, 0, 0, 0])
def get_simulation_tree(rate_scaling_factor):
# Define the tree used for simulation in the Minin and Suchard paper.
# In our case, we will use this to compute log likelihoods for all possible
# alignment site patterns and to subsequently use these likelihoods
# as site-specific weights.
row_nodes = [0, 0, 1, 1]
column_nodes = [2, 1, 3, 4]
rates = np.array([0.3, 0.2, 0.1, 0.1]) * rate_scaling_factor
return dict(
row_nodes = row_nodes,
column_nodes = column_nodes,
edge_rate_scaling_factors = rates.tolist(),
edge_processes = [0, 0, 0, 0])
def get_pattern_likelihoods(scene, rate_scaling_factor):
scene = copy.deepcopy(scene)
scene['tree'] = get_simulation_tree(rate_scaling_factor)
# Define the request for per-pattern log likelihoods.
log_likelihoods_request = {'property' : 'DNNLOGL'}
# Get the per-pattern log likelihoods.
j_in = dict(
scene = scene,
requests = [log_likelihoods_request])
j_out = process_json_in(j_in)
pattern_log_likelihoods = j_out['responses'][0]
pattern_likelihoods = np.exp(pattern_log_likelihoods)
# The sum of likelihoods over all patterns should be 1.
assert_allclose(pattern_likelihoods.sum(), 1)
# Return the pattern likelihoods.
return pattern_likelihoods.tolist()
def main():
# The root prior for every process in this project is uniform.
root_prior = dict(
states = [[0], [1], [2], [3]],
probabilities = [0.25, 0.25, 0.25, 0.25])
# Define the simulation process.
simulation_process = get_K80_process_definition(4)
# Define all possible site patterns.
all_site_patterns = list(itertools.product(range(4), repeat=3))
# Observation data consisting of all site patterns.
observed_all_site_patterns = dict(
nodes = [2, 3, 4],
variables = [0, 0, 0],
iid_observations = all_site_patterns)
# Define the scene associated with the simulation.
scene = dict(
node_count = 5,
process_count = 1,
state_space_shape = [4],
tree = None,
root_prior = root_prior,
process_definitions = [simulation_process],
observed_data = observed_all_site_patterns)
pattern_likelihoods = get_pattern_likelihoods(scene, 1)
partitioned_pattern_likelihoods = []
position_rates = np.array([1.5, 1.0, 3.0])
position_rates = position_rates / position_rates.mean()
for rate_scaling_factor in position_rates:
likelihoods = get_pattern_likelihoods(scene, rate_scaling_factor)
partitioned_pattern_likelihoods.append(likelihoods)
# Run an analysis for each of a few values of kappa.
arr = []
for kappa in 1, 2, 4:
prior_ts, prior_tv, post_ts, post_tv = run_analysis(
scene, pattern_likelihoods, kappa)
partitioned_ts, partitioned_tv = run_partitioned_analysis(
scene, partitioned_pattern_likelihoods, kappa)
arr.extend((
prior_ts, post_ts, partitioned_ts,
prior_tv, post_tv, partitioned_tv))
# Print the table using the template.
s = ['{:>6.1f}'.format(x * 1000) for x in arr]
rst_table_string = _template.format(*s)
print(rst_table_string)
main()
|