IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
domino/save_assignments.py

It can often be useful to save the assignments to a file. IMP provides support to do this to a data set in an HDF5 file.

You can use h5dump to view the contents of the created file.

1 ## \example domino/save_assignments.py
2 # It can often be useful to save the assignments to a file. \imp provides
3 # support to do this to a data set in an HDF5 file.
4 #
5 # You can use \c h5dump to view the contents of the created file.
6 
7 import IMP.domino
8 import sys
9 
10 IMP.setup_from_argv(sys.argv, "save assignments")
11 
12 # create a model and some particles; they are just used as markers here
13 m = IMP.Model()
14 ps = [IMP.Particle(m) for i in range(0, 10)]
15 
16 # create a subset with a few of the particles
17 ss = IMP.domino.Subset([ps[3], ps[5], ps[7]])
18 
19 file_name = IMP.create_temporary_file_name("assignments", ".asn")
20 
21 print("File name is", file_name)
22 
23 # create a list of assignments
24 
25 IMP.set_log_level(IMP.MEMORY)
26 asl = IMP.domino.WriteAssignmentContainer(file_name, ss, ps, "writer")
27 written = []
28 for i in range(0, 5):
29  for j in range(0, 5):
30  for k in range(0, 5):
31  a = IMP.domino.Assignment([i, j, k])
32  written.append(a)
33  asl.add_assignment(a)
34 
35 del asl
36 
37 # to check, we can read it back immediately
38 back_asl = IMP.domino.ReadAssignmentContainer(file_name, ss, ps, "reader")
39 
40 
41 if back_asl.get_assignments() == written:
42  print("They match!")
43 else:
44  print(back_asl.get_assignments())
45  print(written)
46  raise RuntimeError("They don't match :-(")
47 
48 
49 # More interestingly, we can create a new model and read back the
50 # assignments for that
51 mp = IMP.Model()
52 psp = [IMP.Particle(mp) for i in range(0, 10)]
53 
54 # create a subset with a few of the particles
55 ssp = IMP.domino.Subset([psp[3], psp[5], psp[7]])
56 
57 print("Note the subsets are differently ordered (most of the time): ", ss, ssp)
58 
59 back_aslp = IMP.domino.ReadAssignmentContainer(file_name, ssp, psp, "reader2")
60 
61 # however, the states are demuxed so they match the particles
62 print([str(a) for a in back_aslp.get_assignments()])