-
Notifications
You must be signed in to change notification settings - Fork 2
/
SubstitutionModels.jl
244 lines (212 loc) · 6.15 KB
/
SubstitutionModels.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
const MCP_TIME_MIN = 1.0E-11
const MCP_TIME_MAX = 100.0
"""
Restriction(base_freq::Vector{Float64},
SubstitutionRates::Vector{Float64})::Tuple{Array{Float64,2}, Array{Float64,1}, Array{Float64,2}}
Calculate the eigenvalue decomposition of the Q matrix of the restriction site model.
The `SubstitutionRates` are ignored, and just for call stability.
The function returns the Eigenvectors, Eigenvalues, inverse of eigenvectors and
the scale factor for expected number changes per site
"""
function Restriction(
base_freq::Vector{T},
SubstitutionRates::Vector,
)::Tuple{Array{T,2},Array{T,1},Array{T,2},T} where {T<:Real}
@assert length(base_freq) == 2
D = [-one(T), zero(T)]
U = [base_freq[2] base_freq[2]; -one(T)+base_freq[2] zero(T)+base_freq[2]]
Uinv = inv(U)
mu::T = 1.0 / (2.0 * prod(base_freq))
return U, D, Uinv, mu
end
"""
JC(base_freq::Vector{Float64},
SubstitutionRates::Vector{Float64})::Tuple{Array{Float64,2}, Array{Float64,1}, Array{Float64,2}}
Calculate the eigenvalue decomposition of the Q matrix of the Jukes-Cantor model.
The `SubstitutionRates` are ignored, and just for call stability.
The function returns the Eigenvectors, Eigenvalues and inverse of eigenvectors.
"""
function JC(
base_freq::Vector{T},
SubstitutionRates::Vector,
)::Tuple{Array{T,2},Array{T,1},Array{T,2},T} where {T<:Real}
Nbases = length(base_freq)
Q = ones(T, Nbases, Nbases)
off_diag = 1.0 / Nbases
diag = off_diag * (Nbases - 1)
Q .= off_diag
Q[diagind(Nbases, Nbases)] .= -diag
D, U = eigen(Q)
Uinv = inv(U)
mu = 1 / sum(diag)
return U, D, Uinv, mu
end
"""
GTR(base_freq::Vector{Float64},
SubstitutionRates::Vector{Float64})::Tuple{Array{Float64,2}, Array{Float64,1}, Array{Float64,2}}
Calculate the eigenvalue decomposition of the Q matrix of the General Time Reversible model.
The function returns the Eigenvectors, Eigenvalues and inverse of eigenvectors.
"""
function GTR(
base_freq::Vector{T},
SubstitutionRates::Vector{T},
)::Tuple{Array{T,2},Array{T,1},Array{T,2},T} where {T<:Real}
Nbases = length(base_freq)
Q = setmatrix(SubstitutionRates)
Q = mapslices(x -> x .* base_freq, Q, dims = 2)
dia = sum(Q, dims = 2)
Q[diagind(Nbases, Nbases)] = -dia
D, U = eigen(Q)
Uinv = inv(U)
return U, D, Uinv, 1 / sum(dia)
end
"""
function freeK(base_freq::Vector{Float64},SubstitutionRates::AbstractArray)::Tuple{Array{N,2}, Array{N,1}, Array{N,2}, M} where {N <: Number, M <: Number}
FreeK model of substitution.
"""
function freeK(
base_freq::Vector{Float64},
SubstitutionRates::AbstractArray,
)::Tuple{Array,Array,Array,Float64}
Nrates = length(SubstitutionRates)
Nbases = Int(ceil(sqrt(Nrates)))
Q = zeros(Nbases, Nbases)
counter = 1
for i = 1:Nbases, j = 1:Nbases
if i != j
Q[j, i] = SubstitutionRates[counter]
counter += 1
end
end
dia = sum(Q, dims = 2)
Q[diagind(Nbases, Nbases)] = -dia
D, U = eigen(Q)
Uinv = inv(U)
return U, D, Uinv, 1 / sum(dia)
end
## Helper Functions ##
function setmatrix(vec_vals::Array{T,1})::Array{T,2} where {T}
n = length(vec_vals)
s = round((sqrt(8n + 1) + 1) / 2)
s * (s - 1) / 2 == n || error("setmatrix: length of vector is not triangular")
k = 0
Q = [i < j ? (k += 1; vec_vals[k]) : 0 for i = 1:s, j = 1:s]
Q .+ transpose(Q)
end
### Calculate Transition Matrices
function calculate_transition(
f::typeof(JC),
rate::R,
mu::R,
time::R1,
U::A,
Uinv::A,
D::Vector,
pi_::Vector,
)::Array{R1,2} where {R1<:Real,R<:Real,A<:AbstractArray{<:Real}}
t = rate * time
if t < MCP_TIME_MIN
return_mat = similar(U)
return_mat .= 0.0
return_mat[diagind(return_mat)] .= 1.0
return return_mat
elseif t > MCP_TIME_MAX
return_mat = similar(U)
return_mat .= 1.0 / length(pi_)
return return_mat
else
t *= mu
return (U * diagm(exp.(D .* t))) * Uinv
end
end
function calculate_transition(
f::typeof(Restriction),
rate::R,
mu::R,
time::R1,
U::A,
Uinv::A,
D::Vector,
pi_::Vector,
)::Array{Float64,2} where {R1<:Real,R<:Real,A<:AbstractArray{<:Real}}
return_mat = similar(U)
t = rate * time
if t < MCP_TIME_MIN
return_mat .= 0.0
return_mat[diagind(return_mat)] .= 1.0
elseif t > MCP_TIME_MAX
return_mat .= reverse(pi_)
else
t *= mu
return (U * diagm(exp.(D .* t))) * Uinv
end
return_mat
end
function calculate_transition(
f,
rate::R,
mu::R,
time::R,
U::A,
Uinv::A,
D::Vector,
pi_::Vector,
)::Array{Float64,2} where {R<:Real,A<:AbstractArray{<:Complex}}
return_mat = Array{Float64,2}(undef, length(pi_), length(pi_))
t = rate * mu * time
return_mat .=
abs.(BLAS.gemm('N', 'N', 1.0, BLAS.symm('R', 'L', diagm(exp.(D .* t)), U), Uinv))
return return_mat
end
function calculate_transition(
f::typeof(JC),
rate::R,
mu::R,
time::R,
U::A,
Uinv::A,
D::Vector,
pi_::Vector,
)::Array{Float64,2} where {R<:Real,A<:AbstractArray{<:Complex}}
return_mat = similar(U)
t = rate * time
if t < MCP_TIME_MIN
return_mat .= 0.0
return_mat[diagind(return_mat)] .= 1.0
elseif t > MCP_TIME_MAX
return_mat .= 1.0 / length(pi_)
else
t *= mu
return_mat .=
abs.(
BLAS.gemm('N', 'N', 1.0, BLAS.symm('R', 'L', diagm(exp.(D .* t)), U), Uinv),
)
end
return_mat
end
function calculate_transition(
f::typeof(Restriction),
rate::R,
mu::R,
time::R,
U::A,
Uinv::A,
D::Vector,
pi_::Vector,
)::Array{Float64,2} where {R<:Real,A<:AbstractArray{<:Complex}}
return_mat = similar(U)
t = rate * time
if t < MCP_TIME_MIN
return_mat .= 0.0
return_mat[diagind(return_mat)] .= 1.0
elseif t > MCP_TIME_MAX
return_mat .= reverse(pi_)
else
t *= mu
return_mat .=
abs.(
BLAS.gemm('N', 'N', 1.0, BLAS.symm('R', 'L', diagm(exp.(D .* t)), U), Uinv),
)
end
return_mat
end