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

Source Code for Module lib.structure.internal.models

  1  0############################################################################## 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2014 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 lib.errors import RelaxError, RelaxFromXMLNotEmptyError 
 30  from lib.structure.internal.molecules import MolList 
 31  from lib.xml import fill_object_contents 
 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 __init__(self):
41 """Set up the class.""" 42 43 # Execute the base class method. 44 super(ModelList, self).__init__() 45 46 # The current model list (used for speed). 47 self.current_models = []
48 49
50 - def __repr__(self):
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
64 - def add_item(self, model_num=None):
65 """Append an empty ModelContainer to the ModelList. 66 67 @keyword model_num: The model number. 68 @type model_num: int 69 """ 70 71 # If no model data exists, replace the empty first model with this model (just a renumbering). 72 if len(self) and self.is_empty(): 73 self[0].num = model_num 74 75 # Otherwise append an empty ModelContainer. 76 else: 77 # Test if the model number already exists. 78 if model_num in self.current_models: 79 raise RelaxError("The model '" + repr(model_num) + "' already exists.") 80 81 # Append an empty ModelContainer. 82 self.append(ModelContainer(model_num)) 83 84 # Update the current model list. 85 self.current_models.append(model_num)
86 87
88 - def is_empty(self):
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 # No ModelContainers. 97 if len(self) == 0: 98 return True 99 100 # There is only one ModelContainer and it is empty. 101 if len(self) == 1 and self[0].is_empty(): 102 return True 103 104 # Otherwise. 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 # Test if empty. 118 if not self.is_empty(): 119 raise RelaxFromXMLNotEmptyError(self.__class__.__name__) 120 121 # Loop over the models. 122 for model_node in model_nodes: 123 # Get the model details and add the model to the ModelList structure. 124 num = eval(model_node.getAttribute('num')) 125 if num == 'None': 126 num = None 127 self.add_item(model_num=num) 128 129 # Get the molecule nodes. 130 mol_nodes = model_node.getElementsByTagName('mol_cont') 131 132 # Recreate the molecule data structures for the current model. 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 # Loop over the models. 146 for i in range(len(self)): 147 # Create an XML element for this model and add it to the higher level element. 148 model_element = doc.createElement('model') 149 element.appendChild(model_element) 150 151 # Set the model attributes. 152 model_element.setAttribute('desc', 'Model container') 153 model_element.setAttribute('num', str(self[i].num)) 154 155 # Add all simple python objects within the ModelContainer to the XML element. 156 fill_object_contents(doc, model_element, object=self[i], blacklist=['num', 'mol'] + list(self[i].__class__.__dict__.keys())) 157 158 # Add the molecule data. 159 self[i].mol.to_xml(doc, model_element)
160 161 162
163 -class ModelContainer(object):
164 """Class containing all the model specific data.""" 165
166 - def __init__(self, model_num=None):
167 """Set up the default objects of the model data container.""" 168 169 # The model number. 170 self.num = model_num 171 172 # The empty molecule list. 173 self.mol = MolList()
174 175
176 - def __repr__(self):
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 # Intro. 184 text = "Class containing the data for model %s.\n" % self.num 185 186 # Objects. 187 text = text + "\n" 188 text = text + "Objects:\n" 189 for name in dir(self): 190 # Molecule list. 191 if name == 'mol': 192 text = text + " mol: The list of %s molecules within the model.\n" % len(self.mol) 193 continue 194 195 # Skip the ModelContainer methods. 196 if name == 'is_empty': 197 continue 198 199 # Skip special objects. 200 if match("^__", name): 201 continue 202 203 # Add the object's attribute to the text string. 204 text = text + " " + name + ": " + repr(getattr(self, name)) + "\n" 205 206 return text
207 208
209 - def is_empty(self):
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 # The model num has been set. 218 if self.num != None: 219 return False 220 221 # An object has been added to the container. 222 for name in dir(self): 223 # Skip the objects initialised in __init__(). 224 if name == 'num' or name == 'mol': 225 continue 226 227 # Skip the ModelContainer methods. 228 if name == 'is_empty': 229 continue 230 231 # Skip special objects. 232 if match("^__", name): 233 continue 234 235 # An object has been added. 236 return False 237 238 # The molecule list is not empty. 239 if not self.mol.is_empty(): 240 return False 241 242 # The ModelContainer is unmodified. 243 return True
244