From 7ea294d806f837a52938459786dfac4b6283d309 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Thu, 21 Jun 2018 15:42:16 +0200
Subject: [PATCH] xic extractor is now embedded in msrun

---
 src/core/msrun.cpp                   | 210 +++++++++++++++++----------
 src/core/msrun.h                     |   6 +
 src/gui/xic_view/xicworkerthread.cpp | 141 +++++++++---------
 3 files changed, 214 insertions(+), 143 deletions(-)

diff --git a/src/core/msrun.cpp b/src/core/msrun.cpp
index d1a0ad8ce..3f577aa1e 100644
--- a/src/core/msrun.cpp
+++ b/src/core/msrun.cpp
@@ -7,26 +7,27 @@
 
 
 /*******************************************************************************
-* 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
-******************************************************************************/
+ * 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"
@@ -36,92 +37,147 @@
 #include <QDir>
 #include <pappsomspp/msrun/msrunreaderfactory.h>
 #include <pappsomspp/exception/exceptionnotfound.h>
+#include <pappsomspp/xicextractor/msrunxicextractorfactory.h>
 
-MsRun::MsRun(const QString & location):pappso::MsRunId(location) {
-    QFileInfo fileinfo(location);
+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(const MsRun &other) : pappso::MsRunId(other)
+{
+  _param_stats = other._param_stats;
 }
 
-MsRun::~MsRun() {
+MsRun::~MsRun()
+{
 }
-pappso::MsRunReaderSp & MsRun::getMsRunReaderSp() {
-    return _msrun_reader_sp;
+pappso::MsRunReaderSp &
+MsRun::getMsRunReaderSp()
+{
+  return _msrun_reader_sp;
 }
 
-void MsRun::setMsRunStatistics(MsRunStatistics param, const QVariant& value) {
-    _param_stats.insert(std::pair<MsRunStatistics, QVariant>(param, value));
+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 std::map<MsRunStatistics, QVariant> &
+MsRun::getMsRunStatisticsMap() const
+{
+  return _param_stats;
 }
 
-const QVariant MsRun::getMsRunStatistics(MsRunStatistics param) const {
-    try {
-        return _param_stats.at(param);
+const QVariant
+MsRun::getMsRunStatistics(MsRunStatistics param) const
+{
+  try
+    {
+      return _param_stats.at(param);
     }
-    catch (std::out_of_range) {
-        return QVariant();
+  catch(std::out_of_range)
+    {
+      return QVariant();
     }
 }
 
-bool MsRun::findMsRunFile() {
+bool
+MsRun::findMsRunFile()
+{
 
-    QFileInfo file(this->getFilename());
-    bool file_ok = false;
+  QFileInfo file(this->getFilename());
+  bool file_ok = false;
 
-    if (file.exists()) {
-        file_ok = true;
+  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;
+  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;
             }
-            if (file.fileName().contains(onlyfilename)) {
-                this->setFilename(file.absoluteFilePath());
-                file_ok =  true;
+          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);
+  if(file_ok)
+    {
+      if(_msrun_reader_sp == nullptr)
+        {
+          _msrun_reader_sp =
+            pappso::MsRunReaderFactory::getInstance().buildPwizMsRunReaderSp(
+              *this);
         }
     }
-    return file_ok;
+  return file_ok;
 }
 
-void MsRun::checkMsRunStatistics() {
-       QVariant msrun_var = getMsRunStatistics(MsRunStatistics::total_spectra);
-    if (msrun_var.isNull()) {
-        try {
-            pappso::MsRunSimpleStatistics 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));
+void
+MsRun::checkMsRunStatistics()
+{
+  QVariant msrun_var = getMsRunStatistics(MsRunStatistics::total_spectra);
+  if(msrun_var.isNull())
+    {
+      try
+        {
+          pappso::MsRunSimpleStatistics 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));
         }
-        catch (pappso::ExceptionNotFound& error) {
-            //no file found, no statistics
+      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;
+}
diff --git a/src/core/msrun.h b/src/core/msrun.h
index fd711b1e0..08f04a853 100644
--- a/src/core/msrun.h
+++ b/src/core/msrun.h
@@ -37,6 +37,7 @@
 #include <QVariant>
 #include <pappsomspp/msrun/msrunid.h>
 #include <pappsomspp/msrun/msrunreader.h>
+#include <pappsomspp/xicextractor/msrunxicextractor.h>
 #include "../utils/types.h"
 
 class MsRun;
@@ -69,11 +70,16 @@ public:
     pappso::MsRunReaderSp & getMsRunReaderSp();
     
     void checkMsRunStatistics();
+    
+    pappso::MsRunXicExtractorSp getMsRunXicExtractorSp();
 
 private :
     std::map<MsRunStatistics, QVariant> _param_stats;
     
     pappso::MsRunReaderSp _msrun_reader_sp;
+    
+    pappso::MsRunXicExtractorSp _xic_extractor_sp;
+    
 };
 
 #endif // MSRUN_H
diff --git a/src/gui/xic_view/xicworkerthread.cpp b/src/gui/xic_view/xicworkerthread.cpp
index f82119f59..98bc21116 100644
--- a/src/gui/xic_view/xicworkerthread.cpp
+++ b/src/gui/xic_view/xicworkerthread.cpp
@@ -5,92 +5,101 @@
  * \brief XIC worker
  */
 /*******************************************************************************
-* Copyright (c) 2018 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
-******************************************************************************/
+ * Copyright (c) 2018 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 <tuple>
 #include "xicworkerthread.h"
 #include <pappsomspp/peptide/peptidenaturalisotopelist.h>
 #include <pappsomspp/pappsoexception.h>
-#include <pappsomspp/xicextractor/msrunxicextractorfactory.h>
 #include <pappsomspp/exception/exceptionnotfound.h>
 #include <QDebug>
 
-void XicWorkerThread::doXicLoad(MsRunSp p_msrun, std::vector<pappso::mz> mz_list, pappso::PrecisionP precision, pappso::XicExtractMethod method) {
+void
+XicWorkerThread::doXicLoad(MsRunSp p_msrun, std::vector<pappso::mz> mz_list,
+                           pappso::PrecisionP precision,
+                           pappso::XicExtractMethod method)
+{
 
-    try {
-        std::vector<pappso::MassRange> mass_list;
-        for (pappso::mz mass:mz_list) {
-            mass_list.push_back(pappso::MassRange(mass, precision));
+  try
+    {
+      std::vector<pappso::MassRange> mass_list;
+      for(pappso::mz mass : mz_list)
+        {
+          mass_list.push_back(pappso::MassRange(mass, precision));
         }
-        //std::vector<pappso::XicSp> xic_sp_list = SpectrumStore::getXicSpFromMsRunSp(p_msrun.get(), mass_list, method);
+      // std::vector<pappso::XicSp> xic_sp_list =
+      // SpectrumStore::getXicSpFromMsRunSp(p_msrun.get(), mass_list, method);
 
-        if (p_msrun.get()->findMsRunFile()) {
-            pappso::MsRunId msrun_id = * (p_msrun.get());
-            qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " " << msrun_id.getFilename();
-            auto it = std::find_if(_extractor_list.begin(), _extractor_list.end(), [msrun_id](const pappso::MsRunXicExtractorSp & extractor_sp) {
-                return extractor_sp.get()->getMsRunId().getXmlId() == msrun_id.getXmlId();
-            });
+      pappso::MsRunXicExtractorSp extractor =
+        p_msrun.get()->getMsRunXicExtractorSp();
+      if(extractor != nullptr)
+        {
+          qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
+          extractor.get()->setXicExtractMethod(method);
 
-            pappso::MsRunXicExtractorSp extractor;
-            if (it == _extractor_list.end()) {
 
-                pappso::MsRunXicExtractorFactory::getInstance().setTmpDir("/tmp");
-                extractor =  pappso::MsRunXicExtractorFactory::getInstance().buildMsRunXicExtractorDiskBufferSp(msrun_id);
-                _extractor_list.push_back(extractor);
-            }
-            else {
-                extractor = *it;
-            }
-            qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ ;
-            extractor.get()->setXicExtractMethod(method);
+          qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
+          std::vector<pappso::XicSp> xic_sp_list =
+            extractor.get()->getXicSpList(mass_list);
 
-
-            qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ ;
-            std::vector<pappso::XicSp> xic_sp_list = extractor.get()->getXicSpList(mass_list);
-
-            qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ ;
-            emit xicLoaded(xic_sp_list);
+          qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
+          emit xicLoaded(xic_sp_list);
         }
-
     }
-    catch (pappso::PappsoException & error) {
-        emit operationFailed(tr("Error extracting XIC for MSrun %1:\n%2").arg(p_msrun->getFilename()).arg(error.qwhat()));
+  catch(pappso::PappsoException &error)
+    {
+      emit operationFailed(tr("Error extracting XIC for MSrun %1:\n%2")
+                             .arg(p_msrun->getFilename())
+                             .arg(error.qwhat()));
     }
 }
 
 
-void XicWorkerThread::doComputeIsotopeMassList(pappso::PeptideSp peptide_sp, unsigned int charge, pappso::PrecisionP precision, double minimum_isotope_pattern_ratio) {
-    qDebug() << "XicWorkerThread::doComputeIsotopeMassList " << precision->toString();
-    std::vector<pappso::PeptideNaturalIsotopeAverageSp> isotope_mass_list;
-    //compute isotope masses :
-    if (peptide_sp != nullptr) {
-        pappso::PeptideNaturalIsotopeList isotope_list(peptide_sp);
-        isotope_mass_list = isotope_list.getByIntensityRatio(charge, precision, minimum_isotope_pattern_ratio);
+void
+XicWorkerThread::doComputeIsotopeMassList(pappso::PeptideSp peptide_sp,
+                                          unsigned int charge,
+                                          pappso::PrecisionP precision,
+                                          double minimum_isotope_pattern_ratio)
+{
+  qDebug() << "XicWorkerThread::doComputeIsotopeMassList "
+           << precision->toString();
+  std::vector<pappso::PeptideNaturalIsotopeAverageSp> isotope_mass_list;
+  // compute isotope masses :
+  if(peptide_sp != nullptr)
+    {
+      pappso::PeptideNaturalIsotopeList isotope_list(peptide_sp);
+      isotope_mass_list = isotope_list.getByIntensityRatio(
+        charge, precision, minimum_isotope_pattern_ratio);
 
-        std::sort (isotope_mass_list.begin(), isotope_mass_list.end(), [](const pappso::PeptideNaturalIsotopeAverageSp & m,const pappso::PeptideNaturalIsotopeAverageSp & n)-> bool {
-            unsigned int mn(m.get()->getIsotopeNumber()), nn(n.get()->getIsotopeNumber());
-            unsigned int mr(m.get()->getIsotopeRank()), nr(n.get()->getIsotopeRank());
-            return (std::tie(mn, mr) < std::tie(nn, nr));
-        });
-        emit isotopeMassListComputed(isotope_mass_list);
+      std::sort(isotope_mass_list.begin(), isotope_mass_list.end(),
+                [](const pappso::PeptideNaturalIsotopeAverageSp &m,
+                   const pappso::PeptideNaturalIsotopeAverageSp &n) -> bool {
+                  unsigned int mn(m.get()->getIsotopeNumber()),
+                    nn(n.get()->getIsotopeNumber());
+                  unsigned int mr(m.get()->getIsotopeRank()),
+                    nr(n.get()->getIsotopeRank());
+                  return (std::tie(mn, mr) < std::tie(nn, nr));
+                });
+      emit isotopeMassListComputed(isotope_mass_list);
     }
 }
-- 
GitLab