libpappsomspp
Library for mass spectrometry
xtandemhyperscore.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/psm/xtandemhyperscore.cpp
3  * \date 19/3/2015
4  * \author Olivier Langella
5  * \brief computation of the X!Tandem hyperscore
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 <cmath>
33 #include "../../pappsoexception.h"
34 #include "../../peptide/peptidefragment.h"
35 #include "../../peptide/peptidefragmentionlistbase.h"
36 #include "xtandemhyperscore.h"
37 #include "../peptidespectrummatch.h"
38 
39 namespace pappso
40 {
41 
42 unsigned int
43 factorial(unsigned int n)
44 {
45  unsigned int retval = 1;
46  for(int i = n; i > 1; --i)
47  retval *= i;
48  return retval;
49 }
50 
52  pappso::PeptideSp peptideSp,
53  unsigned int parent_charge,
54  PrecisionPtr precision,
55  std::list<PeptideIon> ion_list,
56  bool refine_spectrum_synthesis)
57  : _refine_spectrum_synthesis(refine_spectrum_synthesis)
58 {
59  try
60  {
61  /*
62  if ((peptide_ion_sp.get()->getPeptideIonType() == PeptideIon::c) ||
63  (peptide_ion_sp.get()->getPeptideIonType() == PeptideIon::z)) {
64  if(current_max_charge > 2) {
65  current_max_charge--;
66  }
67  }*/
68  unsigned int max_charge = parent_charge;
69  if(parent_charge > 1)
70  {
71  max_charge = parent_charge - 1;
72  }
74  spectrum, peptideSp, max_charge, precision, ion_list);
75 
76  _ion_count = {{PeptideIon::a, 0},
77  {PeptideIon::b, 0},
78  {PeptideIon::bo, 0},
79  {PeptideIon::bstar, 0},
80  {PeptideIon::c, 0},
81  {PeptideIon::y, 0},
82  {PeptideIon::yo, 0},
83  {PeptideIon::ystar, 0},
84  {PeptideIon::z, 0}};
85 
86  std::map<PeptideIon, unsigned int> ion_count;
87  for(auto &&ion_type : ion_list)
88  {
89  ion_count.insert(std::pair<PeptideIon, unsigned int>(ion_type, 0));
90  }
91 
92  std::map<unsigned int, pappso_double> charge_dot_product;
93  std::map<unsigned int, std::map<PeptideIon, unsigned int>>
94  charge_ion_count;
95  for(unsigned int i = 1; i <= max_charge; i++)
96  {
97  charge_dot_product.insert(
98  std::pair<unsigned int, pappso_double>(i, 0));
99  charge_ion_count.insert(
100  std::pair<unsigned int, std::map<PeptideIon, unsigned int>>(
101  i, ion_count));
102  }
103  QString sequence = peptideSp.get()->getSequence();
104  for(auto &&peptide_ion_match : psm)
105  {
106  PeptideIon ion_type = peptide_ion_match.getPeptideIonType();
107  unsigned int charge = peptide_ion_match.getCharge();
108  charge_dot_product[charge] +=
109  peptide_ion_match.getPeak().y *
111  sequence,
112  peptide_ion_match.getPeptideIonDirection(),
113  peptide_ion_match.getPeptideFragmentIonSp().get()->size());
114  charge_ion_count[charge][ion_type] += 1;
115  _ion_count[ion_type] += 1;
116  }
117 
118  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
119  << " _ion_count[PeptideIon::y]=" << _ion_count[PeptideIon::y];
120  // take the 2 best component
121  pappso_double sum_intensity = 0;
122  for(unsigned int i = 1; i <= max_charge; i++)
123  {
124  sum_intensity += charge_dot_product[i];
125  }
126  for(auto count : _ion_count)
127  {
128  sum_intensity *= factorial(count.second);
129  }
130 
131  _proto_hyperscore = sum_intensity;
132  }
133  catch(PappsoException &exception_pappso)
134  {
135  QString errorStr =
136  QObject::tr("ERROR computing hyperscore, PAPPSO exception:\n%1")
137  .arg(exception_pappso.qwhat());
138  qDebug() << "XtandemHyperscore::XtandemHyperscore PappsoException :\n"
139  << errorStr;
140  throw PappsoException(errorStr);
141  }
142  catch(std::exception &exception_std)
143  {
144  QString errorStr =
145  QObject::tr("ERROR computing hyperscore, std exception:\n%1")
146  .arg(exception_std.what());
147  qDebug() << "XtandemHyperscore::XtandemHyperscore std::exception :\n"
148  << errorStr;
149  throw PappsoException(errorStr);
150  }
151 }
152 
153 unsigned int
155 {
156  return _ion_count.at(ion_type);
157 }
159 {
160 }
161 
162 
164  AaFactorMap ret;
165  // populate ret
166  for(long c = 64; c < 126; c++)
167  {
168  ret.insert(std::pair<char, pappso_double>(c, pappso_double(1.0)));
169  }
170  ret['P'] = pappso_double(5.0);
171  return ret;
172 }();
173 
174 
176  AaFactorMap ret;
177  // populate ret
178  for(long c = 64; c < 126; c++)
179  {
180  ret.insert(std::pair<char, pappso_double>(c, pappso_double(1.0)));
181  }
182  ret['D'] = pappso_double(5.0);
183  ret['N'] = pappso_double(2.0);
184  ret['V'] = pappso_double(3.0);
185  ret['E'] = pappso_double(3.0);
186  ret['Q'] = pappso_double(2.0);
187  ret['I'] = pappso_double(3.0);
188  ret['L'] = pappso_double(3.0);
189  return ret;
190 }();
191 
192 
193 unsigned int
195  const QString &sequence,
196  PeptideDirection ion_direction,
197  unsigned int ion_size) const
198 {
199  unsigned int Pi(1);
200 
201  char last_aa_nter('_'), last_aa_cter('_');
202 
203  if(ion_direction == PeptideDirection::Nter)
204  {
205  last_aa_nter = sequence[ion_size - 1].toLatin1();
206  last_aa_cter = sequence[ion_size].toLatin1();
207  if(ion_size == 2)
208  {
209  if(last_aa_nter == 'P')
210  {
211  Pi *= 10;
212  }
213  else
214  {
215  Pi *= 3;
216  }
217  }
218  }
219  else
220  {
221  unsigned int offset(sequence.size() - ion_size);
222  last_aa_nter = sequence[offset - 1].toLatin1();
223  last_aa_cter = sequence[offset].toLatin1();
224  if((offset) == 2)
225  {
226  if(last_aa_nter == 'P')
227  {
228  Pi *= 10;
229  }
230  else
231  {
232  Pi *= 3;
233  }
234  }
235  }
236  // QDebug << " last_aa_nter=" << QChar(last_aa_nter) << "
237  // _aa_ion_factor_b[last_aa_nter]="s ;
238  qDebug() << PeptideFragment::getPeptideIonDirectionName(ion_direction)
239  << " last_aa_nter=" << last_aa_nter
240  << " _aa_ion_factor_b[last_aa_nter]="
241  << _aa_ion_factor_b[last_aa_nter] << " last_aa_cter=" << last_aa_cter
242  << " _aa_ion_factor_y[last_aa_cter]="
243  << _aa_ion_factor_y[last_aa_cter];
245  {
246  Pi *= _aa_ion_factor_b[last_aa_nter] * _aa_ion_factor_y[last_aa_cter];
247  }
248 
249  return Pi;
250 }
251 
252 
255 {
256  try
257  {
258  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
259  << " _proto_hyperscore=" << _proto_hyperscore;
260  return (log10(_proto_hyperscore) * 4);
261  }
262  catch(PappsoException &exception_pappso)
263  {
264  QString errorStr =
265  QObject::tr("ERROR in getHyperscore, PAPPSO exception:\n%1")
266  .arg(exception_pappso.qwhat());
267  qDebug() << "XtandemHyperscore::getHyperscore PappsoException :\n"
268  << errorStr;
269  throw PappsoException(errorStr);
270  }
271  catch(std::exception &exception_std)
272  {
273  QString errorStr =
274  QObject::tr("ERROR in getHyperscore, std exception:\n%1")
275  .arg(exception_std.what());
276  qDebug() << "XtandemHyperscore::getHyperscore std::exception :\n"
277  << errorStr;
278  throw PappsoException(errorStr);
279  }
280 }
281 
282 
283 } // namespace pappso
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::XtandemHyperscore::_refine_spectrum_synthesis
bool _refine_spectrum_synthesis
Definition: xtandemhyperscore.h:58
pappso::XtandemHyperscore::_aa_ion_factor_b
static AaFactorMap _aa_ion_factor_b
Definition: xtandemhyperscore.h:69
pappso::XtandemHyperscore::~XtandemHyperscore
~XtandemHyperscore()
Definition: xtandemhyperscore.cpp:158
pappso::XtandemHyperscore::getHyperscore
pappso_double getHyperscore() const
Definition: xtandemhyperscore.cpp:254
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::XtandemHyperscore::_ion_count
std::map< PeptideIon, unsigned int > _ion_count
Definition: xtandemhyperscore.h:66
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition: massspectrum.h:71
pappso::AxisScale::unset
@ unset
pappso::PeptideSpectrumMatch
Definition: peptidespectrummatch.h:44
pappso::PeptideIon
PeptideIon
PeptideIon enum defines all types of ions (Nter or Cter)
Definition: types.h:351
pappso::XtandemHyperscore::getMatchedIons
unsigned int getMatchedIons(PeptideIon ion_type) const
Definition: xtandemhyperscore.cpp:154
pappso::PeptideFragment::getPeptideIonDirectionName
static const QString getPeptideIonDirectionName(PeptideDirection direction)
Definition: peptidefragment.cpp:98
pappso::XtandemHyperscore::XtandemHyperscore
XtandemHyperscore(const MassSpectrum &spectrum, pappso::PeptideSp peptideSp, unsigned int parent_charge, PrecisionPtr precision, std::list< PeptideIon > ion_list, bool refine_spectrum_synthesis)
Definition: xtandemhyperscore.cpp:51
xtandemhyperscore.h
computation of the X!Tandem hyperscore
pappso::XtandemHyperscore::_aa_ion_factor_y
static AaFactorMap _aa_ion_factor_y
Definition: xtandemhyperscore.h:68
pappso::PrecisionBase
Definition: precision.h:44
pappso::XtandemHyperscore::getXtandemPredictedIonIntensityFactor
unsigned int getXtandemPredictedIonIntensityFactor(const QString &sequence, PeptideDirection ion_direction, unsigned int ion_size) const
Definition: xtandemhyperscore.cpp:194
pappso::factorial
unsigned int factorial(unsigned int n)
Definition: xtandemhyperscore.cpp:43
pappso::PappsoException::qwhat
virtual const QString & qwhat() const
Definition: pappsoexception.h:68
pappso::PeptideIonNter::b
@ b
pappso::XtandemHyperscore::AaFactorMap
std::map< char, pappso_double > AaFactorMap
Definition: xtandemhyperscore.h:45
pappso::PeptideDirection
PeptideDirection
Definition: peptide.h:46
pappso::PeptideIon::y
@ y
Cter amino ions.
pappso::PeptideSp
std::shared_ptr< const Peptide > PeptideSp
Definition: aamodification.h:47
pappso::XtandemHyperscore::_proto_hyperscore
pappso_double _proto_hyperscore
Definition: xtandemhyperscore.h:65
pappso::PappsoException
Definition: pappsoexception.h:42