-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_maf.cpp
191 lines (188 loc) · 8.98 KB
/
process_maf.cpp
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
//
// Created by 박석환 on 2021/08/15.
//
#include "process_maf.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include "codon_frequency.cpp"
#define nuc2int(x) (x & 14u)>>1u
std::vector<std::vector<aligned_codon>> make_msa_to_aligned_coding_codon(const char *file_path_maf, std::vector<CDS_info> coding_regions, int species_num) {
std::vector<char> positive;
positive.reserve(25000000);
std::vector<char> negative;
negative.reserve(25000000);
std::vector<std::vector<aligned_codon>> coding_codon_set;
std::vector<CDS_info> coding_regions_chrom;
std::vector<aligned_codon> codon_set;
aligned_codon temp;
for (int i = 0; i < species_num; i++) {
codon_set.emplace_back(temp);
}
std::ifstream maf;
maf.open(file_path_maf);
std::string line;
std::vector<std::string> block;
int start;
int len;
std::string chrom;
std::string chrom_old = "nothing";
size_t pos_start;
size_t pos_middle;
size_t pos_end;
size_t pos_dot;
getline(maf, line);
std::string codon;
bool intact_codon;
while (!line.empty()) {
if (line[0] == 'a') {
block.clear();
getline(maf, line);
pos_start = line.find('\t');
pos_dot = line.find('.');
codon_set[0].species = line.substr(pos_start + 1, pos_dot - pos_start - 1);
pos_start = line.find('\t', pos_start + 1);
pos_middle = line.find('\t', pos_start + 1);
pos_end = line.find('\t', pos_middle + 1);
pos_dot = line.find_last_of('.', pos_start);
start = std::stoi(line.substr(pos_start + 1, pos_middle - pos_start - 1));
len = std::stoi(line.substr(pos_middle + 1, pos_end - pos_middle - 1));
chrom = line.substr(pos_dot + 1, pos_start - pos_dot - 1);
pos_start = line.find_last_of('\t');
line.erase(0, pos_start + 1);
if (line.size() < 3) {
continue;
}
block.emplace_back(line);
getline(maf, line);
if (chrom != chrom_old) {
coding_regions_chrom.clear();
std::fill(positive.begin(), positive.end(), 0);
std::fill(negative.begin(), negative.end(), 0);
for (size_t num = 0; num < coding_regions.size(); num++) {
if (coding_regions[num].chrom == chrom) {
coding_regions_chrom.emplace_back(coding_regions[num]);
}
}
for (size_t num = 0; num < coding_regions_chrom.size(); num++) {
if (coding_regions_chrom[num].strand == "+") {
for (int start = coding_regions_chrom[num].start + coding_regions_chrom[num].additional; start < coding_regions_chrom[num].end + 1; start++) {
positive[start] = 1;
}
} else if (coding_regions_chrom[num].strand == "-") {
for (int start = coding_regions_chrom[num].start + coding_regions_chrom[num].additional; start < coding_regions_chrom[num].end + 1; start++) {
negative[start] = 1;
}
} else {
throw ("strand not being positive or negative");
}
}
chrom_old = chrom;
}
size_t it = 1;
while (line[0] == 's') {
pos_start = line.find('\t');
pos_dot = line.find('.');
codon_set[it].species = line.substr(pos_start + 1, pos_dot - pos_start - 1);
pos_start = line.find_last_of('\t');
line.erase(0, pos_start + 1);
block.emplace_back(line);
getline(maf, line);
it++;
}
for (size_t num = 0; num < len - 2; num++) {
if (positive[start + num] == 0) {
continue;
} else if (positive[start + num] == 1) {
if (positive[start + num + 1] == 1 && positive[start + num + 2] == 1) {
intact_codon = true;
codon = block[0].substr(num, 3);
std::transform(codon.begin(), codon.end(), codon.begin(), ::toupper);
int coordinate[3] = {0, 0, 0};
for (short int base = 0; base < 3; base++) {
if (codon[base] != 'T' && codon[base] != 'A' && codon[base] != 'C' && codon[base] != 'G'){
num += 2;
intact_codon = false;
break;
} else {
coordinate[base] = nuc2int(codon[base]);
}
}
if (intact_codon == true) {
codon_set[0].codon = codonTable[coordinate[0]][coordinate[1]][coordinate[2]];
for (size_t block_it = 1; block_it < species_num && intact_codon == true; block_it++) {
codon = block[block_it].substr(num, 3);
std::transform(codon.begin(), codon.end(), codon.begin(), ::toupper);
for (short int base = 0; base < 3; base++) {
if (codon[base] != 'T' && codon[base] != 'A' && codon[base] != 'C' && codon[base] != 'G') {
num += 2;
intact_codon = false;
break;
} else {
coordinate[base] = nuc2int(codon[base]);
}
}
codon_set[block_it].codon = codonTable[coordinate[0]][coordinate[1]][coordinate[2]];
}
if (intact_codon == true) {
coding_codon_set.emplace_back(codon_set);
num += 2;
}
}
}
} else {
throw ("not being bool in vector of bool");
}
}
for (size_t num = 0; num < len - 2; num++) {
if (negative[start + num] == 0) {
continue;
} else if (negative[start + num] == 1) {
if (negative[start + num + 1] == 1 && negative[start + num + 2] == 1) {
intact_codon = true;
codon = block[0].substr(num, 3);
std::transform(codon.begin(), codon.end(), codon.begin(), ::toupper);
return_complement_codon(codon);
int coordinate[3] = {0, 0, 0};
for (short int base = 0; base < 3; base++) {
if (codon[base] != 'T' && codon[base] != 'A' && codon[base] != 'C' && codon[base] != 'G'){
num += 2;
intact_codon = false;
break;
} else {
coordinate[base] = nuc2int(codon[base]);
}
}
if (intact_codon == true) {
codon_set[0].codon = codonTable[coordinate[0]][coordinate[1]][coordinate[2]];
for (size_t block_it = 1; block_it < species_num && intact_codon == true; block_it++) {
codon = block[block_it].substr(num, 3);
std::transform(codon.begin(), codon.end(), codon.begin(), ::toupper);
return_complement_codon(codon);
for (short int base = 0; base < 3; base++) {
if (codon[base] != 'T' && codon[base] != 'A' && codon[base] != 'C' && codon[base] != 'G') {
num += 2;
intact_codon = false;
break;
} else {
coordinate[base] = nuc2int(codon[base]);
}
}
codon_set[block_it].codon = codonTable[coordinate[0]][coordinate[1]][coordinate[2]];
}
if (intact_codon == true) {
coding_codon_set.emplace_back(codon_set);
num += 2;
}
}
}
}
}
} else {
while (line[0] != 'a') {
getline(maf, line);
}
}
}
return coding_codon_set;
}