From f92024c0fce83d3aa2b48a248accd246596ea2fc Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Thu, 15 Jul 2010 14:07:23 +0000
Subject: [PATCH] Align in SceneWin show alignment

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2566 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/bindings/pymod/tmtools.py      | 15 ++++++--
 modules/gui/pymod/export_main_area.cc  | 18 ++++++++-
 modules/gui/pymod/init_context_menu.py | 53 ++++++++++++++++++++------
 3 files changed, 70 insertions(+), 16 deletions(-)

diff --git a/modules/bindings/pymod/tmtools.py b/modules/bindings/pymod/tmtools.py
index 9117b675d..955897190 100644
--- a/modules/bindings/pymod/tmtools.py
+++ b/modules/bindings/pymod/tmtools.py
@@ -28,8 +28,8 @@ tmalign: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
 Authors: Pascal Benkert, Marco Biasini
 """
 
-import subprocess, os, tempfile, platform
-from ost import settings, io, geom
+import subprocess, os, tempfile
+from ost import settings, io, geom, seq
 
 def _SetupFiles(models):
   # create temporary directory
@@ -43,11 +43,13 @@ def _CleanupFiles(dir_name):
   shutil.rmtree(dir_name)
 
 class TMAlignResult:
-  def __init__(self, rmsd, tm_score, aligned_length, transform):
+  def __init__(self, rmsd, tm_score, aligned_length, transform, ref_sequence, alignment):
     self.rmsd=rmsd
     self.tm_score=tm_score    
     self.aligned_length=aligned_length
     self.transform=transform
+    self.ref_sequence =ref_sequence
+    self.alignment=alignment
 
 def _ParseTmAlign(lines):
   info_line=lines[11].split(',')
@@ -61,7 +63,12 @@ def _ParseTmAlign(lines):
                 tf2[4], tf3[2], tf3[3], tf3[4])
   tf=geom.Mat4(rot)
   tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1]))
-  return TMAlignResult(rmsd, aln_length, tm_score, tf)
+  seq1 = seq.CreateSequence("1",lines[20].strip())
+  seq2 = seq.CreateSequence("2",lines[22].strip())
+  alignment = seq.CreateAlignment()
+  alignment.AddSequence(seq1)
+  alignment.AddSequence(seq2)
+  return TMAlignResult(rmsd, aln_length, tm_score, tf, seq1, alignment)
 
 def _RunTmAlign(tmalign, tmp_dir):
   model1_filename=os.path.join(tmp_dir, 'model01.pdb')
diff --git a/modules/gui/pymod/export_main_area.cc b/modules/gui/pymod/export_main_area.cc
index 5a9dfed58..4e6b793b5 100644
--- a/modules/gui/pymod/export_main_area.cc
+++ b/modules/gui/pymod/export_main_area.cc
@@ -142,6 +142,20 @@ void main_area_hide_sub_window(MainArea* m, const SipHandlerBase& sh)
 
 }
 
+void main_area_show_sub_window_b(MainArea* m, object py_object)
+{
+  if(QWidget* widget = get_cpp_qobject<QWidget>(py_object)){
+    m->ShowSubWindow(widget);
+  }
+}
+
+void main_area_hide_sub_window_b(MainArea* m, object py_object)
+{
+  if(QWidget* widget = get_cpp_qobject<QWidget>(py_object)){
+    m->HideSubWindow(widget);
+  }
+}
+
 void export_MainArea()
 {  
   class_<MainArea, boost::noncopyable>("MainArea", no_init)
@@ -161,8 +175,10 @@ void export_MainArea()
     .def("width", &MainArea::width)
     .def("height", &MainArea::height)
     .def("ShowSubWindow", &MainArea::ShowSubWindow)
-    .def("ShowSubWindow", &main_area_show_sub_window)        
+    .def("ShowSubWindow", &main_area_show_sub_window)
+    .def("ShowSubWindow", &main_area_show_sub_window_b)
     .def("HideSubWindow", &MainArea::HideSubWindow)
+    .def("HideSubWindow", &main_area_hide_sub_window_b)
     .def("HideSubWindow", &main_area_hide_sub_window)            
     .def("EnableTabbedMode", &MainArea::EnableTabbedMode, arg("flag")=true)
     .def("GetQObject",&get_py_qobject<MainArea>)
diff --git a/modules/gui/pymod/init_context_menu.py b/modules/gui/pymod/init_context_menu.py
index 7cd032ca2..6bb6db853 100644
--- a/modules/gui/pymod/init_context_menu.py
+++ b/modules/gui/pymod/init_context_menu.py
@@ -2,9 +2,12 @@ import platform
 
 from PyQt4 import QtCore, QtGui
 
-from ost import geom, gfx, gui
+import sip
+
+from ost import geom, gfx, gui, seq
 from ost import settings
 from ost.bindings import tmtools
+from ost.seq import alg
 
 class SelectRefDialog(QtGui.QDialog):
   def __init__(self, ent_list, parent=None):
@@ -23,6 +26,14 @@ class SelectRefDialog(QtGui.QDialog):
     self.list.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
     vb.addWidget(self.label)
     vb.addWidget(self.list)
+    self.show_scores = QtGui.QCheckBox(self)
+    self.show_scores.setText("Show Scores")
+    self.show_scores.setChecked(True)
+    vb.addWidget(self.show_scores)
+    self.show_alignment = QtGui.QCheckBox(self)
+    self.show_alignment.setText("Display Alignment")
+    self.show_alignment.setChecked(False)
+    vb.addWidget(self.show_alignment)
     hb = QtGui.QHBoxLayout()
     hb.setDirection(QtGui.QBoxLayout.LeftToRight)
     cancel_btn = QtGui.QPushButton("Cancel", self)
@@ -58,7 +69,13 @@ class SelectRefDialog(QtGui.QDialog):
         self.ent_list_.remove(ent)
         self.ent_list_.insert(0,ent)
     self.accept()
-      
+     
+  def GetShowScores(self):
+    return self.show_scores.isChecked()
+  
+  def GetDisplayAlignment(self):
+    return self.show_alignment.isChecked()
+  
   def GetEntities(self):
     return self.ent_list_
 
@@ -124,14 +141,11 @@ class AlignmentContextMenu(QtCore.QObject):
     ent_list = list()
     for i in range(0,scene_selection.GetActiveNodeCount()):
       ent_list.append(scene_selection.GetActiveNode(i))
-    if len(ent_list) == 2:
-      self.__Align(ent_list)
-    elif len(ent_list) > 2:
-      sd = SelectRefDialog(ent_list)
-      if(sd.exec_()):
-        self.__Align(sd.GetEntities())
+    sd = SelectRefDialog(ent_list)
+    if(sd.exec_()):
+      self.__Align(sd.GetEntities(),sd.GetShowScores(), sd.GetDisplayAlignment())
         
-  def __Align(self, ent_list):
+  def __Align(self, ent_list,show_scores=True, display_alignment=False):
     node = ent_list[0]
     res_list = list()
     if isinstance(node, gfx.Entity):
@@ -141,7 +155,10 @@ class AlignmentContextMenu(QtCore.QObject):
         if isinstance(node, gfx.Entity):
           res_list.append(tmtools.TMAlign(node.view.handle, ref))
           node.UpdatePositions()
-    self.__ShowScore(ent_list, res_list)
+    if show_scores:
+      self.__ShowScore(ent_list, res_list)
+    if display_alignment:
+      self.__DisplayAlignment(res_list)
     
   def __ShowScore(self, ent_list, res_list):
     if(len(res_list)==1):
@@ -151,7 +168,21 @@ class AlignmentContextMenu(QtCore.QObject):
     elif(len(res_list)>1):
       ShowResultDialog(ent_list, res_list).exec_()
       
-    
+  def __DisplayAlignment(self, res_list):
+    if(len(res_list)>0):
+      ref_seq = res_list[0].ref_sequence
+      aln_list = seq.AlignmentList()
+      if(ref_seq.IsValid()):
+        for res in res_list:
+          aln_list.append(res.alignment)
+        alignment = alg.MergePairwiseAlignments(aln_list, ref_seq)
+        gosty = gui.GostyApp.Instance()
+        main_area = gosty.perspective.GetMainArea()
+        seq_viewer = gui.SequenceViewerV2(True)
+        gosty.AddWidgetToApp(str(ref_seq),seq_viewer)
+        seq_viewer.AddAlignment(alignment)
+        main_area.ShowSubWindow(seq_viewer)
+  
 def _InitContextMenu(app):
   cm=app.scene_win.GetContextMenu()
   AlignmentContextMenu(cm)
\ No newline at end of file
-- 
GitLab