-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdc_gy94.m
225 lines (189 loc) · 4.85 KB
/
dc_gy94.m
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
function [dS,dN,dN_dS,lnL,value] = dc_gy94(aln,a,b)
%DC_GY - dS, dN estimation by codeml method
%
% [dS,dN] = dc_gy94(aln,a,b)
% calculates dS and dN between sequence a and b in aln.
%
%%
% Molecular Biology and Evolution Toolbox (MBEToolbox)
% Author: James Cai
% Email: jcai@tamu.edu
% Website: http://bioinformatics.org/mbetoolbox/
%
% $LastChangedDate: 2013-01-05 12:04:29 -0600 (Sat, 05 Jan 2013) $
% $LastChangedRevision: 327 $
% $LastChangedBy: jcai $
if nargin < 3, error('DC_GY94 requires at least three input arguments'); end
global noise
noise=1;
if (isstruct(aln)), seq=aln.seq; else seq=aln; end
[seq]=rmcodongaps(seq);
s1=seq(a,:); s2=seq(b,:);
%if (nargin<4)
% kappa=1.6; % fixed kappa
% omega=0.8;
% md=modelgy94(omega,kappa);
%end
%%
% Guess: if not codonise61ed then do it
%%
if (sum(s1>5)<2 && sum(s2>5)<2),
s1=codonise61(s1); s2=codonise61(s2);
% disp('s1 and s2 have been codonise61 now')
end
m=size(s1,2);
%select=nargin-2;
select=1;
switch (select)
case (1)
disp('optimising everything: t, kappa and omega')
[para,lnL]=i_optimtko(s1,s2);
t=para(1);
kappa=para(2);
omega=para(3);
md=modelgy94(omega,kappa); % build model from optimised values
case (2)
disp('fixed kappa, optimising t and omega.')
error('under development!')
case (3)
disp('fixed kappa and omega, optimising t.')
error('under development!')
kappa=1.6; % fixed kappa
omega=0.8;
md=modelgy94(omega,kappa);
[t,lnL] = optimlikelidist(md,s1,s2,0,2);
otherwise
error('invalid selection!')
end
%%
% Composes substitution rate matrix, Q
%%
Q=composeQ(md.R,diag(md.freq))./61;
%[V,D] = eig(Q*t);
%P=V*diag(exp(diag(D)))/V;
%%P=expm(Q*t);
%%
% Making a mask matrix, M
%%
icode=1; [TABLE] = codontable;
stops=TABLE(icode,:)=='*';
TABLE=TABLE(icode,:);
TABLE(stops)=[];
M=zeros(61);
for i=1:61
for j=i:61
if (i~=j)
if (TABLE(i)==TABLE(j)) % synony changes
M(i,j)=1;
end
end
end
end
M=M+M';
%%
% Calculate pS and pN, when omega = optimised omega
%%
pS=sum(sum(Q.*M));
pN=1-pS;
%%
% Calculate pS and pN when omega = 1
%%
md0=modelgy94(1,kappa);
Q0=composeQ(md0.R,diag(md0.freq))./61;
pS0=sum(sum(Q0.*M));
pN0=1-pS0;
%%
% Calculates dS and dN
%%
dS=t*pS/(pS0*3);
dN=t*pN/(pN0*3);
%%
% Outputs
%%
if (nargout>2),
dN_dS=dN./dS;
value.lnL=lnL;
value.kappa=kappa;
value.omega=omega;
Ss=pS0*3*m;
Ns=3*m-Ss;
value.S=Ss;
value.N=Ns;
if (noise),
fprintf('S=%.1f, N=%.1f\n',Ss,Ns);
end
end
disp(' ')
disp('NOTE: Here we used an equal codon frequence!')
disp('(i.e., in codeml setting: CodonFreq = 0 * 0:1/61 each, 1:F1X4, 2:F3X4, 3:codon table)')
if (kappa>990)
disp('The estimate of kappa is infinity. This can happen when you have extreme data,')
disp('with very divergent sequences, or when there is no transversion.');
end
disp(' ')
function [para,lnL]=i_optimtko(s1,s2)
global noise;
et=0.5; ek=1.5; eo=0.8; % initial values for t, kappa and omega
options = optimset('fminsearch');
if (noise)
options=optimset(options,'display','iter');
else
options=optimset(options,'display','off');
end
[para,f_opt]=fminsearch(@i_likelifuntko,[et,ek,eo],options,s1,s2);
lnL=-f_opt;
if (noise),
fprintf('lnL = %.5f\n',lnL);
fprintf('t=%.5f, kappa=%.5f, omega=%.5f\n',para(1),para(2),para(3));
end
function [lnL] = i_likelifuntko(x,s1,s2)
lnL=inf;
if (any(x<eps)||any(x>999)), return; end
t=x(1); kappa=x(2); omega=x(3);
if (t<eps||t>5), return; end
if (kappa<eps||kappa>999), return; end
if (omega<eps||omega>10), return; end
md=modelgy94(omega,kappa);
[lnL] = -1*likelidist(t,md,s1,s2);
%
% Fixed omega
%
function [para,lnL]=i_optimtk(s1,s2,omega)
global noise;
et=0.5; ek=1.5; % initial values for t and kappa
options = optimset('fminsearch');
if (noise), options=optimset(options,'display','iter'); end
[para,f_opt]=fminsearch(@i_likelifuntk,[et,ek],options,s1,s2,omega);
lnL=-f_opt;
if (noise),
disp(sprintf('lnL = %.5f',lnL))
disp(sprintf('t=%.5f, kappa=%.5f, omega(fixed)=%.5f',para(1),para(2),omega))
end
function [lnL] = i_likelifuntk(x,s1,s2,omega)
lnL=inf;
if (any(x<eps)||any(x>999)), return; end
t=x(1); kappa=x(2);
if (t<eps||t>5), return; end
if (kappa<eps||kappa>999), return; end
md=modelgy94(omega,kappa);
[lnL] = -1*likelidist(t,md,s1,s2);
%%
% Fixed omega and kappa
%%
function [para,lnL]=i_optimt(s1,s2,kappa,omega)
global noise;
et=0.5; % initial values for t
options = optimset('fminsearch');
if (noise), options=optimset(options,'display','iter'); end
md=modelgy94(omega,kappa);
[para,f_opt]=fminsearch(@i_likelifunt,[et],options,s1,s2,md);
lnL=-f_opt;
if (noise),
disp(sprintf('lnL = %.5f',lnL))
disp(sprintf('t=%.5f, kappa(fixed)=%.5f, omega(fixed)=%.5f',para(1),kappa,omega))
end
function [lnL] = i_likelifunt(x,s1,s2,md)
lnL=inf;
t=x(1);
if (t<eps||t>5), return; end
[lnL] = -1*likelidist(t,md,s1,s2);