1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """The objects representing models in the internal structural object."""
24
25
26 from re import match
27
28
29 from data_store.relax_xml import fill_object_contents
30 from lib.errors import RelaxError, RelaxFromXMLNotEmptyError
31 from lib.structure.internal.molecules import MolList
32
33
35 """List type data container for the different structural models.
36
37 Here different models are defined as the same molecule but with different conformations.
38 """
39
41 """The string representation of the object.
42
43 Rather than using the standard Python conventions (either the string representation of the
44 value or the "<...desc...>" notation), a rich-formatted description of the object is given.
45 """
46
47 text = "Models.\n\n"
48 text = text + "%-8s%-8s" % ("Index", "Model number") + "\n"
49 for i in range(len(self)):
50 text = text + "%-8i%-8s" % (i, self[i].num) + "\n"
51 return text
52
53
55 """Append an empty ModelContainer to the ModelList.
56
57 @keyword model_num: The model number.
58 @type model_num: int
59 """
60
61
62 if len(self) and self.is_empty():
63 self[0].num = model_num
64
65
66 else:
67
68 for i in range(len(self)):
69 if self[i].num == model_num:
70 raise RelaxError("The model '" + repr(model_num) + "' already exists.")
71
72
73 self.append(ModelContainer(model_num))
74
75
76 if not hasattr(self, 'model_indices'):
77 self.model_indices = {}
78 self.model_indices[model_num] = len(self) - 1
79
80
81 self.model_list = list(self.model_indices.keys())
82 self.model_list.sort()
83
84
86 """Method for testing if this ModelList object is empty.
87
88 @return: True if this list only has one ModelContainer and the model name has not
89 been set, False otherwise.
90 @rtype: bool
91 """
92
93
94 if len(self) == 0:
95 return True
96
97
98 if len(self) == 1 and self[0].is_empty():
99 return True
100
101
102 return False
103
104
105 - def from_xml(self, model_nodes, file_version=1):
106 """Recreate a model list data structure from the XML model nodes.
107
108 @param model_nodes: The model XML nodes.
109 @type model_nodes: xml.dom.minicompat.NodeList instance
110 @keyword file_version: The relax XML version of the XML file.
111 @type file_version: int
112 """
113
114
115 if not self.is_empty():
116 raise RelaxFromXMLNotEmptyError(self.__class__.__name__)
117
118
119 for model_node in model_nodes:
120
121 num = eval(model_node.getAttribute('num'))
122 if num == 'None':
123 num = None
124 self.add_item(model_num=num)
125
126
127 mol_nodes = model_node.getElementsByTagName('mol_cont')
128
129
130 self[-1].mol.from_xml(mol_nodes, file_version=file_version)
131
132
133 - def to_xml(self, doc, element):
134 """Create XML elements for each model.
135
136 @param doc: The XML document object.
137 @type doc: xml.dom.minidom.Document instance
138 @param element: The element to add the model XML elements to.
139 @type element: XML element object
140 """
141
142
143 for i in range(len(self)):
144
145 model_element = doc.createElement('model')
146 element.appendChild(model_element)
147
148
149 model_element.setAttribute('desc', 'Model container')
150 model_element.setAttribute('num', str(self[i].num))
151
152
153 fill_object_contents(doc, model_element, object=self[i], blacklist=['num', 'mol'] + list(self[i].__class__.__dict__.keys()))
154
155
156 self[i].mol.to_xml(doc, model_element)
157
158
159
161 """Class containing all the model specific data."""
162
164 """Set up the default objects of the model data container."""
165
166
167 self.num = model_num
168
169
170 self.mol = MolList()
171
172
174 """The string representation of the object.
175
176 Rather than using the standard Python conventions (either the string representation of the
177 value or the "<...desc...>" notation), a rich-formatted description of the object is given.
178 """
179
180
181 text = "Class containing the data for model %s.\n" % self.num
182
183
184 text = text + "\n"
185 text = text + "Objects:\n"
186 for name in dir(self):
187
188 if name == 'mol':
189 text = text + " mol: The list of %s molecules within the model.\n" % len(self.mol)
190 continue
191
192
193 if name == 'is_empty':
194 continue
195
196
197 if match("^__", name):
198 continue
199
200
201 text = text + " " + name + ": " + repr(getattr(self, name)) + "\n"
202
203 return text
204
205
207 """Method for testing if this ModelContainer object is empty.
208
209 @return: True if this container is empty and the model number has not been set, False
210 otherwise.
211 @rtype: bool
212 """
213
214
215 if self.num != None:
216 return False
217
218
219 for name in dir(self):
220
221 if name == 'num' or name == 'mol':
222 continue
223
224
225 if name == 'is_empty':
226 continue
227
228
229 if match("^__", name):
230 continue
231
232
233 return False
234
235
236 if not self.mol.is_empty():
237 return False
238
239
240 return True
241