table 4.3 JC69ΒΆ

Although incomplete ambiguity of observations is not supported, it is possible to emulate these types of observations using an expanded multivariate state space.

In this example, we expand a univariate 4-state nucleotide process to a \(2 \times 2 \times 2 \times 2\) multivariate process. The edge rate scaling factors are estimated using expectation maximization to find the log likelihood of -6,262.01 reported in Ziheng Yang’s 2014 textbook.

The output includes the results of 8 iterations of EM.

  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
from __future__ import print_function, division, absolute_import

import copy
import json

import pyparsing

import numpy as np
from numpy.testing import assert_equal

import jsonctmctree.interface

s_tree = """(kiwi_fruit, ((((agave, garlic), rice), black_pepper),
((cabbage, cotton), (cucumber, walnut))), (sunflower, (tomato, tobacco)))"""

def _help_build_tree(parent, root, node, name_to_node, edges):
    if parent is not None:
        edges.append((parent, node))
    neo = node + 1
    if isinstance(root, basestring):
        name_to_node[root] = node
    else:
        for element in root:
            neo = _help_build_tree(node, element, neo, name_to_node, edges)
    return neo

def get_tree_info():
    # Return a dictionary mapping name to node index,
    # and return a list of edges as ordered pairs of node indices.
    tree = s_tree.replace(',', ' ')
    nestedItems = pyparsing.nestedExpr(opener='(', closer=')')
    tree = (nestedItems + pyparsing.stringEnd).parseString(tree).asList()[0]
    name_to_node = {}
    edges = []
    _help_build_tree(None, tree, 0, name_to_node, edges)
    return name_to_node, edges

def get_nucleotide_alignment_info(name_to_node):
    # Return an ordered list of observable node indices,
    # and return the array of iid observations.
    # An observed state of -1 means completely missing data.
    nt_to_state = {
            'A' : [1, 0, 0, 0],
            'C' : [0, 1, 0, 0],
            'G' : [0, 0, 1, 0],
            'T' : [0, 0, 0, 1],
            '?' : [-1, -1, -1, -1],
            '-' : [-1, -1, -1, -1],
            'N' : [-1, -1, -1, -1],
            'M' : [-1, -1, 0, 0],
            'R' : [-1, 0, -1, 0],
            'W' : [-1, 0, 0, -1],
            'S' : [0, -1, -1, 0],
            'Y' : [0, -1, 0, -1],
            'K' : [0, 0, -1, -1],
            }
    sequences = []
    nodes = []
    variables = []
    with open('vegetables.rbcL.txt') as fin:
        lines = fin.readlines()
        header = lines[0]
        for line in lines[1:]:
            name, sequence = line.strip().split()
            node = name_to_node[name]
            nodes.extend([node]*4)
            sequences.append([nt_to_state[c] for c in sequence])
            variables.extend([0, 1, 2, 3])
    # arr[node, site, variable]
    arr = np.array(sequences, dtype=int)
    arr = np.transpose(arr, (1, 0, 2))
    iid_observations = np.reshape(arr, (arr.shape[0], -1))
    iid_observations = iid_observations.tolist()
    return nodes, variables, iid_observations


def main():

    # Read the tree.
    name_to_node, edges = get_tree_info()
    edge_count = len(edges)
    node_count = edge_count + 1

    # Read the alignment.
    info = get_nucleotide_alignment_info(name_to_node)
    nodes, variables, iid_observations = info

    # Define the tree component of the scene
    row_nodes, column_nodes = zip(*edges)
    tree = dict(
            row_nodes = list(row_nodes),
            column_nodes = list(column_nodes),
            edge_rate_scaling_factors = [0.01] * edge_count,
            edge_processes = [0] * edge_count)

    # Define the Jukes-Cantor process.
    # Rates are scaled so that the exit rate is 1 from every state.
    row_states = []
    column_states = []
    rates = []
    ident = np.identity(4, dtype=int).tolist()
    for i in range(4):
        for j in range(4):
            if i != j:
                row_states.append(ident[i])
                column_states.append(ident[j])
                rates.append(1/3)
    process_definition = dict(
            row_states = row_states,
            column_states = column_states,
            transition_rates = rates)

    # Define the root distribution.
    root_prior = dict(
            states = ident,
            probabilities = [0.25, 0.25, 0.25, 0.25])

    # Define the observed data.
    observed_data = dict(
            nodes = nodes,
            variables = variables,
            iid_observations = iid_observations)

    # Assemble the scene.
    scene = dict(
            node_count = node_count,
            process_count = 1,
            state_space_shape = [2, 2, 2, 2],
            tree = tree,
            root_prior = root_prior,
            process_definitions = [process_definition],
            observed_data = observed_data)

    # Define some requests.
    # These include the log likelihood
    # and the sum of transition count expectations.
    requests = [
            {"property" : "SNNLOGL"},
            {
                "property" : "SDNTRAN",
                "transition_reduction" : {
                    "row_states" : row_states,
                    "column_states" : column_states,
                    "weights" : [1] * len(row_states)
                }
            }]

    # Request some stuff.
    j_in = {
            "scene" : scene,
            "requests" : requests
            }

    arr = []
    j_out = None
    nsites = len(iid_observations)
    for i in range(8):
        if j_out is None:
            j_out = jsonctmctree.interface.process_json_in(j_in)
        else:
            ll, trans_counts = j_out['responses']
            edge_rates = [t/nsites for t in trans_counts]
            j_in['scene']['tree']['edge_rate_scaling_factors'] = edge_rates
            j_out = jsonctmctree.interface.process_json_in(j_in)
        arr.append(copy.deepcopy(j_out))

    print(json.dumps(arr, indent=4))


main()
  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
[
    {
        "status": "feasible", 
        "responses": [
            -6749.528980298121, 
            [
                38.54275388560657, 
                12.037458550453508, 
                40.25804994338261, 
                38.6294765231242, 
                30.956776009282507, 
                37.905224291636515, 
                27.076723254699374, 
                98.91403030760839, 
                72.57833339446896, 
                10.915565332307388, 
                19.818530425852366, 
                75.4194698042263, 
                45.878226215070306, 
                18.55467221991212, 
                50.8147692090247, 
                34.95460442071937, 
                19.535805753716325, 
                64.19946564149734, 
                57.14875533831474, 
                12.279596433719373, 
                6.273710329493812
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6269.538919189791, 
            [
                42.36745935386131, 
                8.343051121349692, 
                39.80229475456449, 
                33.82736183847302, 
                25.38361434935728, 
                41.14491161678539, 
                27.24204157259882, 
                117.18477482945298, 
                83.17253553302476, 
                8.359629610310979, 
                19.423860770757848, 
                80.19248152905962, 
                46.204607788170875, 
                17.412114829006097, 
                55.28870029823665, 
                33.97401029224916, 
                16.13674981226181, 
                68.51605179424939, 
                59.457459696833936, 
                12.732578296702457, 
                5.408973242355975
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.648537452265, 
            [
                43.26155383917572, 
                7.377407685404555, 
                39.452290914705884, 
                31.803358090339447, 
                23.02986859386654, 
                41.69395769194157, 
                27.104405858749466, 
                121.81814776580859, 
                85.68052632101165, 
                7.827083920444984, 
                19.639482660481956, 
                80.7411381917629, 
                46.11170689049418, 
                17.298793103678776, 
                56.27124624729835, 
                33.39112998360662, 
                15.31911320355264, 
                69.29635966845777, 
                59.794832403922484, 
                12.872426981591603, 
                5.266804842862319
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.0766288079885, 
            [
                43.43972227516929, 
                7.097242101593483, 
                39.41560284934213, 
                31.16302099630199, 
                22.186268209151805, 
                41.80005373451851, 
                27.072898822436837, 
                123.16440187335672, 
                86.35518545626206, 
                7.717453395101064, 
                19.769411855630814, 
                80.82128846120366, 
                46.05366552971124, 
                17.311826059875358, 
                56.54276249800578, 
                33.17637078535594, 
                15.160381698517718, 
                69.47714143694492, 
                59.88886885322965, 
                12.897759415300118, 
                5.242207596873577
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.021218360713, 
            [
                43.4666592227035, 
                7.007028544921212, 
                39.43300881628473, 
                30.973573921125244, 
                21.882814323238073, 
                41.82195995619257, 
                27.071402862284955, 
                123.58231892189434, 
                86.54588656745273, 
                7.698089979842188, 
                19.81890208382613, 
                80.83705923220205, 
                46.02914416918375, 
                17.321711384134503, 
                56.6263771459661, 
                33.102189459842094, 
                15.141732266103729, 
                69.51751060338685, 
                59.91492303942013, 
                12.901981436535692, 
                5.238133741051998
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.015269165555, 
            [
                43.46765103953117, 
                6.975135058680808, 
                39.446628514616236, 
                30.920939532982572, 
                21.771262239982473, 
                41.82658418573561, 
                27.07363049487077, 
                123.71703630476571, 
                86.60096140804822, 
                7.697252465108768, 
                19.835359366498157, 
                80.84136325347782, 
                46.02004663383481, 
                17.32470852830142, 
                56.65338805715798, 
                33.07740465628548, 
                15.146052262833477, 
                69.52528735443195, 
                59.9212734954846, 
                12.902794402731823, 
                5.237339200662891
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.014562740338, 
            [
                43.466125164186806, 
                6.962854386714466, 
                39.45297358636966, 
                30.908049315202256, 
                21.729304846744984, 
                41.82757259281829, 
                27.07511201974201, 
                123.7615655358188, 
                86.61705637162535, 
                7.699259478467494, 
                19.840476413726925, 
                80.8429093535462, 
                46.01675839941207, 
                17.3251771283209, 
                56.66221430698873, 
                33.06937013598747, 
                15.15083554279568, 
                69.5263020430904, 
                59.922479915025, 
                12.903064539870932, 
                5.2370679723310225
            ]
        ]
    }, 
    {
        "status": "feasible", 
        "responses": [
            -6262.0144698716895, 
            [
                43.465231282349144, 
                6.957760143386452, 
                39.45543646714917, 
                30.905896448964036, 
                21.71316292192042, 
                41.82779368593223, 
                27.07580783269745, 
                123.77654998947922, 
                86.62177587728902, 
                7.700989279531713, 
                19.8419703359981, 
                80.8435784642758, 
                46.01553157780708, 
                17.32503450330232, 
                56.665059542726176, 
                33.06686677911831, 
                15.15353043699532, 
                69.52622770286203, 
                59.922529594330896, 
                12.903201969935026, 
                5.236928552139572
            ]
        ]
    }
]