From fa7a8d57976929ffc06d8b69b20d73ab6351ebc2 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Sun, 3 Apr 2011 18:22:32 +0200
Subject: [PATCH] added AlignmentFromChainView

---
 modules/seq/alg/doc/seqalg.rst            |  6 ++--
 modules/seq/alg/pymod/__init__.py         | 37 ++++++++++++++++++++++-
 modules/seq/base/pymod/export_sequence.cc |  1 +
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/modules/seq/alg/doc/seqalg.rst b/modules/seq/alg/doc/seqalg.rst
index ca6476b66..66cbb0f41 100644
--- a/modules/seq/alg/doc/seqalg.rst
+++ b/modules/seq/alg/doc/seqalg.rst
@@ -1,4 +1,4 @@
-:mod:`mol.alg <ost.seq.alg>` -- Algorithms for Sequences
+:mod:`seq.alg <ost.seq.alg>` -- Algorithms for Sequences
 ================================================================================
 
 .. currentmodule:: ost.seq.alg
@@ -30,4 +30,6 @@
      considered as aligned. There is no information in the pairwise alignment to 
      guide the merging, the result is undefined.
 
-.. autofunction:: AlignToSEQRES
\ No newline at end of file
+.. autofunction:: AlignToSEQRES
+
+.. autofunction:: AlignmentFromChainView
\ No newline at end of file
diff --git a/modules/seq/alg/pymod/__init__.py b/modules/seq/alg/pymod/__init__.py
index 1862fc812..ce80e939e 100644
--- a/modules/seq/alg/pymod/__init__.py
+++ b/modules/seq/alg/pymod/__init__.py
@@ -39,5 +39,40 @@ def AlignToSEQRES(chain, seqres):
   return seq.CreateAlignment(seq.CreateSequence('SEQRES', str(seqres)), 
                              seq.CreateSequence('atoms', aln_seq))
 
-    
+
+def AlignmentFromChainView(chain, handle_seq_name='handle', 
+                           view_seq_name='view'):
+  """
+  Creates and returns the sequence alignment of the given chain view to the 
+  chain handle. The alignment contains two sequences, the first containing all 
+  non-ligand peptide-linking residues, the second containing all non-ligand 
+  peptide-linking residues that are part of the view. 
   
+  :param chain: A valid chain
+  :type chain: :class:`~ost.mol.ChainView`
+  
+  :param handle_seq_name: Name of the handle sequence in the output alignment
+  :param view_seq_name: Name of the view sequence in the output alignment
+  :returns: The alignment
+  :rtype: :class:`~ost.seq.AlignmentHandle`
+  
+  """
+  from ost import seq
+  v0=chain.handle.Select('ligand=false and peptide=true')
+  v1=chain.Select('ligand=false and peptide=true')
+  s0=seq.CreateSequence(handle_seq_name, '')
+  s1=seq.CreateSequence(view_seq_name, '')
+  s0.AttachView(v0)
+  s1.AttachView(v1)
+  res0=v0.residues
+  res1=v1.residues
+  idx0, idx1=(0, 0)
+  while idx0<len(res0):
+    s0.Append(res0[idx0].one_letter_code)
+    if idx1<len(res1) and res1[idx1].handle==res0[idx0].handle:
+      s1.Append(res1[idx1].one_letter_code)
+      idx1+=1
+    else:
+      s1.Append('-')
+    idx0+=1
+  return seq.CreateAlignment(s0, s1)
diff --git a/modules/seq/base/pymod/export_sequence.cc b/modules/seq/base/pymod/export_sequence.cc
index ca673fcba..c72fe3ae8 100644
--- a/modules/seq/base/pymod/export_sequence.cc
+++ b/modules/seq/base/pymod/export_sequence.cc
@@ -256,6 +256,7 @@ void export_sequence()
     .def("SetOffset", &SequenceHandle::SetOffset)
     .def("AttachView", attach_one)
     .def("AttachView", attach_two)
+    .def("Append", &SequenceHandle::Append)
     .def("SetString", &SequenceHandle::SetString)
     .add_property("string",
                   make_function(&SequenceHandle::GetString,
-- 
GitLab