Repository navigation
Expand file tree
/
Copy pathMutation.cpp
More file actions
309 lines (249 loc) · 10.4 KB
/
Copy pathMutation.cpp
File metadata and controls
309 lines (249 loc) · 10.4 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
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
#include <iostream>
#include "Mutation.hpp"
#include "Arguments.hpp"
mut::MutationContainer mut::search_in(std::vector<std::string> const &sequences, unsigned reference_index)
{
MutationContainer mutations;
std::string const &reference = sequences[reference_index];
std::size_t const col = reference.size();
unsigned const row = sequences.size();
for (unsigned i = 0; i != row; ++i)
{
if (i == reference_index)
continue;
for (std::uint64_t j = 0; j != col; )
if (reference[j] != sequences[i][j])
extract_mutation(mutations, sequences, reference_index, i, j);
else
++j;
}
return mutations;
}
mut::MutationContainer mut::search_in(utils::MultipleAlignmentFormat const &infile, unsigned reference_index)
{
MutationContainer mutations;
for (unsigned i = 0; i != infile.records.size(); ++i)
{
auto const &record = infile.records[i];
unsigned const row = record.sequences.size();
unsigned const reference_index_in_this_record = record.where_is(reference_index);
if (reference_index_in_this_record == row)
continue;
std::uint64_t const offset = record.begins[reference_index_in_this_record];
std::string const &reference = record.sequences[reference_index_in_this_record];
std::size_t const col = reference.size();
auto const &map_to_source_site = record.map_to_source_site;
MutationContainer const raw_mutations = search_in(record.sequences, reference_index_in_this_record);
for (auto const &raw_mutation : raw_mutations)
{
auto mutation = raw_mutation.first;
mutation.first = map_to_source_site[mutation.first] + offset;
for (; mutation.last != col && reference[mutation.last] == '-'; ++mutation.last)
;
mutation.last = map_to_source_site[mutation.last] + offset;
auto &positions = mutations[std::move(mutation)];
for (auto const raw_where : raw_mutation.second)
positions.emplace_back(i, raw_where.sequence);
}
}
return mutations;
}
mut::MutationContainer mut::search_in(std::vector<std::string> const &sequences,
std::vector<std::string> const &names,
const std::string &reference_prefix)
{
MutationContainer mutations;
// may contain multiple references
unsigned const row = sequences.size();
std::vector<bool> is_ref(row, false);
for (unsigned ref_id = 0; ref_id != row; ++ ref_id)
if(names[ref_id].starts_with(reference_prefix))
is_ref[ref_id] = true;
for (unsigned ref_id = 0; ref_id != row; ++ ref_id)
{
if(is_ref[ref_id])
{
std::string const &reference = sequences[ref_id];
std::size_t const col = reference.size();
for (unsigned i = 0; i != row; ++i)
{
if (is_ref[i]) continue;
for (std::uint64_t j = 0; j != col; )
if (reference[j] != sequences[i][j])
extract_mutation(mutations, sequences, ref_id, i, j);
else
++j;
}
}
}
return mutations;
}
mut::MutationContainer mut::search_in(utils::MultipleAlignmentFormat &infile, const std::string &reference_prefix)
{
MutationContainer mutations;
for (unsigned i = 0; i != infile.records.size(); ++i)
{
auto &record = infile.records[i];
unsigned const row = record.sequences.size();
unsigned reference_index_in_this_record = row;
bool once = false;
for (unsigned j = 0; j != row; ++ j)
{
if (infile.is_prefix[record.belongs[j]])
{
reference_index_in_this_record = j;
// assume we found once
if (once) {std::cerr << "\033[33mWarning: found two fitted prefix. Please check the block "<< i << " in maf. Program will ignore the reference\033[0m\n";}
once = true;
}
}
if (reference_index_in_this_record == row)
continue;
{
std::string const &reference = record.sequences[reference_index_in_this_record];
std::size_t const col = reference.size();
auto &map_to = record.map_to_source_site; map_to.clear(); map_to.reserve(col + 1);
auto &map_from = record.map_from_source_site; map_from.clear(); map_from.reserve(col + 1);
for (std::size_t i = 0; i != col; ++i)
if (reference[i] != '-') {
map_to.push_back(map_from.size());
map_from.push_back(i);
} else {
map_to.push_back(map_from.size());
}
map_to.push_back(map_from.size());
map_from.push_back(col);
}
std::uint64_t const offset = record.begins[reference_index_in_this_record];
std::string const &reference = record.sequences[reference_index_in_this_record];
std::size_t const col = reference.size();
auto const &map_to_source_site = record.map_to_source_site;
MutationContainer const raw_mutations = search_in(record.sequences, reference_index_in_this_record);
for (auto const &raw_mutation : raw_mutations)
{
auto mutation = raw_mutation.first;
mutation.first = map_to_source_site[mutation.first] + offset;
for (; mutation.last != col && reference[mutation.last] == '-'; ++mutation.last)
;
mutation.last = map_to_source_site[mutation.last] + offset;
auto &positions = mutations[std::move(mutation)];
for (auto const raw_where : raw_mutation.second)
positions.emplace_back(i, raw_where.sequence);
}
}
return mutations;
}
unsigned mut::deduce_variation_type(char lhs, char rhs) noexcept
{
// assert lhs != rhs
if (lhs == '-')
return INS;
else if (rhs == '-')
return DEL;
else
return SUB;
}
template<typename lhs_iter, typename rhs_iter>
unsigned mut::deduce_variation_type(lhs_iter lhs_first, lhs_iter lhs_last, rhs_iter rhs_first) noexcept
{
// assert *lhs_first != *rhs_first
unsigned const flag = deduce_variation_type(*lhs_first++, *rhs_first++);
for (; lhs_first != lhs_last; ++lhs_first, ++rhs_first)
// if *lhs_first == *rhs_first
// assert *lhs_first == '-'
if (*lhs_first != *rhs_first && flag != deduce_variation_type(*lhs_first, *rhs_first))
return REP;
return flag;
}
void mut::extract_mutation(mut::MutationContainer &mutations,
std::vector<std::string> const &sequences,
unsigned reference_index, unsigned which_sequence,
std::uint64_t &position)
{
// assert postition < until
// assert reference_index < sequences.size()
// assert which_sequence < sequences.size()
std::string const &lhs = sequences[reference_index];
std::string const &rhs = sequences[which_sequence];
// assert lhs.size() == rhs.size()
// assert lhs[position] != rhs[position]
Mutation mutation;
mutation.first = position;
// str[str.size()] returns charT() since c++11
// position will end no after than lhs.size()
for (++position; lhs[position] != rhs[position] || lhs[position] == '-'; ++position)
;
mutation.last = position;
// position is anchored here, i.e., the next site pending check
if (position != lhs.size())
++position;
// step back while the end is double '-'
// mutation.last will never reach 0 in this loop because of the caret prefix
while (lhs[mutation.last - 1] == '-' && rhs[mutation.last - 1] == '-')
--mutation.last;
mutation.variation_type = deduce_variation_type(lhs.cbegin() + mutation.first,
lhs.cbegin() + mutation.last,
rhs.cbegin() + mutation.first);
// step back first if the variation type is not substitution
// while making sure that reference[mutation.first] is not '-'
if (mutation.variation_type != SUB)
// assert lhs[mutation.first - 1] == rhs[mutation.first - 1]
for (--mutation.first; lhs[mutation.first] == '-'; --mutation.first)
;
// assert lhs[mutation.first] == rhs[mutation.first]
for (std::uint64_t i = mutation.first; i != mutation.last; ++i)
{
if (lhs[i] != '-') mutation.reference_segment.push_back(lhs[i]);
if (rhs[i] != '-') mutation.counterpart_segment.push_back(rhs[i]);
}
// if (mutation.reference_segment.size() == 1 && mutation.reference_segment[0] == '^'
// || mutation.counterpart_segment.size() == 1 && mutation.counterpart_segment[0] == '^')
if (mutation.reference_segment.front() == '^')
{
std::uint64_t i = mutation.last;
while (lhs[i] == '-') ++i;
if (lhs[i] == '\0')
{
mutation.front_anchored = true;
}
else
{
mutation.reference_segment.push_back(lhs[i]);
mutation.counterpart_segment.push_back(lhs[i]);
mutation.front_anchored = false;
}
}
else
{
mutation.front_anchored = true;
}
mutation.seq_id = reference_index;
// .record will be valued in the calling function
mutations[std::move(mutation)].emplace_back().sequence = which_sequence;
}
bool mut::Mutation::operator<(const Mutation &rhs) const noexcept
{
if (first != rhs.first)
return first < rhs.first;
if (variation_type != rhs.variation_type)
return variation_type < rhs.variation_type;
if (last != rhs.last)
return last < rhs.last;
return counterpart_segment < rhs.counterpart_segment;
}
static void helper_intersection_of(unsigned ll, unsigned lr, unsigned rl, unsigned rr, unsigned &l, unsigned &r) noexcept
{
// assert ll <= rl
if (rl < lr) { l = rl; r = std::min(lr, rr); }
else { l = r = 0; }
}
static void intersection_of(unsigned ll, unsigned lr, unsigned rl, unsigned rr, unsigned &l, unsigned &r) noexcept
{
if (ll > rl) helper_intersection_of(rl, rr, ll, lr, l, r);
else helper_intersection_of(ll, lr, rl, rr, l, r);
}
mut::WhereAbout::WhereAbout(unsigned which_record, unsigned which_sequence)
: record(which_record)
, sequence(which_sequence)
{
}