OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
views.py
Go to the documentation of this file.
1 """
2 A bunch of algorithms operating on two views.
3 
4 Authors: Marco Biasini, Pascal Benkert
5 """
6 
7 from ost import io,mol
8 
9 def _GetChain(view):
10  if view.chain_count!=1:
11  raise RuntimeError("chain count of view must be one")
12  return view.chains[0]
13 
14 def _GetChains(view_a, view_b):
15  return _GetChain(view_a), _GetChain(view_b)
16 
17 
18 def _EmptyView(view):
19  if isinstance(view, mol.EntityHandle):
20  return view.CreateEmptyView()
21  return view.handle.CreateEmptyView()
22 
23 
24 def PairResiduesByNum(view_a, view_b,
25  view_add_flags=mol.ViewAddFlag.INCLUDE_ATOMS):
26  """
27  Pair residues by residue number.
28  """
29  not_supported="PairResiduesByNum has no support for unsorted chains"
30  chain_a, chain_b=_GetChains(view_a, view_b)
31  if not chain_a.InSequence() or not chain_b.InSequence():
32  raise RuntimeError(not_supported)
33  residues_a=chain_a.residues
34  residues_b=chain_b.residues
35  residues_a=iter(chain_a.residues)
36  residues_b=iter(chain_b.residues)
37  result_a=_EmptyView(view_a)
38  result_b=_EmptyView(view_b)
39  try:
40  while True:
41  r1=residues_a.next()
42  r2=residues_b.next()
43  while r1.number<r2.number:
44  r1=residues_a.next()
45  while r2.number<r1.number:
46  r2=residues_b.next()
47  assert r1.number==r2.number
48  result_a.AddResidue(r1, view_add_flags)
49  result_b.AddResidue(r2, view_add_flags)
50  except StopIteration:
51  pass
52  return result_a, result_b
53 
54 def RepresentativeAtoms(ent, chain=None, alpha_and_beta=False):
55  """
56  Returns a view with one or two representative atom per amino acid residue.
57 
58  There are two basic modes, controlled by the alpha_and_beta parameter:
59 
60  When the parameter is false, for residues with a sidechain, the C-beta atom is
61  used, for residues without sidechain, the C-alpha atom is used. Note that this
62  is different from using the selection
63 
64  (aname=CA and rname=GLY) or (aname=CB and rname!=GLY)
65 
66  When the alpha_and_beta parameter is true, both C-alpha and C-beta (if
67  available) are added to the view.
68 
69  If chain is not equal to None, only atoms of the chain with that chain name
70  will be added to the view.
71  """
72  e_view=ent.CreateEmptyView()
73  if ent.IsValid():
74  # all chains:
75  if chain==None:
76  for res in ent.residues:
77  res=res.handle
78  if res.IsPeptideLinking():
79  atom = res.FindAtom('CB')
80  if atom.IsValid():
81  e_view.AddAtom(atom)
82 
83  #also add C-alpha for hybrid-potential:
84  if alpha_and_beta == 1:
85  atom = res.FindAtom('CA')
86  if atom.IsValid():
87  e_view.AddAtom(atom)
88 
89  else: # Cbeta does not exist, Calpha?
90  atom = res.FindAtom('CA')
91  if atom.IsValid():
92  e_view.AddAtom(atom)
93 
94  # count C_alphas twice for hybrid-potential:
95  if alpha_and_beta == 1:
96  e_view.AddAtom(atom)
97 
98  elif chain != "" and ent.FindChain(chain).IsValid():
99  for res in ent.FindChain(chain).GetResidueList():
100  res=res.handle
101  if res.IsPeptideLinking():
102  atom = res.FindAtom('CB')
103  if atom.IsValid():
104  e_view.AddAtom(atom)
105 
106  if alpha_and_beta == 1:
107  atom = res.FindAtom('CA')
108  if atom.IsValid():
109  e_view.AddAtom(atom)
110 
111  else: # Cbeta does not exist, Calpha?
112  atom = res.FindAtom('CA')
113  if atom.IsValid():
114  e_view.AddAtom(atom)
115 
116  # count C_alphas twice for glycine:
117  if alpha_and_beta == 1:
118  e_view.AddAtom(atom)
119  return e_view