RMF
alternatives.py

The RMF format supports storing alternative representations of the structure through the RMF::decorator::Alternatives decorator and associated types. Multiple versions of the hierarchy are stored, all representing the same biological entity. Different representations can be used for different purposes based on resolution or other criteria.

1 ## \example alternatives.py
2 # The RMF format supports storing alternative representations of the structure
3 # through the RMF::decorator::Alternatives decorator and associated types.
4 # Multiple versions of the hierarchy are stored, all representing the same
5 # biological entity. Different representations can be used for different
6 # purposes based on resolution or other criteria.
7 
8 from __future__ import print_function
9 import RMF
10 
11 tfn = RMF._get_temporary_file_path("alternatives.rmf3")
12 print("File is", tfn)
13 
14 f = RMF.create_rmf_file(tfn)
15 f.add_frame("frame", RMF.FRAME)
16 
17 # make a trivial set of multiple resolution represntations
18 rh = f.get_root_node()
19 
20 pf = RMF.ParticleFactory(f)
21 gpf = RMF.GaussianParticleFactory(f)
22 rff = RMF.ReferenceFrameFactory(f)
23 
24 
25 def frange(x, y, jump):
26  while x < y:
27  yield x
28  x += jump
29 
30 
31 def create_hierarchy(radius):
32  root = f.add_node(str(radius), RMF.REPRESENTATION)
33  for i in frange(0., 8, radius):
34  for j in frange(0., 8, radius):
35  for k in frange(0., 8, radius):
36  center = RMF.Vector3(
37  i + radius / 2,
38  j + radius / 2,
39  k + radius / 2)
40  n = root.add_child(
41  str(i) + "-" + str(j) + "-" + str(k),
42  RMF.REPRESENTATION)
43  d = pf.get(n)
44  d.set_static_radius(radius)
45  d.set_frame_coordinates(center)
46  d.set_static_mass(radius ** 3)
47  return root
48 
49 
50 def create_gmm(radius=1.):
51  root = f.add_node("gmm" + str(radius), RMF.REPRESENTATION)
52  for i in frange(0., 8, radius):
53  for j in frange(0., 8, radius):
54  center = RMF.Vector3(
55  i + radius / 2,
56  j + radius / 2,
57  4)
58 
59  n = root.add_child(
60  str(i) + "-" + str(j),
61  RMF.REPRESENTATION)
62  d = gpf.get(n)
63  d.set_variances(RMF.Vector3(radius / 2, radius / 2, 4))
64  d.set_static_mass(radius ** 3)
65  rf = rff.get(n)
66  rf.set_rotation(RMF.Vector4(1, 0, 0, 0))
67  rf.set_translation(center)
68  return root
69 
70 n = create_hierarchy(4)
71 rh.add_child(n)
72 
73 af = RMF.AlternativesFactory(f)
74 da = af.get(n)
75 da.add_alternative(create_hierarchy(1), RMF.PARTICLE)
76 da.add_alternative(create_hierarchy(2), RMF.PARTICLE)
77 da.add_alternative(create_hierarchy(4), RMF.PARTICLE)
78 da.add_alternative(create_gmm(), RMF.GAUSSIAN_PARTICLE)
79 
81 
82 print("particles", RMF.get_resolutions(n, RMF.PARTICLE))
83 print("gaussian particles", RMF.get_resolutions(n, RMF.GAUSSIAN_PARTICLE))