Loading...
Searching...
No Matches
aacode.cpp
Go to the documentation of this file.
/**
* \file pappsomspp/amino_acid/aacode.cpp
* \date 03/05/2023
* \author Olivier Langella
* \brief give an integer code to each amino acid
*/
/*******************************************************************************
* Copyright (c) 2023 Olivier Langella <Olivier.Langella@u-psud.fr>.
*
* This file is part of PAPPSOms-tools.
*
* PAPPSOms-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAPPSOms-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "pappsomspp/core/amino_acid/aacode.h"
#include "pappsomspp/core/exception/exceptionnotfound.h"
#include "pappsomspp/core/exception/exceptionoutofrange.h"
using namespace pappso;
AaCode::AaCode()
{
m_asciiTable.resize(90, 0);
m_aaCollection.push_back(Aa('A'));
m_aaCollection.push_back(Aa('C'));
m_aaCollection.push_back(Aa('D'));
m_aaCollection.push_back(Aa('E'));
m_aaCollection.push_back(Aa('F'));
m_aaCollection.push_back(Aa('G'));
m_aaCollection.push_back(Aa('H'));
m_aaCollection.push_back(Aa('I'));
m_aaCollection.push_back(Aa('K'));
m_aaCollection.push_back(Aa('M'));
m_aaCollection.push_back(Aa('N'));
m_aaCollection.push_back(Aa('P'));
m_aaCollection.push_back(Aa('Q'));
m_aaCollection.push_back(Aa('R'));
m_aaCollection.push_back(Aa('S'));
m_aaCollection.push_back(Aa('T'));
m_aaCollection.push_back(Aa('V'));
m_aaCollection.push_back(Aa('W'));
m_aaCollection.push_back(Aa('Y'));
updateNumbers();
}
pappso::AaCode::AaCode(const pappso::AaCode &other)
{
m_asciiTable = other.m_asciiTable;
m_aaCollection = other.m_aaCollection;
}
AaCode::~AaCode()
{
}
std::size_t
pappso::AaCode::getSize() const
{
return 19;
}
uint8_t
pappso::AaCode::getAaCode(char aa_letter) const
{
// qDebug() << aa_letter << " " << (uint8_t)aa_letter;
// qDebug() << m_asciiTable[77];
uint8_t aa_code = m_asciiTable[aa_letter];
if(aa_code == 0)
{
throw pappso::ExceptionOutOfRange(
QObject::tr("getAaCode(char aa_letter) error, %1 is null : no amino acid for letter \"%2\"")
.arg(aa_code)
.arg(aa_letter));
}
else if(aa_code >= m_massCollection.size())
{
throw pappso::ExceptionOutOfRange(
QObject::tr("getAaCode(char aa_letter) error, %1 amino acid code not found in "
"m_aaCollection for letter \"%2\"")
.arg(aa_code)
.arg(aa_letter));
}
return aa_code;
}
uint8_t
pappso::AaCode::getAaCode(pappso::Enums::AminoAcidChar aa) const
{
uint8_t aa_code = m_asciiTable[(char)aa];
if(aa_code == 0)
{
throw pappso::ExceptionOutOfRange(
QObject::tr("getAaCode(pappso::Enums::AminoAcidChar aa) error, %1 is null : no amino acid "
"for letter \"%2\"")
.arg(aa_code)
.arg(char(aa)));
}
else if(aa_code >= m_massCollection.size())
{
throw pappso::ExceptionOutOfRange(
QObject::tr("getAaCode(pappso::Enums::AminoAcidChar aa) error, %1 amino acid code not "
"found in m_aaCollection for letter \"%2\"")
.arg(aa_code)
.arg(char(aa)));
}
return aa_code;
}
const pappso::Aa &
pappso::AaCode::getAa(char aa_letter) const
{
if(aa_letter == 'L')
return m_leucine;
auto it = std::find_if(m_aaCollection.begin(), m_aaCollection.end(), [aa_letter](const Aa &aa) {
if(aa.getLetter() == aa_letter)
return true;
return false;
});
if(it != m_aaCollection.end())
{
return *it;
}
throw pappso::ExceptionNotFound(
QObject::tr("error, %1 amino acid not found in m_aaCollection").arg(aa_letter));
}
const pappso::Aa &
pappso::AaCode::getAa(uint8_t aa_code) const
{
if(aa_code == 0)
{
throw pappso::ExceptionOutOfRange(
QObject::tr("error, 0 is null : no amino acid").arg(aa_code));
}
else if(aa_code > 19)
{
throw pappso::ExceptionOutOfRange(
QObject::tr("error, %1 amino acid code not found in m_aaCollection").arg(aa_code));
}
return m_aaCollection[aa_code - 1];
}
void
pappso::AaCode::addAaModification(char aa_letter, pappso::AaModificationP aaModification)
{
auto it = std::find_if(m_aaCollection.begin(), m_aaCollection.end(), [aa_letter](const Aa &aa) {
if(aa.getLetter() == aa_letter)
return true;
return false;
});
if(it != m_aaCollection.end())
{
it->addAaModification(aaModification);
}
else
{
throw pappso::ExceptionNotFound(
QObject::tr("error, %1 amino acid not found in m_aaCollection").arg(aa_letter));
}
updateNumbers();
}
void
pappso::AaCode::updateNumbers()
{
std::sort(m_aaCollection.begin(), m_aaCollection.end(), [](const Aa &aa1, const Aa &aa2) {
return aa1.getMass() < aa2.getMass();
});
std::size_t n = 1;
for(const Aa &aa : m_aaCollection)
{
// qDebug() << aa.getLetter() << " " << n;
m_asciiTable[aa.getLetter()] = n;
n++;
}
m_asciiTable['L'] = m_asciiTable['I'];
updateMass();
}
void
pappso::AaCode::updateMass()
{
m_massCollection.resize(1);
for(const Aa &aa : m_aaCollection)
{
m_massCollection.push_back(aa.getMass());
}
}
double
pappso::AaCode::getMass(uint8_t aa_code) const
{
return m_massCollection[aa_code];
}
double
pappso::AaCode::getMass(char aa_letter) const
{
try
{
return m_massCollection[this->getAaCode(aa_letter)];
}
catch(const pappso::PappsoException &err)
{
throw pappso::PappsoException(
QObject::tr("getMass(char aa_letter) failed :\n%1").arg(err.qwhat()));
}
}
uint8_t
pappso::AaCode::getAaCodeByMass(double mass, pappso::PrecisionPtr precision) const
{
double delta = precision->delta(mass);
double mass_min = mass - delta;
double mass_max = mass + delta;
uint8_t aa_code = 0;
for(uint8_t i = 1; i < m_massCollection.size(); i++)
{
if(m_massCollection.at(i) >= mass_min)
{
if(m_massCollection.at(i) <= mass_max)
{
aa_code = i;
}
break;
}
}
return aa_code;
}
const std::vector<Aa> &
pappso::AaCode::getAaCollection() const
{
return m_aaCollection;
}
aacode.h
pappso::AaCode
collection of integer code for each amino acid 0 => null 1 to 20 => amino acid sorted by there mass (...
Definition aacode.h:44
pappso::AaCode::updateNumbers
void updateNumbers()
give a number (the code) to each amino acid sorted by mass
Definition aacode.cpp:192
pappso::AaCode::~AaCode
~AaCode()
Definition aacode.cpp:69
pappso::AaCode::addAaModification
void addAaModification(char aa_letter, AaModificationP aaModification)
add a modification on an amino acid for example carbamido on C
Definition aacode.cpp:169
pappso::AaCode::m_massCollection
std::vector< double > m_massCollection
Definition aacode.h:116
pappso::AaCode::m_asciiTable
std::vector< uint8_t > m_asciiTable
Definition aacode.h:113
pappso::AaCode::updateMass
void updateMass()
update mass cache
Definition aacode.cpp:212
pappso::AaCode::m_leucine
pappso::Aa m_leucine
Definition aacode.h:118
pappso::AaCode::getAaCodeByMass
uint8_t getAaCodeByMass(double mass, PrecisionPtr precision) const
get the integer code of an amino acid given a mass and a precision
Definition aacode.cpp:245
pappso::AaCode::AaCode
AaCode()
Definition aacode.cpp:34
pappso::AaCode::getAaCollection
const std::vector< Aa > & getAaCollection() const
Definition aacode.cpp:266
pappso::AaCode::getAaCode
uint8_t getAaCode(char aa_letter) const
get the integer code of an amino acid with the one letter code
Definition aacode.cpp:81
pappso::AaCode::getMass
double getMass(uint8_t aa_code) const
get the mass of the amino acid given its integer code the amino acid can bear some modification (if a...
Definition aacode.cpp:224
pappso::AaCode::getSize
std::size_t getSize() const
number of amino acid coded letters in this aa code
Definition aacode.cpp:74
pappso::AaCode::m_aaCollection
std::vector< Aa > m_aaCollection
Definition aacode.h:115
pappso::AaCode::getAa
const Aa & getAa(char aa_letter) const
get the Aa object from the one letter code
Definition aacode.cpp:132
pappso::Aa
Definition aa.h:45
pappso::ExceptionNotFound
Definition exceptionnotfound.h:32
pappso::ExceptionOutOfRange
Definition exceptionoutofrange.h:32
pappso::PappsoException
Definition pappsoexception.h:41
pappso::PappsoException::qwhat
virtual const QString & qwhat() const
Definition pappsoexception.h:66
pappso::PrecisionBase::delta
virtual pappso_double delta(pappso_double value) const =0
exceptionnotfound.h
exceptionoutofrange.h
pappso::Enums::AminoAcidChar
AminoAcidChar
Definition types.h:196
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::AaModificationP
const AaModification * AaModificationP
Definition aamodification.h:52
pappso::PrecisionPtr
const PrecisionBase * PrecisionPtr
Definition precision.h:122