From 85964c66c2364ead234cb7c90d0439d8290b7785 Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Mon, 24 Jul 2017 08:10:18 +0200
Subject: [PATCH] WIP: building ptm peptide table model

---
 src/CMakeLists.txt                            |   7 +
 src/grouping/ptm/ptmsamplescan.cpp            |  16 +-
 src/grouping/ptm/ptmsamplescan.h              |   1 +
 .../ptm_peptide_list_view.ui                  |  11 +-
 .../ptmpeptidelistwindow.cpp                  |  45 +++++
 .../ptmpeptidelistwindow.h                    |  28 ++-
 .../ptmpeptidetablemodel.cpp                  | 190 ++++++++++++++++++
 .../ptmpeptidetablemodel.h                    |  51 ++++-
 .../ptmpeptidetableproxymodel.cpp             |  13 ++
 .../ptmpeptidetableproxymodel.h               |  17 +-
 10 files changed, 368 insertions(+), 11 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index bdaca3a10..3895ace75 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -128,6 +128,9 @@ SET(XTPCPP_SRCS
   ./gui/ptm_island_list_view/ptmislandlistwindow.cpp
   ./gui/ptm_island_list_view/ptmislandtablemodel.cpp
   ./gui/ptm_island_list_view/ptmislandproxymodel.cpp
+  ./gui/ptm_peptide_list_view/ptmpeptidelistwindow.cpp
+  ./gui/ptm_peptide_list_view/ptmpeptidetablemodel.cpp
+  ./gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.cpp
   ./gui/waiting_message_dialog/waitingmessagedialog.cpp
   ./gui/workerthread.cpp
   ./utils/workmonitor.cpp
@@ -148,6 +151,7 @@ SET (GUI_UIS
   ./gui/protein_list_view/protein_view.ui
   ./gui/protein_view/protein_detail_view.ui
   ./gui/ptm_island_list_view/ptm_island_list_view.ui
+  ./gui/ptm_peptide_list_view/ptm_peptide_list_view.ui
   ./gui/waiting_message_dialog/waiting_message_dialog.ui
 )
 
@@ -174,6 +178,9 @@ SET(XTPCPP_MOC_HDRS
   ./gui/ptm_island_list_view/ptmislandlistwindow.h
   ./gui/ptm_island_list_view/ptmislandtablemodel.h
   ./gui/ptm_island_list_view/ptmislandproxymodel.h
+  ./gui/ptm_peptide_list_view/ptmpeptidelistwindow.h
+  ./gui/ptm_peptide_list_view/ptmpeptidetablemodel.h
+  ./gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.h
   ./gui/waiting_message_dialog/waitingmessagedialog.h
   ./gui/workerthread.h
   ./utils/workmonitor.h
diff --git a/src/grouping/ptm/ptmsamplescan.cpp b/src/grouping/ptm/ptmsamplescan.cpp
index 986bc7f36..f3db43d1d 100644
--- a/src/grouping/ptm/ptmsamplescan.cpp
+++ b/src/grouping/ptm/ptmsamplescan.cpp
@@ -30,6 +30,7 @@
 #include "ptmsamplescan.h"
 
 PtmSampleScan::PtmSampleScan(const PeptideMatch * p_peptide_match) {
+    _peptide_match_list.push_back(p_peptide_match);
 }
 
 PtmSampleScan::PtmSampleScan(const PtmSampleScan& other){
@@ -37,6 +38,19 @@ PtmSampleScan::PtmSampleScan(const PtmSampleScan& other){
 
 PtmSampleScan::~PtmSampleScan() {
 }
+const PeptideMatch * PtmSampleScan::getRepresentativePeptideMatch() const {
+    return _peptide_match_list[0];
+}
 bool PtmSampleScan::add(const PeptideMatch * p_peptide_match) {
-    return false;
+    if(getRepresentativePeptideMatch()->getHashSampleScan() != p_peptide_match->getHashSampleScan()) {
+        return false;
+    }
+    if(getRepresentativePeptideMatch()->getPeptideXtpSp().get()->getSequence() != p_peptide_match->getPeptideXtpSp().get()->getSequence()) {
+        return false;
+    }
+    if(getRepresentativePeptideMatch()->getPeptideXtpSp().get()->getMass() != p_peptide_match->getPeptideXtpSp().get()->getMass()) {
+        return false;
+    }
+    _peptide_match_list.push_back(p_peptide_match);
+    return true;
 }
diff --git a/src/grouping/ptm/ptmsamplescan.h b/src/grouping/ptm/ptmsamplescan.h
index 498933e81..f0446c3cc 100644
--- a/src/grouping/ptm/ptmsamplescan.h
+++ b/src/grouping/ptm/ptmsamplescan.h
@@ -47,6 +47,7 @@ public:
     PtmSampleScan(const PtmSampleScan& other);
     ~PtmSampleScan();
     
+    const PeptideMatch * getRepresentativePeptideMatch() const;
     bool add(const PeptideMatch * p_peptide_match);
 private:
     std::vector<const PeptideMatch *> _peptide_match_list;
diff --git a/src/gui/ptm_peptide_list_view/ptm_peptide_list_view.ui b/src/gui/ptm_peptide_list_view/ptm_peptide_list_view.ui
index 8e2c18919..f1e7c54d9 100644
--- a/src/gui/ptm_peptide_list_view/ptm_peptide_list_view.ui
+++ b/src/gui/ptm_peptide_list_view/ptm_peptide_list_view.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ui version="4.0">
- <class>PtmIslandListWindow</class>
- <widget class="QMainWindow" name="PtmIslandListWindow">
+ <class>PtmPeptideListWindow</class>
+ <widget class="QMainWindow" name="PtmPeptideListWindow">
   <property name="geometry">
    <rect>
     <x>0</x>
@@ -14,7 +14,7 @@
    <enum>Qt::CustomContextMenu</enum>
   </property>
   <property name="windowTitle">
-   <string>PTM island list</string>
+   <string>PTM peptide list</string>
   </property>
   <widget class="QWidget" name="centralwidget">
    <property name="contextMenuPolicy">
@@ -22,7 +22,7 @@
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
-     <widget class="QTableView" name="ptm_island_tableview">
+     <widget class="QTableView" name="ptm_peptide_tableview">
       <property name="contextMenuPolicy">
        <enum>Qt::DefaultContextMenu</enum>
       </property>
@@ -79,7 +79,7 @@
      <x>0</x>
      <y>0</y>
      <width>826</width>
-     <height>23</height>
+     <height>25</height>
     </rect>
    </property>
   </widget>
@@ -95,4 +95,3 @@
   <slot>doSearchOn(QString)</slot>
  </slots>
 </ui>
-
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.cpp b/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.cpp
index 3309a7dbe..69d462d69 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.cpp
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.cpp
@@ -28,3 +28,48 @@
 ******************************************************************************/
 
 #include "ptmpeptidelistwindow.h"
+#include "../ptm_island_list_view/ptmislandlistwindow.h"
+
+#include "ui_ptm_peptide_list_view.h"
+
+PtmPeptideListWindow::PtmPeptideListWindow(PtmIslandListWindow * parent):QMainWindow(parent),
+    ui(new Ui::PtmPeptideListWindow) {
+    _p_ptm_island_list_window = parent;
+    
+    ui->setupUi(this);
+    /*
+    */
+    _ptm_table_model_p = new PtmPeptideTableModel(this);
+
+
+    _ptm_proxy_model_p = new PtmPeptideTableProxyModel(this, _ptm_table_model_p);
+    _ptm_proxy_model_p->setSourceModel(_ptm_table_model_p);
+    _ptm_proxy_model_p->setDynamicSortFilter(true);
+    ui->ptm_peptide_tableview->setModel( _ptm_proxy_model_p );
+    ui->ptm_peptide_tableview->setSortingEnabled(true);
+    ui->ptm_peptide_tableview->setAlternatingRowColors(true);
+
+
+
+#if QT_VERSION >= 0x050000
+    // Qt5 code
+    /*
+    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
+    connect(this, &PtSpectrumViewer::operateMsDataFile, worker, &PwizLoaderThread::doMsDataFileLoad);
+    connect(worker, &PwizLoaderThread::msDataReady, this, &PtSpectrumViewer::handleMsDataFile);
+    */
+#else
+// Qt4 code
+    //connect (this, SIGNAL(ptmIslandDataChanged()), _ptm_table_model_p, SLOT(onPtmIslandDataChanged()));
+
+    //connect (_project_window, SIGNAL(identificationPtmGroupGrouped(IdentificationGroup *)), this,SLOT(doIdentificationPtmGroupGrouped(IdentificationGroup *)));
+    //connect (_project_window, SIGNAL(identificationGroupGrouped(IdentificationGroup *)), this,SLOT(doIdentificationGroupGrouped(IdentificationGroup *)));
+    connect(ui->ptm_peptide_tableview, SIGNAL(clicked(const QModelIndex &)), _ptm_table_model_p, SLOT(onTableClicked(const QModelIndex &)));
+
+#endif
+}
+PtmPeptideListWindow::~PtmPeptideListWindow() {
+}
+void PtmPeptideListWindow::setPtmIsland(PtmIsland * p_ptm_island) {
+    _p_ptm_island = _p_ptm_island;
+}
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.h b/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.h
index e6bc83630..5a6af5cf6 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.h
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidelistwindow.h
@@ -30,8 +30,32 @@
 #ifndef PTMPEPTIDELISTWINDOW_H
 #define PTMPEPTIDELISTWINDOW_H
 
-class PtmPeptideListWindow
-{
+#include <QMainWindow>
+#include "../../grouping/ptm/ptmisland.h"
+#include "ptmpeptidetablemodel.h"
+#include "ptmpeptidetableproxymodel.h"
+
+class PtmIslandListWindow;
+
+namespace Ui {
+class PtmPeptideListWindow;
+}
+
+class PtmPeptideListWindow: public QMainWindow {
+    Q_OBJECT
+public:
+
+    explicit PtmPeptideListWindow(PtmIslandListWindow * parent = 0);
+    ~PtmPeptideListWindow();
+    void setPtmIsland(PtmIsland * p_ptm_island);
+
+private:
+    PtmIsland * _p_ptm_island=nullptr;
+    Ui::PtmPeptideListWindow *ui;
+    PtmPeptideTableModel * _ptm_table_model_p = nullptr;
+    PtmPeptideTableProxyModel * _ptm_proxy_model_p = nullptr;
+    PtmIslandListWindow * _p_ptm_island_list_window;
+
 };
 
 #endif // PTMPEPTIDELISTWINDOW_H
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.cpp b/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.cpp
index 6ce26eecb..1ad88b010 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.cpp
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.cpp
@@ -27,3 +27,193 @@
 ******************************************************************************/
 
 #include "ptmpeptidetablemodel.h"
+#include "ptmpeptidelistwindow.h"
+#include <QDebug>
+
+PtmPeptideTableModel::PtmPeptideTableModel(PtmPeptideListWindow * p_ptm_peptide_list_window) {
+}
+
+PtmPeptideTableModel::~PtmPeptideTableModel() {
+}
+
+const QString PtmPeptideTableModel::getTitle(PtmPeptideListColumn column) {
+    qDebug() << "PtmPeptideTableModel::getTitle begin ";
+    return PtmPeptideTableModel::getTitle((std::int8_t) column);
+    //qDebug() << "ProteinTableModel::getTitle end ";
+}
+const QString PtmPeptideTableModel::getDescription(PtmPeptideListColumn column) {
+    qDebug() << "PtmPeptideTableModel::columnCount begin ";
+    return PtmPeptideTableModel::getDescription((std::int8_t) column);
+    //qDebug() << "ProteinTableModel::columnCount end ";
+}
+
+const QString PtmPeptideTableModel::getTitle(std::int8_t column) {
+    switch (column) {
+
+    case (std::int8_t) PtmPeptideListColumn::peptide_ptm_grouping_id:
+        return "peptide ID";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::sample:
+        return "sample";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::scan:
+        return "scan";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::rt:
+        return "RT";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::charge:
+        return "charge";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::sequence:
+        return "sequence";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::modifs:
+        return "modifs";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::label:
+        return "label";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::start:
+        return "start";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::length:
+        return "length";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::used:
+        return "used";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::subgroups:
+        return "subgroups";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::Evalue:
+        return "Evalue";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::theoretical_mhplus:
+        return "theoretical MH+";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::delta_mhplus:
+        return "delta MH+";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::hyperscore:
+        return "hyperscore";
+
+    }
+    return "";
+}
+
+const QString PtmPeptideTableModel::getDescription(std::int8_t column) {
+    switch (column) {
+
+    case (std::int8_t) PtmPeptideListColumn::peptide_ptm_grouping_id:
+        return "unique PTM peptide identifier within this PTM grouping experiment";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::sample:
+        return "MS sample name";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::scan:
+        return "scan number";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::rt:
+        return "retention time in seconds";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::charge:
+        return "peptide charge";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::sequence:
+        return "peptide sequence";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::modifs:
+        return "peptide modifications";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::label:
+        return "peptide label";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::start:
+        return "peptide start position on protein";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::length:
+        return "peptide length";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::used:
+        return "number of subgroups in which this peptide is used";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::subgroups:
+        return "list of subgroups in which this peptide is used";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::Evalue:
+        return "peptide Evalue";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::theoretical_mhplus:
+        return "peptide theoretical MH+";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::delta_mhplus:
+        return "peptide mass difference betwenn observed mass and theoretical mass";
+        break;
+    case (std::int8_t) PtmPeptideListColumn::hyperscore:
+        return "X!Tandem hyperscore";
+
+    }
+    return "";
+}
+
+int PtmPeptideTableModel::rowCount(const QModelIndex &parent ) const {
+    qDebug() << "PtmPeptideTableModel::rowCount begin ";
+    return _ptm_sample_scan_list.size();
+}
+int PtmPeptideTableModel::columnCount(const QModelIndex &parent ) const {
+    //qDebug() << "ProteinTableModel::columnCount begin ";
+    return 6;
+}
+QVariant PtmPeptideTableModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    if (orientation == Qt::Horizontal)
+    {
+        switch (role) {
+        case Qt::DisplayRole :
+            return QVariant(getTitle(section));
+            break;
+	case Qt::ToolTipRole :
+	   return QVariant(getDescription(section));
+            break;
+
+        }
+    }
+    return QVariant();
+}
+QVariant PtmPeptideTableModel::data(const QModelIndex &index, int role ) const {
+    // generate a log message when this method gets called
+    int row = index.row();
+    int col = index.column();
+    
+    if (row >= _ptm_sample_scan_list.size()) return QVariant();
+    switch(role) {
+    case Qt::DisplayRole:
+        switch (col) {
+
+            case (std::int8_t)PtmPeptideListColumn::peptide_ptm_grouping_id:
+                return QVariant();
+                break;
+            case (std::int8_t)PtmPeptideListColumn::sample:
+                return _ptm_sample_scan_list.at(row).get()->getRepresentativePeptideMatch()->getIdentificationDataSource()->getSampleName();
+                break;
+            case (std::int8_t)PtmPeptideListColumn::scan:
+                //return QVariant ((quint32) _p_protein_match->getPeptideMatchList().at(row)->getScan());
+                break;
+            case (std::int8_t)PtmPeptideListColumn::rt:
+                //return QVariant ((qreal) _p_protein_match->getPeptideMatchList().at(row)->getRetentionTime());
+                break;
+            case (std::int8_t)PtmPeptideListColumn::charge:
+                //return QVariant ((qreal) _p_protein_match->getPeptideMatchList().at(row)->getCharge());
+                break;
+            case (std::int8_t)PtmPeptideListColumn::sequence:
+                //return _p_protein_match->getPeptideMatchList().at(row)->getPeptideXtpSp().get()->getSequence();
+                break;
+            case (std::int8_t)PtmPeptideListColumn::modifs:
+                //return _p_protein_match->getPeptideMatchList().at(row)->getPeptideXtpSp().get()->getModifString();
+                break;
+
+        }
+    }
+    return QVariant();
+}
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.h b/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.h
index d1eed50da..96d744c52 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.h
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidetablemodel.h
@@ -29,8 +29,57 @@
 #ifndef PTMPEPTIDETABLEMODEL_H
 #define PTMPEPTIDETABLEMODEL_H
 
-class PtmPeptideTableModel
+#include <QAbstractTableModel>
+#include "../../grouping/ptm/ptmsamplescan.h"
+
+
+
+/** \def PtmPeptideListColumn list of available fields to display in ptm peptide list
+ *
+ */
+
+enum class PtmPeptideListColumn: std::int8_t {
+    peptide_ptm_grouping_id=0, ///< peptide PTM grouping id
+    sample=1,
+    scan=2,
+    rt=3,
+    charge=4,
+    sequence=5,
+    modifs=6,
+    label=7,
+    start=8,
+    length=9,
+    used=10,
+    subgroups=11,
+    Evalue=12,
+    theoretical_mhplus=13,
+    delta_mhplus=14,
+    hyperscore=15
+
+};
+
+
+class PtmPeptideListWindow;
+class PtmPeptideTableModel: public QAbstractTableModel
 {
+    Q_OBJECT
+public:
+    PtmPeptideTableModel(PtmPeptideListWindow * p_ptm_peptide_list_window);
+    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
+    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
+    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
+    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+    static const QString getTitle(PtmPeptideListColumn column);
+    static const QString getDescription(PtmPeptideListColumn column);
+
+    ~PtmPeptideTableModel();
+    
+private :
+    static const QString getTitle(std::int8_t column);
+    static const QString getDescription(std::int8_t column);
+   
+private:
+    std::vector<PtmSampleScanSp> _ptm_sample_scan_list;
 };
 
 #endif // PTMPEPTIDETABLEMODEL_H
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.cpp b/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.cpp
index b6b394c4a..9be577329 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.cpp
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.cpp
@@ -27,3 +27,16 @@
 ******************************************************************************/
 
 #include "ptmpeptidetableproxymodel.h"
+#include "ptmpeptidetablemodel.h"
+#include "ptmpeptidelistwindow.h"
+
+PtmPeptideTableProxyModel::PtmPeptideTableProxyModel(PtmPeptideListWindow * p_ptm_peptide_list_window, PtmPeptideTableModel* ptm_table_model_p)
+{
+  _p_ptm_peptide_list_window = p_ptm_peptide_list_window;
+  _p_ptm_table_model = ptm_table_model_p;
+}
+
+PtmPeptideTableProxyModel::~PtmPeptideTableProxyModel()
+{
+
+}
diff --git a/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.h b/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.h
index 5e0a9f676..c680c57b9 100644
--- a/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.h
+++ b/src/gui/ptm_peptide_list_view/ptmpeptidetableproxymodel.h
@@ -29,8 +29,23 @@
 #ifndef PTMPEPTIDETABLEPROXYMODEL_H
 #define PTMPEPTIDETABLEPROXYMODEL_H
 
-class PtmPeptideTableProxyModel
+#include <QAbstractTableModel>
+#include <QSortFilterProxyModel>
+
+
+class PtmPeptideListWindow;
+class PtmPeptideTableModel;
+
+class PtmPeptideTableProxyModel: public QSortFilterProxyModel
 {
+    Q_OBJECT
+public:
+    PtmPeptideTableProxyModel(PtmPeptideListWindow * p_ptm_peptide_list_window, PtmPeptideTableModel* ptm_table_model_p);
+    ~PtmPeptideTableProxyModel();
+
+private:
+    PtmPeptideListWindow * p_ptm_peptide_list_window;
+    PtmPeptideTableModel* ptm_table_model_p;
 };
 
 #endif // PTMPEPTIDETABLEPROXYMODEL_H
-- 
GitLab