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 first sequence of the alignment is considered
00048 the full-length sequence and it must match the model sequence wherever it is
00049 aligned (i.e. excluding gaps).
00050
00051 .. code-block:: python
00052
00053 from ost.seq.alg import renumber
00054 from ost.bindings.clustalw import *
00055 ent = io.LoadPDB("path_to_model")
00056 s = io.LoadSequence("path_to_full_length_fasta_seqeunce")
00057 pdb_seq = seq.SequenceFromChain("model", ent.chains[0])
00058 aln = ClustalW(s, pdb_seq)
00059 aln.AttachView(1, ent.chains[0].Select(""))
00060 e = Renumber(aln.sequences[1])
00061 io.SavePDB(e, "renum.pdb")
00062
00063 :param seq_handle: Sequence or alignment handle with attached view.
00064 :type seq_handle: :class:`SequenceHandle` / :class:`AlignmentHandle`
00065 :param sequence_number_with_attached_view: Sequence number for the aln. handle
00066 (not used if seq. handle given)
00067 :type sequence_number_with_attached_view: :class:`int`
00068 :raises: :exc:`RuntimeError` if unknown type of *seq_handle* or if attached
00069 view is missing or if the given alignment sequence is inconsistent.
00070 """
00071 if isinstance(seq_handle, seq.SequenceHandle) \
00072 or isinstance(seq_handle, seq.ConstSequenceHandle):
00073 ev, new_numbers = _RenumberSeq(seq_handle)
00074 elif isinstance(seq_handle, seq.AlignmentHandle):
00075 ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view)
00076 else:
00077 raise RuntimeError("Unknown input type " + str(type(seq_handle)))
00078
00079 ev.AddAllInclusiveBonds()
00080 new_ent = mol.CreateEntityFromView(ev, False);
00081 new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
00082 return new_ent
00083
00084
00085 __all__ = ('Renumber', )