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 
154 {
155  _ion_count = other._ion_count;
158 }
159 
160 unsigned int
162 {
163  return _ion_count.at(ion_type);
164 }
166 {
167 }
168 
169 
171  AaFactorMap ret;
172  // populate ret
173  for(long c = 64; c < 126; c++)
174  {
175  ret.insert(std::pair<char, pappso_double>(c, pappso_double(1.0)));
176  }
177  ret['P'] = pappso_double(5.0);
178  return ret;
179 }();
180 
181 
183  AaFactorMap ret;
184  // populate ret
185  for(long c = 64; c < 126; c++)
186  {
187  ret.insert(std::pair<char, pappso_double>(c, pappso_double(1.0)));
188  }
189  ret['D'] = pappso_double(5.0);
190  ret['N'] = pappso_double(2.0);
191  ret['V'] = pappso_double(3.0);
192  ret['E'] = pappso_double(3.0);
193  ret['Q'] = pappso_double(2.0);
194  ret['I'] = pappso_double(3.0);
195  ret['L'] = pappso_double(3.0);
196  return ret;
197 }();
198 
199 
200 unsigned int
202  const QString &sequence,
203  PeptideDirection ion_direction,
204  unsigned int ion_size) const
205 {
206  unsigned int Pi(1);
207 
208  char last_aa_nter('_'), last_aa_cter('_');
209 
210  if(ion_direction == PeptideDirection::Nter)
211  {
212  last_aa_nter = sequence[ion_size - 1].toLatin1();
213  last_aa_cter = sequence[ion_size].toLatin1();
214  if(ion_size == 2)
215  {
216  if(last_aa_nter == 'P')
217  {
218  Pi *= 10;
219  }
220  else
221  {
222  Pi *= 3;
223  }
224  }
225  }
226  else
227  {
228  unsigned int offset(sequence.size() - ion_size);
229  last_aa_nter = sequence[offset - 1].toLatin1();
230  last_aa_cter = sequence[offset].toLatin1();
231  if((offset) == 2)
232  {
233  if(last_aa_nter == 'P')
234  {
235  Pi *= 10;
236  }
237  else
238  {
239  Pi *= 3;
240  }
241  }
242  }
243  // QDebug << " last_aa_nter=" << QChar(last_aa_nter) << "
244  // _aa_ion_factor_b[last_aa_nter]="s ;
245  qDebug() << PeptideFragment::getPeptideIonDirectionName(ion_direction)
246  << " last_aa_nter=" << last_aa_nter
247  << " _aa_ion_factor_b[last_aa_nter]="
248  << _aa_ion_factor_b[last_aa_nter] << " last_aa_cter=" << last_aa_cter
249  << " _aa_ion_factor_y[last_aa_cter]="
250  << _aa_ion_factor_y[last_aa_cter];
252  {
253  Pi *= _aa_ion_factor_b[last_aa_nter] * _aa_ion_factor_y[last_aa_cter];
254  }
255 
256  return Pi;
257 }
258 
259 
262 {
263  try
264  {
265  qDebug() << " _proto_hyperscore=" << _proto_hyperscore;
266  double hyperscore = (log10(_proto_hyperscore) * 4);
267  if(hyperscore < 0)
268  return 0;
269  return hyperscore;
270  }
271  catch(PappsoException &exception_pappso)
272  {
273  QString errorStr =
274  QObject::tr("ERROR in getHyperscore, PAPPSO exception:\n%1")
275  .arg(exception_pappso.qwhat());
276  qDebug() << "XtandemHyperscore::getHyperscore PappsoException :\n"
277  << errorStr;
278  throw PappsoException(errorStr);
279  }
280  catch(std::exception &exception_std)
281  {
282  QString errorStr =
283  QObject::tr("ERROR in getHyperscore, std exception:\n%1")
284  .arg(exception_std.what());
285  qDebug() << "XtandemHyperscore::getHyperscore std::exception :\n"
286  << errorStr;
287  throw PappsoException(errorStr);
288  }
289 }
290 
291 
292 } // namespace pappso
Class to represent a mass spectrum.
Definition: massspectrum.h:71
virtual const QString & qwhat() const
static const QString getPeptideIonDirectionName(PeptideDirection direction)
unsigned int getMatchedIons(PeptideIon ion_type) const
unsigned int getXtandemPredictedIonIntensityFactor(const QString &sequence, PeptideDirection ion_direction, unsigned int ion_size) const
pappso_double getHyperscore() const
static AaFactorMap _aa_ion_factor_y
std::map< char, pappso_double > AaFactorMap
static AaFactorMap _aa_ion_factor_b
std::map< PeptideIon, unsigned int > _ion_count
XtandemHyperscore(const MassSpectrum &spectrum, pappso::PeptideSp peptideSp, unsigned int parent_charge, PrecisionPtr precision, std::list< PeptideIon > ion_list, bool refine_spectrum_synthesis)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
PeptideIon
PeptideIon enum defines all types of ions (Nter or Cter)
Definition: types.h:386
@ a
Nter aldimine ions.
@ y
Cter amino ions.
@ c
Nter amino ions.
@ ystar
Cter amino ions + NH3 loss.
@ yo
Cter amino ions + H2O loss.
@ bstar
Nter acylium ions + NH3 loss.
@ b
Nter acylium ions.
@ bo
Nter acylium ions + H2O loss.
@ z
Cter carbocations.
std::shared_ptr< const Peptide > PeptideSp
PeptideDirection
Definition: peptide.h:46
double pappso_double
A type definition for doubles.
Definition: types.h:49
unsigned int factorial(unsigned int n)
computation of the X!Tandem hyperscore