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
#[cfg(feature = "simd_avx2")]
use crate::avx2::*;
#[cfg(feature = "simd_wasm")]
use crate::simd128::*;
use std::i8;
pub trait Matrix {
const NULL: u8;
fn new() -> Self;
fn set(&mut self, a: u8, b: u8, score: i8);
fn get(&self, a: u8, b: u8) -> i8;
fn as_ptr(&self, i: usize) -> *const i8;
unsafe fn get_scores(&self, c: u8, v: HalfSimd, right: bool) -> Simd;
fn convert_char(c: u8) -> u8;
}
#[repr(C, align(32))]
#[derive(Clone, PartialEq, Debug)]
pub struct AAMatrix {
scores: [i8; 27 * 32]
}
impl AAMatrix {
pub const fn new_simple(match_score: i8, mismatch_score: i8) -> Self {
let mut scores = [i8::MIN; 27 * 32];
let mut i = b'A';
while i <= b'Z' {
let mut j = b'A';
while j <= b'Z' {
let idx = ((i - b'A') as usize) * 32 + ((j - b'A') as usize);
scores[idx] = if i == j { match_score } else { mismatch_score };
j += 1;
}
i += 1;
}
Self { scores }
}
}
impl Matrix for AAMatrix {
const NULL: u8 = b'A' + 26u8;
fn new() -> Self {
Self { scores: [i8::MIN; 27 * 32] }
}
fn set(&mut self, a: u8, b: u8, score: i8) {
let a = a.to_ascii_uppercase();
let b = b.to_ascii_uppercase();
assert!(b'A' <= a && a <= b'Z' + 1);
assert!(b'A' <= b && b <= b'Z' + 1);
let idx = ((a - b'A') as usize) * 32 + ((b - b'A') as usize);
self.scores[idx] = score;
let idx = ((b - b'A') as usize) * 32 + ((a - b'A') as usize);
self.scores[idx] = score;
}
fn get(&self, a: u8, b: u8) -> i8 {
let a = a.to_ascii_uppercase();
let b = b.to_ascii_uppercase();
assert!(b'A' <= a && a <= b'Z' + 1);
assert!(b'A' <= b && b <= b'Z' + 1);
let idx = ((a - b'A') as usize) * 32 + ((b - b'A') as usize);
self.scores[idx]
}
#[inline]
fn as_ptr(&self, i: usize) -> *const i8 {
debug_assert!(i < 27);
unsafe { self.scores.as_ptr().add(i * 32) }
}
#[cfg_attr(feature = "simd_avx2", target_feature(enable = "avx2"))]
#[cfg_attr(feature = "simd_wasm", target_feature(enable = "simd128"))]
#[inline]
unsafe fn get_scores(&self, c: u8, v: HalfSimd, _right: bool) -> Simd {
let matrix_ptr = self.as_ptr(c as usize);
let scores1 = halfsimd_load(matrix_ptr as *const HalfSimd);
let scores2 = halfsimd_load((matrix_ptr as *const HalfSimd).add(1));
halfsimd_lookup2_i16(scores1, scores2, v)
}
#[inline]
fn convert_char(c: u8) -> u8 {
let c = c.to_ascii_uppercase();
assert!(c >= b'A' && c <= Self::NULL);
c - b'A'
}
}
#[repr(C, align(32))]
#[derive(Clone, PartialEq, Debug)]
pub struct NucMatrix {
scores: [i8; 8 * 16]
}
impl NucMatrix {
pub const fn new_simple(match_score: i8, mismatch_score: i8) -> Self {
let mut scores = [i8::MIN; 8 * 16];
let alpha = [b'A', b'T', b'C', b'G', b'N'];
let mut i = 0;
while i < alpha.len() {
let mut j = 0;
while j < alpha.len() {
let idx = ((alpha[i] & 0b111) as usize) * 16 + ((alpha[j] & 0b1111) as usize);
scores[idx] = if i == j { match_score } else { mismatch_score };
j += 1;
}
i += 1;
}
Self { scores }
}
}
impl Matrix for NucMatrix {
const NULL: u8 = b'Z';
fn new() -> Self {
Self { scores: [i8::MIN; 8 * 16] }
}
fn set(&mut self, a: u8, b: u8, score: i8) {
let a = a.to_ascii_uppercase();
let b = b.to_ascii_uppercase();
assert!(b'A' <= a && a <= b'Z');
assert!(b'A' <= b && b <= b'Z');
let idx = ((a & 0b111) as usize) * 16 + ((b & 0b1111) as usize);
self.scores[idx] = score;
let idx = ((b & 0b111) as usize) * 16 + ((a & 0b1111) as usize);
self.scores[idx] = score;
}
fn get(&self, a: u8, b: u8) -> i8 {
let a = a.to_ascii_uppercase();
let b = b.to_ascii_uppercase();
assert!(b'A' <= a && a <= b'Z');
assert!(b'A' <= b && b <= b'Z');
let idx = ((a & 0b111) as usize) * 16 + ((b & 0b1111) as usize);
self.scores[idx]
}
#[inline]
fn as_ptr(&self, i: usize) -> *const i8 {
unsafe { self.scores.as_ptr().add((i & 0b111) * 16) }
}
#[cfg_attr(feature = "simd_avx2", target_feature(enable = "avx2"))]
#[cfg_attr(feature = "simd_wasm", target_feature(enable = "simd128"))]
#[inline]
unsafe fn get_scores(&self, c: u8, v: HalfSimd, _right: bool) -> Simd {
let matrix_ptr = self.as_ptr(c as usize);
let scores = halfsimd_load(matrix_ptr as *const HalfSimd);
halfsimd_lookup1_i16(scores, v)
}
#[inline]
fn convert_char(c: u8) -> u8 {
let c = c.to_ascii_uppercase();
assert!(c >= b'A' && c <= Self::NULL);
c
}
}
#[repr(C)]
#[derive(Clone, PartialEq, Debug)]
pub struct ByteMatrix {
match_score: i8,
mismatch_score: i8
}
impl ByteMatrix {
pub const fn new_simple(match_score: i8, mismatch_score: i8) -> Self {
Self { match_score, mismatch_score }
}
}
impl Matrix for ByteMatrix {
const NULL: u8 = b'\0';
fn new() -> Self {
Self { match_score: i8::MIN, mismatch_score: i8::MIN }
}
fn set(&mut self, _a: u8, _b: u8, _score: i8) {
unimplemented!();
}
fn get(&self, a: u8, b: u8) -> i8 {
if a == b { self.match_score } else { self.mismatch_score }
}
#[inline]
fn as_ptr(&self, _i: usize) -> *const i8 {
unimplemented!()
}
#[cfg_attr(feature = "simd_avx2", target_feature(enable = "avx2"))]
#[cfg_attr(feature = "simd_wasm", target_feature(enable = "simd128"))]
#[inline]
unsafe fn get_scores(&self, c: u8, v: HalfSimd, _right: bool) -> Simd {
let match_scores = halfsimd_set1_i8(self.match_score);
let mismatch_scores = halfsimd_set1_i8(self.mismatch_score);
halfsimd_lookup_bytes_i16(match_scores, mismatch_scores, halfsimd_set1_i8(c as i8), v)
}
#[inline]
fn convert_char(c: u8) -> u8 {
c
}
}
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static NW1: NucMatrix = NucMatrix::new_simple(1, -1);
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BLOSUM45: AAMatrix = AAMatrix { scores: include!("../matrices/BLOSUM45") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BLOSUM50: AAMatrix = AAMatrix { scores: include!("../matrices/BLOSUM50") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BLOSUM62: AAMatrix = AAMatrix { scores: include!("../matrices/BLOSUM62") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BLOSUM80: AAMatrix = AAMatrix { scores: include!("../matrices/BLOSUM80") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BLOSUM90: AAMatrix = AAMatrix { scores: include!("../matrices/BLOSUM90") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static PAM100: AAMatrix = AAMatrix { scores: include!("../matrices/PAM100") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static PAM120: AAMatrix = AAMatrix { scores: include!("../matrices/PAM120") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static PAM160: AAMatrix = AAMatrix { scores: include!("../matrices/PAM160") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static PAM200: AAMatrix = AAMatrix { scores: include!("../matrices/PAM200") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static PAM250: AAMatrix = AAMatrix { scores: include!("../matrices/PAM250") };
#[cfg_attr(not(target_arch = "wasm32"), no_mangle)]
pub static BYTES1: ByteMatrix = ByteMatrix::new_simple(1, -1);
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Gaps {
pub open: i8,
pub extend: i8
}