00001 """
00002 A bunch of algorithms operating on two views.
00003
00004 Authors: Marco Biasini, Pascal Benkert
00005 """
00006
00007 from ost import io,mol
00008
00009 def _GetChain(view):
00010 if view.chain_count!=1:
00011 raise RuntimeError("chain count of view must be one")
00012 return view.chains[0]
00013
00014 def _GetChains(view_a, view_b):
00015 return _GetChain(view_a), _GetChain(view_b)
00016
00017
00018 def _EmptyView(view):
00019 if isinstance(view, mol.EntityHandle):
00020 return view.CreateEmptyView()
00021 return view.handle.CreateEmptyView()
00022
00023
00024 def PairResiduesByNum(view_a, view_b,
00025 view_add_flags=mol.ViewAddFlag.INCLUDE_ATOMS):
00026 """
00027 Pair residues by residue number.
00028 """
00029 not_supported="PairResiduesByNum has no support for unsorted chains"
00030 chain_a, chain_b=_GetChains(view_a, view_b)
00031 if not chain_a.InSequence() or not chain_b.InSequence():
00032 raise RuntimeError(not_supported)
00033 residues_a=chain_a.residues
00034 residues_b=chain_b.residues
00035 residues_a=iter(chain_a.residues)
00036 residues_b=iter(chain_b.residues)
00037 result_a=_EmptyView(view_a)
00038 result_b=_EmptyView(view_b)
00039 try:
00040 while True:
00041 r1=residues_a.next()
00042 r2=residues_b.next()
00043 while r1.number<r2.number:
00044 r1=residues_a.next()
00045 while r2.number<r1.number:
00046 r2=residues_b.next()
00047 assert r1.number==r2.number
00048 result_a.AddResidue(r1, view_add_flags)
00049 result_b.AddResidue(r2, view_add_flags)
00050 except StopIteration:
00051 pass
00052 return result_a, result_b
00053
00054 def RepresentativeAtoms(ent, chain=None, alpha_and_beta=False):
00055 """
00056 Returns a view with one or two representative atom per amino acid residue.
00057
00058 There are two basic modes, controlled by the alpha_and_beta parameter:
00059
00060 When the parameter is false, for residues with a sidechain, the C-beta atom is
00061 used, for residues without sidechain, the C-alpha atom is used. Note that this
00062 is different from using the selection
00063
00064 (aname=CA and rname=GLY) or (aname=CB and rname!=GLY)
00065
00066 When the alpha_and_beta parameter is true, both C-alpha and C-beta (if
00067 available) are added to the view.
00068
00069 If chain is not equal to None, only atoms of the chain with that chain name
00070 will be added to the view.
00071 """
00072 e_view=ent.CreateEmptyView()
00073 if ent.IsValid():
00074
00075 if chain==None:
00076 for res in ent.residues:
00077 res=res.handle
00078 if res.IsPeptideLinking():
00079 atom = res.FindAtom('CB')
00080 if atom.IsValid():
00081 e_view.AddAtom(atom)
00082
00083
00084 if alpha_and_beta == 1:
00085 atom = res.FindAtom('CA')
00086 if atom.IsValid():
00087 e_view.AddAtom(atom)
00088
00089 else:
00090 atom = res.FindAtom('CA')
00091 if atom.IsValid():
00092 e_view.AddAtom(atom)
00093
00094
00095 if alpha_and_beta == 1:
00096 e_view.AddAtom(atom)
00097
00098 elif chain != "" and ent.FindChain(chain).IsValid():
00099 for res in ent.FindChain(chain).GetResidueList():
00100 res=res.handle
00101 if res.IsPeptideLinking():
00102 atom = res.FindAtom('CB')
00103 if atom.IsValid():
00104 e_view.AddAtom(atom)
00105
00106 if alpha_and_beta == 1:
00107 atom = res.FindAtom('CA')
00108 if atom.IsValid():
00109 e_view.AddAtom(atom)
00110
00111 else:
00112 atom = res.FindAtom('CA')
00113 if atom.IsValid():
00114 e_view.AddAtom(atom)
00115
00116
00117 if alpha_and_beta == 1:
00118 e_view.AddAtom(atom)
00119 return e_view