From a27b3d698c680249630ef63d1ecd53dec296ba23 Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Sun, 30 Apr 2017 13:57:27 +0200
Subject: [PATCH] peptide pos ODS sheet added

---
 src/CMakeLists.txt                            |   1 +
 .../export_spreadsheet_dialog.ui              |   7 +
 .../exportspreadsheetdialog.cpp               |   4 +
 src/output/ods/odsexport.cpp                  |   4 +
 src/output/ods/peptidepossheet.cpp            | 159 ++++++++++++++++++
 src/output/ods/peptidepossheet.h              |  55 ++++++
 6 files changed, 230 insertions(+)
 create mode 100644 src/output/ods/peptidepossheet.cpp
 create mode 100644 src/output/ods/peptidepossheet.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c08b83a5..ef8833ad 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -71,6 +71,7 @@ SET(CPP_FILES
   input/xtandemsaxhandler.cpp
   output/masschroqml.cpp
   output/ods/odsexport.cpp
+  output/ods/peptidepossheet.cpp
   output/ods/peptidesheet.cpp
   output/ods/proteinsheet.cpp
   output/ods/simplesheet.cpp
diff --git a/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui b/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
index cb9dbb9f..62c6fed4 100644
--- a/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
+++ b/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
@@ -48,6 +48,13 @@
         </property>
        </widget>
       </item>
+      <item>
+       <widget class="QCheckBox" name="peptidepos_checkbox">
+        <property name="text">
+         <string>peptide pos : lis of identified peptide sequences per protein</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
diff --git a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
index 7fdd01b8..1ab11695 100644
--- a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
+++ b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
@@ -53,6 +53,10 @@ ExportSpreadsheetDialog::ExportSpreadsheetDialog(QWidget * parent):
     if (settings.value("export_ods/spectra", "true").toBool()) {
         ui->spectra_checkbox->setCheckState(Qt::Checked);
     }
+    ui->peptidepos_checkbox->setCheckState(Qt::Unchecked);
+    if (settings.value("export_ods/peptidepos", "true").toBool()) {
+        ui->peptidepos_checkbox->setCheckState(Qt::Checked);
+    }
     //param.setFilterCrossSamplePeptideNumber(settings.value("automatic_filter/cross_sample", "true").toBool());
     
 #if QT_VERSION >= 0x050000
diff --git a/src/output/ods/odsexport.cpp b/src/output/ods/odsexport.cpp
index b77b165c..adce377b 100644
--- a/src/output/ods/odsexport.cpp
+++ b/src/output/ods/odsexport.cpp
@@ -32,6 +32,7 @@
 #include "proteinsheet.h"
 #include "peptidesheet.h"
 #include "spectrasheet.h"
+#include "peptidepossheet.h"
 #include <QSettings>
 
 OdsExport::OdsExport(const Project * project):_p_project(project) {
@@ -68,4 +69,7 @@ void OdsExport::write(CalcWriterInterface * p_writer) {
     if (settings.value("export_ods/spectra", "true").toBool()) {
         SpectraSheet(this, p_writer, _p_project);
     }
+    if (settings.value("export_ods/peptidepos", "true").toBool()) {
+        PeptidePosSheet(this, p_writer, _p_project);
+    }
 }
diff --git a/src/output/ods/peptidepossheet.cpp b/src/output/ods/peptidepossheet.cpp
new file mode 100644
index 00000000..d3145f0f
--- /dev/null
+++ b/src/output/ods/peptidepossheet.cpp
@@ -0,0 +1,159 @@
+/**
+ * \file output/ods/peptidepossheet.cpp
+ * \date 30/4/2017
+ * \author Olivier Langella
+ * \brief ODS peptide pos sheet
+ */
+
+/*******************************************************************************
+* 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 "peptidepossheet.h"
+#include <tuple>
+#include <pappsomspp/utils.h>
+#include <QDebug>
+
+
+PeptidePosSheet::PeptidePosSheet (OdsExport * p_ods_export, CalcWriterInterface * p_writer, const Project * p_project): _p_project(p_project) {
+    _p_ods_export = p_ods_export;
+    _p_writer = p_writer;
+    p_writer->writeSheet("peptide pos");
+
+    std::vector<IdentificationGroup *> identification_list = p_project->getIdentificationGroupList();
+    for (IdentificationGroup * p_ident:identification_list) {
+        //writeHeaders(p_ident);
+        writeIdentificationGroup(p_ident);
+    }
+}
+
+
+void PeptidePosSheet::writeHeaders(IdentificationGroup * p_ident)  {
+    // Peptide ID	Protein ID	accession	description	Sequence	Modifs	Start	Stop	MH+ theo
+
+
+    //MS Sample :	20120906_balliau_extract_1_A01_urnb-1
+    const std::vector<MsRunSp> msrun_list = p_ident->getMsRunSpList();
+    if (msrun_list.size() == 1) {
+        _p_writer->writeCell("sample");
+        _p_writer->writeLine();
+        _p_writer->writeCell(msrun_list[0].get()->getSampleName());
+        _p_writer->writeLine();
+    }
+
+
+    _p_writer->writeLine();
+    _p_writer->writeCell("Group ID");
+    _p_writer->writeCell("Peptide ID");
+    //_p_writer->setCellAnnotation("MS sample name (MS run)");
+    _p_writer->writeCell("Protein ID");
+    _p_writer->writeCell("accession");
+    _p_writer->writeCell("description");
+    _p_writer->writeCell("Sequence");
+    _p_writer->writeCell("Modifs");
+    _p_writer->writeCell("Start");
+    _p_writer->writeCell("Stop");
+    _p_writer->writeCell("MH+ theo");
+
+
+}
+
+void PeptidePosSheet::writeBestPeptideMatch(const ProteinMatch * p_protein_match,const PeptideMatch * p_peptide_match) {
+
+    _p_writer->writeLine();
+
+
+    unsigned int group_number = p_peptide_match->getGrpPeptideSp().get()->getGroupNumber();
+    unsigned int rank_number = p_peptide_match->getGrpPeptideSp().get()->getRank();
+
+    _p_ods_export->setEvenOrOddStyle(group_number, _p_writer);
+    _p_writer->writeCell(pappso::Utils::getLexicalOrderedString(group_number));
+    _p_ods_export->setEvenOrOddStyle(rank_number, _p_writer);
+    _p_writer->writeCell(p_peptide_match->getGrpPeptideSp().get()->getGroupingId());
+    _p_writer->clearTableCellStyleRef();
+    _p_writer->writeCell(p_protein_match->getGrpProteinSp().get()->getGroupingId());
+    _p_writer->writeCell(p_protein_match->getProteinXtpSp().get()->getAccession());
+    _p_writer->writeCell(p_protein_match->getProteinXtpSp().get()->getDescription());
+    _p_writer->writeCell(p_peptide_match->getPeptideXtpSp().get()->getSequence());
+    _p_writer->writeCell(p_peptide_match->getPeptideXtpSp().get()->getModifString());
+    _p_writer->writeCell(p_peptide_match->getStart()+1);
+    _p_writer->writeCell(p_peptide_match->getStart()+p_peptide_match->getPeptideXtpSp().get()->getSequence().size());
+    _p_writer->writeCell(p_peptide_match->getPeptideXtpSp().get()->getMz(1));
+
+
+}
+
+void PeptidePosSheet::writeIdentificationGroup(IdentificationGroup * p_ident) {
+qDebug() << "PeptidePosSheet::writeIdentificationGroup begin";
+    writeHeaders(p_ident);
+    for (ProteinMatch * p_protein_match : p_ident->getProteinMatchList()) {
+        if (p_protein_match->getValidationState() < ValidationState::grouped) continue;
+
+
+        std::vector<PeptideMatch *> peptide_match_list;
+
+        for (auto & peptide_match: p_protein_match->getPeptideMatchList()) {
+            if (peptide_match->getValidationState() < ValidationState::grouped) continue;
+            peptide_match_list.push_back(peptide_match);
+        }
+        std::sort(peptide_match_list.begin(), peptide_match_list.end(),
+                  [](const PeptideMatch * a, const PeptideMatch * b)
+        {
+            unsigned int arank = a->getGrpPeptideSp().get()->getRank();
+            unsigned int aposition = a->getStart();
+            unsigned int brank = b->getGrpPeptideSp().get()->getRank();
+            unsigned int bposition = b->getStart();
+            return  std::tie(arank, aposition) < std::tie(brank, bposition);
+        });
+
+        const PeptideMatch * p_best_peptide_match = nullptr;
+
+        for (auto & peptide_match:peptide_match_list) {
+            if (p_best_peptide_match == nullptr) {
+                p_best_peptide_match = peptide_match;
+            }
+            //change spectra :
+            unsigned int arank = p_best_peptide_match->getGrpPeptideSp().get()->getRank();
+            unsigned int aposition = p_best_peptide_match->getStart();
+            unsigned int brank = peptide_match->getGrpPeptideSp().get()->getRank();
+            unsigned int bposition = peptide_match->getStart();
+
+            if (std::tie(arank, aposition) != std::tie(brank, bposition)) {
+                //write p_best_peptide_match
+                writeBestPeptideMatch(p_protein_match, p_best_peptide_match);
+                p_best_peptide_match = peptide_match;
+            }
+            else {
+                if (p_best_peptide_match->getEvalue()> peptide_match->getEvalue()) {
+                    p_best_peptide_match = peptide_match;
+                }
+            }
+        }
+
+        if (p_best_peptide_match != nullptr) {
+            writeBestPeptideMatch(p_protein_match, p_best_peptide_match);
+
+        }
+    }
+    _p_writer->writeLine();
+    _p_writer->writeLine();
+qDebug() << "PeptidePosSheet::writeIdentificationGroup end";
+}
diff --git a/src/output/ods/peptidepossheet.h b/src/output/ods/peptidepossheet.h
new file mode 100644
index 00000000..de1777a0
--- /dev/null
+++ b/src/output/ods/peptidepossheet.h
@@ -0,0 +1,55 @@
+/**
+ * \file output/ods/peptidepossheet.h
+ * \date 30/4/2017
+ * \author Olivier Langella
+ * \brief ODS peptide pos sheet
+ */
+
+/*******************************************************************************
+* 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
+******************************************************************************/
+
+
+#ifndef PEPTIDEPOSSHEET_H
+#define PEPTIDEPOSSHEET_H
+
+
+#include "../../core/project.h"
+#include <odsstream/calcwriterinterface.h>
+#include "../../core/proteinmatch.h"
+#include "odsexport.h"
+
+class PeptidePosSheet
+{
+public :
+    PeptidePosSheet (OdsExport * p_ods_export, CalcWriterInterface * p_writer, const Project * p_project);
+private :
+    void writeIdentificationGroup(IdentificationGroup * p_ident);
+    void writeHeaders(IdentificationGroup * p_ident);
+    void writeBestPeptideMatch(const ProteinMatch * p_protein_match, const PeptideMatch * p_peptide_match);
+
+private :
+    OdsExport * _p_ods_export;
+    const Project * _p_project;
+    CalcWriterInterface * _p_writer;
+};
+
+#endif // PEPTIDEPOSSHEET_H
-- 
GitLab