File: test_psm.cpp

package info (click to toggle)
libpappsomspp 0.11.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,032 kB
  • sloc: cpp: 78,332; xml: 44,164; python: 668; sql: 186; sh: 33; makefile: 31
file content (441 lines) | stat: -rw-r--r-- 17,931 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//
// File: test_psm.cpp
// Created by: Olivier Langella
// Created on: 22/1/2018
//
/*******************************************************************************
 * Copyright (c) 2018 Olivier Langella <Olivier.Langella@u-psud.fr>.
 *
 * This file is part of the PAPPSOms++ library.
 *
 *     PAPPSOms++ 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++ is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warrantyo f
 *     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++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
 *implementation
 ******************************************************************************/

// make test ARGS="-V -I 18,18"
// ./tests/catch2-only-tests [psm] -s
// ./tests/catch2-only-tests [psmfeature] -s

#include <algorithm>


#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>

#include <pappsomspp/core/mzrange.h>
#include <pappsomspp/core/psm/peptideisotopespectrummatch.h>
#include <pappsomspp/core/psm/peptidespectrummatch.h>
#include <pappsomspp/core/psm/features/psmfeatures.h>
#include <pappsomspp/core/processing/filters/filterpeakdelta.h>
#include <pappsomspp/core/peptide/peptideproformaparser.h>

#include <iostream>
#include <iomanip>
#include <set>
#include <QDebug>
#include <QString>
#include "common.h"
#include "tests/tests-config.h"

using namespace pappso;
using namespace std;
// using namespace pwiz::msdata;


TEST_CASE("psm test suite.", "[psm]")
{
  // Set the debugging message formatting pattern.
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION("..:: readMgf ::.. GKQQVM[MOD:00425]VGYSDSGK", "[psm]")
  {
    std::cout << std::endl << "..:: readMgf ::.. GKQQVM[MOD:00425]VGYSDSGK" << std::endl;
    // bool refine_spectrum_synthesis = false;

    pappso::QualifiedMassSpectrum spectrum_simple = readQualifiedMassSpectrumMgf(
      QString(CMAKE_SOURCE_DIR)
        .append("/tests/data/scans/20120906_balliau_extract_1_A05_urnb-2_scan_3340.mgf"));


    PeptideSp peptide_sp = PeptideProFormaParser::parseString("GKQQVM[MOD:00425]VGYSDSGK");
    // peptide_sp.get()->addAaModification(AaModification::getInstance("MOD:00397"),
    // 0);

    PrecisionPtr precision         = PrecisionFactory::getDaltonInstance(0.02);
    std::list<Enums::PeptideIon> ion_list = pappso::PeptideFragmentIonListBase::getCIDionList();
    std::cout << "spectrum_simple size  " << spectrum_simple.size() << std::endl;
    // REQUIRE(spectrum_simple.size() == 100);

    PeptideIsotopeSpectrumMatch psm(
      *(spectrum_simple.getMassSpectrumSPtr().get()), peptide_sp, 2, precision, ion_list, 2, 0);

    const std::list<PeakIonIsotopeMatch> peak_ion_list = psm.getPeakIonIsotopeMatchList();

    std::vector<int> ion_ms2;
    for(const PeakIonIsotopeMatch &ion_match : peak_ion_list)
      {
        ion_ms2.push_back((int)ion_match.getPeptideIonType());
      }

    REQUIRE_THAT(ion_ms2,
                 Catch::Matchers::Approx(std::vector<int>(
                   {0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  8,  8,  8,  8,  8, 8, 8, 8, 8, 8, 8, 8,
                    8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 3, 3, 3,
                    3, 3, 3, 3, 3, 3, 3, 3,  3,  2,  2,  2,  2,  2,  2,  2,  2, 1, 1, 1, 1})));
  }

  SECTION("..:: readMgf ::..", "[psm]")
  {
    std::cout << std::endl << "..:: readMgf ::.." << std::endl;
    // bool refine_spectrum_synthesis = false;

    MassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR).append("/tests/data/peaklist_15046_simple_xt.mgf"));
    //.applyCutOff(150).takeNmostIntense(100).applyDynamicRange(100);


    Peptide peptide("AIADGSLLDLLR");
    PeptideSp peptide_sp(peptide.makePeptideSp());
    // peptide_sp.get()->addAaModification(AaModification::getInstance("MOD:00397"),
    // 0);

    PrecisionPtr precision = PrecisionFactory::getDaltonInstance(0.02);
    std::list<Enums::PeptideIon> ion_list;
    ion_list.push_back(Enums::PeptideIon::y);
    ion_list.push_back(Enums::PeptideIon::b);
    std::cout << "spectrum_simple size  " << spectrum_simple.size() << std::endl;
    REQUIRE(spectrum_simple.size() == 100);

    PeptideSpectrumMatch psm(spectrum_simple, peptide_sp, 2, precision, ion_list);

    REQUIRE(psm.countTotalMatchedIons() == 11);

    writeJsonPsm("psm.json", spectrum_simple, psm);

    PeptideSpectrumMatch psm2(
      spectrum_simple, peptide_sp, 2, precision, PeptideFragmentIonListBase::getCIDionList());

    writeJsonPsm("psm2.json", spectrum_simple, psm2);
    REQUIRE(psm2.countTotalMatchedIons() == 21);

    unsigned int test_count = psm.countTotalMatchedIons();
    REQUIRE(test_count == 11);
    // hyperscore="33.5"
    precision = PrecisionFactory::getDaltonInstance(0.0);
    PeptideSpectrumMatch psmb(spectrum_simple, peptide_sp, 2, precision, ion_list);
    test_count = psmb.countTotalMatchedIons();
    REQUIRE(test_count == 0);


    precision = PrecisionFactory::getDaltonInstance(0.02);
    PeptideIsotopeSpectrumMatch psm_iso(spectrum_simple, peptide_sp, 2, precision, ion_list, 2, 0);
    REQUIRE(psm_iso.getPeakIonIsotopeMatchList().size() == 19);


    writeJsonPsmIsotope("psm_isotope.json", spectrum_simple, psm_iso);


    PeptideIsotopeSpectrumMatch psm_iso2(spectrum_simple, peptide_sp, 2, precision, ion_list, 2, 4);
    REQUIRE(psm_iso2.getPeakIonIsotopeMatchList().size() == 0);

    std::list<PeakIonIsotopeMatch> peak_ion_isotope_match_list =
      psm_iso.getPeakIonIsotopeMatchList();

    for(auto peak : peak_ion_isotope_match_list)
      {
        qDebug() << peak.toString() << " " << peak.getPeptideNaturalIsotopeAverageSp().get();
      }
    REQUIRE(peak_ion_isotope_match_list.size() == 19);

    psm_iso.dropPeaksLackingMonoisotope();
    peak_ion_isotope_match_list = psm_iso.getPeakIonIsotopeMatchList();
    for(auto peak : peak_ion_isotope_match_list)
      {
        qDebug() << peak.toString() << " " << peak.getPeptideNaturalIsotopeAverageSp().get();
      }
    REQUIRE(peak_ion_isotope_match_list.size() == 19);

    qDebug() << peak_ion_isotope_match_list.size();
    peak_ion_isotope_match_list.sort(
      [](const PeakIonIsotopeMatch &a, const PeakIonIsotopeMatch &b) {
        qDebug() << a.getPeak().y << " > ";
        qDebug() << b.getPeak().y << " . ";
        return (a.getPeak().y > b.getPeak().y);
      });


    spectrum_simple = readMgf(QString(CMAKE_SOURCE_DIR).append("/tests/data/peaklist_15046.mgf"));

    writeJsonTrace("peaklist_15046.json", spectrum_simple);
    peptide_sp = Peptide("AIADGSLLDLLR").makePeptideSp();


    precision = PrecisionFactory::getDaltonInstance(0.02);
    qDebug();
    PeptideIsotopeSpectrumMatch psm_iso_check(
      spectrum_simple, peptide_sp, 2, precision, ion_list, 2, 0);

    qDebug();
    peak_ion_isotope_match_list = psm_iso_check.getPeakIonIsotopeMatchList();

    REQUIRE(peak_ion_isotope_match_list.size() == 39);
    qDebug();

    for(auto &peak : peak_ion_isotope_match_list)
      {
        qDebug() << peak.toString() << " " << peak.getPeptideNaturalIsotopeAverageSp().get();
      }


    peak_ion_isotope_match_list.sort(
      [](const PeakIonIsotopeMatch &a, const PeakIonIsotopeMatch &b) {
        if(a.getPeptideIonType() < b.getPeptideIonType())
          return true;
        if(a.getPeptideFragmentIonSp().get()->size() < b.getPeptideFragmentIonSp().get()->size())
          return true;
        if(a.getCharge() < b.getCharge())
          return true;
        if(a.getPeptideNaturalIsotopeAverageSp().get()->getIsotopeNumber() <
           b.getPeptideNaturalIsotopeAverageSp().get()->getIsotopeNumber())
          return true;
        return false;
      });


    auto it = peak_ion_isotope_match_list.begin();
    REQUIRE(it->toString().toStdString() == "y1+isotope0r1mz175.119");
    it++;
    REQUIRE(it->toString().toStdString() == "y1++isotope2r1mz89.0601");

    for(auto peak : peak_ion_isotope_match_list)
      {
        qDebug() << peak.toString() << " " << peak.getPeptideNaturalIsotopeAverageSp().get();
      }

    psm_iso_check.dropPeaksLackingMonoisotope();
    peak_ion_isotope_match_list = psm_iso_check.getPeakIonIsotopeMatchList();

    for(auto peak : peak_ion_isotope_match_list)
      {
        qDebug() << peak.toString() << " " << peak.getPeptideNaturalIsotopeAverageSp().get();
      }
    REQUIRE(peak_ion_isotope_match_list.size() == 39);
  }
}


TEST_CASE("psm features test suite.", "[psmfeature]")
{
  // Set the debugging message formatting pattern.
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION("..:: readMgf ::..", "[psmfeature]")
  {
    std::cout << std::endl << "..:: readMgf ::.." << std::endl;
    // bool refine_spectrum_synthesis = false;

    MassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR).append("/tests/data/scan_2016.mgf"));
    //.applyCutOff(150).takeNmostIntense(100).applyDynamicRange(100);


    Peptide peptide("EDKPQPPPEGR");
    PeptideSp peptide_sp(peptide.makePeptideSp());
    // peptide_sp.get()->addAaModification(AaModification::getInstance("MOD:00397"),
    // 0);

    PsmFeatures features(PrecisionFactory::getDaltonInstance(0.02), 150);

    features.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 3, 1);


    LinearRegression lr = features.getIonIsotopeLinearRegression();
    REQUIRE(lr.getCoefficientOfDetermination() == 0.0);
    REQUIRE(features.getTotalIntensity() == Catch::Approx(248641.8659667977));
    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::b) ==
            Catch::Approx(1751.5864257813).epsilon(0.001));
    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::y) ==
            Catch::Approx(57112.2947998047).epsilon(0.001));


    REQUIRE(features.countMatchedIonComplementPairs() == 0);


    /*
    <group id="15968" mh="1275.731419" z="2" rt="PT2843.58S" expect="1.7e-04"
  label="GRMZM5G815453_P01 P00874 Ribulose bisphosphate carboxylase large chain
  Precursor..." type="model" sumI="6.08" maxI="111642" fI="1116.42" act="0" >
  <protein expect="-191.8" id="15968.1" uid="49237" label="GRMZM5G815453_P01
  P00874 Ribulose bisphosphate carboxylase large chain Precursor..." sumI="8.97"
  > <note label="description">GRMZM5G815453_P01 P00874 Ribulose bisphosphate
  carboxylase large chain Precursor (RuBisCO large subunit)(EC 4.1.1.39)
  seq=translation; coord=Pt:56824..58323:1; parent_transcript=GRMZM5G815453_T01;
  parent_gene=GRMZM5G815453</note> <file type="peptide"
  URL="/gorgone/pappso/formation/TD/Database/Genome_Z_mays_5a.fasta"/> <peptide
  start="1" end="483"> <domain id="15968.1.1" start="347" end="357"
  expect="1.7e-04" mh="1275.7311" delta="0.0003" hyperscore="29.0"
  nextscore="10.8" y_score="10.5" y_ions="8" b_score="3.8" b_ions="1" pre="EGER"
  post="DDFI" seq="EITLGFVDLLR" missed_cleavages="0">
  </domain>
  </peptide>
  */
    spectrum_simple = readMgf(QString(CMAKE_SOURCE_DIR).append("/tests/data/scan_15968.mgf"));

    REQUIRE(spectrum_simple.size() == 304);

    peptide_sp = Peptide("EITLGFVDLLR").makePeptideSp();

    PsmFeatures features2(PrecisionFactory::getDaltonInstance(0.02), 150);

    features2.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 2, 1);

    REQUIRE(features2.getIonIsotopeLinearRegression().getCoefficientOfDetermination() ==
            Catch::Approx(0.6323680888).epsilon(0.01));

    features.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 2, 1);


    REQUIRE(features.getIonIsotopeLinearRegression().getCoefficientOfDetermination() ==
            Catch::Approx(0.6323680888).epsilon(0.01));
    REQUIRE(features.getMaxIntensityMatchedIonComplementPairPrecursorMassDelta() ==
            Catch::Approx(-0.0036778069).epsilon(0.00001));
    REQUIRE(features.countMatchedIonComplementPairs() == 3);

    REQUIRE(features.getPeakIonPairs().size() == 3);

    REQUIRE(features.getPeakIonPairs()[0].first.toString().toStdString() ==
            "b2+isotope0r1mz243.134");
    REQUIRE(features.getPeakIonPairs()[0].second.toString().toStdString() ==
            "y9+isotope0r1mz1033.61");

    REQUIRE(features.getPeakIonPairs()[1].first.toString().toStdString() ==
            "b6+isotope0r1mz661.367");
    REQUIRE(features.getPeakIonPairs()[1].second.toString().toStdString() ==
            "y5+isotope0r1mz615.383");

    REQUIRE(features.getPeakIonPairs()[2].first.toString().toStdString() ==
            "b7+isotope0r1mz760.435");
    REQUIRE(features.getPeakIonPairs()[2].second.toString().toStdString() ==
            "y4+isotope0r1mz516.315");


    REQUIRE(features.getIonPairPrecursorMassDelta(features.getPeakIonPairs()[0]) ==
            Catch::Approx(-0.0036778069).epsilon(0.001));
    REQUIRE(features.getIonPairPrecursorMassDelta(features.getPeakIonPairs()[1]) ==
            Catch::Approx(-0.0118262069).epsilon(0.001));
    REQUIRE(features.getIonPairPrecursorMassDelta(features.getPeakIonPairs()[2]) ==
            Catch::Approx(-0.0114600069).epsilon(0.001));


    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::y) == 9);
    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::b) == 2);
    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::a) == 0);


    spectrum_simple = readMgf(QString(CMAKE_SOURCE_DIR).append("/tests/data/peaklist_15046.mgf"));

    peptide_sp = Peptide("AIADGSLLDLLR").makePeptideSp();


    features.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 2, 0);


    REQUIRE(features.getIonIsotopeLinearRegression().getCoefficientOfDetermination() ==
            Catch::Approx(0.0).epsilon(0.01));

    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::b) == 3);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::b) == 4);


    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::y) == Catch::Approx(654880.6992189999));
    REQUIRE(features.getMaxIntensityPeakIonMatch(Enums::PeptideIon::y) == Catch::Approx(114514.0625));

    features.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 2, 1);


    REQUIRE(features.getIonIsotopeLinearRegression().getCoefficientOfDetermination() ==
            Catch::Approx(0.4555707503).epsilon(0.04));

    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::b) == 3);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::b) == 4);


    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::y) == Catch::Approx(863068.6967769999));
    REQUIRE(features.getMaxIntensityPeakIonMatch(Enums::PeptideIon::y) == Catch::Approx(114514.0625));

    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::b) == Catch::Approx(254791.342651));
    REQUIRE(features.getMaxIntensityPeakIonMatch(Enums::PeptideIon::b) == Catch::Approx(161016.984375));


    // SeqCoverComplementPeaks
    REQUIRE(features.getComplementPairsAaSequenceCoverage() == 5);
    REQUIRE(((double)features.getComplementPairsAaSequenceCoverage() /
             (double)peptide_sp.get()->size()) == Catch::Approx(5.0 / 12.0));
    REQUIRE(((double)0 / (double)peptide_sp.get()->size()) == Catch::Approx(0));


    REQUIRE((features.getTotalIntensityOfMatchedIonComplementPairs() /
             features.getTotalIntensity()) == Catch::Approx(0.1727291768));

    REQUIRE(
      (features.getTotalIntensityOfMatchedIonComplementPairs() < features.getTotalIntensity()));

    REQUIRE(features.getMaxIntensityMatchedIonComplementPairPrecursorMassDelta() ==
            Catch::Approx(0.0008592852).epsilon(0.00001));


    features.setPeptideSpectrumCharge(peptide_sp, &spectrum_simple, 2, 2);


    REQUIRE(features.getIonIsotopeLinearRegression().getCoefficientOfDetermination() ==
            Catch::Approx(0.4555707503).epsilon(0.04));

    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getMaxConsecutiveIon(Enums::PeptideIon::b) == 3);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::y) == 10);
    REQUIRE(features.getAaSequenceCoverage(Enums::PeptideIon::b) == 4);


    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::y) == Catch::Approx(911305.6467289998));
    REQUIRE(features.getMaxIntensityPeakIonMatch(Enums::PeptideIon::y) == Catch::Approx(114514.0625));

    REQUIRE(features.getIntensityOfMatchedIon(Enums::PeptideIon::b) == Catch::Approx(271127.594604));
    REQUIRE(features.getMaxIntensityPeakIonMatch(Enums::PeptideIon::b) == Catch::Approx(161016.984375));


    // SeqCoverComplementPeaks
    REQUIRE(features.getComplementPairsAaSequenceCoverage() == 5);
    REQUIRE(((double)features.getComplementPairsAaSequenceCoverage() /
             (double)peptide_sp.get()->size()) == Catch::Approx(5.0 / 12.0));
    REQUIRE(((double)0 / (double)peptide_sp.get()->size()) == Catch::Approx(0));


    REQUIRE((features.getTotalIntensityOfMatchedIonComplementPairs() /
             features.getTotalIntensity()) == Catch::Approx(0.1790448148));

    REQUIRE(
      (features.getTotalIntensityOfMatchedIonComplementPairs() < features.getTotalIntensity()));

    REQUIRE(features.getMaxIntensityMatchedIonComplementPairPrecursorMassDelta() ==
            Catch::Approx(0.0008592852).epsilon(0.00001));
  }
}