msrun.cpp 21.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
/**
 * \filed core/msrun.cpp
 * \date 5/4/2017
 * \author Olivier Langella
 * \brief describes an MS run (chemical sample injected in a mass spectrometer)
 */


/*******************************************************************************
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 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
 ******************************************************************************/
31
32
33
34


#include "msrun.h"
#include <QFileInfo>
Langella Olivier's avatar
Langella Olivier committed
35
36
37
38
#include <QSettings>
#include <QDebug>
#include <QDir>
#include <pappsomspp/exception/exceptionnotfound.h>
39
#include <pappsomspp/xicextractor/msrunxicextractorfactory.h>
40
#include "../utils/msrunstatisticshandler.h"
41
42
#include <pappsomspp/msfile/msfileaccessor.h>
#include <pappsomspp/exception/exceptionnotimplemented.h>
43
44
#include <pappsomspp/msrun/private/timsmsrunreaderms2.h>
#include <pappsomspp/processing/filters/filterpseudocentroid.h>
45
#include <pappsomspp/processing/filters/filterchargedeconvolution.h>
46
47
#include <pappsomspp/processing/filters/filtertriangle.h>
#include <pappsomspp/precision.h>
48
#include "peptideevidence.h"
Renne Thomas's avatar
Renne Thomas committed
49
#include "alignmentgroup.h"
50

51
52
53
MsRun::MsRun(const QString &location) : pappso::MsRunId(location)
{
  QFileInfo fileinfo(location);
54
55
}

56
57
MsRun::MsRun(const MsRun &other) : pappso::MsRunId(other)
{
58
59
  _param_stats = other._param_stats;
  // m_centroidOptions = other.m_centroidOptions;
60
61
}

62
63
MsRun::~MsRun()
{
64
65
66
67
  if(mpa_msrunRetentionTime != nullptr)
    {
      delete mpa_msrunRetentionTime;
    }
68
}
69
70
pappso::MsRunReaderSPtr &
MsRun::getMsRunReaderSPtr()
71
{
72
  // qDebug() << " filename=" << getFileName();
73
74
  if(_msrun_reader_sp == nullptr)
    {
Langella Olivier's avatar
Langella Olivier committed
75
      buildMsRunReaderSp();
76
    }
77

78
79
  return _msrun_reader_sp;
}
80

81
void
Langella Olivier's avatar
Langella Olivier committed
82
MsRun::buildMsRunReaderSp()
83
84
85
{
  qDebug() << " runid=" << getRunId() << " xmlid=" << getXmlId();
  pappso::MsFileAccessor *p_accessor = nullptr;
86

87
88
89
  try
    {

Langella Olivier's avatar
Langella Olivier committed
90
      p_accessor = new pappso::MsFileAccessor(getFileName(), "runa1");
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

      p_accessor->setPreferedFileReaderType(pappso::MzFormat::brukerTims,
                                            pappso::FileReaderType::tims_ms2);
    }
  catch(pappso::ExceptionNotFound &error)
    {
      qDebug() << "not found";
      _msrun_reader_sp = nullptr;
      /// p_accessor       = nullptr;
      // throw error;
      // file not found, don't throw exception, we'll try another location
    }
  if(p_accessor != nullptr)
    {
      try
106
        {
107
108
109
110
          _msrun_reader_sp =
            p_accessor->getMsRunReaderSPtrByRunId(getRunId(), getXmlId());
          delete p_accessor;
          p_accessor = nullptr;
111

112
113
114
          pappso::TimsMsRunReaderMs2 *tims2_reader =
            dynamic_cast<pappso::TimsMsRunReaderMs2 *>(_msrun_reader_sp.get());
          if(tims2_reader != nullptr)
115
            {
116

117
118
              std::shared_ptr<pappso::FilterSuite> ms2filter =
                std::make_shared<pappso::FilterSuite>();
119

120
121
              ms2filter.get()->push_back(
                std::make_shared<pappso::FilterChargeDeconvolution>(
Langella Olivier's avatar
Langella Olivier committed
122
                  pappso::PrecisionFactory::getDaltonInstance(0.01)));
123

124
              tims2_reader->setMs2FilterCstSPtr(ms2filter);
125
            }
126
127
128

          this->setMzFormat(
            _msrun_reader_sp.get()->getMsRunId().get()->getMzFormat());
129
        }
130

131
132
133
134
      catch(pappso::PappsoException &error2)
        {
          _msrun_reader_sp = nullptr;
          if(p_accessor != nullptr)
135
            {
136
137
              delete p_accessor;
              p_accessor = nullptr;
138
            }
139
          throw error2;
140
141
142
143
144
        }
    }
}


145
146
147
148
149
150
void
MsRun::freeMsRunReaderSp()
{
  _msrun_reader_sp = nullptr;
}

151
152
153
154
void
MsRun::setMsRunStatistics(MsRunStatistics param, const QVariant &value)
{
  _param_stats.insert(std::pair<MsRunStatistics, QVariant>(param, value));
Langella Olivier's avatar
Langella Olivier committed
155
}
Langella Olivier's avatar
Langella Olivier committed
156

157
158
159
160
const std::map<MsRunStatistics, QVariant> &
MsRun::getMsRunStatisticsMap() const
{
  return _param_stats;
Langella Olivier's avatar
Langella Olivier committed
161
162
}

163
164
165
166
167
168
const QVariant
MsRun::getMsRunStatistics(MsRunStatistics param) const
{
  try
    {
      return _param_stats.at(param);
Langella Olivier's avatar
Langella Olivier committed
169
    }
170
  catch(std::out_of_range &std_error)
171
172
    {
      return QVariant();
Langella Olivier's avatar
Langella Olivier committed
173
174
    }
}
Langella Olivier's avatar
Langella Olivier committed
175

176
pappso::MsRunReaderSPtr
177
178
MsRun::findMsRunFile()
{
179
  // first look at the file path :)
180
181
182
  qDebug();
  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp != nullptr)
183
    {
184
      QFileInfo real_file(this->getFileName());
185
186
      if(real_file.exists())
        {
187
188
189
190
191

          qDebug();
          freeMsRunReaderSp();
          qDebug();
          return msrun_reader_sp;
192
193
194
        }
      else
        {
195
          msrun_reader_sp = nullptr;
196
197
        }
    }
198
  qDebug();
199
  QFileInfo file_original(this->getFileName());
Langella Olivier's avatar
Langella Olivier committed
200
201
  QString basename     = file_original.baseName();
  QString onlyfilename = file_original.fileName();
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  QSettings settings;
  QString path = settings.value("path/mzdatadir", "").toString();

  QDir dir_search(path);

  QFileInfoList files = dir_search.entryInfoList();
  foreach(QFileInfo file, files)
    {
      if(file.isDir())
        {
          qDebug() << "DIR: " << file.fileName();
        }
      else
        {
          qDebug() << "FILE: " << file.fileName();
          if(onlyfilename == file.fileName())
            {
219
              this->setFileName(file.absoluteFilePath());
Langella Olivier's avatar
Langella Olivier committed
220
              try
221
                {
222
                  if(msrun_reader_sp == nullptr)
Langella Olivier's avatar
Langella Olivier committed
223
                    {
224

225
                      pappso::MsFileAccessor accessor(getFileName(), "");
226
227
228
                      accessor.setPreferedFileReaderType(
                        pappso::MzFormat::brukerTims,
                        pappso::FileReaderType::tims_ms2);
229
                      qDebug()
230
231
                        << " runid=" << getRunId() << " xmlid=" << getXmlId();

232
                      msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
233
                        getRunId(), getXmlId());
234

235
236
237
238
239
                      this->setMzFormat(msrun_reader_sp.get()
                                          ->getMsRunId()
                                          .get()
                                          ->getMzFormat());

240
241
                      freeMsRunReaderSp();
                      return msrun_reader_sp;
Langella Olivier's avatar
Langella Olivier committed
242
243
244
245
                    }
                }
              catch(pappso::PappsoException &error)
                {
246
                }
Langella Olivier's avatar
Langella Olivier committed
247
            }
248
249
250
          if((file.fileName().contains(onlyfilename)) ||
             (file.fileName().contains(basename)) ||
             (file.fileName().contains(this->getSampleName())))
251
            {
252
253
              try
                {
254
                  this->setFileName(file.absoluteFilePath());
255

256
                  if(msrun_reader_sp == nullptr)
257
                    {
258

259
                      pappso::MsFileAccessor accessor(getFileName(), "");
260
261
262
263

                      accessor.setPreferedFileReaderType(
                        pappso::MzFormat::brukerTims,
                        pappso::FileReaderType::tims_ms2);
264
                      qDebug()
265
                        << " runid=" << getRunId() << " xmlid=" << getXmlId();
266
                      msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
267
                        getRunId(), getXmlId());
268

269
270
271
272
                      this->setMzFormat(msrun_reader_sp.get()
                                          ->getMsRunId()
                                          .get()
                                          ->getMzFormat());
273
274
                      freeMsRunReaderSp();
                      return msrun_reader_sp;
275
276
277
278
                    }
                }
              catch(pappso::PappsoException &error)
                {
279
                  msrun_reader_sp = nullptr;
280
                }
Langella Olivier's avatar
Langella Olivier committed
281
282
283
            }
        }
    }
284
  this->setFileName(file_original.absoluteFilePath());
Langella Olivier's avatar
Langella Olivier committed
285
  freeMsRunReaderSp();
286
287
  qDebug();
  return msrun_reader_sp;
Langella Olivier's avatar
Langella Olivier committed
288
289
}

290
291
292
void
MsRun::checkMsRunStatistics()
{
293

294
295
  MsRunStatisticsHandler stats;
  checkMsRunStatistics(&stats);
Langella Olivier's avatar
Langella Olivier committed
296
}
297

298
299
300
void
MsRun::checkMsRunStatistics(MsRunStatisticsHandler *currentHandler)
{
301

302
303
304
305
306
  QVariant msrun_var = getMsRunStatistics(MsRunStatistics::total_spectra);
  if(msrun_var.isNull())
    {
      try
        {
307

308
          _msrun_reader_sp = getMsRunReaderSPtr();
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
          if(this->getMzFormat() == pappso::MzFormat::brukerTims)
            {
              pappso::TimsMsRunReaderMs2 *tims2_reader =
                dynamic_cast<pappso::TimsMsRunReaderMs2 *>(
                  _msrun_reader_sp.get());
              if(tims2_reader != nullptr)
                {
                  pappso::TimsDataSp tims_data =
                    tims2_reader->getTimsDataSPtr();
                  if(currentHandler->shouldStop())
                    { // statistics handler was stopped : don't use collected
                      // statistics because it is only partial
                    }
                  else
                    {
                      setMsRunStatistics(MsRunStatistics::total_spectra, "NA");
                      // not the number of MS1 or MS2 but the number of scans
                      // and
                      // precursors.
                      // Compute from th mobillity results
                      setMsRunStatistics(
                        MsRunStatistics::total_spectra_ms1,
                        (unsigned int)tims_data->getTotalNumberOfPrecursors());
                      setMsRunStatistics(
                        MsRunStatistics::total_spectra_ms2,
                        (unsigned int)tims_data->getTotalNumberOfScans());
                      setMsRunStatistics(MsRunStatistics::total_spectra_ms3, 0);
                      setMsRunStatistics(MsRunStatistics::tic_spectra_ms1,
                                         "NA");
                      setMsRunStatistics(MsRunStatistics::tic_spectra_ms2,
                                         "NA");
                      setMsRunStatistics(MsRunStatistics::tic_spectra_ms3,
                                         "NA");
                    }
                }
344
345
346
            }
          else
            {
347
348
              _msrun_reader_sp.get()->readSpectrumCollection(*currentHandler);

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
              setMsRunStatistics(MsRunStatistics::total_spectra,
                                 (unsigned int)currentHandler->getTotalCount());
              setMsRunStatistics(
                MsRunStatistics::total_spectra_ms1,
                (unsigned int)currentHandler->getMsLevelCount(1));
              setMsRunStatistics(
                MsRunStatistics::total_spectra_ms2,
                (unsigned int)currentHandler->getMsLevelCount(2));
              setMsRunStatistics(
                MsRunStatistics::total_spectra_ms3,
                (unsigned int)currentHandler->getMsLevelCount(3));
              setMsRunStatistics(MsRunStatistics::tic_spectra_ms1,
                                 currentHandler->getMsLevelTic(1));
              setMsRunStatistics(MsRunStatistics::tic_spectra_ms2,
                                 currentHandler->getMsLevelTic(2));
              setMsRunStatistics(MsRunStatistics::tic_spectra_ms3,
                                 currentHandler->getMsLevelTic(3));
            }
367
368
369
370
371
372
        }
      catch(pappso::ExceptionNotFound &error)
        {
          // no file found, no statistics
        }
    }
373
  freeMsRunReaderSp();
374
375
}

Langella Olivier's avatar
Langella Olivier committed
376
377
pappso::MsRunXicExtractorInterfaceSp
MsRun::getMsRunXicExtractorInterfaceSp()
378
379
380
381
382
{
  if(_xic_extractor_sp != nullptr)
    {
      return _xic_extractor_sp;
    }
383
384
  qDebug();
  _msrun_reader_sp = this->findMsRunFile();
385
  qDebug();
386
  if(_msrun_reader_sp != nullptr)
387
388
    {

389
      qDebug();
390
391
392
393
394
395
396
397
      pappso::MsRunXicExtractorFactory::getInstance().setTmpDir(
        QDir::tempPath());

      QSettings settings;
      QString xic_extraction_method =
        settings.value("global/xic_extractor", "pwiz").toString();
      if(xic_extraction_method == "pwiz")
        {
Langella Olivier's avatar
Langella Olivier committed
398
399
400
          pappso::MsRunXicExtractorFactory::getInstance()
            .setMsRunXicExtractorFactoryType(
              pappso::MsRunXicExtractorFactoryType::direct);
401
          _xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
Langella Olivier's avatar
Langella Olivier committed
402
                                .buildMsRunXicExtractorSp(_msrun_reader_sp);
403
404
405
        }
      else
        {
Langella Olivier's avatar
Langella Olivier committed
406
407
408
409
410
          pappso::MsRunXicExtractorFactory::getInstance()
            .setMsRunXicExtractorFactoryType(
              pappso::MsRunXicExtractorFactoryType::diskbuffer);
          _xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
                                .buildMsRunXicExtractorSp(_msrun_reader_sp);
411
        }
412
413
      //_xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
      //                      .buildMsRunXicExtractorDiskBufferSp(*this);
414
415

      qDebug();
416
    }
417
  freeMsRunReaderSp();
418
419
  return _xic_extractor_sp;
}
420
421

void
422
MsRun::buildMsRunRetentionTime(
423
424
  const PeptideEvidenceStore &peptide_evidence_store)
{
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  pappso::MsRunReaderSPtr reader;
  try
    {
      reader = getMsRunReaderSPtr();
    }
  catch(pappso::PappsoException &error)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr(
          "Error while processing MSrun \"%1\" for sample named \"%2\" :\n%3")
          .arg(getXmlId())
          .arg(getSampleName())
          .arg(error.qwhat()));
    }
Langella Olivier's avatar
Langella Olivier committed
439
440
441
  if(reader == nullptr)
    {
      throw pappso::ExceptionNotFound(
442
443
        QObject::tr(
          "MSrun \"%1\" for sample named \"%2\" file \"%3\" not found")
Langella Olivier's avatar
Langella Olivier committed
444
          .arg(getXmlId())
445
446
          .arg(getSampleName())
          .arg(getFileName()));
Langella Olivier's avatar
Langella Olivier committed
447
    }
448
449
450
451
452
  if(mpa_msrunRetentionTime == nullptr)
    {
      mpa_msrunRetentionTime =
        new pappso::MsRunRetentionTime<const pappso::Peptide *>(reader);
    }
453
454
  for(auto &peptide_evidence : peptide_evidence_store.getPeptideEvidenceList())
    {
455
      if(peptide_evidence.get()->isValid())
456
        {
457
458
459
460
461
462
463
464
465
466
          if(peptide_evidence.get()->getMsRunP() == this)
            {
              mpa_msrunRetentionTime->addPeptideAsSeamark(
                peptide_evidence.get()
                  ->getPeptideXtpSp()
                  .get()
                  ->getNativePeptideP(),
                reader.get()->scanNumber2SpectrumIndex(
                  peptide_evidence.get()->getScanNumber()));
            }
467
468
        }
    }
469
  reader.get()->releaseDevice();
470
  // mpa_msrunRetentionTime->computePeptideRetentionTimes();
471
  freeMsRunReaderSp();
472
473
474
475
476
}

void
MsRun::computeMsRunRetentionTime()
{
477
  if(mpa_msrunRetentionTime != nullptr)
478
    {
479
480
      qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
               << " msrun id=" << getXmlId();
481
482
483
484
485
486
487
      mpa_msrunRetentionTime->computePeptideRetentionTimes();
    }
}

void
MsRun::clearMsRunRetentionTime()
{
488
  if(mpa_msrunRetentionTime != nullptr)
489
490
491
    {
      delete mpa_msrunRetentionTime;
      mpa_msrunRetentionTime = nullptr;
492
493
    }
}
494
495
496
497
498
499
500


pappso::MsRunRetentionTime<const pappso::Peptide *> *
MsRun::getMsRunRetentionTimePtr()
{
  return mpa_msrunRetentionTime;
}
501
502


Renne Thomas's avatar
Renne Thomas committed
503
MsRunAlignmentGroupSp
504
505
MsRun::getAlignmentGroup()
{
Renne Thomas's avatar
Renne Thomas committed
506
  return msp_alignmentGroup;
507
508
509
}

void
Renne Thomas's avatar
Renne Thomas committed
510
MsRun::setAlignmentGroup(MsRunAlignmentGroupSp new_group)
511
{
Renne Thomas's avatar
Renne Thomas committed
512
  msp_alignmentGroup = new_group;
513
}
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566


pappso::MassSpectrumCstSPtr
MsRun::getMassSpectrumCstSPtrByScanNumber(std::size_t scan_number)
{

  qDebug() << "scan=" << scan_number;
  if(getMzFormat() == pappso::MzFormat::MGF)
    {
      scan_number = scan_number - 1;
      qDebug() << "MGF format";
    }
  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();

  if(msrun_reader_sp == nullptr)
    {
      msrun_reader_sp = findMsRunFile();
      if(msrun_reader_sp == nullptr)
        {
          throw pappso::ExceptionNotFound(
            QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
        }
    }
  qDebug();
  msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp == nullptr)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
    }


  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__  <<
  // _ms_run_sp.get()->getMsRunReaderSp().get()->getSpectrumListSize();

  std::size_t spectrum_index = scan_number;

  if(msrun_reader_sp.get()->hasScanNumbers())
    {
      qDebug() << "HAS SCAN NUMBERS";
      spectrum_index =
        msrun_reader_sp.get()->scanNumber2SpectrumIndex(scan_number);
    }

  qDebug() << " spectrum file="
           << msrun_reader_sp.get()->getMsRunId().get()->getFileName()
           << " index=" << spectrum_index;
  pappso::MassSpectrumCstSPtr spectrum_sp =
    msrun_reader_sp.get()->massSpectrumCstSPtr(spectrum_index);
  qDebug();
  return spectrum_sp;
}

567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
pappso::QualifiedMassSpectrum
MsRun::getQualifiedMassSpectrumByScanNumber(std::size_t scan_number, bool data)
{

  qDebug() << "scan=" << scan_number;
  if(getMzFormat() == pappso::MzFormat::MGF)
    {
      scan_number = scan_number - 1;
      qDebug() << "MGF format";
    }
  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();

  if(msrun_reader_sp == nullptr)
    {
      msrun_reader_sp = findMsRunFile();
      if(msrun_reader_sp == nullptr)
        {
          throw pappso::ExceptionNotFound(
            QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
        }
    }
  qDebug();
  msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp == nullptr)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
    }


  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__  <<
  // _ms_run_sp.get()->getMsRunReaderSp().get()->getSpectrumListSize();

  std::size_t spectrum_index = scan_number;

  if(msrun_reader_sp.get()->hasScanNumbers())
    {
      qDebug() << "HAS SCAN NUMBERS";
      spectrum_index =
        msrun_reader_sp.get()->scanNumber2SpectrumIndex(scan_number);
    }

  qDebug() << " spectrum file="
           << msrun_reader_sp.get()->getMsRunId().get()->getFileName()
           << " index=" << spectrum_index;
  return msrun_reader_sp.get()->qualifiedMassSpectrum(spectrum_index, data);
}


616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
pappso::MassSpectrumCstSPtr
MsRun::getMassSpectrumCstSPtrBySpectrumIndex(std::size_t spectrum_index)
{

  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();

  if(msrun_reader_sp == nullptr)
    {
      msrun_reader_sp = findMsRunFile();
      if(msrun_reader_sp == nullptr)
        {
          throw pappso::ExceptionNotFound(
            QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
        }
    }
  qDebug();
  msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp == nullptr)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
    }

  qDebug() << " spectrum file="
           << msrun_reader_sp.get()->getMsRunId().get()->getFileName()
           << " index=" << spectrum_index;
  pappso::MassSpectrumCstSPtr spectrum_sp =
    msrun_reader_sp.get()->massSpectrumCstSPtr(spectrum_index);
  qDebug();
  return spectrum_sp;
}
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

pappso::QualifiedMassSpectrum
MsRun::getQualifiedMassSpectrumBySpectrumIndex(std::size_t spectrum_index,
                                               bool data)
{

  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();

  if(msrun_reader_sp == nullptr)
    {
      msrun_reader_sp = findMsRunFile();
      if(msrun_reader_sp == nullptr)
        {
          throw pappso::ExceptionNotFound(
            QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
        }
    }
  qDebug();
  msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp == nullptr)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
    }

  qDebug() << " spectrum file="
           << msrun_reader_sp.get()->getMsRunId().get()->getFileName()
           << " index=" << spectrum_index;
  return msrun_reader_sp.get()->qualifiedMassSpectrum(spectrum_index, data);
}
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720


std::size_t
MsRun::scanNumber2SpectrumIndex(std::size_t scan_number)
{

  qDebug() << "scan=" << scan_number;
  if(getMzFormat() == pappso::MzFormat::MGF)
    {
      scan_number = scan_number - 1;
      qDebug() << "MGF format";
    }
  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();

  if(msrun_reader_sp == nullptr)
    {
      msrun_reader_sp = findMsRunFile();
      if(msrun_reader_sp == nullptr)
        {
          throw pappso::ExceptionNotFound(
            QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
        }
    }
  qDebug();
  msrun_reader_sp = getMsRunReaderSPtr();
  if(msrun_reader_sp == nullptr)
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file not found").arg(getFileName()));
    }


  if(msrun_reader_sp.get()->hasScanNumbers())
    {
      qDebug() << "HAS SCAN NUMBERS";
      return msrun_reader_sp.get()->scanNumber2SpectrumIndex(scan_number);
    }
  else
    {
      throw pappso::ExceptionNotFound(
        QObject::tr("mz data \"%1\" file does not contain scan numbers")
          .arg(getFileName()));
    }
}