/** * \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> #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; } MsRun::~MsRun() { } pappso::MsRunReaderSp & MsRun::getMsRunReaderSp() { qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " filename=" << getFilename(); if(_msrun_reader_sp == nullptr) { try { _msrun_reader_sp = pappso::MsRunReaderFactory::getInstance().buildPwizMsRunReaderSp( *this); } catch(pappso::PappsoException &error) { _msrun_reader_sp = nullptr; } } return _msrun_reader_sp; } 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() { // first look at the file path :) _msrun_reader_sp = getMsRunReaderSp(); if(_msrun_reader_sp != nullptr) return true; QFileInfo file_original(this->getFilename()); QString basename = file_original.baseName(); QString onlyfilename = file_original.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()); try { if(_msrun_reader_sp == nullptr) { _msrun_reader_sp = pappso::MsRunReaderFactory::getInstance() .buildPwizMsRunReaderSp(*this); return true; } } catch(pappso::PappsoException &error) { _msrun_reader_sp = nullptr; } } if(file.fileName().contains(onlyfilename)) { try { this->setFilename(file.absoluteFilePath()); if(_msrun_reader_sp == nullptr) { _msrun_reader_sp = pappso::MsRunReaderFactory::getInstance() .buildPwizMsRunReaderSp(*this); return true; } } catch(pappso::PappsoException &error) { _msrun_reader_sp = nullptr; } } } } this->setFilename(file_original.absoluteFilePath()); _msrun_reader_sp = nullptr; return false; } 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, stats.getMsLevelTic(1)); setMsRunStatistics(MsRunStatistics::tic_spectra_ms2, stats.getMsLevelTic(2)); setMsRunStatistics(MsRunStatistics::tic_spectra_ms3, stats.getMsLevelTic(3)); } 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; }