00001 from ost import seq, mol
00002
00003 def _RenumberSeq(seq_handle):
00004 if not seq_handle.HasAttachedView():
00005 raise RuntimeError("Sequence Handle has no attached view")
00006 ev = seq_handle.attached_view.CreateEmptyView()
00007 new_numbers = mol.ResNumList()
00008 for pos in range(len(seq_handle)):
00009 if seq_handle[pos] != '-':
00010 r = seq_handle.GetResidue(pos)
00011 if r.IsValid():
00012 ev.AddResidue(r, mol.INCLUDE_ALL)
00013 new_numbers.append(pos+1)
00014 else:
00015 raise RuntimeError('Error: renumbering failed at position %s' % pos)
00016 return ev, new_numbers
00017
00018 def _RenumberAln(aln, seq_index):
00019 if not aln.sequences[seq_index].HasAttachedView():
00020 raise RuntimeError("Sequence Handle has no attached view")
00021 counter=0
00022 ev = aln.sequences[seq_index].attached_view.CreateEmptyView()
00023 new_numbers = mol.ResNumList()
00024 for col in aln:
00025 if col[0] != '-' and col[seq_index] != '-':
00026 if col[0] != col[seq_index]:
00027 raise RuntimeError("residue mismatch at position %d (%s vs %s) "\
00028 "(renumbering failed)" % (counter, col[0],
00029 col[seq_index]))
00030 rnum = aln.GetSequence(seq_index).GetResidueIndex(counter)
00031 r = aln.GetSequence(seq_index).GetResidue(counter)
00032 if not r.IsValid():
00033 raise RuntimeError("invalid residue at postion %s (renumbering failed)"\
00034 % (counter))
00035 ev.AddResidue(r, mol.INCLUDE_ALL)
00036 new_numbers.append(counter+1)
00037 counter += 1
00038 return ev, new_numbers
00039
00040
00041 def Renumber(seq_handle, sequence_number_with_attached_view=1):
00042 """
00043 Function to renumber an entity according to an alignment between the model
00044 sequence and the full-length target sequence. The aligned model sequence or
00045 the alignment itself with an attached view needs to be provided. Upon
00046 succcess, the renumbered entity is returned.
00047 If an alignment is given, the sequence must
00048
00049 .. code-block:: python
00050
00051 from ost.seq.alg import renumber
00052 from ost.bindings.clustalw import *
00053 ent = io.LoadPDB("path_to_model")
00054 s = io.LoadSequence("path_to_full_length_fasta_seqeunce")
00055 pdb_seq = seq.SequenceFromChain("model", ent.chains[0])
00056 aln = ClustalW(s, pdb_seq)
00057 aln.AttachView(1, ent.chains[0].Select(""))
00058 e = Renumber(aln.sequences[1])
00059 io.SavePDB(e, "renum.pdb")
00060
00061 :param seq_handle: Sequence or alignment handle with attached view.
00062 :type seq_handle: :class:`SequenceHandle` / :class:`AlignmentHandle`
00063 :param sequence_number_with_attached_view: Sequence number for the aln. handle
00064 (not used if seq. handle given)
00065 :type sequence_number_with_attached_view: :class:`int`
00066 :raises: :exc:`RuntimeError` if unknown type of *seq_handle* or if attached
00067 view is missing or if the given alignment sequence is inconsistent.
00068 """
00069 if isinstance(seq_handle, seq.SequenceHandle) \
00070 or isinstance(seq_handle, seq.ConstSequenceHandle):
00071 ev, new_numbers = _RenumberSeq(seq_handle)
00072 elif isinstance(seq_handle, seq.AlignmentHandle):
00073 ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view)
00074 else:
00075 raise RuntimeError("Unknown input type " + str(type(seq_handle)))
00076
00077 ev.AddAllInclusiveBonds()
00078 new_ent = mol.CreateEntityFromView(ev, False);
00079 new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
00080 return new_ent
00081
00082
00083 __all__ = ('Renumber', )