peptideevidence.cpp 11.9 KB
Newer Older
1
2
3
4
/**
 * \file utils/peptideevidence.cpp
 * \date 18/11/2017
 * \author Olivier Langella
Langella Olivier's avatar
Langella Olivier committed
5
6
 * \brief peptide evidence : a peptide sequence + spectrum + identification
 * engine evaluation (psm)
7
8
9
10
 */


/*******************************************************************************
Langella Olivier's avatar
Langella Olivier committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Copyright (c) 2017 Olivier Langella <Olivier.Langella@u-psud.fr>.
 *
 * This file is part of XTPcpp.
 *
 *     XTPcpp 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.
 *
 *     XTPcpp 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 XTPcpp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
 *implementation
 ******************************************************************************/
32
33
34

#include "peptideevidence.h"
#include <pappsomspp/pappsoexception.h>
35
#include <pappsomspp/exception/exceptionnotfound.h>
36
37
38

std::hash<std::string> PeptideEvidence::_hash_fn;

39
PeptideEvidence::PeptideEvidence(MsRun *msrunid_sp, std::size_t scan_index)
Langella Olivier's avatar
Langella Olivier committed
40
{
41
  _msrunid_sp   = msrunid_sp;
42
  m_scan_number = scan_index;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  m_isSpectrumIndex =
    false; // we are not sure as it were not defined. By default, consider it is
           // a scan number to ensure compatibility

  _hash_sample_scan      = PeptideEvidence::_hash_fn(QString("%1 %2")
                                                  .arg(msrunid_sp->getXmlId())
                                                  .arg(m_scan_number)
                                                  .toStdString());
  _identification_engine = IdentificationEngine::unknown;
}

PeptideEvidence::PeptideEvidence(MsRun *msrunid_sp,
                                 std::size_t scan_index,
                                 bool isSpectrumIndex)
{
  _msrunid_sp       = msrunid_sp;
  m_scan_number     = scan_index;
  m_isSpectrumIndex = isSpectrumIndex;
61

62
63
  _hash_sample_scan      = PeptideEvidence::_hash_fn(QString("%1 %2")
                                                  .arg(msrunid_sp->getXmlId())
64
                                                  .arg(m_scan_number)
65
                                                  .toStdString());
Langella Olivier's avatar
Langella Olivier committed
66
  _identification_engine = IdentificationEngine::unknown;
67
68
}

Langella Olivier's avatar
Langella Olivier committed
69
PeptideEvidence::PeptideEvidence(const PeptideEvidence &other)
70
  : PeptideEvidence(other._msrunid_sp, other.m_scan_number)
Langella Olivier's avatar
Langella Olivier committed
71
72
73
74
75
76
77
78
79
80
81
82
83
{
  _hash_sample_scan        = other._hash_sample_scan;
  _sp_grp_peptide          = other._sp_grp_peptide;
  _peptide_sp              = other._peptide_sp;
  _rt                      = other._rt;
  _evalue                  = other._evalue;
  _exp_mass                = other._exp_mass;
  _charge                  = other._charge;
  _p_identification_source = other._p_identification_source;
  _params                  = other._params;
  _checked                 = other._checked;
  _proxy_valid             = other._proxy_valid;
  _identification_engine   = other._identification_engine;
84
  m_scan_number            = other.m_scan_number;
85
  m_isSpectrumIndex        = other.m_isSpectrumIndex;
86
87
}

88
89
90
91
PeptideEvidence::~PeptideEvidence()
{
}

92
pappso::pappso_double
Langella Olivier's avatar
Langella Olivier committed
93
94
95
PeptideEvidence::getTheoreticalMz() const
{
  return (getPeptideXtpSp().get()->getMz(getCharge()));
Langella Olivier's avatar
Langella Olivier committed
96
97
}

Langella Olivier's avatar
Langella Olivier committed
98
99
100
101
PeptideEvidenceSp
PeptideEvidence::makePeptideEvidenceSp() const
{
  return std::make_shared<PeptideEvidence>(*this);
102
103
}

Langella Olivier's avatar
Langella Olivier committed
104
105
106
107
void
PeptideEvidence::updateAutomaticFilters(
  const AutomaticFilterParameters &automatic_filter_parameters)
{
108
  // qDebug() << this->_p_identification_source;
109
110
  _proxy_valid =
    this->_p_identification_source->isValid(this, automatic_filter_parameters);
111
  // qDebug();
112
  if(_proxy_valid)
113
    {
114
115
      unsigned int peprepro =
        automatic_filter_parameters.getFilterPeptideObservedInLessSamplesThan();
116
      // qDebug();
117
      if(peprepro > 1)
118
        {
119
120
121
122
123
124
          if(this->getPeptideXtpSp().get()->getObservedMsrunList().size() <
             peprepro)
            {
              _proxy_valid = false;
              return;
            }
125
126
        }
    }
127
  // qDebug();
Langella Olivier's avatar
Langella Olivier committed
128
129
130
131
132
}
void
PeptideEvidence::setRetentionTime(pappso::pappso_double rt)
{
  _rt = rt;
133
}
Langella Olivier's avatar
Langella Olivier committed
134
135
136
137
void
PeptideEvidence::setEvalue(pappso::pappso_double evalue)
{
  _evalue = evalue;
138
}
Langella Olivier's avatar
Langella Olivier committed
139
140
141
142
143
144
145


void
PeptideEvidence::setIdentificationEngine(
  IdentificationEngine identification_engine)
{
  _identification_engine = identification_engine;
146
147
}

148
149
150
151
152
153
IdentificationEngine
PeptideEvidence::getIdentificationEngine() const
{
  return _identification_engine;
}

154
155
/** \brief set specific parameter value
 */
Langella Olivier's avatar
Langella Olivier committed
156
157
158
void
PeptideEvidence::setParam(PeptideEvidenceParam param, const QVariant &value)
{
159
160
161
162
163
164
165
  auto ret =
    _params.insert(std::pair<PeptideEvidenceParam, QVariant>(param, value));

  if(ret.second == false)
    {
      ret.first->second = value;
    }
Langella Olivier's avatar
Langella Olivier committed
166
167
168
169
170
171
172
}
const QVariant
PeptideEvidence::getParam(PeptideEvidenceParam param) const
{
  try
    {
      return _params.at(param);
173
    }
174
  catch(std::out_of_range &std_error)
Langella Olivier's avatar
Langella Olivier committed
175
176
    {
      return QVariant();
177
178
    }
}
Langella Olivier's avatar
Langella Olivier committed
179
180
181
182
const std::map<PeptideEvidenceParam, QVariant> &
PeptideEvidence::getParamList() const
{
  return _params;
183
184
}

Langella Olivier's avatar
Langella Olivier committed
185
186
187
188
189
190
191
std::size_t
PeptideEvidence::getHashPeptideMassSample() const
{
  return PeptideEvidence::_hash_fn(QString("%1 %2")
                                     .arg(_peptide_sp.get()->toAbsoluteString())
                                     .arg(_msrunid_sp->getXmlId())
                                     .toStdString());
192
193
}

Langella Olivier's avatar
Langella Olivier committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
std::size_t
PeptideEvidence::getHashSampleScan() const
{
  return _hash_sample_scan;
}
pappso::pappso_double
PeptideEvidence::getEvalue() const
{
  return _evalue;
}
pappso::pappso_double
PeptideEvidence::getExperimentalMass() const
{
  return _exp_mass;
}
pappso::pappso_double
PeptideEvidence::getExperimentalMhplus() const
{
  return _exp_mass + pappso::MHPLUS;
213
214
}

Langella Olivier's avatar
Langella Olivier committed
215
216
217
pappso::pappso_double
PeptideEvidence::getExperimentalMz() const
{
218
  pappso::pappso_double mz = _exp_mass;
Langella Olivier's avatar
Langella Olivier committed
219
220
221
  for(unsigned int i = 0; i < _charge; i++)
    {
      mz += pappso::MHPLUS;
222
    }
Langella Olivier's avatar
Langella Olivier committed
223
224
225
226
227
228
229
230
  mz = mz / _charge;
  return mz;
}
void
PeptideEvidence::setExperimentalMass(pappso::pappso_double exp_mass)
{
  _exp_mass = exp_mass;
}
231
pappso::pappso_double
Langella Olivier's avatar
Langella Olivier committed
232
233
234
235
PeptideEvidence::getDeltaMass() const
{
  return ((_exp_mass + pappso::MHPLUS) - _peptide_sp.get()->getMz(1));
}
236
pappso::pappso_double
Langella Olivier's avatar
Langella Olivier committed
237
238
239
240
241
242
243
PeptideEvidence::getPpmDeltaMass() const
{
  // return (_peptide_sp.get()->getMz(1) - (_exp_mass+pappso::MHPLUS));
  pappso::pappso_double diff = getDeltaMass();
  while(diff > 0.5)
    {
      diff = diff - pappso::DIFFC12C13;
244
    }
Langella Olivier's avatar
Langella Olivier committed
245
246
  diff = (diff / getPeptideXtpSp().get()->getMz(1)) * pappso::ONEMILLION;
  return diff;
247
}
Langella Olivier's avatar
Langella Olivier committed
248
249
250
251
void
PeptideEvidence::setCharge(unsigned int charge)
{
  _charge = charge;
252
253
}

Langella Olivier's avatar
Langella Olivier committed
254
255
256
257
void
PeptideEvidence::setPeptideXtpSp(PeptideXtpSp peptide)
{
  _peptide_sp = peptide;
258
  _peptide_sp.get()->observedInMsRun(_msrunid_sp);
259
260
}

Langella Olivier's avatar
Langella Olivier committed
261
262
263
264
void
PeptideEvidence::setChecked(bool arg1)
{
  _checked = arg1;
265
266
}

Langella Olivier's avatar
Langella Olivier committed
267
268
269
270
271
272
273
274
275
276
ValidationState
PeptideEvidence::getValidationState() const
{
  if(isGrouped())
    {
      return ValidationState::grouped;
    }
  else if(isValidAndChecked())
    {
      return ValidationState::validAndChecked;
277
    }
Langella Olivier's avatar
Langella Olivier committed
278
279
280
281
282
  else if(isValid())
    {
      return ValidationState::valid;
    }
  return ValidationState::notValid;
283
284
}

Langella Olivier's avatar
Langella Olivier committed
285
286
287
288
bool
PeptideEvidence::isValid() const
{
  return _proxy_valid;
289
}
Langella Olivier's avatar
Langella Olivier committed
290
291
292
293
bool
PeptideEvidence::isChecked() const
{
  return _checked;
294
295
}

Langella Olivier's avatar
Langella Olivier committed
296
297
298
299
bool
PeptideEvidence::isValidAndChecked() const
{
  return _proxy_valid && _checked;
300
301
}

Langella Olivier's avatar
Langella Olivier committed
302
303
304
305
306
307
bool
PeptideEvidence::isGrouped() const
{
  if(_sp_grp_peptide.get() == nullptr)
    {
      return false;
308
    }
Langella Olivier's avatar
Langella Olivier committed
309
310
311
  if(_sp_grp_peptide.get()->getGroupNumber() == 0)
    {
      return false;
312
    }
Langella Olivier's avatar
Langella Olivier committed
313
  return true;
314
}
Langella Olivier's avatar
Langella Olivier committed
315
316
317
318
319
320
321
322
323
324
void
PeptideEvidence::setIdentificationDataSource(
  IdentificationDataSource *identification_source)
{
  _p_identification_source = identification_source;

  if(_identification_engine == IdentificationEngine::unknown)
    {
      _identification_engine = identification_source->getIdentificationEngine();
    }
325
}
Langella Olivier's avatar
Langella Olivier committed
326
327
328
329
IdentificationDataSource *
PeptideEvidence::getIdentificationDataSource() const
{
  return _p_identification_source;
330
}
Langella Olivier's avatar
Langella Olivier committed
331
unsigned int
332
PeptideEvidence::getScanNumber() const
Langella Olivier's avatar
Langella Olivier committed
333
{
334
  return m_scan_number;
335
}
Langella Olivier's avatar
Langella Olivier committed
336
337
338
339
pappso::pappso_double
PeptideEvidence::getRetentionTime() const
{
  return _rt;
340
}
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

pappso::pappso_double
PeptideEvidence::getHardenedRetentionTime() const
{
  if(_rt == 0)
    {
      // try to look at mz data file
      if(_p_identification_source != nullptr)
        {
          MsRunSp msrunsp = _p_identification_source->getMsRunSp();
          if(msrunsp != nullptr)
            {
              pappso::MsRunReaderSPtr reader =
                msrunsp.get()->getMsRunReaderSPtr();
              if(reader != nullptr)
                {
                  pappso::QualifiedMassSpectrum qmassspec =
                    reader.get()->qualifiedMassSpectrum(m_scan_number, false);

                  return qmassspec.getRtInSeconds();
                }
            }
        }
    }
  return _rt;
}

Langella Olivier's avatar
Langella Olivier committed
368
369
370
371
unsigned int
PeptideEvidence::getCharge() const
{
  return _charge;
372
373
}

Langella Olivier's avatar
Langella Olivier committed
374
375
376
377
378
379
380
381
const PeptideXtpSp &
PeptideEvidence::getPeptideXtpSp() const
{
  // if (_sp_grp_peptide.get() == nullptr) {
  //    throw pappso::PappsoException(QObject::tr("Peptide is null in %1
  //    %2").arg(_msrunid_sp->getXmlId()).arg(this->getScan()));
  //}
  return _peptide_sp;
382
383
}

Langella Olivier's avatar
Langella Olivier committed
384
385
386
387
const MsRun *
PeptideEvidence::getMsRunP() const
{
  return _msrunid_sp;
388
}
389
390
391
392
393
394
395
396
397


MsRun *
PeptideEvidence::getMsRunPtr()
{
  return _msrunid_sp;
}


Langella Olivier's avatar
Langella Olivier committed
398
399
400
401
void
PeptideEvidence::setGrpPeptideSp(const pappso::GrpPeptideSp &sp_grp_peptide)
{
  _sp_grp_peptide = sp_grp_peptide;
402
403
}

Langella Olivier's avatar
Langella Olivier committed
404
405
406
407
const pappso::GrpPeptideSp &
PeptideEvidence::getGrpPeptideSp() const
{
  return _sp_grp_peptide;
408
}
409
410
411
412
413
414
415
416
417
418
419
420
421

pappso::MassSpectrumCstSPtr
PeptideEvidence::getMassSpectrumCstSPtr() const
{
  if(m_isSpectrumIndex)
    {
      return _msrunid_sp->getMassSpectrumCstSPtrBySpectrumIndex(m_scan_number);
    }
  else
    {
      return _msrunid_sp->getMassSpectrumCstSPtrByScanNumber(m_scan_number);
    }
}
422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
pappso::QualifiedMassSpectrum
PeptideEvidence::getQualifiedMassSpectrum(bool data) const
{

  if(m_isSpectrumIndex)
    {
      return _msrunid_sp->getQualifiedMassSpectrumBySpectrumIndex(m_scan_number,
                                                                  data);
    }
  else
    {
      return _msrunid_sp->getQualifiedMassSpectrumByScanNumber(m_scan_number,
                                                               data);
    }
}

439
440
441
442
443
444
445
446
447
448

std::size_t
PeptideEvidence::getSpectrumIndex() const
{
  if(m_isSpectrumIndex)
    {
      return m_scan_number;
    }
  throw pappso::ExceptionNotFound(
    QObject::tr(
449
      "this peptide evidence has no spectrum index, only a scan number : %1")
450
451
      .arg(m_scan_number));
}
452
453


454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
std::size_t
PeptideEvidence::getSpectrumIndexByScanNumber() const
{
  if(m_isSpectrumIndex)
    {
      return m_scan_number;
    }
  else
    {
      if(_msrunid_sp != nullptr)
        {
          return _msrunid_sp->scanNumber2SpectrumIndex(m_scan_number);
        }
      else
        {
          throw pappso::PappsoException(
            QObject::tr("_msrunid_sp == nullptr"));
        }
    }
}


476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
QString
PeptideEvidence::getHtmlSequence() const
{
  QString html = getPeptideXtpSp().get()->getSequence();

  std::vector<std::size_t> position_list;
  QVariant param = getParam(PeptideEvidenceParam::deepprot_delta_positions);
  if(!param.isNull())
    {
      for(auto pos_str : param.toString().split(" ", QString::SkipEmptyParts))
        {
          position_list.push_back(pos_str.toUInt());
        }
    }

  std::sort(position_list.begin(),
            position_list.end(),
            [](std::size_t a, std::size_t b) { return a > b; });

  for(std::size_t pos : position_list)
    {
      html.insert(pos + 1, "</font>");
      html.insert(pos, "<font color=\"red\">");
    }

  return html;
}