From bcf22670c708fc21197ec6c8f5656824cabec339 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Thu, 23 Nov 2017 16:55:37 +0100
Subject: [PATCH] WIP : writing and reading new xpip file format

---
 src/gui/mainwindow.cpp                      |  6 +-
 src/gui/mainwindow.h                        |  1 +
 src/gui/workerthread.cpp                    | 22 ++++++-
 src/gui/workerthread.h                      |  1 +
 src/input/xtpxpipsaxhandler.cpp             | 68 ++++++++++++++++-----
 src/input/xtpxpipsaxhandler.h               |  5 +-
 src/utils/identificationdatasourcestore.cpp |  5 +-
 src/utils/peptideevidencestore.cpp          |  6 ++
 src/utils/peptideevidencestore.h            |  5 ++
 9 files changed, 96 insertions(+), 23 deletions(-)

diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp
index 169b28a36..5b22c3743 100644
--- a/src/gui/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -328,10 +328,8 @@ void MainWindow::doActionSaveProject() {
         }
 
         settings.setValue("path/xpipfile", QFileInfo(filename).absolutePath());
-
-        Xpip xpip_file(filename);
-        xpip_file.write(_project_sp);
-        xpip_file.close();
+        showWaitingMessage(tr("Writing %1 XPIP file").arg(QFileInfo(filename).fileName()));
+        emit operateWritingXpipFile(filename, _project_sp);
 
     }
     catch (pappso::PappsoException & error) {
diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h
index 402122239..2ee0090c1 100644
--- a/src/gui/mainwindow.h
+++ b/src/gui/mainwindow.h
@@ -96,6 +96,7 @@ signals:
     //void peptideChanged(pappso::PeptideSp peptide);
     void operateXpipFile(QString xpip_file);
     void operateLoadingResults(bool is_individual, AutomaticFilterParameters param, QStringList file_list);
+    void operateWritingXpipFile(QString filename, ProjectSp project_sp);
     void operateWritingOdsFile(QString filename, ProjectSp project_sp);
     void operateWritingMassChroqFile(QString filename, ProjectSp project_sp);
     void operateWritingProticFile(QString filename, ProjectSp project_sp);
diff --git a/src/gui/workerthread.cpp b/src/gui/workerthread.cpp
index a840abef1..47ea9ae0d 100644
--- a/src/gui/workerthread.cpp
+++ b/src/gui/workerthread.cpp
@@ -34,6 +34,7 @@
 #include "../output/masschroqml.h"
 #include "../output/proticdbml.h"
 #include "../output/ods/odsexport.h"
+#include "../output/xpip.h"
 #include "files/xpipfile.h"
 #include "mainwindow.h"
 #include "project_view/projectwindow.h"
@@ -58,6 +59,7 @@ WorkerThread::WorkerThread(MainWindow * p_main_window)
     connect(_p_work_monitor, &WorkMonitor::workerJobFinished, p_main_window,&MainWindow::doDisplayJobFinished);
 
     connect(p_main_window, &MainWindow::operateXpipFile, this,&WorkerThread::doXpipFileLoad);
+    connect(p_main_window, &MainWindow::operateWritingXpipFile, this,&WorkerThread::doWritingXpipFile);
     connect(this, &WorkerThread::projectReady, p_main_window,&MainWindow::doProjectReady);
 
     connect(p_main_window, &MainWindow::operateLoadingResults, this,&WorkerThread::doLoadingResults);
@@ -258,6 +260,22 @@ void WorkerThread::doGrouping(ProjectSp project_sp) {
     }
     qDebug() << "WorkerThread::doGrouping end ";
 }
+
+void WorkerThread::doWritingXpipFile(QString filename, ProjectSp project_sp) {
+
+    try {
+        emit loadingMessage(tr("writing XPIP file, please wait"));
+        
+        Xpip xpip_file(filename);
+        xpip_file.write(project_sp);
+        xpip_file.close();
+
+        emit operationFinished();
+    }
+    catch (pappso::PappsoException & error) {
+        emit operationFailed(tr("Error while writing XPIP file :\n%1").arg(error.qwhat()));
+    }
+}
 void WorkerThread::doWritingOdsFile(QString filename, ProjectSp project_sp) {
 
     try {
@@ -342,13 +360,13 @@ void WorkerThread::doRunningXtandem(TandemRunBatch tandem_run_batch) {
         bool use_htcondor = settings.value("tandem/use_HTCondor", "false").toBool();
 
         if (use_htcondor) {
-           TandemCondorProcess xt_process (_p_main_window ,_p_work_monitor, tandem_run_batch);
+            TandemCondorProcess xt_process (_p_main_window ,_p_work_monitor, tandem_run_batch);
             xt_process.run();
         }
         else {
             TandemBatchProcess xt_process (_p_main_window ,_p_work_monitor, tandem_run_batch);
             xt_process.run();
-         }
+        }
 
         emit operationFinished();
     }
diff --git a/src/gui/workerthread.h b/src/gui/workerthread.h
index d357d710e..dd120d7a1 100644
--- a/src/gui/workerthread.h
+++ b/src/gui/workerthread.h
@@ -58,6 +58,7 @@ protected:
 public slots:
     void doXpipFileLoad(QString filename);
     void doLoadingResults(bool is_individual, AutomaticFilterParameters param, QStringList file_list);
+    void doWritingXpipFile(QString filename, ProjectSp project_sp);
     void doWritingOdsFile(QString filename, ProjectSp project_sp);
     void doWritingMassChroqFile(QString filename, ProjectSp project_sp);
     void doWritingProticFile(QString filename, ProjectSp project_sp);
diff --git a/src/input/xtpxpipsaxhandler.cpp b/src/input/xtpxpipsaxhandler.cpp
index abf30e56d..8788d8ce0 100644
--- a/src/input/xtpxpipsaxhandler.cpp
+++ b/src/input/xtpxpipsaxhandler.cpp
@@ -76,9 +76,11 @@ bool XtpXpipSaxHandler::startElement(const QString & namespaceURI, const QString
             is_ok = startElement_identification_source(attributes);
         } else if (qName == "param") {
             is_ok = startElement_param(attributes);
+        } else if (qName == "identification_group") {
+            is_ok = startElement_identification_group(attributes);
         }
         //<sample value="P6_08_10"/>
-        else if (qName == "sample") {
+        else if (qName == "msrun") {
             is_ok = startElement_msrun(attributes);
         } else if (qName == "peptide") {
             is_ok = startElement_peptide(attributes);
@@ -129,6 +131,13 @@ bool XtpXpipSaxHandler::endElement(const QString & namespaceURI, const QString &
         else if (qName == "protein_match") {
             is_ok = endElement_protein_match();
         }
+        else if (qName == "peptide_evidence") {
+            is_ok = endElement_peptide_evidence();
+        }
+        else if (qName == "identification_group") {
+            is_ok = endElement_identification_group();
+        }
+
 
         // end of detection_moulon
         // else if ((_tag_stack.size() > 1) &&
@@ -182,7 +191,7 @@ bool XtpXpipSaxHandler::startElement_description(QXmlAttributes attributes) {
     return true;
 }
 
-        
+
 bool XtpXpipSaxHandler::startElement_param(QXmlAttributes attributes) {
 
     /*
@@ -213,6 +222,15 @@ bool XtpXpipSaxHandler::startElement_modification(QXmlAttributes attributes) {
     return true;
 }
 
+bool XtpXpipSaxHandler::startElement_identification_group(QXmlAttributes attributes) {
+
+    qDebug() << "startElement_identification_group ";
+    _current_identification_group_p =  _p_project->newIdentificationGroup();
+
+    qDebug() << "startElement_identification_group end" ;
+    return true;
+}
+
 bool XtpXpipSaxHandler::startElement_identification_source(QXmlAttributes attributes) {
     //<identification_source id="identa0" msrun_id="sampa0" path="/gorgone/pappso/formation/Janvier2014/TD/xml_tandem/20120906_balliau_extract_1_A01_urnb-1.xml" engine="1" version=""/>
 
@@ -222,8 +240,10 @@ bool XtpXpipSaxHandler::startElement_identification_source(QXmlAttributes attrib
     IdentificationDataSourceSp sp_ident_source =  _p_project->getIdentificationDataSourceStore().getInstance(attributes.value("path").simplified(), engine);
     sp_ident_source.get()->setXmlId(attributes.value("id").simplified());
     _map_ident_sources.insert(std::pair<QString, IdentificationDataSourceSp>(sp_ident_source.get()->getXmlId(), sp_ident_source));
-    
+
+    qDebug() << "startElement_identification_source setMsRunSp";
     sp_ident_source->setMsRunSp(_map_msruns.at(attributes.value("msrun_id").simplified()));
+    qDebug() << "startElement_identification_source setIdentificationEngineVersion";
     sp_ident_source->setIdentificationEngineVersion(attributes.value("version"));
     //_current_identification_group_p->addMsRunSp(ms_run);
     qDebug() << "startElement_identification_source end" ;
@@ -257,7 +277,7 @@ bool XtpXpipSaxHandler::startElement_peptide_evidence(QXmlAttributes attributes)
     unsigned int scan = attributes.value("scan").toUInt();
     _p_peptide_evidence = new PeptideEvidence(sp_ident_source.get()->getMsRunSp().get(), scan);
     _p_peptide_evidence->setIdentificationDataSource(sp_ident_source.get());
-    
+
     _p_peptide_evidence->setChecked(false);
     if (attributes.value("checked").simplified().toLower() == "true") {
         _p_peptide_evidence->setChecked(true);
@@ -267,8 +287,8 @@ bool XtpXpipSaxHandler::startElement_peptide_evidence(QXmlAttributes attributes)
     _p_peptide_evidence->setEvalue(attributes.value("evalue").toDouble());
     _p_peptide_evidence->setExperimentalMass(attributes.value("exp_mass").toDouble());
     _p_peptide_evidence->setPeptideXtpSp(_map_peptides.at( attributes.value("peptide_id").simplified()));
-    
-    _sp_current_peptide_evidence = sp_ident_source.get()->getPeptideEvidenceStore().getInstance(_p_peptide_evidence);
+
+    _sp_current_peptide_evidence = sp_ident_source.get()->getPeptideEvidenceStore().recordInstance(_p_peptide_evidence);
     _map_peptide_evidences.insert(std::pair<QString, PeptideEvidenceSp>(attributes.value("id").simplified(), _sp_current_peptide_evidence));
     qDebug() << "startElement_peptide_evidence end" ;
     return true;
@@ -278,13 +298,14 @@ bool XtpXpipSaxHandler::startElement_peptide_match(QXmlAttributes attributes) {
 
     qDebug() << "startElement_peptide_match ";
     /*
-     * <protein_match acc="GRMZM2G138258_P01" checked="true">
-                <peptide_match source_id="identa0" peptide_id="pd3533" scan="1907" rt="646.406" evalue="0.00044" exp_mass="2190.816924" start="103" charge="3" checked="true">
-                    <param key="0" value="23.6"/>
-                </peptide_match>
+     *<peptide_match peptide_evidence_id="pea1" start="619"/>
               */
-    _p_protein_match = new ProteinMatch();
+    PeptideMatch peptide_match;
+    peptide_match.setPeptideEvidenceSp(_map_peptide_evidences.at(attributes.value("peptide_evidence_id").simplified()));
+    peptide_match.setStart(attributes.value("start").toUInt());
     qDebug() << "startElement_peptide_match end" ;
+    _p_protein_match->addPeptideMatch(peptide_match);
+    qDebug() << "startElement_peptide_match " << _p_protein_match->getPeptideMatchList().size();
     return true;
 }
 
@@ -292,12 +313,15 @@ bool XtpXpipSaxHandler::startElement_protein_match(QXmlAttributes attributes) {
 
     qDebug() << "startElement_protein_match ";
     /*
-     * <protein_match acc="GRMZM2G138258_P01" checked="true">
-                <peptide_match source_id="identa0" peptide_id="pd3533" scan="1907" rt="646.406" evalue="0.00044" exp_mass="2190.816924" start="103" charge="3" checked="true">
-                    <param key="0" value="23.6"/>
-                </peptide_match>
+     * <protein_match acc="GRMZM2G083841_P01" checked="true">
+                    <peptide_match peptide_evidence_id="pea1" start="619"/>
+                    <peptide_match peptide_evidence_id="pea2" start="12"/>
+
               */
     _p_protein_match = new ProteinMatch();
+
+
+    _p_protein_match->setProteinXtpSp(_map_proteins.at(attributes.value("acc").simplified()));
     _p_protein_match->setChecked(false);
     if (attributes.value("checked").simplified().toLower() == "true") {
         _p_protein_match->setChecked(true);
@@ -338,6 +362,7 @@ bool XtpXpipSaxHandler::startElement_protein(QXmlAttributes attributes) {
     if (attributes.value("is_decoy").simplified().toLower() == "true") {
         _current_protein.setIsDecoy(true);
     }
+
     qDebug() << "startElement_protein end" ;
     return true;
 }
@@ -364,12 +389,25 @@ bool XtpXpipSaxHandler::startElement_mod(QXmlAttributes attributes) {
     qDebug() << "startElement_mod end" ;
     return true;
 }
+bool XtpXpipSaxHandler::endElement_identification_group() {
+    qDebug() << "endElement_identification_group ";
+    _current_identification_group_p = nullptr;
+    return true;
+}
+
 bool XtpXpipSaxHandler::endElement_peptide() {
     qDebug() << "endElement_peptide ";
 
     return true;
 }
 
+bool XtpXpipSaxHandler::endElement_peptide_evidence() {
+    qDebug() << "endElement_peptide_evidence begin ";
+    _sp_current_peptide_evidence = nullptr;
+    qDebug() << "endElement_peptide_evidence end ";
+    return true;
+}
+
 bool XtpXpipSaxHandler::endElement_sequence() {
     qDebug() << "endElement_sequence begin ";
     //if ((_tag_stack.size() > 1) && (_tag_stack[_tag_stack.size() - 1] == "protein")) {
diff --git a/src/input/xtpxpipsaxhandler.h b/src/input/xtpxpipsaxhandler.h
index 830feef13..57c519ffb 100644
--- a/src/input/xtpxpipsaxhandler.h
+++ b/src/input/xtpxpipsaxhandler.h
@@ -76,6 +76,8 @@ private:
     bool startElement_protein(QXmlAttributes attributes);
     bool startElement_msrun(QXmlAttributes attributes);
     bool startElement_identification_source(QXmlAttributes attributes);
+    bool startElement_identification_group(QXmlAttributes attributes);
+    
     bool startElement_modification(QXmlAttributes attributes);
     bool startElement_mod(QXmlAttributes attributes);
     bool startElement_param(QXmlAttributes attributes);
@@ -84,6 +86,8 @@ private:
     bool endElement_protein();
     bool endElement_peptide();
     bool endElement_protein_match();
+    bool endElement_peptide_evidence();
+    bool endElement_identification_group();
 
 private:
     WorkMonitorInterface * _p_monitor;
@@ -94,7 +98,6 @@ private:
 
     Project * _p_project;
     ProteinMatch * _p_protein_match;
-    PeptideMatch _current_peptide_match;
     PeptideEvidence * _p_peptide_evidence;
     IdentificationGroup * _current_identification_group_p;
     
diff --git a/src/utils/identificationdatasourcestore.cpp b/src/utils/identificationdatasourcestore.cpp
index 46059e90c..c0e100dd0 100644
--- a/src/utils/identificationdatasourcestore.cpp
+++ b/src/utils/identificationdatasourcestore.cpp
@@ -46,14 +46,15 @@ IdentificationDataSourceStore::~IdentificationDataSourceStore()
 
 IdentificationDataSourceSp IdentificationDataSourceStore::getInstance(const QString & location, IdentificationEngine engine) {
     qDebug() << "IdentificationDataSourceStore::getInstance begin " << location;
-    qDebug() << " " << _map_identification_data_sources.size();
     std::map< QString, IdentificationDataSourceSp >::iterator it = _map_identification_data_sources.find(location);
     if (it != _map_identification_data_sources.end()) {
+        qDebug() << "IdentificationDataSourceStore::getInstance found " << location;
         return it->second;
     }
     else {
         QFileInfo location_file(location);
         QString ext = location_file.suffix();
+        qDebug() << "IdentificationDataSourceStore::getInstance coucou 1 ";
         //QString sample_name = location_file.baseName();
         IdentificationDataSourceSp p_identfile = nullptr;
         if (ext.toLower() == "xml") {
@@ -68,10 +69,12 @@ IdentificationDataSourceSp IdentificationDataSourceStore::getInstance(const QStr
         if (p_identfile == nullptr) {
             throw pappso::PappsoException(QObject::tr("Identification resource %1 not recognized (null pointer)").arg(location));
         }
+         qDebug() << "IdentificationDataSourceStore::getInstance coucou 2 ";
         p_identfile.get()->setXmlId(QString("ident%1").arg(pappso::Utils::getLexicalOrderedString(_map_identification_data_sources.size())));
         p_identfile.get()->setIdentificationEngine(engine);
         _map_identification_data_sources.insert(std::pair< QString, IdentificationDataSourceSp >(location, p_identfile));
         _map_identification_data_sources.insert(std::pair< QString, IdentificationDataSourceSp >(location_file.absoluteFilePath(), p_identfile));
+        qDebug() << "IdentificationDataSourceStore::getInstance inserted " << location;
         return p_identfile;
     }
     throw pappso::PappsoException(QObject::tr("Identification resource %1 not recognized").arg(location));
diff --git a/src/utils/peptideevidencestore.cpp b/src/utils/peptideevidencestore.cpp
index e43b819d1..92713a489 100644
--- a/src/utils/peptideevidencestore.cpp
+++ b/src/utils/peptideevidencestore.cpp
@@ -33,6 +33,12 @@
 #include <QDebug>
 
 #include "../core/peptideevidence.h"
+std::shared_ptr<PeptideEvidence> & PeptideEvidenceStore::recordInstance(const PeptideEvidence * p_peptide_evidence) {
+    _peptide_evidence_list.push_back(p_peptide_evidence->makePeptideEvidenceSp());
+
+    //qDebug() << "PeptideEvidenceStore::getInstance end " << find_it->second.get()->getScan() << " size=" << _multimap_scan_evidence.size();
+    return _peptide_evidence_list.back();
+}
 
 std::shared_ptr<PeptideEvidence> & PeptideEvidenceStore::getInstance(const PeptideEvidence * p_peptide_evidence) {
 
diff --git a/src/utils/peptideevidencestore.h b/src/utils/peptideevidencestore.h
index e51fda1b4..5272945e2 100644
--- a/src/utils/peptideevidencestore.h
+++ b/src/utils/peptideevidencestore.h
@@ -41,6 +41,11 @@ class PeptideEvidence;
 class PeptideEvidenceStore
 {
 public:
+    /** @brief directly register an instance of peptide evidence : no duplication check
+     */
+    std::shared_ptr<PeptideEvidence> & recordInstance(const PeptideEvidence * p_peptide_evidence);
+    /** @brief register an instance of peptide evidence : duplication check
+     */
     std::shared_ptr<PeptideEvidence> & getInstance(const PeptideEvidence * p_peptide_evidence);
     void updateAutomaticFilters(const AutomaticFilterParameters & automatic_filter_parameters);
     
-- 
GitLab