From 01a1fa8e339bdbac0a109e2d35d6f136a87522f9 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Mon, 9 Jul 2018 12:05:40 +0200
Subject: [PATCH] mzidentml : circumvent buggy spectrum id

---
 src/input/identificationpwizreader.cpp | 65 ++++++++++++++++++++++----
 1 file changed, 56 insertions(+), 9 deletions(-)

diff --git a/src/input/identificationpwizreader.cpp b/src/input/identificationpwizreader.cpp
index 4cfb8bfe..45492efd 100644
--- a/src/input/identificationpwizreader.cpp
+++ b/src/input/identificationpwizreader.cpp
@@ -35,11 +35,13 @@
 #include <QFileInfo>
 #include <QDir>
 #include <QString>
+#include <QRegularExpression>
 #include <pappsomspp/pappsoexception.h>
 #include <pappsomspp/exception/exceptionoutofrange.h>
 
 #include <pwiz/data/common/ParamTypes.hpp>
 #include <pwiz/data/identdata/IdentData.hpp>
+#include <pwiz/data/msdata/MSData.hpp>
 #include <pwiz/data/identdata/IdentDataFile.hpp>
 #include "../core/peptidematch.h"
 #include <locale>
@@ -138,11 +140,11 @@ IdentificationPwizReader::getIdentificationEngine() const
       </AnalysisSoftware>
   </AnalysisSoftwareList>
   */
-  IdentificationEngine identification_engine = IdentificationEngine::peptider;
+  IdentificationEngine identification_engine = IdentificationEngine::unknown;
   for(pwiz::identdata::AnalysisSoftwarePtr software_ptr :
       _pwiz_ident_data_file->analysisSoftwareList)
     {
-      return getIdentificationEngine(software_ptr);
+      identification_engine = getIdentificationEngine(software_ptr);
     }
   return identification_engine;
 }
@@ -166,9 +168,17 @@ IdentificationPwizReader::getIdentificationEngine(const QString &xml_id) const
     {
       if(software_ptr.get()->id == xml_id.toStdString())
         {
-          return getIdentificationEngine(software_ptr);
+          identification_engine = getIdentificationEngine(software_ptr);
         }
     }
+
+  /*
+    throw pappso::PappsoException(
+      QObject::tr("Analysis software (%1 %2) not known to XTPcpp")
+        .arg(analysis_software_p->id.c_str())
+        .arg(analysis_software_p->name.c_str()));
+        */
+
   return identification_engine;
 }
 
@@ -192,10 +202,7 @@ IdentificationPwizReader::getIdentificationEngine(
     }
   else
     {
-      throw pappso::PappsoException(
-        QObject::tr("Analysis software (%1 %2) not known to XTPcpp")
-          .arg(analysis_software_p->id.c_str())
-          .arg(analysis_software_p->name.c_str()));
+      return IdentificationEngine::unknown;
     }
 
   return identification_engine;
@@ -364,8 +371,48 @@ IdentificationPwizReader::read(
                 }
               else
                 {
-                  throw pappso::PappsoException(
-                    QObject::tr("Scan number not found"));
+                  // spectrumID="20170616_test_digestion_liquide_2_2_1_1H4.mzXML
+                  // scan 2271 (charge 2)"
+                  QRegularExpression re("^.* scan (\\d+) .*$");
+                  QRegularExpressionMatch match =
+                    re.match(spectrum_ptr.get()->spectrumID.c_str());
+                  if(match.hasMatch())
+                    {
+                      // this is a bad hack to circumvent buggy spectrum id...
+                      scan = match.captured(1).toULong();
+                    }
+                  else
+                    {
+                      // last try : regular proteowizard spectrum id
+                      try
+                        {
+                          scan =
+                            QString(pwiz::msdata::id::value(
+                                      spectrum_ptr.get()->spectrumID, "scan")
+                                      .c_str())
+                              .toULong();
+                        }
+                      catch(runtime_error &pwiz_error)
+                        {
+                          throw pappso::PappsoException(
+                            QObject::tr("Pwiz runtime error :\n%1\n parsing "
+                                        "spectrum ID :\n%2")
+                              .arg(pwiz_error.what())
+                              .arg(spectrum_ptr.get()->spectrumID.c_str()));
+                        }
+                    }
+                  /* QString(pwiz::msdata::id::translateNativeIDToScanNumber(
+                             pwiz::cv::CVID::MS_mzIdentML_format,
+                             spectrum_ptr.get()->spectrumID)
+                             .c_str())
+                     .toULong();*/
+                  if(scan == 0)
+                    {
+                      throw pappso::PappsoException(
+                        QObject::tr(
+                          "Failure parsing spectrumID %1 to find scan number")
+                          .arg(spectrum_ptr.get()->spectrumID.c_str()));
+                    }
                 }
               //<cvParam accession="MS:1001115" cvRef="PSI-MS" value="4925"
               // name="scan number(s)"/>
-- 
GitLab