IMP  2.3.1
The Integrative Modeling Platform
tools.py
1 #!/usr/bin/env python
2 
3 """@namespace IMP.pmi.tools
4  Miscellaneous utilities.
5 """
6 
7 import IMP
8 import IMP.algebra
9 import collections
10 
11 
12 class Stopwatch(object):
13 
14  def __init__(self, isdelta=True):
15  global time
16  import time
17 
18  self.starttime = time.clock()
19  self.label = "None"
20  self.isdelta = isdelta
21 
22  def set_label(self, labelstr):
23  self.label = labelstr
24 
25  def get_output(self):
26  output = {}
27  if self.isdelta:
28  newtime = time.clock()
29  output[
30  "Stopwatch_" +
31  self.label +
32  "_delta_seconds"] = str(
33  newtime -
34  self.starttime)
35  self.starttime = newtime
36  else:
37  output["Stopwatch_" + self.label +
38  "_elapsed_seconds"] = str(time.clock() - self.starttime)
39  return output
40 
41 
42 class SetupNuisance(object):
43 
44  def __init__(self, m, initialvalue, minvalue, maxvalue, isoptimized=True):
45  import IMP.isd
46 
47  nuisance = IMP.isd.Scale.setup_particle(IMP.Particle(m), initialvalue)
48  if minvalue:
49  nuisance.set_lower(minvalue)
50  if maxvalue:
51  nuisance.set_upper(maxvalue)
52 
53  # m.add_score_state(IMP.core.SingletonConstraint(IMP.isd.NuisanceRangeModifier(),None,nuisance))
54  nuisance.set_is_optimized(nuisance.get_nuisance_key(), isoptimized)
55  self.nuisance = nuisance
56 
57  def get_particle(self):
58  return self.nuisance
59 
60 
61 class SetupWeight(object):
62 
63  def __init__(self, m, isoptimized=True):
64  import IMP.isd
65  pw = IMP.Particle(m)
66  self.weight = IMP.isd.Weight.setup_particle(pw)
67  self.weight.set_weights_are_optimized(True)
68 
69  def get_particle(self):
70  return self.weight
71 
72 
73 class ParticleToSampleFilter(object):
74  def __init__(self, sampled_objects):
75  self.sampled_objects=sampled_objects
76  self.filters=[]
77 
78  def add_filter(self,filter_string):
79  self.filters.append(filter_string)
80 
81  def get_particles_to_sample(self):
82  particles_to_sample={}
83  for so in self.sampled_objects:
84  ps_dict=so.get_particles_to_sample()
85  for key in ps_dict:
86  for f in self.filters:
87  if f in key:
88  if key not in particles_to_sample:
89  particles_to_sample[key]=ps_dict[key]
90  else:
91  particles_to_sample[key]+=ps_dict[key]
92  return particles_to_sample
93 
94 class ParticleToSampleList(object):
95 
96  def __init__(self, label="None"):
97 
98  self.dictionary_particle_type = {}
99  self.dictionary_particle_transformation = {}
100  self.dictionary_particle_name = {}
101  self.label = label
102 
103  def add_particle(
104  self,
105  particle,
106  particle_type,
107  particle_transformation,
108  name):
109  if not particle_type in ["Rigid_Bodies", "Floppy_Bodies", "Nuisances", "X_coord", "Weights"]:
110  raise TypeError("not the right particle type")
111  else:
112  self.dictionary_particle_type[particle] = particle_type
113  if particle_type == "Rigid_Bodies":
114  if type(particle_transformation) == tuple and len(particle_transformation) == 2 and type(particle_transformation[0]) == float and type(particle_transformation[1]) == float:
115  self.dictionary_particle_transformation[
116  particle] = particle_transformation
117  self.dictionary_particle_name[particle] = name
118  else:
119  raise TypeError("ParticleToSampleList: not the right transformation format for Rigid_Bodies, should be a tuple a floats")
120  else:
121  if type(particle_transformation) == float:
122  self.dictionary_particle_transformation[
123  particle] = particle_transformation
124  self.dictionary_particle_name[particle] = name
125  else:
126  raise TypeError("ParticleToSampleList: not the right transformation format sould be a float")
127 
128  def get_particles_to_sample(self):
129  ps = {}
130  for particle in self.dictionary_particle_type:
131  key = self.dictionary_particle_type[
132  particle] + "ParticleToSampleList_" + self.dictionary_particle_name[particle] + "_" + self.label
133  value = (
134  [particle],
135  self.dictionary_particle_transformation[particle])
136  ps[key] = value
137  return ps
138 
139 
140 class Variance(object):
141 
142  def __init__(self, model, tau, niter, prot, th_profile, write_data=False):
143  global sqrt, os, random
144  from math import sqrt
145  import os
146  import random
147 
148  self.model = model
149  self.write_data = write_data
150  self.tau = tau
151  self.niter = niter
152  #! select particles from the model
153  particles = IMP.atom.get_by_type(prot, IMP.atom.ATOM_TYPE)
154  self.particles = particles
155  # store reference coordinates and theoretical profile
156  self.refpos = [IMP.core.XYZ(p).get_coordinates() for p in particles]
157  self.model_profile = th_profile
158 
159  def perturb_particles(self, perturb=True):
160  for i, p in enumerate(self.particles):
161  newpos = array(self.refpos[i])
162  if perturb:
163  newpos += random.normal(0, self.tau, 3)
164  newpos = IMP.algebra.Vector3D(newpos)
165  IMP.core.XYZ(p).set_coordinates(newpos)
166 
167  def get_profile(self):
168  model_profile = self.model_profile
169  p = model_profile.calculate_profile(self.particles, IMP.saxs.CA_ATOMS)
170  return array([model_profile.get_intensity(i) for i in
171  xrange(model_profile.size())])
172 
173  def init_variances(self):
174  # create placeholders
175  N = self.model_profile.size()
176  a = self.profiles[0][:]
177  self.m = matrix(a).T # Nx1
178  self.V = self.m * self.m.T
179  self.normm = linalg.norm(self.m)
180  self.normV = linalg.norm(self.V)
181 
182  def update_variances(self):
183  a = matrix(self.profiles[-1]) # 1xN
184  n = float(len(self.profiles))
185  self.m = a.T / n + (n - 1) / n * self.m
186  self.V = a.T * a + self.V
187  self.oldnormm = self.normm
188  self.oldnormV = self.normV
189  self.normm = linalg.norm(self.m)
190  self.normV = linalg.norm(self.V)
191  self.diffm = (self.oldnormm - self.normm) / self.oldnormm
192  self.diffV = (self.oldnormV - self.normV) / self.oldnormV
193 
194  def get_direct_stats(self, a):
195  nq = len(a[0])
196  nprof = len(a)
197  m = [0] * nq
198  for prof in a:
199  for q, I in enumerate(prof):
200  m[q] += I
201  m = array(m) / nprof
202  V = matrix(a)
203  V = V.T * V
204  Sigma = (matrix(a - m))
205  Sigma = Sigma.T * Sigma / (nprof - 1)
206  mi = matrix(diag(1. / m))
207  Sigmarel = mi.T * Sigma * mi
208  return m, V, Sigma, Sigmarel
209 
210  def store_data(self):
211  if not os.path.isdir('data'):
212  os.mkdir('data')
213  profiles = matrix(self.profiles)
214  self.directm, self.directV, self.Sigma, self.Sigmarel = \
215  self.get_direct_stats(array(profiles))
216  directV = self.directV
217  # print "V comparison",(linalg.norm(directV-self.V)/self.normV)
218  save('data/profiles', profiles)
219  # absolute profile differences
220  fl = open('data/profiles.dat', 'w')
221  for i, l in enumerate(array(profiles).T):
222  self.model_profile.get_q(i)
223  fl.write('%s ' % i)
224  for k in l:
225  fl.write('%s ' % (k - self.directm[i]))
226  fl.write('\n')
227  # relative profile differences
228  fl = open('data/profiles_rel.dat', 'w')
229  for i, l in enumerate(array(profiles).T):
230  self.model_profile.get_q(i)
231  fl.write('%s ' % i)
232  for k in l:
233  fl.write('%s ' % ((k - self.directm[i]) / self.directm[i]))
234  fl.write('\n')
235  save('data/m', self.directm)
236  save('data/V', self.directV)
237  Sigma = self.Sigma
238  save('data/Sigma', Sigma)
239  # Sigma matrix
240  fl = open('data/Sigma.dat', 'w')
241  model_profile = self.model_profile
242  for i in xrange(model_profile.size()):
243  qi = model_profile.get_q(i)
244  for j in xrange(model_profile.size()):
245  qj = model_profile.get_q(j)
246  vij = self.Sigma[i, j]
247  fl.write('%s %s %s\n' % (qi, qj, vij))
248  fl.write('\n')
249  # Sigma eigenvalues
250  fl = open('data/eigenvals', 'w')
251  for i in linalg.eigvalsh(Sigma):
252  fl.write('%s\n' % i)
253  Sigmarel = self.Sigmarel
254  save('data/Sigmarel', Sigmarel)
255  # Sigmarel matrix
256  fl = open('data/Sigmarel.dat', 'w')
257  model_profile = self.model_profile
258  for i in xrange(model_profile.size()):
259  qi = model_profile.get_q(i)
260  for j in xrange(model_profile.size()):
261  qj = model_profile.get_q(j)
262  vij = self.Sigmarel[i, j]
263  fl.write('%s %s %s\n' % (qi, qj, vij))
264  fl.write('\n')
265  # Sigma eigenvalues
266  fl = open('data/eigenvals_rel', 'w')
267  for i in linalg.eigvalsh(Sigmarel):
268  fl.write('%s\n' % i)
269  # mean profile
270  fl = open('data/mean.dat', 'w')
271  for i in xrange(len(self.directm)):
272  qi = self.model_profile.get_q(i)
273  fl.write('%s ' % qi)
274  fl.write('%s ' % self.directm[i])
275  fl.write('%s ' % sqrt(self.Sigma[i, i]))
276  fl.write('\n')
277 
278  def try_chol(self, jitter):
279  Sigma = self.Sigma
280  try:
281  linalg.cholesky(Sigma + matrix(eye(len(Sigma))) * jitter)
282  except linalg.LinAlgError:
283  print "Decomposition failed with jitter =", jitter
284  return
285  print "Successful decomposition with jitter =", jitter
286 
287  def run(self):
288  self.profiles = [self.get_profile()]
289  # self.init_variances()
290  for n in xrange(self.niter):
291  self.perturb_particles()
292  self.profiles.append(self.get_profile())
293  # self.update_variances()
294  #profiles = matrix(self.profiles)
295  # print n,self.diffm,self.diffV
296  # print
297  #
298  if self.write_data:
299  self.store_data()
300  # self.try_chol(0.)
301  # for i in logspace(-7,0,num=8):
302  # self.try_chol(i)
303 
304  def get_cov(self, relative=True):
305  if not relative:
306  return self.Sigma
307  else:
308  return self.Sigmarel
309 
310  #-------------------------------
311 
312 def get_random_cross_link_dataset(representation,
313  resolution=1.0,
314  number_of_cross_links=10,
315  ambiguity_probability=0.1,
316  confidence_score_range=[0,100],
317  avoid_same_particles=False):
318 
319  '''returns a random cross-link dataset into a string were every
320  line is a residue pair, together with UniqueIdentifier and XL score'''
321 
322  residue_pairs=get_random_residue_pairs(representation, resolution, number_of_cross_links, avoid_same_particles)
323 
324  from random import random
325  unique_identifier=0
326  cmin=float(min(confidence_score_range))
327  cmax=float(max(confidence_score_range))
328 
329  dataset="#\n"
330 
331  for (name1, r1, name2, r2) in residue_pairs:
332  if random() > ambiguity_probability:
333  unique_identifier+=1
334  score=random()*(cmax-cmin)+cmin
335  dataset+=str(name1)+" "+str(name2)+" "+str(r1)+" "+str(r2)+" "+str(score)+" "+str(unique_identifier)+"\n"
336 
337  return dataset
338 
339 
340  #-------------------------------
341 
342 def get_cross_link_data(directory, filename, (distmin, distmax, ndist),
343  (omegamin, omegamax, nomega),
344  (sigmamin, sigmamax, nsigma),
345  don=None, doff=None, prior=0, type_of_profile="gofr"):
346 
347  import IMP.isd
348 
349  filen = IMP.isd.get_data_path("CrossLinkPMFs.dict")
350  xlpot = open(filen)
351 
352  for line in xlpot:
353  dictionary = eval(line)
354  break
355 
356  xpot = dictionary[directory][filename]["distance"]
357  pot = dictionary[directory][filename][type_of_profile]
358 
359  dist_grid = get_grid(distmin, distmax, ndist, False)
360  omega_grid = get_log_grid(omegamin, omegamax, nomega)
361  sigma_grid = get_log_grid(sigmamin, sigmamax, nsigma)
362 
363  if not don is None and not doff is None:
364  xlmsdata = IMP.isd.CrossLinkData(
365  dist_grid,
366  omega_grid,
367  sigma_grid,
368  xpot,
369  pot,
370  don,
371  doff,
372  prior)
373  else:
374  xlmsdata = IMP.isd.CrossLinkData(
375  dist_grid,
376  omega_grid,
377  sigma_grid,
378  xpot,
379  pot)
380  return xlmsdata
381 
382  #-------------------------------
383 
384 
385 def get_cross_link_data_from_length(length, (distmin, distmax, ndist),
386  (omegamin, omegamax, nomega),
387  (sigmamin, sigmamax, nsigma)):
388  import IMP.isd
389 
390  dist_grid = get_grid(distmin, distmax, ndist, False)
391  omega_grid = get_log_grid(omegamin, omegamax, nomega)
392  sigma_grid = get_log_grid(sigmamin, sigmamax, nsigma)
393 
394  xlmsdata = IMP.isd.CrossLinkData(dist_grid, omega_grid, sigma_grid, length)
395  return xlmsdata
396 
397 
398 def get_grid(gmin, gmax, ngrid, boundaries):
399  grid = []
400  dx = (gmax - gmin) / float(ngrid)
401  for i in range(0, ngrid + 1):
402  if(not boundaries and i == 0):
403  continue
404  if(not boundaries and i == ngrid):
405  continue
406  grid.append(gmin + float(i) * dx)
407  return grid
408 
409  #-------------------------------
410 
411 
412 def get_log_grid(gmin, gmax, ngrid):
413  from math import exp, log
414  grid = []
415  for i in range(0, ngrid + 1):
416  grid.append(gmin * exp(float(i) / ngrid * log(gmax / gmin)))
417  return grid
418 
419  #-------------------------------
420 
421 
423  '''
424  example '"{ID_Score}" > 28 AND "{Sample}" ==
425  "%10_1%" OR ":Sample}" == "%10_2%" OR ":Sample}"
426  == "%10_3%" OR ":Sample}" == "%8_1%" OR ":Sample}" == "%8_2%"'
427  '''
428 
429  import pyparsing as pp
430 
431  operator = pp.Regex(">=|<=|!=|>|<|==|in").setName("operator")
432  value = pp.QuotedString(
433  '"') | pp.Regex(
434  r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?")
435  identifier = pp.Word(pp.alphas, pp.alphanums + "_")
436  comparison_term = identifier | value
437  condition = pp.Group(comparison_term + operator + comparison_term)
438 
439  expr = pp.operatorPrecedence(condition, [
440  ("OR", 2, pp.opAssoc.LEFT, ),
441  ("AND", 2, pp.opAssoc.LEFT, ),
442  ])
443 
444  parsedstring = str(expr.parseString(inputstring)) \
445  .replace("[", "(") \
446  .replace("]", ")") \
447  .replace(",", " ") \
448  .replace("'", " ") \
449  .replace("%", "'") \
450  .replace("{", "float(entry['") \
451  .replace("}", "'])") \
452  .replace(":", "str(entry['") \
453  .replace("}", "'])") \
454  .replace("AND", "and") \
455  .replace("OR", "or")
456  return parsedstring
457 
458 
459 def open_file_or_inline_text(filename):
460  try:
461  fl = open(filename, "r")
462  except IOError:
463  fl = filename.split("\n")
464  return fl
465 
466 
467 def get_drmsd(prot0, prot1):
468  drmsd = 0.
469  npairs = 0.
470  for i in range(0, len(prot0) - 1):
471  for j in range(i + 1, len(prot0)):
472  dist0 = IMP.core.get_distance(prot0[i], prot0[j])
473  dist1 = IMP.core.get_distance(prot1[i], prot1[j])
474  drmsd += (dist0 - dist1) ** 2
475  npairs += 1.
476  return math.sqrt(drmsd / npairs)
477 
478  #-------------------------------
479 
480 
481 def get_ids_from_fasta_file(fastafile):
482  ids = []
483  ff = open(fastafile, "r")
484  for l in ff:
485  if l[0] == ">":
486  ids.append(l[1:-1])
487  return ids
488 
489 
490 def get_closest_residue_position(hier, resindex, terminus="N"):
491  '''
492  this function works with plain hierarchies, as read from the pdb,
493  no multi-scale hierarchies
494  '''
495  p = []
496  niter = 0
497  while len(p) == 0:
498  niter += 1
499  sel = IMP.atom.Selection(hier, residue_index=resindex,
500  atom_type=IMP.atom.AT_CA)
501 
502  if terminus == "N":
503  resindex += 1
504  if terminus == "C":
505  resindex -= 1
506 
507  if niter >= 10000:
508  print "get_closest_residue_position: exiting while loop without result"
509  break
510  p = sel.get_selected_particles()
511 
512  if len(p) == 1:
513  return IMP.core.XYZ(p[0]).get_coordinates()
514  elif len(p) == 0:
515  print "get_closest_residue_position: got NO residues for hierarchy %s and residue %i" % (hier, resindex)
516  raise Exception, "get_closest_residue_position: got NO residues for hierarchy %s and residue %i" % (
517  hier, resindex)
518  else:
519  raise ValueError("got multiple residues for hierarchy %s and residue %i; the list of particles is %s" % (hier, resindex, str([pp.get_name() for pp in p])))
520 
521 def get_position_terminal_residue(hier, terminus="C", resolution=1):
522  '''
523  this function get the xyz position of the
524  C or N terminal residue of a hierarchy, given the resolution.
525  the argument of terminus can be either N or C
526  '''
527  termresidue = None
528  termparticle = None
529  for p in IMP.atom.get_leaves(hier):
530  if IMP.pmi.Resolution(p).get_resolution() == resolution:
532  if terminus == "C":
533  if max(residues) >= termresidue and not termresidue is None:
534  termresidue = max(residues)
535  termparticle = p
536  elif termresidue is None:
537  termresidue = max(residues)
538  termparticle = p
539  elif terminus == "N":
540  if min(residues) <= termresidue and not termresidue is None:
541  termresidue = min(residues)
542  termparticle = p
543  elif termresidue is None:
544  termresidue = min(residues)
545  termparticle = p
546  else:
547  raise ValueError("terminus argument should be either N or C")
548 
549  return IMP.core.XYZ(termparticle).get_coordinates()
550 
551 
552 def get_residue_gaps_in_hierarchy(hierarchy, start, end):
553  '''
554  returns the residue index gaps and contiguous segments as tuples given the hierarchy, the first
555  residue and the last residue indexes. The list is organized as
556  [[1,100,"cont"],[101,120,"gap"],[121,200,"cont"]]
557  '''
558  gaps = []
559  for n, rindex in enumerate(range(start, end + 1)):
560  sel = IMP.atom.Selection(hierarchy, residue_index=rindex,
561  atom_type=IMP.atom.AT_CA)
562 
563  if len(sel.get_selected_particles()) == 0:
564  if n == 0:
565  # set the initial condition
566  rindexgap = start
567  rindexcont = start - 1
568  if rindexgap == rindex - 1:
569  # residue is contiguous with the previously discovered gap
570  gaps[-1][1] += 1
571  else:
572  # residue is not contiguous with the previously discovered gap
573  # hence create a new gap tuple
574  gaps.append([rindex, rindex, "gap"])
575  # update the index of the last residue gap
576  rindexgap = rindex
577  else:
578  if n == 0:
579  # set the initial condition
580  rindexgap = start - 1
581  rindexcont = start
582  if rindexcont == rindex - 1:
583  # residue is contiguous with the previously discovered
584  # continuous part
585  gaps[-1][1] += 1
586  else:
587  # residue is not contiguous with the previously discovered continuous part
588  # hence create a new cont tuple
589  gaps.append([rindex, rindex, "cont"])
590  # update the index of the last residue gap
591  rindexcont = rindex
592  return gaps
593 
594 
595 class map(object):
596 
597  def __init__(self):
598  self.map = {}
599 
600  def set_map_element(self, xvalue, yvalue):
601  self.map[xvalue] = yvalue
602 
603  def get_map_element(self, invalue):
604  if type(invalue) == float:
605  n = 0
606  mindist = 1
607  for x in self.map:
608  dist = (invalue - x) * (invalue - x)
609 
610  if n == 0:
611  mindist = dist
612  minx = x
613  if dist < mindist:
614  mindist = dist
615  minx = x
616  n += 1
617  return self.map[minx]
618  elif type(invalue) == str:
619  return self.map[invalue]
620  else:
621  raise TypeError("wrong type for map")
622 
623 
624 def select(representation,
625  resolution=None,
626  hierarchies=None,
627  selection_arguments=None,
628  name=None,
629  name_is_ambiguous=False,
630  first_residue=None,
631  last_residue=None,
632  residue=None,
633  representation_type=None):
634  '''
635  this function uses representation=SimplifiedModel
636  it returns the corresponding selected particles
637  representation_type="Beads", "Res:X", "Densities", "Representation", "Molecule"
638  '''
639 
640  if resolution is None:
641  allparticles = IMP.atom.get_leaves(representation.prot)
642  resolution_particles = None
643  hierarchies_particles = None
644  names_particles = None
645  residue_range_particles = None
646  residue_particles = None
647  representation_type_particles = None
648 
649  if not resolution is None:
650  resolution_particles = []
651  hs = representation.get_hierarchies_at_given_resolution(resolution)
652  for h in hs:
653  resolution_particles += IMP.atom.get_leaves(h)
654 
655  if not hierarchies is None:
656  hierarchies_particles = []
657  for h in hierarchies:
658  hierarchies_particles += IMP.atom.get_leaves(h)
659 
660  if not name is None:
661  names_particles = []
662  if name_is_ambiguous:
663  for namekey in representation.hier_dict:
664  if name in namekey:
665  names_particles += IMP.atom.get_leaves(
666  representation.hier_dict[namekey])
667  elif name in representation.hier_dict:
668  names_particles += IMP.atom.get_leaves(representation.hier_dict[name])
669  else:
670  print "select: component %s is not there" % name
671 
672  if not first_residue is None and not last_residue is None:
673  sel = IMP.atom.Selection(representation.prot,
674  residue_indexes=range(first_residue, last_residue + 1))
675  residue_range_particles = [IMP.atom.Hierarchy(p)
676  for p in sel.get_selected_particles()]
677 
678  if not residue is None:
679  sel = IMP.atom.Selection(representation.prot, residue_index=residue)
680  residue_particles = [IMP.atom.Hierarchy(p)
681  for p in sel.get_selected_particles()]
682 
683  if not representation_type is None:
684  representation_type_particles = []
685  if representation_type == "Molecule":
686  for name in representation.hier_representation:
687  for repr_type in representation.hier_representation[name]:
688  if repr_type == "Beads" or "Res:" in repr_type:
689  h = representation.hier_representation[name][repr_type]
690  representation_type_particles += IMP.atom.get_leaves(h)
691 
692  elif representation_type == "PDB":
693  for name in representation.hier_representation:
694  for repr_type in representation.hier_representation[name]:
695  if repr_type == "Res:" in repr_type:
696  h = representation.hier_representation[name][repr_type]
697  representation_type_particles += IMP.atom.get_leaves(h)
698 
699  else:
700  for name in representation.hier_representation:
701  h = representation.hier_representation[
702  name][
703  representation_type]
704  representation_type_particles += IMP.atom.get_leaves(h)
705 
706  selections = [hierarchies_particles, names_particles,
707  residue_range_particles, residue_particles, representation_type_particles]
708 
709  if resolution is None:
710  selected_particles = set(allparticles)
711  else:
712  selected_particles = set(resolution_particles)
713 
714  for s in selections:
715  if not s is None:
716  selected_particles = (set(s) & selected_particles)
717 
718  return list(selected_particles)
719 
720 
721 def select_by_tuple(
722  representation,
723  tupleselection,
724  resolution=None,
725  name_is_ambiguous=False):
726  if isinstance(tupleselection, tuple) and len(tupleselection) == 3:
727  particles = IMP.pmi.tools.select(representation, resolution=resolution,
728  name=tupleselection[2],
729  first_residue=tupleselection[0],
730  last_residue=tupleselection[1],
731  name_is_ambiguous=name_is_ambiguous)
732  elif isinstance(tupleselection, str):
733  particles = IMP.pmi.tools.select(representation, resolution=resolution,
734  name=tupleselection,
735  name_is_ambiguous=name_is_ambiguous)
736  else:
737  raise ValueError('you passed something bad to select_by_tuple()')
738  # now order the result by residue number
739  particles = IMP.pmi.tools.sort_by_residues(particles)
740 
741  return particles
742 
743 
744 def get_db_from_csv(csvfilename):
745  import csv
746  outputlist = []
747  csvr = csv.DictReader(open(csvfilename, "rU"))
748  for l in csvr:
749  outputlist.append(l)
750  return outputlist
751 
752 
753 class HierarchyDatabase(object):
754 
755  def __init__(self):
756  self.db = {}
757  # this dictionary map a particle to its root hierarchy
758  self.root_hierarchy_dict = {}
759  self.preroot_fragment_hierarchy_dict = {}
760  self.particle_to_name = {}
761  self.model = None
762 
763  def add_name(self, name):
764  if name not in self.db:
765  self.db[name] = {}
766 
767  def add_residue_number(self, name, resn):
768  resn = int(resn)
769  self.add_name(name)
770  if resn not in self.db[name]:
771  self.db[name][resn] = {}
772 
773  def add_resolution(self, name, resn, resolution):
774  resn = int(resn)
775  resolution = float(resolution)
776  self.add_name(name)
777  self.add_residue_number(name, resn)
778  if resolution not in self.db[name][resn]:
779  self.db[name][resn][resolution] = []
780 
781  def add_particles(self, name, resn, resolution, particles):
782  resn = int(resn)
783  resolution = float(resolution)
784  self.add_name(name)
785  self.add_residue_number(name, resn)
786  self.add_resolution(name, resn, resolution)
787  self.db[name][resn][resolution] += particles
788  for p in particles:
789  (rh, prf) = self.get_root_hierarchy(p)
790  self.root_hierarchy_dict[p] = rh
791  self.preroot_fragment_hierarchy_dict[p] = prf
792  self.particle_to_name[p] = name
793  if self.model is None:
794  self.model = particles[0].get_model()
795 
796  def get_model(self):
797  return self.model
798 
799  def get_names(self):
800  names = self.db.keys()
801  names.sort()
802  return names
803 
804  def get_particles(self, name, resn, resolution):
805  resn = int(resn)
806  resolution = float(resolution)
807  return self.db[name][resn][resolution]
808 
809  def get_particles_at_closest_resolution(self, name, resn, resolution):
810  resn = int(resn)
811  resolution = float(resolution)
812  closestres = min(self.get_residue_resolutions(name, resn),
813  key=lambda x: abs(float(x) - float(resolution)))
814  return self.get_particles(name, resn, closestres)
815 
816  def get_residue_resolutions(self, name, resn):
817  resn = int(resn)
818  resolutions = self.db[name][resn].keys()
819  resolutions.sort()
820  return resolutions
821 
822  def get_molecule_resolutions(self, name):
823  resolutions = set()
824  for resn in self.db[name]:
825  resolutions.update(self.db[name][resn].keys())
826  resolutions.sort()
827  return resolutions
828 
829  def get_residue_numbers(self, name):
830  residue_numbers = self.db[name].keys()
831  residue_numbers.sort()
832  return residue_numbers
833 
834  def get_particles_by_resolution(self, name, resolution):
835  resolution = float(resolution)
836  particles = []
837  for resn in self.get_residue_numbers(name):
838  result = self.get_particles_at_closest_resolution(
839  name,
840  resn,
841  resolution)
842  pstemp = [p for p in result if p not in particles]
843  particles += pstemp
844  return particles
845 
846  def get_all_particles_by_resolution(self, resolution):
847  resolution = float(resolution)
848  particles = []
849  for name in self.get_names():
850  particles += self.get_particles_by_resolution(name, resolution)
851  return particles
852 
853  def get_root_hierarchy(self, particle):
854  prerootfragment = particle
855  while IMP.atom.Atom.get_is_setup(particle) or \
856  IMP.atom.Residue.get_is_setup(particle) or \
858  if IMP.atom.Atom.get_is_setup(particle):
859  p = IMP.atom.Atom(particle).get_parent()
860  elif IMP.atom.Residue.get_is_setup(particle):
861  p = IMP.atom.Residue(particle).get_parent()
862  elif IMP.atom.Fragment.get_is_setup(particle):
863  p = IMP.atom.Fragment(particle).get_parent()
864  prerootfragment = particle
865  particle = p
866  return (
867  (IMP.atom.Hierarchy(particle), IMP.atom.Hierarchy(prerootfragment))
868  )
869 
870  def get_all_root_hierarchies_by_resolution(self, resolution):
871  hierarchies = []
872  resolution = float(resolution)
873  particles = self.get_all_particles_by_resolution(resolution)
874  for p in particles:
875  rh = self.root_hierarchy_dict[p]
876  if rh not in hierarchies:
877  hierarchies.append(IMP.atom.Hierarchy(rh))
878  return hierarchies
879 
880  def get_preroot_fragments_by_resolution(self, name, resolution):
881  fragments = []
882  resolution = float(resolution)
883  particles = self.get_particles_by_resolution(name, resolution)
884  for p in particles:
885  fr = self.preroot_fragment_hierarchy_dict[p]
886  if fr not in fragments:
887  fragments.append(fr)
888  return fragments
889 
890  def show(self, name):
891  print name
892  for resn in self.get_residue_numbers(name):
893  print resn
894  for resolution in self.get_residue_resolutions(name, resn):
895  print "----", resolution
896  for p in self.get_particles(name, resn, resolution):
897  print "--------", p.get_name()
898 
899 
900 def get_prot_name_from_particle(p, list_of_names):
901  ''' this function returns the component name provided a particle and a list of names'''
902  root = p
903  protname = root.get_name()
904  is_a_bead = False
905  while not protname in list_of_names:
906  root0 = root.get_parent()
907  if root0 == IMP.atom.Hierarchy():
908  return (None, None)
909  protname = root0.get_name()
910 
911  # check if that is a bead
912  # this piece of code might be dangerous if
913  # the hierarchy was called Bead :)
914  if "Beads" in protname:
915  is_a_bead = True
916  root = root0
917  return (protname, is_a_bead)
918 
919 
921  '''
922  This "overloaded" function retrieves the residue indexes
923  for each particle which is an instance of Fragment,Residue or Atom
924  '''
925  resind = []
927  resind = IMP.atom.Fragment(hier).get_residue_indexes()
929  resind = [IMP.atom.Residue(hier).get_index()]
930  elif IMP.atom.Atom.get_is_setup(hier):
931  a = IMP.atom.Atom(hier)
932  resind = [IMP.atom.Residue(a.get_parent()).get_index()]
933  else:
934  print "get_residue_indexes> input is not Fragment, Residue or Atom"
935  resind = []
936  return resind
937 
938 
939 def sort_by_residues(particles):
940  particles_residues = [(p, IMP.pmi.tools.get_residue_indexes(p))
941  for p in particles]
942  sorted_particles_residues = sorted(
943  particles_residues,
944  key=lambda tup: tup[1])
945  particles = [p[0] for p in sorted_particles_residues]
946  return particles
947 
948 
949 def get_residue_to_particle_map(particles):
950  # this function returns a dictionary that map particles to residue indexes
951  particles = sort_by_residues(particles)
952  particles_residues = [(p, IMP.pmi.tools.get_residue_indexes(p))
953  for p in particles]
954  return dict(zip(particles_residues, particles))
955 
956 #
957 # Parallel Computation
958 #
959 
960 
962  """Synchronize data over a parallel run"""
963  from mpi4py import MPI
964  comm = MPI.COMM_WORLD
965  rank = comm.Get_rank()
966  number_of_processes = comm.size
967  comm.Barrier()
968  if rank != 0:
969  comm.send(data, dest=0, tag=11)
970 
971  elif rank == 0:
972  for i in range(1, number_of_processes):
973  data_tmp = comm.recv(source=i, tag=11)
974  if type(data) == list:
975  data += data_tmp
976  elif type(data) == dict:
977  data.update(data_tmp)
978  else:
979  raise TypeError("data not supported, use list or dictionaries")
980 
981  for i in range(1, number_of_processes):
982  comm.send(data, dest=i, tag=11)
983 
984  if rank != 0:
985  data = comm.recv(source=0, tag=11)
986  return data
987 
989  """Synchronize data over a parallel run"""
990  from mpi4py import MPI
991  comm = MPI.COMM_WORLD
992  rank = comm.Get_rank()
993  number_of_processes = comm.size
994  comm.Barrier()
995  if rank != 0:
996  comm.send(data, dest=0, tag=11)
997  elif rank == 0:
998  for i in range(1, number_of_processes):
999  data_tmp = comm.recv(source=i, tag=11)
1000  for k in data:
1001  data[k]+=data_tmp[k]
1002 
1003  for i in range(1, number_of_processes):
1004  comm.send(data, dest=i, tag=11)
1005 
1006  if rank != 0:
1007  data = comm.recv(source=0, tag=11)
1008  return data
1009 
1010 
1011 #
1012 ### Lists and iterators
1013 #
1014 
1015 def sublist_iterator(l, lmin=None, lmax=None):
1016  '''
1017  this iterator yields all sublists
1018  of length >= lmin and <= lmax
1019  '''
1020  if lmin is None:
1021  lmin = 0
1022  if lmax is None:
1023  lmax = len(l)
1024  n = len(l) + 1
1025  for i in xrange(n):
1026  for j in xrange(i + 1, n):
1027  if len(l[i:j]) <= lmax and len(l[i:j]) >= lmin:
1028  yield l[i:j]
1029 
1030 
1031 def flatten_list(l):
1032  return [item for sublist in l for item in sublist]
1033 
1034 
1035 def list_chunks_iterator(list, length):
1036  """ Yield successive length-sized chunks from a list.
1037  """
1038  for i in xrange(0, len(list), length):
1039  yield list[i:i + length]
1040 
1041 
1042 def chunk_list_into_segments(seq, num):
1043  avg = len(seq) / float(num)
1044  out = []
1045  last = 0.0
1046 
1047  while last < len(seq):
1048  out.append(seq[int(last):int(last + avg)])
1049  last += avg
1050 
1051  return out
1052 
1053 #
1054 # COORDINATE MANIPULATION
1055 #
1056 
1057 
1058 def translate_hierarchy(hierarchy, translation_vector):
1059  '''
1060  this will apply a translation to a hierarchy along the input vector
1061  '''
1062  rbs = set()
1063  xyzs = set()
1064  if type(translation_vector) == list:
1065  transformation = IMP.algebra.Transformation3D(
1066  IMP.algebra.Vector3D(translation_vector))
1067  else:
1068  transformation = IMP.algebra.Transformation3D(translation_vector)
1069  for p in IMP.atom.get_leaves(hierarchy):
1071  rbs.add(IMP.core.RigidBody(p))
1073  rb = IMP.core.RigidMember(p).get_rigid_body()
1074  rbs.add(rb)
1075  else:
1076  xyzs.add(p)
1077  for xyz in xyzs:
1078  IMP.core.transform(IMP.core.XYZ(xyz), transformation)
1079  for rb in rbs:
1080  IMP.core.transform(rb, transformation)
1081 
1082 
1083 def translate_hierarchies(hierarchies, translation_vector):
1084  for h in hierarchies:
1085  IMP.pmi.tools.translate_hierarchy(h, translation_vector)
1086 
1087 
1088 def translate_hierarchies_to_reference_frame(hierarchies):
1089  xc = 0
1090  yc = 0
1091  zc = 0
1092  nc = 0
1093  for h in hierarchies:
1094  for p in IMP.atom.get_leaves(h):
1095  coor = IMP.core.XYZ(p).get_coordinates()
1096  nc += 1
1097  xc += coor[0]
1098  yc += coor[1]
1099  zc += coor[2]
1100  xc = xc / nc
1101  yc = yc / nc
1102  zc = zc / nc
1103  IMP.pmi.tools.translate_hierarchies(hierarchies, (-xc, -yc, -zc))
1104 
1105 
1106 #
1107 # Tools to simulate data
1108 #
1109 
1110 def normal_density_function(expected_value, sigma, x):
1111  return (
1112  1 / math.sqrt(2 * math.pi) / sigma *
1113  math.exp(-(x - expected_value) ** 2 / 2 / sigma / sigma)
1114  )
1115 
1116 
1117 def log_normal_density_function(expected_value, sigma, x):
1118  return (
1119  1 / math.sqrt(2 * math.pi) / sigma / x *
1120  math.exp(-(math.log(x / expected_value) ** 2 / 2 / sigma / sigma))
1121  )
1122 
1123 
1124 def get_random_residue_pairs(representation, resolution,
1125  number,
1126  avoid_same_particles=False,
1127  names=None):
1128 
1129  from random import choice
1130  particles = []
1131  if names is None:
1132  names=representation.hier_dict.keys()
1133 
1134  for name in names:
1135  prot = representation.hier_dict[name]
1136  particles += select(representation,name=name,resolution=resolution)
1137  random_residue_pairs = []
1138  while len(random_residue_pairs)<=number:
1139  p1 = choice(particles)
1140  p2 = choice(particles)
1141  if p1==p2 and avoid_same_particles: continue
1142  r1 = choice(IMP.pmi.tools.get_residue_indexes(p1))
1143  r2 = choice(IMP.pmi.tools.get_residue_indexes(p2))
1144  name1 = representation.get_prot_name_from_particle(p1)
1145  name2 = representation.get_prot_name_from_particle(p2)
1146  random_residue_pairs.append((name1, r1, name2, r2))
1147 
1148  return random_residue_pairs
1149 
1150 
1151 def get_random_data_point(
1152  expected_value,
1153  ntrials,
1154  sensitivity,
1155  sigma,
1156  outlierprob,
1157  begin_end_nbins_tuple,
1158  log=False,
1159  loggrid=False):
1160  import random
1161 
1162  begin = begin_end_nbins_tuple[0]
1163  end = begin_end_nbins_tuple[1]
1164  nbins = begin_end_nbins_tuple[2]
1165 
1166  if not loggrid:
1167  fmod_grid = get_grid(begin, end, nbins, True)
1168  else:
1169  fmod_grid = get_log_grid(begin, end, nbins)
1170 
1171  norm = 0
1172  cumul = []
1173  cumul.append(0)
1174 
1175  a = []
1176  for i in range(0, ntrials):
1177  a.append([random.random(), True])
1178 
1179  if sigma != 0.0:
1180  for j in range(1, len(fmod_grid)):
1181  fj = fmod_grid[j]
1182  fjm1 = fmod_grid[j - 1]
1183  df = fj - fjm1
1184 
1185  if not log:
1186  pj = normal_density_function(expected_value, sigma, fj)
1187  pjm1 = normal_density_function(expected_value, sigma, fjm1)
1188  else:
1189  pj = log_normal_density_function(expected_value, sigma, fj)
1190  pjm1 = log_normal_density_function(expected_value, sigma, fjm1)
1191 
1192  norm += (pj + pjm1) / 2.0 * df
1193  cumul.append(norm)
1194  # print fj, pj
1195 
1196  random_points = []
1197 
1198  for i in range(len(cumul)):
1199  # print i,a, cumul[i], norm
1200  for aa in a:
1201  if (aa[0] <= cumul[i] / norm and aa[1]):
1202  random_points.append(
1203  int(fmod_grid[i] / sensitivity) * sensitivity)
1204  aa[1] = False
1205 
1206  else:
1207  random_points = [expected_value] * ntrials
1208 
1209  for i in range(len(random_points)):
1210  if random.random() < outlierprob:
1211  a = random.uniform(begin, end)
1212  random_points[i] = int(a / sensitivity) * sensitivity
1213  print random_points
1214  '''
1215  for i in range(ntrials):
1216  if random.random() > OUTLIERPROB_:
1217  r=truncnorm.rvs(0.0,1.0,expected_value,BETA_)
1218  if r>1.0: print r,expected_value,BETA_
1219  else:
1220  r=random.random()
1221  random_points.append(int(r/sensitivity)*sensitivity)
1222  '''
1223 
1224  rmean = 0.
1225  rmean2 = 0.
1226  for r in random_points:
1227  rmean += r
1228  rmean2 += r * r
1229 
1230  rmean /= float(ntrials)
1231  rmean2 /= float(ntrials)
1232  stddev = math.sqrt(max(rmean2 - rmean * rmean, 0.))
1233  return rmean, stddev
1234 
1235 is_already_printed = {}
1236 
1237 
1238 def print_deprecation_warning(old_name, new_name):
1239  if old_name not in is_already_printed:
1240  print "WARNING: " + old_name + " is deprecated, use " + new_name + " instead"
1241  is_already_printed[old_name] = True
1242 
1243 
1244 def print_multicolumn(list_of_strings, ncolumns=2, truncate=40):
1245 
1246  l = list_of_strings
1247 
1248  cols = ncolumns
1249  # add empty entries after l
1250  for i in range(len(l) % cols):
1251  l.append(" ")
1252 
1253  split = [l[i:i + len(l) / cols] for i in range(0, len(l), len(l) / cols)]
1254  for row in zip(*split):
1255  print "".join(str.ljust(i, truncate) for i in row)
1256 
1257 
1258 def parse_dssp(dssp_fn, limit_to_chains=''):
1259  ''' read dssp file, get SSEs. values are all PDB residue numbering. returns dict of sel tuples
1260 helix : [ [ ['A',5,7] ] , [['B',15,17]] , ...] two helices A:5-7,B:15-17
1261 beta : [ [ ['A',1,3] , ['A',100,102] ] , ...] one sheet: A:1-3 & A:100-102
1262 loop : same format as helix, it's the contiguous loops
1263 '''
1264 
1265  from collections import defaultdict
1266 
1267  # setup
1268  sses = {'helix': [],
1269  'beta': [],
1270  'loop': []}
1271  helix_classes = 'GHI'
1272  strand_classes = 'EB'
1273  loop_classes = [' ', '', 'T', 'S']
1274  sse_dict = {}
1275  for h in helix_classes:
1276  sse_dict[h] = 'helix'
1277  for s in strand_classes:
1278  sse_dict[s] = 'beta'
1279  for l in loop_classes:
1280  sse_dict[l] = 'loop'
1281 
1282  # read file and parse
1283  start = False
1284  # temporary beta dictionary indexed by DSSP's ID
1285  beta_dict = defaultdict(list)
1286  prev_sstype = None
1287  cur_sse = []
1288  prev_beta_id = None
1289  for line in open(dssp_fn, 'r'):
1290  fields = line.split()
1291  chain_break = False
1292  if len(fields) < 2:
1293  continue
1294  if fields[1] == "RESIDUE":
1295  # Start parsing from here
1296  start = True
1297  continue
1298  if not start:
1299  continue
1300  if line[9] == " ":
1301  chain_break = True
1302  elif limit_to_chains != '' and line[11] not in limit_to_chains:
1303  break
1304 
1305  # gather line info
1306  if not chain_break:
1307  pdb_res_num = int(line[5:10])
1308  chain = 'chain' + line[11]
1309  sstype = line[16]
1310  beta_id = line[33]
1311 
1312  # decide whether to extend or store the SSE
1313  if prev_sstype is None:
1314  cur_sse = [pdb_res_num, pdb_res_num, chain]
1315  elif sstype != prev_sstype or chain_break:
1316  # add cur_sse to the right place
1317  if sse_dict[prev_sstype] in ['helix', 'loop']:
1318  sses[sse_dict[prev_sstype]].append([cur_sse])
1319  if sse_dict[prev_sstype] == 'beta':
1320  beta_dict[prev_beta_id].append(cur_sse)
1321  cur_sse = [pdb_res_num, pdb_res_num, chain]
1322  else:
1323  cur_sse[1] = pdb_res_num
1324  if chain_break:
1325  prev_sstype = None
1326  prev_beta_id = None
1327  else:
1328  prev_sstype = sstype
1329  prev_beta_id = beta_id
1330 
1331  # final SSE processing
1332  if not prev_sstype is None:
1333  if sse_dict[prev_sstype] in ['helix', 'loop']:
1334  sses[sse_dict[prev_sstype]].append([cur_sse])
1335  if sse_dict[prev_sstype] == 'beta':
1336  beta_dict[prev_beta_id].append(cur_sse)
1337 
1338  # gather betas
1339  for beta_sheet in beta_dict:
1340  sses['beta'].append(beta_dict[beta_sheet])
1341 
1342  return sses
1343 
1344 
1345 def sse_selections_to_chimera_colors(dssp_dict, chimera_model_num=0):
1346  ''' get chimera command to check if you've correctly made the dssp dictionary
1347  colors each helix and beta sheet'''
1348  cmds = {
1349  'helix': 'color green ',
1350  'beta': 'color blue ',
1351  'loop': 'color red '}
1352  for skey in dssp_dict.keys():
1353  for sgroup in dssp_dict[skey]:
1354  for sse in sgroup:
1355  start, stop, chain = sse
1356  chain = chain.strip('chain')
1357  cmds[
1358  skey] += '#%i:%s-%s.%s ' % (chimera_model_num, start, stop, chain)
1359  print '; '.join([cmds[k] for k in cmds])
1360 
1361 
1362 
1363 
1364 class ColorChange(object):
1365  '''a class to change color code to hexadecimal to rgb'''
1366  def __init__(self):
1367  self._NUMERALS = '0123456789abcdefABCDEF'
1368  self._HEXDEC = dict((v, int(v, 16)) for v in (x+y for x in self._NUMERALS for y in self._NUMERALS))
1369  self.LOWERCASE, self.UPPERCASE = 'x', 'X'
1370 
1371  def rgb(self,triplet):
1372  return float(self._HEXDEC[triplet[0:2]]), float(self._HEXDEC[triplet[2:4]]), float(self._HEXDEC[triplet[4:6]])
1373 
1374  def triplet(self,rgb, lettercase=None):
1375  if lettercase is None: lettercase=self.LOWERCASE
1376  return format(rgb[0]<<16 | rgb[1]<<8 | rgb[2], '06'+lettercase)
1377 
1378 
1379 class OrderedSet(collections.MutableSet):
1380 
1381  def __init__(self, iterable=None):
1382  self.end = end = []
1383  end += [None, end, end] # sentinel node for doubly linked list
1384  self.map = {} # key --> [key, prev, next]
1385  if iterable is not None:
1386  self |= iterable
1387 
1388  def __len__(self):
1389  return len(self.map)
1390 
1391  def __contains__(self, key):
1392  return key in self.map
1393 
1394  def add(self, key):
1395  if key not in self.map:
1396  end = self.end
1397  curr = end[1]
1398  curr[2] = end[1] = self.map[key] = [key, curr, end]
1399 
1400  def discard(self, key):
1401  if key in self.map:
1402  key, prev, next = self.map.pop(key)
1403  prev[2] = next
1404  next[1] = prev
1405 
1406  def __iter__(self):
1407  end = self.end
1408  curr = end[2]
1409  while curr is not end:
1410  yield curr[0]
1411  curr = curr[2]
1412 
1413  def __reversed__(self):
1414  end = self.end
1415  curr = end[1]
1416  while curr is not end:
1417  yield curr[0]
1418  curr = curr[1]
1419 
1420  def pop(self, last=True):
1421  if not self:
1422  raise KeyError('set is empty')
1423  if last:
1424  key = self.end[1][0]
1425  else:
1426  key = self.end[2][0]
1427  self.discard(key)
1428  return key
1429 
1430  def __repr__(self):
1431  if not self:
1432  return '%s()' % (self.__class__.__name__,)
1433  return '%s(%r)' % (self.__class__.__name__, list(self))
1434 
1435  def __eq__(self, other):
1436  if isinstance(other, OrderedSet):
1437  return len(self) == len(other) and list(self) == list(other)
1438  return set(self) == set(other)
def list_chunks_iterator
Yield successive length-sized chunks from a list.
Definition: tools.py:1035
Simple 3D transformation class.
A decorator to associate a particle with a part of a protein/DNA/RNA.
Definition: Fragment.h:20
static Weight setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: Weight.h:31
Ints get_index(const kernel::ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
def get_random_cross_link_dataset
returns a random cross-link dataset into a string were every line is a residue pair, together with UniqueIdentifier and XL score
Definition: tools.py:312
def get_position_terminal_residue
this function get the xyz position of the C or N terminal residue of a hierarchy, given the resolutio...
Definition: tools.py:521
double get_drmsd(const Vector3DsOrXYZs0 &m0, const Vector3DsOrXYZs1 &m1)
Calculate distance the root mean square deviation between two sets of 3D points.
Definition: atom/distance.h:88
def scatter_and_gather_dict_append
Synchronize data over a parallel run.
Definition: tools.py:988
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
double get_resolution(kernel::Model *m, kernel::ParticleIndex pi)
a class to change color code to hexadecimal to rgb
Definition: tools.py:1364
static bool get_is_setup(const IMP::kernel::ParticleAdaptor &p)
Definition: rigid_bodies.h:157
static Scale setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: Scale.h:30
log
Definition: log.py:1
def get_prot_name_from_particle
this function returns the component name provided a particle and a list of names
Definition: tools.py:900
double get_distance(XYZR a, XYZR b)
Compute the sphere distance between a and b.
Definition: XYZR.h:87
def get_residue_gaps_in_hierarchy
returns the residue index gaps and contiguous segments as tuples given the hierarchy, the first residue and the last residue indexes.
Definition: tools.py:552
Add resolution to a particle.
Definition: Resolution.h:23
def sse_selections_to_chimera_colors
get chimera command to check if you've correctly made the dssp dictionary colors each helix and beta ...
Definition: tools.py:1345
algebra::GridD< 3, algebra::DenseGridStorageD< 3, float >, float > get_grid(DensityMap *in_map)
def translate_hierarchy
this will apply a translation to a hierarchy along the input vector
Definition: tools.py:1058
The standard decorator for manipulating molecular structures.
A decorator for a particle representing an atom.
Definition: atom/Atom.h:234
def scatter_and_gather
Synchronize data over a parallel run.
Definition: tools.py:961
void transform(XYZ a, const algebra::Transformation3D &tr)
Apply a transformation to the particle.
Hierarchies get_by_type(Hierarchy mhd, GetByType t)
void add_particles(RMF::FileHandle fh, const kernel::ParticlesTemp &hs)
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
Class to handle individual model particles.
def get_closest_residue_position
this function works with plain hierarchies, as read from the pdb, no multi-scale hierarchies ...
Definition: tools.py:490
static bool get_is_setup(const IMP::kernel::ParticleAdaptor &p)
Definition: atom/Atom.h:241
A decorator for a residue.
Definition: Residue.h:134
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
static bool get_is_setup(const IMP::kernel::ParticleAdaptor &p)
Definition: Residue.h:155
static bool get_is_setup(kernel::Model *m, kernel::ParticleIndex pi)
Definition: Fragment.h:46
static bool get_is_setup(const IMP::kernel::ParticleAdaptor &p)
Definition: rigid_bodies.h:479
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
def parse_dssp
read dssp file, get SSEs.
Definition: tools.py:1258
VectorD< 3 > Vector3D
Definition: VectorD.h:395
std::string get_data_path(std::string file_name)
Return the full path to installed data.
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: tools.py:624
A decorator for a rigid body.
Definition: rigid_bodies.h:75
def cross_link_db_filter_parser
example '"{ID_Score}" > 28 AND "{Sample}" == "%10_1%" OR ":Sample}" == "%10_2%" OR ":Sample...
Definition: tools.py:422
void add_particle(RMF::FileHandle fh, kernel::Particle *hs)
Hierarchies get_leaves(const Selection &h)
Select hierarchy particles identified by the biological name.
Definition: Selection.h:62
def get_residue_indexes
This "overloaded" function retrieves the residue indexes for each particle which is an instance of Fr...
Definition: tools.py:920
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...
def sublist_iterator
this iterator yields all sublists of length >= lmin and <= lmax
Definition: tools.py:1015