From 057f78d86a9bc18934398dfe5aaf8a96a49e0db4 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Wed, 31 Oct 2018 17:16:33 +0100
Subject: [PATCH] WIP: check tandem.exe binary and version

---
 .../tandem_run_dialog/tandem_run_dialog.ui    | 71 ++++++++++---------
 src/gui/tandem_run_dialog/tandemrundialog.cpp |  6 ++
 src/utils/utils.cpp                           | 60 ++++++++++++++++
 src/utils/utils.h                             |  1 +
 4 files changed, 106 insertions(+), 32 deletions(-)

diff --git a/src/gui/tandem_run_dialog/tandem_run_dialog.ui b/src/gui/tandem_run_dialog/tandem_run_dialog.ui
index 336fae989..1f1639d9a 100644
--- a/src/gui/tandem_run_dialog/tandem_run_dialog.ui
+++ b/src/gui/tandem_run_dialog/tandem_run_dialog.ui
@@ -28,7 +28,45 @@
      <property name="checked">
       <bool>false</bool>
      </property>
-     <layout class="QHBoxLayout" name="horizontalLayout_6">
+     <layout class="QVBoxLayout" name="verticalLayout">
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout_3">
+        <item>
+         <widget class="QLabel" name="tandem_bin_label">
+          <property name="text">
+           <string>TextLabel</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="horizontalSpacer_4">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>528</width>
+            <height>20</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+        <item>
+         <widget class="QPushButton" name="pushButton_4">
+          <property name="text">
+           <string>change</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </item>
+      <item>
+       <widget class="QLabel" name="tandem_version_label">
+        <property name="text">
+         <string>X!Tandem not found</string>
+        </property>
+       </widget>
+      </item>
       <item>
        <widget class="QWidget" name="exe_group_widget" native="true">
         <layout class="QVBoxLayout" name="verticalLayout_3">
@@ -47,37 +85,6 @@
          <property name="bottomMargin">
           <number>0</number>
          </property>
-         <item>
-          <layout class="QHBoxLayout" name="horizontalLayout_3">
-           <item>
-            <widget class="QLabel" name="tandem_bin_label">
-             <property name="text">
-              <string>TextLabel</string>
-             </property>
-            </widget>
-           </item>
-           <item>
-            <spacer name="horizontalSpacer_4">
-             <property name="orientation">
-              <enum>Qt::Horizontal</enum>
-             </property>
-             <property name="sizeHint" stdset="0">
-              <size>
-               <width>528</width>
-               <height>20</height>
-              </size>
-             </property>
-            </spacer>
-           </item>
-           <item>
-            <widget class="QPushButton" name="pushButton_4">
-             <property name="text">
-              <string>change</string>
-             </property>
-            </widget>
-           </item>
-          </layout>
-         </item>
          <item>
           <widget class="QGroupBox" name="use_htcondor_groupbox">
            <property name="title">
diff --git a/src/gui/tandem_run_dialog/tandemrundialog.cpp b/src/gui/tandem_run_dialog/tandemrundialog.cpp
index fd78ee5f5..def39f97e 100644
--- a/src/gui/tandem_run_dialog/tandemrundialog.cpp
+++ b/src/gui/tandem_run_dialog/tandemrundialog.cpp
@@ -37,6 +37,7 @@
 #include <QStandardPaths>
 #include <pappsomspp/pappsoexception.h>
 #include "../../files/tandemparametersfile.h"
+#include "../../utils/utils.h"
 
 // Q_DECLARE_METATYPE(QFileInfo)
 
@@ -182,6 +183,11 @@ TandemRunDialog::reset()
   QString tandem_bin_path =
     settings.value("path/tandem_bin", "/usr/bin/tandem").toString();
 
+
+  ui->tandem_version_label->setText(
+    Utils::checkXtandemVersion(tandem_bin_path));
+
+
   ui->output_directory_label->setText(default_output_location);
   ui->tandem_bin_label->setText(tandem_bin_path);
 
diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp
index 737282c2c..c236c399d 100644
--- a/src/utils/utils.cpp
+++ b/src/utils/utils.cpp
@@ -2,6 +2,8 @@
 #include <pappsomspp/exception/exceptionnotfound.h>
 #include <pappsomspp/mass_range.h>
 #include <cmath>
+#include <QProcess>
+#include <QDebug>
 
 const QUrl
 Utils::getOlsUrl(QString psimod_accession)
@@ -239,3 +241,61 @@ Utils::translateAaModificationFromUnimod(const QString &unimod_accession)
            << unimod_accession << " not found";
   return nullptr;
 }
+
+
+const QString
+Utils::checkXtandemVersion(const QString &tandem_bin_path)
+{
+  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
+  QString version_return;
+  QStringList arguments;
+
+  arguments << "-v";
+
+  QProcess *xt_process = new QProcess();
+  // hk_process->setWorkingDirectory(QFileInfo(_hardklor_exe).absolutePath());
+
+  xt_process->start(tandem_bin_path, arguments);
+
+  if(!xt_process->waitForStarted())
+    {
+      throw pappso::PappsoException(
+        QObject::tr("X!Tandem process failed to start"));
+    }
+
+  while(xt_process->waitForReadyRead(1000))
+    {
+    }
+  /*
+  if (!xt_process->waitForFinished(_max_xt_time_ms)) {
+      throw pappso::PappsoException(QObject::tr("can't wait for X!Tandem process
+  to finish : timeout at %1").arg(_max_xt_time_ms));
+  }
+  */
+  QByteArray result = xt_process->readAll();
+
+
+  qDebug() << result.constData();
+
+  QRegExp parse_version("X! TANDEM ([A-Z]+) \\((.*)\\)");
+  // Pattern patt = Pattern.compile("X! TANDEM [A-Z]+ \\((.*)\\)",
+  //			Pattern.CASE_INSENSITIVE);
+
+  if(parse_version.exactMatch(result.constData()))
+    {
+      version_return = parse_version.capturedTexts().join(" ");
+    }
+
+  QProcess::ExitStatus Status = xt_process->exitStatus();
+  delete xt_process;
+  if(Status != 0)
+    {
+      // != QProcess::NormalExit
+      throw pappso::PappsoException(
+        QObject::tr("error executing X!Tandem Status != 0 : %1 %2\n%3")
+          .arg(tandem_bin_path)
+          .arg(arguments.join(" ").arg(result.data())));
+    }
+  return version_return;
+  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
+}
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 13de1f616..52ecc77f5 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -36,6 +36,7 @@ class Utils
   static const QString getIdentificationEngineName(IdentificationEngine engine);
   static const QString getDatabaseName(ExternalDatabase database);
   static const QString getXmlDouble(pappso::pappso_double number);
+  static const QString checkXtandemVersion(const QString &tandem_bin_path);
   static pappso::AaModificationP
   guessAaModificationPbyMonoisotopicMassDelta(pappso::mz mass);
   static pappso::AaModificationP
-- 
GitLab