OpenMS
AhoCorasickAmbiguous.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h>
39 
40 #include <cassert>
41 #include <functional> // for std::hash
42 #include <limits>
43 #include <queue>
44 #include <string>
45 #include <unordered_map>
46 #include <vector>
47 
48 namespace OpenMS
49 {
52  constexpr char const AAtoChar[28] = {
53  'A', // 00 Ala Alanine
54  'Y', // 01 Tyr Tyrosine
55  'C', // 02 Cys Cysteine
56  'D', // 03 Asp Aspartic Acid // B
57  'N', // 04 Asn Asparagine // B
58  'F', // 05 Phe Phenylalanine
59  'G', // 06 Gly Glycine
60  'H', // 07 His Histidine
61  'I', // 08 Ile Isoleucine // J
62  'L', // 09 Leu Leucine // J
63  'K', // 10 Lys Lysine
64  'W', // 11 Trp Tryptophan
65  'M', // 12 Met Methionine
66  'O', // 13 Pyl Pyrrolysine
67  'P', // 14 Pro Proline
68  'E', // 15 Glu Glutamic Acid // Z
69  'Q', // 16 Gln Glutamine // Z
70  'R', // 17 Arg Arginine
71  'S', // 18 Ser Serine
72  'T', // 19 Thr Threonine
73  'U', // 20 Selenocysteine
74  'V', // 21 Val Valine
75  // ambiguous AAs start here (index: 22...25)
76  'B', // 22 Aspartic Acid, Asparagine $ // the ambAA's need to be consecutive (B,J,Z,X,$)
77  'J', // 23 Leucine, Isoleucine $
78  'Z', // 24 Glutamic Acid, Glutamine $
79  'X', // 25 Unknown
80  // non-regular AA's, which are special
81  '$', // 26 superAA, i.e. it models a mismatch, which can be anything, including AAAs
82  '?', // 27 invalid AA (will usually be skipped) -- must be the last AA (AA::operator++ and others rely on it)
83  };
84 
86  constexpr char const CharToAA[128] = {
87  // ASCII char (7-bit Int with values from 0..127) --> amino acid
88  27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, // 0
89  27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, // 1
90  // $
91  27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, // 2
92  27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, // 3
93 
94  // , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O,
95  27, 00, 22, 02, 03, 15, 05, 06, 07, 8, 23, 10, 9, 12, 04, 13, // 4
96 
97  // P, Q, R, S, T, U, V, W, X, Y, Z, , , , , ,
98  14, 16, 17, 18, 19, 20, 21, 11, 25, 01, 24, 27, 27, 27, 27, 27, // 5
99 
100  // , a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,
101  27, 00, 22, 02, 03, 15, 05, 06, 07, 8, 23, 10, 9, 12, 04, 13, // 6
102 
103  // p, q, r, s, t, u, v, w, x, y, z, , , , , ,
104  14, 16, 17, 18, 19, 20, 21, 11, 25, 01, 24, 27, 27, 27, 27, 27, // 7
105  };
106 
109  struct OPENMS_DLLAPI Hit {
110  using T = uint32_t;
111  Hit() = default;
112  Hit(T needle_index, T needle_length, T query_pos) : needle_index(needle_index), needle_length(needle_length), query_pos(query_pos) {};
116  bool operator==(const Hit& rhs) const
117  {
118  return needle_index == rhs.needle_index && needle_length == rhs.needle_length && query_pos == rhs.query_pos;
119  }
120  };
121 
124  struct OPENMS_DLLAPI AA
125  {
127  constexpr AA() : aa_(AA('?').aa_)
128  {
129  }
130 
134  constexpr explicit AA(const char c) : aa_(CharToAA[(unsigned char)c])
135  {
136  }
137 
139  constexpr uint8_t operator()() const
140  {
141  return aa_;
142  }
143 
145  constexpr bool operator==(const AA other) const
146  {
147  return aa_ == other.aa_;
148  }
149 
151  constexpr bool operator<=(const AA other) const
152  {
153  return aa_ <= other.aa_;
154  }
155 
157  constexpr bool isAmbiguous() const
158  {
159  return aa_ >= AA('B').aa_;
160  }
161 
163  constexpr bool isValid() const
164  {
165  return aa_ != AA('?').aa_;
166  }
167 
169  constexpr bool isValidForPeptide() const
170  {
171  return aa_ <= AA('X').aa_; // '$' or '?'
172  }
173 
175  constexpr AA& operator++()
176  {
177  ++aa_;
178  assert(aa_ <= AA('?').aa_); // make sure we don't overflow
179  return *this;
180  }
181 
183  constexpr AA operator++(int)
184  {
185  AA r(*this);
186  ++aa_;
187  assert(aa_ <= AA('?').aa_); // make sure we don't overflow
188  return r;
189  }
190 
192  constexpr AA operator-(const AA rhs) const
193  {
194  AA r(*this);
195  r.aa_ -= rhs.aa_;
196  return r;
197  }
198 
199  private:
200  uint8_t aa_;
201  };
202 
205  class OPENMS_DLLAPI Index
206  {
207  public:
208  using T = uint32_t;
210  Index() = default;
211 
213  Index(T val) : i_(val) {};
214 
216  bool isInvalid() const;
217 
219  bool isValid() const;
220 
222  T operator()() const;
223 
225  bool operator==(const Index other) const;
226 
228  T& pos();
229 
231  T pos() const;
232  private:
233  T i_ = std::numeric_limits<T>::max();
234  };
235  } // namespace OpenMS
236 
237 // this needs to go into namespace std
238 template<> struct std::hash<OpenMS::Index>
239 {
240  std::size_t operator()(OpenMS::Index const& s) const noexcept
241  {
242  return std::hash<OpenMS::Index::T> {}(s());
243  }
244 };
245 
246 namespace OpenMS
247 {
250  struct OPENMS_DLLAPI ACNode
251  {
253  ACNode() {};
254 
256  ACNode(const AA label, const uint8_t depth) : edge(label)
257  {
258  depth_and_hits.depth = depth;
259  }
260 
262  struct DepthHits {
264  {
265  memset(this, 0, sizeof *this); // make sure bitfields are 0; C++20 allows {} initialization ...
266  };
267  uint8_t has_hit : 1;
268  // we could add another bit here to distinguish between a local hit and suffix hit, but on Windows, this slows it down
269  uint8_t depth : 7;
270  };
271 
272  using ChildCountType = uint8_t;
273 
274  Index suffix {0};
275  Index first_child {0};
276  // there is room for optimization here, by pulling edge labels into a separate vector (allows for SSE-enabled search of children)
277  AA edge {0};
278  ChildCountType nr_children = 0;
280  };
281 
282  // forward declaration
283  struct ACTrieState;
284 
286  struct OPENMS_DLLAPI ACSpawn
287  {
289  ACSpawn() = delete;
290 
292  ACSpawn(std::string::const_iterator query_pos, Index tree_pos, uint8_t max_aa, uint8_t max_mm, uint8_t max_prefix_loss);
293 
295  size_t textPos(const ACTrieState& state) const;
296 
299  AA nextValidAA(const ACTrieState& state);
300 
301  std::string::const_iterator it_query;
303  uint8_t max_aaa_leftover {0};
304  uint8_t max_mm_leftover {0};
305  uint8_t max_prefix_loss_leftover {0};
306  };
307 
310  OPENMS_DLLAPI AA nextValidAA(const std::string::const_iterator end, std::string::const_iterator& it_q);
311 
314  struct OPENMS_DLLAPI ACTrieState
315  {
316  friend ACSpawn;
319  void setQuery(const std::string& haystack);
320 
322  size_t textPos() const;
323 
325  std::string::const_iterator textPosIt() const;
326 
328  const std::string& getQuery() const;
329 
333 
334  std::vector<Hit> hits;
336  std::queue<ACSpawn> spawns;
337 
338  private:
339  std::string query_;
340  std::string::const_iterator it_q_;
341  };
342 
344  class OPENMS_DLLAPI ACTrie
345  {
346  public:
353  ACTrie(uint32_t max_aaa = 0, uint32_t max_mm = 0);
354 
357 
361  void addNeedle(const std::string& needle);
362 
366  void addNeedles(const std::vector<std::string>& needles);
367 
370  void addNeedlesAndCompress(const std::vector<std::string>& needles);
371 
379  void compressTrie();
380 
382  size_t getNeedleCount() const;
383 
386  void setMaxAAACount(const uint32_t max_aaa);
387 
389  uint32_t getMaxAAACount() const;
390 
393  void setMaxMMCount(const uint32_t max_mm);
394 
396  uint32_t getMaxMMCount() const;
397 
401  bool nextHits(ACTrieState& state) const;
402 
405  void getAllHits(ACTrieState& state) const;
406 
407  private:
411  bool nextHitsNoClear_(ACTrieState& state) const;
412 
416  Index add_(const Index from, const AA edge);
417 
426  bool addHits_(Index i, const size_t text_pos, std::vector<Hit>& hits) const;
427 
429  bool addHitsSpawn_(Index i, const ACSpawn& spawn, const size_t text_pos, std::vector<Hit>& hits, const int current_spawn_depths) const;
430 
434  Index follow_(const Index i, const AA edge) const;
435 
438  bool followSpawn_(ACSpawn& spawn, const AA edge, ACTrieState& state) const;
439 
442  Index stepMaster_(const Index i, const AA edge, ACTrieState& state) const;
443 
446  bool stepSpawn_(ACSpawn& spawn, ACTrieState& state) const;
447 
450  void createSpawns_(const Index i, const AA fromAA, const AA toAA, ACTrieState& state, const uint32_t aaa_left, const uint32_t mm_left) const;
451 
453  void createSubSpawns_(const ACSpawn& prototype, const AA fromAA, const AA toAA, ACTrieState& state) const;
454 
456  void createMMSpawns_(const Index i, const AA except_fromAA, const AA except_toAA, const AA except_edge, ACTrieState& state, const uint32_t aaa_left, const uint32_t mm_left) const;
457 
459  void createMMSubSpawns_(const ACSpawn& prototype, const AA except_fromAA, const AA except_toAA, const AA except_edge, ACTrieState& state) const;
460 
462  Index findChildNaive_(Index parent, AA child_label);
463 
465  Index findChildBFS_(const Index parent, const AA child_label) const;
466 
467  std::vector<ACNode> trie_;
468  uint32_t needle_count_ {0};
469  uint32_t max_aaa_ {0};
470  uint32_t max_mm_ {0};
471 
473  std::unordered_map<Index, std::vector<uint32_t>> umap_index2needles_;
475  std::unordered_map<Index, std::vector<Index>> umap_index2children_naive_;
476  };
477 
478 } // namespace OpenMS
479 
An Aho Corasick trie (a set of nodes with suffix links mainly)
Definition: AhoCorasickAmbiguous.h:345
Index stepMaster_(const Index i, const AA edge, ACTrieState &state) const
bool stepSpawn_(ACSpawn &spawn, ACTrieState &state) const
void setMaxMMCount(const uint32_t max_mm)
uint32_t getMaxMMCount() const
Maximum number of mismatches allowed during search.
ACTrie(uint32_t max_aaa=0, uint32_t max_mm=0)
Default C'tor which just creates a root node.
void addNeedlesAndCompress(const std::vector< std::string > &needles)
bool nextHitsNoClear_(ACTrieState &state) const
std::vector< ACNode > trie_
the trie, in either naive structure or BFS order (after compressTrie)
Definition: AhoCorasickAmbiguous.h:467
Index add_(const Index from, const AA edge)
void addNeedle(const std::string &needle)
void setMaxAAACount(const uint32_t max_aaa)
std::unordered_map< Index, std::vector< uint32_t > > umap_index2needles_
maps a node to which needles end there (valid for both naive and BFS tree)
Definition: AhoCorasickAmbiguous.h:473
void addNeedles(const std::vector< std::string > &needles)
bool addHitsSpawn_(Index i, const ACSpawn &spawn, const size_t text_pos, std::vector< Hit > &hits, const int current_spawn_depths) const
same as addHits_, but only follows the suffix chain until the spawn loses its prefix
Index follow_(const Index i, const AA edge) const
void getAllHits(ACTrieState &state) const
Index findChildBFS_(const Index parent, const AA child_label) const
After compression (BFS trie), obtain the child with edge child_label from parent; if it does not exis...
bool addHits_(Index i, const size_t text_pos, std::vector< Hit > &hits) const
Add all hits occurring in node i (including all its suffix hits)
void createSpawns_(const Index i, const AA fromAA, const AA toAA, ACTrieState &state, const uint32_t aaa_left, const uint32_t mm_left) const
bool followSpawn_(ACSpawn &spawn, const AA edge, ACTrieState &state) const
uint32_t getMaxAAACount() const
Maximum number of ambiguous amino acids allowed during search.
Index findChildNaive_(Index parent, AA child_label)
During needle addition (naive trie), obtain the child with edge child_label from parent; if it does n...
size_t getNeedleCount() const
How many needles were added to the trie?
void compressTrie()
Traverses the trie in BFS order and makes it more compact and efficient to traverse.
void createSubSpawns_(const ACSpawn &prototype, const AA fromAA, const AA toAA, ACTrieState &state) const
Create spawns from a spawn with an AAA or MM, using prototype as template, following edges in range f...
void createMMSpawns_(const Index i, const AA except_fromAA, const AA except_toAA, const AA except_edge, ACTrieState &state, const uint32_t aaa_left, const uint32_t mm_left) const
Same as createSpawns_, but instantiate all possible AA's except for those in the range from except_fr...
std::unordered_map< Index, std::vector< Index > > umap_index2children_naive_
maps the child nodes of a node for the naive tree; only needed during naive trie construction; storin...
Definition: AhoCorasickAmbiguous.h:475
void createMMSubSpawns_(const ACSpawn &prototype, const AA except_fromAA, const AA except_toAA, const AA except_edge, ACTrieState &state) const
Same as createSubSpawns_, but instantiate all possible AA's except for those in the range from except...
bool nextHits(ACTrieState &state) const
Definition: AhoCorasickAmbiguous.h:206
bool operator==(const Index other) const
equality operator
uint32_t T
Definition: AhoCorasickAmbiguous.h:208
bool isInvalid() const
is this Index invalid, i.e. should not be dereferenced
T pos() const
allows to read the index, using `index.pos()`
Index()=default
default C'tor; creates an invalid index
Index(T val)
C'tor from T.
Definition: AhoCorasickAmbiguous.h:213
bool isValid() const
is this Index valid, i.e. an actual index into a vector?
T operator()() const
convert to a number (might be invalid, check with .isValid() first)
T & pos()
allows to set the index, using `index.pos() = 3;` or simply read its value
const double c
Definition: Constants.h:214
static String suffix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:156
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
AA nextValidAA(const std::string::const_iterator end, std::string::const_iterator &it_q)
constexpr char const AAtoChar[28]
Definition: AhoCorasickAmbiguous.h:52
constexpr char const CharToAA[128]
Conversion table from 7-bit ASCII char to internal value representation for an amino acid (AA)
Definition: AhoCorasickAmbiguous.h:86
Definition: AhoCorasickAmbiguous.h:125
constexpr bool isValid() const
is AA a letter or '$' ?
Definition: AhoCorasickAmbiguous.h:163
constexpr AA(const char c)
Definition: AhoCorasickAmbiguous.h:134
constexpr bool isAmbiguous() const
is AA a 'B', 'J', 'Z', 'X', or '$' ?
Definition: AhoCorasickAmbiguous.h:157
constexpr bool isValidForPeptide() const
is the AA a letter, i.e. A-Z or a-z?
Definition: AhoCorasickAmbiguous.h:169
constexpr AA operator++(int)
Post-increment operator (advance to next AA)
Definition: AhoCorasickAmbiguous.h:183
uint8_t aa_
internal representation as 1-byte integer
Definition: AhoCorasickAmbiguous.h:200
constexpr AA & operator++()
Pre-increment operator (advance to next AA)
Definition: AhoCorasickAmbiguous.h:175
constexpr bool operator<=(const AA other) const
less-or-equal operator
Definition: AhoCorasickAmbiguous.h:151
constexpr uint8_t operator()() const
yields the internal 8bit representation
Definition: AhoCorasickAmbiguous.h:139
constexpr AA operator-(const AA rhs) const
Decrement operator.
Definition: AhoCorasickAmbiguous.h:192
constexpr AA()
Default C'tor; creates an invalid AA.
Definition: AhoCorasickAmbiguous.h:127
constexpr bool operator==(const AA other) const
equality operator
Definition: AhoCorasickAmbiguous.h:145
internal struct to steal one bit from depth to use as hit indicator
Definition: AhoCorasickAmbiguous.h:262
DepthHits()
Definition: AhoCorasickAmbiguous.h:263
uint8_t depth
depth of node in the trie
Definition: AhoCorasickAmbiguous.h:269
uint8_t has_hit
does a pattern end here (or when following suffix links)?
Definition: AhoCorasickAmbiguous.h:266
Definition: AhoCorasickAmbiguous.h:251
DepthHits depth_and_hits
depth of node in the tree and one bit if a needle ends in this node or any of its suffices
Definition: AhoCorasickAmbiguous.h:279
ACNode(const AA label, const uint8_t depth)
C'tor from an edge label (from parent to this node) and a depth in the tree.
Definition: AhoCorasickAmbiguous.h:256
uint8_t ChildCountType
Definition: AhoCorasickAmbiguous.h:272
ACNode()
Default C'tor.
Definition: AhoCorasickAmbiguous.h:253
a spin-off search path through the trie, which can deal with ambiguous AAs and mismatches
Definition: AhoCorasickAmbiguous.h:287
AA nextValidAA(const ACTrieState &state)
std::string::const_iterator it_query
position in query
Definition: AhoCorasickAmbiguous.h:301
Index tree_pos
position in trie
Definition: AhoCorasickAmbiguous.h:302
size_t textPos(const ACTrieState &state) const
Where in the text are we currently?
ACSpawn()=delete
No default C'tor.
ACSpawn(std::string::const_iterator query_pos, Index tree_pos, uint8_t max_aa, uint8_t max_mm, uint8_t max_prefix_loss)
C'tor with arguments.
Definition: AhoCorasickAmbiguous.h:315
std::vector< Hit > hits
current hits found
Definition: AhoCorasickAmbiguous.h:334
void setQuery(const std::string &haystack)
const std::string & getQuery() const
The current query.
std::string query_
current query ( = haystack = text)
Definition: AhoCorasickAmbiguous.h:339
std::string::const_iterator it_q_
position in query
Definition: AhoCorasickAmbiguous.h:340
size_t textPos() const
Where in the text are we currently?
friend ACSpawn
Definition: AhoCorasickAmbiguous.h:316
Index tree_pos
position in trie (for the master)
Definition: AhoCorasickAmbiguous.h:335
std::queue< ACSpawn > spawns
initial spawn points which are currently active and need processing
Definition: AhoCorasickAmbiguous.h:336
std::string::const_iterator textPosIt() const
Where in the text are we currently?
Definition: AhoCorasickAmbiguous.h:109
uint32_t T
Definition: AhoCorasickAmbiguous.h:110
T query_pos
Definition: AhoCorasickAmbiguous.h:115
T needle_index
Definition: AhoCorasickAmbiguous.h:112
T needle_length
Definition: AhoCorasickAmbiguous.h:114
bool operator==(const Hit &rhs) const
Definition: AhoCorasickAmbiguous.h:116
Hit()=default
Hit(T needle_index, T needle_length, T query_pos)
Definition: AhoCorasickAmbiguous.h:112
std::size_t operator()(OpenMS::Index const &s) const noexcept
Definition: AhoCorasickAmbiguous.h:240