OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
renumber.py
Go to the documentation of this file.
1 from ost import seq, mol
2 
3 def _RenumberSeq(seq_handle, old_number_label=None):
4  if not seq_handle.HasAttachedView():
5  raise RuntimeError("Sequence Handle has no attached view")
6  ev = seq_handle.attached_view.CreateEmptyView()
7  new_numbers = mol.ResNumList()
8  for pos in range(len(seq_handle)):
9  if seq_handle[pos] != '-':
10  r = seq_handle.GetResidue(pos)
11  if r.IsValid():
12  ev.AddResidue(r, mol.INCLUDE_ALL)
13  new_numbers.append(pos+1)
14  if old_number_label is not None:
15  r.SetIntProp(old_number_label, r.number.GetNum())
16  else:
17  raise RuntimeError('Error: renumbering failed at position %s' % pos)
18  return ev, new_numbers
19 
20 def _RenumberAln(aln, seq_index, old_number_label=None):
21  if not aln.sequences[seq_index].HasAttachedView():
22  raise RuntimeError("Sequence Handle has no attached view")
23  counter=0
24  ev = aln.sequences[seq_index].attached_view.CreateEmptyView()
25  new_numbers = mol.ResNumList()
26  for col in aln:
27  if col[0] != '-' and col[seq_index] != '-':
28  if col[0] != col[seq_index]:
29  raise RuntimeError("residue mismatch at position %d (%s vs %s) "\
30  "(renumbering failed)" % (counter, col[0],
31  col[seq_index]))
32  rnum = aln.GetSequence(seq_index).GetResidueIndex(counter)
33  r = aln.GetSequence(seq_index).GetResidue(counter)
34  if not r.IsValid():
35  raise RuntimeError("invalid residue at postion %s (renumbering failed)"\
36  % (counter))
37  ev.AddResidue(r, mol.INCLUDE_ALL)
38  new_numbers.append(counter+1)
39  if old_number_label is not None:
40  r.SetIntProp(old_number_label, r.number.GetNum())
41  counter += 1
42  return ev, new_numbers
43 
44 
45 def Renumber(seq_handle, sequence_number_with_attached_view=1, old_number_label=None):
46  """
47  Function to renumber an entity according to an alignment between the model
48  sequence and the full-length target sequence. The aligned model sequence or
49  the alignment itself with an attached view needs to be provided. Upon
50  succcess, the renumbered entity is returned.
51  If an alignment is given, the first sequence of the alignment is considered
52  the full-length sequence and it must match the model sequence wherever it is
53  aligned (i.e. excluding gaps).
54 
55  .. code-block:: python
56 
57  from ost.seq.alg import renumber
58  from ost.bindings.clustalw import *
59  ent = io.LoadPDB("path_to_model")
60  s = io.LoadSequence("path_to_full_length_fasta_seqeunce")
61  pdb_seq = seq.SequenceFromChain("model", ent.chains[0])
62  aln = ClustalW(s, pdb_seq)
63  aln.AttachView(1, ent.chains[0].Select(""))
64  e = Renumber(aln.sequences[1])
65  io.SavePDB(e, "renum.pdb")
66 
67  :param seq_handle: Sequence or alignment handle with attached view.
68  :type seq_handle: :class:`SequenceHandle` / :class:`AlignmentHandle`
69  :param sequence_number_with_attached_view: Sequence number for the aln. handle
70  (not used if seq. handle given)
71  :type sequence_number_with_attached_view: :class:`int`
72  :raises: :exc:`RuntimeError` if unknown type of *seq_handle* or if attached
73  view is missing or if the given alignment sequence is inconsistent.
74  """
75  if isinstance(seq_handle, seq.SequenceHandle) \
76  or isinstance(seq_handle, seq.ConstSequenceHandle):
77  ev, new_numbers = _RenumberSeq(seq_handle, old_number_label)
78  elif isinstance(seq_handle, seq.AlignmentHandle):
79  ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view, old_number_label)
80  else:
81  raise RuntimeError("Unknown input type " + str(type(seq_handle)))
82 
83  ev.AddAllInclusiveBonds()
84  new_ent = mol.CreateEntityFromView(ev, False);
85  new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
86  return new_ent
87 
88 # choose visible interface
89 __all__ = ('Renumber', )
mutable sequence handle.
representation of a multiple sequence alignemnt consisting of two or more sequences ...