From 7f2d3bad9e2ae079983f52ce4fb165a27c77e28b Mon Sep 17 00:00:00 2001
From: Thomas Renne <thomas.renne@u-psud.fr>
Date: Tue, 21 Apr 2020 10:43:07 +0200
Subject: [PATCH] Add TDF MsRunStatistics to MsIdentification view

---
 src/core/msrun.cpp                            | 37 +++++++++++++++++++
 src/core/msrun.h                              |  1 +
 .../msidlistworkerstatus.cpp                  | 17 +++++++++
 .../msidlistworkerstatus.h                    |  7 ++--
 .../msidentificationtablemodel.cpp            | 10 -----
 .../msidentificationtablemodel.h              |  3 +-
 src/input/pepxmlsaxhandler.cpp                |  2 +-
 src/utils/types.h                             |  3 +-
 8 files changed, 63 insertions(+), 17 deletions(-)

diff --git a/src/core/msrun.cpp b/src/core/msrun.cpp
index e7ceae2b..f523a05b 100644
--- a/src/core/msrun.cpp
+++ b/src/core/msrun.cpp
@@ -335,6 +335,43 @@ MsRun::checkMsRunStatistics(MsRunStatisticsHandler *currentHandler)
     }
 }
 
+void
+MsRun::checkMsRunStatisticsForTdf(MsRunStatisticsHandler *currentHandler)
+{
+  QVariant msrun_var = getMsRunStatistics(MsRunStatistics::total_spectra);
+  if(msrun_var.isNull())
+    {
+      try
+        {
+          QString directory =
+            QFileInfo(getFileName()).absoluteDir().absolutePath();
+          pappso::TimsData *tims_data = new pappso::TimsData(directory);
+          if(currentHandler->shouldStop())
+            { // statistics handler was stopped : don't use collected statistics
+              // because it is only partial
+            }
+          else
+            {
+              setMsRunStatistics(MsRunStatistics::total_spectra, "NA");
+              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");
+            }
+        }
+      catch(pappso::ExceptionNotFound &error)
+        {
+          // no file found, no statistics
+        }
+    }
+}
+
 pappso::MsRunXicExtractorInterfaceSp
 MsRun::getMsRunXicExtractorInterfaceSp()
 {
diff --git a/src/core/msrun.h b/src/core/msrun.h
index e6436651..031c1e28 100644
--- a/src/core/msrun.h
+++ b/src/core/msrun.h
@@ -83,6 +83,7 @@ class MsRun : public pappso::MsRunId
 
   void checkMsRunStatistics();
   void checkMsRunStatistics(MsRunStatisticsHandler *currentHandler);
+  void checkMsRunStatisticsForTdf(MsRunStatisticsHandler *currentHandler);
 
   pappso::MsRunXicExtractorInterfaceSp getMsRunXicExtractorInterfaceSp();
 
diff --git a/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.cpp b/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.cpp
index dc17389a..3c865363 100644
--- a/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.cpp
+++ b/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.cpp
@@ -180,6 +180,23 @@ MsIdListWorkerStatus::doComputeMsNumberForTDF(
     {
       try
         {
+          if(mpa_currentHandler == nullptr)
+            {
+              mpa_currentHandler =
+                new MsIdentificationTableMsRunStatisticsHandler;
+              identificationDataSource->getMsRunSp()
+                ->checkMsRunStatisticsForTdf(mpa_currentHandler);
+              if(!mpa_currentHandler->shouldStop())
+                {
+                  // if statistics computing is interrupted : don't take
+                  // into account results because it is partial
+                  m_identificationReady.push_back(
+                    identificationDataSource.get());
+                }
+
+              delete mpa_currentHandler;
+              mpa_currentHandler = nullptr;
+            }
         }
       catch(pappso::PappsoException &error)
         {
diff --git a/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.h b/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.h
index 2eb70ea9..ba61ff05 100644
--- a/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.h
+++ b/src/gui/lists/ms_identification_run_list_view/ms_identification_worker/msidlistworkerstatus.h
@@ -63,7 +63,6 @@ enum class WorkerStatus : std::int8_t
   Ready        = 3,
   NotReady     = 4,
   Error        = 5,
-  NotAvailable = 6,
 };
 class MsIdentificationListWindow;
 class MsIdListWorkerStatus
@@ -78,8 +77,10 @@ class MsIdListWorkerStatus
    */
   WorkerStatus getStatus(IdentificationDataSourceSp identificationDataSource);
   void doComputeMsNumber(IdentificationDataSourceSp identificationDataSource);
-  void doComputeMsNumberForTDF(IdentificationDataSourceSp identificationDataSource);
-  void doComputeMsNumberForMzXML(IdentificationDataSourceSp identificationDataSource);
+  void
+  doComputeMsNumberForTDF(IdentificationDataSourceSp identificationDataSource);
+  void doComputeMsNumberForMzXML(
+    IdentificationDataSourceSp identificationDataSource);
   void changeWaitingQueue(int row);
 
 
diff --git a/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.cpp b/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.cpp
index 9280959d..7b2d73f9 100644
--- a/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.cpp
+++ b/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.cpp
@@ -400,18 +400,8 @@ MsIdentificationTableModel::data(const QModelIndex &index, int role) const
           }
         if(col == (std::int8_t)msIdentificationListColumn::nb_tic_1)
           {
-            //             if(QFileInfo(m_identificationDataSourceSpList.at(row)
-            //                            ->getMsRunSp()
-            //                            ->getFileName())
-            //                  .completeSuffix() == "tdf")
-            //               {
-            //                 return QVariant("NA");
-            //               }
-            //             else
-            //               {
             return getMsRunStatisticsNumber(row,
                                             MsRunStatistics::tic_spectra_ms1);
-            //               }
           }
         if(col == (std::int8_t)msIdentificationListColumn::nb_tic_2)
           {
diff --git a/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.h b/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.h
index c43fb920..c843159f 100644
--- a/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.h
+++ b/src/gui/lists/ms_identification_run_list_view/msidentificationtablemodel.h
@@ -109,7 +109,8 @@ class MsIdentificationTableModel : public QAbstractTableModel
 
   private:
   static int getColumnWidth(int column);
-  QVariant getMsRunStatisticsFollowingFormat(MsRunSp msrun_sp, MsRunStatistics column) const;
+  QVariant getMsRunStatisticsFollowingFormat(MsRunSp msrun_sp,
+                                             MsRunStatistics column) const;
 
   private:
   std::vector<IdentificationDataSourceSp> m_identificationDataSourceSpList;
diff --git a/src/input/pepxmlsaxhandler.cpp b/src/input/pepxmlsaxhandler.cpp
index 4d3b8432..b47dc86c 100644
--- a/src/input/pepxmlsaxhandler.cpp
+++ b/src/input/pepxmlsaxhandler.cpp
@@ -332,7 +332,7 @@ PepXmlSaxHandler::startElement_spectrum_query(QXmlAttributes attributes)
 
 //<alternative_protein protein="sp|P46784|RS10B_YEAST" protein_descr="40S
 //       ribosomal protein S10-B OS=Saccharomyces cerevisiae (strain ATCC 204508
-//                                                    \
+//                                                     \
 //S288c) GN=RPS10B PE=1 SV=1" num_tol_term="2" peptide_prev_aa="K"
 // peptide_next_aa="N"/>
 bool
diff --git a/src/utils/types.h b/src/utils/types.h
index 4fcc17bb..83206f04 100644
--- a/src/utils/types.h
+++ b/src/utils/types.h
@@ -88,8 +88,7 @@ enum class PeptideEvidenceParam : std::int8_t
   comet_sprank  = 18, ///< MS:1002256  "The Comet result 'SpRank'." [PSI:PI]
   comet_expectation_value =
     19, ///< MS:1002257  "The Comet result 'Expectation value'." [PSI:PI]
-  pappso_qvalue =
-    20, ///< q-value computed with FDR = decoy / (decoy + target)
+  pappso_qvalue = 20, ///< q-value computed with FDR = decoy / (decoy + target)
 
 };
 
-- 
GitLab