Package lib :: Package structure :: Package internal :: Module models
[hide private]
[frames] | no frames]

Source Code for Module lib.structure.internal.models

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """The objects representing models in the internal structural object.""" 
 24   
 25  # Python module imports. 
 26  from re import match 
 27   
 28  # relax module import. 
 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   
34 -class ModelList(list):
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
40 - def __repr__(self):
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
54 - def add_item(self, model_num=None):
55 """Append an empty ModelContainer to the ModelList. 56 57 @keyword model_num: The model number. 58 @type model_num: int 59 """ 60 61 # If no model data exists, replace the empty first model with this model (just a renumbering). 62 if len(self) and self.is_empty(): 63 self[0].num = model_num 64 65 # Otherwise append an empty ModelContainer. 66 else: 67 # Test if the model number already exists. 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 # Append an empty ModelContainer. 73 self.append(ModelContainer(model_num)) 74 75 # Store the model indices. 76 if not hasattr(self, 'model_indices'): 77 self.model_indices = {} 78 self.model_indices[model_num] = len(self) - 1 79 80 # The sorted model numbers. 81 self.model_list = list(self.model_indices.keys()) 82 self.model_list.sort()
83 84
85 - def is_empty(self):
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 # No ModelContainers. 94 if len(self) == 0: 95 return True 96 97 # There is only one ModelContainer and it is empty. 98 if len(self) == 1 and self[0].is_empty(): 99 return True 100 101 # Otherwise. 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 # Test if empty. 115 if not self.is_empty(): 116 raise RelaxFromXMLNotEmptyError(self.__class__.__name__) 117 118 # Loop over the models. 119 for model_node in model_nodes: 120 # Get the model details and add the model to the ModelList structure. 121 num = eval(model_node.getAttribute('num')) 122 if num == 'None': 123 num = None 124 self.add_item(model_num=num) 125 126 # Get the molecule nodes. 127 mol_nodes = model_node.getElementsByTagName('mol_cont') 128 129 # Recreate the molecule data structures for the current model. 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 # Loop over the models. 143 for i in range(len(self)): 144 # Create an XML element for this model and add it to the higher level element. 145 model_element = doc.createElement('model') 146 element.appendChild(model_element) 147 148 # Set the model attributes. 149 model_element.setAttribute('desc', 'Model container') 150 model_element.setAttribute('num', str(self[i].num)) 151 152 # Add all simple python objects within the ModelContainer to the XML element. 153 fill_object_contents(doc, model_element, object=self[i], blacklist=['num', 'mol'] + list(self[i].__class__.__dict__.keys())) 154 155 # Add the molecule data. 156 self[i].mol.to_xml(doc, model_element)
157 158 159
160 -class ModelContainer(object):
161 """Class containing all the model specific data.""" 162
163 - def __init__(self, model_num=None):
164 """Set up the default objects of the model data container.""" 165 166 # The model number. 167 self.num = model_num 168 169 # The empty molecule list. 170 self.mol = MolList()
171 172
173 - def __repr__(self):
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 # Intro. 181 text = "Class containing the data for model %s.\n" % self.num 182 183 # Objects. 184 text = text + "\n" 185 text = text + "Objects:\n" 186 for name in dir(self): 187 # Molecule list. 188 if name == 'mol': 189 text = text + " mol: The list of %s molecules within the model.\n" % len(self.mol) 190 continue 191 192 # Skip the ModelContainer methods. 193 if name == 'is_empty': 194 continue 195 196 # Skip special objects. 197 if match("^__", name): 198 continue 199 200 # Add the object's attribute to the text string. 201 text = text + " " + name + ": " + repr(getattr(self, name)) + "\n" 202 203 return text
204 205
206 - def is_empty(self):
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 # The model num has been set. 215 if self.num != None: 216 return False 217 218 # An object has been added to the container. 219 for name in dir(self): 220 # Skip the objects initialised in __init__(). 221 if name == 'num' or name == 'mol': 222 continue 223 224 # Skip the ModelContainer methods. 225 if name == 'is_empty': 226 continue 227 228 # Skip special objects. 229 if match("^__", name): 230 continue 231 232 # An object has been added. 233 return False 234 235 # The molecule list is not empty. 236 if not self.mol.is_empty(): 237 return False 238 239 # The ModelContainer is unmodified. 240 return True
241