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
92 class OPENMS_DLLAPI AASequence final
93 {
94public:
95
96 class Iterator;
97
102 class OPENMS_DLLAPI ConstIterator final
103 {
104 public:
105 // TODO Iterator constructor for ConstIterator
106
107 typedef const Residue& const_reference;
109 typedef const Residue* const_pointer;
110 typedef std::vector<const Residue*>::difference_type difference_type;
112 typedef const Residue* pointer;
113 typedef std::random_access_iterator_tag iterator_category;
114
119 ConstIterator() = default;
120
122 ConstIterator(const std::vector<const Residue*>* vec_ptr, difference_type position)
123 : vector_{vec_ptr},
124 position_{position}
125 {
126 }
127
129 ConstIterator(const ConstIterator& rhs) = default;
130
133 vector_(rhs.vector_),
134 position_(rhs.position_)
135 {
136 }
137
139 ~ConstIterator() = default;
140
142
144 ConstIterator& operator=(const ConstIterator& rhs) = default;
145
151 {
152 return *(*vector_)[position_];
153 }
154
157 {
158 return (*vector_)[position_];
159 }
160
163 {
164 return ConstIterator(vector_, position_ + diff);
165 }
166
168 {
169 return position_ - rhs.position_;
170 }
171
174 {
175 return ConstIterator(vector_, position_ - diff);
176 }
177
179 bool operator==(const ConstIterator& rhs) const
180 {
181 return vector_ == rhs.vector_ && position_ == rhs.position_;
182 }
183
185 bool operator!=(const ConstIterator& rhs) const
186 {
187 return vector_ != rhs.vector_ || position_ != rhs.position_;
188 }
189
192 {
193 ++position_;
194 return *this;
195 }
196
199 {
200 --position_;
201 return *this;
202 }
203
205
206protected:
207
208 // pointer to the AASequence vector
209 const std::vector<const Residue*>* vector_ {};
210
211 // position in the AASequence vector
212 difference_type position_ {};
213 };
214
215
220 class OPENMS_DLLAPI Iterator final
221 {
222public:
223
225
226 typedef const Residue& const_reference;
228 typedef const Residue* const_pointer;
229 typedef const Residue* pointer;
230 typedef std::vector<const Residue*>::difference_type difference_type;
231
236 Iterator() = default;
237
239 Iterator(std::vector<const Residue*>* vec_ptr, difference_type position)
240 : vector_ {vec_ptr},
241 position_{position}
242 {
243 }
244
246 Iterator(const Iterator& rhs) = default;
247
249 ~Iterator() = default;
250
252
255 {
256 if (this != &rhs)
257 {
258 position_ = rhs.position_;
259 vector_ = rhs.vector_;
260 }
261 return *this;
262 }
263
269 {
270 return *(*vector_)[position_];
271 }
272
275 {
276 return (*vector_)[position_];
277 }
278
281 {
282 return (*vector_)[position_];
283 }
284
287 {
288 return Iterator(vector_, position_ + diff);
289 }
290
292 {
293 return position_ - rhs.position_;
294 }
295
298 {
299 return Iterator(vector_, position_ - diff);
300 }
301
303 bool operator==(const Iterator& rhs) const
304 {
305 return vector_ == rhs.vector_ && position_ == rhs.position_;
306 }
307
309 bool operator!=(const Iterator& rhs) const
310 {
311 return vector_ != rhs.vector_ || position_ != rhs.position_;
312 }
313
316 {
317 ++position_;
318 return *this;
319 }
320
323 {
324 --position_;
325 return *this;
326 }
327
329
330protected:
331
332 // pointer to the AASequence vector
333 std::vector<const Residue*>* vector_ {};
334
335 // position in the AASequence vector
336 difference_type position_ {};
337 };
338
342
344 AASequence() = default;
345
347 AASequence(const AASequence&) = default;
348
350 AASequence(AASequence&&) = default;
351
353 ~AASequence() = default;
355
357 AASequence& operator=(const AASequence&) = default;
358
361
363 bool empty() const;
364
368
380 std::string toString() const;
381
383 std::string toUnmodifiedString() const;
384
393 std::string toUniModString() const;
394
413 std::string toBracketString(bool integer_mass = true,
414 bool mass_delta = false,
415 const std::vector<std::string> & fixed_modifications = std::vector<std::string>()) const;
416
419 void setModification(Size index, const std::string& modification);
420
422 void setModification(Size index, const Residue* modification);
423
425 void setModification(Size index, const ResidueModification* modification);
426
429 void setModification(Size index, const ResidueModification& modification);
430
432 void setModificationByDiffMonoMass(Size index, double diffMonoMass);
433
436 void setNTerminalModification(const std::string& modification);
437
440
443
445 void setNTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term);
446
448 const std::string& getNTerminalModificationName() const;
449
452
455 void setCTerminalModification(const std::string& modification);
456
459
462
464 void setCTerminalModificationByDiffMonoMass(double diffMonoMass, bool protein_term);
465
467 const std::string& getCTerminalModificationName() const;
468
471
473 const Residue& getResidue(Size index) const;
474
476 EmpiricalFormula getFormula(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
477
479 double getAverageWeight(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
480
485 double getMonoWeight(Residue::ResidueType type = Residue::Full, Int charge = 0) const;
486
492 double getMZ(Int charge, Residue::ResidueType type = Residue::Full) const;
493
495 const Residue& operator[](Size index) const;
496
498 AASequence operator+(const AASequence& peptide) const;
499
502
504 AASequence operator+(const Residue* residue) const;
505
508
510 Size size() const;
511
514
517
519 AASequence getSubsequence(Size index, UInt number) const;
520
522 void getAAFrequencies(std::map<std::string, Size>& frequency_table) const;
523
525
530 bool has(const Residue& residue) const;
531
534 bool hasSubsequence(const AASequence& peptide) const;
535
538 bool hasPrefix(const AASequence& peptide) const;
539
542 bool hasSuffix(const AASequence& peptide) const;
543
546
549
551 bool isModified() const;
552
554 bool operator==(const AASequence& rhs) const;
555
557 bool operator<(const AASequence& rhs) const;
558
560 bool operator!=(const AASequence& rhs) const;
562
566 inline Iterator begin() { return Iterator(&peptide_, 0); }
567
568 inline ConstIterator begin() const { return ConstIterator(&peptide_, 0); }
569
570 inline Iterator end() { return Iterator(&peptide_, (Int) peptide_.size()); }
571
572 inline ConstIterator end() const { return ConstIterator(&peptide_, (Int) peptide_.size()); }
574
579 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AASequence& peptide);
580
582 friend OPENMS_DLLAPI std::istream& operator>>(std::istream& is, const AASequence& peptide);
584
593 static AASequence fromString(const std::string& s,
594 bool permissive = true);
595
604 static AASequence fromString(const char* s,
605 bool permissive = true);
606
609 explicit AASequence(const std::string& s);
610
613 explicit AASequence(const char* s);
614
618 explicit AASequence(const std::string& s, bool permissive);
619
623 explicit AASequence(const char* s, bool permissive);
624
625 protected:
626
627 std::vector<const Residue*> peptide_;
628
629 const ResidueModification* n_term_mod_ = nullptr;
630
631 const ResidueModification* c_term_mod_ = nullptr;
632
646 static std::string::const_iterator parseModRoundBrackets_(const std::string::const_iterator str_it,
647 const std::string& str,
648 AASequence& aas,
649 const ResidueModification::TermSpecificity& specificity);
650
673 static std::string::const_iterator parseModSquareBrackets_(const std::string::const_iterator str_it,
674 const std::string& str,
675 AASequence& aas,
676 const ResidueModification::TermSpecificity& specificity);
677
678 static void parseString_(const std::string& peptide, AASequence& aas,
679 bool permissive = true);
680 };
681
682 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AASequence& peptide);
683
684 OPENMS_DLLAPI std::istream& operator>>(std::istream& os, const AASequence& peptide);
685
686} // namespace OpenMS
687
688// Hash function specialization for AASequence
689// Placed in std namespace to allow use with std::unordered_map/set
690namespace std
691{
706 template<>
707 struct hash<OpenMS::AASequence>
708 {
709 std::size_t operator()(const OpenMS::AASequence& seq) const noexcept
710 {
711 std::size_t seed = 0;
712
713 // Hash each residue
714 for (const auto& residue : seq)
715 {
716 // Hash one-letter code (single character, fast)
717 const std::string& olc = residue.getOneLetterCode();
718 if (!olc.empty())
719 {
721 }
722
723 // Hash modification if present
724 const OpenMS::ResidueModification* mod = residue.getModification();
725 if (mod != nullptr)
726 {
727 // Use full ID for portability (e.g., "Oxidation (M)")
728 // std::string inherits from std::string, no copy needed
730 }
731 }
732
733 // Hash N-terminal modification if present
734 const OpenMS::ResidueModification* n_mod = seq.getNTerminalModification();
735 if (n_mod != nullptr)
736 {
737 // Use a different seed offset for N-term to distinguish from C-term
738 std::size_t n_hash = OpenMS::fnv1a_hash_string(n_mod->getFullId());
739 OpenMS::hash_combine(seed, n_hash ^ 0x4e5445524dULL); // "NTERM" in hex-like
740 }
741
742 // Hash C-terminal modification if present
743 const OpenMS::ResidueModification* c_mod = seq.getCTerminalModification();
744 if (c_mod != nullptr)
745 {
746 // Use a different seed offset for C-term
747 std::size_t c_hash = OpenMS::fnv1a_hash_string(c_mod->getFullId());
748 OpenMS::hash_combine(seed, c_hash ^ 0x435445524dULL); // "CTERM" in hex-like
749 }
750
751 return seed;
752 }
753 };
754} // namespace std
755
756
ConstIterator for AASequence.
Definition AASequence.h:103
const_pointer operator->() const
dereference operator
Definition AASequence.h:156
ConstIterator(const ConstIterator &rhs)=default
copy constructor
const ConstIterator operator-(difference_type diff) const
backward jump operator
Definition AASequence.h:173
Residue value_type
Definition AASequence.h:111
ConstIterator(const std::vector< const Residue * > *vec_ptr, difference_type position)
detailed constructor with pointer to the vector and offset position
Definition AASequence.h:122
bool operator!=(const ConstIterator &rhs) const
inequality operator
Definition AASequence.h:185
ConstIterator(const AASequence::Iterator &rhs)
copy constructor from Iterator
Definition AASequence.h:132
const ConstIterator operator+(difference_type diff) const
forward jump operator
Definition AASequence.h:162
~ConstIterator()=default
destructor
ConstIterator()=default
default constructor
ConstIterator & operator--()
decrement operator
Definition AASequence.h:198
std::random_access_iterator_tag iterator_category
Definition AASequence.h:113
bool operator==(const ConstIterator &rhs) const
equality comparator
Definition AASequence.h:179
const std::vector< const Residue * > * vector_
Definition AASequence.h:209
const Residue * const_pointer
Definition AASequence.h:109
difference_type position_
Definition AASequence.h:212
std::vector< constResidue * >::difference_type difference_type
Definition AASequence.h:110
const Residue * pointer
Definition AASequence.h:112
Residue & reference
Definition AASequence.h:108
const Residue & const_reference
Definition AASequence.h:107
const_reference operator*() const
dereference operator
Definition AASequence.h:150
ConstIterator & operator++()
increment operator
Definition AASequence.h:191
ConstIterator & operator=(const ConstIterator &rhs)=default
assignment operator
difference_type operator-(ConstIterator rhs) const
Definition AASequence.h:167
Iterator class for AASequence.
Definition AASequence.h:221
const_pointer operator->() const
dereference operator
Definition AASequence.h:274
Iterator()=default
default constructor
pointer operator->()
mutable dereference operator
Definition AASequence.h:280
const Iterator operator+(difference_type diff) const
forward jump operator
Definition AASequence.h:286
Iterator & operator=(const Iterator &rhs)
assignment operator
Definition AASequence.h:254
Iterator(const Iterator &rhs)=default
copy constructor
std::vector< const Residue * > * vector_
Definition AASequence.h:333
difference_type operator-(Iterator rhs) const
Definition AASequence.h:291
~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:239
bool operator==(const Iterator &rhs) const
equality comparator
Definition AASequence.h:303
const Residue * const_pointer
Definition AASequence.h:228
difference_type position_
Definition AASequence.h:336
std::vector< constResidue * >::difference_type difference_type
Definition AASequence.h:230
const Residue * pointer
Definition AASequence.h:229
Residue & reference
Definition AASequence.h:227
const Residue & const_reference
Definition AASequence.h:226
const_reference operator*() const
dereference operator
Definition AASequence.h:268
bool operator!=(const Iterator &rhs) const
inequality operator
Definition AASequence.h:309
Iterator & operator++()
increment operator
Definition AASequence.h:315
Iterator & operator--()
decrement operator
Definition AASequence.h:322
const Iterator operator-(difference_type diff) const
backward jump operator
Definition AASequence.h:297
Representation of a peptide/protein sequence.
Definition AASequence.h:93
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:566
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:627
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:572
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:570
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
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:568
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:709