From ad7b23aec528c43fa68cd7976dcfa65e772d6c9c Mon Sep 17 00:00:00 2001
From: Gerardo Tauriello <gerardo.tauriello@unibas.ch>
Date: Fri, 24 Jun 2016 13:56:45 +0200
Subject: [PATCH] fixed Renumber-function which was failing for the given
 example code

---
 modules/seq/alg/doc/seqalg.rst    |  1 +
 modules/seq/alg/pymod/renumber.py | 65 +++++++++++++++++++------------
 2 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/modules/seq/alg/doc/seqalg.rst b/modules/seq/alg/doc/seqalg.rst
index 5b70b99fc..0285d04c7 100644
--- a/modules/seq/alg/doc/seqalg.rst
+++ b/modules/seq/alg/doc/seqalg.rst
@@ -163,6 +163,7 @@ Algorithms for Alignments
   :param gap_ext: The gap extension penalty. Must be a negative number
   :returns: best-scoring alignment of *seq1* and *seq2*.
 
+.. autofunction:: ost.seq.alg.renumber.Renumber
 
 .. _contact-prediction:
 
diff --git a/modules/seq/alg/pymod/renumber.py b/modules/seq/alg/pymod/renumber.py
index 22873111e..899ad1e16 100644
--- a/modules/seq/alg/pymod/renumber.py
+++ b/modules/seq/alg/pymod/renumber.py
@@ -1,6 +1,4 @@
-from ost import io, seq, mol, conop
-from ost import *
-
+from ost import seq, mol
 
 def _RenumberSeq(seq_handle):
   if not seq_handle.HasAttachedView():
@@ -8,14 +6,13 @@ def _RenumberSeq(seq_handle):
   ev = seq_handle.attached_view.CreateEmptyView()
   new_numbers = mol.ResNumList()
   for pos in range(len(seq_handle)):
-    if seq_handle[pos]!='-':
-      r=seq_handle.GetResidue(pos)
+    if seq_handle[pos] != '-':
+      r = seq_handle.GetResidue(pos)
       if r.IsValid():
-          #print seq_handle[pos],r.number.num,pos+1
           ev.AddResidue(r, mol.INCLUDE_ALL)
           new_numbers.append(pos+1)
       else:
-        raise RuntimeError('Error: renumbering failed at position %s' %pos)
+        raise RuntimeError('Error: renumbering failed at position %s' % pos)
   return ev, new_numbers
 
 def _RenumberAln(aln, seq_index):
@@ -25,44 +22,62 @@ def _RenumberAln(aln, seq_index):
   ev = aln.sequences[seq_index].attached_view.CreateEmptyView()
   new_numbers = mol.ResNumList()
   for col in aln:
-    if col[0]!='-' and col[seq_index]!='-':
-      if col[0]!=col[seq_index]:
-        raise RuntimeError("residue mismatch at position %d (%s vs %s) (renumbering failed)"%(counter, col[0],col[1]))
-      rnum=aln.GetSequence(seq_index).GetResidueIndex(counter)
-      r=aln.GetSequence(seq_index).GetResidue(counter)
+    if col[0] != '-' and col[seq_index] != '-':
+      if col[0] != col[seq_index]:
+        raise RuntimeError("residue mismatch at position %d (%s vs %s) "\
+                           "(renumbering failed)" % (counter, col[0],
+                                                     col[seq_index]))
+      rnum = aln.GetSequence(seq_index).GetResidueIndex(counter)
+      r = aln.GetSequence(seq_index).GetResidue(counter)
       if not r.IsValid():
-        raise RuntimeError("invalid residue at postion %s (renumbering failed)" %(counter))
+        raise RuntimeError("invalid residue at postion %s (renumbering failed)"\
+                           % (counter))
       ev.AddResidue(r, mol.INCLUDE_ALL)
       new_numbers.append(counter+1)
-    counter+=1
+    counter += 1
   return ev, new_numbers
 
 
 def Renumber(seq_handle, sequence_number_with_attached_view=1):
   """
-  Function to renumber an entity according to an alignment between the model sequence 
-  and the full-length target sequence. The aligned model sequence or the alignment itself
-  with an attached view needs to be provided. Upon succcess, the renumbered entity is returned.
+  Function to renumber an entity according to an alignment between the model
+  sequence and the full-length target sequence. The aligned model sequence or
+  the alignment itself with an attached view needs to be provided. Upon
+  succcess, the renumbered entity is returned.
+  If an alignment is given, the sequence must 
 
   .. code-block:: python
 
     from ost.seq.alg import renumber
     from ost.bindings.clustalw import *
-    ent=io.LoadPDB("path_to_model")
-    s=io.LoadSequence("path_to_full_length_fasta_seqeunce")
-    pdb_seq=seq.SequenceFromChain("model", ent.chains[0])
-    aln=ClustalW(s,pdb_seq)
-    aln.AttachView(1,ent.chains[0].Select(""))
-    e=Renumber(aln.GetSequence(sequence_number_with_attached_view))
+    ent = io.LoadPDB("path_to_model")
+    s = io.LoadSequence("path_to_full_length_fasta_seqeunce")
+    pdb_seq = seq.SequenceFromChain("model", ent.chains[0])
+    aln = ClustalW(s, pdb_seq)
+    aln.AttachView(1, ent.chains[0].Select(""))
+    e = Renumber(aln.sequences[1])
     io.SavePDB(e, "renum.pdb")
    
+  :param seq_handle: Sequence or alignment handle with attached view.
+  :type seq_handle:  :class:`SequenceHandle` / :class:`AlignmentHandle`
+  :param sequence_number_with_attached_view: Sequence number for the aln. handle
+                                             (not used if seq. handle given)
+  :type sequence_number_with_attached_view:  :class:`int`
+  :raises: :exc:`RuntimeError` if unknown type of *seq_handle* or if attached
+           view is missing or if the given alignment sequence is inconsistent.
   """
-  if isinstance(seq_handle, seq.SequenceHandle):
+  if isinstance(seq_handle, seq.SequenceHandle) \
+     or isinstance(seq_handle, seq.ConstSequenceHandle):
     ev, new_numbers = _RenumberSeq(seq_handle)
   elif isinstance(seq_handle, seq.AlignmentHandle):
     ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view)
+  else:
+    raise RuntimeError("Unknown input type " + str(type(seq_handle)))
 
   ev.AddAllInclusiveBonds()
-  new_ent = mol.CreateEntityFromView(ev,False);
+  new_ent = mol.CreateEntityFromView(ev, False);
   new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
   return new_ent
+
+# choose visible interface
+__all__ = ('Renumber', )
\ No newline at end of file
-- 
GitLab