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