00001 from ost import io, seq, mol, conop
00002 from ost import *
00003
00004
00005 def _RenumberSeq(seq_handle):
00006 if not seq_handle.HasAttachedView():
00007 raise RuntimeError("Sequence Handle has no attached view")
00008 ev = seq_handle.attached_view.CreateEmptyView()
00009 new_numbers = mol.ResNumList()
00010 for pos in range(len(seq_handle)):
00011 if seq_handle[pos]!='-':
00012 r=seq_handle.GetResidue(pos)
00013 if r.IsValid():
00014
00015 ev.AddResidue(r, mol.INCLUDE_ALL)
00016 new_numbers.append(pos+1)
00017 else:
00018 raise RuntimeError('Error: renumbering failed at position %s' %pos)
00019 return ev, new_numbers
00020
00021 def _RenumberAln(aln, seq_index):
00022 if not aln.sequences[seq_index].HasAttachedView():
00023 raise RuntimeError("Sequence Handle has no attached view")
00024 counter=0
00025 ev = aln.sequences[seq_index].attached_view.CreateEmptyView()
00026 new_numbers = mol.ResNumList()
00027 for col in aln:
00028 if col[0]!='-' and col[seq_index]!='-':
00029 if col[0]!=col[seq_index]:
00030 raise RuntimeError("residue mismatch at position %d (%s vs %s) (renumbering failed)"%(counter, col[0],col[1]))
00031 rnum=aln.GetSequence(seq_index).GetResidueIndex(counter)
00032 r=aln.GetSequence(seq_index).GetResidue(counter)
00033 if not r.IsValid():
00034 raise RuntimeError("invalid residue at postion %s (renumbering failed)" %(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 sequence
00044 and the full-length target sequence. The aligned model sequence or the alignment itself
00045 with an attached view needs to be provided. Upon succcess, the renumbered entity is returned.
00046
00047 .. code-block:: python
00048
00049 from ost.seq.alg import renumber
00050 from ost.bindings.clustalw import *
00051 ent=io.LoadPDB("path_to_model")
00052 s=io.LoadSequence("path_to_full_length_fasta_seqeunce")
00053 pdb_seq=seq.SequenceFromChain("model", ent.chains[0])
00054 aln=ClustalW(s,pdb_seq)
00055 aln.AttachView(1,ent.chains[0].Select(""))
00056 e=Renumber(aln.GetSequence(sequence_number_with_attached_view))
00057 io.SavePDB(e, "renum.pdb")
00058
00059 """
00060 if isinstance(seq_handle, seq.SequenceHandle):
00061 ev, new_numbers = _RenumberSeq(seq_handle)
00062 elif isinstance(seq_handle, seq.AlignmentHandle):
00063 ev, new_numbers = _RenumberAln(seq_handle, sequence_number_with_attached_view)
00064
00065 ev.AddAllInclusiveBonds()
00066 new_ent = mol.CreateEntityFromView(ev,False);
00067 new_ent.EditXCS().RenumberChain(new_ent.chains[0], new_numbers)
00068 return new_ent