Skip to content
Snippets Groups Projects
msrun.cpp 5.23 KiB
Newer Older
/**
 * \filed core/msrun.cpp
 * \date 5/4/2017
 * \author Olivier Langella
 * \brief describes an MS run (chemical sample injected in a mass spectrometer)
 */


/*******************************************************************************
 * 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
 ******************************************************************************/


#include "msrun.h"
#include <QFileInfo>
Langella Olivier's avatar
Langella Olivier committed
#include <QSettings>
#include <QDebug>
#include <QDir>
#include <pappsomspp/msrun/msrunreaderfactory.h>
#include <pappsomspp/exception/exceptionnotfound.h>
#include <pappsomspp/xicextractor/msrunxicextractorfactory.h>
#include "../utils/msrunstatisticshandler.h"
MsRun::MsRun(const QString &location) : pappso::MsRunId(location)
{
  QFileInfo fileinfo(location);
MsRun::MsRun(const MsRun &other) : pappso::MsRunId(other)
{
  _param_stats = other._param_stats;
pappso::MsRunReaderSp &
MsRun::getMsRunReaderSp()
{
  return _msrun_reader_sp;
Langella Olivier's avatar
Langella Olivier committed
}
void
MsRun::setMsRunStatistics(MsRunStatistics param, const QVariant &value)
{
  _param_stats.insert(std::pair<MsRunStatistics, QVariant>(param, value));
const std::map<MsRunStatistics, QVariant> &
MsRun::getMsRunStatisticsMap() const
{
  return _param_stats;
const QVariant
MsRun::getMsRunStatistics(MsRunStatistics param) const
{
  try
    {
      return _param_stats.at(param);
  catch(std::out_of_range)
    {
      return QVariant();
bool
MsRun::findMsRunFile()
{
  QFileInfo file(this->getFilename());
  bool file_ok = false;
  if(file.exists())
    {
      file_ok = true;
  QString basename     = file.baseName();
  QString onlyfilename = file.fileName();
  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())
            {
              this->setFilename(file.absoluteFilePath());
              file_ok = true;
Langella Olivier's avatar
Langella Olivier committed
            }
          if(file.fileName().contains(onlyfilename))
            {
              this->setFilename(file.absoluteFilePath());
              file_ok = true;
  if(file_ok)
    {
      if(_msrun_reader_sp == nullptr)
        {
          _msrun_reader_sp =
            pappso::MsRunReaderFactory::getInstance().buildPwizMsRunReaderSp(
              *this);
  return file_ok;
void
MsRun::checkMsRunStatistics()
{
  QVariant msrun_var = getMsRunStatistics(MsRunStatistics::total_spectra);
  if(msrun_var.isNull())
    {
      try
        {
          MsRunStatisticsHandler stats;
          _msrun_reader_sp.get()->readSpectrumCollection(stats);

          setMsRunStatistics(MsRunStatistics::total_spectra,
                             (unsigned int)stats.getTotalCount());
          setMsRunStatistics(MsRunStatistics::total_spectra_ms1,
                             (unsigned int)stats.getMsLevelCount(1));
          setMsRunStatistics(MsRunStatistics::total_spectra_ms2,
                             (unsigned int)stats.getMsLevelCount(2));
          setMsRunStatistics(MsRunStatistics::total_spectra_ms3,
                             (unsigned int)stats.getMsLevelCount(3));
          setMsRunStatistics(MsRunStatistics::tic_spectra_ms1,
                             (unsigned int)stats.getMsLevelTic(1));
          setMsRunStatistics(MsRunStatistics::tic_spectra_ms2,
                             (unsigned int)stats.getMsLevelTic(2));
          setMsRunStatistics(MsRunStatistics::tic_spectra_ms3,
                             (unsigned int)stats.getMsLevelTic(3));
Langella Olivier's avatar
Langella Olivier committed
        }
      catch(pappso::ExceptionNotFound &error)
        {
          // no file found, no statistics

pappso::MsRunXicExtractorSp
MsRun::getMsRunXicExtractorSp()
{
  if(_xic_extractor_sp != nullptr)
    {
      return _xic_extractor_sp;
    }
  if(this->findMsRunFile())
    {

      pappso::MsRunXicExtractorFactory::getInstance().setTmpDir("/tmp");
      //_xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
      //                      .buildMsRunXicExtractorDiskBufferSp(*this);
      _xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
                            .buildMsRunXicExtractorPwizSp(*this);
    }
  return _xic_extractor_sp;
}