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 if isinstance(ent,mol.EntityHandle):
00073 e_view = ent.CreateEmptyView()
00074 elif isinstance(ent,mol.EntityView):
00075 e_view = ent.GetHandle().CreateEmptyView()
00076 if ent.IsValid():
00077
00078 if chain==None:
00079 for res in ent.residues:
00080 if res.IsPeptideLinking():
00081 atom = res.FindAtom('CB')
00082 if atom.IsValid():
00083 e_view.AddAtom(atom)
00084
00085
00086 if alpha_and_beta == 1:
00087 atom = res.FindAtom('CA')
00088 if atom.IsValid():
00089 e_view.AddAtom(atom)
00090
00091 else:
00092 atom = res.FindAtom('CA')
00093 if atom.IsValid():
00094 e_view.AddAtom(atom)
00095
00096
00097 if alpha_and_beta == 1:
00098 e_view.AddAtom(atom)
00099
00100 elif chain != "" and ent.FindChain(chain).IsValid():
00101 for res in ent.FindChain(chain).GetResidueList():
00102 if res.IsPeptideLinking():
00103 atom = res.FindAtom('CB')
00104 if atom.IsValid():
00105 e_view.AddAtom(atom)
00106
00107 if alpha_and_beta == 1:
00108 atom = res.FindAtom('CA')
00109 if atom.IsValid():
00110 e_view.AddAtom(atom)
00111
00112 else:
00113 atom = res.FindAtom('CA')
00114 if atom.IsValid():
00115 e_view.AddAtom(atom)
00116
00117
00118 if alpha_and_beta == 1:
00119 e_view.AddAtom(atom)
00120 return e_view