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
/*
* The MIT License
*
* Wavefront Alignment Algorithms
* Copyright (c) 2017 by Santiago Marco-Sola <santiagomsola@gmail.com>
*
* This file is part of Wavefront Alignment Algorithms.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* PROJECT: Wavefront Alignment Algorithms
* AUTHOR(S): Santiago Marco-Sola <santiagomsola@gmail.com>
* DESCRIPTION: WFA module for the "extension" of exact matches
*/
// Use cross-platform header
#include <sys/types.h>
#include "wavefront_extend_kernels.h"
#include "wavefront_termination.h"
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define wavefront_extend_matches_kernel wavefront_extend_matches_kernel_blockwise
#else
#define wavefront_extend_matches_kernel wavefront_extend_matches_kernel_charwise
#endif
/*
* Inner-most extend kernel
*/
FORCE_INLINE wf_offset_t wavefront_extend_matches_kernel_charwise(
wavefront_aligner_t* const wf_aligner,
const int k,
wf_offset_t offset) {
// Fetch pattern/text
char* pattern_ptr = wf_aligner->sequences.pattern + WAVEFRONT_V(k,offset);
char* text_ptr = wf_aligner->sequences.text + WAVEFRONT_H(k,offset);
// Compare 64-bits blocks
while (*pattern_ptr == *text_ptr) {
// Increment offset
offset++;
// Next chars
++pattern_ptr;
++text_ptr;
}
// Return extended offset
return offset;
}
FORCE_INLINE wf_offset_t wavefront_extend_matches_kernel_blockwise(
wavefront_aligner_t* const wf_aligner,
const int k,
wf_offset_t offset) {
// Fetch pattern/text blocks
uint64_t* pattern_blocks = (uint64_t*)(wf_aligner->sequences.pattern+WAVEFRONT_V(k,offset));
uint64_t* text_blocks = (uint64_t*)(wf_aligner->sequences.text+WAVEFRONT_H(k,offset));
// Compare 64-bits blocks
uint64_t cmp = *pattern_blocks ^ *text_blocks;
while (__builtin_expect(cmp==0,0)) {
// Increment offset (full block)
offset += 8;
// Next blocks
++pattern_blocks;
++text_blocks;
// Compare
cmp = *pattern_blocks ^ *text_blocks;
}
// Count equal characters
const int equal_right_bits = __builtin_ctzl(cmp);
const int equal_chars = DIV_FLOOR(equal_right_bits,8);
offset += equal_chars;
// Return extended offset
return offset;
}
/*
* Wavefront-Extend Inner Kernels
* Wavefront offset extension comparing characters
* Remember:
* - No offset is out of boundaries !(h>tlen,v>plen)
* - if (h==tlen,v==plen) extension won't increment (sentinels)
*/
FORCE_NO_INLINE void wavefront_extend_matches_packed_end2end(
wavefront_aligner_t* const wf_aligner,
wavefront_t* const mwavefront,
const int lo,
const int hi) {
wf_offset_t* const offsets = mwavefront->offsets;
int k;
for (k=lo;k<=hi;++k) {
// Fetch offset
const wf_offset_t offset = offsets[k];
if (offset == WAVEFRONT_OFFSET_NULL) continue;
// Extend offset
offsets[k] = wavefront_extend_matches_kernel(wf_aligner,k,offset);
}
}
FORCE_NO_INLINE wf_offset_t wavefront_extend_matches_packed_end2end_max(
wavefront_aligner_t* const wf_aligner,
wavefront_t* const mwavefront,
const int lo,
const int hi) {
wf_offset_t* const offsets = mwavefront->offsets;
wf_offset_t max_antidiag = 0;
int k;
for (k=lo;k<=hi;++k) {
// Fetch offset
const wf_offset_t offset = offsets[k];
if (offset == WAVEFRONT_OFFSET_NULL) continue;
// Extend offset
offsets[k] = wavefront_extend_matches_kernel(wf_aligner,k,offset);
// Compute max
const wf_offset_t antidiag = WAVEFRONT_ANTIDIAGONAL(k,offsets[k]);
if (max_antidiag < antidiag) max_antidiag = antidiag;
}
return max_antidiag;
}
FORCE_NO_INLINE bool wavefront_extend_matches_packed_endsfree(
wavefront_aligner_t* const wf_aligner,
wavefront_t* const mwavefront,
const int score,
const int lo,
const int hi) {
// Parameters
wf_offset_t* const offsets = mwavefront->offsets;
int k;
for (k=lo;k<=hi;++k) {
// Fetch offset
wf_offset_t offset = offsets[k];
if (offset == WAVEFRONT_OFFSET_NULL) continue;
// Extend offset
offset = wavefront_extend_matches_kernel(wf_aligner,k,offset);
offsets[k] = offset;
// Check ends-free reaching boundaries
if (wavefront_termination_endsfree(wf_aligner,mwavefront,score,k,offset)) {
return true; // Quit (we are done)
}
/*
* TODO
const int h_pos = WAVEFRONT_H(k,offset);
const int v_pos = WAVEFRONT_V(k,offset);
if (h_pos >= text_length || v_pos >= pattern_length) { // FIXME Use wherever necessary
if (wavefront_extend_endsfree_check_termination(wf_aligner,mwavefront,score,k,offset)) {
return true; // Quit (we are done)
}
*/
}
// Alignment not finished
return false;
}
/*
* Wavefront-Extend Inner Kernel (Custom match function)
*/
bool wavefront_extend_matches_custom(
wavefront_aligner_t* const wf_aligner,
wavefront_t* const mwavefront,
const int score,
const int lo,
const int hi,
const bool endsfree,
wf_offset_t* const max_antidiag) {
// Parameters
wavefront_sequences_t* const seqs = &wf_aligner->sequences;
// Extend diagonally each wavefront point
wf_offset_t* const offsets = mwavefront->offsets;
*max_antidiag = 0;
int k;
for (k=lo;k<=hi;++k) {
// Check offset
wf_offset_t offset = offsets[k];
if (offset == WAVEFRONT_OFFSET_NULL) continue;
// Count equal characters
int v = WAVEFRONT_V(k,offset);
int h = WAVEFRONT_H(k,offset);
while (wavefront_sequences_cmp(seqs,v,h)) {
h++; v++; offset++;
}
// Update offset
offsets[k] = offset;
// Compute max
const wf_offset_t antidiag = WAVEFRONT_ANTIDIAGONAL(k,offset);
if (*max_antidiag < antidiag) *max_antidiag = antidiag;
// Check ends-free reaching boundaries
if (endsfree && wavefront_termination_endsfree(wf_aligner,mwavefront,score,k,offset)) {
return true; // Quit (we are done)
}
}
// Alignment not finished
return false;
}