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          @return:            The model container. 
 70          @rtype:             ModelContainer instance 
 71          """ 
 72   
 73           
 74          if len(self) and self.is_empty(): 
 75              self[0].num = model_num 
 76   
 77           
 78          else: 
 79               
 80              if model_num in self.current_models: 
 81                  raise RelaxError("The model '" + repr(model_num) + "' already exists.") 
 82   
 83               
 84              self.append(ModelContainer(model_num)) 
 85   
 86           
 87          self.current_models.append(model_num) 
 88   
 89           
 90          return self[-1] 
  91   
 92   
 94          """Delete the given model from the list. 
 95   
 96          @keyword model_num: The model to delete. 
 97          @type model_num:    int 
 98          """ 
 99   
100           
101          if model_num not in self.current_models: 
102              raise RelaxError("The model %s does not exist." % model_num) 
103   
104           
105          index = self.current_models.index(model_num) 
106          self.pop(index) 
107          self.current_models.pop(index) 
 108   
109   
111          """Method for testing if this ModelList object is empty. 
112   
113          @return:    True if this list only has one ModelContainer and the model name has not 
114                      been set, False otherwise. 
115          @rtype:     bool 
116          """ 
117   
118           
119          if len(self) == 0: 
120              return True 
121   
122           
123          if len(self) == 1 and self[0].is_empty(): 
124              return True 
125   
126           
127          return False 
 128   
129   
130 -    def from_xml(self, model_nodes, file_version=1): 
 131          """Recreate a model list data structure from the XML model nodes. 
132   
133          @param model_nodes:     The model XML nodes. 
134          @type model_nodes:      xml.dom.minicompat.NodeList instance 
135          @keyword file_version:  The relax XML version of the XML file. 
136          @type file_version:     int 
137          """ 
138   
139           
140          if not self.is_empty(): 
141              raise RelaxFromXMLNotEmptyError(self.__class__.__name__) 
142   
143           
144          for model_node in model_nodes: 
145               
146              num = eval(model_node.getAttribute('num')) 
147              if num == 'None': 
148                  num = None 
149              self.add_item(model_num=num) 
150   
151               
152              mol_nodes = model_node.getElementsByTagName('mol_cont') 
153   
154               
155              self[-1].mol.from_xml(mol_nodes, file_version=file_version) 
 156   
157   
158 -    def to_xml(self, doc, element): 
 159          """Create XML elements for each model. 
160   
161          @param doc:     The XML document object. 
162          @type doc:      xml.dom.minidom.Document instance 
163          @param element: The element to add the model XML elements to. 
164          @type element:  XML element object 
165          """ 
166   
167           
168          for i in range(len(self)): 
169               
170              model_element = doc.createElement('model') 
171              element.appendChild(model_element) 
172   
173               
174              model_element.setAttribute('desc', 'Model container') 
175              model_element.setAttribute('num', str(self[i].num)) 
176   
177               
178              fill_object_contents(doc, model_element, object=self[i], blacklist=['num', 'mol'] + list(self[i].__class__.__dict__.keys())) 
179   
180               
181              self[i].mol.to_xml(doc, model_element) 
  182   
183   
184   
186      """Class containing all the model specific data.""" 
187   
189          """Set up the default objects of the model data container.""" 
190   
191           
192          self.num = model_num 
193   
194           
195          self.mol = MolList() 
 196   
197   
199          """The string representation of the object. 
200   
201          Rather than using the standard Python conventions (either the string representation of the 
202          value or the "<...desc...>" notation), a rich-formatted description of the object is given. 
203          """ 
204   
205           
206          text = "Class containing the data for model %s.\n" % self.num 
207   
208           
209          text = text + "\n" 
210          text = text + "Objects:\n" 
211          for name in dir(self): 
212               
213              if name == 'mol': 
214                  text = text + "  mol: The list of %s molecules within the model.\n" % len(self.mol) 
215                  continue 
216   
217               
218              if name == 'is_empty': 
219                  continue 
220   
221               
222              if match("^__", name): 
223                  continue 
224   
225               
226              text = text + "  " + name + ": " + repr(getattr(self, name)) + "\n" 
227   
228          return text 
 229   
230   
232          """Method for testing if this ModelContainer object is empty. 
233   
234          @return:    True if this container is empty and the model number has not been set, False 
235                      otherwise. 
236          @rtype:     bool 
237          """ 
238   
239           
240          if self.num != None: 
241              return False 
242   
243           
244          for name in dir(self): 
245               
246              if name == 'num' or name == 'mol': 
247                  continue 
248   
249               
250              if name == 'is_empty': 
251                  continue 
252   
253               
254              if match("^__", name): 
255                  continue 
256   
257               
258              return False 
259   
260           
261          if not self.mol.is_empty(): 
262              return False 
263   
264           
265          return True 
 266   
267   
269          """Generator method to loop over the molecules of this model. 
270   
271          @return:    The molecules of this model. 
272          @rtype:     MolContainer instance 
273          """ 
274   
275           
276          for mol in self.mol: 
277               
278              if mol.is_empty(): 
279                  continue 
280   
281               
282              yield mol 
  283