From c798286525afeb6b75e991aef16cadfb5d89d2b1 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Thu, 22 Mar 2018 17:13:01 +0100
Subject: [PATCH] WIP: writing PTM spectra ODS sheet

---
 src/CMakeLists.txt                            |   1 +
 src/grouping/ptm/ptmisland.cpp                |   3 +
 src/grouping/ptm/ptmisland.h                  |   2 +
 .../export_spreadsheet_dialog.ui              |  74 ++++++---
 .../exportspreadsheetdialog.cpp               |   8 +
 .../exportspreadsheetdialog.h                 |   1 +
 .../peptide_list_view/peptidetablemodel.cpp   |  22 ++-
 src/gui/peptide_list_view/peptidetablemodel.h |   8 +-
 src/output/ods/odsexport.cpp                  |   6 +
 src/output/ods/ptm/ptmspectrasheet.cpp        | 156 ++++++++++++------
 src/output/ods/ptm/ptmspectrasheet.h          |   3 +-
 11 files changed, 201 insertions(+), 83 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 69230341c..62d206981 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -102,6 +102,7 @@ SET(CPP_FILES
   input/xtandemsaxhandler.cpp
   input/xtpxpipsaxhandler.cpp
   output/ods/ptm/ptmislandsheet.cpp
+  output/ods/ptm/ptmspectrasheet.cpp
   output/ods/comparbasesheet.cpp
   output/ods/comparspecificspectrasheet.cpp
   output/ods/comparspectrasheet.cpp
diff --git a/src/grouping/ptm/ptmisland.cpp b/src/grouping/ptm/ptmisland.cpp
index bae1d3bf0..388780249 100644
--- a/src/grouping/ptm/ptmisland.cpp
+++ b/src/grouping/ptm/ptmisland.cpp
@@ -172,6 +172,9 @@ bool PtmIsland::merge(PtmIslandSp ptm_island_sp) {
         return true;
     }
 }
+const std::vector<PeptideMatch> & PtmIsland::getPeptideMatchList() const {
+    return _peptide_match_list;
+}
 
 std::vector<PtmSampleScanSp> PtmIsland::getPtmSampleScanSpList() const {
     qDebug() << "PtmIsland::getPtmSampleScanSpList begin" ;
diff --git a/src/grouping/ptm/ptmisland.h b/src/grouping/ptm/ptmisland.h
index b1fe701b5..7d66eaa3d 100644
--- a/src/grouping/ptm/ptmisland.h
+++ b/src/grouping/ptm/ptmisland.h
@@ -74,6 +74,8 @@ public:
     unsigned int size() const;
     
     std::vector<PtmSampleScanSp> getPtmSampleScanSpList() const;
+    
+    const std::vector<PeptideMatch> & getPeptideMatchList() const;
 
 private:
     const ProteinMatch * _protein_match_p;
diff --git a/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui b/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
index 72cf2b741..c2d5c0c1d 100644
--- a/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
+++ b/src/gui/export_spreadsheet_dialog/export_spreadsheet_dialog.ui
@@ -121,14 +121,21 @@
         <property name="title">
          <string>PTM</string>
         </property>
-        <layout class="QVBoxLayout" name="verticalLayout">
-         <item>
+        <layout class="QGridLayout" name="gridLayout_2">
+         <item row="0" column="0">
           <widget class="QCheckBox" name="ptmislands_checkbox">
            <property name="text">
             <string>PTM islands</string>
            </property>
           </widget>
          </item>
+         <item row="0" column="1">
+          <widget class="QCheckBox" name="ptmspectra_checkbox">
+           <property name="text">
+            <string>PTM spectra</string>
+           </property>
+          </widget>
+         </item>
         </layout>
        </widget>
       </item>
@@ -178,7 +185,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>665</x>
-     <y>523</y>
+     <y>507</y>
     </hint>
     <hint type="destinationlabel">
      <x>286</x>
@@ -194,7 +201,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>579</x>
-     <y>523</y>
+     <y>507</y>
     </hint>
     <hint type="destinationlabel">
      <x>286</x>
@@ -209,8 +216,8 @@
    <slot>doGroups(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>210</x>
-     <y>101</y>
+     <x>231</x>
+     <y>69</y>
     </hint>
     <hint type="destinationlabel">
      <x>839</x>
@@ -225,8 +232,8 @@
    <slot>doProteins(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>108</x>
-     <y>133</y>
+     <x>129</x>
+     <y>101</y>
     </hint>
     <hint type="destinationlabel">
      <x>814</x>
@@ -241,8 +248,8 @@
    <slot>doPeptides(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>382</x>
-     <y>165</y>
+     <x>403</x>
+     <y>133</y>
     </hint>
     <hint type="destinationlabel">
      <x>857</x>
@@ -257,8 +264,8 @@
    <slot>doSpectra(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>99</x>
-     <y>197</y>
+     <x>120</x>
+     <y>165</y>
     </hint>
     <hint type="destinationlabel">
      <x>760</x>
@@ -273,8 +280,8 @@
    <slot>doPeptidePos(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>317</x>
-     <y>229</y>
+     <x>338</x>
+     <y>197</y>
     </hint>
     <hint type="destinationlabel">
      <x>764</x>
@@ -289,8 +296,8 @@
    <slot>doSamples(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>244</x>
-     <y>261</y>
+     <x>265</x>
+     <y>229</y>
     </hint>
     <hint type="destinationlabel">
      <x>835</x>
@@ -305,8 +312,8 @@
    <slot>doComparSpectra(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>124</x>
-     <y>328</y>
+     <x>157</x>
+     <y>296</y>
     </hint>
     <hint type="destinationlabel">
      <x>785</x>
@@ -322,7 +329,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>437</x>
-     <y>328</y>
+     <y>296</y>
     </hint>
     <hint type="destinationlabel">
      <x>836</x>
@@ -338,7 +345,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>231</x>
-     <y>360</y>
+     <y>328</y>
     </hint>
     <hint type="destinationlabel">
      <x>736</x>
@@ -354,7 +361,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>437</x>
-     <y>360</y>
+     <y>328</y>
     </hint>
     <hint type="destinationlabel">
      <x>753</x>
@@ -370,7 +377,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>642</x>
-     <y>328</y>
+     <y>296</y>
     </hint>
     <hint type="destinationlabel">
      <x>790</x>
@@ -386,7 +393,7 @@
    <hints>
     <hint type="sourcelabel">
      <x>642</x>
-     <y>360</y>
+     <y>328</y>
     </hint>
     <hint type="destinationlabel">
      <x>793</x>
@@ -401,8 +408,8 @@
    <slot>doPtmIslands(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>79</x>
-     <y>427</y>
+     <x>112</x>
+     <y>407</y>
     </hint>
     <hint type="destinationlabel">
      <x>765</x>
@@ -410,6 +417,22 @@
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>ptmspectra_checkbox</sender>
+   <signal>clicked(bool)</signal>
+   <receiver>ExportSpreadsheetDialog</receiver>
+   <slot>doPtmSpectra(bool)</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>389</x>
+     <y>392</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>976</x>
+     <y>366</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>reject()</slot>
@@ -428,5 +451,6 @@
   <slot>doComparPai(bool)</slot>
   <slot>doComparEmpai(bool)</slot>
   <slot>doPtmIslands(bool)</slot>
+  <slot>doPtmSpectra(bool)</slot>
  </slots>
 </ui>
diff --git a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
index 7a28da0d2..45a1a878f 100644
--- a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
+++ b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.cpp
@@ -96,6 +96,10 @@ ExportSpreadsheetDialog::ExportSpreadsheetDialog(QWidget * parent):
     if (settings.value("export_ods/ptmislands", "true").toBool()) {
         ui->ptmislands_checkbox->setCheckState(Qt::Checked);
     }
+    ui->ptmspectra_checkbox->setCheckState(Qt::Unchecked);
+    if (settings.value("export_ods/ptmspectra", "true").toBool()) {
+        ui->ptmspectra_checkbox->setCheckState(Qt::Checked);
+    }
     //param.setFilterCrossSamplePeptideNumber(settings.value("automatic_filter/cross_sample", "true").toBool());
 
 #if QT_VERSION >= 0x050000
@@ -192,6 +196,10 @@ void ExportSpreadsheetDialog::doPtmIslands(bool simple) {
     QSettings settings;
     settings.setValue("export_ods/ptmislands", QString("%1").arg(simple));
 }
+void ExportSpreadsheetDialog::doPtmSpectra(bool simple) {
+    QSettings settings;
+    settings.setValue("export_ods/ptmspectra", QString("%1").arg(simple));
+}
 
 void ExportSpreadsheetDialog::setProject(const Project * p_project) {
 
diff --git a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.h b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.h
index 15113e8da..33892c523 100644
--- a/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.h
+++ b/src/gui/export_spreadsheet_dialog/exportspreadsheetdialog.h
@@ -60,6 +60,7 @@ public slots:
     void doPeptides(bool simple);
     void doProteins(bool simple);
     void doPtmIslands(bool simple);
+    void doPtmSpectra(bool simple);
     
 signals:
 
diff --git a/src/gui/peptide_list_view/peptidetablemodel.cpp b/src/gui/peptide_list_view/peptidetablemodel.cpp
index 866894021..396aac97e 100644
--- a/src/gui/peptide_list_view/peptidetablemodel.cpp
+++ b/src/gui/peptide_list_view/peptidetablemodel.cpp
@@ -68,7 +68,7 @@ int PeptideTableModel::rowCount(const QModelIndex &parent ) const {
     return 0;
 }
 int PeptideTableModel::columnCount(const QModelIndex &parent ) const {
-    return 20;
+    return 22;
 }
 QVariant PeptideTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
@@ -185,12 +185,18 @@ QVariant PeptideTableModel::data(const QModelIndex &index, int role ) const {
             case (std::int8_t)PeptideListColumn::Evalue:
                 return QVariant((qreal) _p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getEvalue());
                 break;
+            case (std::int8_t)PeptideListColumn::experimental_mhplus:
+                return QVariant((qreal) (_p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getExperimentalMass()+pappso::MHPLUS));
+                break;
             case (std::int8_t)PeptideListColumn::theoretical_mhplus:
                 return QVariant((qreal) _p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getPeptideXtpSp().get()->getMz(1));
                 break;
             case (std::int8_t)PeptideListColumn::delta_mhplus:
                 return QVariant((qreal) _p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getDeltaMass());
                 break;
+            case (std::int8_t)PeptideListColumn::delta_ppm:
+                return QVariant((qreal) _p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getPpmDeltaMass());
+                break;
             case (std::int8_t)PeptideListColumn::hyperscore:
                 return _p_protein_match->getPeptideMatchList().at(row).getPeptideEvidence()->getParam(PeptideEvidenceParam::tandem_hyperscore);
                 break;
@@ -281,12 +287,18 @@ const QString PeptideTableModel::getTitle(std::int8_t column) {
     case (std::int8_t) PeptideListColumn::Evalue:
         return "Evalue";
         break;
+    case (std::int8_t) PeptideListColumn::experimental_mhplus:
+        return "observed MH+";
+        break;
     case (std::int8_t) PeptideListColumn::theoretical_mhplus:
         return "theoretical MH+";
         break;
     case (std::int8_t) PeptideListColumn::delta_mhplus:
         return "delta MH+";
         break;
+    case (std::int8_t) PeptideListColumn::delta_ppm:
+        return "delta ppm";
+        break;
     case (std::int8_t) PeptideListColumn::hyperscore:
         return "hyperscore";
         break;
@@ -350,11 +362,17 @@ const QString PeptideTableModel::getDescription(std::int8_t column) {
     case (std::int8_t) PeptideListColumn::Evalue:
         return "peptide Evalue";
         break;
+    case (std::int8_t) PeptideListColumn::experimental_mhplus:
+        return "observed peptide mass + H+";
+        break;
     case (std::int8_t) PeptideListColumn::theoretical_mhplus:
         return "peptide theoretical MH+";
         break;
     case (std::int8_t) PeptideListColumn::delta_mhplus:
-        return "peptide mass difference between observed mass and theoretical mass";
+        return "peptide mass difference in dalton between theoretical mass and observed mass (theoretical - observed)";
+        break;
+    case (std::int8_t) PeptideListColumn::delta_ppm:
+        return "peptide mass difference in ppm between theoretical mass and observed mass";
         break;
     case (std::int8_t) PeptideListColumn::hyperscore:
         return "X!Tandem hyperscore";
diff --git a/src/gui/peptide_list_view/peptidetablemodel.h b/src/gui/peptide_list_view/peptidetablemodel.h
index 2281ede65..5aa61a294 100644
--- a/src/gui/peptide_list_view/peptidetablemodel.h
+++ b/src/gui/peptide_list_view/peptidetablemodel.h
@@ -49,9 +49,11 @@ enum class PeptideListColumn: std::int8_t {
     subgroups=14,
     Evalue=15,
     theoretical_mhplus=16,
-    delta_mhplus=17,
-    hyperscore=18,
-    mascot_score=19
+    experimental_mhplus=17,
+    delta_mhplus=18,
+    delta_ppm=19,
+    hyperscore=20,
+    mascot_score=21
 };
 
 class PeptideListWindow;
diff --git a/src/output/ods/odsexport.cpp b/src/output/ods/odsexport.cpp
index 86699dee8..ad1b661c6 100644
--- a/src/output/ods/odsexport.cpp
+++ b/src/output/ods/odsexport.cpp
@@ -39,6 +39,7 @@
 #include "samplesheet.h"
 #include "groupingsheet.h"
 #include "ptm/ptmislandsheet.h"
+#include "ptm/ptmspectrasheet.h"
 #include <QSettings>
 
 OdsExport::OdsExport(const Project * project):_p_project(project) {
@@ -124,5 +125,10 @@ void OdsExport::write(CalcWriterInterface * p_writer, WorkMonitorInterface * p_m
             p_monitor->message(QObject::tr("writing PTM islands"));
             PtmIslandSheet(this, p_writer, _p_project);
         }
+
+        if (settings.value("export_ods/ptmspectra", "true").toBool()) {
+            p_monitor->message(QObject::tr("writing PTM spectra"));
+            PtmSpectraSheet(this, p_writer, _p_project);
+        }
     }
 }
diff --git a/src/output/ods/ptm/ptmspectrasheet.cpp b/src/output/ods/ptm/ptmspectrasheet.cpp
index 49bc3ff82..b8010e170 100644
--- a/src/output/ods/ptm/ptmspectrasheet.cpp
+++ b/src/output/ods/ptm/ptmspectrasheet.cpp
@@ -70,33 +70,82 @@ void PtmSpectraSheet::writeHeaders(IdentificationGroup * p_ident)  {
 
 
     _p_writer->writeLine();
+
+    // Group ID
     _p_writer->writeCell("Group ID");
+
+    // Sub-group ID
+
+    // Phosphopeptide ID
     _p_writer->writeCell("Peptide ID");
-    _p_writer->setCellAnnotation("MS sample name (MS run)");
-    _p_writer->writeCell("Sample");
+
+    // Modifs (top)	Best position in peptide	All observed positions in phosphopeptide	Number of phosphoislands	Phosphoislands Ids	Best e-value	Charge	MH+ Obs	MH+ theo	DeltaMH+	Delta-ppm
+
+    // Sample
+    writeCellHeader(PeptideListColumn::sample);
+
+    // Scan
     writeCellHeader(PeptideListColumn::scan);
-    _p_writer->setCellAnnotation("retention time in seconds");
-    _p_writer->writeCell("Rt (seconds)");
-    _p_writer->setCellAnnotation("experimental precursor m/z");
-    _p_writer->writeCell("Precursor m/z");
+
+    // Rt
+    writeCellHeader(PeptideListColumn::rt);
+
+    // Sequence (top)
     _p_writer->setCellAnnotation("peptide sequence (best match)");
-    _p_writer->writeCell("Sequence (top)");
-    _p_writer->setCellAnnotation("peptide charge (best match)");
-    _p_writer->writeCell("Charge (top)");
-    _p_writer->setCellAnnotation("peptide modifications (best match)");
-    _p_writer->writeCell("Modifs (top)");
+    _p_writer->writeCell("sequence (top)");
+
+
+    // Modifs (top)
+    _p_writer->setCellAnnotation("peptide modification list (best match)");
+    _p_writer->writeCell("modifs (top)");
+
     if (_p_project->getLabelingMethodSp().get() != nullptr) {
         _p_writer->setCellAnnotation("peptide label (best match)");
         _p_writer->writeCell("Label (top)");
     }
-    _p_writer->writeCell("Best E-value");
-    _p_writer->writeCell("Best hyperscore");
+
+    // Best position in peptide
+    _p_writer->setCellAnnotation("best PTM positions for this sample/scan");
+    _p_writer->writeCell("best positions in peptide");
+
+    // All observed positions in phosphopeptide
+    _p_writer->setCellAnnotation("All observed positions for this sample/scan");
+    _p_writer->writeCell("all positions in peptide");
+
+    // Number of phosphoislands
+    _p_writer->setCellAnnotation("Number of phosphoislands share this sample/scan");
+    _p_writer->writeCell("number of phosphoislands");
+
+    // Phosphoislands Ids
+    _p_writer->setCellAnnotation("Phosphoislands id list");
+    _p_writer->writeCell("phosphoislands");
+
+    // Best e-value
+    _p_writer->writeCell("best E-value");
+
+
+    _p_writer->writeCell("best hyperscore");
+
+
+    // Charge
+    writeCellHeader(PeptideListColumn::charge);
+
+    // MH+ Obs
+    writeCellHeader(PeptideListColumn::experimental_mhplus);
+
+    // MH+ theo
+    writeCellHeader(PeptideListColumn::theoretical_mhplus);
+
+    // DeltaMH+
     writeCellHeader(PeptideListColumn::delta_mhplus);
 
+    // Delta-ppm
+    writeCellHeader(PeptideListColumn::delta_ppm);
+
 
 }
 
-void PtmSpectraSheet::writeBestPeptideEvidence(const GroupingGroup * p_group,const PeptideEvidence * p_peptide_evidence) {
+void PtmSpectraSheet::writeBestPeptideEvidence(const PeptideEvidence * p_peptide_evidence, std::set<const PtmIsland *> & ptmisland_occurence_list) {
 
     _p_writer->writeLine();
 
@@ -136,50 +185,53 @@ void PtmSpectraSheet::writeIdentificationGroup(IdentificationGroup * p_ident) {
     _p_ptm_grouping_experiment = p_ident->getPtmGroupingExperiment();
 
     writeHeaders(p_ident);
-    _p_ptm_grouping_experiment->get
-    for (const std::pair<unsigned int, GroupingGroupSp> & group_pair : p_ident->getGroupStore().getGroupMap()) {
-
-        std::vector<const PeptideEvidence *> peptide_evidence_list = group_pair.second.get()->getPeptideEvidenceList();
-
-        std::sort(peptide_evidence_list.begin(), peptide_evidence_list.end(),
-                  [](const PeptideEvidence * a, const PeptideEvidence * b)
-        {
-            unsigned int arank = a->getGrpPeptideSp().get()->getRank();
-            unsigned int ascan = a->getScan();
-            unsigned int brank = b->getGrpPeptideSp().get()->getRank();
-            unsigned int bscan = b->getScan();
-            return  std::tie(arank, a->getMsRunP()->getSampleName(), ascan) < std::tie(brank, b->getMsRunP()->getSampleName(), bscan);
-        });
-
-        const PeptideEvidence * p_best_peptide_evidence = nullptr;
-
-        for (auto & peptide_evidence:peptide_evidence_list) {
-            if (p_best_peptide_evidence == nullptr) {
-                p_best_peptide_evidence = peptide_evidence;
+    for (const PtmIslandSubgroupSp & ptm_island_subgroup_sp : _p_ptm_grouping_experiment->getPtmIslandSubgroupList() ) {
+        writePtmIslandSubgroupSp(ptm_island_subgroup_sp);
+    }
+
+    _p_writer->writeLine();
+    _p_writer->writeLine();
+    qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
+}
+
+void PtmSpectraSheet::writePtmIslandSubgroupSp(const PtmIslandSubgroupSp & ptm_island_subgroup_sp) {
+    qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
+    struct PtmSampleScanOccurence {
+        PtmSampleScan ptm_sample_scan;
+        std::set<const PtmIsland *> ptmisland_occurence_list;
+    };
+    std::vector<PtmSampleScanOccurence> ptm_sample_scan_occurence_list;
+
+    for (PtmIslandSp ptm_island_sp :ptm_island_subgroup_sp.get()->getPtmIslandList()) {
+        for (PeptideMatch peptide_match: ptm_island_sp.get()->getPeptideMatchList()) {
+            std::vector<PtmSampleScanOccurence>::iterator it_ptm = ptm_sample_scan_occurence_list.begin();
+            std::vector<PtmSampleScanOccurence>::iterator it_ptm_end = ptm_sample_scan_occurence_list.end();
+            while ((it_ptm != it_ptm_end) && (it_ptm->ptm_sample_scan.add(peptide_match) == false)) {
+                //peptide added
+                it_ptm++;
             }
-            //change spectra :
-            unsigned int arank = p_best_peptide_evidence->getGrpPeptideSp().get()->getRank();
-            unsigned int ascan = p_best_peptide_evidence->getScan();
-            unsigned int brank = peptide_evidence->getGrpPeptideSp().get()->getRank();
-            unsigned int bscan = peptide_evidence->getScan();
-
-            if (std::tie(arank, p_best_peptide_evidence->getMsRunP()->getSampleName(), ascan) != std::tie(brank, peptide_evidence->getMsRunP()->getSampleName(), bscan)) {
-                //write p_best_peptide_match
-                writeBestPeptideEvidence(group_pair.second.get(), p_best_peptide_evidence);
-                p_best_peptide_evidence = peptide_evidence;
+            if (it_ptm == it_ptm_end) {
+                qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
+                //peptide NOT added
+                PtmSampleScanOccurence new_occurence = {PtmSampleScan(peptide_match),std::set<const PtmIsland *>()};
+                new_occurence.ptmisland_occurence_list.insert(ptm_island_sp.get());
+                ptm_sample_scan_occurence_list.push_back(new_occurence);
+                qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
             }
             else {
-                if (p_best_peptide_evidence->getEvalue()> peptide_evidence->getEvalue()) {
-                    p_best_peptide_evidence = peptide_evidence;
-                }
+                 //peptide added
+                qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
+                it_ptm->ptmisland_occurence_list.insert(ptm_island_sp.get());
+                
+                qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
             }
         }
+    }
+    
+    qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
 
-        if (p_best_peptide_evidence != nullptr) {
-            writeBestPeptideEvidence(group_pair.second.get(), p_best_peptide_evidence);
-        }
+    for (PtmSampleScanOccurence & occurence: ptm_sample_scan_occurence_list) {
+        writeBestPeptideEvidence(occurence.ptm_sample_scan.getRepresentativePeptideMatch().getPeptideEvidence(), occurence.ptmisland_occurence_list);
     }
-    _p_writer->writeLine();
-    _p_writer->writeLine();
     qDebug() << __FILE__ << " " << __FUNCTION__<< " " << __LINE__;
 }
diff --git a/src/output/ods/ptm/ptmspectrasheet.h b/src/output/ods/ptm/ptmspectrasheet.h
index deba3a2c2..3a7f48bfd 100644
--- a/src/output/ods/ptm/ptmspectrasheet.h
+++ b/src/output/ods/ptm/ptmspectrasheet.h
@@ -45,9 +45,10 @@ protected :
     PtmSpectraSheet (OdsExport * p_ods_export, CalcWriterInterface * p_writer, const Project * p_project, const QString & sheet_name);
     virtual void writeIdentificationGroup(IdentificationGroup * p_ident);
     void writeHeaders(IdentificationGroup * p_ident);
-    void writeBestPeptideEvidence(const GroupingGroup * p_group, const PeptideEvidence * p_peptide_evidence);
+    void writeBestPeptideEvidence(const PeptideEvidence * p_peptide_evidence, std::set<const PtmIsland *> & ptmisland_occurence_list);
     void writeCellHeader(PeptideListColumn column);
 
+    void writePtmIslandSubgroupSp(const PtmIslandSubgroupSp & ptm_island_subgroup_sp);
 protected :
     OdsExport * _p_ods_export;
     const PtmGroupingExperiment * _p_ptm_grouping_experiment;
-- 
GitLab