From 9393c31cd50e261be4b8e7084d51b9f78b76a27c Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Thu, 16 Nov 2017 13:15:20 +0100
Subject: [PATCH] xpip export system is going on

---
 src/core/identificationgroup.cpp |   3 +
 src/core/identificationgroup.h   |   1 +
 src/core/labeling/label.cpp      |  10 +-
 src/core/labeling/label.h        |   3 +
 src/core/peptidextp.h            |   2 +-
 src/output/xpip.cpp              | 155 ++++++++++++++++++++++++++++++-
 src/output/xpip.h                |  10 ++
 src/utils/peptidestore.cpp       |   4 +
 src/utils/peptidestore.h         |   2 +
 src/utils/proteinstore.cpp       |   5 +
 src/utils/proteinstore.h         |   2 +
 11 files changed, 191 insertions(+), 6 deletions(-)

diff --git a/src/core/identificationgroup.cpp b/src/core/identificationgroup.cpp
index 2403b432b..4b3c3e3b7 100644
--- a/src/core/identificationgroup.cpp
+++ b/src/core/identificationgroup.cpp
@@ -184,6 +184,9 @@ const std::vector<MsRunSp> & IdentificationGroup::getMsRunSpList() const {
 const std::vector<IdentificationDataSource *> & IdentificationGroup::getIdentificationDataSourceList() const {
     return _id_source_list;
 }
+const std::vector<ProteinMatch *> & IdentificationGroup::getProteinMatchList() const {
+    return _protein_match_list;
+}
 std::vector<ProteinMatch *> & IdentificationGroup::getProteinMatchList() {
     return _protein_match_list;
 }
diff --git a/src/core/identificationgroup.h b/src/core/identificationgroup.h
index 01dbafba4..243f77811 100644
--- a/src/core/identificationgroup.h
+++ b/src/core/identificationgroup.h
@@ -50,6 +50,7 @@ public:
     const GroupStore & getGroupStore() const;
     void addProteinMatch(ProteinMatch * protein_match);
     std::vector<ProteinMatch *> & getProteinMatchList();
+    const std::vector<ProteinMatch *> & getProteinMatchList() const;
     void addIdentificationDataSourceP(IdentificationDataSource *  p_identification_source);
 
     const PtmGroupingExperiment * getPtmGroupingExperiment() const;
diff --git a/src/core/labeling/label.cpp b/src/core/labeling/label.cpp
index e3093ea61..c0a50b11e 100644
--- a/src/core/labeling/label.cpp
+++ b/src/core/labeling/label.cpp
@@ -85,6 +85,10 @@ const QString & Label::getXmlId() const {
     return _xml_id;
 }
 
+const std::vector<LabelModification> & Label::getLabelModifictionList() const {
+    return _modification_list;
+}
+
 void Label::writeMassChroqMl(QXmlStreamWriter* output_stream) const {
 
 
@@ -95,11 +99,11 @@ void Label::writeMassChroqMl(QXmlStreamWriter* output_stream) const {
     //		<mod at="K" value="28.0" acc="MOD:00429"/>
     for (const LabelModification label_modification: _modification_list) {
         output_stream->writeStartElement("mod");
-	output_stream->writeAttribute("at",label_modification.at);
+        output_stream->writeAttribute("at",label_modification.at);
         output_stream->writeAttribute("value",Utils::getXmlDouble(label_modification.modification->getMass()));
         output_stream->writeAttribute("acc",label_modification.modification->getAccession());
-	output_stream->writeEndElement();
-	output_stream->writeComment(label_modification.modification->getName());
+        output_stream->writeEndElement();
+        output_stream->writeComment(label_modification.modification->getName());
     }
     //	</isotope_label>
     output_stream->writeEndElement();
diff --git a/src/core/labeling/label.h b/src/core/labeling/label.h
index ba941181b..00140c1d8 100644
--- a/src/core/labeling/label.h
+++ b/src/core/labeling/label.h
@@ -52,6 +52,9 @@ public:
     pappso::PeptideSp getLabeledPeptideSp(const pappso::Peptide * p_peptide) const;
     void writeMassChroqMl(QXmlStreamWriter* output_stream) const;
     const QString & getXmlId() const;
+    
+    const std::vector<LabelModification> & getLabelModifictionList() const;
+    
 private:
     QString _xml_id;
     std::vector<LabelModification> _modification_list;
diff --git a/src/core/peptidextp.h b/src/core/peptidextp.h
index 4701e551f..8353eb854 100644
--- a/src/core/peptidextp.h
+++ b/src/core/peptidextp.h
@@ -42,7 +42,7 @@ public:
 
     PeptideXtpSp makePeptideXtpSp() const;
 
-    /** \brief get the tehoretical mass to use for grouping
+    /** \brief get the theoretical mass to use for grouping
      * This mass might be different than the true peptide mass to recognize that a tagged
      * peptide with taget modification is a light, inter or heavy version of the same peptide
     */
diff --git a/src/output/xpip.cpp b/src/output/xpip.cpp
index cc2f478ee..20f9077c2 100644
--- a/src/output/xpip.cpp
+++ b/src/output/xpip.cpp
@@ -31,6 +31,8 @@
 #include "../config.h"
 #include <QDateTime>
 #include <pappsomspp/pappsoexception.h>
+#include <pappsomspp/utils.h>
+#include <QDebug>
 
 Xpip::Xpip(const QString & out_filename)
 {
@@ -117,8 +119,10 @@ void Xpip::write(ProjectSp sp_project) {
     writeMsrunList(_sp_project.get()->getMsRunStore());
     writeIdentificationDataSourceList(_sp_project.get()->getIdentificationDataSourceStore());
 
+    writeProteinList();
+    writePeptideList();
 
-
+    writeIdentificationGroupList();
     _output_stream->writeEndDocument();
 }
 void Xpip::writeDoubleAttribute(const QString & attribute, pappso::pappso_double value) {
@@ -135,13 +139,14 @@ void Xpip::writeBooleanAttribute(const QString & attribute, bool value) {
 
 }
 void Xpip::writeFilterParameters(const AutomaticFilterParameters & filters) {
-
+    qDebug() << "Xpip::writeFilterParameters begin";
     _output_stream->writeStartElement("filter_params");
     writeDoubleAttribute("pep_evalue",filters.getFilterPeptideEvalue());
     writeDoubleAttribute("prot_evalue",filters.getFilterProteinEvalue());
     _output_stream->writeAttribute("pep_number",QString("%1").arg(filters.getFilterMinimumPeptidePerMatch()));
     writeBooleanAttribute("cross_sample",filters.getFilterCrossSamplePeptideNumber());
     _output_stream->writeEndElement();
+    qDebug() << "Xpip::writeFilterParameters end";
 }
 
 void Xpip::writeDescription() {
@@ -206,6 +211,7 @@ void Xpip::writeMsrunList(const MsRunStore & msrun_store) {
 }
 
 void Xpip::writeFastaFileList(const FastaFileStore & fasta_store) {
+    qDebug() << "Xpip::writeFastaFileList begin";
     _output_stream->writeStartElement("fasta_file_list");
     for (FastaFileSp fasta_file_sp : fasta_store.getFastaFileList()) {
         _output_stream->writeStartElement("fasta_file");
@@ -215,4 +221,149 @@ void Xpip::writeFastaFileList(const FastaFileStore & fasta_store) {
         _output_stream->writeEndElement();
     }
     _output_stream->writeEndElement();
+    qDebug() << "Xpip::writeFastaFileList end";
+}
+
+void Xpip::writeProteinList() {
+    qDebug() << "Xpip::writeProteinList begin";
+    _output_stream->writeStartElement("protein_list");
+    const ProteinStore & protein_store = _sp_project.get()->getProteinStore();
+    for (std::pair<QString, ProteinXtpSp> protein_pair : protein_store.getProteinMap()) {
+        const ProteinXtp * p_protein = protein_pair.second.get();
+        _output_stream->writeStartElement("protein");
+        _output_stream->writeAttribute("acc",p_protein->getAccession());
+        _output_stream->writeAttribute("description",p_protein->getDescription());
+        _output_stream->writeAttribute("is_decoy","false");
+        if (p_protein->isDecoy()) {
+            _output_stream->writeAttribute("is_decoy","true");
+        }
+        _output_stream->writeAttribute("is_contaminant","false");
+        if (p_protein->isContaminant()) {
+            _output_stream->writeAttribute("is_contaminant","true");
+        }
+
+        _output_stream->writeStartElement("sequence");
+        _output_stream->writeCharacters(p_protein->getSequence());
+        _output_stream->writeEndElement();
+
+        for (DbXref db_xref : p_protein->getDbxrefList()) {
+            _output_stream->writeStartElement("dbxref");
+            _output_stream->writeAttribute("acc",db_xref.accession);
+            _output_stream->writeAttribute("database",QString("%1").arg(static_cast<std::int8_t>(db_xref.database)));
+            _output_stream->writeEndElement();
+        }
+        _output_stream->writeEndElement();
+    }
+    _output_stream->writeEndElement();
+    qDebug() << "Xpip::writeProteinList end";
+}
+
+QString Xpip::getPeptideId(std::size_t crc_peptide) const {
+    return QString("p%1").arg(pappso::Utils::getLexicalOrderedString(crc_peptide));
+}
+
+void Xpip::writeLabelingMethod() {
+    qDebug() << "Xpip::writeLabelingMethod begin";
+
+    LabelingMethod * p_labeling_method = _sp_project.get()->getLabelingMethodSp().get();
+    if (p_labeling_method == nullptr) return;
+    _output_stream->writeStartElement("label_method");
+    _output_stream->writeAttribute("id",p_labeling_method->getXmlId());
+
+    _output_stream->writeStartElement("label_list");
+    for (const Label * p_label : p_labeling_method->getLabelList()) {
+        _output_stream->writeStartElement("label");
+        _output_stream->writeAttribute("id", p_label->getXmlId());
+        for (const LabelModification label_modification : p_label->getLabelModifictionList()) {
+            _output_stream->writeStartElement("label_modification");
+            _output_stream->writeAttribute("at", label_modification.at);
+            _output_stream->writeAttribute("mod", label_modification.modification->getAccession());
+            _output_stream->writeEndElement();// label_modification
+        }
+        _output_stream->writeEndElement();// label
+    }
+    _output_stream->writeEndElement();// label_list
+
+    _output_stream->writeEndElement();// label_method
+    qDebug() << "Xpip::writeLabelingMethod end";
+}
+
+void Xpip::writePeptideList() {
+    qDebug() << "Xpip::writePeptideList begin";
+    _output_stream->writeStartElement("peptide_list");
+
+    writeLabelingMethod();
+
+    const PeptideStore & peptide_store = _sp_project.get()->getPeptideStore();
+    _output_stream->writeStartElement("modification_list");
+    for (pappso::AaModificationP mod_p : peptide_store.getModificationCollection()) {
+        QString id = QString("mod%1").arg(pappso::Utils::getLexicalOrderedString(_map_modifications.size()+1));
+        _map_modifications.insert(std::pair<pappso::AaModificationP, QString>(mod_p, id));
+        _output_stream->writeStartElement("modification");
+        _output_stream->writeAttribute("id", id);
+        _output_stream->writeAttribute("mod", mod_p->getAccession());
+        _output_stream->writeEndElement();// modification
+    }
+    _output_stream->writeEndElement();// modification_list
+
+    for (std::pair<std::size_t, PeptideXtpSp> peptide_pair : peptide_store.getPeptideMap()) {
+        const PeptideXtp * p_peptide = peptide_pair.second.get();
+        _output_stream->writeStartElement("peptide");
+        QString idp = QString("p%1").arg(pappso::Utils::getLexicalOrderedString(_map_peptides.size()+1));
+        _map_peptides.insert(std::pair<const PeptideXtp *, QString>(p_peptide, idp));
+        _output_stream->writeAttribute("id",idp);
+        _output_stream->writeAttribute("seq",p_peptide->getSequence());
+
+        const Label * p_label = p_peptide->getLabel();
+        if (p_label != nullptr) {
+            _output_stream->writeAttribute("label_id",p_label->getXmlId());
+        }
+
+
+        unsigned int i=1;
+        for (const pappso::Aa & amino_acid: *p_peptide) {
+
+            std::list<pappso::AaModificationP> aa_modif_list = amino_acid.getModificationList();
+
+
+            for (auto && aa_modif : aa_modif_list) {
+                if (!aa_modif->isInternal()) {
+                    _output_stream->writeStartElement("mod");
+                    _output_stream->writeAttribute("ref", _map_modifications.at(aa_modif));
+                    _output_stream->writeAttribute("position", QString ("%1").arg(i));
+                    _output_stream->writeAttribute("aa", QString(amino_acid.getLetter()));
+                    _output_stream->writeEndElement();// mod
+                }
+            }
+            i++;
+        }
+
+        _output_stream->writeEndElement();// peptide
+    }
+    _output_stream->writeEndElement(); //peptide_list
+    qDebug() << "Xpip::writePeptideList end";
+}
+
+void Xpip::writeIdentificationGroupList() {
+    qDebug() << "Xpip::writeIdentificationGroupList begin";
+    _output_stream->writeStartElement("identification_group_list");
+    for (const IdentificationGroup * p_identification_group : _sp_project.get()->getIdentificationGroupList()) {
+        //std::vector<ProteinMatch *> & getProteinMatchList()
+        writeIdentificationGroup(p_identification_group);
+    }
+    _output_stream->writeEndElement(); //identification_group_list
+    qDebug() << "Xpip::writeIdentificationGroupList end";
+}
+
+void Xpip::writeIdentificationGroup(const IdentificationGroup * p_identification_group) {
+    qDebug() << "Xpip::writeIdentificationGroup begin";
+    _output_stream->writeStartElement("protein_match_list");
+    for (const ProteinMatch * p_protein_match : p_identification_group->getProteinMatchList()) {
+        //std::vector<ProteinMatch *> & getProteinMatchList()
+        _output_stream->writeStartElement("protein_match");
+        _output_stream->writeAttribute("acc", p_protein_match->getProteinXtpSp().get()->getAccession());
+        _output_stream->writeEndElement();// protein_match
+    }
+    _output_stream->writeEndElement(); //protein_match_list
+    qDebug() << "Xpip::writeIdentificationGroup end";
 }
diff --git a/src/output/xpip.h b/src/output/xpip.h
index 595eae0ea..2775fb89b 100644
--- a/src/output/xpip.h
+++ b/src/output/xpip.h
@@ -53,12 +53,22 @@ private :
     void writeFastaFileList(const FastaFileStore & fasta_store);
     void writeDoubleAttribute(const QString & attribute, pappso::pappso_double value);
     void writeBooleanAttribute(const QString & attribute, bool value);
+    
+    void writeProteinList();
+    void writePeptideList();
+    void writeLabelingMethod();
+    void writeIdentificationGroupList();
+    void writeIdentificationGroup(const IdentificationGroup * p_identification_group);
 
+    QString getPeptideId(std::size_t crc_peptide) const;
 
 private :
     QFile * _output_file;
     QXmlStreamWriter * _output_stream;
     ProjectSp _sp_project;
+    
+    std::map<pappso::AaModificationP, QString> _map_modifications;
+    std::map<const PeptideXtp *, QString> _map_peptides;
 
 };
 
diff --git a/src/utils/peptidestore.cpp b/src/utils/peptidestore.cpp
index aeba5eedd..0e3ce9405 100644
--- a/src/utils/peptidestore.cpp
+++ b/src/utils/peptidestore.cpp
@@ -103,3 +103,7 @@ void PeptideStore::setLabelingMethodSp(LabelingMethodSp labeling_method_sp) {
         peptide_xtp.get()->applyLabelingMethod(labeling_method_sp);
     }
 }
+
+const std::unordered_map<std::size_t, PeptideXtpSp> & PeptideStore::getPeptideMap() const {
+    return _map_crc_peptide_list;
+}
diff --git a/src/utils/peptidestore.h b/src/utils/peptidestore.h
index 81dc770b3..cb2099c16 100644
--- a/src/utils/peptidestore.h
+++ b/src/utils/peptidestore.h
@@ -56,6 +56,8 @@ public:
     /** @brief apply labeling method to all peptide match
      * */
     void setLabelingMethodSp(LabelingMethodSp labeling_method_sp);
+    
+    const std::unordered_map<std::size_t, PeptideXtpSp> & getPeptideMap() const;
 private:
     /** @brief reset labeling method
      * */
diff --git a/src/utils/proteinstore.cpp b/src/utils/proteinstore.cpp
index 7a8640977..9a23471dc 100644
--- a/src/utils/proteinstore.cpp
+++ b/src/utils/proteinstore.cpp
@@ -69,6 +69,11 @@ void ProteinStore::setRegexpContaminantPattern(const QString & pattern) {
 QRegExp ProteinStore::getRegexpDecoy() const {
     return (_regexp_decoy);
 }
+
+const std::map<QString, ProteinXtpSp> & ProteinStore::getProteinMap() const {
+    return _map_accession_protein_list;
+}
+
 void ProteinStore::setRegexpDecoyPattern(const QString & pattern) {
     _regexp_decoy.setPattern(pattern);
 
diff --git a/src/utils/proteinstore.h b/src/utils/proteinstore.h
index fa06e1304..14c5ef5fc 100644
--- a/src/utils/proteinstore.h
+++ b/src/utils/proteinstore.h
@@ -69,6 +69,8 @@ public:
     
     void setContaminantAccession(QString accession);
     void setDecoyAccession(QString accession);
+    
+    const std::map<QString, ProteinXtpSp> & getProteinMap() const;
 
 private :
     void setProteinInformations(ProteinXtpSp & protein_in);
-- 
GitLab