-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathestimateyn00kappa.m
184 lines (148 loc) · 4.75 KB
/
estimateyn00kappa.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
function [kappa] = estimateyn00kappa(S)
%ESTIMATEYN00KAPPA - Estimates transition/transversion rate ratio (kappa) by method of YN00
%This function calculates mutational transition/transversion rate ratio
%kappa using 4-fold degenerate sites from pairwise comparisons
%under HKY85, weighting estimates by the numbers of sites
%
%REF: Yan Z and Nielsen R (2000) Estimating synonymous and nonsynonymous
%subsititution rates under realistic evolutionary models. p. 34
%
%Yang and Nielsen (2000) use the fourfold-degenerate sites at the third
%codon positions and the nondegenerate sites to estimate
%$\kappa$. Mutations at the fourfold-degenerate sites do not
%change the amino acid, and thus the transition/transversion
%rate bias at those sites should reflect the mutational
%bias. Mutations at nondegenerate sites all lead to amino
%acid changes and can also be used to estimate
%
% Syntax: [kappa] = estimateyn00kappa(S,method)
%
% Inputs:
% S - Sequence pair
% method - k2p|f84|hky
%
% Outputs:
% kappa - $\kappa=\alpha/\beta$
%
% See also: DN_PDIST
% 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 $
[n,m]=size(S);
kappa=0;
for (i=1:n),
for (j=i:n),
if ~(i==j)
spair =S([i j],:);
% [t,k] = i_SeqPairDistanceF84(spair)
[picker0,picker2,picker4] = getDegenerateSitesIndex(spair);
seq0 = spair(:,picker0);
seq4 = spair(:,picker4);
[k0] = i_SeqPairDistanceF84(seq0);
[k4] = i_SeqPairDistanceF84(seq4);
wk0=sum(sum(picker0)); wk4=sum(sum(picker4));
k=(k0*wk0+k4*wk4)/(wk0+wk4);
kappa=kappa+k/(n*(n-1)/2);
end
end
end
function [k,t,SEt] = i_SeqPairDistanceF84(S)
% This calculates kappa and d from P (proportion of transitions) & Q
% (proportion of transversions) & pi under F84.
% When F84 fails, we try to use K80. When K80 fails, we try
% to use JC69. When JC69 fails, we set distance t to maxt.
% Variance formula under F84 is from Tateno et al. (1994), and briefly
% checked against simulated data sets.
t=99; k=999; SEt=0;
[n,m]=size(S);
[P,Q]=countseqpq(S); P=P./m; Q=Q./m;
% Qsmall=min2(1e-10,0.1/m)
N=[sum(sum(S==1)),sum(sum(S==2)),sum(sum(S==3)),sum(sum(S==4))];
Pi=N./sum(N); % Pi(A,C,G,T);
tc=Pi(4)*Pi(2);
ag=Pi(1)*Pi(3);
% Y=p[0]+p[1]; R=p[2]+p[3]; tc=p[0]*p[1]; ag=p[2]*p[3];
Y=Pi(1)+Pi(3); % Y=TC
R=Pi(2)+Pi(4); % R=AG
A=tc/Y+ag/R; B=tc+ag; C=Y*R;
a=(2*B+2*(tc*R/Y+ag*Y/R)*(1-Q/(2*C)) - P) / (2*A);
b=1-Q/(2*C);
if (a<=0 || b<=0)
failF84=1;
return;
end
a=-.5*log(a); b=-.5*log(b);
if(b<=0)
failF84=1;
return;
end
k_F84=a/b-1;
t = 4*b*(tc*(1+ k_F84/Y)+ag*(1+ k_F84/R)+C);
k_HKY = (B + (tc/Y+ag/R)* k_F84)/B; % k_F84=>k_HKY85
if (k_HKY>999)
k_HKY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 0; 0 0 2;
2 0 2; 0 0 2; 2 0 2; 0 0 2; 0 0 0];
for (p=1:n),
for (q=1:m/3)
deg = degtable(S2(p,q),:);
a=q;
q=(q-1)*3+1; % jump index p every next three bases.
marker(p,q)=deg(1,1); marker(p,q+1)=deg(1,2); marker(p,q+2)=deg(1,3);
end
end
validflag=zeros(1,m);
k=1:m;
validflag(1,k)=min(marker(:,k))==max(marker(:,k)); % valid columns's flag, 0 or 1
pick0=marker==0;
pick2=marker==2;
pick4=marker==4;
picker0=pick0(1,:)&validflag;
picker2=pick2(1,:)&validflag;
picker4=pick4(1,:)&validflag;
%function [P,Q]=countseqpq(S)
%
%[n,m] = size(S);
%if (n~=2) error('Not a sequence pair'); end
%
%TS = [0, 0, 1, 0;
% 0, 0, 0, 1;
% 1, 0, 0, 0;
% 0, 1, 0, 0];
%
%TV = [0, 1, 0, 1;
% 1, 0, 1, 0;
% 0, 1, 0, 1;
% 1, 0, 1, 0];
%
%P = zeros(n);
%Q = zeros(n);
% [S,gap] = countntchange(S(1,:), S(2,:));
% P = sum(sum(TS.*S));
% Q = sum(sum(TV.*S));