5 #ifndef YMIR_ALIGNMENT_MATRIX_H 6 #define YMIR_ALIGNMENT_MATRIX_H 13 #include "nogap_alignment_vector.h" 14 #include "gapped_alignment_vector.h" 22 template <
typename AlignmentType>
27 typedef std::vector<bool> bit_storage_t;
30 typedef std::vector<alignment_score_t> score_storage_t;
33 static const seq_len_t default_nrows = 80;
36 static const seq_len_t default_ncols = 20;
43 _starts(_nrow * _ncol,
false),
44 _matrix(_nrow * _ncol, 0)
50 const sequence_t &pattern,
51 const sequence_t &text)
53 _nrow(pattern.size() + 1),
54 _ncol(text.size() + 1),
55 _starts(_nrow * _ncol,
false),
56 _matrix(_nrow * _ncol, 0)
61 void reinit(seg_index_t gene,
62 const sequence_t &pattern,
63 const sequence_t &text)
66 _nrow = pattern.size() + 1;
67 _ncol = text.size() + 1;
68 _starts.resize(_nrow * _ncol);
69 std::fill(_starts.begin(), _starts.end(),
false);
70 _matrix.resize(_nrow * _ncol);
71 std::fill(_matrix.begin(), _matrix.end(), 0);
75 void setStart(seq_len_t row, seq_len_t col) { _starts[index(row, col)] =
true; }
78 alignment_score_t getBestAlignment(AlignmentType *vec,
const sequence_t &pattern,
const sequence_t &text)
const;
84 alignment_score_t score(seq_len_t row, seq_len_t col)
const {
return _matrix[index(row, col)]; }
87 alignment_score_t& score(seq_len_t row, seq_len_t col) {
return _matrix[index(row, col)]; }
92 seq_len_t _nrow, _ncol;
94 bit_storage_t _starts;
95 score_storage_t _matrix;
98 size_t index(seq_len_t row, seq_len_t col)
const {
return row * _ncol + col; }
111 const sequence_t &pattern,
112 const sequence_t &text)
const 114 AlignmentVectorBase::events_storage_t bitvec;
117 seq_len_t max_i = 0, max_j = 0;
118 alignment_score_t max_score = -1;
119 for (seq_len_t i = 0; i < _nrow; ++i) {
120 for (seq_len_t j = 0; j < _ncol; ++j) {
121 if (score(i, j) > max_score) {
124 max_score = score(i, j);
130 seq_len_t cur_i = max_i, cur_j = max_j;
131 while (cur_i != 1 && cur_j != 1) {
136 seq_len_t start_i = cur_i, start_j = cur_j;
139 while (cur_i < _nrow && cur_j < _ncol) {
140 bitvec.push_back(pattern[cur_i - 1] != text[cur_j - 1]);
147 return score(max_i, max_j);
153 const sequence_t &pattern,
154 const sequence_t &text)
const 156 AlignmentVectorBase::events_storage_t bitvec;
159 seq_len_t max_i = 0, max_j = 0;
160 alignment_score_t max_score = -1;
161 for (seq_len_t i = 0; i < _nrow; ++i) {
162 for (seq_len_t j = 0; j < _ncol; ++j) {
163 if (score(i, j) > max_score) {
166 max_score = score(i, j);
172 seq_len_t cur_i = max_i, cur_j = max_j, max_index = 0;
173 std::array<alignment_score_t, 3> score_arr;
174 while (!_starts[index(cur_i, cur_j)]) {
175 score_arr[0] = score(cur_i - 1, cur_j - 1);
176 score_arr[1] = cur_j > 0 ? score(cur_i, cur_j - 1) : -1;
177 score_arr[2] = cur_i > 0 ? score(cur_i - 1, cur_j) : -1;
178 max_index = std::distance(score_arr.begin(), std::max_element(score_arr.begin(), score_arr.end()));
182 pattern[cur_i - 1] == text[cur_j - 1] ? add_match(&bitvec) : add_mismatch(&bitvec);
204 AlignmentVectorBase::events_storage_t bitvec2;
206 for (
int i = bitvec.size() / 2 - 1; i >= 0; --i) {
207 bitvec2.push_back(bitvec[i*2]);
208 bitvec2.push_back(bitvec[i*2 + 1]);
210 vec->addAlignment(_gene, cur_i, cur_j, bitvec2);
212 return score(max_i, max_j);
217 #endif //YMIR_ALIGNMENT_MATRIX_H
Definition: nogap_alignment_vector.h:37
void addAlignment(seg_index_t id, seq_len_t p_start, seq_len_t t_start, seq_len_t size)
Add a new alignment to the vector.
Definition: nogap_alignment_vector.h:55
Definition: alignment_matrix.h:23
Definition: gapped_alignment_vector.h:52