IMP  2.0.1
The Integrative Modeling Platform
_display.py
1 import IMP
2 import IMP.display
3 
4 class Display(object):
5  """Store Display"""
6  def __init__(self):
7  self._children = list()
8 
9  def create_log(self, repr, log_name):
10  """Create Chimera log.
11  @param repr <a href="classIMP_1_1restrainer_1_1representation_1_1Representation.html">
12  Representation</a> object.
13  @param log_name Log name
14  @return IMP::display::WriteOptimizerState"""
15 
16  # Try to create Chimera log (like in display_log.py example)
18  # Find IMP particle that corresponds to a representation id
19  for child in self._children:
20  child.find_model_decorator(repr)
21  # Detach parents and children, so the geometries are separate (so that
22  # we can set separate colors)
23  for child in self._children:
24  child.detach_parent()
25  # Add geometries of proper colors to the log
26  for child in self._children:
27  child.create_xyz(1.0, 1.0, 1.0, log)
28  # Reunite parents with children
29  for child in self._children:
30  child.attach_parent()
31  return log
32 
33  def __str__(self):
34  return '<Display>\n%s\n</Display>' %\
35  ('\n'.join([child._to_str(1) for child in self._children]))
36 
37 class _DisplayNode(object):
38  counter = 0
39  def __init__(self, attributes):
40  id = attributes.get('id')
41  if id:
42  self.id = id
43  else:
44  self.id = 'object_%d' % _DisplayNode.counter
45  _DisplayNode.counter += 1
46  self._children = list()
47 
48  def get_color(self):
49  for child in self._children:
50  if isinstance(child, _DisplayColor):
51  return child
52  return None
53 
54  def find_model_decorator(self, repr):
55  particle = repr.find_by_id(self.id)
56  if particle and particle.model_decorator:
57  self.model_decorator = particle.model_decorator
58  else:
59  self.model_decorator = None
60  for child in self._children:
61  child.find_model_decorator(repr)
62 
63  def detach_parent(self):
64  if self.model_decorator:
65  self.parent = self.model_decorator.get_parent()
66  self.parent.remove_child(self.model_decorator)
67  for child in self._children:
68  child.detach_parent()
69 
70  def attach_parent(self):
71  if self.model_decorator:
72  self.parent.add_child(self.model_decorator)
73  for child in self._children:
74  child.attach_parent()
75 
76  def create_xyz(self, r, g, b, log):
77  color = self.get_color()
78  if color:
79  r = color.r
80  g = color.g
81  b = color.b
82  atom_list = list()
83  if self.model_decorator:
84  atoms = IMP.atom.get_by_type(self.model_decorator,
85  IMP.atom.XYZR_TYPE)
86  for atomh in atoms:
87  for atom in IMP.atom.get_leaves(atomh):
88  atom_list.append(atom)
89  if len(atom_list) >0:
91  geometry = IMP.core.XYZRsGeometry(c)
92  geometry.set_name(self.id)
93  geometry.set_color(IMP.display.Color(r, g, b))
94  log.add_geometry(geometry)
95  for child in self._children:
96  child.create_xyz(r, g, b, log)
97 
98  def _attr_to_str(self):
99  return ('DisplayNode', 'id="%s"' % self.id)
100 
101  def _to_str(self, level):
102  indent = ' '*level
103  name, strattr = self._attr_to_str()
104  if not self._children:
105  return '%s<%s %s/>' % (indent, name, strattr)
106  else:
107  return '%s<%s %s>\n%s\n%s</%s>' %\
108  (indent, name, strattr,
109  '\n'.join([child._to_str(level + 1)
110  for child in self._children]), indent, name)
111 
112  def __str__(self):
113  return self._to_str(0)
114 
115 
116 class _DisplayColor(_DisplayNode):
117  def __init__(self, attributes):
118  _DisplayNode.__init__(self, attributes)
119  self.r = float(attributes.get('r', 0))
120  self.g = float(attributes.get('g', 0))
121  self.b = float(attributes.get('b', 0))
122 
123  def _attr_to_str(self):
124  return ('Color', 'id="%s" r="%s" g="%s" b="%s"' %
125  (self.id, self.r, self.g, self.b))
126 
127 class _DisplayResidue(_DisplayNode):
128  def __init__(self, attributes):
129  _DisplayNode.__init__(self, attributes)
130  self.start = int(attributes.get('start', -1))
131  self.end = int(attributes.get('end', -1))
132 
133  def _attr_to_str(self):
134  return ('Residue', 'id="%s" start="%s" end="%s"' %
135  (self.id, self.start, self.end))
136 
137 class _DisplayUniverse(_DisplayNode):
138  def __init__(self, attributes):
139  _DisplayNode.__init__(self, attributes)
140 
141  def _attr_to_str(self):
142  return ('Universe', 'id="%s"' % self.id)
143 
144 class _DisplayCollection(_DisplayNode):
145  def __init__(self, attributes):
146  _DisplayNode.__init__(self, attributes)
147 
148  def _attr_to_str(self):
149  return ('Collection', 'id="%s"' % self.id)
150 
151 class _DisplayAssembly(_DisplayNode):
152  def __init__(self, attributes):
153  _DisplayNode.__init__(self, attributes)
154 
155  def _attr_to_str(self):
156  return ('Assembly', 'id="%s"' % self.id)
157 
158 class _DisplaySegment(_DisplayNode):
159  def __init__(self, attributes):
160  _DisplayNode.__init__(self, attributes)
161 
162  def _attr_to_str(self):
163  return ('Segment', 'id="%s"' % self.id)
164 
165 class _DisplayMolecule(_DisplayNode):
166  def __init__(self, attributes):
167  _DisplayNode.__init__(self, attributes)
168 
169  def _attr_to_str(self):
170  return ('Molecule', 'id="%s"' % self.id)
171 
172 class _DisplayProtein(_DisplayNode):
173  def __init__(self, attributes):
174  _DisplayNode.__init__(self, attributes)
175 
176  def _attr_to_str(self):
177  return ('Protein', 'id="%s"' % self.id)
178 
179 class _DisplayNucleicAcid(_DisplayNode):
180  def __init__(self, attributes):
181  _DisplayNode.__init__(self, attributes)
182 
183  def _attr_to_str(self):
184  return ('NucleicAcid', 'id="%s"' % self.id)
185 
186 class _DisplayChain(_DisplayNode):
187  def __init__(self, attributes):
188  _DisplayNode.__init__(self, attributes)
189 
190  def _attr_to_str(self):
191  return ('Chain', 'id="%s"' % self.id)
192 
193 class _DisplayFragment(_DisplayNode):
194  def __init__(self, attributes):
195  _DisplayNode.__init__(self, attributes)
196 
197  def _attr_to_str(self):
198  return ('Fragment', 'id="%s"' % self.id)