libpappsomspp
Library for mass spectrometry
peptide.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/peptide/peptide.cpp
3  * \date 7/3/2015
4  * \author Olivier Langella
5  * \brief peptide model
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2015 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
10  *
11  * This file is part of the PAPPSOms++ library.
12  *
13  * PAPPSOms++ is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * PAPPSOms++ is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25  *
26  * Contributors:
27  * Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and
28  *implementation
29  ******************************************************************************/
30 
31 #include <QDebug>
32 #include <algorithm>
33 #include "peptide.h"
34 #include "../pappsoexception.h"
35 #include "../exception/exceptionoutofrange.h"
36 #include "../exception/exceptionnotpossible.h"
38 
39 namespace pappso
40 {
41 
42 
43 bool
45 {
46  if((std::int8_t)ion_type < (std::int8_t)8)
47  {
48  return true;
49  }
50  return false;
51 }
52 
55 {
56  if(peptideIonIsNter(ion_type))
57  {
58  return PeptideDirection::Nter;
59  }
61 }
62 
63 Peptide::Peptide(const QString &pepstr)
64 {
65 
66  QString::const_iterator it(pepstr.begin());
67  if(it != pepstr.end())
68  {
69  // first amino acid is the Nter one
70  // by default, it is obtained by hydrolytic cleavage in normal water
71  // and it is loaded with one Hydrogen
72  Aa nter_aa(it->toLatin1());
73  nter_aa.addAaModification(
74  AaModification::getInstance("internal:Nter_hydrolytic_cleavage_H"));
75  m_aaVec.push_back(nter_aa);
76  it++;
77 
78  while(it != pepstr.end())
79  {
80  m_aaVec.push_back(Aa(it->toLatin1()));
81  it++;
82  }
83  // by default, Nter aa is obtained by hydrolytic cleavage in normal water
84  // and it is loaded with Hydrogen + Oxygen
85  m_aaVec.back().addAaModification(
86  AaModification::getInstance("internal:Cter_hydrolytic_cleavage_HO"));
87  getMass();
88  qDebug() << "blabla " << m_aaVec.back().toString();
89  }
90 }
91 
93 {
94 }
95 
96 Peptide::Peptide(const Peptide &peptide)
97  : m_aaVec(peptide.m_aaVec), m_proxyMass(peptide.m_proxyMass)
98 {
99 }
100 
101 
102 Peptide::Peptide(Peptide &&toCopy) // move constructor
103  : m_aaVec(std::move(toCopy.m_aaVec)), m_proxyMass(toCopy.m_proxyMass)
104 {
105 }
106 
107 
108 PeptideSp
110 {
111  return std::make_shared<const Peptide>(*this);
112 }
113 
116 {
117  return std::make_shared<Peptide>(*this);
118 }
119 
120 void
122  unsigned int position)
123 {
124  if(position >= size())
125  {
126  throw ExceptionOutOfRange(
127  QObject::tr("position (%1) > size (%2)").arg(position).arg(size()));
128  }
129  m_proxyMass = -1;
130  qDebug() << "Peptide::addAaModification begin " << position;
131  std::vector<Aa>::iterator it = m_aaVec.begin() + position;
132  it->addAaModification(aaModification);
133  getMass();
134  qDebug() << "Peptide::addAaModification end";
135 }
136 
137 
138 const QString
140 {
141  QString seq = "";
142  std::vector<Aa>::const_iterator it(m_aaVec.begin());
143  while(it != m_aaVec.end())
144  {
145  seq += it->getLetter();
146  it++;
147  }
148  return seq;
149 }
150 const QString
152 {
153  QString seq = "";
154  std::vector<Aa>::const_iterator it(m_aaVec.begin());
155  while(it != m_aaVec.end())
156  {
157  seq += it->toAbsoluteString();
158  it++;
159  }
160  return seq;
161 }
162 
163 const QString
165 {
166  QString seq = "";
167  std::vector<Aa>::const_iterator it(m_aaVec.begin());
168  while(it != m_aaVec.end())
169  {
170  seq += it->toAbsoluteString();
171  it++;
172  }
173  return seq.replace("L", "I");
174 }
175 
176 
177 const QString
179 {
180  QString seq = "";
181  std::vector<Aa>::const_iterator it(m_aaVec.begin());
182  while(it != m_aaVec.end())
183  {
184  seq += it->toString();
185  it++;
186  }
187  return seq;
188 }
189 
192 {
193  qDebug() << "Aa::getMass() begin";
194  if(m_proxyMass < 0)
195  {
196  m_proxyMass = 0;
197  for(auto aa : m_aaVec)
198  {
199  m_proxyMass += aa.getMass();
200  }
201  }
202  qDebug() << "Aa::getMass() end " << m_proxyMass;
203  return m_proxyMass;
204 }
205 
206 int
208 {
209  int number = 0;
210  std::vector<Aa>::const_iterator it(m_aaVec.begin());
211  while(it != m_aaVec.end())
212  {
213  number += it->getNumberOfAtom(atom);
214  it++;
215  }
216  // qDebug() << "Aa::getMass() end " << mass;
217  return number;
218 }
219 
220 int
222 {
223  int number = 0;
224  std::vector<Aa>::const_iterator it(m_aaVec.begin());
225  while(it != m_aaVec.end())
226  {
227  number += it->getNumberOfIsotope(isotope);
228  it++;
229  }
230  // qDebug() << "Aa::getMass() end " << mass;
231  return number;
232 }
233 
234 
235 unsigned int
237 {
238  unsigned int number = 0;
239  std::vector<Aa>::const_iterator it(m_aaVec.begin());
240  while(it != m_aaVec.end())
241  {
242  number += it->getNumberOfModification(mod);
243  it++;
244  }
245  // qDebug() << "Aa::getMass() end " << mass;
246  return number;
247 }
248 
249 unsigned int
251  const std::vector<char> &aa_list) const
252 {
253  unsigned int number = 0;
254  std::vector<Aa>::const_iterator it(m_aaVec.begin());
255  while(it != m_aaVec.end())
256  {
257  if(std::find(aa_list.begin(), aa_list.end(), it->getLetter()) !=
258  aa_list.end())
259  {
260  number += it->getNumberOfModification(mod);
261  }
262  it++;
263  }
264  // qDebug() << "Aa::getMass() end " << mass;
265  return number;
266 }
267 
268 void
270 {
271  if(oldmod == newmod)
272  return;
273  std::vector<Aa>::iterator it(m_aaVec.begin());
274  while(it != m_aaVec.end())
275  {
276  it->replaceAaModification(oldmod, newmod);
277  it++;
278  }
279  m_proxyMass = -1;
280  getMass();
281 }
282 void
284 {
285  std::vector<Aa>::iterator it(m_aaVec.begin());
286  while(it != m_aaVec.end())
287  {
288  it->removeAaModification(mod);
289  qDebug() << it->toString() << " " << toAbsoluteString();
290  it++;
291  }
292  m_proxyMass = -1;
293  getMass();
294  // qDebug() << "Aa::getMass() end " << mass;
295 }
296 std::vector<unsigned int>
298 {
299  std::vector<unsigned int> position_list;
300  unsigned int position = 0;
301  std::vector<Aa>::const_iterator it(m_aaVec.begin());
302  while(it != m_aaVec.end())
303  {
304  unsigned int number = 0;
305  number += it->getNumberOfModification(mod);
306  for(unsigned int j = 0; j < number; j++)
307  {
308  position_list.push_back(position);
309  }
310  it++;
311  position++;
312  }
313  // qDebug() << "Aa::getMass() end " << mass;
314  return position_list;
315 }
316 
317 std::vector<unsigned int>
319  const std::vector<char> &aa_list) const
320 {
321  std::vector<unsigned int> position_list;
322  unsigned int position = 0;
323  std::vector<Aa>::const_iterator it(m_aaVec.begin());
324  while(it != m_aaVec.end())
325  {
326  if(std::find(aa_list.begin(), aa_list.end(), it->getLetter()) !=
327  aa_list.end())
328  {
329  unsigned int number = 0;
330  number += it->getNumberOfModification(mod);
331  for(unsigned int j = 0; j < number; j++)
332  {
333  position_list.push_back(position);
334  }
335  }
336  it++;
337  position++;
338  }
339  // qDebug() << "Aa::getMass() end " << mass;
340  return position_list;
341 }
342 
343 std::vector<unsigned int>
345 {
346  std::vector<unsigned int> position_list;
347  unsigned int number = 0;
348  std::vector<Aa>::const_iterator it(m_aaVec.begin());
349  while(it != m_aaVec.end())
350  {
351  if(it->getLetter() == aa)
352  position_list.push_back(number);
353  number++;
354  it++;
355  }
356  // qDebug() << "Aa::getMass() end " << mass;
357  return position_list;
358 }
359 
360 std::vector<unsigned int>
361 Peptide::getAaPositionList(std::list<char> list_aa) const
362 {
363  std::vector<unsigned int> position_list;
364  unsigned int number = 0;
365  std::vector<Aa>::const_iterator it(m_aaVec.begin());
366  while(it != m_aaVec.end())
367  {
368 
369  bool found =
370  (std::find(list_aa.begin(), list_aa.end(), it->getLetter()) !=
371  list_aa.end());
372  if(found)
373  {
374  position_list.push_back(number);
375  }
376  number++;
377  it++;
378  }
379  // qDebug() << "Aa::getMass() end " << mass;
380  return position_list;
381 }
382 
385 {
386  std::vector<Aa>::const_iterator it(m_aaVec.begin());
387  if(it != m_aaVec.end())
388  {
389  return it->getInternalNterModification();
390  }
391 
392  return nullptr;
393 }
396 {
397  std::vector<Aa>::const_iterator it(m_aaVec.end());
398  it--;
399  if(it != m_aaVec.end())
400  {
401  return it->getInternalCterModification();
402  }
403  return nullptr;
404 }
405 void
407 {
408  std::vector<Aa>::iterator it(m_aaVec.begin());
409  if(it != m_aaVec.end())
410  {
411  m_proxyMass -= it->getMass();
412  it->removeInternalNterModification();
413  m_proxyMass += it->getMass();
414  }
415 }
416 void
418 {
419  std::vector<Aa>::iterator it(m_aaVec.end());
420  it--;
421  if(it != m_aaVec.end())
422  {
423  m_proxyMass -= it->getMass();
424  it->removeInternalCterModification();
425  m_proxyMass += it->getMass();
426  }
427 }
428 
429 
430 void
432 {
433  if(mod->getAccession().startsWith("internal:Nter_"))
434  {
436  std::vector<Aa>::iterator it(m_aaVec.begin());
437  if(it != m_aaVec.end())
438  {
439  it->addAaModification(mod);
440  }
441  else
442  {
443  throw ExceptionOutOfRange(QObject::tr("peptide is empty"));
444  }
445  }
446  else
447  {
448  throw ExceptionNotPossible(
449  QObject::tr("modification is not an internal Nter modification : %1")
450  .arg(mod->getAccession()));
451  }
452 }
453 void
455 {
456  if(mod->getAccession().startsWith("internal:Cter_"))
457  {
459  std::vector<Aa>::iterator it(m_aaVec.end());
460  it--;
461  if(it != m_aaVec.end())
462  {
463  it->addAaModification(mod);
464  }
465  else
466  {
467  throw ExceptionOutOfRange(QObject::tr("peptide is empty"));
468  }
469  }
470  else
471  {
472  throw ExceptionNotPossible(
473  QObject::tr("modification is not an internal Cter modification : %1")
474  .arg(mod->getAccession()));
475  }
476 }
477 
478 void
480 {
483  m_aaVec.begin()->removeInternalNterModification();
485  std::rotate(m_aaVec.begin(), m_aaVec.begin() + 1, m_aaVec.end());
486  m_aaVec.begin()->addAaModification(modNter);
487  (m_aaVec.end() - 1)->addAaModification(modCter);
488 }
489 
490 void
492 {
495  m_aaVec.begin()->removeInternalNterModification();
497  std::reverse(m_aaVec.begin(), m_aaVec.end());
498  m_aaVec.begin()->addAaModification(modNter);
499  (m_aaVec.end() - 1)->addAaModification(modCter);
500 }
501 
502 
503 bool
505 {
506  std::size_t size = m_aaVec.size();
507  std::size_t k = (size - 1);
508  for(std::size_t i = 0; i < (size / 2); i++, k--)
509  {
510  if(m_aaVec[i].getLetter() != m_aaVec[k].getLetter())
511  {
512  return false;
513  }
514  }
515  return true;
516 }
517 
518 Aa &
519 Peptide::getAa(unsigned int position)
520 {
521  if(position >= m_aaVec.size())
522  {
523  throw ExceptionOutOfRange(
524  QObject::tr("no AA at position %1").arg(position));
525  }
526  return m_aaVec.at(position);
527 }
528 const Aa &
529 Peptide::getConstAa(unsigned int position) const
530 {
531  if(position >= m_aaVec.size())
532  {
533  throw ExceptionOutOfRange(
534  QObject::tr("no AA at position %1").arg(position));
535  }
536  return m_aaVec.at(position);
537 }
538 
539 
540 void
542 {
543 
544  std::vector<Aa>::iterator it(m_aaVec.begin());
545  std::vector<Aa>::iterator itend(m_aaVec.end());
546  for(; it != itend; it++)
547  {
548  it->replaceLeucineIsoleucine();
549  }
550 }
551 
552 
553 void
555 {
556  std::vector<Aa>::iterator it(m_aaVec.begin());
557  if(it != m_aaVec.end())
558  {
559  AaModificationP nter_modification = getInternalNterModification();
560  m_aaVec.erase(it);
561  if(nter_modification != nullptr)
562  {
563  m_aaVec.begin()->addAaModification(nter_modification);
564  }
565 
566  m_proxyMass = -1;
567  getMass();
568  }
569  else
570  {
571  throw ExceptionOutOfRange(QObject::tr("peptide is empty"));
572  }
573 }
574 
575 
576 void
578 {
579  std::vector<Aa>::iterator it(m_aaVec.end());
580  it--;
581  if(it != m_aaVec.end())
582  {
583  AaModificationP cter_modification = getInternalCterModification();
584  m_aaVec.erase(it);
585  if(cter_modification != nullptr)
586  {
587  it = m_aaVec.end();
588  it--;
589  it->addAaModification(cter_modification);
590  }
591  m_proxyMass = -1;
592  getMass();
593  }
594  else
595  {
596  throw ExceptionOutOfRange(QObject::tr("peptide is empty"));
597  }
598 }
599 
600 } // namespace pappso
pappso::Peptide::removeInternalCterModification
void removeInternalCterModification()
Definition: peptide.cpp:417
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::AaModification::getAccession
const QString & getAccession() const
Definition: aamodification.cpp:90
pappso::Peptide::removeAaModification
void removeAaModification(AaModificationP mod)
removes all occurences of a modification
Definition: peptide.cpp:283
pappso::Peptide::size
unsigned int size() const override
Definition: peptide.h:178
pappso::Peptide::replaceLeucineIsoleucine
void replaceLeucineIsoleucine()
Definition: peptide.cpp:541
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::Peptide::replaceAaModification
void replaceAaModification(AaModificationP oldmod, AaModificationP newmod)
replaces all occurences of a modification by a new one
Definition: peptide.cpp:269
pappso::Peptide::getModificationPositionList
std::vector< unsigned int > getModificationPositionList(AaModificationP mod) const
get modification positions
Definition: peptide.cpp:297
pappso::Peptide::getInternalCterModification
AaModificationP getInternalCterModification() const
Definition: peptide.cpp:395
pappso::Isotope
Isotope
Definition: types.h:91
pappso::Peptide::isPalindrome
virtual bool isPalindrome() const override
tells if the peptide sequence is a palindrome
Definition: peptide.cpp:504
pappso::Aa::addAaModification
void addAaModification(AaModificationP aaModification)
Definition: aa.cpp:150
pappso::Peptide::getLiAbsoluteString
const QString getLiAbsoluteString() const
get all sequence string with modifications and converting Leucine to Isoleucine
Definition: peptide.cpp:164
pappso::Peptide::toAbsoluteString
const QString toAbsoluteString() const
print all modifications
Definition: peptide.cpp:151
pappso::Peptide::getSequence
const QString getSequence() const override
print amino acid sequence without modifications
Definition: peptide.cpp:139
peptide.h
peptide model
pappso::PeptideIon
PeptideIon
PeptideIon enum defines all types of ions (Nter or Cter)
Definition: types.h:351
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:29
pappso::Peptide::removeCterAminoAcid
void removeCterAminoAcid()
Definition: peptide.cpp:577
pappso::Peptide::countModificationOnAa
unsigned int countModificationOnAa(AaModificationP mod, const std::vector< char > &aa_list) const
count modification occurence
Definition: peptide.cpp:250
pappso::Peptide
Definition: peptide.h:93
pappso::Peptide::makePeptideSp
PeptideSp makePeptideSp() const
Definition: peptide.cpp:109
pappso::ExceptionOutOfRange
Definition: exceptionoutofrange.h:32
pappso::Peptide::Peptide
Peptide(const QString &pepstr)
Definition: peptide.cpp:63
pappso::Peptide::getNumberOfModification
unsigned int getNumberOfModification(AaModificationP mod) const
count modification occurence
Definition: peptide.cpp:236
pappso::Peptide::getNumberOfAtom
virtual int getNumberOfAtom(AtomIsotopeSurvey atom) const override
get the number of atom C, O, N, H in the molecule
Definition: peptide.cpp:207
pappso::Peptide::toString
const QString toString() const
print modification except internal modifications
Definition: peptide.cpp:178
pappso::Peptide::setInternalCterModification
void setInternalCterModification(AaModificationP mod)
Definition: peptide.cpp:454
pappso::Peptide::getNumberOfIsotope
virtual int getNumberOfIsotope(Isotope isotope) const override
get the number of isotopes C13, H2, O17, O18, N15, S33, S34, S36 in the molecule
Definition: peptide.cpp:221
pappso::Peptide::getInternalNterModification
AaModificationP getInternalNterModification() const
Definition: peptide.cpp:384
pappso::Peptide::removeNterAminoAcid
void removeNterAminoAcid()
Definition: peptide.cpp:554
pappso::AaModification::getInstance
static AaModificationP getInstance(const QString &accession)
Definition: aamodification.cpp:367
pappso::Peptide::~Peptide
virtual ~Peptide()
Definition: peptide.cpp:92
pappso::Peptide::m_aaVec
std::vector< Aa > m_aaVec
Definition: peptide.h:95
pappso::Peptide::removeInternalNterModification
void removeInternalNterModification()
Definition: peptide.cpp:406
pappso::Peptide::getConstAa
const Aa & getConstAa(unsigned int position) const
Definition: peptide.cpp:529
pappso::Peptide::addAaModification
void addAaModification(AaModificationP aaModification, unsigned int position)
adds a modification to amino acid sequence
Definition: peptide.cpp:121
pappso::peptideIonIsNter
bool peptideIonIsNter(PeptideIon ion_type)
tells if an ion is Nter
Definition: peptide.cpp:44
pappso::getPeptideIonDirection
PeptideDirection getPeptideIonDirection(PeptideIon ion_type)
get the direction of a peptide ion
Definition: peptide.cpp:54
pappso::Peptide::rotate
void rotate()
Definition: peptide.cpp:479
pappso::Peptide::setInternalNterModification
void setInternalNterModification(AaModificationP mod)
Definition: peptide.cpp:431
pappso::PeptideDirection
PeptideDirection
Definition: peptide.h:46
pappso::Peptide::getAaPositionList
std::vector< unsigned int > getAaPositionList(char aa) const
get positions of one amino acid in peptide
Definition: peptide.cpp:344
pappso::Peptide::m_proxyMass
pappso_double m_proxyMass
Definition: peptide.h:96
pappso::PeptideDirection::Nter
@ Nter
pappso::Peptide::getMass
pappso_double getMass()
Definition: peptide.cpp:191
peptidenaturalisotopelist.h
peptide natural isotope model
pappso::PeptideSp
std::shared_ptr< const Peptide > PeptideSp
Definition: aamodification.h:47
pappso::Peptide::makeNoConstPeptideSp
NoConstPeptideSp makeNoConstPeptideSp() const
Definition: peptide.cpp:115
pappso::Aa
Definition: aa.h:45
pappso::Peptide::reverse
void reverse()
Definition: peptide.cpp:491
pappso::AaModification
Definition: aamodification.h:57
pappso::NoConstPeptideSp
std::shared_ptr< Peptide > NoConstPeptideSp
Definition: peptide.h:90
pappso::AtomIsotopeSurvey
AtomIsotopeSurvey
Definition: types.h:76
pappso::Peptide::getAa
Aa & getAa(unsigned int position)
Definition: peptide.cpp:519