1 """@namespace IMP.pmi.output
2 Classes for writing output files and processing them.
5 from __future__
import print_function, division
17 import cPickle
as pickle
25 if t
is tuple
or t
is list:
26 for elt2
in _flatten(elt):
33 """Class for easy writing of PDBs, RMFs, and stat files"""
34 def __init__(self, ascii=True,atomistic=False):
35 self.dictionary_pdbs = {}
36 self.dictionary_rmfs = {}
37 self.dictionary_stats = {}
38 self.dictionary_stats2 = {}
39 self.best_score_list =
None
40 self.nbestscoring =
None
42 self.replica_exchange =
False
46 self.chainids =
"ABCDEFGHIJKLMNOPQRSTUVXYWZabcdefghijklmnopqrstuvxywz0123456789"
48 self.particle_infos_for_pdb = {}
49 self.atomistic=atomistic
52 def get_pdb_names(self):
53 return list(self.dictionary_pdbs.keys())
55 def get_rmf_names(self):
56 return list(self.dictionary_rmfs.keys())
58 def get_stat_names(self):
59 return list(self.dictionary_stats.keys())
63 @param name The PDB filename
64 @param prot The hierarchy to write to this pdb file
65 \note if the PDB name is 'System' then will use Selection to get molecules
67 flpdb = open(name,
'w')
69 self.dictionary_pdbs[name] = prot
70 self.dictchain[name] = {}
77 for n,mol
in enumerate(IMP.atom.get_by_type(prot,IMP.atom.MOLECULE_TYPE)):
81 for n, i
in enumerate(self.dictionary_pdbs[name].get_children()):
82 self.dictchain[name][i.get_name()] = self.chainids[n]
84 def write_psf(self,filename,name):
85 flpsf=open(filename,
'w')
86 flpsf.write(
"PSF CMAP CHEQ"+
"\n")
87 index_residue_pair_list={}
88 (particle_infos_for_pdb, geometric_center)=self.get_particle_infos_for_pdb_writing(name)
89 nparticles=len(particle_infos_for_pdb)
90 flpsf.write(str(nparticles)+
" !NATOM"+
"\n")
91 for n,p
in enumerate(particle_infos_for_pdb):
97 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))
100 if chain
not in index_residue_pair_list:
101 index_residue_pair_list[chain]=[(atom_index,resid)]
103 index_residue_pair_list[chain].append((atom_index,resid))
108 for chain
in sorted(index_residue_pair_list.keys()):
110 ls=index_residue_pair_list[chain]
112 ls=sorted(ls, key=
lambda tup: tup[1])
114 indexes=[x[0]
for x
in ls]
117 nbonds=len(indexes_pairs)
118 flpsf.write(str(nbonds)+
" !NBOND: bonds"+
"\n")
120 sublists=[indexes_pairs[i:i+4]
for i
in range(0,len(indexes_pairs),4)]
125 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],
126 ip[1][0],ip[1][1],ip[2][0],ip[2][1],ip[3][0],ip[3][1]))
128 flpsf.write(
'{0:8d}{1:8d}{2:8d}{3:8d}{4:8d}{5:8d}'.format(ip[0][0],ip[0][1],ip[1][0],
129 ip[1][1],ip[2][0],ip[2][1]))
131 flpsf.write(
'{0:8d}{1:8d}{2:8d}{3:8d}'.format(ip[0][0],ip[0][1],ip[1][0],ip[1][1]))
133 flpsf.write(
'{0:8d}{1:8d}'.format(ip[0][0],ip[0][1]))
136 del particle_infos_for_pdb
141 translate_to_geometric_center=
False):
143 flpdb = open(name,
'a')
145 flpdb = open(name,
'w')
147 (particle_infos_for_pdb,
148 geometric_center) = self.get_particle_infos_for_pdb_writing(name)
150 if not translate_to_geometric_center:
151 geometric_center = (0, 0, 0)
153 for n,tupl
in enumerate(particle_infos_for_pdb):
154 (xyz, atom_type, residue_type,
155 chain_id, residue_index,radius) = tupl
156 flpdb.write(IMP.atom.get_pdb_string((xyz[0] - geometric_center[0],
157 xyz[1] - geometric_center[1],
158 xyz[2] - geometric_center[2]),
159 n+1, atom_type, residue_type,
160 chain_id, residue_index,
' ',1.00,radius))
161 flpdb.write(
"ENDMDL\n")
164 del particle_infos_for_pdb
166 def get_particle_infos_for_pdb_writing(self, name):
176 particle_infos_for_pdb = []
178 geometric_center = [0, 0, 0]
184 ps =
IMP.atom.Selection(self.dictionary_pdbs[name],resolution=0).get_selected_particles()
188 for n, p
in enumerate(ps):
196 p, self.dictchain[name])
198 if protname
not in resindexes_dict:
199 resindexes_dict[protname] = []
203 rt = residue.get_residue_type()
204 resind = residue.get_index()
208 geometric_center[0] += xyz[0]
209 geometric_center[1] += xyz[1]
210 geometric_center[2] += xyz[2]
212 particle_infos_for_pdb.append((xyz,
213 atomtype, rt, self.dictchain[name][protname], resind,radius))
214 resindexes_dict[protname].append(resind)
219 resind = residue.get_index()
222 if resind
in resindexes_dict[protname]:
225 resindexes_dict[protname].append(resind)
226 rt = residue.get_residue_type()
229 geometric_center[0] += xyz[0]
230 geometric_center[1] += xyz[1]
231 geometric_center[2] += xyz[2]
233 particle_infos_for_pdb.append((xyz,
234 IMP.atom.AT_CA, rt, self.dictchain[name][protname], resind,radius))
238 resind = resindexes[len(resindexes) // 2]
239 if resind
in resindexes_dict[protname]:
242 resindexes_dict[protname].append(resind)
246 geometric_center[0] += xyz[0]
247 geometric_center[1] += xyz[1]
248 geometric_center[2] += xyz[2]
250 particle_infos_for_pdb.append((xyz,
251 IMP.atom.AT_CA, rt, self.dictchain[name][protname], resind,radius))
257 if len(resindexes) > 0:
258 resind = resindexes[len(resindexes) // 2]
261 geometric_center[0] += xyz[0]
262 geometric_center[1] += xyz[1]
263 geometric_center[2] += xyz[2]
265 particle_infos_for_pdb.append((xyz,
266 IMP.atom.AT_CA, rt, self.dictchain[name][protname], resind,radius))
269 geometric_center = (geometric_center[0] / atom_count,
270 geometric_center[1] / atom_count,
271 geometric_center[2] / atom_count)
273 particle_infos_for_pdb = sorted(particle_infos_for_pdb, key=operator.itemgetter(3, 4))
275 return (particle_infos_for_pdb, geometric_center)
278 def write_pdbs(self, appendmode=True):
279 for pdb
in self.dictionary_pdbs.keys():
280 self.write_pdb(pdb, appendmode)
282 def init_pdb_best_scoring(self,
286 replica_exchange=
False):
290 self.suffixes.append(suffix)
291 self.replica_exchange = replica_exchange
292 if not self.replica_exchange:
296 self.best_score_list = []
300 self.best_score_file_name =
"best.scores.rex.py"
301 self.best_score_list = []
302 best_score_file = open(self.best_score_file_name,
"w")
303 best_score_file.write(
304 "self.best_score_list=" + str(self.best_score_list))
305 best_score_file.close()
307 self.nbestscoring = nbestscoring
308 for i
in range(self.nbestscoring):
309 name = suffix +
"." + str(i) +
".pdb"
310 flpdb = open(name,
'w')
312 self.dictionary_pdbs[name] = prot
313 self.dictchain[name] = {}
316 self.atomistic =
True
317 for n,mol
in enumerate(IMP.atom.get_by_type(prot,IMP.atom.MOLECULE_TYPE)):
320 for n, i
in enumerate(self.dictionary_pdbs[name].get_children()):
321 self.dictchain[name][i.get_name()] = self.chainids[n]
323 def write_pdb_best_scoring(self, score):
324 if self.nbestscoring
is None:
325 print(
"Output.write_pdb_best_scoring: init_pdb_best_scoring not run")
328 if self.replica_exchange:
330 exec(open(self.best_score_file_name).read())
332 if len(self.best_score_list) < self.nbestscoring:
333 self.best_score_list.append(score)
334 self.best_score_list.sort()
335 index = self.best_score_list.index(score)
336 for suffix
in self.suffixes:
337 for i
in range(len(self.best_score_list) - 2, index - 1, -1):
338 oldname = suffix +
"." + str(i) +
".pdb"
339 newname = suffix +
"." + str(i + 1) +
".pdb"
341 if os.path.exists(newname):
343 os.rename(oldname, newname)
344 filetoadd = suffix +
"." + str(index) +
".pdb"
345 self.write_pdb(filetoadd, appendmode=
False)
348 if score < self.best_score_list[-1]:
349 self.best_score_list.append(score)
350 self.best_score_list.sort()
351 self.best_score_list.pop(-1)
352 index = self.best_score_list.index(score)
353 for suffix
in self.suffixes:
354 for i
in range(len(self.best_score_list) - 1, index - 1, -1):
355 oldname = suffix +
"." + str(i) +
".pdb"
356 newname = suffix +
"." + str(i + 1) +
".pdb"
357 os.rename(oldname, newname)
358 filenametoremove = suffix + \
359 "." + str(self.nbestscoring) +
".pdb"
360 os.remove(filenametoremove)
361 filetoadd = suffix +
"." + str(index) +
".pdb"
362 self.write_pdb(filetoadd, appendmode=
False)
364 if self.replica_exchange:
366 best_score_file = open(self.best_score_file_name,
"w")
367 best_score_file.write(
368 "self.best_score_list=" + str(self.best_score_list))
369 best_score_file.close()
371 def init_rmf(self, name, hierarchies, rs=None, geometries=None):
372 rh = RMF.create_rmf_file(name)
376 if geometries
is not None:
378 self.dictionary_rmfs[name] = rh
380 def add_restraints_to_rmf(self, name, objectlist):
381 flatobjectlist=_flatten(objectlist)
382 for o
in flatobjectlist:
384 rs = o.get_restraint_for_rmf()
386 rs = o.get_restraint()
388 self.dictionary_rmfs[name],
391 def add_geometries_to_rmf(self, name, objectlist):
393 geos = o.get_geometries()
396 def add_particle_pair_from_restraints_to_rmf(self, name, objectlist):
399 pps = o.get_particle_pairs()
402 self.dictionary_rmfs[name],
405 def write_rmf(self, name):
407 self.dictionary_rmfs[name].flush()
409 def close_rmf(self, name):
410 del self.dictionary_rmfs[name]
412 def write_rmfs(self):
413 for rmf
in self.dictionary_rmfs.keys():
416 def init_stat(self, name, listofobjects):
418 flstat = open(name,
'w')
421 flstat = open(name,
'wb')
425 for l
in listofobjects:
426 if not "get_output" in dir(l):
427 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(l))
428 self.dictionary_stats[name] = listofobjects
430 def set_output_entry(self, key, value):
431 self.initoutput.update({key: value})
433 def write_stat(self, name, appendmode=True):
434 output = self.initoutput
435 for obj
in self.dictionary_stats[name]:
438 dfiltered = dict((k, v)
for k, v
in d.items()
if k[0] !=
"_")
439 output.update(dfiltered)
447 flstat = open(name, writeflag)
448 flstat.write(
"%s \n" % output)
451 flstat = open(name, writeflag +
'b')
452 cPickle.dump(output, flstat, 2)
455 def write_stats(self):
456 for stat
in self.dictionary_stats.keys():
457 self.write_stat(stat)
459 def get_stat(self, name):
461 for obj
in self.dictionary_stats[name]:
462 output.update(obj.get_output())
465 def write_test(self, name, listofobjects):
472 flstat = open(name,
'w')
473 output = self.initoutput
474 for l
in listofobjects:
475 if not "get_test_output" in dir(l)
and not "get_output" in dir(l):
476 raise ValueError(
"Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
477 self.dictionary_stats[name] = listofobjects
479 for obj
in self.dictionary_stats[name]:
481 d = obj.get_test_output()
485 dfiltered = dict((k, v)
for k, v
in d.items()
if k[0] !=
"_")
486 output.update(dfiltered)
490 flstat.write(
"%s \n" % output)
493 def test(self, name, listofobjects, tolerance=1e-5):
494 output = self.initoutput
495 for l
in listofobjects:
496 if not "get_test_output" in dir(l)
and not "get_output" in dir(l):
497 raise ValueError(
"Output: object %s doesn't have get_output() or get_test_output() method" % str(l))
498 for obj
in listofobjects:
500 output.update(obj.get_test_output())
502 output.update(obj.get_output())
507 flstat = open(name,
'r')
514 old_value = str(test_dict[k])
515 new_value = str(output[k])
523 fold = float(old_value)
524 fnew = float(new_value)
525 diff = abs(fold - fnew)
527 print(
"%s: test failed, old value: %s new value %s; "
528 "diff %f > %f" % (str(k), str(old_value),
529 str(new_value), diff,
530 tolerance), file=sys.stderr)
532 elif test_dict[k] != output[k]:
533 if len(old_value) < 50
and len(new_value) < 50:
534 print(
"%s: test failed, old value: %s new value %s"
535 % (str(k), old_value, new_value), file=sys.stderr)
538 print(
"%s: test failed, omitting results (too long)"
539 % str(k), file=sys.stderr)
543 print(
"%s from old objects (file %s) not in new objects"
544 % (str(k), str(name)), file=sys.stderr)
547 def get_environment_variables(self):
549 return str(os.environ)
551 def get_versions_of_relevant_modules(self):
558 except (ImportError):
562 versions[
"ISD2_VERSION"] = IMP.isd2.get_module_version()
563 except (ImportError):
567 versions[
"ISD_EMXL_VERSION"] = IMP.isd_emxl.get_module_version()
568 except (ImportError):
578 listofsummedobjects=
None):
584 if listofsummedobjects
is None:
585 listofsummedobjects = []
586 if extralabels
is None:
588 flstat = open(name,
'w')
590 stat2_keywords = {
"STAT2HEADER":
"STAT2HEADER"}
591 stat2_keywords.update(
592 {
"STAT2HEADER_ENVIRON": str(self.get_environment_variables())})
593 stat2_keywords.update(
594 {
"STAT2HEADER_IMP_VERSIONS": str(self.get_versions_of_relevant_modules())})
597 for l
in listofobjects:
598 if not "get_output" in dir(l):
599 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(l))
603 dfiltered = dict((k, v)
604 for k, v
in d.items()
if k[0] !=
"_")
605 output.update(dfiltered)
608 for l
in listofsummedobjects:
610 if not "get_output" in dir(t):
611 raise ValueError(
"Output: object %s doesn't have get_output() method" % str(t))
613 if "_TotalScore" not in t.get_output():
614 raise ValueError(
"Output: object %s doesn't have _TotalScore entry to be summed" % str(t))
616 output.update({l[1]: 0.0})
618 for k
in extralabels:
619 output.update({k: 0.0})
621 for n, k
in enumerate(output):
622 stat2_keywords.update({n: k})
623 stat2_inverse.update({k: n})
625 flstat.write(
"%s \n" % stat2_keywords)
627 self.dictionary_stats2[name] = (
633 def write_stat2(self, name, appendmode=True):
635 (listofobjects, stat2_inverse, listofsummedobjects,
636 extralabels) = self.dictionary_stats2[name]
639 for obj
in listofobjects:
640 od = obj.get_output()
641 dfiltered = dict((k, v)
for k, v
in od.items()
if k[0] !=
"_")
643 output.update({stat2_inverse[k]: od[k]})
646 for l
in listofsummedobjects:
650 partial_score += float(d[
"_TotalScore"])
651 output.update({stat2_inverse[l[1]]: str(partial_score)})
654 for k
in extralabels:
655 if k
in self.initoutput:
656 output.update({stat2_inverse[k]: self.initoutput[k]})
658 output.update({stat2_inverse[k]:
"None"})
665 flstat = open(name, writeflag)
666 flstat.write(
"%s \n" % output)
669 def write_stats2(self):
670 for stat
in self.dictionary_stats2.keys():
671 self.write_stat2(stat)
675 """A class for reading stat files"""
676 def __init__(self, filename):
677 self.filename = filename
682 if not self.filename
is None:
683 f = open(self.filename,
"r")
685 raise ValueError(
"No file name provided. Use -h for help")
688 for line
in f.readlines():
690 self.klist = list(d.keys())
692 if "STAT2HEADER" in self.klist:
695 if "STAT2HEADER" in str(k):
701 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
703 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
704 self.invstat2_dict = {}
706 self.invstat2_dict.update({stat2_dict[k]: k})
717 def show_keys(self, ncolumns=2, truncate=65):
718 IMP.pmi.tools.print_multicolumn(self.get_keys(), ncolumns, truncate)
720 def get_fields(self, fields, filtertuple=None, filterout=None, get_every=1):
722 Get the desired field names, and return a dictionary.
724 @param fields desired field names
725 @param filterout specify if you want to "grep" out something from
726 the file, so that it is faster
727 @param filtertuple a tuple that contains
728 ("TheKeyToBeFiltered",relationship,value)
729 where relationship = "<", "==", or ">"
730 @param get_every only read every Nth line from the file
738 f = open(self.filename,
"r")
741 for line
in f.readlines():
742 if not filterout
is None:
743 if filterout
in line:
747 if line_number % get_every != 0:
754 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
759 if not filtertuple
is None:
760 keytobefiltered = filtertuple[0]
761 relationship = filtertuple[1]
762 value = filtertuple[2]
763 if relationship ==
"<":
764 if float(d[keytobefiltered]) >= value:
766 if relationship ==
">":
767 if float(d[keytobefiltered]) <= value:
769 if relationship ==
"==":
770 if float(d[keytobefiltered]) != value:
772 [outdict[field].append(d[field])
for field
in fields]
778 if not filtertuple
is None:
779 keytobefiltered = filtertuple[0]
780 relationship = filtertuple[1]
781 value = filtertuple[2]
782 if relationship ==
"<":
783 if float(d[self.invstat2_dict[keytobefiltered]]) >= value:
785 if relationship ==
">":
786 if float(d[self.invstat2_dict[keytobefiltered]]) <= value:
788 if relationship ==
"==":
789 if float(d[self.invstat2_dict[keytobefiltered]]) != value:
792 [outdict[field].append(d[self.invstat2_dict[field]])
799 class CrossLinkIdentifierDatabase(object):
803 def check_key(self,key):
804 if key
not in self.clidb:
807 def set_unique_id(self,key,value):
809 self.clidb[key][
"XLUniqueID"]=str(value)
811 def set_protein1(self,key,value):
813 self.clidb[key][
"Protein1"]=str(value)
815 def set_protein2(self,key,value):
817 self.clidb[key][
"Protein2"]=str(value)
819 def set_residue1(self,key,value):
821 self.clidb[key][
"Residue1"]=int(value)
823 def set_residue2(self,key,value):
825 self.clidb[key][
"Residue2"]=int(value)
827 def set_idscore(self,key,value):
829 self.clidb[key][
"IDScore"]=float(value)
831 def set_state(self,key,value):
833 self.clidb[key][
"State"]=int(value)
835 def set_sigma1(self,key,value):
837 self.clidb[key][
"Sigma1"]=str(value)
839 def set_sigma2(self,key,value):
841 self.clidb[key][
"Sigma2"]=str(value)
843 def set_psi(self,key,value):
845 self.clidb[key][
"Psi"]=str(value)
847 def get_unique_id(self,key):
848 return self.clidb[key][
"XLUniqueID"]
850 def get_protein1(self,key):
851 return self.clidb[key][
"Protein1"]
853 def get_protein2(self,key):
854 return self.clidb[key][
"Protein2"]
856 def get_residue1(self,key):
857 return self.clidb[key][
"Residue1"]
859 def get_residue2(self,key):
860 return self.clidb[key][
"Residue2"]
862 def get_idscore(self,key):
863 return self.clidb[key][
"IDScore"]
865 def get_state(self,key):
866 return self.clidb[key][
"State"]
868 def get_sigma1(self,key):
869 return self.clidb[key][
"Sigma1"]
871 def get_sigma2(self,key):
872 return self.clidb[key][
"Sigma2"]
874 def get_psi(self,key):
875 return self.clidb[key][
"Psi"]
877 def set_float_feature(self,key,value,feature_name):
879 self.clidb[key][feature_name]=float(value)
881 def set_int_feature(self,key,value,feature_name):
883 self.clidb[key][feature_name]=int(value)
885 def set_string_feature(self,key,value,feature_name):
887 self.clidb[key][feature_name]=str(value)
889 def get_feature(self,key,feature_name):
890 return self.clidb[key][feature_name]
892 def write(self,filename):
894 with open(filename,
'wb')
as handle:
895 pickle.dump(self.clidb,handle)
897 def load(self,filename):
899 with open(filename,
'rb')
as handle:
900 self.clidb=pickle.load(handle)
902 def plot_fields(fields, framemin=None, framemax=None):
903 import matplotlib
as mpl
905 import matplotlib.pyplot
as plt
907 plt.rc(
'lines', linewidth=4)
908 fig, axs = plt.subplots(nrows=len(fields))
909 fig.set_size_inches(10.5, 5.5 * len(fields))
910 plt.rc(
'axes', color_cycle=[
'r'])
917 framemax = len(fields[key])
918 x = list(range(framemin, framemax))
919 y = [float(y)
for y
in fields[key][framemin:framemax]]
922 axs[n].set_title(key, size=
"xx-large")
923 axs[n].tick_params(labelsize=18, pad=10)
926 axs.set_title(key, size=
"xx-large")
927 axs.tick_params(labelsize=18, pad=10)
931 plt.subplots_adjust(hspace=0.3)
936 name, values_lists, valuename=
None, bins=40, colors=
None, format=
"png",
937 reference_xline=
None, yplotrange=
None, xplotrange=
None,normalized=
True,
940 '''Plot a list of histograms from a value list.
941 @param name the name of the plot
942 @param value_lists the list of list of values eg: [[...],[...],[...]]
943 @param valuename the y-label
944 @param bins the number of bins
945 @param colors If None, will use rainbow. Else will use specific list
946 @param format output format
947 @param reference_xline plot a reference line parallel to the y-axis
948 @param yplotrange the range for the y-axis
949 @param normalized whether the histogram is normalized or not
950 @param leg_names names for the legend
953 import matplotlib
as mpl
955 import matplotlib.pyplot
as plt
956 import matplotlib.cm
as cm
957 fig = plt.figure(figsize=(18.0, 9.0))
960 colors = cm.rainbow(np.linspace(0, 1, len(values_lists)))
961 for nv,values
in enumerate(values_lists):
963 if leg_names
is not None:
968 [float(y)
for y
in values],
971 normed=normalized,histtype=
'step',lw=4,
975 plt.tick_params(labelsize=12, pad=10)
976 if valuename
is None:
977 plt.xlabel(name, size=
"xx-large")
979 plt.xlabel(valuename, size=
"xx-large")
980 plt.ylabel(
"Frequency", size=
"xx-large")
982 if not yplotrange
is None:
984 if not xplotrange
is None:
989 if not reference_xline
is None:
996 plt.savefig(name +
"." + format, dpi=150, transparent=
True)
1001 valuename=
"None", positionname=
"None", xlabels=
None,scale_plot_length=1.0):
1003 Plot time series as boxplots.
1004 fields is a list of time series, positions are the x-values
1005 valuename is the y-label, positionname is the x-label
1008 import matplotlib
as mpl
1010 import matplotlib.pyplot
as plt
1011 from matplotlib.patches
import Polygon
1014 fig = plt.figure(figsize=(float(len(positions))*scale_plot_length, 5.0))
1015 fig.canvas.set_window_title(name)
1017 ax1 = fig.add_subplot(111)
1019 plt.subplots_adjust(left=0.1, right=0.990, top=0.95, bottom=0.4)
1021 bps.append(plt.boxplot(values, notch=0, sym=
'', vert=1,
1022 whis=1.5, positions=positions))
1024 plt.setp(bps[-1][
'boxes'], color=
'black', lw=1.5)
1025 plt.setp(bps[-1][
'whiskers'], color=
'black', ls=
":", lw=1.5)
1027 if frequencies
is not None:
1028 for n,v
in enumerate(values):
1029 plist=[positions[n]]*len(v)
1030 ax1.plot(plist, v,
'gx', alpha=0.7, markersize=7)
1033 if not xlabels
is None:
1034 ax1.set_xticklabels(xlabels)
1035 plt.xticks(rotation=90)
1036 plt.xlabel(positionname)
1037 plt.ylabel(valuename)
1039 plt.savefig(name+
".pdf",dpi=150)
1043 def plot_xy_data(x,y,title=None,out_fn=None,display=True,set_plot_yaxis_range=None,
1044 xlabel=
None,ylabel=
None):
1045 import matplotlib
as mpl
1047 import matplotlib.pyplot
as plt
1048 plt.rc(
'lines', linewidth=2)
1050 fig, ax = plt.subplots(nrows=1)
1051 fig.set_size_inches(8,4.5)
1052 if title
is not None:
1053 fig.canvas.set_window_title(title)
1056 ax.plot(x,y,color=
'r')
1057 if set_plot_yaxis_range
is not None:
1058 x1,x2,y1,y2=plt.axis()
1059 y1=set_plot_yaxis_range[0]
1060 y2=set_plot_yaxis_range[1]
1061 plt.axis((x1,x2,y1,y2))
1062 if title
is not None:
1064 if xlabel
is not None:
1065 ax.set_xlabel(xlabel)
1066 if ylabel
is not None:
1067 ax.set_ylabel(ylabel)
1068 if out_fn
is not None:
1069 plt.savefig(out_fn+
".pdf")
1074 def plot_scatter_xy_data(x,y,labelx="None",labely="None",
1075 xmin=
None,xmax=
None,ymin=
None,ymax=
None,
1076 savefile=
False,filename=
"None.eps",alpha=0.75):
1078 import matplotlib
as mpl
1080 import matplotlib.pyplot
as plt
1082 from matplotlib
import rc
1084 rc(
'font',**{
'family':
'sans-serif',
'sans-serif':[
'Helvetica']})
1087 fig, axs = plt.subplots(1)
1091 axs0.set_xlabel(labelx, size=
"xx-large")
1092 axs0.set_ylabel(labely, size=
"xx-large")
1093 axs0.tick_params(labelsize=18, pad=10)
1097 plot2.append(axs0.plot(x, y,
'o', color=
'k',lw=2, ms=0.1, alpha=alpha, c=
"w"))
1106 fig.set_size_inches(8.0, 8.0)
1107 fig.subplots_adjust(left=0.161, right=0.850, top=0.95, bottom=0.11)
1108 if (
not ymin
is None)
and (
not ymax
is None):
1109 axs0.set_ylim(ymin,ymax)
1110 if (
not xmin
is None)
and (
not xmax
is None):
1111 axs0.set_xlim(xmin,xmax)
1115 fig.savefig(filename, dpi=300)
1118 def get_graph_from_hierarchy(hier):
1122 (graph, depth, depth_dict) = recursive_graph(
1123 hier, graph, depth, depth_dict)
1126 node_labels_dict = {}
1128 for key
in depth_dict:
1129 node_size_dict = 10 / depth_dict[key]
1130 if depth_dict[key] < 3:
1131 node_labels_dict[key] = key
1133 node_labels_dict[key] =
""
1134 draw_graph(graph, labels_dict=node_labels_dict)
1137 def recursive_graph(hier, graph, depth, depth_dict):
1140 index = str(hier.get_particle().
get_index())
1141 name1 = nameh +
"|#" + index
1142 depth_dict[name1] = depth
1146 if len(children) == 1
or children
is None:
1148 return (graph, depth, depth_dict)
1152 (graph, depth, depth_dict) = recursive_graph(
1153 c, graph, depth, depth_dict)
1155 index = str(c.get_particle().
get_index())
1156 namec = nameh +
"|#" + index
1157 graph.append((name1, namec))
1160 return (graph, depth, depth_dict)
1163 def draw_graph(graph, labels_dict=None, graph_layout='spring',
1164 node_size=5, node_color=
None, node_alpha=0.3,
1165 node_text_size=11, fixed=
None, pos=
None,
1166 edge_color=
'blue', edge_alpha=0.3, edge_thickness=1,
1168 validation_edges=
None,
1169 text_font=
'sans-serif',
1172 import matplotlib
as mpl
1174 import networkx
as nx
1175 import matplotlib.pyplot
as plt
1176 from math
import sqrt, pi
1182 if type(edge_thickness)
is list:
1183 for edge,weight
in zip(graph,edge_thickness):
1184 G.add_edge(edge[0], edge[1], weight=weight)
1187 G.add_edge(edge[0], edge[1])
1189 if node_color==
None:
1190 node_color_rgb=(0,0,0)
1191 node_color_hex=
"000000"
1196 for node
in G.nodes():
1197 cctuple=cc.rgb(node_color[node])
1198 tmpcolor_rgb.append((cctuple[0]/255,cctuple[1]/255,cctuple[2]/255))
1199 tmpcolor_hex.append(node_color[node])
1200 node_color_rgb=tmpcolor_rgb
1201 node_color_hex=tmpcolor_hex
1204 if type(node_size)
is dict:
1206 for node
in G.nodes():
1207 size=sqrt(node_size[node])/pi*10.0
1208 tmpsize.append(size)
1211 for n,node
in enumerate(G.nodes()):
1212 color=node_color_hex[n]
1214 nx.set_node_attributes(G,
"graphics", {node : {
'type':
'ellipse',
'w': size,
'h': size,
'fill':
'#'+color,
'label': node}})
1215 nx.set_node_attributes(G,
"LabelGraphics", {node : {
'type':
'text',
'text':node,
'color':
'#000000',
'visible':
'true'}})
1217 for edge
in G.edges():
1218 nx.set_edge_attributes(G,
"graphics", {edge : {
'width': 1,
'fill':
'#000000'}})
1220 for ve
in validation_edges:
1222 if (ve[0],ve[1])
in G.edges():
1223 print(
"found forward")
1224 nx.set_edge_attributes(G,
"graphics", {ve : {
'width': 1,
'fill':
'#00FF00'}})
1225 elif (ve[1],ve[0])
in G.edges():
1226 print(
"found backward")
1227 nx.set_edge_attributes(G,
"graphics", {(ve[1],ve[0]) : {
'width': 1,
'fill':
'#00FF00'}})
1229 G.add_edge(ve[0], ve[1])
1231 nx.set_edge_attributes(G,
"graphics", {ve : {
'width': 1,
'fill':
'#FF0000'}})
1235 if graph_layout ==
'spring':
1237 graph_pos = nx.spring_layout(G,k=1.0/8.0,fixed=fixed,pos=pos)
1238 elif graph_layout ==
'spectral':
1239 graph_pos = nx.spectral_layout(G)
1240 elif graph_layout ==
'random':
1241 graph_pos = nx.random_layout(G)
1243 graph_pos = nx.shell_layout(G)
1247 nx.draw_networkx_nodes(G, graph_pos, node_size=node_size,
1248 alpha=node_alpha, node_color=node_color_rgb,
1250 nx.draw_networkx_edges(G, graph_pos, width=edge_thickness,
1251 alpha=edge_alpha, edge_color=edge_color)
1252 nx.draw_networkx_labels(
1253 G, graph_pos, labels=labels_dict, font_size=node_text_size,
1254 font_family=text_font)
1256 plt.savefig(out_filename)
1257 nx.write_gml(G,
'out.gml')
1265 from ipyD3
import d3object
1266 from IPython.display
import display
1268 d3 = d3object(width=800,
1273 title=
'Example table with d3js',
1274 desc=
'An example table created created with d3js with data generated with Python.')
1360 [72.0, 60.0, 60.0, 10.0, 120.0, 172.0, 1092.0, 675.0, 408.0, 360.0, 156.0, 100.0]]
1361 data = [list(i)
for i
in zip(*data)]
1362 sRows = [[
'January',
1374 sColumns = [[
'Prod {0}'.format(i)
for i
in range(1, 9)],
1375 [
None,
'',
None,
None,
'Group 1',
None,
None,
'Group 2']]
1376 d3.addSimpleTable(data,
1377 fontSizeCells=[12, ],
1380 sRowsMargins=[5, 50, 0],
1381 sColsMargins=[5, 20, 10],
1384 addOutsideBorders=-1,
1388 html = d3.render(mode=[
'html',
'show'])
static bool get_is_setup(const IMP::ParticleAdaptor &p)
A class for reading stat files.
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.
std::string get_module_version()
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
def get_fields
Get the desired field names, and return a dictionary.
static bool get_is_setup(Model *m, ParticleIndex pi)
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.
A decorator for a particle representing an atom.
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)
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.
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.
std::string get_module_version()
A decorator for a particle with x,y,z coordinates and a radius.