view allpy/markups.py @ 852:077e5e18a600
Added allpy.markups.MarkupIOMixin to simplify new saveable markups creation. Switched existing saveable makrups to using it. Tested. (closes #82)
author |
Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
date |
Wed, 20 Jul 2011 20:44:59 +0400 |
parents |
137981a4686f |
children |
9b253b69f8f1 |
line source
4 """A dictionary of default markup name -> markup class."""
7 """Update `by_name` dictionary.
9 If any arguments are given, add them to markups namespace beforehands.
11 # Add user classes if necessary
12 for markup_class in args:
13 class_name = markup_class.__name__
14 assert class_name not in globals(), "SameNamed markup already exists!"
15 globals()[class_name] = markup_class
16 # Update `by_name` dictonary
19 for markup_class in globals().values():
20 if hasattr(markup_class, 'name') and hasattr(markup_class, 'kind'):
21 fullname = markup_class.kind, markup_class.name
22 assert fullname not in by_name, "Samenamed markup already exists!"
23 by_name[fullname] = markup_class
25 class MarkupIOMixin(base.Markup):
26 """Standard helper mixin for creating saveable markups."""
29 """Separator to use when saving/loading markup."""
32 """MUST be overloaded when subclassing. io_class in file."""
35 def parse_item(key, value):
36 """Deserialize one item of markup. Overload when subclassing."""
40 def repr_item(key, value):
41 """Serialize one item of markup. Overload when subclassing."""
45 def from_record(cls, container, record, name=None):
46 """Read markup from semi-parsed record from 'markup' file."""
47 assert record['io_class'] == cls.io_class
48 separator = record.get('separator', cls.separator)
49 values = record['markup'].split(separator)
50 result = container.add_markup(name, markup_class=cls)
51 assert len(values) == len(result.sorted_keys())
52 for key, value in zip(result.sorted_keys(), values):
54 result[key] = cls.parse_item(key, value)
58 """Write markup to semi-serialized record for 'markup' file."""
60 for key in self.sorted_keys():
62 values.append(self.repr_item(key, self[key]))
65 markup = self.separator.join(values)
68 'io_class': self.io_class,
69 'separator': self.separator,
72 class IntMarkupMixin(MarkupIOMixin):
73 """Markup that has integer values."""
75 io_class = 'IntMarkup'
78 def parse_item(key, value):
81 class SequenceNumberMarkup(base.SequenceMarkup):
86 for number, monomer in enumerate(self.sequence, 1):
87 monomer.number = number
89 class SequenceIndexMarkup(base.SequenceMarkup):
94 for index, monomer in enumerate(self.sequence):
97 class AlignmentNumberMarkup(base.AlignmentMarkup):
102 for number, column in enumerate(self.alignment.columns, 1):
103 self[column] = number
105 class AlignmentIndexMarkup(base.AlignmentMarkup):
110 for index, column in enumerate(self.alignment.columns):
113 class SequenceCaseMarkup(base.SequenceMarkup, MarkupIOMixin):
116 io_class = 'SequenceCaseMarkup'
119 for monomer in self.sequence:
120 if monomer.input_code1.isupper():
121 monomer.case = 'upper'
122 elif monomer.input_code1.islower():
123 monomer.case = 'lower'
126 def parse_value(monomer, value):
127 assert mnomer.code1 == value.upper()
134 def repr_value(monomer, value):
135 if monomer.case == 'upper':
136 return monomer.code1.upper()
137 if monomer.case == 'lower':
138 return monomer.code1.lower()
139 raise AssertionError("Unknown monomer case")
141 class SequencePdbResiMarkup(base.SequenceMarkup, IntMarkupMixin):
145 for monomer in self.sequence:
147 monomer.pdb_resi = monomer.pdb_residue.id[1]
151 def add_pdb(self, download_pdb=None, xyz_only=False):
153 if download_pdb is None:
154 download_pdb = structure.cached_download_pdb
156 match = structure.pdb_id_parse(self.sequence.name)
157 code, model , chain = match['code'], match['model'], match['chain']
158 pdb_file = download_pdb(code)
159 pdb_structure = structure.get_structure(pdb_file, self.sequence.name)
160 pdb_chain = pdb_structure[0][chain]
162 self.sequence.pdb_chain = pdb_chain
163 for monomer in self.sequence:
165 pdb_residue = pdb_chain[' ', monomer.pdb_resi, ' ']
166 monomer.ca_xyz = pdb_residue['CA'].get_vector()
168 monomer.pdb_residue = pdb_residue
170 # This MUST be the last statement in this module.
173 # vim: set ts=4 sts=4 sw=4 et: