IMP logo
IMP Reference Guide  2.10.1
The Integrative Modeling Platform
output.py
1 """@namespace IMP.pmi.output
2  Classes for writing output files and processing them.
3 """
4 
5 from __future__ import print_function, division
6 import IMP
7 import IMP.atom
8 import IMP.core
9 import IMP.pmi
10 import IMP.pmi.tools
11 import IMP.pmi.io
12 import os
13 import sys
14 import ast
15 import RMF
16 import numpy as np
17 import operator
18 import string
19 try:
20  import cPickle as pickle
21 except ImportError:
22  import pickle
23 
24 class _ChainIDs(object):
25  """Map indices to multi-character chain IDs.
26  We label the first 26 chains A-Z, then we move to two-letter
27  chain IDs: AA through AZ, then BA through BZ, through to ZZ.
28  This continues with longer chain IDs."""
29  def __getitem__(self, ind):
30  chars = string.ascii_uppercase
31  lc = len(chars)
32  ids = []
33  while ind >= lc:
34  ids.append(chars[ind % lc])
35  ind = ind // lc - 1
36  ids.append(chars[ind])
37  return "".join(reversed(ids))
38 
39 
40 class ProtocolOutput(object):
41  """Base class for capturing a modeling protocol.
42  Unlike simple output of model coordinates, a complete
43  protocol includes the input data used, details on the restraints,
44  sampling, and clustering, as well as output models.
45  Use via IMP.pmi.representation.Representation.add_protocol_output()
46  (for PMI 1) or
47  IMP.pmi.topology.System.add_protocol_output() (for PMI 2).
48 
49  @see IMP.pmi.mmcif.ProtocolOutput for a concrete subclass that outputs
50  mmCIF files.
51  """
52  pass
53 
54 def _flatten(seq):
55  for elt in seq:
56  if isinstance(elt, (tuple, list)):
57  for elt2 in _flatten(elt):
58  yield elt2
59  else:
60  yield elt
61 
62 class Output(object):
63  """Class for easy writing of PDBs, RMFs, and stat files
64 
65  @note Model should be updated prior to writing outputs.
66  """
67  def __init__(self, ascii=True,atomistic=False):
68  self.dictionary_pdbs = {}
69  self.dictionary_rmfs = {}
70  self.dictionary_stats = {}
71  self.dictionary_stats2 = {}
72  self.best_score_list = None
73  self.nbestscoring = None
74  self.suffixes = []
75  self.replica_exchange = False
76  self.ascii = ascii
77  self.initoutput = {}
78  self.residuetypekey = IMP.StringKey("ResidueName")
79  # 1-character chain IDs, suitable for PDB output
80  self.chainids = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
81  # Multi-character chain IDs, suitable for mmCIF output
82  self.multi_chainids = _ChainIDs()
83  self.dictchain = {} # keys are molecule names, values are chain ids
84  self.particle_infos_for_pdb = {}
85  self.atomistic=atomistic
86  self.use_pmi2 = False
87 
88  def get_pdb_names(self):
89  """Get a list of all PDB files being output by this instance"""
90  return list(self.dictionary_pdbs.keys())
91 
92  def get_rmf_names(self):
93  return list(self.dictionary_rmfs.keys())
94 
95  def get_stat_names(self):
96  return list(self.dictionary_stats.keys())
97 
98  def _init_dictchain(self, name, prot, multichar_chain=False):
99  self.dictchain[name] = {}
100  self.use_pmi2 = False
101 
102  # attempt to find PMI objects.
103  if IMP.pmi.get_is_canonical(prot):
104  self.use_pmi2 = True
105  self.atomistic = True #detects automatically
106  for n,mol in enumerate(IMP.atom.get_by_type(prot,IMP.atom.MOLECULE_TYPE)):
107  chid = IMP.atom.Chain(mol).get_id()
108  self.dictchain[name][IMP.pmi.get_molecule_name_and_copy(mol)] = chid
109  else:
110  chainids = self.multi_chainids if multichar_chain else self.chainids
111  for n, i in enumerate(self.dictionary_pdbs[name].get_children()):
112  self.dictchain[name][i.get_name()] = chainids[n]
113 
114  def init_pdb(self, name, prot):
115  """Init PDB Writing.
116  @param name The PDB filename
117  @param prot The hierarchy to write to this pdb file
118  @note if the PDB name is 'System' then will use Selection to get molecules
119  """
120  flpdb = open(name, 'w')
121  flpdb.close()
122  self.dictionary_pdbs[name] = prot
123  self._init_dictchain(name, prot)
124 
125  def write_psf(self,filename,name):
126  flpsf=open(filename,'w')
127  flpsf.write("PSF CMAP CHEQ"+"\n")
128  index_residue_pair_list={}
129  (particle_infos_for_pdb, geometric_center)=self.get_particle_infos_for_pdb_writing(name)
130  nparticles=len(particle_infos_for_pdb)
131  flpsf.write(str(nparticles)+" !NATOM"+"\n")
132  for n,p in enumerate(particle_infos_for_pdb):
133  atom_index=n+1
134  residue_type=p[2]
135  chain=p[3]
136  resid=p[4]
137  flpsf.write('{0:8d}{1:1s}{2:4s}{3:1s}{4:4s}{5:1s}{6:4s}{7:1s}{8:4s}{9:1s}{10:4s}{11:14.6f}{12:14.6f}{13:8d}{14:14.6f}{15:14.6f}'.format(atom_index," ",chain," ",str(resid)," ",'"'+residue_type.get_string()+'"'," ","C"," ","C",1.0,0.0,0,0.0,0.0))
138  flpsf.write('\n')
139  #flpsf.write(str(atom_index)+" "+str(chain)+" "+str(resid)+" "+str(residue_type).replace('"','')+" C C "+"1.0 0.0 0 0.0 0.0\n")
140  if chain not in index_residue_pair_list:
141  index_residue_pair_list[chain]=[(atom_index,resid)]
142  else:
143  index_residue_pair_list[chain].append((atom_index,resid))
144 
145 
146  #now write the connectivity
147  indexes_pairs=[]
148  for chain in sorted(index_residue_pair_list.keys()):
149 
150  ls=index_residue_pair_list[chain]
151  #sort by residue
152  ls=sorted(ls, key=lambda tup: tup[1])
153  #get the index list
154  indexes=[x[0] for x in ls]
155  # get the contiguous pairs
156  indexes_pairs+=list(IMP.pmi.tools.sublist_iterator(indexes,lmin=2,lmax=2))
157  nbonds=len(indexes_pairs)
158  flpsf.write(str(nbonds)+" !NBOND: bonds"+"\n")
159 
160  # save bonds in fixed column format
161  for i in range(0,len(indexes_pairs),4):
162  for bond in indexes_pairs[i:i+4]:
163  flpsf.write('{0:8d}{1:8d}'.format(*bond))
164  flpsf.write('\n')
165 
166  del particle_infos_for_pdb
167  flpsf.close()
168 
169  def write_pdb(self,name,
170  appendmode=True,
171  translate_to_geometric_center=False,
172  write_all_residues_per_bead=False):
173  if appendmode:
174  flpdb = open(name, 'a')
175  else:
176  flpdb = open(name, 'w')
177 
178  (particle_infos_for_pdb,
179  geometric_center) = self.get_particle_infos_for_pdb_writing(name)
180 
181  if not translate_to_geometric_center:
182  geometric_center = (0, 0, 0)
183 
184  for n,tupl in enumerate(particle_infos_for_pdb):
185  (xyz, atom_type, residue_type,
186  chain_id, residue_index, all_indexes, radius) = tupl
187  if atom_type is None:
188  atom_type = IMP.atom.AT_CA
189  if ( (write_all_residues_per_bead) and (all_indexes is not None) ):
190  for residue_number in all_indexes:
191  flpdb.write(IMP.atom.get_pdb_string((xyz[0] - geometric_center[0],
192  xyz[1] - geometric_center[1],
193  xyz[2] - geometric_center[2]),
194  n+1, atom_type, residue_type,
195  chain_id, residue_number,' ',1.00,radius))
196  else:
197  flpdb.write(IMP.atom.get_pdb_string((xyz[0] - geometric_center[0],
198  xyz[1] - geometric_center[1],
199  xyz[2] - geometric_center[2]),
200  n+1, atom_type, residue_type,
201  chain_id, residue_index,' ',1.00,radius))
202  flpdb.write("ENDMDL\n")
203  flpdb.close()
204 
205  del particle_infos_for_pdb
206 
207  def get_prot_name_from_particle(self, name, p):
208  """Get the protein name from the particle.
209  This is done by traversing the hierarchy."""
210  if self.use_pmi2:
211  return IMP.pmi.get_molecule_name_and_copy(p), True
212  else:
214  p, self.dictchain[name])
215 
216  def get_particle_infos_for_pdb_writing(self, name):
217  # index_residue_pair_list={}
218 
219  # the resindexes dictionary keep track of residues that have been already
220  # added to avoid duplication
221  # highest resolution have highest priority
222  resindexes_dict = {}
223 
224  # this dictionary dill contain the sequence of tuples needed to
225  # write the pdb
226  particle_infos_for_pdb = []
227 
228  geometric_center = [0, 0, 0]
229  atom_count = 0
230  atom_index = 0
231 
232  if self.use_pmi2:
233  # select highest resolution
234  ps = IMP.atom.Selection(self.dictionary_pdbs[name],resolution=0).get_selected_particles()
235  else:
236  ps = IMP.atom.get_leaves(self.dictionary_pdbs[name])
237 
238  for n, p in enumerate(ps):
239  protname, is_a_bead = self.get_prot_name_from_particle(name, p)
240 
241  if protname not in resindexes_dict:
242  resindexes_dict[protname] = []
243 
244  if IMP.atom.Atom.get_is_setup(p) and self.atomistic:
245  residue = IMP.atom.Residue(IMP.atom.Atom(p).get_parent())
246  rt = residue.get_residue_type()
247  resind = residue.get_index()
248  atomtype = IMP.atom.Atom(p).get_atom_type()
249  xyz = list(IMP.core.XYZ(p).get_coordinates())
250  radius = IMP.core.XYZR(p).get_radius()
251  geometric_center[0] += xyz[0]
252  geometric_center[1] += xyz[1]
253  geometric_center[2] += xyz[2]
254  atom_count += 1
255  particle_infos_for_pdb.append((xyz,
256  atomtype, rt, self.dictchain[name][protname], resind, None, radius))
257  resindexes_dict[protname].append(resind)
258 
260 
261  residue = IMP.atom.Residue(p)
262  resind = residue.get_index()
263  # skip if the residue was already added by atomistic resolution
264  # 0
265  if resind in resindexes_dict[protname]:
266  continue
267  else:
268  resindexes_dict[protname].append(resind)
269  rt = residue.get_residue_type()
270  xyz = IMP.core.XYZ(p).get_coordinates()
271  radius = IMP.core.XYZR(p).get_radius()
272  geometric_center[0] += xyz[0]
273  geometric_center[1] += xyz[1]
274  geometric_center[2] += xyz[2]
275  atom_count += 1
276  particle_infos_for_pdb.append((xyz, None,
277  rt, self.dictchain[name][protname], resind, None, radius))
278 
279  elif IMP.atom.Fragment.get_is_setup(p) and not is_a_bead:
280  resindexes = IMP.pmi.tools.get_residue_indexes(p)
281  resind = resindexes[len(resindexes) // 2]
282  if resind in resindexes_dict[protname]:
283  continue
284  else:
285  resindexes_dict[protname].append(resind)
286  rt = IMP.atom.ResidueType('BEA')
287  xyz = IMP.core.XYZ(p).get_coordinates()
288  radius = IMP.core.XYZR(p).get_radius()
289  geometric_center[0] += xyz[0]
290  geometric_center[1] += xyz[1]
291  geometric_center[2] += xyz[2]
292  atom_count += 1
293  particle_infos_for_pdb.append((xyz, None,
294  rt, self.dictchain[name][protname], resind, resindexes, radius))
295 
296  else:
297  if is_a_bead:
298  rt = IMP.atom.ResidueType('BEA')
299  resindexes = IMP.pmi.tools.get_residue_indexes(p)
300  if len(resindexes) > 0:
301  resind = resindexes[len(resindexes) // 2]
302  xyz = IMP.core.XYZ(p).get_coordinates()
303  radius = IMP.core.XYZR(p).get_radius()
304  geometric_center[0] += xyz[0]
305  geometric_center[1] += xyz[1]
306  geometric_center[2] += xyz[2]
307  atom_count += 1
308  particle_infos_for_pdb.append((xyz, None,
309  rt, self.dictchain[name][protname], resind, resindexes, radius))
310 
311  if atom_count > 0:
312  geometric_center = (geometric_center[0] / atom_count,
313  geometric_center[1] / atom_count,
314  geometric_center[2] / atom_count)
315 
316  # sort by chain ID, then residue index. Longer chain IDs (e.g. AA)
317  # should always come after shorter (e.g. Z)
318  particle_infos_for_pdb = sorted(particle_infos_for_pdb,
319  key=lambda x: (len(x[3]), x[3], x[4]))
320 
321  return (particle_infos_for_pdb, geometric_center)
322 
323 
324  def write_pdbs(self, appendmode=True):
325  for pdb in self.dictionary_pdbs.keys():
326  self.write_pdb(pdb, appendmode)
327 
328  def init_pdb_best_scoring(self,
329  suffix,
330  prot,
331  nbestscoring,
332  replica_exchange=False):
333  # save only the nbestscoring conformations
334  # create as many pdbs as needed
335 
336  self.suffixes.append(suffix)
337  self.replica_exchange = replica_exchange
338  if not self.replica_exchange:
339  # common usage
340  # if you are not in replica exchange mode
341  # initialize the array of scores internally
342  self.best_score_list = []
343  else:
344  # otherwise the replicas must communicate
345  # through a common file to know what are the best scores
346  self.best_score_file_name = "best.scores.rex.py"
347  self.best_score_list = []
348  with open(self.best_score_file_name, "w") as best_score_file:
349  best_score_file.write(
350  "self.best_score_list=" + str(self.best_score_list) + "\n")
351 
352  self.nbestscoring = nbestscoring
353  for i in range(self.nbestscoring):
354  name = suffix + "." + str(i) + ".pdb"
355  flpdb = open(name, 'w')
356  flpdb.close()
357  self.dictionary_pdbs[name] = prot
358  self._init_dictchain(name, prot)
359 
360  def write_pdb_best_scoring(self, score):
361  if self.nbestscoring is None:
362  print("Output.write_pdb_best_scoring: init_pdb_best_scoring not run")
363 
364  # update the score list
365  if self.replica_exchange:
366  # read the self.best_score_list from the file
367  exec(open(self.best_score_file_name).read())
368 
369  if len(self.best_score_list) < self.nbestscoring:
370  self.best_score_list.append(score)
371  self.best_score_list.sort()
372  index = self.best_score_list.index(score)
373  for suffix in self.suffixes:
374  for i in range(len(self.best_score_list) - 2, index - 1, -1):
375  oldname = suffix + "." + str(i) + ".pdb"
376  newname = suffix + "." + str(i + 1) + ".pdb"
377  # rename on Windows fails if newname already exists
378  if os.path.exists(newname):
379  os.unlink(newname)
380  os.rename(oldname, newname)
381  filetoadd = suffix + "." + str(index) + ".pdb"
382  self.write_pdb(filetoadd, appendmode=False)
383 
384  else:
385  if score < self.best_score_list[-1]:
386  self.best_score_list.append(score)
387  self.best_score_list.sort()
388  self.best_score_list.pop(-1)
389  index = self.best_score_list.index(score)
390  for suffix in self.suffixes:
391  for i in range(len(self.best_score_list) - 1, index - 1, -1):
392  oldname = suffix + "." + str(i) + ".pdb"
393  newname = suffix + "." + str(i + 1) + ".pdb"
394  os.rename(oldname, newname)
395  filenametoremove = suffix + \
396  "." + str(self.nbestscoring) + ".pdb"
397  os.remove(filenametoremove)
398  filetoadd = suffix + "." + str(index) + ".pdb"
399  self.write_pdb(filetoadd, appendmode=False)
400 
401  if self.replica_exchange:
402  # write the self.best_score_list to the file
403  with open(self.best_score_file_name, "w") as best_score_file:
404  best_score_file.write(
405  "self.best_score_list=" + str(self.best_score_list) + '\n')
406 
407  def init_rmf(self, name, hierarchies, rs=None, geometries=None, listofobjects=None):
408  """
409  This function initialize an RMF file
410 
411  @param name the name of the RMF file
412  @param hierarchies the hiearchies to be included (it is a list)
413  @param rs optional, the restraint sets (it is a list)
414  @param geometries optional, the geometries (it is a list)
415  @param listofobjects optional, the list of objects for the stat (it is a list)
416  """
417  rh = RMF.create_rmf_file(name)
418  IMP.rmf.add_hierarchies(rh, hierarchies)
419  cat=None
420  outputkey_rmfkey=None
421 
422  if rs is not None:
424  if geometries is not None:
425  IMP.rmf.add_geometries(rh,geometries)
426  if listofobjects is not None:
427  cat = rh.get_category("stat")
428  outputkey_rmfkey={}
429  for l in listofobjects:
430  if not "get_output" in dir(l):
431  raise ValueError("Output: object %s doesn't have get_output() method" % str(l))
432  output=l.get_output()
433  for outputkey in output:
434  rmftag=RMF.string_tag
435  if type(output[outputkey]) is float:
436  rmftag=RMF.float_tag
437  elif type(output[outputkey]) is int:
438  rmftag=RMF.int_tag
439  elif type(output[outputkey]) is str:
440  rmftag = RMF.string_tag
441  else:
442  rmftag = RMF.string_tag
443  rmfkey=rh.get_key(cat, outputkey, rmftag)
444  outputkey_rmfkey[outputkey]=rmfkey
445  outputkey_rmfkey["rmf_file"]=rh.get_key(cat, "rmf_file", RMF.string_tag)
446  outputkey_rmfkey["rmf_frame_index"]=rh.get_key(cat, "rmf_frame_index", RMF.int_tag)
447 
448  self.dictionary_rmfs[name] = (rh,cat,outputkey_rmfkey,listofobjects)
449 
450  def add_restraints_to_rmf(self, name, objectlist):
451  for o in _flatten(objectlist):
452  try:
453  rs = o.get_restraint_for_rmf()
454  except:
455  rs = o.get_restraint()
457  self.dictionary_rmfs[name][0],
458  rs.get_restraints())
459 
460  def add_geometries_to_rmf(self, name, objectlist):
461  for o in objectlist:
462  geos = o.get_geometries()
463  IMP.rmf.add_geometries(self.dictionary_rmfs[name][0], geos)
464 
465  def add_particle_pair_from_restraints_to_rmf(self, name, objectlist):
466  for o in objectlist:
467 
468  pps = o.get_particle_pairs()
469  for pp in pps:
471  self.dictionary_rmfs[name][0],
473 
474  def write_rmf(self, name):
475  IMP.rmf.save_frame(self.dictionary_rmfs[name][0])
476  if self.dictionary_rmfs[name][1] is not None:
477  cat=self.dictionary_rmfs[name][1]
478  outputkey_rmfkey=self.dictionary_rmfs[name][2]
479  listofobjects=self.dictionary_rmfs[name][3]
480  for l in listofobjects:
481  output=l.get_output()
482  for outputkey in output:
483  rmfkey=outputkey_rmfkey[outputkey]
484  try:
485  self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey,output[outputkey])
486  except NotImplementedError:
487  continue
488  rmfkey = outputkey_rmfkey["rmf_file"]
489  self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey, name)
490  rmfkey = outputkey_rmfkey["rmf_frame_index"]
491  nframes=self.dictionary_rmfs[name][0].get_number_of_frames()
492  self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey, nframes-1)
493  self.dictionary_rmfs[name][0].flush()
494 
495  def close_rmf(self, name):
496  rh = self.dictionary_rmfs[name][0]
497  del self.dictionary_rmfs[name]
498  del rh
499 
500  def write_rmfs(self):
501  for rmfinfo in self.dictionary_rmfs.keys():
502  self.write_rmf(rmfinfo[0])
503 
504  def init_stat(self, name, listofobjects):
505  if self.ascii:
506  flstat = open(name, 'w')
507  flstat.close()
508  else:
509  flstat = open(name, 'wb')
510  flstat.close()
511 
512  # check that all objects in listofobjects have a get_output method
513  for l in listofobjects:
514  if not "get_output" in dir(l):
515  raise ValueError("Output: object %s doesn't have get_output() method" % str(l))
516  self.dictionary_stats[name] = listofobjects
517 
518  def set_output_entry(self, key, value):
519  self.initoutput.update({key: value})
520 
521  def write_stat(self, name, appendmode=True):
522  output = self.initoutput
523  for obj in self.dictionary_stats[name]:
524  d = obj.get_output()
525  # remove all entries that begin with _ (private entries)
526  dfiltered = dict((k, v) for k, v in d.items() if k[0] != "_")
527  output.update(dfiltered)
528 
529  if appendmode:
530  writeflag = 'a'
531  else:
532  writeflag = 'w'
533 
534  if self.ascii:
535  flstat = open(name, writeflag)
536  flstat.write("%s \n" % output)
537  flstat.close()
538  else:
539  flstat = open(name, writeflag + 'b')
540  cPickle.dump(output, flstat, 2)
541  flstat.close()
542 
543  def write_stats(self):
544  for stat in self.dictionary_stats.keys():
545  self.write_stat(stat)
546 
547  def get_stat(self, name):
548  output = {}
549  for obj in self.dictionary_stats[name]:
550  output.update(obj.get_output())
551  return output
552 
553  def write_test(self, name, listofobjects):
554 # write the test:
555 # output=output.Output()
556 # output.write_test("test_modeling11_models.rmf_45492_11Sep13_veena_imp-020713.dat",outputobjects)
557 # run the test:
558 # output=output.Output()
559 # output.test("test_modeling11_models.rmf_45492_11Sep13_veena_imp-020713.dat",outputobjects)
560  flstat = open(name, 'w')
561  output = self.initoutput
562  for l in listofobjects:
563  if not "get_test_output" in dir(l) and not "get_output" in dir(l):
564  raise ValueError("Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
565  self.dictionary_stats[name] = listofobjects
566 
567  for obj in self.dictionary_stats[name]:
568  try:
569  d = obj.get_test_output()
570  except:
571  d = obj.get_output()
572  # remove all entries that begin with _ (private entries)
573  dfiltered = dict((k, v) for k, v in d.items() if k[0] != "_")
574  output.update(dfiltered)
575  #output.update({"ENVIRONMENT": str(self.get_environment_variables())})
576  #output.update(
577  # {"IMP_VERSIONS": str(self.get_versions_of_relevant_modules())})
578  flstat.write("%s \n" % output)
579  flstat.close()
580 
581  def test(self, name, listofobjects, tolerance=1e-5):
582  output = self.initoutput
583  for l in listofobjects:
584  if not "get_test_output" in dir(l) and not "get_output" in dir(l):
585  raise ValueError("Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
586  for obj in listofobjects:
587  try:
588  output.update(obj.get_test_output())
589  except:
590  output.update(obj.get_output())
591  #output.update({"ENVIRONMENT": str(self.get_environment_variables())})
592  #output.update(
593  # {"IMP_VERSIONS": str(self.get_versions_of_relevant_modules())})
594 
595  flstat = open(name, 'r')
596 
597  passed=True
598  for l in flstat:
599  test_dict = ast.literal_eval(l)
600  for k in test_dict:
601  if k in output:
602  old_value = str(test_dict[k])
603  new_value = str(output[k])
604  try:
605  float(old_value)
606  is_float = True
607  except ValueError:
608  is_float = False
609 
610  if is_float:
611  fold = float(old_value)
612  fnew = float(new_value)
613  diff = abs(fold - fnew)
614  if diff > tolerance:
615  print("%s: test failed, old value: %s new value %s; "
616  "diff %f > %f" % (str(k), str(old_value),
617  str(new_value), diff,
618  tolerance), file=sys.stderr)
619  passed=False
620  elif test_dict[k] != output[k]:
621  if len(old_value) < 50 and len(new_value) < 50:
622  print("%s: test failed, old value: %s new value %s"
623  % (str(k), old_value, new_value), file=sys.stderr)
624  passed=False
625  else:
626  print("%s: test failed, omitting results (too long)"
627  % str(k), file=sys.stderr)
628  passed=False
629 
630  else:
631  print("%s from old objects (file %s) not in new objects"
632  % (str(k), str(name)), file=sys.stderr)
633  return passed
634 
635  def get_environment_variables(self):
636  import os
637  return str(os.environ)
638 
639  def get_versions_of_relevant_modules(self):
640  import IMP
641  versions = {}
642  versions["IMP_VERSION"] = IMP.get_module_version()
643  try:
644  import IMP.pmi
645  versions["PMI_VERSION"] = IMP.pmi.get_module_version()
646  except (ImportError):
647  pass
648  try:
649  import IMP.isd2
650  versions["ISD2_VERSION"] = IMP.isd2.get_module_version()
651  except (ImportError):
652  pass
653  try:
654  import IMP.isd_emxl
655  versions["ISD_EMXL_VERSION"] = IMP.isd_emxl.get_module_version()
656  except (ImportError):
657  pass
658  return versions
659 
660 #-------------------
661  def init_stat2(
662  self,
663  name,
664  listofobjects,
665  extralabels=None,
666  listofsummedobjects=None):
667  # this is a new stat file that should be less
668  # space greedy!
669  # listofsummedobjects must be in the form [([obj1,obj2,obj3,obj4...],label)]
670  # extralabels
671 
672  if listofsummedobjects is None:
673  listofsummedobjects = []
674  if extralabels is None:
675  extralabels = []
676  flstat = open(name, 'w')
677  output = {}
678  stat2_keywords = {"STAT2HEADER": "STAT2HEADER"}
679  stat2_keywords.update(
680  {"STAT2HEADER_ENVIRON": str(self.get_environment_variables())})
681  stat2_keywords.update(
682  {"STAT2HEADER_IMP_VERSIONS": str(self.get_versions_of_relevant_modules())})
683  stat2_inverse = {}
684 
685  for l in listofobjects:
686  if not "get_output" in dir(l):
687  raise ValueError("Output: object %s doesn't have get_output() method" % str(l))
688  else:
689  d = l.get_output()
690  # remove all entries that begin with _ (private entries)
691  dfiltered = dict((k, v)
692  for k, v in d.items() if k[0] != "_")
693  output.update(dfiltered)
694 
695  # check for customizable entries
696  for l in listofsummedobjects:
697  for t in l[0]:
698  if not "get_output" in dir(t):
699  raise ValueError("Output: object %s doesn't have get_output() method" % str(t))
700  else:
701  if "_TotalScore" not in t.get_output():
702  raise ValueError("Output: object %s doesn't have _TotalScore entry to be summed" % str(t))
703  else:
704  output.update({l[1]: 0.0})
705 
706  for k in extralabels:
707  output.update({k: 0.0})
708 
709  for n, k in enumerate(output):
710  stat2_keywords.update({n: k})
711  stat2_inverse.update({k: n})
712 
713  flstat.write("%s \n" % stat2_keywords)
714  flstat.close()
715  self.dictionary_stats2[name] = (
716  listofobjects,
717  stat2_inverse,
718  listofsummedobjects,
719  extralabels)
720 
721  def write_stat2(self, name, appendmode=True):
722  output = {}
723  (listofobjects, stat2_inverse, listofsummedobjects,
724  extralabels) = self.dictionary_stats2[name]
725 
726  # writing objects
727  for obj in listofobjects:
728  od = obj.get_output()
729  dfiltered = dict((k, v) for k, v in od.items() if k[0] != "_")
730  for k in dfiltered:
731  output.update({stat2_inverse[k]: od[k]})
732 
733  # writing summedobjects
734  for l in listofsummedobjects:
735  partial_score = 0.0
736  for t in l[0]:
737  d = t.get_output()
738  partial_score += float(d["_TotalScore"])
739  output.update({stat2_inverse[l[1]]: str(partial_score)})
740 
741  # writing extralabels
742  for k in extralabels:
743  if k in self.initoutput:
744  output.update({stat2_inverse[k]: self.initoutput[k]})
745  else:
746  output.update({stat2_inverse[k]: "None"})
747 
748  if appendmode:
749  writeflag = 'a'
750  else:
751  writeflag = 'w'
752 
753  flstat = open(name, writeflag)
754  flstat.write("%s \n" % output)
755  flstat.close()
756 
757  def write_stats2(self):
758  for stat in self.dictionary_stats2.keys():
759  self.write_stat2(stat)
760 
761 
762 class OutputStatistics(object):
763  """Collect statistics from ProcessOutput.get_fields().
764  Counters of the total number of frames read, plus the models that
765  passed the various filters used in get_fields(), are provided."""
766  def __init__(self):
767  self.total = 0
768  self.passed_get_every = 0
769  self.passed_filterout = 0
770  self.passed_filtertuple = 0
771 
772 
773 class ProcessOutput(object):
774  """A class for reading stat files (either rmf or ascii v1 and v2)"""
775  def __init__(self, filename):
776  self.filename = filename
777  self.isstat1 = False
778  self.isstat2 = False
779  self.isrmf = False
780 
781  if self.filename is None:
782  raise ValueError("No file name provided. Use -h for help")
783 
784  try:
785  #let's see if that is an rmf file
786  rh = RMF.open_rmf_file_read_only(self.filename)
787  self.isrmf=True
788  cat=rh.get_category('stat')
789  rmf_klist=rh.get_keys(cat)
790  self.rmf_names_keys=dict([(rh.get_name(k),k) for k in rmf_klist])
791  del rh
792 
793  except IOError:
794  f = open(self.filename, "r")
795  # try with an ascii stat file
796  # get the keys from the first line
797  for line in f.readlines():
798  d = ast.literal_eval(line)
799  self.klist = list(d.keys())
800  # check if it is a stat2 file
801  if "STAT2HEADER" in self.klist:
802  self.isstat2 = True
803  for k in self.klist:
804  if "STAT2HEADER" in str(k):
805  # if print_header: print k, d[k]
806  del d[k]
807  stat2_dict = d
808  # get the list of keys sorted by value
809  kkeys = [k[0]
810  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
811  self.klist = [k[1]
812  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
813  self.invstat2_dict = {}
814  for k in kkeys:
815  self.invstat2_dict.update({stat2_dict[k]: k})
816  else:
817  IMP.handle_use_deprecated("statfile v1 is deprecated. "
818  "Please convert to statfile v2.\n")
819  self.isstat1 = True
820  self.klist.sort()
821 
822  break
823  f.close()
824 
825 
826  def get_keys(self):
827  if self.isrmf:
828  return sorted(self.rmf_names_keys.keys())
829  else:
830  return self.klist
831 
832  def show_keys(self, ncolumns=2, truncate=65):
833  IMP.pmi.tools.print_multicolumn(self.get_keys(), ncolumns, truncate)
834 
835  def get_fields(self, fields, filtertuple=None, filterout=None, get_every=1,
836  statistics=None):
837  '''
838  Get the desired field names, and return a dictionary.
839  Namely, "fields" are the queried keys in the stat file (eg. ["Total_Score",...])
840  The returned data structure is a dictionary, where each key is a field and the value
841  is the time series (ie, frame ordered series)
842  of that field (ie, {"Total_Score":[Score_0,Score_1,Score_2,Score_3,...],....} )
843 
844  @param fields (list of strings) queried keys in the stat file (eg. "Total_Score"....)
845  @param filterout specify if you want to "grep" out something from
846  the file, so that it is faster
847  @param filtertuple a tuple that contains
848  ("TheKeyToBeFiltered",relationship,value)
849  where relationship = "<", "==", or ">"
850  @param get_every only read every Nth line from the file
851  @param statistics if provided, accumulate statistics in an
852  OutputStatistics object
853  '''
854 
855  if statistics is None:
856  statistics = OutputStatistics()
857  outdict = {}
858  for field in fields:
859  outdict[field] = []
860 
861  # print fields values
862  if self.isrmf:
863  rh = RMF.open_rmf_file_read_only(self.filename)
864  nframes=rh.get_number_of_frames()
865  for i in range(nframes):
866  statistics.total += 1
867  # "get_every" and "filterout" not enforced for RMF
868  statistics.passed_get_every += 1
869  statistics.passed_filterout += 1
870  IMP.rmf.load_frame(rh, RMF.FrameID(i))
871  if not filtertuple is None:
872  keytobefiltered = filtertuple[0]
873  relationship = filtertuple[1]
874  value = filtertuple[2]
875  datavalue=rh.get_root_node().get_value(self.rmf_names_keys[keytobefiltered])
876  if self.isfiltered(datavalue,relationship,value): continue
877 
878  statistics.passed_filtertuple += 1
879  for field in fields:
880  outdict[field].append(rh.get_root_node().get_value(self.rmf_names_keys[field]))
881 
882  else:
883  f = open(self.filename, "r")
884  line_number = 0
885 
886  for line in f.readlines():
887  statistics.total += 1
888  if not filterout is None:
889  if filterout in line:
890  continue
891  statistics.passed_filterout += 1
892  line_number += 1
893 
894  if line_number % get_every != 0:
895  if line_number == 1 and self.isstat2:
896  statistics.total -= 1
897  statistics.passed_filterout -= 1
898  continue
899  statistics.passed_get_every += 1
900  #if line_number % 1000 == 0:
901  # print "ProcessOutput.get_fields: read line %s from file %s" % (str(line_number), self.filename)
902  try:
903  d = ast.literal_eval(line)
904  except:
905  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
906  continue
907 
908  if self.isstat1:
909 
910  if not filtertuple is None:
911  keytobefiltered = filtertuple[0]
912  relationship = filtertuple[1]
913  value = filtertuple[2]
914  datavalue=d[keytobefiltered]
915  if self.isfiltered(datavalue, relationship, value): continue
916 
917  statistics.passed_filtertuple += 1
918  [outdict[field].append(d[field]) for field in fields]
919 
920  elif self.isstat2:
921  if line_number == 1:
922  statistics.total -= 1
923  statistics.passed_filterout -= 1
924  statistics.passed_get_every -= 1
925  continue
926 
927  if not filtertuple is None:
928  keytobefiltered = filtertuple[0]
929  relationship = filtertuple[1]
930  value = filtertuple[2]
931  datavalue=d[self.invstat2_dict[keytobefiltered]]
932  if self.isfiltered(datavalue, relationship, value): continue
933 
934  statistics.passed_filtertuple += 1
935  [outdict[field].append(d[self.invstat2_dict[field]]) for field in fields]
936 
937  f.close()
938 
939  return outdict
940 
941  def isfiltered(self,datavalue,relationship,refvalue):
942  dofilter=False
943  try:
944  fdatavalue=float(datavalue)
945  except ValueError:
946  raise ValueError("ProcessOutput.filter: datavalue cannot be converted into a float")
947 
948  if relationship == "<":
949  if float(datavalue) >= refvalue:
950  dofilter=True
951  if relationship == ">":
952  if float(datavalue) <= refvalue:
953  dofilter=True
954  if relationship == "==":
955  if float(datavalue) != refvalue:
956  dofilter=True
957  return dofilter
958 
959 
961  """ class to allow more advanced handling of RMF files.
962  It is both a container and a IMP.atom.Hierarchy.
963  - it is iterable (while loading the corresponding frame)
964  - Item brackets [] load the corresponding frame
965  - slice create an iterator
966  - can relink to another RMF file
967  """
968  def __init__(self,model,rmf_file_name):
969  """
970  @param model: the IMP.Model()
971  @param rmf_file_name: str, path of the rmf file
972  """
973  self.model=model
974  try:
975  self.rh_ref = RMF.open_rmf_file_read_only(rmf_file_name)
976  except TypeError:
977  raise TypeError("Wrong rmf file name or type: %s"% str(rmf_file_name))
978  hs = IMP.rmf.create_hierarchies(self.rh_ref, self.model)
979  IMP.rmf.load_frame(self.rh_ref, RMF.FrameID(0))
980  self.root_hier_ref = hs[0]
981  IMP.atom.Hierarchy.__init__(self, self.root_hier_ref)
982  self.model.update()
983  self.ColorHierarchy=None
984 
985 
986  def link_to_rmf(self,rmf_file_name):
987  """
988  Link to another RMF file
989  """
990  self.rh_ref = RMF.open_rmf_file_read_only(rmf_file_name)
991  IMP.rmf.link_hierarchies(self.rh_ref, [self])
992  if self.ColorHierarchy:
993  self.ColorHierarchy.method()
994  RMFHierarchyHandler.set_frame(self,0)
995 
996  def set_frame(self,index):
997  try:
998  IMP.rmf.load_frame(self.rh_ref, RMF.FrameID(index))
999  except:
1000  print("skipping frame %s:%d\n"%(self.current_rmf, index))
1001  self.model.update()
1002 
1003  def get_number_of_frames(self):
1004  return self.rh_ref.get_number_of_frames()
1005 
1006  def __getitem__(self,int_slice_adaptor):
1007  if type(int_slice_adaptor) is int:
1008  self.set_frame(int_slice_adaptor)
1009  return int_slice_adaptor
1010  elif type(int_slice_adaptor) is slice:
1011  return self.__iter__(int_slice_adaptor)
1012  else:
1013  raise TypeError("Unknown Type")
1014 
1015  def __len__(self):
1016  return self.get_number_of_frames()
1017 
1018  def __iter__(self,slice_key=None):
1019  if slice_key is None:
1020  for nframe in range(len(self)):
1021  yield self[nframe]
1022  else:
1023  for nframe in list(range(len(self)))[slice_key]:
1024  yield self[nframe]
1025 
1026 class CacheHierarchyCoordinates(object):
1027  def __init__(self,StatHierarchyHandler):
1028  self.xyzs=[]
1029  self.nrms=[]
1030  self.rbs=[]
1031  self.nrm_coors={}
1032  self.xyz_coors={}
1033  self.rb_trans={}
1034  self.current_index=None
1035  self.rmfh=StatHierarchyHandler
1036  rbs,xyzs=IMP.pmi.tools.get_rbs_and_beads([self.rmfh])
1037  self.model=self.rmfh.get_model()
1038  self.rbs=rbs
1039  for xyz in xyzs:
1041  nrm=IMP.core.NonRigidMember(xyz)
1042  self.nrms.append(nrm)
1043  else:
1044  fb=IMP.core.XYZ(xyz)
1045  self.xyzs.append(fb)
1046 
1047  def do_store(self,index):
1048  self.rb_trans[index]={}
1049  self.nrm_coors[index]={}
1050  self.xyz_coors[index]={}
1051  for rb in self.rbs:
1052  self.rb_trans[index][rb]=rb.get_reference_frame()
1053  for nrm in self.nrms:
1054  self.nrm_coors[index][nrm]=nrm.get_internal_coordinates()
1055  for xyz in self.xyzs:
1056  self.xyz_coors[index][xyz]=xyz.get_coordinates()
1057  self.current_index=index
1058 
1059  def do_update(self,index):
1060  if self.current_index!=index:
1061  for rb in self.rbs:
1062  rb.set_reference_frame(self.rb_trans[index][rb])
1063  for nrm in self.nrms:
1064  nrm.set_internal_coordinates(self.nrm_coors[index][nrm])
1065  for xyz in self.xyzs:
1066  xyz.set_coordinates(self.xyz_coors[index][xyz])
1067  self.current_index=index
1068  self.model.update()
1069 
1070  def get_number_of_frames(self):
1071  return len(self.rb_trans.keys())
1072 
1073  def __getitem__(self,index):
1074  if type(index) is int:
1075  if index in self.rb_trans.keys():
1076  return True
1077  else:
1078  return False
1079  else:
1080  raise TypeError("Unknown Type")
1081 
1082  def __len__(self):
1083  return self.get_number_of_frames()
1084 
1085 
1086 
1087 
1089  """ class to link stat files to several rmf files """
1090  def __init__(self,model=None,stat_file=None,number_best_scoring_models=None,score_key=None,StatHierarchyHandler=None,cache=None):
1091  """
1092 
1093  @param model: IMP.Model()
1094  @param stat_file: either 1) a list or 2) a single stat file names (either rmfs or ascii, or pickled data or pickled cluster), 3) a dictionary containing an rmf/ascii
1095  stat file name as key and a list of frames as values
1096  @param number_best_scoring_models:
1097  @param StatHierarchyHandler: copy constructor input object
1098  @param cache: cache coordinates and rigid body transformations.
1099  """
1100 
1101  if not StatHierarchyHandler is None:
1102  #overrides all other arguments
1103  #copy constructor: create a copy with different RMFHierarchyHandler
1104  self.model=StatHierarchyHandler.model
1105  self.data=StatHierarchyHandler.data
1106  self.number_best_scoring_models=StatHierarchyHandler.number_best_scoring_models
1107  self.is_setup=True
1108  self.current_rmf=StatHierarchyHandler.current_rmf
1109  self.current_frame=None
1110  self.current_index=None
1111  self.score_threshold=StatHierarchyHandler.score_threshold
1112  self.score_key=StatHierarchyHandler.score_key
1113  self.cache=StatHierarchyHandler.cache
1114  RMFHierarchyHandler.__init__(self, self.model,self.current_rmf)
1115  if self.cache:
1116  self.cache=CacheHierarchyCoordinates(self)
1117  else:
1118  self.cache=None
1119  self.set_frame(0)
1120 
1121  else:
1122  #standard constructor
1123  self.model=model
1124  self.data=[]
1125  self.number_best_scoring_models=number_best_scoring_models
1126  self.cache=cache
1127 
1128  if score_key is None:
1129  self.score_key="Total_Score"
1130  else:
1131  self.score_key=score_key
1132  self.is_setup=None
1133  self.current_rmf=None
1134  self.current_frame=None
1135  self.current_index=None
1136  self.score_threshold=None
1137 
1138  if type(stat_file) is str:
1139  self.add_stat_file(stat_file)
1140  elif type(stat_file) is list:
1141  for f in stat_file:
1142  self.add_stat_file(f)
1143 
1144  def add_stat_file(self,stat_file):
1145  try:
1146  import cPickle as pickle
1147  except ImportError:
1148  import pickle
1149 
1150  try:
1151  '''check that it is not a pickle file with saved data from a previous calculation'''
1152  self.load_data(stat_file)
1153 
1154  if self.number_best_scoring_models:
1155  scores = self.get_scores()
1156  max_score = sorted(scores)[0:min(len(self), self.number_best_scoring_models)][-1]
1157  self.do_filter_by_score(max_score)
1158 
1159  except pickle.UnpicklingError:
1160  '''alternatively read the ascii stat files'''
1161  try:
1162  scores,rmf_files,rmf_frame_indexes,features = self.get_info_from_stat_file(stat_file, self.score_threshold)
1163  except (KeyError, SyntaxError):
1164  # in this case check that is it an rmf file, probably without stat stored in
1165  try:
1166  # let's see if that is an rmf file
1167  rh = RMF.open_rmf_file_read_only(stat_file)
1168  nframes = rh.get_number_of_frames()
1169  scores=[0.0]*nframes
1170  rmf_files=[stat_file]*nframes
1171  rmf_frame_indexes=range(nframes)
1172  features={}
1173  except:
1174  return
1175 
1176 
1177  if len(set(rmf_files)) > 1:
1178  raise ("Multiple RMF files found")
1179 
1180  if not rmf_files:
1181  print("StatHierarchyHandler: Error: Trying to set none as rmf_file (probably empty stat file), aborting")
1182  return
1183 
1184  for n,index in enumerate(rmf_frame_indexes):
1185  featn_dict=dict([(k,features[k][n]) for k in features])
1186  self.data.append(IMP.pmi.output.DataEntry(stat_file,rmf_files[n],index,scores[n],featn_dict))
1187 
1188  if self.number_best_scoring_models:
1189  scores=self.get_scores()
1190  max_score=sorted(scores)[0:min(len(self),self.number_best_scoring_models)][-1]
1191  self.do_filter_by_score(max_score)
1192 
1193  if not self.is_setup:
1194  RMFHierarchyHandler.__init__(self, self.model,self.get_rmf_names()[0])
1195  if self.cache:
1196  self.cache=CacheHierarchyCoordinates(self)
1197  else:
1198  self.cache=None
1199  self.is_setup=True
1200  self.current_rmf=self.get_rmf_names()[0]
1201 
1202  self.set_frame(0)
1203 
1204  def save_data(self,filename='data.pkl'):
1205  try:
1206  import cPickle as pickle
1207  except ImportError:
1208  import pickle
1209  with open(filename, 'wb') as fl:
1210  pickle.dump(self.data, fl)
1211 
1212  def load_data(self,filename='data.pkl'):
1213  try:
1214  import cPickle as pickle
1215  except ImportError:
1216  import pickle
1217  with open(filename, 'rb') as fl:
1218  data_structure=pickle.load(fl)
1219  #first check that it is a list
1220  if not type(data_structure) is list:
1221  raise TypeError("%filename should contain a list of IMP.pmi.output.DataEntry or IMP.pmi.output.Cluster" % filename)
1222  # second check the types
1223  if all(isinstance(item, IMP.pmi.output.DataEntry) for item in data_structure):
1224  self.data=data_structure
1225  elif all(isinstance(item, IMP.pmi.output.Cluster) for item in data_structure):
1226  nmodels=0
1227  for cluster in data_structure:
1228  nmodels+=len(cluster)
1229  self.data=[None]*nmodels
1230  for cluster in data_structure:
1231  for n,data in enumerate(cluster):
1232  index=cluster.members[n]
1233  self.data[index]=data
1234  else:
1235  raise TypeError("%filename should contain a list of IMP.pmi.output.DataEntry or IMP.pmi.output.Cluster" % filename)
1236 
1237  def set_frame(self,index):
1238  if self.cache is not None and self.cache[index]:
1239  self.cache.do_update(index)
1240  else:
1241  nm=self.data[index].rmf_name
1242  fidx=self.data[index].rmf_index
1243  if nm != self.current_rmf:
1244  self.link_to_rmf(nm)
1245  self.current_rmf=nm
1246  self.current_frame=-1
1247  if fidx!=self.current_frame:
1248  RMFHierarchyHandler.set_frame(self, fidx)
1249  self.current_frame=fidx
1250  if self.cache is not None:
1251  self.cache.do_store(index)
1252 
1253  self.current_index = index
1254 
1255  def __getitem__(self,int_slice_adaptor):
1256  if type(int_slice_adaptor) is int:
1257  self.set_frame(int_slice_adaptor)
1258  return self.data[int_slice_adaptor]
1259  elif type(int_slice_adaptor) is slice:
1260  return self.__iter__(int_slice_adaptor)
1261  else:
1262  raise TypeError("Unknown Type")
1263 
1264  def __len__(self):
1265  return len(self.data)
1266 
1267  def __iter__(self,slice_key=None):
1268  if slice_key is None:
1269  for i in range(len(self)):
1270  yield self[i]
1271  else:
1272  for i in range(len(self))[slice_key]:
1273  yield self[i]
1274 
1275  def do_filter_by_score(self,maximum_score):
1276  self.data=[d for d in self.data if d.score<=maximum_score]
1277 
1278  def get_scores(self):
1279  return [d.score for d in self.data]
1280 
1281  def get_feature_series(self,feature_name):
1282  return [d.features[feature_name] for d in self.data]
1283 
1284  def get_feature_names(self):
1285  return self.data[0].features.keys()
1286 
1287  def get_rmf_names(self):
1288  return [d.rmf_name for d in self.data]
1289 
1290  def get_stat_files_names(self):
1291  return [d.stat_file for d in self.data]
1292 
1293  def get_rmf_indexes(self):
1294  return [d.rmf_index for d in self.data]
1295 
1296  def get_info_from_stat_file(self, stat_file, score_threshold=None):
1297  po=ProcessOutput(stat_file)
1298  fs=po.get_keys()
1299  models = IMP.pmi.io.get_best_models([stat_file],
1300  score_key=self.score_key,
1301  feature_keys=fs,
1302  rmf_file_key="rmf_file",
1303  rmf_file_frame_key="rmf_frame_index",
1304  prefiltervalue=score_threshold,
1305  get_every=1)
1306 
1307 
1308 
1309  scores = [float(y) for y in models[2]]
1310  rmf_files = models[0]
1311  rmf_frame_indexes = models[1]
1312  features=models[3]
1313  return scores, rmf_files, rmf_frame_indexes,features
1314 
1315 
1316 class DataEntry(object):
1317  '''
1318  A class to store data associated to a model
1319  '''
1320  def __init__(self,stat_file=None,rmf_name=None,rmf_index=None,score=None,features=None):
1321  self.rmf_name=rmf_name
1322  self.rmf_index=rmf_index
1323  self.score=score
1324  self.features=features
1325  self.stat_file=stat_file
1326 
1327  def __repr__(self):
1328  s= "IMP.pmi.output.DataEntry\n"
1329  s+="---- stat file %s \n"%(self.stat_file)
1330  s+="---- rmf file %s \n"%(self.rmf_name)
1331  s+="---- rmf index %s \n"%(str(self.rmf_index))
1332  s+="---- score %s \n"%(str(self.score))
1333  s+="---- number of features %s \n"%(str(len(self.features.keys())))
1334  return s
1335 
1336 
1337 class Cluster(object):
1338  '''
1339  A container for models organized into clusters
1340  '''
1341  def __init__(self,cid=None):
1342  self.cluster_id=cid
1343  self.members=[]
1344  self.precision=None
1345  self.center_index=None
1346  self.members_data={}
1347 
1348  def add_member(self,index,data=None):
1349  self.members.append(index)
1350  self.members_data[index]=data
1351  self.average_score=self.compute_score()
1352 
1353  def compute_score(self):
1354  try:
1355  score=sum([d.score for d in self])/len(self)
1356  except AttributeError:
1357  score=None
1358  return score
1359 
1360  def __repr__(self):
1361  s= "IMP.pmi.output.Cluster\n"
1362  s+="---- cluster_id %s \n"%str(self.cluster_id)
1363  s+="---- precision %s \n"%str(self.precision)
1364  s+="---- average score %s \n"%str(self.average_score)
1365  s+="---- number of members %s \n"%str(len(self.members))
1366  s+="---- center index %s \n"%str(self.center_index)
1367  return s
1368 
1369  def __getitem__(self,int_slice_adaptor):
1370  if type(int_slice_adaptor) is int:
1371  index=self.members[int_slice_adaptor]
1372  return self.members_data[index]
1373  elif type(int_slice_adaptor) is slice:
1374  return self.__iter__(int_slice_adaptor)
1375  else:
1376  raise TypeError("Unknown Type")
1377 
1378  def __len__(self):
1379  return len(self.members)
1380 
1381  def __iter__(self,slice_key=None):
1382  if slice_key is None:
1383  for i in range(len(self)):
1384  yield self[i]
1385  else:
1386  for i in range(len(self))[slice_key]:
1387  yield self[i]
1388 
1389  def __add__(self, other):
1390  self.members+=other.members
1391  self.members_data.update(other.members_data)
1392  self.average_score=self.compute_score()
1393  self.precision=None
1394  self.center_index=None
1395  return self
1396 
1397 
1398 def plot_clusters_populations(clusters):
1399  indexes=[]
1400  populations=[]
1401  for cluster in clusters:
1402  indexes.append(cluster.cluster_id)
1403  populations.append(len(cluster))
1404 
1405  import matplotlib.pyplot as plt
1406  fig, ax = plt.subplots()
1407  ax.bar(indexes, populations, 0.5, color='r') #, yerr=men_std)
1408  ax.set_ylabel('Population')
1409  ax.set_xlabel(('Cluster index'))
1410  plt.show()
1411 
1412 def plot_clusters_precisions(clusters):
1413  indexes=[]
1414  precisions=[]
1415  for cluster in clusters:
1416  indexes.append(cluster.cluster_id)
1417 
1418  prec=cluster.precision
1419  print(cluster.cluster_id,prec)
1420  if prec is None:
1421  prec=0.0
1422  precisions.append(prec)
1423 
1424  import matplotlib.pyplot as plt
1425  fig, ax = plt.subplots()
1426  ax.bar(indexes, precisions, 0.5, color='r') #, yerr=men_std)
1427  ax.set_ylabel('Precision [A]')
1428  ax.set_xlabel(('Cluster index'))
1429  plt.show()
1430 
1431 def plot_clusters_scores(clusters):
1432  indexes=[]
1433  values=[]
1434  for cluster in clusters:
1435  indexes.append(cluster.cluster_id)
1436  values.append([])
1437  for data in cluster:
1438  values[-1].append(data.score)
1439 
1440  plot_fields_box_plots("scores.pdf", values, indexes, frequencies=None,
1441  valuename="Scores", positionname="Cluster index", xlabels=None,scale_plot_length=1.0)
1442 
1443 class CrossLinkIdentifierDatabase(object):
1444  def __init__(self):
1445  self.clidb=dict()
1446 
1447  def check_key(self,key):
1448  if key not in self.clidb:
1449  self.clidb[key]={}
1450 
1451  def set_unique_id(self,key,value):
1452  self.check_key(key)
1453  self.clidb[key]["XLUniqueID"]=str(value)
1454 
1455  def set_protein1(self,key,value):
1456  self.check_key(key)
1457  self.clidb[key]["Protein1"]=str(value)
1458 
1459  def set_protein2(self,key,value):
1460  self.check_key(key)
1461  self.clidb[key]["Protein2"]=str(value)
1462 
1463  def set_residue1(self,key,value):
1464  self.check_key(key)
1465  self.clidb[key]["Residue1"]=int(value)
1466 
1467  def set_residue2(self,key,value):
1468  self.check_key(key)
1469  self.clidb[key]["Residue2"]=int(value)
1470 
1471  def set_idscore(self,key,value):
1472  self.check_key(key)
1473  self.clidb[key]["IDScore"]=float(value)
1474 
1475  def set_state(self,key,value):
1476  self.check_key(key)
1477  self.clidb[key]["State"]=int(value)
1478 
1479  def set_sigma1(self,key,value):
1480  self.check_key(key)
1481  self.clidb[key]["Sigma1"]=str(value)
1482 
1483  def set_sigma2(self,key,value):
1484  self.check_key(key)
1485  self.clidb[key]["Sigma2"]=str(value)
1486 
1487  def set_psi(self,key,value):
1488  self.check_key(key)
1489  self.clidb[key]["Psi"]=str(value)
1490 
1491  def get_unique_id(self,key):
1492  return self.clidb[key]["XLUniqueID"]
1493 
1494  def get_protein1(self,key):
1495  return self.clidb[key]["Protein1"]
1496 
1497  def get_protein2(self,key):
1498  return self.clidb[key]["Protein2"]
1499 
1500  def get_residue1(self,key):
1501  return self.clidb[key]["Residue1"]
1502 
1503  def get_residue2(self,key):
1504  return self.clidb[key]["Residue2"]
1505 
1506  def get_idscore(self,key):
1507  return self.clidb[key]["IDScore"]
1508 
1509  def get_state(self,key):
1510  return self.clidb[key]["State"]
1511 
1512  def get_sigma1(self,key):
1513  return self.clidb[key]["Sigma1"]
1514 
1515  def get_sigma2(self,key):
1516  return self.clidb[key]["Sigma2"]
1517 
1518  def get_psi(self,key):
1519  return self.clidb[key]["Psi"]
1520 
1521  def set_float_feature(self,key,value,feature_name):
1522  self.check_key(key)
1523  self.clidb[key][feature_name]=float(value)
1524 
1525  def set_int_feature(self,key,value,feature_name):
1526  self.check_key(key)
1527  self.clidb[key][feature_name]=int(value)
1528 
1529  def set_string_feature(self,key,value,feature_name):
1530  self.check_key(key)
1531  self.clidb[key][feature_name]=str(value)
1532 
1533  def get_feature(self,key,feature_name):
1534  return self.clidb[key][feature_name]
1535 
1536  def write(self,filename):
1537  import pickle
1538  with open(filename, 'wb') as handle:
1539  pickle.dump(self.clidb,handle)
1540 
1541  def load(self,filename):
1542  import pickle
1543  with open(filename, 'rb') as handle:
1544  self.clidb=pickle.load(handle)
1545 
1546 def plot_fields(fields, framemin=None, framemax=None):
1547  import matplotlib as mpl
1548  mpl.use('Agg')
1549  import matplotlib.pyplot as plt
1550 
1551  plt.rc('lines', linewidth=4)
1552  fig, axs = plt.subplots(nrows=len(fields))
1553  fig.set_size_inches(10.5, 5.5 * len(fields))
1554  plt.rc('axes', color_cycle=['r'])
1555 
1556  n = 0
1557  for key in fields:
1558  if framemin is None:
1559  framemin = 0
1560  if framemax is None:
1561  framemax = len(fields[key])
1562  x = list(range(framemin, framemax))
1563  y = [float(y) for y in fields[key][framemin:framemax]]
1564  if len(fields) > 1:
1565  axs[n].plot(x, y)
1566  axs[n].set_title(key, size="xx-large")
1567  axs[n].tick_params(labelsize=18, pad=10)
1568  else:
1569  axs.plot(x, y)
1570  axs.set_title(key, size="xx-large")
1571  axs.tick_params(labelsize=18, pad=10)
1572  n += 1
1573 
1574  # Tweak spacing between subplots to prevent labels from overlapping
1575  plt.subplots_adjust(hspace=0.3)
1576  plt.show()
1577 
1578 
1580  name, values_lists, valuename=None, bins=40, colors=None, format="png",
1581  reference_xline=None, yplotrange=None, xplotrange=None,normalized=True,
1582  leg_names=None):
1583 
1584  '''Plot a list of histograms from a value list.
1585  @param name the name of the plot
1586  @param value_lists the list of list of values eg: [[...],[...],[...]]
1587  @param valuename the y-label
1588  @param bins the number of bins
1589  @param colors If None, will use rainbow. Else will use specific list
1590  @param format output format
1591  @param reference_xline plot a reference line parallel to the y-axis
1592  @param yplotrange the range for the y-axis
1593  @param normalized whether the histogram is normalized or not
1594  @param leg_names names for the legend
1595  '''
1596 
1597  import matplotlib as mpl
1598  mpl.use('Agg')
1599  import matplotlib.pyplot as plt
1600  import matplotlib.cm as cm
1601  fig = plt.figure(figsize=(18.0, 9.0))
1602 
1603  if colors is None:
1604  colors = cm.rainbow(np.linspace(0, 1, len(values_lists)))
1605  for nv,values in enumerate(values_lists):
1606  col=colors[nv]
1607  if leg_names is not None:
1608  label=leg_names[nv]
1609  else:
1610  label=str(nv)
1611  h=plt.hist(
1612  [float(y) for y in values],
1613  bins=bins,
1614  color=col,
1615  normed=normalized,histtype='step',lw=4,
1616  label=label)
1617 
1618  # plt.title(name,size="xx-large")
1619  plt.tick_params(labelsize=12, pad=10)
1620  if valuename is None:
1621  plt.xlabel(name, size="xx-large")
1622  else:
1623  plt.xlabel(valuename, size="xx-large")
1624  plt.ylabel("Frequency", size="xx-large")
1625 
1626  if not yplotrange is None:
1627  plt.ylim()
1628  if not xplotrange is None:
1629  plt.xlim(xplotrange)
1630 
1631  plt.legend(loc=2)
1632 
1633  if not reference_xline is None:
1634  plt.axvline(
1635  reference_xline,
1636  color='red',
1637  linestyle='dashed',
1638  linewidth=1)
1639 
1640  plt.savefig(name + "." + format, dpi=150, transparent=True)
1641  plt.show()
1642 
1643 
1644 def plot_fields_box_plots(name, values, positions, frequencies=None,
1645  valuename="None", positionname="None", xlabels=None,scale_plot_length=1.0):
1646  '''
1647  Plot time series as boxplots.
1648  fields is a list of time series, positions are the x-values
1649  valuename is the y-label, positionname is the x-label
1650  '''
1651 
1652  import matplotlib as mpl
1653  mpl.use('Agg')
1654  import matplotlib.pyplot as plt
1655  from matplotlib.patches import Polygon
1656 
1657  bps = []
1658  fig = plt.figure(figsize=(float(len(positions))*scale_plot_length, 5.0))
1659  fig.canvas.set_window_title(name)
1660 
1661  ax1 = fig.add_subplot(111)
1662 
1663  plt.subplots_adjust(left=0.1, right=0.990, top=0.95, bottom=0.4)
1664 
1665  bps.append(plt.boxplot(values, notch=0, sym='', vert=1,
1666  whis=1.5, positions=positions))
1667 
1668  plt.setp(bps[-1]['boxes'], color='black', lw=1.5)
1669  plt.setp(bps[-1]['whiskers'], color='black', ls=":", lw=1.5)
1670 
1671  if frequencies is not None:
1672  for n,v in enumerate(values):
1673  plist=[positions[n]]*len(v)
1674  ax1.plot(plist, v, 'gx', alpha=0.7, markersize=7)
1675 
1676  # print ax1.xaxis.get_majorticklocs()
1677  if not xlabels is None:
1678  ax1.set_xticklabels(xlabels)
1679  plt.xticks(rotation=90)
1680  plt.xlabel(positionname)
1681  plt.ylabel(valuename)
1682 
1683  plt.savefig(name+".pdf",dpi=150)
1684  plt.show()
1685 
1686 
1687 def plot_xy_data(x,y,title=None,out_fn=None,display=True,set_plot_yaxis_range=None,
1688  xlabel=None,ylabel=None):
1689  import matplotlib as mpl
1690  mpl.use('Agg')
1691  import matplotlib.pyplot as plt
1692  plt.rc('lines', linewidth=2)
1693 
1694  fig, ax = plt.subplots(nrows=1)
1695  fig.set_size_inches(8,4.5)
1696  if title is not None:
1697  fig.canvas.set_window_title(title)
1698 
1699  #plt.rc('axes', color='r')
1700  ax.plot(x,y,color='r')
1701  if set_plot_yaxis_range is not None:
1702  x1,x2,y1,y2=plt.axis()
1703  y1=set_plot_yaxis_range[0]
1704  y2=set_plot_yaxis_range[1]
1705  plt.axis((x1,x2,y1,y2))
1706  if title is not None:
1707  ax.set_title(title)
1708  if xlabel is not None:
1709  ax.set_xlabel(xlabel)
1710  if ylabel is not None:
1711  ax.set_ylabel(ylabel)
1712  if out_fn is not None:
1713  plt.savefig(out_fn+".pdf")
1714  if display:
1715  plt.show()
1716  plt.close(fig)
1717 
1718 def plot_scatter_xy_data(x,y,labelx="None",labely="None",
1719  xmin=None,xmax=None,ymin=None,ymax=None,
1720  savefile=False,filename="None.eps",alpha=0.75):
1721 
1722  import matplotlib as mpl
1723  mpl.use('Agg')
1724  import matplotlib.pyplot as plt
1725  import sys
1726  from matplotlib import rc
1727  #rc('font', **{'family':'serif','serif':['Palatino']})
1728  rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
1729  #rc('text', usetex=True)
1730 
1731  fig, axs = plt.subplots(1)
1732 
1733  axs0 = axs
1734 
1735  axs0.set_xlabel(labelx, size="xx-large")
1736  axs0.set_ylabel(labely, size="xx-large")
1737  axs0.tick_params(labelsize=18, pad=10)
1738 
1739  plot2 = []
1740 
1741  plot2.append(axs0.plot(x, y, 'o', color='k',lw=2, ms=0.1, alpha=alpha, c="w"))
1742 
1743  axs0.legend(
1744  loc=0,
1745  frameon=False,
1746  scatterpoints=1,
1747  numpoints=1,
1748  columnspacing=1)
1749 
1750  fig.set_size_inches(8.0, 8.0)
1751  fig.subplots_adjust(left=0.161, right=0.850, top=0.95, bottom=0.11)
1752  if (not ymin is None) and (not ymax is None):
1753  axs0.set_ylim(ymin,ymax)
1754  if (not xmin is None) and (not xmax is None):
1755  axs0.set_xlim(xmin,xmax)
1756 
1757  #plt.show()
1758  if savefile:
1759  fig.savefig(filename, dpi=300)
1760 
1761 
1762 def get_graph_from_hierarchy(hier):
1763  graph = []
1764  depth_dict = {}
1765  depth = 0
1766  (graph, depth, depth_dict) = recursive_graph(
1767  hier, graph, depth, depth_dict)
1768 
1769  # filters node labels according to depth_dict
1770  node_labels_dict = {}
1771  node_size_dict = {}
1772  for key in depth_dict:
1773  node_size_dict = 10 / depth_dict[key]
1774  if depth_dict[key] < 3:
1775  node_labels_dict[key] = key
1776  else:
1777  node_labels_dict[key] = ""
1778  draw_graph(graph, labels_dict=node_labels_dict)
1779 
1780 
1781 def recursive_graph(hier, graph, depth, depth_dict):
1782  depth = depth + 1
1783  nameh = IMP.atom.Hierarchy(hier).get_name()
1784  index = str(hier.get_particle().get_index())
1785  name1 = nameh + "|#" + index
1786  depth_dict[name1] = depth
1787 
1788  children = IMP.atom.Hierarchy(hier).get_children()
1789 
1790  if len(children) == 1 or children is None:
1791  depth = depth - 1
1792  return (graph, depth, depth_dict)
1793 
1794  else:
1795  for c in children:
1796  (graph, depth, depth_dict) = recursive_graph(
1797  c, graph, depth, depth_dict)
1798  nameh = IMP.atom.Hierarchy(c).get_name()
1799  index = str(c.get_particle().get_index())
1800  namec = nameh + "|#" + index
1801  graph.append((name1, namec))
1802 
1803  depth = depth - 1
1804  return (graph, depth, depth_dict)
1805 
1806 
1807 def draw_graph(graph, labels_dict=None, graph_layout='spring',
1808  node_size=5, node_color=None, node_alpha=0.3,
1809  node_text_size=11, fixed=None, pos=None,
1810  edge_color='blue', edge_alpha=0.3, edge_thickness=1,
1811  edge_text_pos=0.3,
1812  validation_edges=None,
1813  text_font='sans-serif',
1814  out_filename=None):
1815 
1816  import matplotlib as mpl
1817  mpl.use('Agg')
1818  import networkx as nx
1819  import matplotlib.pyplot as plt
1820  from math import sqrt, pi
1821 
1822  # create networkx graph
1823  G = nx.Graph()
1824 
1825  # add edges
1826  if type(edge_thickness) is list:
1827  for edge,weight in zip(graph,edge_thickness):
1828  G.add_edge(edge[0], edge[1], weight=weight)
1829  else:
1830  for edge in graph:
1831  G.add_edge(edge[0], edge[1])
1832 
1833  if node_color==None:
1834  node_color_rgb=(0,0,0)
1835  node_color_hex="000000"
1836  else:
1837  cc=IMP.pmi.tools.ColorChange()
1838  tmpcolor_rgb=[]
1839  tmpcolor_hex=[]
1840  for node in G.nodes():
1841  cctuple=cc.rgb(node_color[node])
1842  tmpcolor_rgb.append((cctuple[0]/255,cctuple[1]/255,cctuple[2]/255))
1843  tmpcolor_hex.append(node_color[node])
1844  node_color_rgb=tmpcolor_rgb
1845  node_color_hex=tmpcolor_hex
1846 
1847  # get node sizes if dictionary
1848  if type(node_size) is dict:
1849  tmpsize=[]
1850  for node in G.nodes():
1851  size=sqrt(node_size[node])/pi*10.0
1852  tmpsize.append(size)
1853  node_size=tmpsize
1854 
1855  for n,node in enumerate(G.nodes()):
1856  color=node_color_hex[n]
1857  size=node_size[n]
1858  nx.set_node_attributes(G, "graphics", {node : {'type': 'ellipse','w': size, 'h': size,'fill': '#'+color, 'label': node}})
1859  nx.set_node_attributes(G, "LabelGraphics", {node : {'type': 'text','text':node, 'color':'#000000', 'visible':'true'}})
1860 
1861  for edge in G.edges():
1862  nx.set_edge_attributes(G, "graphics", {edge : {'width': 1,'fill': '#000000'}})
1863 
1864  for ve in validation_edges:
1865  print(ve)
1866  if (ve[0],ve[1]) in G.edges():
1867  print("found forward")
1868  nx.set_edge_attributes(G, "graphics", {ve : {'width': 1,'fill': '#00FF00'}})
1869  elif (ve[1],ve[0]) in G.edges():
1870  print("found backward")
1871  nx.set_edge_attributes(G, "graphics", {(ve[1],ve[0]) : {'width': 1,'fill': '#00FF00'}})
1872  else:
1873  G.add_edge(ve[0], ve[1])
1874  print("not found")
1875  nx.set_edge_attributes(G, "graphics", {ve : {'width': 1,'fill': '#FF0000'}})
1876 
1877  # these are different layouts for the network you may try
1878  # shell seems to work best
1879  if graph_layout == 'spring':
1880  print(fixed, pos)
1881  graph_pos = nx.spring_layout(G,k=1.0/8.0,fixed=fixed,pos=pos)
1882  elif graph_layout == 'spectral':
1883  graph_pos = nx.spectral_layout(G)
1884  elif graph_layout == 'random':
1885  graph_pos = nx.random_layout(G)
1886  else:
1887  graph_pos = nx.shell_layout(G)
1888 
1889 
1890  # draw graph
1891  nx.draw_networkx_nodes(G, graph_pos, node_size=node_size,
1892  alpha=node_alpha, node_color=node_color_rgb,
1893  linewidths=0)
1894  nx.draw_networkx_edges(G, graph_pos, width=edge_thickness,
1895  alpha=edge_alpha, edge_color=edge_color)
1896  nx.draw_networkx_labels(
1897  G, graph_pos, labels=labels_dict, font_size=node_text_size,
1898  font_family=text_font)
1899  if out_filename:
1900  plt.savefig(out_filename)
1901  nx.write_gml(G,'out.gml')
1902  plt.show()
1903 
1904 
1905 def draw_table():
1906 
1907  # still an example!
1908 
1909  from ipyD3 import d3object
1910  from IPython.display import display
1911 
1912  d3 = d3object(width=800,
1913  height=400,
1914  style='JFTable',
1915  number=1,
1916  d3=None,
1917  title='Example table with d3js',
1918  desc='An example table created created with d3js with data generated with Python.')
1919  data = [
1920  [1277.0,
1921  654.0,
1922  288.0,
1923  1976.0,
1924  3281.0,
1925  3089.0,
1926  10336.0,
1927  4650.0,
1928  4441.0,
1929  4670.0,
1930  944.0,
1931  110.0],
1932  [1318.0,
1933  664.0,
1934  418.0,
1935  1952.0,
1936  3581.0,
1937  4574.0,
1938  11457.0,
1939  6139.0,
1940  7078.0,
1941  6561.0,
1942  2354.0,
1943  710.0],
1944  [1783.0,
1945  774.0,
1946  564.0,
1947  1470.0,
1948  3571.0,
1949  3103.0,
1950  9392.0,
1951  5532.0,
1952  5661.0,
1953  4991.0,
1954  2032.0,
1955  680.0],
1956  [1301.0,
1957  604.0,
1958  286.0,
1959  2152.0,
1960  3282.0,
1961  3369.0,
1962  10490.0,
1963  5406.0,
1964  4727.0,
1965  3428.0,
1966  1559.0,
1967  620.0],
1968  [1537.0,
1969  1714.0,
1970  724.0,
1971  4824.0,
1972  5551.0,
1973  8096.0,
1974  16589.0,
1975  13650.0,
1976  9552.0,
1977  13709.0,
1978  2460.0,
1979  720.0],
1980  [5691.0,
1981  2995.0,
1982  1680.0,
1983  11741.0,
1984  16232.0,
1985  14731.0,
1986  43522.0,
1987  32794.0,
1988  26634.0,
1989  31400.0,
1990  7350.0,
1991  3010.0],
1992  [1650.0,
1993  2096.0,
1994  60.0,
1995  50.0,
1996  1180.0,
1997  5602.0,
1998  15728.0,
1999  6874.0,
2000  5115.0,
2001  3510.0,
2002  1390.0,
2003  170.0],
2004  [72.0, 60.0, 60.0, 10.0, 120.0, 172.0, 1092.0, 675.0, 408.0, 360.0, 156.0, 100.0]]
2005  data = [list(i) for i in zip(*data)]
2006  sRows = [['January',
2007  'February',
2008  'March',
2009  'April',
2010  'May',
2011  'June',
2012  'July',
2013  'August',
2014  'September',
2015  'October',
2016  'November',
2017  'Deecember']]
2018  sColumns = [['Prod {0}'.format(i) for i in range(1, 9)],
2019  [None, '', None, None, 'Group 1', None, None, 'Group 2']]
2020  d3.addSimpleTable(data,
2021  fontSizeCells=[12, ],
2022  sRows=sRows,
2023  sColumns=sColumns,
2024  sRowsMargins=[5, 50, 0],
2025  sColsMargins=[5, 20, 10],
2026  spacing=0,
2027  addBorders=1,
2028  addOutsideBorders=-1,
2029  rectWidth=45,
2030  rectHeight=0
2031  )
2032  html = d3.render(mode=['html', 'show'])
2033  display(html)
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: Residue.h:155
A container for models organized into clusters.
Definition: output.py:1337
A class for reading stat files (either rmf or ascii v1 and v2)
Definition: output.py:773
atom::Hierarchies create_hierarchies(RMF::FileConstHandle fh, Model *m)
RMF::FrameID save_frame(RMF::FileHandle file, std::string name="")
Save the current state of the linked objects as a new RMF frame.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: atom/Atom.h:241
def plot_field_histogram
Plot a list of histograms from a value list.
Definition: output.py:1579
def plot_fields_box_plots
Plot time series as boxplots.
Definition: output.py:1644
Utility classes and functions for reading and storing PMI files.
def get_best_models
Given a list of stat files, read them all and find the best models.
Miscellaneous utilities.
Definition: tools.py:1
A class to store data associated to a model.
Definition: output.py:1316
void handle_use_deprecated(std::string message)
std::string get_module_version()
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
Collect statistics from ProcessOutput.get_fields().
Definition: output.py:762
def get_prot_name_from_particle
Return the component name provided a particle and a list of names.
Definition: tools.py:1043
def get_fields
Get the desired field names, and return a dictionary.
Definition: output.py:835
static bool get_is_setup(Model *m, ParticleIndex pi)
Definition: Fragment.h:46
def link_to_rmf
Link to another RMF file.
Definition: output.py:986
std::string get_molecule_name_and_copy(atom::Hierarchy h)
Walk up a PMI2 hierarchy/representations and get the "molname.copynum".
Definition: utilities.h:85
The standard decorator for manipulating molecular structures.
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
def init_pdb
Init PDB Writing.
Definition: output.py:114
int get_number_of_frames(const ::npctransport_proto::Assignment &config, double time_step)
A decorator for a particle representing an atom.
Definition: atom/Atom.h:234
Base class for capturing a modeling protocol.
Definition: output.py:40
The type for a residue.
void load_frame(RMF::FileConstHandle file, RMF::FrameID frame)
Load the given RMF frame into the state of the linked objects.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
A base class for Keys.
Definition: Key.h:44
void add_hierarchies(RMF::NodeHandle fh, const atom::Hierarchies &hs)
Class for easy writing of PDBs, RMFs, and stat files.
Definition: output.py:62
void add_geometries(RMF::NodeHandle parent, const display::GeometriesTemp &r)
Add geometries to a given parent node.
void add_restraints(RMF::NodeHandle fh, const Restraints &hs)
A decorator for a particle that is part of a rigid body but not rigid.
Definition: rigid_bodies.h:709
bool get_is_canonical(atom::Hierarchy h)
Walk up a PMI2 hierarchy/representations and check if the root is named System.
Definition: utilities.h:91
Display a segment connecting a pair of particles.
Definition: XYZR.h:170
A decorator for a residue.
Definition: Residue.h:134
Basic functionality that is expected to be used by a wide variety of IMP users.
def get_pdb_names
Get a list of all PDB files being output by this instance.
Definition: output.py:88
def get_prot_name_from_particle
Get the protein name from the particle.
Definition: output.py:207
class to link stat files to several rmf files
Definition: output.py:1088
class to allow more advanced handling of RMF files.
Definition: output.py:960
void link_hierarchies(RMF::FileConstHandle fh, const atom::Hierarchies &hs)
void add_geometry(RMF::FileHandle file, display::Geometry *r)
Add a single geometry to the file.
Store info for a chain of a protein.
Definition: Chain.h:61
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:66
def init_rmf
This function initialize an RMF file.
Definition: output.py:407
def get_residue_indexes
Retrieve the residue indexes for the given particle.
Definition: tools.py:1063
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: rigid_bodies.h:711
std::string get_module_version()
A decorator for a particle with x,y,z coordinates and a radius.
Definition: XYZR.h:27