From ef98debe3ac1303fc2269ed623907cca4edb4724 Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Wed, 2 Jun 2010 11:51:52 +0000
Subject: [PATCH] New SequenceViewer, coloring by conservation

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2310 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/gui/src/CMakeLists.txt                |   3 +
 .../gui/src/sequence/alignment_view_object.cc | 108 +++++++++++++++++-
 .../gui/src/sequence/alignment_view_object.hh |   8 ++
 modules/gui/src/sequence/base_view_object.hh  |   8 +-
 .../gui/src/sequence/conservation_painter.cc  |  45 ++++++++
 .../gui/src/sequence/conservation_painter.hh  |  44 +++++++
 modules/gui/src/sequence/seq_text_painter.cc  |   2 +-
 .../gui/src/sequence/sequence_view_object.cc  |   5 +-
 8 files changed, 215 insertions(+), 8 deletions(-)
 create mode 100644 modules/gui/src/sequence/conservation_painter.cc
 create mode 100644 modules/gui/src/sequence/conservation_painter.hh

diff --git a/modules/gui/src/CMakeLists.txt b/modules/gui/src/CMakeLists.txt
index decd467a8..f8aeaaf00 100644
--- a/modules/gui/src/CMakeLists.txt
+++ b/modules/gui/src/CMakeLists.txt
@@ -24,6 +24,7 @@ set(OST_GUI_SEQUENCE_VIEW_HEADERS
 align_properties_painter.hh
 base_row.hh
 background_painter.hh
+conservation_painter.hh
 painter.hh
 secstr_row.hh
 seq_secstr_painter.hh
@@ -218,6 +219,7 @@ sequence_viewer/sequence_search_bar.cc
 sequence/align_properties_painter.cc
 sequence/base_row.cc
 sequence/background_painter.cc
+sequence/conservation_painter.cc
 sequence/secstr_row.cc
 sequence/seq_secstr_painter.cc
 sequence/seq_selection_painter.cc
@@ -352,6 +354,7 @@ sequence_viewer/sequence_search_bar.hh
 sequence/align_properties_painter.hh
 sequence/background_painter.hh
 sequence/base_row.hh
+sequence/conservation_painter.hh
 sequence/painter.hh
 sequence/secstr_row.hh
 sequence/seq_secstr_painter.hh
diff --git a/modules/gui/src/sequence/alignment_view_object.cc b/modules/gui/src/sequence/alignment_view_object.cc
index bee250f9f..40003412f 100644
--- a/modules/gui/src/sequence/alignment_view_object.cc
+++ b/modules/gui/src/sequence/alignment_view_object.cc
@@ -27,6 +27,8 @@
 #include <ost/mol/mol.hh>
 #include <ost/mol/view_op.hh>
 
+#include <ost/seq/aligned_region.hh>
+
 #include "sequence_row.hh"
 #include "secstr_row.hh"
 
@@ -40,12 +42,114 @@
 
 namespace ost { namespace gui {
 
-AlignmentViewObject::AlignmentViewObject(const seq::AlignmentHandle& alignment, QObject* parent): SequenceViewObject(parent)
+namespace {
+QMap<QString,int> GetGroupMap(){
+  QMap<QString,int> map;
+  map["G"]=1;
+  map["A"]=1;
+  map["V"]=1;
+  map["L"]=1;
+  map["I"]=1;
+  map["F"]=2;
+  map["Y"]=2;
+  map["W"]=2;
+  map["C"]=3;
+  map["M"]=3;
+  map["S"]=4;
+  map["T"]=4;
+  map["K"]=5;
+  map["R"]=5;
+  map["H"]=5;
+  map["D"]=6;
+  map["E"]=6;
+  map["N"]=6;
+  map["Q"]=6;
+  map["P"]=7;
+  return map;
+}
+
+QColor GetColor(int cons){
+  int color = 255 - int((float(cons) / 100) * 255);
+  return QColor(color,color,color);
+}
+
+QColor GetForeGroundColor(QColor background){
+  if(background == Qt::transparent){
+    return Qt::black;
+  }
+  int gray = 255 - background.red();
+  return QColor(gray,gray,gray);
+}
+
+}
+
+
+QMap<QString,int> AlignmentViewObject::group_map_ = GetGroupMap();
+
+AlignmentViewObject::AlignmentViewObject(const seq::AlignmentHandle& alignment, QObject* parent): SequenceViewObject(parent), alignment_(alignment)
 {
-  for(int i=0; i<alignment.GetCount();i++){
+  for(int i=0; i<alignment.GetCount(); i++){
     seq::SequenceHandle seq_handle = alignment.GetSequence(i).Copy();
     this->AddSequence(seq_handle, seq_handle.GetName().c_str());
   }
+
+  for(int j=0; j<alignment.GetLength(); j++){
+    int group = 0;
+    QString element = "";
+    for(int i=0; i<alignment.GetCount();i++){
+      QString code = QString(alignment.GetOneLetterCode(i,j));
+      if(element.size()==0){
+        element = code;
+      }
+      else if (element != code){
+        element = "  ";
+      }
+      if(group_map_.contains(code) && group>=0){
+        if(group == 0) {
+            group = group_map_[code];
+        }
+        else if(group_map_[code] != group){
+          group = -1;
+        }
+      }
+      else{
+        group = -1;
+      }
+
+    }
+    if(element.size()==1){
+      conservation_[j] = GetColor(100);
+    }
+    else if(group > 0){
+      conservation_[j] = GetColor(70);
+    }
+    else{
+      conservation_[j] = Qt::transparent;
+    }
+  }
+}
+
+
+QVariant AlignmentViewObject::GetData(int row, int column, int role)
+{
+  if(column > 0 && column <= alignment_.GetLength()){
+    if(role == Qt::UserRole+3 ){
+      if(column -1 < conservation_.size()){
+        return QVariant(conservation_[column-1]);
+      }
+        return QVariant(Qt::transparent);
+    }
+
+    if(role == Qt::ForegroundRole){
+      if(column -1 < conservation_.size()){
+        return QVariant(GetForeGroundColor(conservation_[column-1]));
+      }
+        return QVariant(Qt::transparent);
+    }
+
+  }
+
+  return BaseViewObject::GetData(row,column,role);
 }
 
 }}
diff --git a/modules/gui/src/sequence/alignment_view_object.hh b/modules/gui/src/sequence/alignment_view_object.hh
index a75159c22..71a9dc322 100644
--- a/modules/gui/src/sequence/alignment_view_object.hh
+++ b/modules/gui/src/sequence/alignment_view_object.hh
@@ -35,6 +35,14 @@ class AlignmentViewObject : public SequenceViewObject
 
 public:
   AlignmentViewObject(const seq::AlignmentHandle& alignment, QObject* parent = 0);
+
+  QVariant GetData(int row, int column, int role);
+
+private:
+  seq::AlignmentHandle alignment_;
+  QMap<int, QColor> conservation_;
+
+  static QMap<QString,int> group_map_;
 };
 
 
diff --git a/modules/gui/src/sequence/base_view_object.hh b/modules/gui/src/sequence/base_view_object.hh
index b75124462..6f227b9c9 100644
--- a/modules/gui/src/sequence/base_view_object.hh
+++ b/modules/gui/src/sequence/base_view_object.hh
@@ -45,11 +45,11 @@ public:
   int GetRowCount();
   int GetMaxColumnCount() const;
 
-  void SetSelection(int row, const QSet<int>& added, const QSet<int>& removed);
+  virtual void SetSelection(int row, const QSet<int>& added, const QSet<int>& removed);
 
-  QVariant GetData(int row, int column, int role);
-  bool SetData(int row, int column, const QVariant& value, int role);
-  Qt::ItemFlags Flags(int row, int column) const;
+  virtual QVariant GetData(int row, int column, int role);
+  virtual bool SetData(int row, int column, const QVariant& value, int role);
+  virtual Qt::ItemFlags Flags(int row, int column) const;
 
   void DoubleClicked(int row, int column);
   void ZoomIn();
diff --git a/modules/gui/src/sequence/conservation_painter.cc b/modules/gui/src/sequence/conservation_painter.cc
new file mode 100644
index 000000000..7bab56edc
--- /dev/null
+++ b/modules/gui/src/sequence/conservation_painter.cc
@@ -0,0 +1,45 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2010 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library 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 Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+
+/*
+  Author: Stefan Scheuber
+ */
+
+
+#include <QtGui>
+
+#include "conservation_painter.hh"
+
+namespace ost { namespace gui {
+
+
+ConservationPainter::ConservationPainter(QObject* parent)
+    : Painter(parent)
+{}
+
+void ConservationPainter::Paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index){
+  painter->save();
+  if (index.column()>0){
+    QColor cons = index.data(Qt::UserRole+3).value<QColor>();
+    painter->fillRect(option.rect, cons);
+  }
+  painter->restore();
+}
+
+}}
diff --git a/modules/gui/src/sequence/conservation_painter.hh b/modules/gui/src/sequence/conservation_painter.hh
new file mode 100644
index 000000000..c6549f661
--- /dev/null
+++ b/modules/gui/src/sequence/conservation_painter.hh
@@ -0,0 +1,44 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2010 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library 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 Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+#ifndef OST_SEQUENCE_VIEWER_CONSERVATION_PAINTER
+#define OST_SEQUENCE_VIEWER_CONSERVATION_PAINTER
+
+/*
+  Author: Stefan Scheuber
+ */
+
+#include <QObject>
+
+#include "painter.hh"
+
+namespace ost { namespace gui {
+
+
+
+class ConservationPainter : public Painter
+{
+  Q_OBJECT
+public:
+  ConservationPainter(QObject* parent = 0);
+  void Paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index);
+};
+
+}}
+
+#endif
diff --git a/modules/gui/src/sequence/seq_text_painter.cc b/modules/gui/src/sequence/seq_text_painter.cc
index 30c1e1623..50334bf39 100644
--- a/modules/gui/src/sequence/seq_text_painter.cc
+++ b/modules/gui/src/sequence/seq_text_painter.cc
@@ -34,7 +34,7 @@ SeqTextPainter::SeqTextPainter(QObject* parent)
 
 void SeqTextPainter::Paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index){
   painter->save();
-  painter->setPen(QPen(Qt::black));
+  painter->setPen(index.data(Qt::ForegroundRole).value<QColor>());
   QVariant value = index.data(Qt::DisplayRole);
   if (value.isValid()){
     QString text = value.toString();
diff --git a/modules/gui/src/sequence/sequence_view_object.cc b/modules/gui/src/sequence/sequence_view_object.cc
index daf0bbf65..0ded72932 100644
--- a/modules/gui/src/sequence/sequence_view_object.cc
+++ b/modules/gui/src/sequence/sequence_view_object.cc
@@ -31,6 +31,7 @@
 #include "secstr_row.hh"
 
 #include "align_properties_painter.hh"
+#include "conservation_painter.hh"
 #include "painter.hh"
 #include "background_painter.hh"
 #include "seq_secstr_painter.hh"
@@ -85,10 +86,12 @@ void SequenceViewObject::AddSequence(seq::SequenceHandle& sequence, const QStrin
   new_row->InsertPainter(p);
   p = new AlignPropertiesPainter(this);
   new_row->InsertPainter(p);
-  p = new SeqSelectionPainter(this);
+  p = new ConservationPainter(this);
   new_row->InsertPainter(p);
   p = new SeqTextPainter(this);
   new_row->InsertPainter(p);
+  p = new SeqSelectionPainter(this);
+  new_row->InsertPainter(p);
   rows_.append(new_row);
 }
 
-- 
GitLab