-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcodon_models.jl
362 lines (335 loc) · 11.6 KB
/
codon_models.jl
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
nuc2num = Dict('A' => 1, 'C' => 2, 'G' => 3, 'T' => 4);
#Returns the "codon_pos,from,to" position in a nucleotide table
function codon_diff(c1, c2)
diffs = [c1[i] != c2[i] for i = 1:3]
if sum(diffs) == 1
ind = [1, 2, 3][diffs][1]
return (ind, nuc2num[c1[ind]], nuc2num[c2[ind]])
else
return (-1, -1, -1) #Ugh, gross.
end
end
#A struct to hold the contents/lookup tables for a particular genetic code.
#Need to re-think what we should store here for various use-cases, including:
#Construction of codon Q matrices
#Construction of single rows of codon Q matrices
#Convert from codon to AA
#Convert from codon to nuc string
#F3x4 calculations
#complete exhaustive list
struct GeneticCode
genetic_code::Dict{String,Char}
codons::Array{String,1}
sense_codons::Array{String,1}
stop_codons::Array{String,1}
amino_acid_lookup::Array{Char,1}
sense2all::Array{Int64,1}
string2sense::Dict{String,Int}
syn_positions::Array{Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64,Int64}},1}
nonsyn_positions::Array{Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64,Int64}},1}
amino_acids::Array{Char,1}
codon2AA_pos::Array{Int64,1}
codon_to_nuc_map::Array{Tuple{Int64,Int64,Int64},2}
num_sense::Int64
function GeneticCode(genetic_code::Dict{String,Char})
codons = sort(collect(keys(genetic_code)))
sense_codons = [i for i in codons if genetic_code[i] != '*']
stop_codons = [i for i in codons if genetic_code[i] == '*']
amino_acid_lookup = [genetic_code[i] for i in sense_codons]
sense2all = zeros(Int, length(sense_codons))
count = 1
for i = 1:length(codons)
sense2all[count] = i
if genetic_code[codons[i]] != '*'
count += 1
end
end
string2sense = Dict{String,Int}()
for cod = 1:length(sense_codons)
string2sense[sense_codons[cod]] = cod
end
#These encode the position in the codon model, the 1,2,3 codon position difference, and the corresponding nucleotide change.
#They could be structured by nuc change to enable partial matrix updates when only some nuc parameters change.
syn_positions = Array{Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64,Int64}},1}([])
nonsyn_positions = Array{Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64,Int64}},1}([])
#A dense version of the above, for using when you only need to construct one row at a time
codon_to_nuc_map = hcat(
[
[(-1, -1, -1) for i = 1:length(sense_codons)] for
i = 1:length(sense_codons)
]...,
)
for i = 1:length(sense_codons)
for j = 1:length(sense_codons)
c1, c2 = codons[sense2all[i]], codons[sense2all[j]]
diff = codon_diff(c1, c2)
if diff != (-1, -1, -1)
codon_to_nuc_map[i, j] = diff
if amino_acid_lookup[i] == amino_acid_lookup[j]
push!(syn_positions, ((i, j), diff))
else
push!(nonsyn_positions, ((i, j), diff))
end
end
end
end
amino_acids = sort(union([genetic_code[cod] for cod in sense_codons]))
codon2AA_pos =
[findfirst(genetic_code[cod] .== amino_acids) for cod in sense_codons]
new(
genetic_code,
codons,
sense_codons,
stop_codons,
amino_acid_lookup,
sense2all,
string2sense,
syn_positions,
nonsyn_positions,
amino_acids,
codon2AA_pos,
codon_to_nuc_map,
length(sense_codons),
)
end
end
universal_genetic_code = Dict(
"ATA" => 'I',
"ATC" => 'I',
"ATT" => 'I',
"ATG" => 'M',
"ACA" => 'T',
"ACC" => 'T',
"ACG" => 'T',
"ACT" => 'T',
"AAC" => 'N',
"AAT" => 'N',
"AAA" => 'K',
"AAG" => 'K',
"AGC" => 'S',
"AGT" => 'S',
"AGA" => 'R',
"AGG" => 'R',
"CTA" => 'L',
"CTC" => 'L',
"CTG" => 'L',
"CTT" => 'L',
"CCA" => 'P',
"CCC" => 'P',
"CCG" => 'P',
"CCT" => 'P',
"CAC" => 'H',
"CAT" => 'H',
"CAA" => 'Q',
"CAG" => 'Q',
"CGA" => 'R',
"CGC" => 'R',
"CGG" => 'R',
"CGT" => 'R',
"GTA" => 'V',
"GTC" => 'V',
"GTG" => 'V',
"GTT" => 'V',
"GCA" => 'A',
"GCC" => 'A',
"GCG" => 'A',
"GCT" => 'A',
"GAC" => 'D',
"GAT" => 'D',
"GAA" => 'E',
"GAG" => 'E',
"GGA" => 'G',
"GGC" => 'G',
"GGG" => 'G',
"GGT" => 'G',
"TCA" => 'S',
"TCC" => 'S',
"TCG" => 'S',
"TCT" => 'S',
"TTC" => 'F',
"TTT" => 'F',
"TTA" => 'L',
"TTG" => 'L',
"TAC" => 'Y',
"TAT" => 'Y',
"TAA" => '*',
"TAG" => '*',
"TGC" => 'C',
"TGT" => 'C',
"TGA" => '*',
"TGG" => 'W',
)
const universal_code = GeneticCode(universal_genetic_code);
function count_F3x4(seqs::Array{String})
F3x4 = zeros(3, 4)
for k = 1:3
pos1inds = [3 * (i - 1) + k for i = 1:Int(length(seqs[1]) / 3)]
d = proportionmap(collect(join([s[pos1inds] for s in seqs])))
F3x4[k, 1] = get(d, 'A', 0.0)
F3x4[k, 2] = get(d, 'C', 0.0)
F3x4[k, 3] = get(d, 'G', 0.0)
F3x4[k, 4] = get(d, 'T', 0.0)
end
return F3x4
end
function F3x4_eq_freqs(F3x4; genetic_code = universal_code)
eq = [
F3x4[1, nuc2num[c[1]]] * F3x4[2, nuc2num[c[2]]] * F3x4[3, nuc2num[c[3]]] for
c in genetic_code.sense_codons
]
return eq ./ sum(eq)
end
function MG94_F3x4(alpha, beta, nuc_matrix, F3x4; genetic_code = universal_code)
codon_matrix =
zeros(length(genetic_code.sense_codons), length(genetic_code.sense_codons))
for p in genetic_code.syn_positions
codon_matrix[p[1][1], p[1][2]] =
alpha * nuc_matrix[p[2][2], p[2][3]] * F3x4[p[2][1], p[2][3]]
end
for p in genetic_code.nonsyn_positions
codon_matrix[p[1][1], p[1][2]] =
beta * nuc_matrix[p[2][2], p[2][3]] * F3x4[p[2][1], p[2][3]]
end
for i = 1:length(genetic_code.sense_codons)
codon_matrix[i, i] = -sum(codon_matrix[i, :])
end
return codon_matrix
end
function count_F61(seqs::Array{String}; code = universal_code)
F61 = zeros(length(code.sense_codons))
dic = proportionmap(
vcat(
[[seq[3*(i-1)+1:3*(i-1)+3] for i = 1:Int(length(seq) / 3)] for seq in seqs]...,
),
)
for i = 1:length(F61)
F61[i] = get(dic, code.sense_codons[i], 0.0)
end
return sum2one(F61)
end
function MG94_F61(alpha, beta, nuc_matrix, F61; genetic_code = universal_code)
codon_matrix =
zeros(length(genetic_code.sense_codons), length(genetic_code.sense_codons))
for p in genetic_code.syn_positions
codon_matrix[p[1][1], p[1][2]] = alpha * nuc_matrix[p[2][2], p[2][3]] * F61[p[1][2]]
end
for p in genetic_code.nonsyn_positions
codon_matrix[p[1][1], p[1][2]] = beta * nuc_matrix[p[2][2], p[2][3]] * F61[p[1][2]]
end
for i = 1:length(genetic_code.sense_codons)
codon_matrix[i, i] = -sum(codon_matrix[i, :])
end
return codon_matrix
end
function HB98_F61(alpha, nuc_matrix, F61; genetic_code = universal_code)
#See https://www.ncbi.nlm.nih.gov/pubmed/9656490 but beware of the typo in equation 9.
codon_matrix =
zeros(length(genetic_code.sense_codons), length(genetic_code.sense_codons))
for p in genetic_code.syn_positions
#a substitution from codon p[1][1] to codon p[1][2]
PIaPab = F61[p[1][1]] * nuc_matrix[p[2][2], p[2][3]]
PIbPba = F61[p[1][2]] * nuc_matrix[p[2][3], p[2][2]]
if PIaPab != PIbPba
f_ab = log(PIbPba / PIaPab) / (1 - (PIaPab / PIbPba))
else
f_ab = 1.0
end
codon_matrix[p[1][1], p[1][2]] = alpha * nuc_matrix[p[2][2], p[2][3]] * f_ab
end
for p in genetic_code.nonsyn_positions
#duplicated code, because this model doesn't really use the syn/non-syn distinction
PIaPab = F61[p[1][1]] * nuc_matrix[p[2][2], p[2][3]]
PIbPba = F61[p[1][2]] * nuc_matrix[p[2][3], p[2][2]]
if PIaPab != PIbPba
f_ab = log(PIbPba / PIaPab) / (1 - (PIaPab / PIbPba))
else
f_ab = 1.0
end
codon_matrix[p[1][1], p[1][2]] = alpha * nuc_matrix[p[2][2], p[2][3]] * f_ab
end
for i = 1:length(genetic_code.sense_codons)
codon_matrix[i, i] = -sum(codon_matrix[i, :])
end
return codon_matrix
end
function HB98_AAfit(alpha, nuc_matrix, AA_fitness; genetic_code = universal_code)
#Halpern and Bruno, but with the fitnesses directly parameterized.
#Closer to https://academic.oup.com/mbe/article/32/4/1097/1077799
#2Ns_ij = 2Nf_j - 2Nf_i
#Sub rate = mutation_ij * 2Ns_ij/(1-e^-2Ns_ij)
codon_matrix =
zeros(length(genetic_code.sense_codons), length(genetic_code.sense_codons))
for p in genetic_code.syn_positions
#In this AA_fitness model, fitnesses are always equal for syn changes.
codon_matrix[p[1][1], p[1][2]] = alpha * nuc_matrix[p[2][2], p[2][3]]
end
for p in genetic_code.nonsyn_positions
#duplicated code, because this model doesn't really use the syn/non-syn distinction
diff = (
AA_fitness[genetic_code.codon2AA_pos[p[1][2]]] -
AA_fitness[genetic_code.codon2AA_pos[p[1][1]]]
)#/(1+AA_fitness[genetic_code.codon2AA_pos[p[1][1]]]) #Need to think about this term in the context of the approximation
if abs(diff) < 0.001 #To catch the hole discontinuity a 2nd order approximation that is REALLY close.
f_ab = 1 / 12 * (12 + diff * (6 + diff)) #1+0.5*diff
else
f_ab = diff / (1 - exp(-diff))
end
codon_matrix[p[1][1], p[1][2]] = alpha * nuc_matrix[p[2][2], p[2][3]] * f_ab
end
for i = 1:length(genetic_code.sense_codons)
codon_matrix[i, i] = -sum(codon_matrix[i, :])
end
return codon_matrix
end
function codonHKY85(TrTv)
return [
0.0 1.0 TrTv 1.0
1.0 0.0 1.0 TrTv
TrTv 1.0 0.0 1.0
1.0 TrTv 1.0 0.0
]
end
mutable struct CodonPartition <: DiscretePartition
state::Array{Float64,2}
states::Int
sites::Int
scaling::Array{Float64,1}
function CodonPartition(sites; code = universal_code)
new(
zeros(length(code.sense_codons), sites),
length(code.sense_codons),
sites,
zeros(sites),
)
end
function CodonPartition(state, states, sites, scaling; code = universal_code)
@assert size(state) == (states, sites) && states == length(code.sense_codons)
new(state, states, sites, scaling)
end
end
#Make this handle IUPAC ambigs sensible. Any codon compatible with the ambig should get a 1.0
function obs2partition!(dest::CodonPartition, seq::String; code = universal_code)
problem_codons = String[]
if mod(length(seq), 3) != 0
error("Codon sequences must be divisible by 3")
end
if length(seq) / 3 != dest.sites
error("Sequence length does not match partition")
end
@views for j in axes(dest.state, 2)
c = seq[3*(j-1)+1:3*(j-1)+3]
cod_ind = get(code.string2sense, c, -1)
if cod_ind == -1
fill!(dest.state[:, j], 1.0)
push!(problem_codons, c)
else
fill!(dest.state[:, j], 0.0)
dest.state[cod_ind, j] = 1.0
end
end
fill!(dest.scaling, 0.0)
return countmap(problem_codons)
end
function partition2obs(part::CodonPartition; code = universal_code)
return join([code.sense_codons[argmax(part.state[:, i])] for i = 1:part.sites])
end