forked from sheneman/evalyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnjhooks.c
executable file
·333 lines (246 loc) · 7.52 KB
/
rnjhooks.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* rnjhooks.c
*
* $Id: rnjhooks.c,v 1.1.1.1 2006/05/09 21:00:46 cvsuser Exp $
*
*
* Copyright (c) 2005, Luke Sheneman. All rights reserved.
*
*
* Routines for integrating
*
* Luke Sheneman
* sheneman@cs.uidaho.edu
*
*/
#include <stdio.h>
#include <string.h>
#include <align.h>
#include <nj_fasta.h>
#include <nj_dist.h>
#include "ltree.h"
#include "rnjhooks.h"
/*
* LTREE_create_rnj_tree()
*
* Construct a Relaxed Neighbor Joining tree
* and stick it into the population at some
* randomly chosen point
*
*/
LTREE_tree *
LTREE_create_rnj_tree(ALIGN_scoring_system *ss,
LTREE_population *pop,
ALIGN_seqset *seqset) {
NJ_DMAT *dmat, *pw_dmat;
NJ_ARGS rnj_args;
NJ_TREE *rnj_tree;
ALIGN_alignment *a, *b, *ltree_alignment;
NJ_alignment *rnj_alignment;
LTREE_tree *ltree;
int i, j;
printf("In LTREE_create_rnj_tree() - \n");
/*************************************************************
*
* ALLOCATE AND INITIALIZE DISTANCE MATRIX
*
*************************************************************/
dmat = (NJ_DMAT *)calloc(1, sizeof(NJ_DMAT));
dmat->ntaxa = seqset->num;
dmat->size = dmat->ntaxa;
dmat->taxaname = (char **)calloc(dmat->ntaxa, sizeof(char *));
/* copy taxanames from seqset into dmat */
for(i=0;i<dmat->ntaxa;i++) {
dmat->taxaname[i] = (char *)calloc(strlen(seqset->seq[i].title)+1, sizeof(char));
strncpy(dmat->taxaname[i], seqset->seq[i].title, strlen(seqset->seq[i].title)+1);
}
/* allocate the floats in the matrix here */
dmat->val = (float *)calloc(NJ_NCELLS(dmat->ntaxa), sizeof(float));
dmat->valhandle = dmat->val;
dmat->rhandle = dmat->r = (float *)calloc(dmat->ntaxa, sizeof(float));
dmat->r2handle = dmat->r2 = (float *)calloc(dmat->ntaxa, sizeof(float));
dmat->maxulps = 4*(dmat->ntaxa+4) + 100;
/*************************************************************
*
* END OF ALLOCATE AND INITIALIZE OF DISTANCE MATRIX
*
*************************************************************/
/* initialize rnj_args, basically just to choose correction model */
rnj_args.kimura_flag = 1;
rnj_args.jukes_flag = 0;
rnj_args.correction_model = NJ_MODEL_KIMURA;
/* lame hack, but this will have to do for now */
if(seqset->symtab->nsyms < 20) {
rnj_args.dna_flag = 1;
rnj_args.protein_flag = 0;
} else {
rnj_args.dna_flag = 0;
rnj_args.protein_flag = 1;
}
/*
* Construct a distance matrix for the input sequences
* by pairwise aligning each pair of sequences in the
* seqset in the context of the specified scoring system.
*
* For now, use k2p distance correction.
*
*/
a = b = ltree_alignment = NULL;
for(i=0;i<seqset->num;i++) {
/* construct an alignment for a, which is sequence i in the seqset */
if(a) {
ALIGN_free_alignment(a);
}
a =
ALIGN_init_alignment(seqset->seq[i].data,
seqset->symtab,
seqset->seq[i].length,
i);
for(j=i+1;j<seqset->num;j++) {
/* construct an alignment for b, which is sequence j in the seqset */
if(b) {
ALIGN_free_alignment(b);
}
b =
ALIGN_init_alignment(seqset->seq[j].data,
seqset->symtab,
seqset->seq[j].length,
j);
/* we have both one-sequence alignments, so now do pairwise alignment */
if(ltree_alignment) { free(ltree_alignment); }
ltree_alignment = ALIGN_alignment_alignment(ss, a, b);
/* convert alignment formats */
rnj_alignment = LTREE_ev_alignment_to_rnj(seqset, ltree_alignment);
/* compute the pairwise distances given a pairwise alignment */
pw_dmat = NJ_compute_dmat(&rnj_args, rnj_alignment);
NJ_free_alignment(rnj_alignment);
/* fill cells in "big" distance matrix (dmat) */
dmat->val[NJ_MAP(i, j, dmat->ntaxa)] = pw_dmat->val[NJ_MAP(0, 1, 2)];
NJ_free_dmat(pw_dmat);
}
}
/* build a RNJ tree based on the distance matrix */
rnj_tree = NJ_relaxed_nj(&rnj_args, dmat);
// rnj_tree = NJ_neighbor_joining(&rnj_args, dmat);
NJ_output_tree(&rnj_args,
rnj_tree,
dmat,
1);
/* convert the RNJ tree to an LTREE */
ltree =
LTREE_rnjtree_to_ltree(rnj_tree,
dmat,
seqset);
LTREE_print_tree(ltree);
printf(" ;\n\n");
/* free everything */
NJ_free_dmat(dmat); /* free the distance matrix */
ALIGN_free_alignment(a); /* free evalyn alignment a */
ALIGN_free_alignment(b); /* free evalyn alignment b */
return(ltree);
}
/*
* LTREE_rnjtree_to_ltree() -
*
* Converts a tree data structure used by Clearcut (RNJ Implementation)
* into a usable tree datastructure usable by Evalyn.
*
*/
LTREE_tree *
LTREE_rnjtree_to_ltree(NJ_TREE *rnj_tree,
NJ_DMAT *dmat,
ALIGN_seqset *seqset) {
LTREE_tree *tree;
tree = (LTREE_tree *)calloc(1, sizeof(LTREE_tree));
tree->root =
LTREE_recurse_rnjtree_to_ltree(rnj_tree,
dmat,
seqset);
tree->seqset = seqset;
return(tree);
}
/*
* LTREE_recurse_rnjtree_to_ltree() -
*
* Converts a tree data structure used by Clearcut (RNJ Implementation)
* into a usable tree datastructure usable by Evalyn.
*
*/
LTREE_treenode *
LTREE_recurse_rnjtree_to_ltree(NJ_TREE *rnj_tree,
NJ_DMAT *dmat,
ALIGN_seqset *seqset) {
LTREE_treenode *ev_root;
if(!rnj_tree) {
printf("RETURNING NULL IN LTREE_rnjtree_to_ltree()\n");
return(NULL);
}
if(rnj_tree) {
/* allocate the ev_root */
ev_root =
(LTREE_treenode *)calloc(1, sizeof(LTREE_treenode));
if(rnj_tree->taxa_index == NJ_INTERNAL_NODE) {
ev_root->seq_id = -1;
} else {
ev_root->seq_id = rnj_tree->taxa_index;
}
}
/* recurse left */
if(rnj_tree->left) {
ev_root->left =
LTREE_recurse_rnjtree_to_ltree(rnj_tree->left,
dmat,
seqset);
} else {
ev_root->left = NULL;
}
/* recurse right */
if(rnj_tree->right) {
ev_root->right =
LTREE_recurse_rnjtree_to_ltree(rnj_tree->right,
dmat,
seqset);
} else {
ev_root->right = NULL;
}
return(ev_root);
}
/*
* LTREE_ev_alignment_to_rnj() -
*
* Convert an Evalyn alignment to a clearcut alignment
*
*/
NJ_alignment *
LTREE_ev_alignment_to_rnj(ALIGN_seqset *seqset,
ALIGN_alignment *ev_alignment) {
NJ_alignment *rnj_alignment;
ALIGN_symtab *symtab;
int i, j;
char c;
symtab = seqset->symtab;
/* allocate our clearcut alignment structure */
rnj_alignment = (NJ_alignment *)calloc(1, sizeof(NJ_alignment));
rnj_alignment->titles = (char **)calloc(ev_alignment->k, sizeof(char *));
rnj_alignment->data = (char *)calloc(ev_alignment->k * (ev_alignment->n+1), sizeof(char));
/* set our dimensions */
rnj_alignment->nseq = ev_alignment->k;
rnj_alignment->length = ev_alignment->n;
for(i=0;i<ev_alignment->k;i++) {
/* allocate and copy titles */
rnj_alignment->titles[i] = (char *)calloc(strlen(seqset->seq[i].title), sizeof(char));
strncpy(rnj_alignment->titles[i], seqset->seq[i].title, strlen(seqset->seq[i].title));
/* copy alignment text */
for(j=0;j<ev_alignment->n;j++) {
c =
symtab->syms[(int)(ev_alignment->text[ALIGN_index(ev_alignment, j, i)])];
if( ( (c >= 'A') && (c <= 'Z') ) ||
( (c >= 'a') && (c <= 'z') ) ) {
rnj_alignment->data[i*rnj_alignment->length+j] = c;
} else {
rnj_alignment->data[i*rnj_alignment->length+j] = NJ_AMBIGUITY_CHAR;
}
}
}
return(rnj_alignment);
}