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 io, seq, mol, conop
2 from ost import *
3 
4 
5 def _RenumberSeq(seq_handle):
6  if not seq_handle.HasAttachedView():
7  raise RuntimeError("Sequence Handle has no attached view")
8  ev = seq_handle.attached_view.CreateEmptyView()
9  new_numbers = mol.ResNumList()
10  for pos in range(len(seq_handle)):
11  if seq_handle[pos]!='-':
12  r=seq_handle.GetResidue(pos)
13  if r.IsValid():
14  #print seq_handle[pos],r.number.num,pos+1
15  ev.AddResidue(r, mol.INCLUDE_ALL)
16  new_numbers.append(pos+1)
17  else:
18  raise RuntimeError('Error: renumbering failed at position %s' %pos)
19  return ev, new_numbers
20 
21 def _RenumberAln(aln, seq_index):
22  if not aln.sequences[seq_index].HasAttachedView():
23  raise RuntimeError("Sequence Handle has no attached view")
24  counter=0
25  ev = aln.sequences[seq_index].attached_view.CreateEmptyView()
26  new_numbers = mol.ResNumList()
27  for col in aln:
28  if col[0]!='-' and col[seq_index]!='-':
29  if col[0]!=col[seq_index]:
30  raise RuntimeError("residue mismatch at position %d (%s vs %s) (renumbering failed)"%(counter, col[0],col[1]))
31  rnum=aln.GetSequence(seq_index).GetResidueIndex(counter)
32  r=aln.GetSequence(seq_index).GetResidue(counter)
33  if not r.IsValid():
34  raise RuntimeError("invalid residue at postion %s (renumbering failed)" %(counter))
35  ev.AddResidue(r, mol.INCLUDE_ALL)
36  new_numbers.append(counter+1)
37  counter+=1
38  return ev, new_numbers
39 
40 
41 def Renumber(seq_handle, sequence_number_with_attached_view=1):
42  """
43  Function to renumber an entity according to an alignment between the model sequence
44  and the full-length target sequence. The aligned model sequence or the alignment itself
45  with an attached view needs to be provided. Upon succcess, the renumbered entity is returned.
46 
47  .. code-block:: python
48 
49  from ost.seq.alg import renumber
50  from ost.bindings.clustalw import *
51  ent=io.LoadPDB("path_to_model")
52  s=io.LoadSequence("path_to_full_length_fasta_seqeunce")
53  pdb_seq=seq.SequenceFromChain("model", ent.chains[0])
54  aln=ClustalW(s,pdb_seq)
55  aln.AttachView(1,ent.chains[0].Select(""))
56  e=Renumber(aln.GetSequence(sequence_number_with_attached_view))
57  io.SavePDB(e, "renum.pdb")
58 
59  """
60  if isinstance(seq_handle, seq.SequenceHandle):
61  ev, new_numbers = _RenumberSeq(seq_handle)
62  elif isinstance(seq_handle, seq.AlignmentHandle):
63  ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view)
64 
65  ev.AddAllInclusiveBonds()
66  new_ent = mol.CreateEntityFromView(ev,False);
67  new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
68  return new_ent