-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalignP_upgma.c
290 lines (252 loc) · 5.83 KB
/
kalignP_upgma.c
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
/*
kalignP_upgma.c
Copyright (C) 2010 Nanjiang Shu <[email protected]>
Department of Biochemistry and Biophysics
Stockholm University
Description: multiple sequence alignment supporting external supplied
position specific gap penalties
This software is derived from
Kalign version 2.03, Copyright (C) 2006 Timo Lassmann
http://msa.cgb.ki.se/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A copy of this license is in the COPYING file.
*/
#include "kalignP.h"
struct aln_tree_node* real_upgma(float **dm)
{
int i,j;
int *as = 0;
float max;
int node_a = 0;
int node_b = 0;
int cnode = numseq;
struct aln_tree_node** tree = 0;
struct aln_tree_node* tmp = 0;
as = malloc(sizeof(int)*numseq);
for (i = numseq; i--;){
as[i] = i+1;
}
tree = malloc(sizeof(struct aln_tree_node*)*numseq);
for (i=0;i < numseq;i++){
tree[i] = malloc(sizeof(struct aln_tree_node));
tree[i]->done = 1;
tree[i]->num = i;
tree[i]->path = 0;
tree[i]->profile = 0;
tree[i]->seq = 0;//seq[i];
tree[i]->len = 0;//len[i];
/*
Needs to be +2 because:
at n = 3 is is possible to get a perfectly balanced binary tree with 4 sequences at intermediate nodes
*/
tree[i]->links = malloc(sizeof(struct aln_tree_node*)*3);
for ( j =0;j < 3;j++){
tree[i]->links[j] = 0;
}
}
while (cnode != numprofiles){
max = -INFTY;
for (i = 0;i < numseq-1; i++){
if (as[i]){
for ( j = i + 1;j < numseq;j++){
if (as[j]){
if (dm[i][j] > max){
max = dm[i][j];
node_a = i;
node_b = j;
}
}
}
}
}
tmp = malloc(sizeof(struct aln_tree_node));
tmp->done = 0;
tmp->path = 0;
tmp->profile = 0;
tmp->num = cnode;
tmp->seq = 0;
tmp->len = 0;
tmp->links = malloc(sizeof(struct aln_tree_node*)*(3));
tmp->links[0] = tree[node_a];
tmp->links[1] = tree[node_b];
tmp->links[2] =0;
tree[node_a] = tmp;
tree[node_b] = 0;
/*deactivate sequences to be joined*/
as[node_a] = cnode+1;
as[node_b] = 0;
cnode++;
/*calculate new distances*/
for (j = numseq;j--;){
if (j != node_b){
dm[node_a][j] = (dm[node_a][j] + dm[node_b][j])/2;
}
}
dm[node_a][node_a] = 0.0f;
for (j = numseq;j--;){
dm[j][node_a] = dm[node_a][j];
dm[j][node_b] = 0.0f;
dm[node_b][j] = 0.0f;
}
}
tmp = tree[node_a];
for (i = numprofiles;i--;){
free(dm[i]);
}
free(dm);
free(tree);
free(as);
return tmp;
}
int* nj(float **dm,int* tree)
{
int i,j;
//float **dm = 0;
float *r = 0;
float *r_div = 0;
int *active = 0;
int node = 0;
float min = 0;
int join_a = 0;
int join_b = 0;
int leaves = 0;
int c =0;
leaves = numseq;
r = malloc ((numseq*2-1) *sizeof(float));
r_div = malloc ((numseq*2-1) *sizeof(float));
active = malloc((numseq*2-1)*sizeof(int));
for ( i = 0;i < numseq*2-1;i++){
active[i] = 0;
}
for ( i = 0;i < numseq;i++){
active[i] = 1;
}
node = numseq;
while (node != numseq*2 -1){
for (i = 0;i<numseq*2-1;i++){
if (active[i]){
r[i] = 0;
for (j = 0;j < numseq*2-1;j++){
if (active[j]){
r[i] += (i<j) ?dm[i][j]:dm[j][i];
}
}
r_div[i] = r[i] / (leaves-2);
}
}
for ( j = 0;j < numseq*2-1;j++){
if (active[j]){
for ( i = j+1;i < numseq*2-1;i++){
if (active[i]){
dm[i][j] = dm[j][i] - (r[i] + r[j])/2;
}
}
}
}
min = -INFTY;
for ( j = 0;j < numseq*2-1;j++){
if (active[j]){
for ( i = j+1;i < numseq*2-1;i++){
if (active[i]){
if (dm[i][j] > min){
min = dm[i][j];
join_a = j;
join_b = i;
}
}
}
}
}
//join_a always smaller than join_b && both smaller than node
dm[join_a][node] = dm[join_a][join_b]/2 + (r_div[join_a] - r_div[join_b])/2;
dm[join_b][node] = dm[join_a][join_b] - dm[join_a][node];
active[join_a] = 0;
active[join_b] = 0;
tree[c] = join_a;
tree[c+1] = join_b;
tree[c+2] = node;
for (i = 0;i<numseq*2-1;i++){
if (active[i]){
dm[i][node] = (i>join_a) ? dm[join_a][i]: dm[i][join_a];
dm[i][node] -= dm[join_a][node];
dm[i][node] += (i > join_b) ? dm[join_b][i] : dm[i][join_b] ;
dm[i][node] -= dm[join_b][node];
dm[i][node] /= 2;
}
}
active[node] = 1;
c += 3;
node++;
}
for (i = numprofiles;i--;){
free(dm[i]);
}
free(dm);
free(r);
free(r_div);
free(active);
return tree;
}
int* upgma(float **dm,int* tree)
{
int i,j,t;
int *as = 0;
float max;
int node_a = 0;
int node_b = 0;
int cnode = numseq;
as = malloc(sizeof(int)*numseq);
for (i = numseq; i--;){
as[i] = i+1;
}
t = 0;
while (cnode != numprofiles){
max = -INFTY;
for (i = 0;i < numseq-1; i++){
if (as[i]){
for ( j = i + 1;j < numseq;j++){
if (as[j]){
if (dm[i][j] > max){
max = dm[i][j];
node_a = i;
node_b = j;
}
}
}
}
}
tree[t] = as[node_a]-1;
tree[t+1] = as[node_b]-1;
tree[t+2] = cnode;
t += 3;
/*deactivate sequences to be joined*/
as[node_a] = cnode+1;
as[node_b] = 0;
cnode++;
/*calculate new distances*/
for (j = numseq;j--;){
if (j != node_b){
dm[node_a][j] = (dm[node_a][j] + dm[node_b][j])/2;
}
}
dm[node_a][node_a] = 0.0f;
for (j = numseq;j--;){
dm[j][node_a] = dm[node_a][j];
dm[j][node_b] = 0.0f;
dm[node_b][j] = 0.0f;
}
}
free(as);
return tree;
}