|
| 1 | +use needletail::parse_fastx_file; |
| 2 | +use std::cmp::Ordering; |
| 3 | + |
| 4 | +use super::*; |
| 5 | + |
| 6 | +#[inline(always)] |
| 7 | +pub fn valid_aa(aa: u8) -> bool { |
| 8 | + matches!(aa, b'a' | b'c'..=b'i' | b'k'..=b'n' | b'p'..=b't' | b'v' | b'w' | b'y' | b'A' | b'C'..=b'I' | b'K'..=b'N' | b'P'..=b'T' | b'V' | b'W' | b'Y' ) |
| 9 | +} |
| 10 | + |
| 11 | +// Split a 64-bit word into 33 and 31-bit sub-words and left-rotate them separately. |
| 12 | +// Increases period from 64 to 1023. |
| 13 | +#[inline(always)] |
| 14 | +pub fn srol(x: u64) -> u64 { |
| 15 | + let m: u64 = ((x & 0x8000000000000000) >> 30) | ((x & 0x100000000) >> 32); |
| 16 | + ((x << 1) & 0xFFFFFFFDFFFFFFFF) | m |
| 17 | +} |
| 18 | + |
| 19 | +/// Stores forward and (optionally) reverse complement hashes of k-mers in a nucleotide sequence |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct AaHashIterator { |
| 22 | + k: usize, |
| 23 | + level: AaLevel, |
| 24 | + fh: u64, |
| 25 | + index: usize, |
| 26 | + seq: Vec<u8>, |
| 27 | + invalid_count: usize, |
| 28 | +} |
| 29 | + |
| 30 | +impl RollHash for AaHashIterator { |
| 31 | + fn set_k(&mut self, k: usize) { |
| 32 | + self.k = k; |
| 33 | + if let Some(new_it) = Self::new_iterator(0, &self.level, &self.seq, k) { |
| 34 | + self.fh = new_it.0; |
| 35 | + self.index = new_it.1; |
| 36 | + } else { |
| 37 | + panic!("K-mer larger than smallest valid sequence"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Retrieve the current hash (minimum of forward and reverse complement hashes) |
| 42 | + fn curr_hash(&self) -> u64 { |
| 43 | + self.fh |
| 44 | + } |
| 45 | + |
| 46 | + fn hash_type(&self) -> HashType { |
| 47 | + HashType::AA(self.level.clone()) |
| 48 | + } |
| 49 | + |
| 50 | + fn seq_len(&self) -> usize { |
| 51 | + self.seq.len() |
| 52 | + } |
| 53 | + |
| 54 | + fn sketch_data(&self) -> (bool, [usize; 4], usize) { |
| 55 | + (false, [0, 0, 0, 0], self.invalid_count) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl AaHashIterator { |
| 60 | + pub fn default(level: AaLevel) -> Self { |
| 61 | + Self { |
| 62 | + k: 0, |
| 63 | + level, |
| 64 | + fh: 0, |
| 65 | + index: 0, |
| 66 | + seq: Vec::new(), |
| 67 | + invalid_count: 0, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pub fn new(file: &str, level: AaLevel, concat_fasta: bool) -> Vec<Self> { |
| 72 | + let mut hash_vec = Vec::new(); |
| 73 | + |
| 74 | + // Read sequence into memory (as we go through multiple times) |
| 75 | + log::debug!("Preprocessing sequence"); |
| 76 | + let mut reader = |
| 77 | + parse_fastx_file(file).unwrap_or_else(|_| panic!("Invalid path/file: {file}")); |
| 78 | + loop { |
| 79 | + let record_read = reader.next(); |
| 80 | + let mut seq_hash_it = Self::default(level.clone()); |
| 81 | + if let Some(record) = record_read { |
| 82 | + let seqrec = record.expect("Invalid FASTA/Q record"); |
| 83 | + if seqrec.qual().is_some() { |
| 84 | + panic!("Unexpected quality information with AA sequences in {file}. Correct sequence type set?"); |
| 85 | + } else { |
| 86 | + for aa in seqrec.seq().iter() { |
| 87 | + if valid_aa(*aa) { |
| 88 | + seq_hash_it.seq.push(*aa) |
| 89 | + } else { |
| 90 | + seq_hash_it.invalid_count += 1; |
| 91 | + seq_hash_it.seq.push(SEQSEP); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if concat_fasta { |
| 96 | + hash_vec.push(seq_hash_it); |
| 97 | + } else { |
| 98 | + seq_hash_it.seq.push(SEQSEP); |
| 99 | + } |
| 100 | + } else { |
| 101 | + if !concat_fasta { |
| 102 | + hash_vec.push(seq_hash_it); |
| 103 | + } |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + hash_vec |
| 108 | + } |
| 109 | + |
| 110 | + fn new_iterator( |
| 111 | + mut start: usize, |
| 112 | + level: &AaLevel, |
| 113 | + seq: &[u8], |
| 114 | + k: usize, |
| 115 | + ) -> Option<(u64, usize)> { |
| 116 | + let mut fh = 0_u64; |
| 117 | + 'outer: while start < (seq.len() - k) { |
| 118 | + '_inner: for (i, v) in seq[start..(start + k)].iter().enumerate() { |
| 119 | + // If invalid seq |
| 120 | + if !valid_aa(*v) { |
| 121 | + start += i + 1; |
| 122 | + if start >= seq.len() { |
| 123 | + return None; |
| 124 | + } |
| 125 | + fh = 0; |
| 126 | + continue 'outer; // Try again from new start |
| 127 | + } |
| 128 | + fh = srol(fh); |
| 129 | + fh ^= level.aa_seed_table(*v); |
| 130 | + } |
| 131 | + break 'outer; // success |
| 132 | + } |
| 133 | + if start >= (seq.len() - k) { |
| 134 | + return None; |
| 135 | + } |
| 136 | + |
| 137 | + Some((fh, start + k)) |
| 138 | + } |
| 139 | + |
| 140 | + /// Move to the next k-mer by adding a new base, removing a base from the end, efficiently updating the hash. |
| 141 | + fn roll_fwd(&mut self, old_aa: u8, new_aa: u8) { |
| 142 | + self.fh = srol(self.fh); |
| 143 | + self.fh ^= self.level.aa_seed_table(new_aa); |
| 144 | + self.fh ^= self.level.aa_roll_table(old_aa, self.k); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl Iterator for AaHashIterator { |
| 149 | + type Item = u64; |
| 150 | + |
| 151 | + fn next(&mut self) -> Option<Self::Item> { |
| 152 | + match self.index.cmp(&self.seq_len()) { |
| 153 | + Ordering::Less => { |
| 154 | + let current = self.curr_hash(); |
| 155 | + let new_aa = self.seq[self.index]; |
| 156 | + // Restart hash if invalid base |
| 157 | + if !valid_aa(new_aa) { |
| 158 | + if let Some(new_it) = |
| 159 | + Self::new_iterator(self.index + 1, &self.level, &self.seq, self.k) |
| 160 | + { |
| 161 | + self.fh = new_it.0; |
| 162 | + self.index = new_it.1; |
| 163 | + } else { |
| 164 | + self.index = self.seq_len(); // End of valid sequence |
| 165 | + } |
| 166 | + } else { |
| 167 | + self.roll_fwd(self.seq[self.index - self.k], new_aa); |
| 168 | + self.index += 1; |
| 169 | + } |
| 170 | + Some(current) |
| 171 | + } |
| 172 | + Ordering::Equal => { |
| 173 | + // Final hash, do not roll forward further |
| 174 | + self.index += 1; |
| 175 | + Some(self.curr_hash()) |
| 176 | + } |
| 177 | + Ordering::Greater => { |
| 178 | + // End of sequence |
| 179 | + None |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 185 | + (self.seq_len(), Some(self.seq_len())) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +// This lets you use collect etc |
| 190 | +impl ExactSizeIterator for AaHashIterator {} |
0 commit comments