-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnjtree.cpp
More file actions
255 lines (242 loc) · 9.25 KB
/
njtree.cpp
File metadata and controls
255 lines (242 loc) · 9.25 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <assert.h>
uint32_t finding_index(std::vector<std::string> &name,std::string chrom) {
int pos_chrom = 0;
for (size_t pos = 0; pos < name.size(); pos++) {
if (name[pos] == chrom) {
break;
}
else {
pos_chrom++;
}
}
return pos_chrom;
}
struct maf_info {
std::string name;
std::string seq;
uint32_t length;
maf_info (std::string name, std::string seq, uint32_t length) :
name(name),
seq(seq),
length(length) {}
};
struct distance_matrix_grid {
uint32_t row;
uint32_t col;
distance_matrix_grid (uint32_t row, uint32_t col) :
row(row),
col(col) {}
};
void read_maf(std::string delimiter, std::string line, std::vector<maf_info> &block) {
size_t pos;
int queue;
std::string name;
std::string seq;
uint32_t length;
while (queue < 7) {
pos = line.find(delimiter);
if (queue == 1) {
name = line.substr(0, line.find("."));
} else if (queue == 6) {
seq = line.substr(0, pos);
length = seq.size();
}
queue++;
line.erase(0, pos + delimiter.length());
}
block.emplace_back(name, seq, length);
}
float ** from_maf_to_distance_matrix (std::vector<std::string> &names, const char *file_path, int species_num) {
float **distance_matrix = new float * [species_num];
for (size_t i = 0; i < species_num; i++) {
distance_matrix[i] = new float [species_num];
}
for (size_t row = 0; row < species_num; row++) {
for (size_t col = 0; col < species_num; col++) {
distance_matrix[row][col] = 0;
}
}
std::ifstream maf;
maf.open(file_path);
std::string line;
std::vector<maf_info> block;
std::vector<std::string> spe_names;
std::string delimiter = "\t";
getline(maf, line);
if (line[0] == '#') {
while (line[0] == '#') {
getline(maf, line);
}
}
while (!(line.empty())) {
if (line[0] == '#') {
while (line[0] == '#') {
getline(maf, line);
}
}
if (line[0] == 'a') {
block.clear();
getline(maf, line);
while (line[0] == 's') {
read_maf(delimiter, line, block);
getline(maf, line);
}
if (names.size() != species_num) {
for (size_t i = 0; i < block.size() ; i++) {
if (std::find(names.begin(), names.end(), block[i].name) == names.end()) {
names.emplace_back(block[i].name);
}
}
}
for (size_t row = 0; row < block.size(); row++) {
for (size_t col = 0; col < block.size(); col++) {
if (row < col) {
assert(block[row].length == block[col].length);
for (size_t pos = 0; pos < block[row].length; pos++) {
if (block[row].seq[pos] != block[col].seq[pos] && block[row].seq[pos] != 'N' && block[row].seq[pos] != 'n'
&& block[row].seq[pos] != '-' && block[col].seq[pos] != 'N' && block[col].seq[pos] != 'n' && block[col].seq[pos] != '-') {
++distance_matrix[finding_index(names, block[row].name)][finding_index(names, block[col].name)];
}
}
}
}
}
} else {
getline(maf, line);
}
}
// make distance matrix neat
for (size_t row = 0; row < species_num; row++) {
for (size_t col = 0; col < species_num; col++) {
if (row < col) {
distance_matrix[col][row] += distance_matrix[row][col];
distance_matrix[row][col] = 0;
} else if (row == col) {
distance_matrix[row][col] = 0;
}
}
}
return distance_matrix;
}
float *compute_divergence(float ** distance_matrix, std::vector<std::string> &names) {
float divergence[names.size()];
for (size_t num = 0; num < names.size(); num++) {
divergence[num] = 0;
}
for (size_t row = 0; row < names.size(); row++) {
for (size_t col = 0; col < names.size(); col++) {
divergence[row] += distance_matrix[row][col];
divergence[col] += distance_matrix[row][col];
}
}
return divergence;
}
distance_matrix_grid make_new_distance_matrix_and_get_minimum(float **distance_matrix, float *divergence, std::vector<std::string> &names) {
float **new_distance_matrix = new float * [names.size()];
for (size_t i = 0; i < names.size(); i++) {
new_distance_matrix[i] = new float [names.size()];
}
for (size_t row = 0; row < names.size(); row++) {
for (size_t col = 0; col < names.size(); col++) {
new_distance_matrix[row][col] = 0;
}
}
for (size_t row = 0; row < names.size(); row++) {
for (size_t col = 0; col < names.size(); col++) {
if (row > col) {
new_distance_matrix[row][col] = distance_matrix[row][col] - (divergence[row] + divergence[col]) / (names.size() - 2);
}
}
}
distance_matrix_grid grid = {1, 0};
for (uint32_t row = 0; row < names.size(); row++) {
for (uint32_t col = 0; col < names.size(); col++) {
if (row > col && new_distance_matrix[grid.row][grid.col] > new_distance_matrix[row][col]) {
grid = {row, col};
}
}
}
for (size_t i = 0; i < names.size(); i++) {
delete [] new_distance_matrix[i];
}
delete [] new_distance_matrix;
return grid;
}
float **distance_matrix_for_next_iteration(float **distance_matrix, float *divergence, distance_matrix_grid grid, std::vector<std::string> &names) {
// get branch length for minimum grid
float branch_length_row = distance_matrix[grid.row][grid.col] / 2 + (divergence[grid.row] - divergence[grid.col]) / (2 * (names.size() - 2));
float branch_length_col = distance_matrix[grid.row][grid.col] - branch_length_row;
std::string new_name = "(" + names[grid.row] + ":" + std::to_string(branch_length_row) + "," + names[grid.col] + ":" + std::to_string(branch_length_col) + ")";
names.erase(names.begin() + grid.row);
names.erase(names.begin() + grid.col);
names.emplace_back(new_name);
float **new_distance_matrix = new float * [names.size()];
for (size_t i = 0; i < names.size(); i++) {
new_distance_matrix[i] = new float [names.size()];
}
uint32_t row_pre = 0;
uint32_t col_pre = 0;
for (size_t row = 0; row < names.size() - 1; row++) {
if (row_pre == grid.row || row_pre == grid.col) {
row_pre++;
if (row_pre == grid.row || row_pre == grid.col) {
row_pre++;
}
}
for (size_t col = 0; col < names.size(); col++) {
if (col_pre == grid.col || col_pre == grid.row) {
col_pre++;
if (col_pre == grid.col || col_pre == grid.row) {
col_pre++;
}
}
if (row <= col) {
new_distance_matrix[row][col] = 0;
col_pre++;
if (col_pre >= names.size() + 1) {
col_pre = 0;
}
} else {
new_distance_matrix[row][col] = distance_matrix[row_pre][col_pre];
col_pre++;
}
}
row_pre++;
}
col_pre = 0;
for (size_t last_line; last_line < names.size() - 1; last_line++) {
if (col_pre == grid.col || col_pre == grid.row) {
col_pre++;
}
new_distance_matrix[names.size() - 1][last_line] = distance_matrix[std::max(col_pre, grid.col)][std::min(col_pre, grid.col)] + distance_matrix[std::max(col_pre, grid.row)][std::min(col_pre, grid.row)]
- distance_matrix[grid.row][grid.col] / 2;
col_pre++;
}
new_distance_matrix[names.size() - 1][names.size() - 1] = 0;
for (size_t i = 0; i < names.size(); i++) {
delete [] distance_matrix[i];
}
delete [] distance_matrix;
return new_distance_matrix;
}
void final_iteration(float **distance_matrix, std::vector<std::string> &names) {
std::string new_name = "(" + names[0] + ":" + std::to_string(distance_matrix[1][0]) + " center," + names[1] + ")";
names.clear();
names.emplace_back(new_name);
}
void get_nj_tree(std::vector<std::string> &names, const char *file_path, int species_num) {
float **distance_matrix = from_maf_to_distance_matrix(names, file_path, species_num);
float *divergence = compute_divergence(distance_matrix, names);
distance_matrix_grid grid = make_new_distance_matrix_and_get_minimum(distance_matrix, divergence, names);
float **next_matrix = distance_matrix_for_next_iteration(distance_matrix, divergence, grid, names);
while (names.size() != 2) {
divergence = compute_divergence(next_matrix, names);
grid = make_new_distance_matrix_and_get_minimum(next_matrix, divergence, names);
next_matrix = distance_matrix_for_next_iteration(next_matrix, divergence, grid, names);
}
final_iteration(next_matrix, names);
}