From b8091b99204cabedc9bbe345bb6aaa3feb94993c Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Fri, 22 Jun 2018 11:24:25 +0200
Subject: [PATCH] find MSrun on absolute file path if possible or on sample
 name only

---
 src/utils/msrunstore.cpp | 127 +++++++++++++++++++++++++--------------
 1 file changed, 81 insertions(+), 46 deletions(-)

diff --git a/src/utils/msrunstore.cpp b/src/utils/msrunstore.cpp
index 677efe12..f867fb0b 100644
--- a/src/utils/msrunstore.cpp
+++ b/src/utils/msrunstore.cpp
@@ -7,72 +7,107 @@
 
 
 /*******************************************************************************
-* 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 "msrunstore.h"
 #include <pappsomspp/exception/exceptionnotfound.h>
 #include <pappsomspp/utils.h>
+#include <QFileInfo>
 
 MsRunStore::MsRunStore()
 {
-
 }
 
 MsRunStore::~MsRunStore()
 {
-
 }
 
-MsRunSp MsRunStore::getInstance(const MsRun * p_msrun) {
-    auto it = std::find_if(_map_msrun.begin(), _map_msrun.end(), [p_msrun] (const MsRunSp & msrun_sp) {return msrun_sp.get()== p_msrun;});
-    if (it != _map_msrun.end()) {
-        return (*it);
+MsRunSp
+MsRunStore::getInstance(const MsRun *p_msrun)
+{
+  auto it = std::find_if(
+    _map_msrun.begin(), _map_msrun.end(),
+    [p_msrun](const MsRunSp &msrun_sp) { return msrun_sp.get() == p_msrun; });
+  if(it != _map_msrun.end())
+    {
+      return (*it);
     }
-    MsRunSp p_xtfile;
-    return p_xtfile;
+  MsRunSp p_xtfile;
+  return p_xtfile;
 }
 
-MsRunSp MsRunStore::getInstance(const QString & location) {
+MsRunSp
+MsRunStore::getInstance(const QString &location)
+{
 
-    auto it = std::find_if(_map_msrun.begin(), _map_msrun.end(), [location] (const MsRunSp & msrun_sp) {return msrun_sp.get()->getFilename()==location;});
-    if (it != _map_msrun.end()) {
-        return *it;
+  QFileInfo file_ondisk(location);
+  auto it = _map_msrun.end();
+  if(file_ondisk.baseName() == location)
+    {
+      // look only for the same sample name
+      it = std::find_if(_map_msrun.begin(), _map_msrun.end(),
+                        [location](const MsRunSp &msrun_sp) {
+                          return msrun_sp.get()->getSampleName() == location;
+                        });
     }
-    else {
-        MsRunSp p_xtfile = std::make_shared<MsRun>(location);
-        p_xtfile.get()->setXmlId(QString("msrun%1").arg(pappso::Utils::getLexicalOrderedString(_map_msrun.size()+1)));
-        p_xtfile.get()->setFilename(location);
-        _map_msrun.push_back( p_xtfile);
-        return p_xtfile;
-
+  else
+    {
+      // look for an absolute file path
+      it = std::find_if(_map_msrun.begin(), _map_msrun.end(),
+                        [location](const MsRunSp &msrun_sp) {
+                          if(msrun_sp.get()->getSampleName() ==
+                             msrun_sp.get()->getFilename())
+                            {
+                              return msrun_sp.get()->getSampleName() ==
+                                     QFileInfo(location).baseName();
+                            }
+                          return msrun_sp.get()->getFilename() == location;
+                        });
+    }
+  if(it != _map_msrun.end())
+    {
+      return *it;
     }
-    throw pappso::ExceptionNotFound(QObject::tr("Ms resource %1 not found").arg(location));
+  else
+    {
+      MsRunSp p_xtfile = std::make_shared<MsRun>(location);
+      p_xtfile.get()->setXmlId(QString("msrun%1").arg(
+        pappso::Utils::getLexicalOrderedString(_map_msrun.size() + 1)));
+      p_xtfile.get()->setFilename(location);
+      _map_msrun.push_back(p_xtfile);
+      return p_xtfile;
+    }
+  throw pappso::ExceptionNotFound(
+    QObject::tr("Ms resource %1 not found").arg(location));
 }
 
 
-const std::vector<MsRunSp> & MsRunStore::getMsRunList() const {
-    //std::vector<MsRunSp> msrun_list;
-   // for (auto & msrun_pair :_map_msrun) {
-   //     msrun_list.push_back(msrun_pair.second);
-   // }
-    return _map_msrun;
+const std::vector<MsRunSp> &
+MsRunStore::getMsRunList() const
+{
+  // std::vector<MsRunSp> msrun_list;
+  // for (auto & msrun_pair :_map_msrun) {
+  //     msrun_list.push_back(msrun_pair.second);
+  // }
+  return _map_msrun;
 }
-- 
GitLab