-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_median_ali_dp_fine_half.m
311 lines (267 loc) · 7.98 KB
/
calc_median_ali_dp_fine_half.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
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
%given samples, return centroid alignment
% - cen_seq: centroid alignment with aligned bin index info
% - cen_ali: centroid alignment with aligned age info
MarginalPairCount = zeros(size(sample_ali,1),length(discrete_target)+2);
for s = 1:size(sample_seq,2)
vv=find(sample_seq(:,s)~=0);
if vv(1)~=1
for i=1:vv(1)-1
MarginalPairCount(i,length(discrete_target)+1)= MarginalPairCount(i...
,length(discrete_target)+1)+1;
end
end
if vv(end)~=size(sample_seq,1)
for i=vv(end)+1:size(sample_seq,1)
MarginalPairCount(i,length(discrete_target)+2)=MarginalPairCount(i,...
length(discrete_target)+2)+1;
end
end
for t1=vv(1):vv(end)
MarginalPairCount(t1,sample_seq(t1,s))=MarginalPairCount(t1,sample_seq(t1,s))+1;
end
end
ProbMarginalPair =zeros(size(sample_ali,1),length(discrete_target)+2);
for i=1:size(ProbMarginalPair,1)
if sum(MarginalPairCount(i,:))==0
ProbMarginalPair(i,:)=0;
else
ProbMarginalPair(i,:) = MarginalPairCount(i,:)/sum(MarginalPairCount(i,:));
end
end
ExpAbsDiff = zeros(size(sample_ali,1),length(discrete_target)+2);
for i=1:size(sample_ali,1)
for j=1:length(discrete_target)+2
if j<=length(discrete_target)
s=0;
for tt=1:length(discrete_target)
s=s+abs(tt-j)*ProbMarginalPair(i,tt);
end
ExpAbsDiff(i,j)=s;
elseif j==length(discrete_target)
s=0;
for tt=1:length(discrete_target)
s=s+abs(tt-0)*ProbMarginalPair(i,tt);
end
ExpAbsDiff(i,j)=s;
else
s=0;
for tt=1:length(discrete_target)
s=s+abs(tt-length(discrete_target))*ProbMarginalPair(i,tt);
end
ExpAbsDiff(i,j)=s;
end
end
end
ExpAbsDiff=-ExpAbsDiff;
multip=4;
VV = zeros(size(sample_seq,1),length(discrete_target),4*max(dis_bin_input_new));
for i=2:size(VV,1)
for j=1:size(VV,2)
if j<=size(VV,3)
for k=1:j
rr = (dis_bin_input_new(i)-1)/max(k-1,0.5);
if rr<0.25 || rr>4
VV(i,j,k)=-inf;
end
end
for k=j+1:size(VV,3)
VV(i,j,k)=-inf;
end
else
for k=1:size(VV,3)
rr = (dis_bin_input_new(i)-1)/max(k-1,0.5);
if rr<0.25 || rr>4
VV(i,j,k)=-inf;
end
end
end
end
end
% for i=1:length(discrete_target)
% for j=1:i-1
% VV(:,j,i)=-inf;
% end
% end
% for i=2:size(sample_seq,1)
% for j = i:length(discrete_target)
% if j+(dis_bin_input_new(i))*multip+1<=length(discrete_target)
% VV(i,j+(dis_bin_input_new(i))*multip+1:end,j) = -inf;
% end
% end
% end
for i=2:size(VV,1)
% i
for j=1:size(VV,2)
if j<=size(VV,3)
for k=1:j
% rr = ratio_track_new(i,j-k+1);
rr = (dis_bin_input_new(i)-1)/max(k-1,0.5);
if rr<0.25 || rr>4
VV(i,j,k)=-inf;
end
end
for k=j+1:size(VV,3)
VV(i,j,k)=-inf;
end
else
for k=1:size(VV,3)
rr=(dis_bin_input_new(i)-1)/max(k-1,0.5);
if rr<0.25 || rr>4
VV(i,j,k)=-inf;
end
end
end
end
end
%log_v(1,1,1) = log(normpdf(input_scaled(1,2)-bin_mean(1),0,sigma));
VV(1,1,1:end) = ExpAbsDiff(1,1);
for i = 2:gap_con_tar
VV(1,i,1:end) = ExpAbsDiff(1,i);
end
for i=gap_con_tar+1:length(discrete_target)
VV(1,i,1:end)=-inf;
end
for i=2:gap_con
VV(i,1,:) = sum(ExpAbsDiff(1:i-1,length(discrete_target)+1))...
+ExpAbsDiff(i,1);
end
for i=gap_con+1:length(input_scaled_new)
VV(i,1,:)=-inf;
end
%recursion
for i=2:length(input_scaled_new)
% i
for j=1:length(discrete_target)
for j2=1:j
tmp=j-j2+1;
if tmp>0 && tmp<=size(VV,3)
if VV(i,j,tmp)==0
% if log_v(i,j,j2)==-inf
% VV(i,j,j2)=-inf;
% else
VV(i,j,tmp) =ExpAbsDiff(i,j)+max(VV(i-1,j2,:));
% end
end
end
end
end
end
gap_add_v_x = zeros(length(input_scaled_new)-1,length(discrete_target)); %f(i,N2,k)
gap_add_v_y = zeros(length(discrete_target)-1,length(discrete_target)-1); %f(N1,j,k)
gap_add_v_e = zeros(length(discrete_target),1);
for i=1:length(input_scaled_new)-1
if i<length(input_scaled_new)-gap_con
gap_add_v_x(i,:)=-inf;
else
for k=1:length(discrete_target)
if length(discrete_target)-k+1>0 && length(discrete_target)-k+1<=size(VV,3)
if VV(i,length(discrete_target),length(discrete_target)-k+1)==-inf
gap_add_v_x(i,k)=-inf;
else
gap_add_v_x(i,k) = VV(i,length(discrete_target),length(discrete_target)-k+1)+sum(ExpAbsDiff(i+1:...
length(input_scaled_new),end));
end
else
gap_add_v_x(i,k)=-inf;
end
end
end
end
for j=1:length(discrete_target)-1
if j<length(discrete_target)-gap_con_tar
gap_add_v_y(j,:)=-inf;
else
for k=1:length(discrete_target)-1
tmp=j-k+1;
if tmp>0 && tmp<=size(VV,3)
if VV(length(input_scaled_new),j,tmp)==-inf
gap_add_v_y(j,k)=-inf;
else
gap_add_v_y(j,k) = VV(length(input_scaled_new),j,tmp);
end
else
gap_add_v_y(j,k)=-inf;
end
end
end
end
for i=1:length(discrete_target)
tmp = length(discrete_target)-i+1;
if tmp>0 && tmp<=size(VV,3)
gap_add_v_e(i) = VV(length(input_scaled_new),length(discrete_target),tmp);
else
gap_add_v_e(i)=-inf;
end
end
%backtrace
ttt=-inf;
for i=1:length(input_scaled_new)-1
for j=1:length(discrete_target)
if gap_add_v_x(i,j)>ttt
ttt=gap_add_v_x(i,j);
ind_i=i;
ind_j=length(discrete_target);
ind_k=j;
end
end
end
for i=1:length(discrete_target)-1
for j=1:length(discrete_target)-1
if gap_add_v_y(i,j)>ttt
ttt=gap_add_v_y(i,j);
ind_i=length(input_scaled_new);
ind_j=i;
ind_k=j;
end
end
end
for i=1:length(discrete_target)
if gap_add_v_e(i)>ttt
ttt=gap_add_v_e(i);
ind_i=length(input_scaled_new);
ind_j=length(discrete_target);
ind_k=i;
end
end
%[ind_i ind_j ind_k] = optimal_ali_ending(log_v_gap);
median_seq = zeros(length(input_scaled_new),1);
median_seq(ind_i) = ind_j;
median_seq(ind_i-1) = ind_k;
for i = ind_i-2:-1:1
%sss = zeros(ind_k,1);
sss=zeros(ind_k+1-max(1,ind_k+1-size(VV,3)),1);
for j = max(1,ind_k+1-size(VV,3)):ind_k
% nearest_index = ratio_track(dis_bin_input(i+2),cen_seq(i+2)-ind_k+1);
% if log_v(i+1,ind_k,j)==-inf
% sss(j)=inf;
% else
sss(j-max(1,ind_k+1-size(VV,3))+1)=abs(ExpAbsDiff(i+2,median_seq(i+2))+VV(i+1,ind_k,ind_k-j+1)-...
VV(i+2,median_seq(i+2),median_seq(i+2)-ind_k+1));
%end
end
%sss(ind_k+1)=abs(ProbMarginalPair(i+2,cen_seq(i+2))+...
% +ProbMarginalPair(i+1,length(discrete_target)+1)-VV(i+2,cen_seq(i+2),ind_k));
[aa bb]= min(sss);
if bb==ind_k+1
median_seq(i)=0;
break
else
median_seq(i)=bb+max(1,ind_k+1-size(VV,3))-1;
ind_k=bb+max(1,ind_k+1-size(VV,3))-1;
if bb+max(1,ind_k+1-size(VV,3))-1==1
break;
end
end
end
median_ali=zeros(length(input_scaled_new),1);
for i=1:length(input_scaled_new)
if median_seq(i)~=0
median_ali(i) = discrete_target(median_seq(i));
end
end
aa_end = max(find(median_ali~=0));
if aa_end~=length(median_ali)
for i=aa_end+1:length(median_ali)
median_ali(i)=median_ali(aa_end);
end
end