IMP  2.4.0
The Integrative Modeling Platform
new/stereochemistry.py
1 #!/usr/bin/env python
2 from __future__ import print_function
3 import IMP
4 import IMP.core
5 import IMP.atom
6 import IMP.isd
7 import itertools
8 import IMP.pmi
9 
10 class MyGetRestraint(object):
11  def __init__(self,rs):
12  self.rs=rs
13  def get_restraint_for_rmf(self):
14  return self.rs
15 
17  """ Enable CHARMM force field """
18  def __init__(self,root,
19  ff_temp=300.0,
20  zone_ps=None,
21  zone_size=10.0,
22  enable_nonbonded=True,
23  enable_bonded=True,
24  zone_nonbonded=False):
25  """Setup the charmm restraint on a selection. Expecting atoms.
26  @param root The node at which to apply the restraint
27  @param ff_temp The temperature of the force field
28  @param zone_ps Create a zone around this set of particles
29  Automatically includes the entire residue (incl. backbone)
30  @param zone_size The size for looking for neighbor residues
31  @param enable_nonbonded Allow the repulsive restraint
32  @param enable_bonded Allow the bonded restraint
33  @param zone_nonbonded EXPERIMENTAL: exclude from nonbonded all sidechains that aren't in zone!
34  """
35 
36  kB = (1.381 * 6.02214) / 4184.0
37 
38  self.mdl = root.get_model()
39  self.bonds_rs = IMP.RestraintSet(self.mdl, 1.0 / (kB * ff_temp), 'BONDED')
40  self.nonbonded_rs = IMP.RestraintSet(self.mdl, 1.0 / (kB * ff_temp), 'NONBONDED')
41  self.weight=1.0
42  self.label=""
43 
44  ### setup topology and bonds etc
45  if enable_bonded:
47  topology = ff.create_topology(root)
48  topology.apply_default_patches()
49  topology.setup_hierarchy(root)
50  if zone_ps is not None:
52  root,
53  zone_ps,
54  zone_size,
55  entire_residues=True,
56  exclude_backbone=False)
58  topology,
59  limit_to_ps)
60  self.ps = limit_to_ps
61  else:
62  r = IMP.atom.CHARMMStereochemistryRestraint(root, topology)
63  self.ps = IMP.core.get_leaves(root)
64  self.bonds_rs.add_restraint(r)
65  ff.add_radii(root)
66  ff.add_well_depths(root)
67 
68  atoms = IMP.atom.get_leaves(root)
69  ### non-bonded forces
70  if enable_nonbonded:
71  if (zone_ps is not None) and zone_nonbonded:
72  print('stereochemistry: zone_nonbonded is True')
73  # atoms list should only include backbone plus zone_ps!
74  backbone_types=['C','N','CB','O']
75  sel = IMP.atom.Selection(root,atom_types=[IMP.atom.AtomType(n)
76  for n in backbone_types])
77  backbone_atoms = sel.get_selected_particles()
78  #x = list(set(backbone_atoms)|set(limit_to_ps))
80  root,
81  zone_ps,
82  zone_size,
83  entire_residues=True,
84  exclude_backbone=True)
85 
89  4.0)
90  else:
92  self.nbl = IMP.container.ClosePairContainer(cont, 4.0)
93  if enable_bonded:
94  self.nbl.add_pair_filter(r.get_full_pair_filter())
95  pairscore = IMP.isd.RepulsiveDistancePairScore(0,1)
96  pr=IMP.container.PairsRestraint(pairscore, self.nbl)
97  self.nonbonded_rs.add_restraint(pr)
98 
99  print('CHARMM is set up')
100 
101  def set_label(self, label):
102  self.label = label
103  self.bonds_rs.set_name(self.bonds_rs.get_name()+label)
104  self.nonbonded_rs.set_name(self.nonbonded_rs.get_name()+label)
105 
106  def add_to_model(self):
107  self.mdl.add_restraint(self.bonds_rs)
108  self.mdl.add_restraint(self.nonbonded_rs)
109 
110  def get_restraints(self):
111  return [self.bonds_rs,self.nonbonded_rs]
112 
113  def get_restraint(self):
114  return self.bonds_rs
115 
116  def get_close_pair_container(self):
117  return self.nbl
118 
119  def get_ps(self):
120  return self.ps
121 
122  def set_weight(self, weight):
123  self.weight = weight
124  self.bonds_rs.set_weight(weight)
125  self.nonbonded_rs.set_weight(weight)
126  print('CHARMM: weight is',self.weight)
127 
128  def get_output(self):
129  self.mdl.update()
130  output = {}
131  bonds_score = self.weight * self.bonds_rs.unprotected_evaluate(None)
132  nonbonded_score = self.weight * self.nonbonded_rs.unprotected_evaluate(None)
133  score=bonds_score+nonbonded_score
134  output["_TotalScore"] = str(score)
135  output["CHARMM_BONDS"+str(self.label)] = str(bonds_score)
136  output["CHARMM_NONBONDED"+str(self.label)] = str(nonbonded_score)
137  return output
138 
139 class ElasticNetworkRestraint(object):
140  def __init__(self,root,
141  subsequence=None,
142  label='',
143  strength=10.0,
144  dist_cutoff=10.0,
145  **kwargs):
146  """ Add harmonic restraints between all pairs below a specified distance
147  @param root Root hierarchy for applying selections
148  @param subsequence A PMI Subsequence class
149  @param strength The elastic bond strength
150  @param dist_cutoff Create bonds when below this distance
151  \note any additional keyword arguments are passed to the Selection
152  E.g. you could pass atom_type=IMP.atom.AtomType("CA")c
153  """
154  self.mdl = root.get_model()
155  self.rs = IMP.RestraintSet(self.mdl, "ElasticNetwork")
156  self.weight = 1
157  self.pairslist = []
158  self.label=label
159  if subsequence is not None:
160  self.ps = subsequence.get_selection(root,**kwargs).get_selected_particles()
161  else:
162  self.ps = IMP.atom.Selection(root,**kwargs).get_selected_particles()
163  if len(self.ps)==0:
164  print('ERROR: Did not select any particles!')
165  exit()
166  self.rs = IMP.pmi.create_elastic_network(self.ps,dist_cutoff,strength)
167  #self.rs = IMP.isd_emxl.create_lognormal_elastic_network(ps,dist_cutoff,strength)
168  print('created elastic network',self.label,'with',self.rs.get_number_of_restraints(),'restraints')
169 
170  def set_label(self, label):
171  self.label = label
172  self.rs.set_name(label)
173  for r in self.rs.get_restraints():
174  r.set_name(label)
175 
176  def get_ps(self):
177  return self.ps
178 
179  def add_to_model(self):
180  self.mdl.add_restraint(self.rs)
181 
182  def get_restraint(self):
183  return self.rs
184 
185  def set_weight(self, weight):
186  self.weight = weight
187  self.rs.set_weight(weight)
188 
189  def get_excluded_pairs(self):
190  return self.pairslist
191 
192  def get_restraints_for_rmf(self):
193  return [MyGetRestraint(self.rs)]
194 
195  def get_output(self):
196  self.mdl.update()
197  output = {}
198  score = self.weight * self.rs.unprotected_evaluate(None)
199  output["ElasticNetworkRestraint_" + self.label] = str(score)
200  output["_TotalScore"] = str(score)
201  return output
202 
203 class SymmetryRestraint(object):
204  def __init__(self,root,
205  symmetry_transforms,
206  selection_dicts,
207  extra_sel={"atom_type":IMP.atom.AtomType("CA")},
208  label='',
209  strength=10.0):
210  """ Add harmonic restraints to the mean positions between multiple selections.
211  @param root Root hierarchy for applying selections
212  @param symmetry_transforms Transforms moving each selection to the first selection
213  @param selection_dicts Selection kwargs for each symmetry unit
214  @param extra_sel Additional selection arguments (e.g., CA only!)
215  @param strength The elastic bond strength
216 
217  \note the length of symmetry_transforms should be one more than the length of selection_dicts
218  Because all transforms are WRT the first selection
219  """
220  self.mdl = root.get_model()
221  self.rs = IMP.RestraintSet(self.mdl, "Symmetry")
222  self.weight = 1
223  self.label=label
224 
225  if len(selection_dicts)!=len(symmetry_transforms)+1:
226  print('Error: There should be one more symmetry transform than selection dict')
227  exit()
228  harmonic = IMP.core.Harmonic(0.,strength)
229  sel0 = hierarchy_tools.combine_dicts(selection_dicts[0],extra_sel)
230  ps0 = IMP.atom.Selection(root,**sel0).get_selected_particles()
231  if len(ps0)==0:
232  print("Error: did not select any particles!")
233  exit()
234  for sdict,trans in zip(selection_dicts[1:],symmetry_transforms):
235  sel = hierarchy_tools.combine_dicts(sdict,extra_sel)
236  ps=IMP.atom.Selection(root,**sel).get_selected_particles()
237  if len(ps0)!=len(ps):
238  print("Error: did not select the same number of particles!")
239  exit()
240  pair_score = IMP.core.TransformedDistancePairScore(harmonic,trans)
241  for p0,p1 in zip(ps0,ps):
242  r = IMP.core.PairRestraint(pair_score,(p0,p1))
243  self.rs.add_restraint(r)
244 
245  print('created symmetry network with',self.rs.get_number_of_restraints(),'restraints')
246 
247  def set_label(self, label):
248  self.label = label
249  self.rs.set_name(label)
250  for r in self.rs.get_restraints():
251  r.set_name(label)
252 
253  def add_to_model(self):
254  self.mdl.add_restraint(self.rs)
255 
256  def get_restraint(self):
257  return self.rs
258 
259  def set_weight(self, weight):
260  self.weight = weight
261  self.rs.set_weight(weight)
262 
263  def get_excluded_pairs(self):
264  return self.pairslist
265 
266  def get_output(self):
267  self.mdl.update()
268  output = {}
269  score = self.weight * self.rs.unprotected_evaluate(None)
270  output["SymmetryRestraint_" + self.label] = str(score)
271  output["_TotalScore"] = str(score)
272  return output
273 
274 class CoarseCARestraint(object):
275  """ Add bonds, angles, and dihedrals for a CA-only model """
276  def __init__(self,root,
277  strength=10.0,
278  bond_distance=3.78,
279  bond_jitter=None,
280  angle_min=100.0,
281  angle_max=140.0,
282  dihedral_seq=None):
283  pass
CHARMMParameters * get_heavy_atom_CHARMM_parameters()
Enforce CHARMM stereochemistry on the given Hierarchy.
def __init__
Setup the charmm restraint on a selection.
def get_particles_within_zone
Utility to retrieve particles from a hierarchy within a zone around a set of ps.
Object used to hold a set of restraints.
Apply a function to the distance between two particles after transforming the first.
kernel::RestraintsTemp get_restraints(const Subset &s, const ParticleStatesTable *pst, const DependencyGraph &dg, kernel::RestraintSet *rs)
The type of an atom.
Return all close unordered pairs of particles taken from the SingletonContainer.
Return all close ordered pairs of particles taken from the two SingletonContainers.
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
RestraintSet * create_elastic_network(const Particles &ps, Float dist_cutoff, Float strength)
Create an elastic network restraint set.
Definition: utilities.h:19
Add bonds, angles, and dihedrals for a CA-only model.
Store a kernel::ParticleIndexes.
Basic functionality that is expected to be used by a wide variety of IMP users.
Applies a PairScore to a Pair.
Definition: PairRestraint.h:29
Python classes to represent, score, sample and analyze models.
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
Select hierarchy particles identified by the biological name.
Definition: Selection.h:62
Applies a PairScore to each Pair in a list.
A repulsive potential on the distance between two atoms.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...
Harmonic function (symmetric about the mean)
Definition: core/Harmonic.h:24