OpenMS
Loading...
Searching...
No Matches
NASequence.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Samuel Wein $
6// $Authors: Samuel Wein, Timo Sachsenberg, Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
17#include <functional>
18#include <iosfwd>
19#include <vector>
20
21namespace OpenMS
22{
36 class OPENMS_DLLAPI NASequence
37 {
42 public:
44 { //< NB: Not all fragments types are valid for all residue types, this class should probably get split
45 Full = 0,
65 SizeOfNASFragmentType
66 };
67
69
70 class Iterator;
71
77 class OPENMS_DLLAPI ConstIterator
78 {
79 public:
83 typedef const value_type* const_pointer;
84 typedef std::vector<const value_type*>::difference_type difference_type;
85 typedef const value_type* pointer;
86 typedef std::random_access_iterator_tag iterator_category;
87
92 ConstIterator() = default;
93
95 ConstIterator(const std::vector<const Ribonucleotide*>* vec_ptr, difference_type position)
96 {
97 vector_ = vec_ptr;
98 position_ = position;
99 }
100
102 ConstIterator(const ConstIterator& rhs) : vector_(rhs.vector_), position_(rhs.position_)
103 {
104 }
105
107 ConstIterator(const NASequence::Iterator& rhs) : vector_(rhs.vector_), position_(rhs.position_)
108 {
109 }
110
113 {
114 }
115
117
120 {
121 if (this != &rhs)
122 {
123 position_ = rhs.position_;
124 vector_ = rhs.vector_;
125 }
126 return *this;
127 }
128
134 {
135 return *(*vector_)[position_];
136 }
137
140 {
141 return (*vector_)[position_];
142 }
143
146 {
147 return ConstIterator(vector_, position_ + diff);
148 }
149
151 {
152 return position_ - rhs.position_;
153 }
154
157 {
158 return ConstIterator(vector_, position_ - diff);
159 }
160
162 bool operator==(const ConstIterator& rhs) const
163 {
164 return (std::tie(vector_, position_) == std::tie(rhs.vector_, rhs.position_));
165 }
166
168 bool operator!=(const ConstIterator& rhs) const
169 {
170 return !(operator==(rhs));
171 }
172
175 {
176 ++position_;
177 return *this;
178 }
179
182 {
183 --position_;
184 return *this;
185 }
186
188
189 protected:
190 // pointer to the vector
191 const std::vector<const Ribonucleotide*>* vector_;
192
193 // position in the vector
195 };
196
197
203 class OPENMS_DLLAPI Iterator
204 {
205 public:
207
211 typedef const value_type* const_pointer;
212 typedef const value_type* pointer;
213 typedef std::vector<const value_type*>::difference_type difference_type;
214
218 Iterator() = default;
219
221 Iterator(std::vector<const Ribonucleotide*>* vec_ptr, difference_type position)
222 {
223 vector_ = vec_ptr;
224 position_ = position;
225 }
226
228 Iterator(const Iterator& rhs) : vector_(rhs.vector_), position_(rhs.position_)
229 {
230 }
231
233 virtual ~Iterator()
234 {
235 }
236
238
241 {
242 if (this != &rhs)
243 {
244 position_ = rhs.position_;
245 vector_ = rhs.vector_;
246 }
247 return *this;
248 }
249
255 {
256 return *(*vector_)[position_];
257 }
258
261 {
262 return (*vector_)[position_];
263 }
264
267 {
268 return (*vector_)[position_];
269 }
270
273 {
274 return Iterator(vector_, position_ + diff);
275 }
276
278 {
279 return position_ - rhs.position_;
280 }
281
284 {
285 return Iterator(vector_, position_ - diff);
286 }
287
289 bool operator==(const Iterator& rhs) const
290 {
291 return (std::tie(vector_, position_) == std::tie(rhs.vector_, rhs.position_));
292 }
293
295 bool operator!=(const Iterator& rhs) const
296 {
297 return !this->operator==(rhs);
298 }
299
302 {
303 ++position_;
304 return *this;
305 }
306
309 {
310 --position_;
311 return *this;
312 }
313
315
316 protected:
317 std::vector<const Ribonucleotide*>* vector_;
318
319 // position in the vector
321 };
322
323 public:
324 /*
325 * Default constructors and assignment operators.
326 */
327 NASequence() = default;
328 NASequence(const NASequence&) = default;
329 NASequence(NASequence&&) = default;
330 NASequence& operator=(const NASequence&) & = default;
332
334 NASequence(std::vector<const Ribonucleotide*> s, const RibonucleotideChainEnd* five_prime, const RibonucleotideChainEnd* three_prime);
335
336 virtual ~NASequence() = default;
337
338 bool operator==(const NASequence& rhs) const;
339 bool operator!=(const NASequence& rhs) const;
340 bool operator<(const NASequence& rhs) const;
341
343 void setSequence(const std::vector<const Ribonucleotide*>& seq);
344
345 const std::vector<const Ribonucleotide*>& getSequence() const
346 {
347 return seq_;
348 }
349
350 std::vector<const Ribonucleotide*>& getSequence()
351 {
352 return seq_;
353 }
354
356 void set(size_t index, const Ribonucleotide* r);
357
358 const Ribonucleotide* get(size_t index)
359 {
360 return seq_[index];
361 }
362
364 inline const Ribonucleotide*& operator[](size_t index)
365 {
366 return seq_[index];
367 }
368
369 inline const Ribonucleotide* const& operator[](size_t index) const
370 {
371 return seq_[index];
372 }
373
374 bool empty() const;
375 size_t size() const;
376 void clear();
377
379 bool hasFivePrimeMod() const;
382 bool hasThreePrimeMod() const;
385
388 {
389 return Iterator(&seq_, 0);
390 }
391
392 inline ConstIterator begin() const
393 {
394 return ConstIterator(&seq_, 0);
395 }
396
397 inline Iterator end()
398 {
399 return Iterator(&seq_, (Int)seq_.size());
400 }
401
402 inline ConstIterator end() const
403 {
404 return ConstIterator(&seq_, (Int)seq_.size());
405 }
406
407 inline ConstIterator cbegin() const
408 {
409 return ConstIterator(&seq_, 0);
410 }
411
412 inline ConstIterator cend() const
413 {
414 return ConstIterator(&seq_, (Int)seq_.size());
415 }
416
418
427 double getMonoWeight(NASFragmentType type = Full, Int charge = 0) const;
428
437 double getAverageWeight(NASFragmentType type = Full, Int charge = 0) const;
438
447 EmpiricalFormula getFormula(NASFragmentType type = Full, Int charge = 0) const;
448
456 NASequence getPrefix(Size length) const;
457
465 NASequence getSuffix(Size length) const;
466
475 NASequence getSubsequence(Size start = 0, Size length = Size(-1)) const;
476
493 NASequence getReversed(bool keep_five_prime = false, bool keep_three_prime = false) const;
494
514 NASequence getShuffled(Math::RandomShuffler& shuffler, double identity_threshold = 0.5,
515 int max_attempts = 30,
516 bool keep_five_prime = false, bool keep_three_prime = false) const;
517
530 float getSequenceIdentity(const NASequence& other) const;
531
539 static NASequence fromString(const std::string& s);
540
544 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const NASequence& seq);
545
553 static NASequence fromString(const char* s);
554
555 std::string toString() const;
556
557 private:
558 // TODO: query RNA / DNA depending on type
559 static void parseString_(const std::string& s, NASequence& nas);
560
570 // TODO: query RNA / DNA depending on type
571 static std::string::const_iterator parseMod_(const std::string::const_iterator str_it, const std::string& str, NASequence& nas);
572
573 std::vector<const Ribonucleotide*> seq_;
574
575 const RibonucleotideChainEnd* five_prime_ = nullptr;
576 const RibonucleotideChainEnd* three_prime_ = nullptr;
577 };
578
579} // namespace OpenMS
580
581// Hash function specialization for NASequence
582// Placed in std namespace to allow use with std::unordered_map/set
583namespace std
584{
603 template<>
604 struct hash<OpenMS::NASequence>
605 {
606 std::size_t operator()(const OpenMS::NASequence& seq) const noexcept
607 {
608 std::size_t seed = 0;
609
610 // Hash each ribonucleotide in the sequence
611 for (const auto& ribo : seq)
612 {
613 // Hash the code (stable identifier)
614 const std::string& code = ribo.getCode();
616 }
617
618 // Hash 5' terminal modification if present
619 const OpenMS::RibonucleotideChainEnd* five_prime = seq.getFivePrimeMod();
620 if (five_prime != nullptr)
621 {
622 // Use a different seed offset for 5' to distinguish from 3'
623 std::size_t five_hash = OpenMS::fnv1a_hash_string(five_prime->getCode());
624 OpenMS::hash_combine(seed, five_hash ^ 0x355052494dULL); // "5PRIM" in hex-like
625 }
626
627 // Hash 3' terminal modification if present
628 const OpenMS::RibonucleotideChainEnd* three_prime = seq.getThreePrimeMod();
629 if (three_prime != nullptr)
630 {
631 // Use a different seed offset for 3'
632 std::size_t three_hash = OpenMS::fnv1a_hash_string(three_prime->getCode());
633 OpenMS::hash_combine(seed, three_hash ^ 0x335052494dULL); // "3PRIM" in hex-like
634 }
635
636 return seed;
637 }
638 };
639} // namespace std
Representation of an empirical formula.
Definition EmpiricalFormula.h:62
Portable random shuffle with a reproducible, platform-independent random sequence.
Definition MathFunctions.h:479
ConstIterator of NASequence class.
Definition NASequence.h:78
ConstIterator(const std::vector< const Ribonucleotide * > *vec_ptr, difference_type position)
detailed constructor with pointer to the vector and offset position
Definition NASequence.h:95
const_pointer operator->() const
dereference operator
Definition NASequence.h:139
const value_type * const_pointer
Definition NASequence.h:83
const ConstIterator operator-(difference_type diff) const
backward jump operator
Definition NASequence.h:156
bool operator!=(const ConstIterator &rhs) const
inequality operator
Definition NASequence.h:168
const ConstIterator operator+(difference_type diff) const
forward jump operator
Definition NASequence.h:145
ConstIterator()=default
default constructor
ConstIterator & operator--()
decrement operator
Definition NASequence.h:181
std::random_access_iterator_tag iterator_category
Definition NASequence.h:86
bool operator==(const ConstIterator &rhs) const
equality comparator
Definition NASequence.h:162
Ribonucleotide value_type
Definition NASequence.h:80
std::vector< constvalue_type * >::difference_type difference_type
Definition NASequence.h:84
ConstIterator(const NASequence::Iterator &rhs)
copy constructor from Iterator
Definition NASequence.h:107
difference_type position_
Definition NASequence.h:194
ConstIterator(const ConstIterator &rhs)
copy constructor
Definition NASequence.h:102
value_type & reference
Definition NASequence.h:82
const std::vector< const Ribonucleotide * > * vector_
Definition NASequence.h:191
const value_type * pointer
Definition NASequence.h:85
const_reference operator*() const
dereference operator
Definition NASequence.h:133
virtual ~ConstIterator()
destructor
Definition NASequence.h:112
ConstIterator & operator++()
increment operator
Definition NASequence.h:174
ConstIterator & operator=(const ConstIterator &rhs)
assignment operator
Definition NASequence.h:119
difference_type operator-(ConstIterator rhs) const
Definition NASequence.h:150
const value_type & const_reference
Definition NASequence.h:81
Iterator of NASequence class.
Definition NASequence.h:204
const_pointer operator->() const
dereference operator
Definition NASequence.h:260
const value_type * const_pointer
Definition NASequence.h:211
pointer operator->()
mutable dereference operator
Definition NASequence.h:266
const Iterator operator+(difference_type diff) const
forward jump operator
Definition NASequence.h:272
Iterator & operator=(const Iterator &rhs)
assignment operator
Definition NASequence.h:240
virtual ~Iterator()
destructor
Definition NASequence.h:233
Iterator(std::vector< const Ribonucleotide * > *vec_ptr, difference_type position)
detailed constructor with pointer to the vector and offset position
Definition NASequence.h:221
difference_type operator-(Iterator rhs) const
Definition NASequence.h:277
Ribonucleotide value_type
Definition NASequence.h:208
bool operator==(const Iterator &rhs) const
equality comparator
Definition NASequence.h:289
std::vector< constvalue_type * >::difference_type difference_type
Definition NASequence.h:213
difference_type position_
Definition NASequence.h:320
value_type & reference
Definition NASequence.h:210
Iterator(const Iterator &rhs)
copy constructor
Definition NASequence.h:228
const value_type * pointer
Definition NASequence.h:212
const_reference operator*() const
dereference operator
Definition NASequence.h:254
bool operator!=(const Iterator &rhs) const
inequality operator
Definition NASequence.h:295
Iterator & operator++()
increment operator
Definition NASequence.h:301
Iterator & operator--()
decrement operator
Definition NASequence.h:308
const Iterator operator-(difference_type diff) const
backward jump operator
Definition NASequence.h:283
std::vector< const Ribonucleotide * > * vector_
Definition NASequence.h:317
const value_type & const_reference
Definition NASequence.h:209
Representation of a nucleic acid sequence.
Definition NASequence.h:37
const Ribonucleotide * get(size_t index)
Definition NASequence.h:358
ConstIterator cend() const
Definition NASequence.h:412
virtual ~NASequence()=default
NASequence & operator=(NASequence &&) &=default
Move assignment operator.
static std::string::const_iterator parseMod_(const std::string::const_iterator str_it, const std::string &str, NASequence &nas)
Parses modifications in square brackets.
bool operator<(const NASequence &rhs) const
less operator
static NASequence fromString(const std::string &s)
create NASequence object by parsing an OpenMS string
bool hasThreePrimeMod() const
NASequence getSuffix(Size length) const
Return sequence suffix of the given length (not start index!)
void setSequence(const std::vector< const Ribonucleotide * > &seq)
getter / setter for sequence
std::string toString() const
Iterator begin()
iterators
Definition NASequence.h:387
NASequence getShuffled(Math::RandomShuffler &shuffler, double identity_threshold=0.5, int max_attempts=30, bool keep_five_prime=false, bool keep_three_prime=false) const
Return a shuffled copy of this sequence with identity quality control.
size_t size() const
bool operator==(const NASequence &rhs) const
destructor
bool hasFivePrimeMod() const
5' and 3' modifications
static void parseString_(const std::string &s, NASequence &nas)
double getMonoWeight(NASFragmentType type=Full, Int charge=0) const
utility functions
void setThreePrimeMod(const RibonucleotideChainEnd *r)
NASequence getPrefix(Size length) const
Return sequence prefix of the given length (not end index!)
std::vector< const Ribonucleotide * > & getSequence()
Definition NASequence.h:350
const Ribonucleotide *const & operator[](size_t index) const
Definition NASequence.h:369
friend std::ostream & operator<<(std::ostream &os, const NASequence &seq)
NASequence()=default
const std::vector< const Ribonucleotide * > & getSequence() const
Definition NASequence.h:345
bool empty() const
ConstIterator end() const
Definition NASequence.h:402
bool operator!=(const NASequence &rhs) const
not quality
NASequence(std::vector< const Ribonucleotide * > s, const RibonucleotideChainEnd *five_prime, const RibonucleotideChainEnd *three_prime)
full constructor
void set(size_t index, const Ribonucleotide *r)
getter / setter for ribonucleotide elements (easily wrapped using pyOpenMS)
float getSequenceIdentity(const NASequence &other) const
Calculate sequence identity between this and another NASequence.
static NASequence fromString(const char *s)
create NASequence object by parsing a C string (character array)
NASequence getReversed(bool keep_five_prime=false, bool keep_three_prime=false) const
Return a reversed copy of this sequence.
const Ribonucleotide *& operator[](size_t index)
getter / setter for sequence elements (C++ container style)
Definition NASequence.h:364
EmpiricalFormula getFormula(NASFragmentType type=Full, Int charge=0) const
Get the formula for a NASequence.
NASFragmentType
an enum of all possible fragment ion types
Definition NASequence.h:44
@ AminusB
A ion with base loss, added for nucleic acid support.
Definition NASequence.h:63
@ YIon
MS:1001220 peptide bond up to the C-terminus.
Definition NASequence.h:53
@ XIon
MS:1001228 amide/C-alpha bond up to the C-terminus.
Definition NASequence.h:52
@ ZIon
MS:1001230 C-alpha/carbonyl carbon bond.
Definition NASequence.h:54
@ WIon
W ion, added for nucleic acid support.
Definition NASequence.h:62
@ BIonMinusH20
MS:1001222 b ion without water.
Definition NASequence.h:56
@ BIonMinusNH3
MS:1001232 b ion without ammonia.
Definition NASequence.h:58
@ AIon
MS:1001229 N-terminus up to the C-alpha/carbonyl carbon bond.
Definition NASequence.h:49
@ Precursor
MS:1001523 Precursor ion.
Definition NASequence.h:55
@ YIonMinusH20
MS:1001223 y ion without water.
Definition NASequence.h:57
@ NonIdentified
MS:1001240 Non-identified ion.
Definition NASequence.h:60
@ BIon
MS:1001224 N-terminus up to the peptide bond.
Definition NASequence.h:50
@ ThreePrime
only 3' terminus
Definition NASequence.h:48
@ CIon
MS:1001231 N-terminus up to the amide/C-alpha bond.
Definition NASequence.h:51
@ YIonMinusNH3
MS:1001233 y ion without ammonia.
Definition NASequence.h:59
@ Internal
internal, without any termini
Definition NASequence.h:46
@ Unannotated
no stored annotation
Definition NASequence.h:61
@ FivePrime
only 5' terminus
Definition NASequence.h:47
@ DIon
D ion, added for nucleic acid support.
Definition NASequence.h:64
NASequence & operator=(const NASequence &) &=default
Copy assignment operator.
ConstIterator cbegin() const
Definition NASequence.h:407
double getAverageWeight(NASFragmentType type=Full, Int charge=0) const
Get the Average Weight of a NASequence. NB returns the uncharged mass + or - proton masses to match t...
Iterator end()
Definition NASequence.h:397
NASequence getSubsequence(Size start=0, Size length=Size(-1)) const
Return subsequence with given starting position and length.
std::vector< const Ribonucleotide * > seq_
Definition NASequence.h:573
void setFivePrimeMod(const RibonucleotideChainEnd *r)
ConstIterator begin() const
Definition NASequence.h:392
const RibonucleotideChainEnd * getThreePrimeMod() const
const RibonucleotideChainEnd * getFivePrimeMod() const
NASequence(NASequence &&)=default
Move constructor.
NASequence(const NASequence &)=default
default constructor
Representation of a ribonucleotide (modified or unmodified)
Definition Ribonucleotide.h:28
const std::string getCode() const
Return the short name.
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
std::size_t operator()(const OpenMS::NASequence &seq) const noexcept
Definition NASequence.h:606