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