OpenMS
Loading...
Searching...
No Matches
PeptDeepUtils.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Satyam Yadav, Justin Sing $
7// --------------------------------------------------------------------------
8#pragma once
9
12#include <string>
13#include <vector>
14#include <array>
15#include <string_view>
16
17namespace OpenMS {
18 namespace ML {
19
20 // --- Shared PeptDeep Architecture Constants ---
21 constexpr int64_t PEPTDEEP_MOD_ELEMENTS = 109;
22
26 inline constexpr std::array<std::string_view, 109> ALPHAPEPTDEEP_MOD_ELEMENTS = {
27 "C", "H", "N", "O", "P", "S", "B", "F", "I", "K", "U", "V", "W", "X", "Y",
28 "Ac", "Ag", "Al", "Am", "Ar", "As", "At", "Au", "Ba", "Be", "Bi", "Bk",
29 "Br", "Ca", "Cd", "Ce", "Cf", "Cl", "Cm", "Co", "Cr", "Cs", "Cu", "Dy",
30 "Er", "Es", "Eu", "Fe", "Fm", "Fr", "Ga", "Gd", "Ge", "He", "Hf", "Hg",
31 "Ho", "In", "Ir", "Kr", "La", "Li", "Lr", "Lu", "Md", "Mg", "Mn", "Mo",
32 "Na", "Nb", "Nd", "Ne", "Ni", "No", "Np", "Os", "Pa", "Pb", "Pd", "Pm",
33 "Po", "Pr", "Pt", "Pu", "Ra", "Rb", "Re", "Rh", "Rn", "Ru", "Sb", "Sc",
34 "Se", "Si", "Sm", "Sn", "Sr", "Ta", "Tb", "Tc", "Te", "Th", "Ti", "Tl",
35 "Tm", "Xe", "Yb", "Zn", "Zr", "2H", "13C", "15N", "18O", "?"
36 };
37
48 constexpr int64_t PEPTDEEP_MAX_INSTRUMENT_NUM = 8;
49
52
61 inline constexpr std::array<std::string_view, 5> ALPHAPEPTDEEP_INSTRUMENTS = {
62 "QE", "Lumos", "timsTOF", "SciexTOF", "ThermoTOF"
63 };
64
75 inline int64_t instrumentIndex(std::string_view name)
76 {
77 auto upper = [](char c) { return (c >= 'a' && c <= 'z') ? static_cast<char>(c - ('a' - 'A')) : c; };
78
79 for (Size i = 0; i < ALPHAPEPTDEEP_INSTRUMENTS.size(); ++i)
80 {
81 const std::string_view candidate = ALPHAPEPTDEEP_INSTRUMENTS[i];
82 if (candidate.size() != name.size()) { continue; }
83
84 bool same = true;
85 for (Size c = 0; c < name.size(); ++c)
86 {
87 if (upper(name[c]) != upper(candidate[c])) { same = false; break; }
88 }
89 if (same) { return static_cast<int64_t>(i); }
90 }
92 }
93
100 inline void validateInstrumentIndices(const std::vector<int64_t>& instrument_indices)
101 {
102 for (int64_t index : instrument_indices)
103 {
104 if (index < 0 || index >= PEPTDEEP_MAX_INSTRUMENT_NUM)
105 {
106 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
107 "Instrument index " + std::to_string(index) + " is outside the range [0, "
108 + std::to_string(PEPTDEEP_MAX_INSTRUMENT_NUM) + ") that the MS2 model encodes. Derive "
109 "indices from ML::instrumentIndex() rather than passing literals.");
110 }
111 }
112 }
113
124 inline OpenMS::Int64 getAAIndex(char aa) {
125 if (aa >= 'A' && aa <= 'Z') return aa - 'A' + 1;
126 if (aa >= 'a' && aa <= 'z') return aa - 'a' + 1;
127 return 0; // 0 serves as the padding and unknown token
128 }
129
135 inline void validatePeptide(const std::string& peptide) {
136 if (peptide.empty()) {
137 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Peptide sequence cannot be empty.");
138 }
139 }
140
141 }
142}
A method or algorithm argument contains illegal values.
Definition Exception.h:633
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
constexpr int64_t PEPTDEEP_MAX_INSTRUMENT_NUM
Width of the instrument one-hot in AlphaPeptDeep's MS2 model.
Definition PeptDeepUtils.h:48
OpenMS::Int64 getAAIndex(char aa)
Maps amino acid characters to 1-based token indices for PeptDeep models.
Definition PeptDeepUtils.h:124
constexpr int64_t PEPTDEEP_UNKNOWN_INSTRUMENT_INDEX
Index peptdeep gives an instrument it does not recognise (featurize.py: max_instrument_num - 1).
Definition PeptDeepUtils.h:51
int64_t instrumentIndex(std::string_view name)
Instrument index for name, mirroring peptdeep's parse_instrument_indices().
Definition PeptDeepUtils.h:75
constexpr int64_t PEPTDEEP_MOD_ELEMENTS
Definition PeptDeepUtils.h:21
constexpr std::array< std::string_view, 5 > ALPHAPEPTDEEP_INSTRUMENTS
AlphaPeptDeep's instrument names, in the order that is their encoding.
Definition PeptDeepUtils.h:61
void validateInstrumentIndices(const std::vector< int64_t > &instrument_indices)
Validates instrument indices for PeptDeep MS2 inference.
Definition PeptDeepUtils.h:100
void validatePeptide(const std::string &peptide)
Validates a peptide sequence for PeptDeep inference. Complex character validation and modification pa...
Definition PeptDeepUtils.h:135
constexpr std::array< std::string_view, 109 > ALPHAPEPTDEEP_MOD_ELEMENTS
AlphaPeptDeep's exact 109-element array for modification tensor mapping. Elements missing from this l...
Definition PeptDeepUtils.h:26
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19