From 2c3478c87e3fdff065f23f45ad5c0540c6fd793b Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Fri, 9 Jun 2017 23:26:23 +0200
Subject: [PATCH] dbxref button works

---
 src/core/proteinxtp.cpp                     | 80 +++++++++++++++++----
 src/core/proteinxtp.h                       |  7 +-
 src/gui/protein_view/protein_detail_view.ui | 46 ++++++------
 src/gui/protein_view/proteinwindow.cpp      | 44 +++++++++---
 src/gui/protein_view/proteinwindow.h        | 15 +++-
 src/utils/types.h                           | 11 +--
 6 files changed, 150 insertions(+), 53 deletions(-)

diff --git a/src/core/proteinxtp.cpp b/src/core/proteinxtp.cpp
index 4dfa3f37a..2f24e43a1 100644
--- a/src/core/proteinxtp.cpp
+++ b/src/core/proteinxtp.cpp
@@ -25,6 +25,44 @@
 #include <pappsomspp/protein/enzyme.h>
 #include <pappsomspp/pappsoexception.h>
 
+DbXref::DbXref() {
+}
+DbXref::DbXref(ExternalDatabase database_in, const QString & accession_in):database(database_in), accession(accession_in) {
+}
+
+DbXref::DbXref(const DbXref & other):database(other.database), accession(other.accession) {
+}
+const QUrl DbXref::getUrl() const {
+    QUrl url;
+    qDebug() << "DbXref::getUrl " << accession << " "<< (std::uint8_t)database;
+    switch (database) {
+
+    case ExternalDatabase::AGI_LocusCode :
+        url.setUrl(QString("http://www.arabidopsis.org/servlets/TairObject?type=locus&name=%1").arg(accession));
+        break;
+    case ExternalDatabase::NCBI_gi :
+        url.setUrl(QString("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=protein&dopt=GenBank&list_uids=%1").arg(accession));
+        break;
+
+    case ExternalDatabase::SwissProt :
+        url.setUrl(QString("http://www.uniprot.org/uniprot/%1").arg(accession));
+        break;
+    case ExternalDatabase::TrEMBL :
+        url.setUrl(QString("http://www.uniprot.org/uniprot/%1").arg(accession));
+        break;
+    case ExternalDatabase::ref :
+        break;
+
+    case ExternalDatabase::OboPsiMod :
+        QString iri(QString("http://purl.obolibrary.org/obo/%1").arg(QString(accession).replace(":","_")));
+        url.setUrl(QString("http://www.ebi.ac.uk/ols/ontologies/mod/terms?iri=%1").arg(iri));
+        break;
+
+    }
+    qDebug() << "DbXref::getUrl end " << url;
+    return url;
+}
+
 ProteinXtp::ProteinXtp():pappso::Protein()
 {
 
@@ -132,41 +170,59 @@ void ProteinXtp::parseAccession2dbxref() {
     QRegExp swiss_prot("^P[A-Z0-9]{5}$");
     QRegExp trembl("^[QOA][A-Z0-9]{5}$");
     QRegExp ref ("^[NZ]P\\_[0-9]{5,8}$");
+    QString accession;
 
-    for (QString & accession :access_list) {
+    for (unsigned int i=0; i < access_list.size(); i++) {
+        accession = access_list.at(i);
         qDebug() << "ProteinXtp::parseAccession2dbxref accession " << accession;
         if (atg.indexIn(accession, 0) != -1) {
             QStringList temp = accession.split("\\.");
             _dbxref_list.push_back(DbXref(ExternalDatabase::AGI_LocusCode, temp.at(0)));
+            continue;
         }
         if (ncbi_gi.indexIn(accession, 0) != -1) {
             _dbxref_list.push_back(DbXref(ExternalDatabase::NCBI_gi, accession));
+            continue;
         }
 
         if (swiss_prot.indexIn(accession, 0) != -1) {
             qDebug() << "ProteinXtp::parseAccession2dbxref accession SwissProt " << accession;
             _dbxref_list.push_back(DbXref(ExternalDatabase::SwissProt, accession));
+            continue;
         }
 
         if (trembl.indexIn(accession, 0) != -1) {
             qDebug() << "ProteinXtp::parseAccession2dbxref accession TrEMBL " << accession;
             _dbxref_list.push_back(DbXref(ExternalDatabase::TrEMBL, accession));
+            continue;
         }
         if (ref.indexIn(accession, 0) != -1) {
             _dbxref_list.push_back(DbXref(ExternalDatabase::ref, accession));
+            continue;
+        }
+        if (i > 0) {
+            if ((access_list.at(i-1) == "sp")&&(accession.size() == 6)) {
+                _dbxref_list.push_back(DbXref(ExternalDatabase::SwissProt, accession));
+            }
+
+            if ((access_list.at(i-1) == "tr")&&(accession.size() == 6)) {
+                _dbxref_list.push_back(DbXref(ExternalDatabase::TrEMBL, accession));
+            }
+
+        }
+
+        if (_dbxref_list.size() > 1) {
+            _dbxref_list.sort([] (const DbXref & first, const DbXref & second)
+            {
+                return std::tie(first.database, first.accession) < std::tie(second.database, second.accession);
+            });
+            _dbxref_list.unique([] (const DbXref & first, const DbXref & second)
+            {
+                return ( first.database == second.database ) && (first.accession == second.accession);
+            });
         }
     }
-    
-    if (_dbxref_list.size() > 1) {
-        _dbxref_list.sort([] (const DbXref & first, const DbXref & second)
-        {
-            return std::tie(first.database, first.accession) < std::tie(second.database, second.accession);
-        });
-        _dbxref_list.unique([] (const DbXref & first, const DbXref & second)
-        {
-            return ( first.database == second.database ) && (first.accession == second.accession);
-        });
-    }
+
 
     qDebug() << "ProteinXtp::parseAccession2dbxref end" ;
 
diff --git a/src/core/proteinxtp.h b/src/core/proteinxtp.h
index 2bde8ff7b..0e8b916b9 100644
--- a/src/core/proteinxtp.h
+++ b/src/core/proteinxtp.h
@@ -24,6 +24,7 @@
 #include <pappsomspp/protein/protein.h>
 #include <pappsomspp/types.h>
 #include <list>
+#include <QUrl>
 #include "sequencedatabase.h"
 #include "../utils/types.h"
 
@@ -31,8 +32,10 @@
 #define PROTEIN_XTP_H
 
 struct DbXref {
-    DbXref(ExternalDatabase database_in, const QString & accession_in):database(database), accession(accession_in) {
-    };
+    DbXref();
+    DbXref(const DbXref & other);
+    DbXref(ExternalDatabase database_in, const QString & accession_in);
+    const QUrl getUrl() const;
     ExternalDatabase database;
     QString accession;
 };
diff --git a/src/gui/protein_view/protein_detail_view.ui b/src/gui/protein_view/protein_detail_view.ui
index e695d9f50..9249c676b 100644
--- a/src/gui/protein_view/protein_detail_view.ui
+++ b/src/gui/protein_view/protein_detail_view.ui
@@ -40,7 +40,7 @@
           <item>
            <widget class="QLabel" name="accession_label">
             <property name="text">
-             <string>TextLabel</string>
+             <string>TextLa&amp;bel</string>
             </property>
             <property name="wordWrap">
              <bool>true</bool>
@@ -48,24 +48,26 @@
            </widget>
           </item>
           <item>
-           <layout class="QHBoxLayout" name="button_space_layout">
-            <item>
-             <layout class="QHBoxLayout" name="dbxref_list_layout"/>
-            </item>
-            <item>
-             <spacer name="horizontalSpacer_2">
-              <property name="orientation">
-               <enum>Qt::Horizontal</enum>
-              </property>
-              <property name="sizeHint" stdset="0">
-               <size>
-                <width>40</width>
-                <height>20</height>
-               </size>
-              </property>
-             </spacer>
-            </item>
-           </layout>
+           <widget class="QWidget" name="button_layout" native="true">
+            <layout class="QHBoxLayout" name="button_space_layout">
+             <item>
+              <layout class="QHBoxLayout" name="dbxref_list_layout"/>
+             </item>
+             <item>
+              <spacer name="button_spacer">
+               <property name="orientation">
+                <enum>Qt::Horizontal</enum>
+               </property>
+               <property name="sizeHint" stdset="0">
+                <size>
+                 <width>40</width>
+                 <height>20</height>
+                </size>
+               </property>
+              </spacer>
+             </item>
+            </layout>
+           </widget>
           </item>
           <item>
            <widget class="QLabel" name="description_label">
@@ -126,14 +128,14 @@
         <item row="0" column="0">
          <widget class="QLabel" name="label">
           <property name="text">
-           <string>coverage</string>
+           <string>cove&amp;rage</string>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QLabel" name="coverage_label">
           <property name="text">
-           <string>0%</string>
+           <string>&amp;0%</string>
           </property>
          </widget>
         </item>
@@ -147,7 +149,7 @@
         <item row="1" column="1">
          <widget class="QLabel" name="mw_label">
           <property name="text">
-           <string>1Da</string>
+           <string>&amp;1Da</string>
           </property>
          </widget>
         </item>
diff --git a/src/gui/protein_view/proteinwindow.cpp b/src/gui/protein_view/proteinwindow.cpp
index 1bf378cca..f66a3898a 100644
--- a/src/gui/protein_view/proteinwindow.cpp
+++ b/src/gui/protein_view/proteinwindow.cpp
@@ -26,8 +26,30 @@
 #include "ui_protein_detail_view.h"
 #include <pappsomspp/pappsoexception.h>
 #include <QMessageBox>
+#include <QDesktopServices>
 #include <cmath>
-#include <QPushButton>
+
+DbXrefButton::DbXrefButton(QWidget * parent, DbXref dbxref): QPushButton(parent) {
+    _dbxref = dbxref;
+#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
+    QObject::connect(this, SIGNAL(clicked()),this, SLOT(clickedSlot()));
+#endif
+    
+    setText(_dbxref.accession);
+}
+
+void DbXrefButton::clickedSlot() {
+    qDebug() << "DbXrefButton::clickedSlot " << _dbxref.getUrl();
+    QDesktopServices::openUrl (_dbxref.getUrl()) ;
+}
 
 ProteinWindow::ProteinWindow(ProjectWindow *parent):
     QMainWindow(parent),
@@ -66,17 +88,18 @@ void ProteinWindow::doIdentificationGroupGrouped(IdentificationGroup * p_identif
 
 void ProteinWindow::doPeptideMatchSelected(PeptideMatch * peptide_match) {
     if (_p_protein_match != nullptr) {
-            ui->sequenceTextEdit->setText(_p_protein_match->getHtmlSequence(peptide_match));
+        ui->sequenceTextEdit->setText(_p_protein_match->getHtmlSequence(peptide_match));
 
     }
 }
 void ProteinWindow::clearDbXrefLayout() {
     for (int i = 0; i < ui->dbxref_list_layout->count(); ++i)
-{
-   delete ui->dbxref_list_layout->itemAt(i)->widget();
+    {
+        delete ui->dbxref_list_layout->itemAt(i)->widget();
+    }
 }
+void ProteinWindow::browseUrl(int i) {
 }
-
 void ProteinWindow::updateDisplay() {
     try {
         clearDbXrefLayout();
@@ -89,15 +112,14 @@ void ProteinWindow::updateDisplay() {
             QString accession = ui->accession_label->text().replace(dbxref.accession, QString("<span style=\"color:%2;\">%1</span>").arg(dbxref.accession).arg("blue"));
             ui->accession_label->setText(accession);
             qDebug() << "ProteinWindow::updateDisplay " << accession;
-            
-            QPushButton * dbxref_button = new QPushButton(this);
-            dbxref_button->setText(dbxref.accession);
+
+            DbXrefButton * dbxref_button = new DbXrefButton(this,dbxref);
             ui->dbxref_list_layout->addWidget(dbxref_button);
         }
-        
-            ui->button_space_layout->setEnabled(true);
+
+        ui->button_layout->setVisible(true);
         if (ui->dbxref_list_layout->count() == 0) {
-            ui->button_space_layout->setEnabled(false);
+            ui->button_layout->setVisible(false);
         }
         ui->description_label->setText(_p_protein_match->getProteinXtpSp().get()->getDescription());
         ui->sequenceTextEdit->setText(_p_protein_match->getHtmlSequence());
diff --git a/src/gui/protein_view/proteinwindow.h b/src/gui/protein_view/proteinwindow.h
index ff6d92e5c..f5bbe42dd 100644
--- a/src/gui/protein_view/proteinwindow.h
+++ b/src/gui/protein_view/proteinwindow.h
@@ -26,8 +26,19 @@
 
 #include <QMainWindow>
 #include <QTextDocument>
+#include <QPushButton>
 #include "../../core/proteinmatch.h"
 
+class DbXrefButton: public QPushButton {
+    Q_OBJECT
+public :
+    DbXrefButton(QWidget * parent, DbXref dbxref);
+protected slots:
+    void clickedSlot();
+private:
+    DbXref _dbxref;
+};
+
 class ProjectWindow;
 
 namespace Ui {
@@ -46,7 +57,9 @@ public:
 public slots:
     void doIdentificationGroupGrouped(IdentificationGroup * p_identification_group);
     void doPeptideMatchSelected(PeptideMatch * peptide_match);
-    
+ protected slots:
+    void browseUrl(int i);
+   
 protected :
     void updateDisplay();
     void clearDbXrefLayout();
diff --git a/src/utils/types.h b/src/utils/types.h
index e1a894909..222651868 100644
--- a/src/utils/types.h
+++ b/src/utils/types.h
@@ -32,11 +32,12 @@
  *
  */
 enum class ExternalDatabase: std::int8_t {
-    SwissProt =1, ///< Swiss-Prot
-    TrEMBL=2, ///< TrEMBL
-    AGI_LocusCode=3, ///< AGI_LocusCode
-    NCBI_gi=4, ///< NCBI_gi
-    ref=5 ///< ref
+    OboPsiMod =1, ///< OBO PSI MOD
+    SwissProt =2, ///< Swiss-Prot
+    TrEMBL=3, ///< TrEMBL
+    AGI_LocusCode=4, ///< AGI_LocusCode
+    NCBI_gi=5, ///< NCBI_gi
+    ref=6 ///< ref
 };
 
 /** \def IdentificationEngine identification engine
-- 
GitLab