From 58f57e451c92c343ce777bb4779863be1885f613 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Mon, 20 Nov 2017 21:54:46 +0100
Subject: [PATCH] WIP: refactoring using PeptideEvidence

---
 src/core/peptideevidence.cpp    | 21 ---------------------
 src/core/peptideevidence.h      |  9 ---------
 src/core/peptidematch.cpp       |  6 ++++++
 src/core/peptidematch.h         |  3 +++
 src/core/proteinmatch.cpp       |  2 +-
 src/grouping/ptm/ptmisland.cpp  | 11 +++++++----
 src/grouping/ptm/ptmisland.h    |  2 +-
 src/input/xpipsaxhandler.cpp    |  9 +++++----
 src/input/xpipsaxhandler.h      |  1 +
 src/input/xtandemsaxhandler.cpp | 20 ++++++++++----------
 src/input/xtandemsaxhandler.h   |  3 ++-
 11 files changed, 36 insertions(+), 51 deletions(-)

diff --git a/src/core/peptideevidence.cpp b/src/core/peptideevidence.cpp
index c1669f2dc..bd9d6a306 100644
--- a/src/core/peptideevidence.cpp
+++ b/src/core/peptideevidence.cpp
@@ -47,7 +47,6 @@ PeptideEvidence::PeptideEvidence(const PeptideEvidence & other): PeptideEvidence
     _rt = other._rt;
     _evalue = other._evalue;
     _exp_mass = other._exp_mass;
-    _start=other._start;
     _charge= other._charge;
     _p_identification_source = other._p_identification_source;
     _params = other._params;
@@ -115,29 +114,9 @@ pappso::pappso_double PeptideEvidence::getExperimentalMz() const {
 void PeptideEvidence::setExperimentalMass(pappso::pappso_double exp_mass) {
     _exp_mass =exp_mass;
 }
-void PeptideEvidence::setStart(unsigned int start) {
-    _start =start;
-}
 pappso::mz PeptideEvidence::getDeltaMass() const {
     return (_peptide_sp.get()->getMz(1) - (_exp_mass+pappso::MHPLUS));
 }
-unsigned int PeptideEvidence::getStart() const {
-    return _start;
-}
-unsigned int PeptideEvidence::getStop() const {
-    return _start+_peptide_sp.get()->size();
-}
-
-bool PeptideEvidence::containsPosition(unsigned int position) const {
-    if (position < _start) {
-        return false;
-    }
-    if (position < getStop()) {
-        return true;
-    }
-    return false;
-
-}
 void PeptideEvidence::setCharge(unsigned int charge) {
     _charge =charge;
 }
diff --git a/src/core/peptideevidence.h b/src/core/peptideevidence.h
index 6ecbb1a2d..79f140d70 100644
--- a/src/core/peptideevidence.h
+++ b/src/core/peptideevidence.h
@@ -85,14 +85,6 @@ public :
     const std::map<PeptideMatchParam, QVariant> & getParamList() const;
 
 
-    /** @brief set start position of this peptide inside the protein sequence
-         * @param start position in the protein amino acid sequence (starts at 0)
-         * */
-    void setStart(unsigned int start);
-    /** @brief get start position of this peptide inside the protein sequence
-         * @return start position in the protein amino acid sequence (starts at 0)
-         * */
-    unsigned int getStart() const;
     /** @brief get stop position of this peptide inside the protein sequence
          * @return stop position in the protein amino acid sequence (starts at 0)
          * */
@@ -143,7 +135,6 @@ private :
     pappso::pappso_double _rt;
     pappso::pappso_double _evalue=0;
     pappso::pappso_double _exp_mass;
-    unsigned int _start=0;
     unsigned int _charge;
     IdentificationDataSource* _p_identification_source = nullptr;
     std::map<PeptideMatchParam, QVariant> _params;
diff --git a/src/core/peptidematch.cpp b/src/core/peptidematch.cpp
index c4a57c3a8..5498b269e 100644
--- a/src/core/peptidematch.cpp
+++ b/src/core/peptidematch.cpp
@@ -58,6 +58,12 @@ unsigned int PeptideMatch::getStop() const {
     return _start+_p_peptide_evidence->getPeptideXtpSp().get()->size();
 }
 
+bool PeptideMatch::operator<(const PeptideMatch& r) const
+{
+    return std::tie(_start, _p_peptide_evidence)
+           < std::tie(r._start, r._p_peptide_evidence); // keep the same order
+}
+
 bool PeptideMatch::containsPosition(unsigned int position) const {
     if (position < _start) {
         return false;
diff --git a/src/core/peptidematch.h b/src/core/peptidematch.h
index 4f18084ca..4dc8369a7 100644
--- a/src/core/peptidematch.h
+++ b/src/core/peptidematch.h
@@ -56,6 +56,9 @@ public :
     void setPeptideEvidenceSp(PeptideEvidenceSp sp_peptide_evidence);
     const PeptideEvidence * getPeptideEvidence() const;
     PeptideEvidence * getPeptideEvidence();
+    
+    
+    bool operator<(const PeptideMatch& r) const;
 
 private :
     unsigned int _start=0;
diff --git a/src/core/proteinmatch.cpp b/src/core/proteinmatch.cpp
index 0d2680d86..016645376 100644
--- a/src/core/proteinmatch.cpp
+++ b/src/core/proteinmatch.cpp
@@ -354,7 +354,7 @@ const QString ProteinMatch::getHtmlSequence(PeptideEvidence * peptide_evidence_t
     for (auto & peptide_match : _peptide_match_list) {
         if (peptide_match.getPeptideEvidence()->isValidAndChecked()) {
             size_t size = peptide_match.getPeptideEvidence()->getPeptideXtpSp().get()->size();
-            size_t offset = peptide_match.getPeptideEvidence()->getStart();
+            size_t offset = peptide_match.getStart();
             if (offset >= 0) {
                 for (size_t i=0; (i < size) && (offset < prot_size) ; i++,offset++) {
                     cover_bool[offset] = true;
diff --git a/src/grouping/ptm/ptmisland.cpp b/src/grouping/ptm/ptmisland.cpp
index 51d41937c..bae1d3bf0 100644
--- a/src/grouping/ptm/ptmisland.cpp
+++ b/src/grouping/ptm/ptmisland.cpp
@@ -105,9 +105,9 @@ std::vector<std::size_t> PtmIsland::getSampleScanSet() const {
     sample_scan_set.erase(std::unique(sample_scan_set.begin(), sample_scan_set.end()), sample_scan_set.end());
     return sample_scan_set;
 }
-bool PtmIsland::containsPeptideMatch(const PeptideMatch* peptide_match) const {
+bool PtmIsland::containsPeptideMatch(const PeptideMatch & peptide_match) const {
 
-    return std::any_of(_peptide_match_list.begin(), _peptide_match_list.end(), [peptide_match](const PeptideMatch* element) {
+    return std::any_of(_peptide_match_list.begin(), _peptide_match_list.end(), [peptide_match](const PeptideMatch & element) {
         return ( peptide_match == element);
     });
 }
@@ -138,7 +138,7 @@ unsigned int PtmIsland::getProteinStartPosition() const {
 }
 
 bool PtmIsland::merge(PtmIslandSp ptm_island_sp) {
-    if (std::none_of(_peptide_match_list.begin(), _peptide_match_list.end(), [ptm_island_sp](const PeptideMatch* element) {
+    if (std::none_of(_peptide_match_list.begin(), _peptide_match_list.end(), [ptm_island_sp](const PeptideMatch & element) {
     return ptm_island_sp.get()->containsPeptideMatch(element);
     })) {
         return false;
@@ -146,7 +146,10 @@ bool PtmIsland::merge(PtmIslandSp ptm_island_sp) {
     else {
         //so we merge
         _peptide_match_list.insert(_peptide_match_list.begin(),ptm_island_sp.get()->_peptide_match_list.begin(),ptm_island_sp.get()->_peptide_match_list.end());
-        std::sort(_peptide_match_list.begin(), _peptide_match_list.end());
+        std::sort(_peptide_match_list.begin(), _peptide_match_list.end(), [](const PeptideMatch & a, const PeptideMatch & b)
+        {
+            return  (a.operator<(b));
+        });
         auto last = std::unique(_peptide_match_list.begin(), _peptide_match_list.end());
         _peptide_match_list.erase(last, _peptide_match_list.end());
 
diff --git a/src/grouping/ptm/ptmisland.h b/src/grouping/ptm/ptmisland.h
index d00537ec8..b1fe701b5 100644
--- a/src/grouping/ptm/ptmisland.h
+++ b/src/grouping/ptm/ptmisland.h
@@ -56,7 +56,7 @@ public:
      * */
     bool merge(PtmIslandSp ptm_island_sp);
 
-    bool containsPeptideMatch(const PeptideMatch* element) const;
+    bool containsPeptideMatch(const PeptideMatch & element) const;
     std::vector<std::size_t> getSampleScanSet() const;
     const ProteinMatch * getProteinMatch() const;
     unsigned int getProteinStartPosition() const;
diff --git a/src/input/xpipsaxhandler.cpp b/src/input/xpipsaxhandler.cpp
index 94fcd0345..c1ce0f21a 100644
--- a/src/input/xpipsaxhandler.cpp
+++ b/src/input/xpipsaxhandler.cpp
@@ -261,7 +261,7 @@ bool XpipSaxHandler::startElement_peptide(QXmlAttributes attributes) {
     _p_peptide_evidence->setEvalue(attributes.value("evalue").simplified().toDouble());
     pappso::pappso_double exp_mass = attributes.value("mhplus_obser").simplified().toDouble() - pappso::MHPLUS;
     _p_peptide_evidence->setExperimentalMass(exp_mass);
-    _p_peptide_evidence->setStart(attributes.value("start").simplified().toUInt()-1);
+    _current_peptide_match.setStart(attributes.value("start").simplified().toUInt()-1);
     _p_peptide_evidence->setCharge(attributes.value("charge").simplified().toUInt());
     _p_peptide_evidence->setParam(PeptideMatchParam::tandem_hyperscore,  QVariant( attributes.value("hypercorr").toDouble()));
 
@@ -297,9 +297,10 @@ bool XpipSaxHandler::endElement_peptide() {
     qDebug() << "endElement_peptide ";
     PeptideXtpSp peptide_const = PeptideXtp(*(_current_peptide_sp.get())).makePeptideXtpSp();
     peptide_const = _p_project->getPeptideStore().getInstance(peptide_const);
-    _p_peptide_match->setPeptideXtpSp(peptide_const);
-    _p_protein_match->addPeptideMatchSp(_p_peptide_match->getIdentificationDataSource()->getPeptideMatchStore().getInstance(_p_peptide_match));
-    delete _p_peptide_match;
+    _p_peptide_evidence->setPeptideXtpSp(peptide_const);
+    _current_peptide_match.setPeptideEvidenceSp(_p_peptide_evidence->getIdentificationDataSource()->getPeptideEvidenceStore().getInstance(_p_peptide_evidence));
+    _p_protein_match->addPeptideMatch(_current_peptide_match);
+    delete _p_peptide_evidence;
     return true;
 }
 
diff --git a/src/input/xpipsaxhandler.h b/src/input/xpipsaxhandler.h
index 29b75cf87..e5187702f 100644
--- a/src/input/xpipsaxhandler.h
+++ b/src/input/xpipsaxhandler.h
@@ -85,6 +85,7 @@ private:
     Project * _p_project;
     ProteinMatch * _p_protein_match;
     PeptideEvidence * _p_peptide_evidence;
+    PeptideMatch _current_peptide_match;
     ProteinXtp _current_protein;
     PeptideXtpSp _current_peptide_sp;
     IdentificationGroup * _current_identification_group_p;
diff --git a/src/input/xtandemsaxhandler.cpp b/src/input/xtandemsaxhandler.cpp
index d1cdd2fcd..f24405bec 100644
--- a/src/input/xtandemsaxhandler.cpp
+++ b/src/input/xtandemsaxhandler.cpp
@@ -284,10 +284,10 @@ bool XtandemSaxHandler::startElement_domain(QXmlAttributes attributes) {
 
     // valeur généric du scan
     _current_peptide_sp = PeptideXtp(attributes.value("seq").simplified()).makePeptideXtpSp();
-    _p_peptide_match = new PeptideMatch(_sp_msrun.get(), attributes.value("id").simplified().section(".",0,0).toUInt());
+    _p_peptide_evidence = new PeptideEvidence(_sp_msrun.get(), attributes.value("id").simplified().section(".",0,0).toUInt());
 
-    _p_peptide_match->setRetentionTime(_retention_time);
-    _p_peptide_match->setEvalue(attributes.value("expect").simplified().toDouble());
+    _p_peptide_evidence->setRetentionTime(_retention_time);
+    _p_peptide_evidence->setEvalue(attributes.value("expect").simplified().toDouble());
     //qDebug() << "XtandemSaxHandler::startElement_domain evalue "  << _p_peptide_match->getEvalue() << " scan " << _p_peptide_match->getScan();
 
     pappso::pappso_double xtandem_mhtheo = attributes.value("mh").simplified().toDouble();
@@ -295,14 +295,14 @@ bool XtandemSaxHandler::startElement_domain(QXmlAttributes attributes) {
 
     pappso::pappso_double exp_mass = xtandem_mhtheo + xtandem_delta - pappso::MHPLUS;
 
-    _p_peptide_match->setExperimentalMass(exp_mass);
-    _p_peptide_match->setStart(attributes.value("start").simplified().toUInt()-1);
-    _p_peptide_match->setCharge(_charge);
+    _p_peptide_evidence->setExperimentalMass(exp_mass);
+    _current_peptide_match.setStart(attributes.value("start").simplified().toUInt()-1);
+    _p_peptide_evidence->setCharge(_charge);
 
-    _p_peptide_match->setParam(PeptideMatchParam::tandem_hyperscore,  QVariant( attributes.value("hyperscore").toDouble()));
+    _p_peptide_evidence->setParam(PeptideMatchParam::tandem_hyperscore,  QVariant( attributes.value("hyperscore").toDouble()));
 
-    _p_peptide_match->setIdentificationDataSource( _p_identification_data_source);
-    _p_peptide_match->setChecked(true);
+    _p_peptide_evidence->setIdentificationDataSource( _p_identification_data_source);
+    _p_peptide_evidence->setChecked(true);
 
 
 
@@ -320,7 +320,7 @@ bool XtandemSaxHandler::startElement_aa(QXmlAttributes attributes) {
     //qDebug() << "startElement_aa ";
     pappso::AaModificationP modif = Utils::guessAaModificationPbyMonoisotopicMassDelta(attributes.value("modified").simplified().toDouble());
     unsigned int position_in_prot = attributes.value("at").simplified().toUInt()-1;
-    _current_peptide_sp.get()->addAaModification(modif, position_in_prot-_p_peptide_match->getStart());
+    _current_peptide_sp.get()->addAaModification(modif, position_in_prot-_current_peptide_match.getStart());
     //qDebug() << "startElement_aa end" ;
     return is_ok;
 }
diff --git a/src/input/xtandemsaxhandler.h b/src/input/xtandemsaxhandler.h
index 0e0a27ce6..a344676a7 100644
--- a/src/input/xtandemsaxhandler.h
+++ b/src/input/xtandemsaxhandler.h
@@ -86,7 +86,8 @@ private:
     MsRunSp _sp_msrun;
 
     ProteinMatch * _p_protein_match;
-    PeptideMatch * _p_peptide_match;
+    PeptideEvidence * _p_peptide_evidence;
+    PeptideMatch _current_peptide_match;
     ProteinXtp _current_protein;
     PeptideXtpSp _current_peptide_sp;
 
-- 
GitLab