OpenMS
Loading...
Searching...
No Matches
AASequence.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: Timo Sachsenberg $
6// $Authors: Andreas Bertsch, Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
17
18#include <vector>
19#include <iosfwd>
20#include <map>
21#include <functional>
22
23namespace OpenMS
24{
25
81
83
85
95 class OPENMS_DLLAPI AASequence final
96 {
97public:
98
99 class Iterator;
100
105 class OPENMS_DLLAPI ConstIterator final
106 {
107 public:
108 // TODO Iterator constructor for ConstIterator
109
110 typedef const Residue& const_reference;
112 typedef const Residue* const_pointer;
113 typedef std::vector<const Residue*>::difference_type difference_type;
115 typedef const Residue* pointer;
116 typedef std::random_access_iterator_tag iterator_category;
117
122 ConstIterator() = default;
123
125 ConstIterator(const std::vector<const Residue*>* vec_ptr, difference_type position)
126 : vector_{vec_ptr},
127 position_{position}
128 {
129 }
130
132 ConstIterator(const ConstIterator& rhs) = default;
133
136 vector_(rhs.vector_),
137 position_(rhs.position_)
138 {
139 }
140
142 ~ConstIterator() = default;
143
145
147 ConstIterator& operator=(const ConstIterator& rhs) = default;
148
154 {
155 return *(*vector_)[position_];
156 }
157
160 {
161 return (*vector_)[position_];
162 }
163
166 {
167 return ConstIterator(vector_, position_ + diff);
168 }
169
171 {
172 return position_ - rhs.position_;
173 }
174
177 {
178 return ConstIterator(vector_, position_ - diff);
179 }
180
182 bool operator==(const ConstIterator& rhs) const
183 {
184 return vector_ == rhs.vector_ && position_ == rhs.position_;
185 }
186
188 bool operator!=(const ConstIterator& rhs) const
189 {
190 return vector_ != rhs.vector_ || position_ != rhs.position_;
191 }
192
195 {
196 ++position_;
197 return *this;
198 }
199
202 {
203 --position_;
204 return *this;
205 }
206
208
209protected:
210
211 // pointer to the AASequence vector
212 const std::vector<const Residue*>* vector_ {};
213
214 // position in the AASequence vector
215 difference_type position_ {};
216 };
217
218
223 class OPENMS_DLLAPI Iterator final
224 {
225public:
226
228
229 typedef const Residue& const_reference;
231 typedef const Residue* const_pointer;
232 typedef const Residue* pointer;
233 typedef std::vector<const Residue*>::difference_type difference_type;
234
239 Iterator() = default;
240
242 Iterator(std::vector<const Residue*>* vec_ptr, difference_type position)
243 : vector_ {vec_ptr},
244 position_{position}
245 {
246 }
247
249 Iterator(const Iterator& rhs) = default;
250
252 ~Iterator() = default;
253
255
258 {
259 if (this != &rhs)
260 {
261 position_ = rhs.position_;
262 vector_ = rhs.vector_;
263 }
264 return *this;
265 }
266
272 {
273 return *(*vector_)[position_];
274 }
275
278 {
279 return (*vector_)[position_];
280 }
281
284 {
285 return (*vector_)[position_];
286 }
287
290 {
291 return Iterator(vector_, position_ + diff);
292 }
293
295 {
296 return position_ - rhs.position_;
297 }
298
301 {
302 return Iterator(vector_, position_ - diff);
303 }
304
306 bool operator==(const Iterator& rhs) const
307 {
308 return vector_ == rhs.vector_ && position_ == rhs.position_;
309 }
310
312 bool operator!=(const Iterator& rhs) const
313 {
314 return vector_ != rhs.vector_ || position_ != rhs.position_;
315 }
316
319 {
320 ++position_;
321 return *this;
322 }
323
326 {
327 --position_;
328 return *this;
329 }
330
332
333protected:
334
335 // pointer to the AASequence vector
336 std::vector<const Residue*>* vector_ {};
337
338 // position in the AASequence vector
339 difference_type position_ {};
340 };
341
345
347 AASequence() = default;
348
350 AASequence(const AASequence&) = default;
351
353 AASequence(AASequence&&) = default;
354
356 ~AASequence() = default;
358
360 AASequence& operator=(const AASequence&) = default;
361
364
366 bool empty() const;
367
371
383 std::string toString() const;
384
386 std::string toUnmodifiedString() const;
387
396 std::string toUniModString() const;
397
416 std::string toBracketString(bool integer_mass = true,
417 bool mass_delta = false,
418 const std::vector<std::string> & fixed_modifications = std::vector<std::string>()) const;
419
422 void setModification(Size index, const std::string& modification);
423
425 void setModification(Size index, const Residue* modification);
426
428 void setModification(Size index, const ResidueModification* modification);
429
432 void setModification(Size index, const ResidueModification& modification);
433
435 void setModificationByDiffMonoMass(Size index, double diffMonoMass);
436
439 void setNTerminalModification(const std::string& modification);
440
443
446
448 void setNTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term);
449
451 const std::string& getNTerminalModificationName() const;
452
455
458 void setCTerminalModification(const std::string& modification);
459
462
465
467 void setCTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term);
468
470 const std::string& getCTerminalModificationName() const;
471
474
476 const Residue& getResidue(Size index) const;
477
479 EmpiricalFormula getFormula(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
480
482 double getAverageWeight(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
483
488 double getMonoWeight(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
489
495 double getMZ(Int charge, Residue::ResidueType type = Residue::Full) const;
496
498 const Residue& operator[](Size index) const;
499
501 AASequence operator+(const AASequence& peptide) const;
502
505
507 AASequence operator+(const Residue* residue) const;
508
511
513 Size size() const;
514
517
520
522 AASequence getSubsequence(Size index, UInt number) const;
523
525 void getAAFrequencies(std::map<std::string, Size>& frequency_table) const;
526
528
533 bool has(const Residue& residue) const;
534
537 bool hasSubsequence(const AASequence& peptide) const;
538
541 bool hasPrefix(const AASequence& peptide) const;
542
545 bool hasSuffix(const AASequence& peptide) const;
546
549
552
554 bool isModified() const;
555
557 bool operator==(const AASequence& rhs) const;
558
560 bool operator<(const AASequence& rhs) const;
561
563 bool operator!=(const AASequence& rhs) const;
565
569 inline Iterator begin() { return Iterator(&peptide_, 0); }
570
571 inline ConstIterator begin() const { return ConstIterator(&peptide_, 0); }
572
573 inline Iterator end() { return Iterator(&peptide_, (Int) peptide_.size()); }
574
575 inline ConstIterator end() const { return ConstIterator(&peptide_, (Int) peptide_.size()); }
577
582 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AASequence& peptide);
583
585 friend OPENMS_DLLAPI std::istream& operator>>(std::istream& is, const AASequence& peptide);
587
596 static AASequence fromString(const std::string& s,
597 bool permissive = true);
598
607 static AASequence fromString(const char* s,
608 bool permissive = true);
609
612 explicit AASequence(const std::string& s);
613
616 explicit AASequence(const char* s);
617
621 explicit AASequence(const std::string& s, bool permissive);
622
626 explicit AASequence(const char* s, bool permissive);
627
628 protected:
631
632
633 std::vector<const Residue*> peptide_;
634
635 const ResidueModification* n_term_mod_ = nullptr;
636
637 const ResidueModification* c_term_mod_ = nullptr;
638
652 static std::string::const_iterator parseModRoundBrackets_(const std::string::const_iterator str_it,
653 const std::string& str,
654 AASequence& aas,
655 const ResidueModification::TermSpecificity& specificity);
656
679 static std::string::const_iterator parseModSquareBrackets_(const std::string::const_iterator str_it,
680 const std::string& str,
681 AASequence& aas,
682 const ResidueModification::TermSpecificity& specificity);
683
684 static void parseString_(const std::string& peptide, AASequence& aas,
685 bool permissive = true);
686 };
687
688 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AASequence& peptide);
689
690 OPENMS_DLLAPI std::istream& operator>>(std::istream& os, const AASequence& peptide);
691
692} // namespace OpenMS
693
694// Hash function specialization for AASequence
695// Placed in std namespace to allow use with std::unordered_map/set
696namespace std
697{
712 template<>
713 struct hash<OpenMS::AASequence>
714 {
715 std::size_t operator()(const OpenMS::AASequence& seq) const noexcept
716 {
717 std::size_t seed = 0;
718
719 // Hash each residue
720 for (const auto& residue : seq)
721 {
722 // Hash one-letter code (single character, fast)
723 const std::string& olc = residue.getOneLetterCode();
724 if (!olc.empty())
725 {
727 }
728
729 // Hash modification if present
730 const OpenMS::ResidueModification* mod = residue.getModification();
731 if (mod != nullptr)
732 {
733 // Use full ID for portability (e.g., "Oxidation (M)")
734 // std::string inherits from std::string, no copy needed
736 }
737 }
738
739 // Hash N-terminal modification if present
740 const OpenMS::ResidueModification* n_mod = seq.getNTerminalModification();
741 if (n_mod != nullptr)
742 {
743 // Use a different seed offset for N-term to distinguish from C-term
744 std::size_t n_hash = OpenMS::fnv1a_hash_string(n_mod->getFullId());
745 OpenMS::hash_combine(seed, n_hash ^ 0x4e5445524dULL); // "NTERM" in hex-like
746 }
747
748 // Hash C-terminal modification if present
749 const OpenMS::ResidueModification* c_mod = seq.getCTerminalModification();
750 if (c_mod != nullptr)
751 {
752 // Use a different seed offset for C-term
753 std::size_t c_hash = OpenMS::fnv1a_hash_string(c_mod->getFullId());
754 OpenMS::hash_combine(seed, c_hash ^ 0x435445524dULL); // "CTERM" in hex-like
755 }
756
757 return seed;
758 }
759 };
760} // namespace std
761
762
ConstIterator for AASequence.
Definition AASequence.h:106
const_pointer operator->() const
dereference operator
Definition AASequence.h:159
ConstIterator(const ConstIterator &rhs)=default
copy constructor
const ConstIterator operator-(difference_type diff) const
backward jump operator
Definition AASequence.h:176
Residue value_type
Definition AASequence.h:114
ConstIterator(const std::vector< const Residue * > *vec_ptr, difference_type position)
detailed constructor with pointer to the vector and offset position
Definition AASequence.h:125
bool operator!=(const ConstIterator &rhs) const
inequality operator
Definition AASequence.h:188
ConstIterator(const AASequence::Iterator &rhs)
copy constructor from Iterator
Definition AASequence.h:135
const ConstIterator operator+(difference_type diff) const
forward jump operator
Definition AASequence.h:165
~ConstIterator()=default
destructor
ConstIterator()=default
default constructor
ConstIterator & operator--()
decrement operator
Definition AASequence.h:201
std::random_access_iterator_tag iterator_category
Definition AASequence.h:116
bool operator==(const ConstIterator &rhs) const
equality comparator
Definition AASequence.h:182
const std::vector< const Residue * > * vector_
Definition AASequence.h:212
const Residue * const_pointer
Definition AASequence.h:112
difference_type position_
Definition AASequence.h:215
std::vector< constResidue * >::difference_type difference_type
Definition AASequence.h:113
const Residue * pointer
Definition AASequence.h:115
Residue & reference
Definition AASequence.h:111
const Residue & const_reference
Definition AASequence.h:110
const_reference operator*() const
dereference operator
Definition AASequence.h:153
ConstIterator & operator++()
increment operator
Definition AASequence.h:194
ConstIterator & operator=(const ConstIterator &rhs)=default
assignment operator
difference_type operator-(ConstIterator rhs) const
Definition AASequence.h:170
Iterator class for AASequence.
Definition AASequence.h:224
const_pointer operator->() const
dereference operator
Definition AASequence.h:277
Iterator()=default
default constructor
pointer operator->()
mutable dereference operator
Definition AASequence.h:283
const Iterator operator+(difference_type diff) const
forward jump operator
Definition AASequence.h:289
Iterator & operator=(const Iterator &rhs)
assignment operator
Definition AASequence.h:257
Iterator(const Iterator &rhs)=default
copy constructor
std::vector< const Residue * > * vector_
Definition AASequence.h:336
difference_type operator-(Iterator rhs) const
Definition AASequence.h:294
~Iterator()=default
destructor
Iterator(std::vector< const Residue * > *vec_ptr, difference_type position)
detailed constructor with pointer to the vector and offset position
Definition AASequence.h:242
bool operator==(const Iterator &rhs) const
equality comparator
Definition AASequence.h:306
const Residue * const_pointer
Definition AASequence.h:231
difference_type position_
Definition AASequence.h:339
std::vector< constResidue * >::difference_type difference_type
Definition AASequence.h:233
const Residue * pointer
Definition AASequence.h:232
Residue & reference
Definition AASequence.h:230
const Residue & const_reference
Definition AASequence.h:229
const_reference operator*() const
dereference operator
Definition AASequence.h:271
bool operator!=(const Iterator &rhs) const
inequality operator
Definition AASequence.h:312
Iterator & operator++()
increment operator
Definition AASequence.h:318
Iterator & operator--()
decrement operator
Definition AASequence.h:325
const Iterator operator-(difference_type diff) const
backward jump operator
Definition AASequence.h:300
Definition AASequence.h:96
static void parseString_(const std::string &peptide, AASequence &aas, bool permissive=true)
void setModification(Size index, const Residue *modification)
sets the modification of AA at index by providing an already, potentially modified residue
void setModificationByDiffMonoMass(Size index, double diffMonoMass)
modifies the residue at index in the sequence and potentially in the ResidueDB
const ResidueModification * getCTerminalModification() const
returns a pointer to the C-terminal modification, or zero if none is set
bool hasNTerminalModification() const
predicate which is true if the peptide is N-term modified
const Residue & operator[](Size index) const
returns a pointer to the residue at given position
AASequence & operator+=(const Residue *)
adds the residues of a peptide
static AASequence fromString(const std::string &s, bool permissive=true)
create AASequence object by parsing an OpenMS string
std::string toString() const
returns the peptide as string with modifications embedded in brackets
double getMZ(Int charge, Residue::ResidueType type=Residue::Full) const
Iterator begin()
Definition AASequence.h:569
friend std::ostream & operator<<(std::ostream &os, const AASequence &peptide)
writes a peptide to an output stream
static std::string::const_iterator parseModRoundBrackets_(const std::string::const_iterator str_it, const std::string &str, AASequence &aas, const ResidueModification::TermSpecificity &specificity)
Parses modifications in round brackets (an identifier)
EmpiricalFormula getFormula(Residue::ResidueType type=Residue::Full, Int charge=0) const
returns the formula of the peptide
const Residue & getResidue(Size index) const
returns a pointer to the residue at position index
std::vector< const Residue * > peptide_
Definition AASequence.h:633
AASequence(const std::string &s, bool permissive)
constructor from String
void setNTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term)
sets the N-terminal modification by the monoisotopic mass difference it introduces (creates a "user-d...
AASequence(AASequence &&)=default
Move constructor.
std::string toBracketString(bool integer_mass=true, bool mass_delta=false, const std::vector< std::string > &fixed_modifications=std::vector< std::string >()) const
create a TPP compatible string of the modified sequence using bracket notation.
double getAverageWeight(Residue::ResidueType type=Residue::Full, Int charge=0) const
returns the average weight of the peptide
void setCTerminalModification(const std::string &modification)
void getAAFrequencies(std::map< std::string, Size > &frequency_table) const
compute frequency table of amino acids
AASequence(const AASequence &)=default
Copy constructor.
void setModification(Size index, const ResidueModification *modification)
sets the modification of AA at index by providing a pointer to a ResidueModification object found in ...
AASequence(const char *s)
constructor from C string
AASequence getPrefix(Size index) const
returns a peptide sequence of the first index residues
bool empty() const
check if sequence is empty
AASequence()=default
Default constructor.
void setModification(Size index, const ResidueModification &modification)
ConstIterator end() const
Definition AASequence.h:575
bool operator==(const AASequence &rhs) const
equality operator. Two sequences are equal iff all amino acids including PTMs are equal
void setCTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term)
sets the C-terminal modification by the monoisotopic mass difference it introduces (creates a "user-d...
bool isModified() const
returns true if any of the residues or termini are modified
double getMonoWeight(Residue::ResidueType type=Residue::Full, Int charge=0) const
void setCTerminalModification(const ResidueModification &mod)
sets the C-terminal modification (copies and adds to database if not present)
static AASequence fromString(const char *s, bool permissive=true)
create AASequence object by parsing a C string (character array)
AASequence & operator=(const AASequence &)=default
Assignment operator.
void setCTerminalModification(const ResidueModification *modification)
sets the C-terminal modification (must be present in the database)
~AASequence()=default
Destructor.
AASequence operator+(const AASequence &peptide) const
adds the residues of the peptide
AASequence & operator=(AASequence &&)=default
Move assignment operator.
bool hasCTerminalModification() const
predicate which is true if the peptide is C-term modified
bool has(const Residue &residue) const
returns true if the peptide contains the given residue
AASequence getSubsequence(Size index, UInt number) const
returns a peptide sequence of number residues, beginning at position index
const std::string & getNTerminalModificationName() const
returns the name (ID) of the N-terminal modification, or an empty string if none is set
bool hasSubsequence(const AASequence &peptide) const
bool operator!=(const AASequence &rhs) const
inequality operator. Complement of equality operator.
AASequence operator+(const Residue *residue) const
adds the residues of the peptide
AASequence getSuffix(Size index) const
returns a peptide sequence of the last index residues
Iterator end()
Definition AASequence.h:573
std::string toUniModString() const
returns the peptide as string with UniMod-style modifications embedded in brackets
void setNTerminalModification(const ResidueModification *modification)
sets the N-terminal modification
void validateSatelliteIon_(Residue::ResidueType type) const
Reject satellite ions whose cleavage residue has no supported loss.
std::string toUnmodifiedString() const
returns the peptide as string without any modifications or (e.g., "PEPTIDER")
AASequence(const char *s, bool permissive)
constructor from C string
void setNTerminalModification(const std::string &modification)
bool hasPrefix(const AASequence &peptide) const
bool operator<(const AASequence &rhs) const
lesser than operator which compares the C-term mods, sequence including PTMS and N-term mods; can be ...
Size size() const
returns the number of residues
const std::string & getCTerminalModificationName() const
returns the name (ID) of the C-terminal modification, or an empty string if none is set
static std::string::const_iterator parseModSquareBrackets_(const std::string::const_iterator str_it, const std::string &str, AASequence &aas, const ResidueModification::TermSpecificity &specificity)
Parses modifications in square brackets (a mass)
const ResidueModification * getNTerminalModification() const
returns a pointer to the N-terminal modification, or zero if none is set
ConstIterator begin() const
Definition AASequence.h:571
friend std::istream & operator>>(std::istream &is, const AASequence &peptide)
reads a peptide from an input stream
AASequence & operator+=(const AASequence &)
adds the residues of a peptide
AASequence(const std::string &s)
constructor from String
void setNTerminalModification(const ResidueModification &mod)
sets the N-terminal modification (copies and adds to database if not present)
bool hasSuffix(const AASequence &peptide) const
void setModification(Size index, const std::string &modification)
Representation of an empirical formula.
Definition EmpiricalFormula.h:62
Representation of a modification on an amino acid residue.
Definition ResidueModification.h:55
TermSpecificity
Position where the modification is allowed to occur.
Definition ResidueModification.h:74
const std::string & getFullId() const
returns the full identifier of the mod (Unimod accession + origin, if available)
Representation of an amino acid residue.
Definition Residue.h:41
ResidueType
Definition Residue.h:153
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
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 hash_char(char c) noexcept
Hash for a character.
Definition HashUtils.h:119
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
std::istream & operator>>(std::istream &os, const AASequence &peptide)
STL namespace.
std::size_t operator()(const OpenMS::AASequence &seq) const noexcept
Definition AASequence.h:715