IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
bff/tools/__init__.py
1 """@namespace IMP.bff.tools
2  Utility functions.
3 """
4 
5 import IMP
6 import IMP.core
7 import IMP.atom
8 import IMP.bff
9 
10 import typing
11 import numpy as np
12 import scipy.stats
13 
14 
16  used_avs: typing.List[IMP.bff.AV],
17  dye_radius: float = 3.5
18 ):
19  """Decorate the accessible volume particles
20  with a mass and radii, so that they appear
21  in the rmf file"""
22  for dye in used_avs:
23  # Add radius and mass to dye
24  p_dye = dye.get_particle()
25  if not IMP.core.XYZR.get_is_setup(p_dye):
26  p_dye = IMP.core.XYZR.setup_particle(p_dye)
27  p_dye.set_radius(dye_radius)
28  else:
29  p_dye = IMP.core.XYZR(p_dye)
30  if not IMP.atom.Mass.get_is_setup(p_dye):
32 
33  # add dye to atom hier
34  p_att = dye.get_source()
35  h_att = IMP.atom.Hierarchy(p_att)
37  h_dye = IMP.atom.Hierarchy(p_dye)
38  h_att.add_child(h_dye)
39  # create bond between dye and source
40  if not IMP.atom.Bonded.get_is_setup(p_dye):
42  if not IMP.atom.Bonded.get_is_setup(p_att):
44  if not IMP.atom.get_bond(IMP.atom.Bonded(p_dye), IMP.atom.Bonded(p_att)):
46  IMP.atom.Bonded(p_dye), IMP.atom.Bonded(p_att), 1)
47 
48 
49 class FRETDistanceConverter(object):
50 
51  _forster_radius_: float = 52.0
52 
53  # current center-to-center distance
54  _dist_center_center_: float = 50.0
55 
56  # Variables for distance distribution computation
57  _distances_: np.ndarray = None # distance array for distance distribution
58  _fret_efficiencies_: np.ndarray = None # FRET efficiency array corre
59  _density_: np.ndarray = None
60 
61  # lookup table to cache the computation
62  _d_mean_: np.ndarray = None
63  _d_mean_fret_: np.ndarray = None
64  _e_mean_: np.ndarray = None
65 
66  def _update_fret_efficiencies_(self, forster_radius: float):
67  RDA = self._distances_
68  R0 = forster_radius
69  self._fret_efficiencies_ = 1. / (1 + (RDA / R0)**6.0)
70 
71  def _update_lookup(self):
72  self._d_mean_ = np.empty_like(self._distances_)
73  self._d_mean_fret_ = np.empty_like(self._distances_)
74  self._e_mean_ = np.empty_like(self._distances_)
75  for i, d in enumerate(self._distances_):
76  self._update_density(d, self._sigma_)
77  self._d_mean_[i] = self.calc_distance_mean()
78  self._d_mean_fret_[i] = self.calc_distance_mean_fret()
79  self._e_mean_[i] = self.calc_fret_efficiency_mean()
80 
81  def _update_density(self, dcc: float, w: float):
82  """Update the distance distribution
83 
84  @param dcc center-to-center distance
85  @param w parameter controlling the distribution width
86  """
87  d = self._distances_
88  n1 = scipy.stats.norm(-dcc, w)
89  n2 = scipy.stats.norm(dcc, w)
90  p = n1.pdf(d) + n2.pdf(d)
91  p /= p.sum()
92  self._density_ = p
93 
94  @property
95  def dist_center_center(self):
96  return self._dist_center_center_
97 
98  @dist_center_center.setter
99  def dist_center_center(self, v):
100  self._dist_center_center_ = v
101 
102  @property
103  def sigma(self):
104  return self._sigma_
105 
106  @sigma.setter
107  def sigma(self, v: float):
108  self._sigma_ = v
109  self._update_lookup()
110 
111  @property
112  def forster_radius(self):
113  return self._forster_radius_
114 
115  @forster_radius.setter
116  def forster_radius(self, v: float):
117  self._forster_radius_ = v
118  self._update_fret_efficiencies_(v)
119  self._update_lookup()
120 
121  def calc_distance_mean(self):
122  p = self._density_
123  v = self._distances_
124  return p @ v
125 
126  def calc_fret_efficiency_mean(self):
127  p = self._density_
128  v = self._fret_efficiencies_
129  return p @ v
130 
131  def calc_distance_mean_fret(self):
132  E = self.calc_fret_efficiency_mean()
133  R0 = self.forster_radius
134  return R0 * (1. / E - 1.)**(1. / 6.)
135 
136  @property
137  def fret_efficiency_mean(self):
138  return np.interp(self.dist_center_center, self._distances_, self._e_mean_)
139 
140  @property
141  def distance_mean(self):
142  return np.interp(self.dist_center_center, self._distances_, self._d_mean_)
143 
144  @property
145  def distance_mean_fret(self):
146  return np.interp(self.dist_center_center, self._distances_, self._d_mean_fret_)
147 
148  def __call__(self, value: float, distance_type: int):
149  self.dist_center_center = value
150  if distance_type == IMP.bff.DYE_PAIR_DISTANCE_MEAN:
151  return self.distance_mean
152  elif distance_type == IMP.bff.DYE_PAIR_DISTANCE_E:
153  return self.distance_mean_fret
154  elif distance_type == IMP.bff.DYE_PAIR_DISTANCE_MP:
155  return value
156 
157  def __init__(
158  self,
159  forster_radius: float = 52.0,
160  sigma: float = 6.0,
161  distance_range: typing.Tuple[float, float] = (1.0, 100.0),
162  n_distances: int = 128
163  ):
164  self._forster_radius_ = forster_radius
165  self._sigma_ = sigma
166  self._distances_ = np.linspace(*distance_range, n_distances, dtype=np.float64)
167  self._density_ = np.zeros_like(self._distances_)
168  self._update_fret_efficiencies_(self._forster_radius_)
169  self._update_lookup()
170 
171 
172 def read_xlink_table(fn: str) -> typing.Dict[int, typing.Dict]:
173  """Read a xlink table
174 
175  :param fn: filename of the xlink table
176  :return:
177  """
178  # Read the xlink table
179  xlinks = {} # a dict of the xlinks, the keys are used to address the xlinks
180  xlink_idx = 0
181  with open(fn, 'r') as fp:
182  lines = fp.readlines()
183  for line in lines[1:]:
184  try:
185  line = line.strip()
186  protein_1, residue_1, protein_2, residue_2 = line.split(',')
187  residue_1 = int(residue_1)
188  residue_2 = int(residue_2)
189  d = {
190  'protein_1': protein_1,
191  'protein_2': protein_2,
192  'residue_1': residue_1,
193  'residue_2': residue_2
194  }
195  xlinks[xlink_idx] = d
196  xlink_idx += 1
197  except ValueError:
198  pass
199  return xlinks
200 
A decorator for a particle with accessible volume (AV).
Definition: AV.h:94
def read_xlink_table
Read a xlink table.
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition: XYZR.h:48
A decorator for a particle which has bonds.
static bool get_is_setup(Model *m, ParticleIndex pi)
Definition: Mass.h:34
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: XYZR.h:47
Bond create_bond(Bonded a, Bonded b, Bond o)
Connect the two wrapped particles by a custom bond.
static Hierarchy setup_particle(Model *m, ParticleIndex pi, ParticleIndexesAdaptor children=ParticleIndexesAdaptor())
Create a Hierarchy of level t by adding the needed attributes.
The standard decorator for manipulating molecular structures.
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
Definition: Mass.h:48
static Bonded setup_particle(Model *m, ParticleIndex pi)
Basic functionality that is expected to be used by a wide variety of IMP users.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Bond get_bond(Bonded a, Bonded b)
Get the bond between two particles.
Functionality for loading, creating, manipulating and scoring atomic structures.
Bayesian Fluorescence Framework.
def display_mean_av_positions
Decorate the accessible volume particles with a mass and radii, so that they appear in the rmf file...
A decorator for a particle with x,y,z coordinates and a radius.
Definition: XYZR.h:27