1 """Interface between XML Representation and IMP Model."""
8 class Representation(object):
9 """Store Representation."""
13 self._children = list()
16 def get_imp_hierarchy_by_id(self, id):
17 """Return an IMP::atom::Hierarchy by particle id.
18 @param id Particle id.
19 @return An IMP::atom::Hierarchy hierarchy."""
20 return self.find_by_id(id).model_decorator
22 def get_root_imp_hierarchy(self):
23 """Return the root of the IMP::atom::Hierarchy"""
24 return self.model_decorator
26 def find_all_by_id(self, id):
27 """Return a list of all nodes that have the id given by the parameter"""
32 for child
in node._children:
36 for child
in self._children:
40 def find_by_id(self, id):
41 """Return a node that have the id given by the parameter"""
46 for child
in node._children:
52 for child
in self._children:
58 def get_model(self, model=None):
59 """Return an IMP::Model that contains the representation"""
70 self.model_decorator = decorator
71 for child
in self._children:
72 child.set_parent(self)
73 for child
in self._children:
74 child.add_as_child(decorator, model)
78 return '<Representation>\n%s\n</Representation>' %\
79 (
'\n'.join([child._to_str(1)
for child
in self._children]))
82 class _RepresentationNode(object):
84 def __init__(self, attributes):
85 id = attributes.get(
'id')
89 self.id =
'object_%d' % _RepresentationNode.counter
90 _RepresentationNode.counter += 1
91 self._children = list()
92 self.model_decorator =
None
96 def set_parent(self, parent):
98 for child
in self._children:
99 child.set_parent(self)
101 def add_as_child(self, particle, model):
102 """Add this node as a child of particle."""
104 if not self.model_decorator:
105 decorator = self.to_particle(model)
106 self.model_decorator = decorator
109 particle.add_child(decorator)
112 for child
in self._children:
113 child.add_as_child(decorator, model)
115 def to_particle(self, model):
120 def _attr_to_str(self):
121 return (
'RepresentationNode',
'id="%s"' % self.id)
123 def _to_str(self, level):
125 name, strattr = self._attr_to_str()
126 if not self._children:
127 return '%s<%s %s/>' % (indent, name, strattr)
129 return '%s<%s %s>\n%s\n%s</%s>' %\
130 (indent, name, strattr,
131 '\n'.join([child._to_str(level + 1)
132 for child
in self._children]), indent, name)
135 return self._to_str(0)
138 class _RepUniverse(_RepresentationNode):
139 def __init__(self, attributes):
140 _RepresentationNode.__init__(self, attributes)
142 def _attr_to_str(self):
143 return (
'Universe',
'id="%s"' % self.id)
146 class _RepCollection(_RepresentationNode):
147 def __init__(self, attributes):
148 _RepresentationNode.__init__(self, attributes)
150 def _attr_to_str(self):
151 return (
'Collection',
'id="%s"' % self.id)
154 class _RepAssembly(_RepresentationNode):
155 def __init__(self, attributes):
156 _RepresentationNode.__init__(self, attributes)
158 def _attr_to_str(self):
159 return (
'Assembly',
'id="%s"' % self.id)
162 class _RepSegment(_RepresentationNode):
163 def __init__(self, attributes):
164 _RepresentationNode.__init__(self, attributes)
166 def _attr_to_str(self):
167 return (
'Segment',
'id="%s"' % self.id)
170 class _RepMolecule(_RepresentationNode):
171 def __init__(self, attributes):
172 _RepresentationNode.__init__(self, attributes)
174 def _attr_to_str(self):
175 return (
'Molecule',
'id="%s"' % self.id)
178 class _RepProtein(_RepresentationNode):
179 def __init__(self, attributes):
180 _RepresentationNode.__init__(self, attributes)
182 def _attr_to_str(self):
183 return (
'Protein',
'id="%s"' % self.id)
186 class _RepNucleicAcid(_RepresentationNode):
187 def __init__(self, attributes):
188 _RepresentationNode.__init__(self, attributes)
190 def _attr_to_str(self):
191 return (
'NucleicAcid',
'id="%s"' % self.id)
194 class _RepChain(_RepresentationNode):
195 def __init__(self, attributes):
196 _RepresentationNode.__init__(self, attributes)
197 self.filename = attributes.get(
'filename',
'')
198 self.chain_label = attributes.get(
'chain_label',
'')
199 self.selector = attributes.get(
'selector',
'')
201 self.force_field = int(attributes.get(
'force_field',
'1'))
207 def _attr_to_str(self):
209 'id="%s" filename="%s" chain_label="%s" selector="%s"' %
210 (self.id, self.filename, self.chain_label, self.selector))
212 def to_particle(self, model):
214 if self.selector ==
'CAlpha':
216 elif self.selector ==
'CBeta':
218 elif self.selector ==
'C':
220 elif self.selector ==
'N':
222 elif self.selector ==
'All':
224 elif self.selector ==
'Chain':
226 elif self.selector ==
'Water':
228 elif self.selector ==
'Hydrogen':
230 elif self.selector ==
'NonWater':
232 elif self.selector ==
'P':
234 elif self.selector ==
'NonAlternatives':
235 selector = IMP.atom.NonAlternativesPDBSelector()
236 elif self.selector ==
'NonWaterNonHydrogen':
243 self.fragment_decorator = chains[0]
244 parent = self.fragment_decorator.get_parent()
245 parent.remove_child(self.fragment_decorator)
246 if self._children
and not self.filename:
251 if not self.filename
and not self._children:
252 raise Exception,
"Filename must be present for childless Chain %s" % self.id
253 decorator = self.fragment_decorator
256 class _RepFragment(_RepresentationNode):
257 def __init__(self, attributes):
258 _RepresentationNode.__init__(self, attributes)
260 def _attr_to_str(self):
261 return (
'Fragment',
'id="%s"' % self.id)
263 def to_particle(self, model):
264 if len(self._children) != 1:
265 raise Exception,
"Fragment %s must have exactly one child" % self.id
267 child = self._children[0]
268 child.add_attributes(particle)
270 if isinstance(child, _RepAtomicRep):
271 if not isinstance(self.parent, _RepChain):
272 raise Exception,
"Parent of Fragment %s must be a chain" % self.id
273 if particle.has_attribute(
IMP.IntKey(
'start_residue')):
274 start_residue = particle.get_value(
IMP.IntKey(
'start_residue'))
276 raise Exception,
"Start residue is required for atomic rep of Fragment %s" % self.id
277 if particle.has_attribute(
IMP.IntKey(
'end_residue')):
278 end_residue = particle.get_value(
IMP.IntKey(
'end_residue'))
280 raise Exception,
"End residue is required for atomic rep of Fragment %s" % self.id
281 for x
in xrange(int(start_residue), int(end_residue) + 1):
284 res_parent = res_part.get_parent()
285 res_parent.remove_child(res_part)
286 decorator.add_child(res_part)
289 particle.add_attribute(
IMP.FloatKey(
'x'), random.uniform(-300, 300))
292 particle.add_attribute(
IMP.FloatKey(
'y'), random.uniform(-300, 300))
295 particle.add_attribute(
IMP.FloatKey(
'z'), random.uniform(-300, 300))
304 class _RepAtomicRep(_RepresentationNode):
305 def __init__(self, attributes):
306 _RepresentationNode.__init__(self, attributes)
307 self.start_residue = int(attributes.get(
'start_residue', -1))
308 if not self.start_residue:
309 self.start_residue = int(attributes.get(
'start_nucleotide', -1))
310 self.end_residue = int(attributes.get(
'end_residue', -1))
311 if not self.end_residue:
312 self.end_residue = int(attributes.get(
'end_nucleotide', -1))
313 if self.start_residue < 0
or self.end_residue < 0:
314 raise Exception,
"AtomicRep %s must have both start_(residue|nucleotide) and end_(residue|nucleotide)" % self.id
316 def _attr_to_str(self):
318 'id="%s" start_residue="%s" end_residue="%s"' %
319 (self.id, self.start_residue, self.end_residue))
321 def to_particle(self, model):
324 def add_attributes(self, parent):
325 parent.add_attribute(
IMP.IntKey(
"start_residue"), self.start_residue)
326 parent.add_attribute(
IMP.IntKey(
"end_residue"), self.end_residue)
327 for child
in self._children:
328 child.add_attributes(parent)
330 class _RepGeometricShapeRep(_RepresentationNode):
331 def __init__(self, attributes):
332 _RepresentationNode.__init__(self, attributes)
333 self.start_residue = int(attributes.get(
'start_residue', -1))
334 if self.start_residue < 0:
335 self.start_residue = int(attributes.get(
'start_nucleotide', -1))
336 self.end_residue = int(attributes.get(
'end_residue', -1))
337 if self.end_residue < 0:
338 self.end_residue = int(attributes.get(
'end_nucleotide', -1))
339 self.total_residue = int(attributes.get(
'total_residue', -1))
340 if self.total_residue >= 0
and self.end_residue >= 0
and \
341 self.start_residue >= 0:
342 if self.total_residue != self.end_residue + 1 - self.start_residue:
343 raise Exception,
"Total residues dubious consistency in Geometric Shape _Rep %s" % self.id
345 def _attr_to_str(self):
346 return (
'GeometricShapeRep',
347 'id="%s" start_residue="%s" end_residue="%s" total_residue="%s"' %
348 (self.id, self.start_residue, self.end_residue, self.total_residue))
350 def to_particle(self, model):
353 def add_attributes(self, parent):
354 parent.add_attribute(
IMP.IntKey(
'start_residue'), self.start_residue)
355 parent.add_attribute(
IMP.IntKey(
'end_residue'), self.end_residue)
357 if self.start_residue < 0
and self.end_residue < 0:
358 total_residue = self.total_residue
360 total_residue = self.end_residue-self.start_residue+1
361 if total_residue >= 0:
364 r = (v/(4.0*math.pi)*3.0)**(1.0/3)
366 for child
in self._children:
367 child.add_attributes(parent)
370 class _RepSphere(_RepresentationNode):
371 def __init__(self, attributes):
372 _RepresentationNode.__init__(self, attributes)
373 self.radius = float(attributes.get(
'radius', -1))
374 self.weight = float(attributes.get(
'weight', 0))
375 self.__initial_position =
None
377 def _attr_to_str(self):
379 'id="%s" radius="%s" weight="%s"' %
380 (self.id, self.radius, self.weight))
382 def initial_position(self):
383 if self.__initial_position
is None:
384 for child
in self._children:
385 if isinstance(child, _RepInitialPosition):
386 self.__initial_position = child
388 return self.__initial_position
390 def to_particle(self, model):
393 def add_attributes(self, parent):
394 parent.add_attribute(
IMP.FloatKey(
"radius"), self.radius)
395 parent.add_attribute(
IMP.FloatKey(
"weight"), self.weight)
397 for child
in self._children:
398 child.add_attributes(parent)
400 class _RepInitialPosition(_RepresentationNode):
401 def __init__(self, attributes):
402 _RepresentationNode.__init__(self, attributes)
403 self.optimize = int(attributes.get(
'optimize', -1))
404 self.x = float(attributes[
'x'])
405 self.y = float(attributes[
'y'])
406 self.z = float(attributes[
'z'])
408 def _attr_to_str(self):
409 return (
'InitialPosition',
410 'id="%s" x="%s" y="%s" z="%s" optimize="%s"' %
411 (self.id, self.x, self.y, self.z, self.optimize))
413 def to_particle(self, model):
416 def add_attributes(self, parent):
420 parent.add_attribute(fl_x, self.x)
421 parent.add_attribute(fl_y, self.y)
422 parent.add_attribute(fl_z, self.z)
423 parent.add_attribute(
IMP.IntKey(
"optimize"), self.optimize)
424 if self.optimize == 1:
425 parent.set_is_optimized(fl_x,
True)
426 parent.set_is_optimized(fl_y,
True)
427 parent.set_is_optimized(fl_z,
True)