1 """@namespace IMP.pmi.output
2 Classes for writing output files and processing them.
5 from __future__
import print_function, division
20 import cPickle
as pickle
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
34 ids.append(chars[ind % lc])
36 ids.append(chars[ind])
37 return "".join(reversed(ids))
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()
47 IMP.pmi.topology.System.add_protocol_output() (for PMI 2).
49 @see IMP.pmi.mmcif.ProtocolOutput for a concrete subclass that outputs
58 if t
is tuple
or t
is list:
59 for elt2
in _flatten(elt):
66 """Class for easy writing of PDBs, RMFs, and stat files"""
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
75 self.replica_exchange =
False
80 self.chainids =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
82 self.multi_chainids = _ChainIDs()
84 self.particle_infos_for_pdb = {}
85 self.atomistic=atomistic
88 def get_pdb_names(self):
89 return list(self.dictionary_pdbs.keys())
91 def get_rmf_names(self):
92 return list(self.dictionary_rmfs.keys())
94 def get_stat_names(self):
95 return list(self.dictionary_stats.keys())
97 def _init_dictchain(self, name, prot, multichar_chain=False):
98 self.dictchain[name] = {}
104 self.atomistic =
True
105 for n,mol
in enumerate(IMP.atom.get_by_type(prot,IMP.atom.MOLECULE_TYPE)):
109 chainids = self.multi_chainids
if multichar_chain
else self.chainids
110 for n, i
in enumerate(self.dictionary_pdbs[name].get_children()):
111 self.dictchain[name][i.get_name()] = chainids[n]
115 @param name The PDB filename
116 @param prot The hierarchy to write to this pdb file
117 \note if the PDB name is 'System' then will use Selection to get molecules
119 flpdb = open(name,
'w')
121 self.dictionary_pdbs[name] = prot
122 self._init_dictchain(name, prot)
124 def write_psf(self,filename,name):
125 flpsf=open(filename,
'w')
126 flpsf.write(
"PSF CMAP CHEQ"+
"\n")
127 index_residue_pair_list={}
128 (particle_infos_for_pdb, geometric_center)=self.get_particle_infos_for_pdb_writing(name)
129 nparticles=len(particle_infos_for_pdb)
130 flpsf.write(str(nparticles)+
" !NATOM"+
"\n")
131 for n,p
in enumerate(particle_infos_for_pdb):
136 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))
139 if chain
not in index_residue_pair_list:
140 index_residue_pair_list[chain]=[(atom_index,resid)]
142 index_residue_pair_list[chain].append((atom_index,resid))
147 for chain
in sorted(index_residue_pair_list.keys()):
149 ls=index_residue_pair_list[chain]
151 ls=sorted(ls, key=
lambda tup: tup[1])
153 indexes=[x[0]
for x
in ls]
156 nbonds=len(indexes_pairs)
157 flpsf.write(str(nbonds)+
" !NBOND: bonds"+
"\n")
159 sublists=[indexes_pairs[i:i+4]
for i
in range(0,len(indexes_pairs),4)]
164 flpsf.write(
'{0:8d}{1:8d}{2:8d}{3:8d}{4:8d}{5:8d}{6:8d}{7:8d}'.format(ip[0][0],ip[0][1],
165 ip[1][0],ip[1][1],ip[2][0],ip[2][1],ip[3][0],ip[3][1]))
167 flpsf.write(
'{0:8d}{1:8d}{2:8d}{3:8d}{4:8d}{5:8d}'.format(ip[0][0],ip[0][1],ip[1][0],
168 ip[1][1],ip[2][0],ip[2][1]))
170 flpsf.write(
'{0:8d}{1:8d}{2:8d}{3:8d}'.format(ip[0][0],ip[0][1],ip[1][0],ip[1][1]))
172 flpsf.write(
'{0:8d}{1:8d}'.format(ip[0][0],ip[0][1]))
175 del particle_infos_for_pdb
180 translate_to_geometric_center=
False,
181 write_all_residues_per_bead=
False):
183 flpdb = open(name,
'a')
185 flpdb = open(name,
'w')
187 (particle_infos_for_pdb,
188 geometric_center) = self.get_particle_infos_for_pdb_writing(name)
190 if not translate_to_geometric_center:
191 geometric_center = (0, 0, 0)
193 for n,tupl
in enumerate(particle_infos_for_pdb):
194 (xyz, atom_type, residue_type,
195 chain_id, residue_index, all_indexes, radius) = tupl
196 if atom_type
is None:
197 atom_type = IMP.atom.AT_CA
198 if ( (write_all_residues_per_bead)
and (all_indexes
is not None) ):
199 for residue_number
in all_indexes:
200 flpdb.write(IMP.atom.get_pdb_string((xyz[0] - geometric_center[0],
201 xyz[1] - geometric_center[1],
202 xyz[2] - geometric_center[2]),
203 n+1, atom_type, residue_type,
204 chain_id, residue_number,
' ',1.00,radius))
206 flpdb.write(IMP.atom.get_pdb_string((xyz[0] - geometric_center[0],
207 xyz[1] - geometric_center[1],
208 xyz[2] - geometric_center[2]),
209 n+1, atom_type, residue_type,
210 chain_id, residue_index,
' ',1.00,radius))
211 flpdb.write(
"ENDMDL\n")
214 del particle_infos_for_pdb
217 """Get the protein name from the particle.
218 This is done by traversing the hierarchy."""
223 p, self.dictchain[name])
225 def get_particle_infos_for_pdb_writing(self, name):
235 particle_infos_for_pdb = []
237 geometric_center = [0, 0, 0]
243 ps =
IMP.atom.Selection(self.dictionary_pdbs[name],resolution=0).get_selected_particles()
247 for n, p
in enumerate(ps):
250 if protname
not in resindexes_dict:
251 resindexes_dict[protname] = []
255 rt = residue.get_residue_type()
256 resind = residue.get_index()
260 geometric_center[0] += xyz[0]
261 geometric_center[1] += xyz[1]
262 geometric_center[2] += xyz[2]
264 particle_infos_for_pdb.append((xyz,
265 atomtype, rt, self.dictchain[name][protname], resind,
None, radius))
266 resindexes_dict[protname].append(resind)
271 resind = residue.get_index()
274 if resind
in resindexes_dict[protname]:
277 resindexes_dict[protname].append(resind)
278 rt = residue.get_residue_type()
281 geometric_center[0] += xyz[0]
282 geometric_center[1] += xyz[1]
283 geometric_center[2] += xyz[2]
285 particle_infos_for_pdb.append((xyz,
None,
286 rt, self.dictchain[name][protname], resind,
None, radius))
290 resind = resindexes[len(resindexes) // 2]
291 if resind
in resindexes_dict[protname]:
294 resindexes_dict[protname].append(resind)
298 geometric_center[0] += xyz[0]
299 geometric_center[1] += xyz[1]
300 geometric_center[2] += xyz[2]
302 particle_infos_for_pdb.append((xyz,
None,
303 rt, self.dictchain[name][protname], resind, resindexes, radius))
309 if len(resindexes) > 0:
310 resind = resindexes[len(resindexes) // 2]
313 geometric_center[0] += xyz[0]
314 geometric_center[1] += xyz[1]
315 geometric_center[2] += xyz[2]
317 particle_infos_for_pdb.append((xyz,
None,
318 rt, self.dictchain[name][protname], resind, resindexes, radius))
321 geometric_center = (geometric_center[0] / atom_count,
322 geometric_center[1] / atom_count,
323 geometric_center[2] / atom_count)
327 particle_infos_for_pdb = sorted(particle_infos_for_pdb,
328 key=
lambda x: (len(x[3]), x[3], x[4]))
330 return (particle_infos_for_pdb, geometric_center)
333 def write_pdbs(self, appendmode=True):
334 for pdb
in self.dictionary_pdbs.keys():
335 self.write_pdb(pdb, appendmode)
337 def init_pdb_best_scoring(self,
341 replica_exchange=
False):
345 self.suffixes.append(suffix)
346 self.replica_exchange = replica_exchange
347 if not self.replica_exchange:
351 self.best_score_list = []
355 self.best_score_file_name =
"best.scores.rex.py"
356 self.best_score_list = []
357 best_score_file = open(self.best_score_file_name,
"w")
358 best_score_file.write(
359 "self.best_score_list=" + str(self.best_score_list))
360 best_score_file.close()
362 self.nbestscoring = nbestscoring
363 for i
in range(self.nbestscoring):
364 name = suffix +
"." + str(i) +
".pdb"
365 flpdb = open(name,
'w')
367 self.dictionary_pdbs[name] = prot
368 self._init_dictchain(name, prot)
370 def write_pdb_best_scoring(self, score):
371 if self.nbestscoring
is None:
372 print(
"Output.write_pdb_best_scoring: init_pdb_best_scoring not run")
375 if self.replica_exchange:
377 exec(open(self.best_score_file_name).read())
379 if len(self.best_score_list) < self.nbestscoring:
380 self.best_score_list.append(score)
381 self.best_score_list.sort()
382 index = self.best_score_list.index(score)
383 for suffix
in self.suffixes:
384 for i
in range(len(self.best_score_list) - 2, index - 1, -1):
385 oldname = suffix +
"." + str(i) +
".pdb"
386 newname = suffix +
"." + str(i + 1) +
".pdb"
388 if os.path.exists(newname):
390 os.rename(oldname, newname)
391 filetoadd = suffix +
"." + str(index) +
".pdb"
392 self.write_pdb(filetoadd, appendmode=
False)
395 if score < self.best_score_list[-1]:
396 self.best_score_list.append(score)
397 self.best_score_list.sort()
398 self.best_score_list.pop(-1)
399 index = self.best_score_list.index(score)
400 for suffix
in self.suffixes:
401 for i
in range(len(self.best_score_list) - 1, index - 1, -1):
402 oldname = suffix +
"." + str(i) +
".pdb"
403 newname = suffix +
"." + str(i + 1) +
".pdb"
404 os.rename(oldname, newname)
405 filenametoremove = suffix + \
406 "." + str(self.nbestscoring) +
".pdb"
407 os.remove(filenametoremove)
408 filetoadd = suffix +
"." + str(index) +
".pdb"
409 self.write_pdb(filetoadd, appendmode=
False)
411 if self.replica_exchange:
413 best_score_file = open(self.best_score_file_name,
"w")
414 best_score_file.write(
415 "self.best_score_list=" + str(self.best_score_list))
416 best_score_file.close()
418 def init_rmf(self, name, hierarchies, rs=None, geometries=None, listofobjects=None):
420 This function initialize an RMF file
422 @param name the name of the RMF file
423 @param hierarchies the hiearchies to be included (it is a list)
424 @param rs optional, the restraint sets (it is a list)
425 @param geometries optional, the geometries (it is a list)
426 @param listofobjects optional, the list of objects for the stat (it is a list)
428 rh = RMF.create_rmf_file(name)
431 outputkey_rmfkey=
None
435 if geometries
is not None:
437 if listofobjects
is not None:
438 cat = rh.get_category(
"stat")
440 for l
in listofobjects:
441 if not "get_output" in dir(l):
442 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(l))
443 output=l.get_output()
444 for outputkey
in output:
445 rmftag=RMF.string_tag
446 if type(output[outputkey])
is float:
448 elif type(output[outputkey])
is int:
450 elif type(output[outputkey])
is str:
451 rmftag = RMF.string_tag
453 rmftag = RMF.string_tag
454 rmfkey=rh.get_key(cat, outputkey, rmftag)
455 outputkey_rmfkey[outputkey]=rmfkey
456 outputkey_rmfkey[
"rmf_file"]=rh.get_key(cat,
"rmf_file", RMF.string_tag)
457 outputkey_rmfkey[
"rmf_frame_index"]=rh.get_key(cat,
"rmf_frame_index", RMF.int_tag)
459 self.dictionary_rmfs[name] = (rh,cat,outputkey_rmfkey,listofobjects)
461 def add_restraints_to_rmf(self, name, objectlist):
462 flatobjectlist=_flatten(objectlist)
463 for o
in flatobjectlist:
465 rs = o.get_restraint_for_rmf()
467 rs = o.get_restraint()
469 self.dictionary_rmfs[name][0],
472 def add_geometries_to_rmf(self, name, objectlist):
474 geos = o.get_geometries()
477 def add_particle_pair_from_restraints_to_rmf(self, name, objectlist):
480 pps = o.get_particle_pairs()
483 self.dictionary_rmfs[name][0],
486 def write_rmf(self, name):
488 if self.dictionary_rmfs[name][1]
is not None:
489 cat=self.dictionary_rmfs[name][1]
490 outputkey_rmfkey=self.dictionary_rmfs[name][2]
491 listofobjects=self.dictionary_rmfs[name][3]
492 for l
in listofobjects:
493 output=l.get_output()
494 for outputkey
in output:
495 rmfkey=outputkey_rmfkey[outputkey]
497 self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey,output[outputkey])
498 except NotImplementedError:
500 rmfkey = outputkey_rmfkey[
"rmf_file"]
501 self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey, name)
502 rmfkey = outputkey_rmfkey[
"rmf_frame_index"]
504 self.dictionary_rmfs[name][0].get_root_node().set_value(rmfkey, nframes-1)
505 self.dictionary_rmfs[name][0].flush()
507 def close_rmf(self, name):
508 rh = self.dictionary_rmfs[name][0]
509 del self.dictionary_rmfs[name]
512 def write_rmfs(self):
513 for rmfinfo
in self.dictionary_rmfs.keys():
514 self.write_rmf(rmfinfo[0])
516 def init_stat(self, name, listofobjects):
518 flstat = open(name,
'w')
521 flstat = open(name,
'wb')
525 for l
in listofobjects:
526 if not "get_output" in dir(l):
527 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(l))
528 self.dictionary_stats[name] = listofobjects
530 def set_output_entry(self, key, value):
531 self.initoutput.update({key: value})
533 def write_stat(self, name, appendmode=True):
534 output = self.initoutput
535 for obj
in self.dictionary_stats[name]:
538 dfiltered = dict((k, v)
for k, v
in d.items()
if k[0] !=
"_")
539 output.update(dfiltered)
547 flstat = open(name, writeflag)
548 flstat.write(
"%s \n" % output)
551 flstat = open(name, writeflag +
'b')
552 cPickle.dump(output, flstat, 2)
555 def write_stats(self):
556 for stat
in self.dictionary_stats.keys():
557 self.write_stat(stat)
559 def get_stat(self, name):
561 for obj
in self.dictionary_stats[name]:
562 output.update(obj.get_output())
565 def write_test(self, name, listofobjects):
572 flstat = open(name,
'w')
573 output = self.initoutput
574 for l
in listofobjects:
575 if not "get_test_output" in dir(l)
and not "get_output" in dir(l):
576 raise ValueError(
"Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
577 self.dictionary_stats[name] = listofobjects
579 for obj
in self.dictionary_stats[name]:
581 d = obj.get_test_output()
585 dfiltered = dict((k, v)
for k, v
in d.items()
if k[0] !=
"_")
586 output.update(dfiltered)
590 flstat.write(
"%s \n" % output)
593 def test(self, name, listofobjects, tolerance=1e-5):
594 output = self.initoutput
595 for l
in listofobjects:
596 if not "get_test_output" in dir(l)
and not "get_output" in dir(l):
597 raise ValueError(
"Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
598 for obj
in listofobjects:
600 output.update(obj.get_test_output())
602 output.update(obj.get_output())
607 flstat = open(name,
'r')
611 test_dict = ast.literal_eval(l)
614 old_value = str(test_dict[k])
615 new_value = str(output[k])
623 fold = float(old_value)
624 fnew = float(new_value)
625 diff = abs(fold - fnew)
627 print(
"%s: test failed, old value: %s new value %s; "
628 "diff %f > %f" % (str(k), str(old_value),
629 str(new_value), diff,
630 tolerance), file=sys.stderr)
632 elif test_dict[k] != output[k]:
633 if len(old_value) < 50
and len(new_value) < 50:
634 print(
"%s: test failed, old value: %s new value %s"
635 % (str(k), old_value, new_value), file=sys.stderr)
638 print(
"%s: test failed, omitting results (too long)"
639 % str(k), file=sys.stderr)
643 print(
"%s from old objects (file %s) not in new objects"
644 % (str(k), str(name)), file=sys.stderr)
647 def get_environment_variables(self):
649 return str(os.environ)
651 def get_versions_of_relevant_modules(self):
658 except (ImportError):
662 versions[
"ISD2_VERSION"] = IMP.isd2.get_module_version()
663 except (ImportError):
667 versions[
"ISD_EMXL_VERSION"] = IMP.isd_emxl.get_module_version()
668 except (ImportError):
678 listofsummedobjects=
None):
684 if listofsummedobjects
is None:
685 listofsummedobjects = []
686 if extralabels
is None:
688 flstat = open(name,
'w')
690 stat2_keywords = {
"STAT2HEADER":
"STAT2HEADER"}
691 stat2_keywords.update(
692 {
"STAT2HEADER_ENVIRON": str(self.get_environment_variables())})
693 stat2_keywords.update(
694 {
"STAT2HEADER_IMP_VERSIONS": str(self.get_versions_of_relevant_modules())})
697 for l
in listofobjects:
698 if not "get_output" in dir(l):
699 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(l))
703 dfiltered = dict((k, v)
704 for k, v
in d.items()
if k[0] !=
"_")
705 output.update(dfiltered)
708 for l
in listofsummedobjects:
710 if not "get_output" in dir(t):
711 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(t))
713 if "_TotalScore" not in t.get_output():
714 raise ValueError(
"Output: object %s doesn't have _TotalScore entry to be summed" % str(t))
716 output.update({l[1]: 0.0})
718 for k
in extralabels:
719 output.update({k: 0.0})
721 for n, k
in enumerate(output):
722 stat2_keywords.update({n: k})
723 stat2_inverse.update({k: n})
725 flstat.write(
"%s \n" % stat2_keywords)
727 self.dictionary_stats2[name] = (
733 def write_stat2(self, name, appendmode=True):
735 (listofobjects, stat2_inverse, listofsummedobjects,
736 extralabels) = self.dictionary_stats2[name]
739 for obj
in listofobjects:
740 od = obj.get_output()
741 dfiltered = dict((k, v)
for k, v
in od.items()
if k[0] !=
"_")
743 output.update({stat2_inverse[k]: od[k]})
746 for l
in listofsummedobjects:
750 partial_score += float(d[
"_TotalScore"])
751 output.update({stat2_inverse[l[1]]: str(partial_score)})
754 for k
in extralabels:
755 if k
in self.initoutput:
756 output.update({stat2_inverse[k]: self.initoutput[k]})
758 output.update({stat2_inverse[k]:
"None"})
765 flstat = open(name, writeflag)
766 flstat.write(
"%s \n" % output)
769 def write_stats2(self):
770 for stat
in self.dictionary_stats2.keys():
771 self.write_stat2(stat)
775 """Collect statistics from ProcessOutput.get_fields().
776 Counters of the total number of frames read, plus the models that
777 passed the various filters used in get_fields(), are provided."""
780 self.passed_get_every = 0
781 self.passed_filterout = 0
782 self.passed_filtertuple = 0
786 """A class for reading stat files (either rmf or ascii v1 and v2)"""
787 def __init__(self, filename):
788 self.filename = filename
794 if not self.filename
is None:
795 f = open(self.filename,
"r")
797 raise ValueError(
"No file name provided. Use -h for help")
801 rh = RMF.open_rmf_file_read_only(self.filename)
803 cat=rh.get_category(
'stat')
804 rmf_klist=rh.get_keys(cat)
805 self.rmf_names_keys=dict([(rh.get_name(k),k)
for k
in rmf_klist])
811 for line
in f.readlines():
812 d = ast.literal_eval(line)
813 self.klist = list(d.keys())
815 if "STAT2HEADER" in self.klist:
818 if "STAT2HEADER" in str(k):
824 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
826 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
827 self.invstat2_dict = {}
829 self.invstat2_dict.update({stat2_dict[k]: k})
832 "Please convert to statfile v2.\n")
842 return sorted(self.rmf_names_keys.keys())
846 def show_keys(self, ncolumns=2, truncate=65):
847 IMP.pmi.tools.print_multicolumn(self.get_keys(), ncolumns, truncate)
849 def get_fields(self, fields, filtertuple=None, filterout=None, get_every=1,
852 Get the desired field names, and return a dictionary.
853 Namely, "fields" are the queried keys in the stat file (eg. ["Total_Score",...])
854 The returned data structure is a dictionary, where each key is a field and the value
855 is the time series (ie, frame ordered series)
856 of that field (ie, {"Total_Score":[Score_0,Score_1,Score_2,Score_3,...],....} )
858 @param fields (list of strings) queried keys in the stat file (eg. "Total_Score"....)
859 @param filterout specify if you want to "grep" out something from
860 the file, so that it is faster
861 @param filtertuple a tuple that contains
862 ("TheKeyToBeFiltered",relationship,value)
863 where relationship = "<", "==", or ">"
864 @param get_every only read every Nth line from the file
865 @param statistics if provided, accumulate statistics in an
866 OutputStatistics object
869 if statistics
is None:
877 rh = RMF.open_rmf_file_read_only(self.filename)
878 nframes=rh.get_number_of_frames()
879 for i
in range(nframes):
880 statistics.total += 1
882 statistics.passed_get_every += 1
883 statistics.passed_filterout += 1
885 if not filtertuple
is None:
886 keytobefiltered = filtertuple[0]
887 relationship = filtertuple[1]
888 value = filtertuple[2]
889 datavalue=rh.get_root_node().get_value(self.rmf_names_keys[keytobefiltered])
890 if self.isfiltered(datavalue,relationship,value):
continue
892 statistics.passed_filtertuple += 1
894 outdict[field].append(rh.get_root_node().get_value(self.rmf_names_keys[field]))
897 f = open(self.filename,
"r")
900 for line
in f.readlines():
901 statistics.total += 1
902 if not filterout
is None:
903 if filterout
in line:
905 statistics.passed_filterout += 1
908 if line_number % get_every != 0:
909 if line_number == 1
and self.isstat2:
910 statistics.total -= 1
911 statistics.passed_filterout -= 1
913 statistics.passed_get_every += 1
917 d = ast.literal_eval(line)
919 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
924 if not filtertuple
is None:
925 keytobefiltered = filtertuple[0]
926 relationship = filtertuple[1]
927 value = filtertuple[2]
928 datavalue=d[keytobefiltered]
929 if self.isfiltered(datavalue, relationship, value):
continue
931 statistics.passed_filtertuple += 1
932 [outdict[field].append(d[field])
for field
in fields]
936 statistics.total -= 1
937 statistics.passed_filterout -= 1
938 statistics.passed_get_every -= 1
941 if not filtertuple
is None:
942 keytobefiltered = filtertuple[0]
943 relationship = filtertuple[1]
944 value = filtertuple[2]
945 datavalue=d[self.invstat2_dict[keytobefiltered]]
946 if self.isfiltered(datavalue, relationship, value):
continue
948 statistics.passed_filtertuple += 1
949 [outdict[field].append(d[self.invstat2_dict[field]])
for field
in fields]
955 def isfiltered(self,datavalue,relationship,refvalue):
958 fdatavalue=float(datavalue)
960 raise ValueError(
"ProcessOutput.filter: datavalue cannot be converted into a float")
962 if relationship ==
"<":
963 if float(datavalue) >= refvalue:
965 if relationship ==
">":
966 if float(datavalue) <= refvalue:
968 if relationship ==
"==":
969 if float(datavalue) != refvalue:
975 """ class to allow more advanced handling of RMF files.
976 It is both a container and a IMP.atom.Hierarchy.
977 - it is iterable (while loading the corresponding frame)
978 - Item brackets [] load the corresponding frame
979 - slice create an iterator
980 - can relink to another RMF file
984 @param model: the IMP.Model()
985 @param rmf_file_name: str, path of the rmf file
989 self.rh_ref = RMF.open_rmf_file_read_only(rmf_file_name)
991 raise TypeError(
"Wrong rmf file name or type: %s"% str(rmf_file_name))
994 self.root_hier_ref = hs[0]
995 IMP.atom.Hierarchy.__init__(self, self.root_hier_ref)
997 self.ColorHierarchy=
None
1002 Link to another RMF file
1004 self.rh_ref = RMF.open_rmf_file_read_only(rmf_file_name)
1006 if self.ColorHierarchy:
1007 self.ColorHierarchy.method()
1008 RMFHierarchyHandler.set_frame(self,0)
1010 def set_frame(self,index):
1014 print(
"skipping frame %s:%d\n"%(self.current_rmf, index))
1018 return self.rh_ref.get_number_of_frames()
1020 def __getitem__(self,int_slice_adaptor):
1021 if type(int_slice_adaptor)
is int:
1022 self.set_frame(int_slice_adaptor)
1023 return int_slice_adaptor
1024 elif type(int_slice_adaptor)
is slice:
1025 return self.__iter__(int_slice_adaptor)
1027 raise TypeError(
"Unknown Type")
1030 return self.get_number_of_frames()
1032 def __iter__(self,slice_key=None):
1033 if slice_key
is None:
1034 for nframe
in range(len(self)):
1037 for nframe
in list(range(len(self)))[slice_key]:
1040 class CacheHierarchyCoordinates(object):
1041 def __init__(self,StatHierarchyHandler):
1048 self.current_index=
None
1049 self.rmfh=StatHierarchyHandler
1051 self.model=self.rmfh.get_model()
1056 self.nrms.append(nrm)
1059 self.xyzs.append(fb)
1061 def do_store(self,index):
1062 self.rb_trans[index]={}
1063 self.nrm_coors[index]={}
1064 self.xyz_coors[index]={}
1066 self.rb_trans[index][rb]=rb.get_reference_frame()
1067 for nrm
in self.nrms:
1068 self.nrm_coors[index][nrm]=nrm.get_internal_coordinates()
1069 for xyz
in self.xyzs:
1070 self.xyz_coors[index][xyz]=xyz.get_coordinates()
1071 self.current_index=index
1073 def do_update(self,index):
1074 if self.current_index!=index:
1076 rb.set_reference_frame(self.rb_trans[index][rb])
1077 for nrm
in self.nrms:
1078 nrm.set_internal_coordinates(self.nrm_coors[index][nrm])
1079 for xyz
in self.xyzs:
1080 xyz.set_coordinates(self.xyz_coors[index][xyz])
1081 self.current_index=index
1085 return len(self.rb_trans.keys())
1087 def __getitem__(self,index):
1088 if type(index)
is int:
1089 if index
in self.rb_trans.keys():
1094 raise TypeError(
"Unknown Type")
1097 return self.get_number_of_frames()
1103 """ class to link stat files to several rmf files """
1104 def __init__(self,model=None,stat_file=None,number_best_scoring_models=None,score_key=None,StatHierarchyHandler=None,cache=None):
1107 @param model: IMP.Model()
1108 @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
1109 stat file name as key and a list of frames as values
1110 @param number_best_scoring_models:
1111 @param StatHierarchyHandler: copy constructor input object
1112 @param cache: cache coordinates and rigid body transformations.
1115 if not StatHierarchyHandler
is None:
1118 self.model=StatHierarchyHandler.model
1119 self.data=StatHierarchyHandler.data
1120 self.number_best_scoring_models=StatHierarchyHandler.number_best_scoring_models
1122 self.current_rmf=StatHierarchyHandler.current_rmf
1123 self.current_frame=
None
1124 self.current_index=
None
1125 self.score_threshold=StatHierarchyHandler.score_threshold
1126 self.score_key=StatHierarchyHandler.score_key
1127 self.cache=StatHierarchyHandler.cache
1128 RMFHierarchyHandler.__init__(self, self.model,self.current_rmf)
1130 self.cache=CacheHierarchyCoordinates(self)
1139 self.number_best_scoring_models=number_best_scoring_models
1142 if score_key
is None:
1143 self.score_key=
"Total_Score"
1145 self.score_key=score_key
1147 self.current_rmf=
None
1148 self.current_frame=
None
1149 self.current_index=
None
1150 self.score_threshold=
None
1152 if type(stat_file)
is str:
1153 self.add_stat_file(stat_file)
1154 elif type(stat_file)
is list:
1156 self.add_stat_file(f)
1158 def add_stat_file(self,stat_file):
1160 import cPickle
as pickle
1165 '''check that it is not a pickle file with saved data from a previous calculation'''
1166 self.load_data(stat_file)
1168 if self.number_best_scoring_models:
1169 scores = self.get_scores()
1170 max_score = sorted(scores)[0:min(len(self), self.number_best_scoring_models)][-1]
1171 self.do_filter_by_score(max_score)
1173 except pickle.UnpicklingError:
1174 '''alternatively read the ascii stat files'''
1176 scores,rmf_files,rmf_frame_indexes,features = self.get_info_from_stat_file(stat_file, self.score_threshold)
1181 rh = RMF.open_rmf_file_read_only(stat_file)
1182 nframes = rh.get_number_of_frames()
1183 scores=[0.0]*nframes
1184 rmf_files=[stat_file]*nframes
1185 rmf_frame_indexes=range(nframes)
1191 if len(set(rmf_files)) > 1:
1192 raise (
"Multiple RMF files found")
1195 print(
"StatHierarchyHandler: Error: Trying to set none as rmf_file (probably empty stat file), aborting")
1198 for n,index
in enumerate(rmf_frame_indexes):
1199 featn_dict=dict([(k,features[k][n])
for k
in features])
1202 if self.number_best_scoring_models:
1203 scores=self.get_scores()
1204 max_score=sorted(scores)[0:min(len(self),self.number_best_scoring_models)][-1]
1205 self.do_filter_by_score(max_score)
1207 if not self.is_setup:
1208 RMFHierarchyHandler.__init__(self, self.model,self.get_rmf_names()[0])
1210 self.cache=CacheHierarchyCoordinates(self)
1214 self.current_rmf=self.get_rmf_names()[0]
1218 def save_data(self,filename='data.pkl'):
1220 import cPickle
as pickle
1223 fl=open(filename,
'wb')
1224 pickle.dump(self.data,fl)
1226 def load_data(self,filename='data.pkl'):
1228 import cPickle
as pickle
1231 fl=open(filename,
'rb')
1232 data_structure=pickle.load(fl)
1234 if not type(data_structure)
is list:
1235 raise TypeError(
"%filename should contain a list of IMP.pmi.output.DataEntry or IMP.pmi.output.Cluster" % filename)
1238 self.data=data_structure
1241 for cluster
in data_structure:
1242 nmodels+=len(cluster)
1243 self.data=[
None]*nmodels
1244 for cluster
in data_structure:
1245 for n,data
in enumerate(cluster):
1246 index=cluster.members[n]
1247 self.data[index]=data
1249 raise TypeError(
"%filename should contain a list of IMP.pmi.output.DataEntry or IMP.pmi.output.Cluster" % filename)
1251 def set_frame(self,index):
1252 if self.cache
is not None and self.cache[index]:
1253 self.cache.do_update(index)
1255 nm=self.data[index].rmf_name
1256 fidx=self.data[index].rmf_index
1257 if nm != self.current_rmf:
1260 self.current_frame=-1
1261 if fidx!=self.current_frame:
1262 RMFHierarchyHandler.set_frame(self, fidx)
1263 self.current_frame=fidx
1264 if self.cache
is not None:
1265 self.cache.do_store(index)
1267 self.current_index = index
1269 def __getitem__(self,int_slice_adaptor):
1270 if type(int_slice_adaptor)
is int:
1271 self.set_frame(int_slice_adaptor)
1272 return self.data[int_slice_adaptor]
1273 elif type(int_slice_adaptor)
is slice:
1274 return self.__iter__(int_slice_adaptor)
1276 raise TypeError(
"Unknown Type")
1279 return len(self.data)
1281 def __iter__(self,slice_key=None):
1282 if slice_key
is None:
1283 for i
in range(len(self)):
1286 for i
in range(len(self))[slice_key]:
1289 def do_filter_by_score(self,maximum_score):
1290 self.data=[d
for d
in self.data
if d.score<=maximum_score]
1292 def get_scores(self):
1293 return [d.score
for d
in self.data]
1295 def get_feature_series(self,feature_name):
1296 return [d.features[feature_name]
for d
in self.data]
1298 def get_feature_names(self):
1299 return self.data[0].features.keys()
1301 def get_rmf_names(self):
1302 return [d.rmf_name
for d
in self.data]
1304 def get_stat_files_names(self):
1305 return [d.stat_file
for d
in self.data]
1307 def get_rmf_indexes(self):
1308 return [d.rmf_index
for d
in self.data]
1310 def get_info_from_stat_file(self, stat_file, score_threshold=None):
1314 score_key=self.score_key,
1316 rmf_file_key=
"rmf_file",
1317 rmf_file_frame_key=
"rmf_frame_index",
1318 prefiltervalue=score_threshold,
1323 scores = [float(y)
for y
in models[2]]
1324 rmf_files = models[0]
1325 rmf_frame_indexes = models[1]
1327 return scores, rmf_files, rmf_frame_indexes,features
1332 A class to store data associated to a model
1334 def __init__(self,stat_file=None,rmf_name=None,rmf_index=None,score=None,features=None):
1335 self.rmf_name=rmf_name
1336 self.rmf_index=rmf_index
1338 self.features=features
1339 self.stat_file=stat_file
1342 s=
"IMP.pmi.output.DataEntry\n"
1343 s+=
"---- stat file %s \n"%(self.stat_file)
1344 s+=
"---- rmf file %s \n"%(self.rmf_name)
1345 s+=
"---- rmf index %s \n"%(str(self.rmf_index))
1346 s+=
"---- score %s \n"%(str(self.score))
1347 s+=
"---- number of features %s \n"%(str(len(self.features.keys())))
1353 A container for models organized into clusters
1355 def __init__(self,cid=None):
1359 self.center_index=
None
1360 self.members_data={}
1362 def add_member(self,index,data=None):
1363 self.members.append(index)
1364 self.members_data[index]=data
1365 self.average_score=self.compute_score()
1367 def compute_score(self):
1369 score=sum([d.score
for d
in self])/len(self)
1370 except AttributeError:
1375 s=
"IMP.pmi.output.Cluster\n"
1376 s+=
"---- cluster_id %s \n"%str(self.cluster_id)
1377 s+=
"---- precision %s \n"%str(self.precision)
1378 s+=
"---- average score %s \n"%str(self.average_score)
1379 s+=
"---- number of members %s \n"%str(len(self.members))
1380 s+=
"---- center index %s \n"%str(self.center_index)
1383 def __getitem__(self,int_slice_adaptor):
1384 if type(int_slice_adaptor)
is int:
1385 index=self.members[int_slice_adaptor]
1386 return self.members_data[index]
1387 elif type(int_slice_adaptor)
is slice:
1388 return self.__iter__(int_slice_adaptor)
1390 raise TypeError(
"Unknown Type")
1393 return len(self.members)
1395 def __iter__(self,slice_key=None):
1396 if slice_key
is None:
1397 for i
in range(len(self)):
1400 for i
in range(len(self))[slice_key]:
1403 def __add__(self, other):
1404 self.members+=other.members
1405 self.members_data.update(other.members_data)
1406 self.average_score=self.compute_score()
1408 self.center_index=
None
1412 def plot_clusters_populations(clusters):
1415 for cluster
in clusters:
1416 indexes.append(cluster.cluster_id)
1417 populations.append(len(cluster))
1419 import matplotlib.pyplot
as plt
1420 fig, ax = plt.subplots()
1421 ax.bar(indexes, populations, 0.5, color=
'r') #, yerr=men_std)
1422 ax.set_ylabel('Population')
1423 ax.set_xlabel((
'Cluster index'))
1426 def plot_clusters_precisions(clusters):
1429 for cluster
in clusters:
1430 indexes.append(cluster.cluster_id)
1432 prec=cluster.precision
1433 print(cluster.cluster_id,prec)
1436 precisions.append(prec)
1438 import matplotlib.pyplot
as plt
1439 fig, ax = plt.subplots()
1440 ax.bar(indexes, precisions, 0.5, color=
'r') #, yerr=men_std)
1441 ax.set_ylabel('Precision [A]')
1442 ax.set_xlabel((
'Cluster index'))
1445 def plot_clusters_scores(clusters):
1448 for cluster
in clusters:
1449 indexes.append(cluster.cluster_id)
1451 for data
in cluster:
1452 values[-1].append(data.score)
1455 valuename=
"Scores", positionname=
"Cluster index", xlabels=
None,scale_plot_length=1.0)
1457 class CrossLinkIdentifierDatabase(object):
1461 def check_key(self,key):
1462 if key
not in self.clidb:
1465 def set_unique_id(self,key,value):
1467 self.clidb[key][
"XLUniqueID"]=str(value)
1469 def set_protein1(self,key,value):
1471 self.clidb[key][
"Protein1"]=str(value)
1473 def set_protein2(self,key,value):
1475 self.clidb[key][
"Protein2"]=str(value)
1477 def set_residue1(self,key,value):
1479 self.clidb[key][
"Residue1"]=int(value)
1481 def set_residue2(self,key,value):
1483 self.clidb[key][
"Residue2"]=int(value)
1485 def set_idscore(self,key,value):
1487 self.clidb[key][
"IDScore"]=float(value)
1489 def set_state(self,key,value):
1491 self.clidb[key][
"State"]=int(value)
1493 def set_sigma1(self,key,value):
1495 self.clidb[key][
"Sigma1"]=str(value)
1497 def set_sigma2(self,key,value):
1499 self.clidb[key][
"Sigma2"]=str(value)
1501 def set_psi(self,key,value):
1503 self.clidb[key][
"Psi"]=str(value)
1505 def get_unique_id(self,key):
1506 return self.clidb[key][
"XLUniqueID"]
1508 def get_protein1(self,key):
1509 return self.clidb[key][
"Protein1"]
1511 def get_protein2(self,key):
1512 return self.clidb[key][
"Protein2"]
1514 def get_residue1(self,key):
1515 return self.clidb[key][
"Residue1"]
1517 def get_residue2(self,key):
1518 return self.clidb[key][
"Residue2"]
1520 def get_idscore(self,key):
1521 return self.clidb[key][
"IDScore"]
1523 def get_state(self,key):
1524 return self.clidb[key][
"State"]
1526 def get_sigma1(self,key):
1527 return self.clidb[key][
"Sigma1"]
1529 def get_sigma2(self,key):
1530 return self.clidb[key][
"Sigma2"]
1532 def get_psi(self,key):
1533 return self.clidb[key][
"Psi"]
1535 def set_float_feature(self,key,value,feature_name):
1537 self.clidb[key][feature_name]=float(value)
1539 def set_int_feature(self,key,value,feature_name):
1541 self.clidb[key][feature_name]=int(value)
1543 def set_string_feature(self,key,value,feature_name):
1545 self.clidb[key][feature_name]=str(value)
1547 def get_feature(self,key,feature_name):
1548 return self.clidb[key][feature_name]
1550 def write(self,filename):
1552 with open(filename,
'wb')
as handle:
1553 pickle.dump(self.clidb,handle)
1555 def load(self,filename):
1557 with open(filename,
'rb')
as handle:
1558 self.clidb=pickle.load(handle)
1560 def plot_fields(fields, framemin=None, framemax=None):
1561 import matplotlib
as mpl
1563 import matplotlib.pyplot
as plt
1565 plt.rc(
'lines', linewidth=4)
1566 fig, axs = plt.subplots(nrows=len(fields))
1567 fig.set_size_inches(10.5, 5.5 * len(fields))
1568 plt.rc(
'axes', color_cycle=[
'r'])
1572 if framemin
is None:
1574 if framemax
is None:
1575 framemax = len(fields[key])
1576 x = list(range(framemin, framemax))
1577 y = [float(y)
for y
in fields[key][framemin:framemax]]
1580 axs[n].set_title(key, size=
"xx-large")
1581 axs[n].tick_params(labelsize=18, pad=10)
1584 axs.set_title(key, size=
"xx-large")
1585 axs.tick_params(labelsize=18, pad=10)
1589 plt.subplots_adjust(hspace=0.3)
1594 name, values_lists, valuename=
None, bins=40, colors=
None, format=
"png",
1595 reference_xline=
None, yplotrange=
None, xplotrange=
None,normalized=
True,
1598 '''Plot a list of histograms from a value list.
1599 @param name the name of the plot
1600 @param value_lists the list of list of values eg: [[...],[...],[...]]
1601 @param valuename the y-label
1602 @param bins the number of bins
1603 @param colors If None, will use rainbow. Else will use specific list
1604 @param format output format
1605 @param reference_xline plot a reference line parallel to the y-axis
1606 @param yplotrange the range for the y-axis
1607 @param normalized whether the histogram is normalized or not
1608 @param leg_names names for the legend
1611 import matplotlib
as mpl
1613 import matplotlib.pyplot
as plt
1614 import matplotlib.cm
as cm
1615 fig = plt.figure(figsize=(18.0, 9.0))
1618 colors = cm.rainbow(np.linspace(0, 1, len(values_lists)))
1619 for nv,values
in enumerate(values_lists):
1621 if leg_names
is not None:
1626 [float(y)
for y
in values],
1629 normed=normalized,histtype=
'step',lw=4,
1633 plt.tick_params(labelsize=12, pad=10)
1634 if valuename
is None:
1635 plt.xlabel(name, size=
"xx-large")
1637 plt.xlabel(valuename, size=
"xx-large")
1638 plt.ylabel(
"Frequency", size=
"xx-large")
1640 if not yplotrange
is None:
1642 if not xplotrange
is None:
1643 plt.xlim(xplotrange)
1647 if not reference_xline
is None:
1654 plt.savefig(name +
"." + format, dpi=150, transparent=
True)
1659 valuename=
"None", positionname=
"None", xlabels=
None,scale_plot_length=1.0):
1661 Plot time series as boxplots.
1662 fields is a list of time series, positions are the x-values
1663 valuename is the y-label, positionname is the x-label
1666 import matplotlib
as mpl
1668 import matplotlib.pyplot
as plt
1669 from matplotlib.patches
import Polygon
1672 fig = plt.figure(figsize=(float(len(positions))*scale_plot_length, 5.0))
1673 fig.canvas.set_window_title(name)
1675 ax1 = fig.add_subplot(111)
1677 plt.subplots_adjust(left=0.1, right=0.990, top=0.95, bottom=0.4)
1679 bps.append(plt.boxplot(values, notch=0, sym=
'', vert=1,
1680 whis=1.5, positions=positions))
1682 plt.setp(bps[-1][
'boxes'], color=
'black', lw=1.5)
1683 plt.setp(bps[-1][
'whiskers'], color=
'black', ls=
":", lw=1.5)
1685 if frequencies
is not None:
1686 for n,v
in enumerate(values):
1687 plist=[positions[n]]*len(v)
1688 ax1.plot(plist, v,
'gx', alpha=0.7, markersize=7)
1691 if not xlabels
is None:
1692 ax1.set_xticklabels(xlabels)
1693 plt.xticks(rotation=90)
1694 plt.xlabel(positionname)
1695 plt.ylabel(valuename)
1697 plt.savefig(name+
".pdf",dpi=150)
1701 def plot_xy_data(x,y,title=None,out_fn=None,display=True,set_plot_yaxis_range=None,
1702 xlabel=
None,ylabel=
None):
1703 import matplotlib
as mpl
1705 import matplotlib.pyplot
as plt
1706 plt.rc(
'lines', linewidth=2)
1708 fig, ax = plt.subplots(nrows=1)
1709 fig.set_size_inches(8,4.5)
1710 if title
is not None:
1711 fig.canvas.set_window_title(title)
1714 ax.plot(x,y,color=
'r')
1715 if set_plot_yaxis_range
is not None:
1716 x1,x2,y1,y2=plt.axis()
1717 y1=set_plot_yaxis_range[0]
1718 y2=set_plot_yaxis_range[1]
1719 plt.axis((x1,x2,y1,y2))
1720 if title
is not None:
1722 if xlabel
is not None:
1723 ax.set_xlabel(xlabel)
1724 if ylabel
is not None:
1725 ax.set_ylabel(ylabel)
1726 if out_fn
is not None:
1727 plt.savefig(out_fn+
".pdf")
1732 def plot_scatter_xy_data(x,y,labelx="None",labely="None",
1733 xmin=
None,xmax=
None,ymin=
None,ymax=
None,
1734 savefile=
False,filename=
"None.eps",alpha=0.75):
1736 import matplotlib
as mpl
1738 import matplotlib.pyplot
as plt
1740 from matplotlib
import rc
1742 rc(
'font',**{
'family':
'sans-serif',
'sans-serif':[
'Helvetica']})
1745 fig, axs = plt.subplots(1)
1749 axs0.set_xlabel(labelx, size=
"xx-large")
1750 axs0.set_ylabel(labely, size=
"xx-large")
1751 axs0.tick_params(labelsize=18, pad=10)
1755 plot2.append(axs0.plot(x, y,
'o', color=
'k',lw=2, ms=0.1, alpha=alpha, c=
"w"))
1764 fig.set_size_inches(8.0, 8.0)
1765 fig.subplots_adjust(left=0.161, right=0.850, top=0.95, bottom=0.11)
1766 if (
not ymin
is None)
and (
not ymax
is None):
1767 axs0.set_ylim(ymin,ymax)
1768 if (
not xmin
is None)
and (
not xmax
is None):
1769 axs0.set_xlim(xmin,xmax)
1773 fig.savefig(filename, dpi=300)
1776 def get_graph_from_hierarchy(hier):
1780 (graph, depth, depth_dict) = recursive_graph(
1781 hier, graph, depth, depth_dict)
1784 node_labels_dict = {}
1786 for key
in depth_dict:
1787 node_size_dict = 10 / depth_dict[key]
1788 if depth_dict[key] < 3:
1789 node_labels_dict[key] = key
1791 node_labels_dict[key] =
""
1792 draw_graph(graph, labels_dict=node_labels_dict)
1795 def recursive_graph(hier, graph, depth, depth_dict):
1798 index = str(hier.get_particle().
get_index())
1799 name1 = nameh +
"|#" + index
1800 depth_dict[name1] = depth
1804 if len(children) == 1
or children
is None:
1806 return (graph, depth, depth_dict)
1810 (graph, depth, depth_dict) = recursive_graph(
1811 c, graph, depth, depth_dict)
1813 index = str(c.get_particle().
get_index())
1814 namec = nameh +
"|#" + index
1815 graph.append((name1, namec))
1818 return (graph, depth, depth_dict)
1821 def draw_graph(graph, labels_dict=None, graph_layout='spring',
1822 node_size=5, node_color=
None, node_alpha=0.3,
1823 node_text_size=11, fixed=
None, pos=
None,
1824 edge_color=
'blue', edge_alpha=0.3, edge_thickness=1,
1826 validation_edges=
None,
1827 text_font=
'sans-serif',
1830 import matplotlib
as mpl
1832 import networkx
as nx
1833 import matplotlib.pyplot
as plt
1834 from math
import sqrt, pi
1840 if type(edge_thickness)
is list:
1841 for edge,weight
in zip(graph,edge_thickness):
1842 G.add_edge(edge[0], edge[1], weight=weight)
1845 G.add_edge(edge[0], edge[1])
1847 if node_color==
None:
1848 node_color_rgb=(0,0,0)
1849 node_color_hex=
"000000"
1854 for node
in G.nodes():
1855 cctuple=cc.rgb(node_color[node])
1856 tmpcolor_rgb.append((cctuple[0]/255,cctuple[1]/255,cctuple[2]/255))
1857 tmpcolor_hex.append(node_color[node])
1858 node_color_rgb=tmpcolor_rgb
1859 node_color_hex=tmpcolor_hex
1862 if type(node_size)
is dict:
1864 for node
in G.nodes():
1865 size=sqrt(node_size[node])/pi*10.0
1866 tmpsize.append(size)
1869 for n,node
in enumerate(G.nodes()):
1870 color=node_color_hex[n]
1872 nx.set_node_attributes(G,
"graphics", {node : {
'type':
'ellipse',
'w': size,
'h': size,
'fill':
'#'+color,
'label': node}})
1873 nx.set_node_attributes(G,
"LabelGraphics", {node : {
'type':
'text',
'text':node,
'color':
'#000000',
'visible':
'true'}})
1875 for edge
in G.edges():
1876 nx.set_edge_attributes(G,
"graphics", {edge : {
'width': 1,
'fill':
'#000000'}})
1878 for ve
in validation_edges:
1880 if (ve[0],ve[1])
in G.edges():
1881 print(
"found forward")
1882 nx.set_edge_attributes(G,
"graphics", {ve : {
'width': 1,
'fill':
'#00FF00'}})
1883 elif (ve[1],ve[0])
in G.edges():
1884 print(
"found backward")
1885 nx.set_edge_attributes(G,
"graphics", {(ve[1],ve[0]) : {
'width': 1,
'fill':
'#00FF00'}})
1887 G.add_edge(ve[0], ve[1])
1889 nx.set_edge_attributes(G,
"graphics", {ve : {
'width': 1,
'fill':
'#FF0000'}})
1893 if graph_layout ==
'spring':
1895 graph_pos = nx.spring_layout(G,k=1.0/8.0,fixed=fixed,pos=pos)
1896 elif graph_layout ==
'spectral':
1897 graph_pos = nx.spectral_layout(G)
1898 elif graph_layout ==
'random':
1899 graph_pos = nx.random_layout(G)
1901 graph_pos = nx.shell_layout(G)
1905 nx.draw_networkx_nodes(G, graph_pos, node_size=node_size,
1906 alpha=node_alpha, node_color=node_color_rgb,
1908 nx.draw_networkx_edges(G, graph_pos, width=edge_thickness,
1909 alpha=edge_alpha, edge_color=edge_color)
1910 nx.draw_networkx_labels(
1911 G, graph_pos, labels=labels_dict, font_size=node_text_size,
1912 font_family=text_font)
1914 plt.savefig(out_filename)
1915 nx.write_gml(G,
'out.gml')
1923 from ipyD3
import d3object
1924 from IPython.display
import display
1926 d3 = d3object(width=800,
1931 title=
'Example table with d3js',
1932 desc=
'An example table created created with d3js with data generated with Python.')
2018 [72.0, 60.0, 60.0, 10.0, 120.0, 172.0, 1092.0, 675.0, 408.0, 360.0, 156.0, 100.0]]
2019 data = [list(i)
for i
in zip(*data)]
2020 sRows = [[
'January',
2032 sColumns = [[
'Prod {0}'.format(i)
for i
in range(1, 9)],
2033 [
None,
'',
None,
None,
'Group 1',
None,
None,
'Group 2']]
2034 d3.addSimpleTable(data,
2035 fontSizeCells=[12, ],
2038 sRowsMargins=[5, 50, 0],
2039 sColsMargins=[5, 20, 10],
2042 addOutsideBorders=-1,
2046 html = d3.render(mode=[
'html',
'show'])
static bool get_is_setup(const IMP::ParticleAdaptor &p)
A container for models organized into clusters.
A class for reading stat files (either rmf or ascii v1 and v2)
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)
def plot_field_histogram
Plot a list of histograms from a value list.
def plot_fields_box_plots
Plot time series as boxplots.
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.
A class to store data associated to a model.
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().
def get_fields
Get the desired field names, and return a dictionary.
static bool get_is_setup(Model *m, ParticleIndex pi)
def link_to_rmf
Link to another RMF file.
std::string get_molecule_name_and_copy(atom::Hierarchy h)
Walk up a PMI2 hierarchy/representations and get the "molname.copynum".
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.
int get_number_of_frames(const ::npctransport_proto::Assignment &config, double time_step)
A decorator for a particle representing an atom.
Base class for capturing a modeling protocol.
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.
void add_hierarchies(RMF::NodeHandle fh, const atom::Hierarchies &hs)
Class for easy writing of PDBs, RMFs, and stat files.
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.
bool get_is_canonical(atom::Hierarchy h)
Walk up a PMI2 hierarchy/representations and check if the root is named System.
Display a segment connecting a pair of particles.
A decorator for a residue.
Basic functionality that is expected to be used by a wide variety of IMP users.
def get_prot_name_from_particle
Get the protein name from the particle.
class to link stat files to several rmf files
class to allow more advanced handling of RMF files.
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.
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.
def init_rmf
This function initialize an RMF file.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
std::string get_module_version()
A decorator for a particle with x,y,z coordinates and a radius.