view allpy/markups.py @ 842:d60f272dc921
blocks3d/wt: suggest better filenames for downloading
If input alignment provided, name of input alignment file is
concatenated with ".html" and used as name of output file.
Otherwise "blocks3d.html" name is used
author |
boris (kodomo) <bnagaev@gmail.com> |
date |
Wed, 20 Jul 2011 02:38:59 +0400 |
parents |
4f896db3531d |
children |
077e5e18a600 |
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 IntMarkupMixin(base.Markup):
28 def from_record(cls, container, record, name=None):
29 assert record['io_class'] == 'IntMarkup'
30 result = container.add_markup(name, markup_class=cls)
31 separator = record.get('separator', ',')
32 values = record['markup'].split(separator)
33 assert len(values) == len(result.sorted_keys())
34 for key, value in zip(result.sorted_keys(), values):
36 result[key] = int(value)
40 values = self.sorted_values(default='', map=str)
41 return {'markup': ','.join(values), 'io_class': 'IntMarkup'}
43 class SequenceNumberMarkup(base.SequenceMarkup):
48 for number, monomer in enumerate(self.sequence, 1):
49 monomer.number = number
51 class SequenceIndexMarkup(base.SequenceMarkup):
56 for index, monomer in enumerate(self.sequence):
59 class AlignmentNumberMarkup(base.AlignmentMarkup):
64 for number, column in enumerate(self.alignment.columns, 1):
67 class AlignmentIndexMarkup(base.AlignmentMarkup):
72 for index, column in enumerate(self.alignment.columns):
75 class SequenceCaseMarkup(base.SequenceMarkup):
80 for monomer in self.sequence:
81 if monomer.input_code1.isupper():
82 monomer.case = 'upper'
83 elif monomer.input_code1.islower():
84 monomer.case = 'lower'
87 def from_record(cls, container, record, name=None):
88 assert record['io_class'] == 'SequenceCaseMarkup'
89 result = container.add_markup(name, markup_class=cls)
90 markup = record['markup']
91 assert markup[0] == markup[-1] == "'"
93 assert len(markup) == len(result.sequence)
94 for monomer, mark in zip(result.sequence, markup):
95 assert monomer.code1 == mark.upper()
97 monomer.case = 'upper'
99 monomer.case = 'lower'
104 for monomer in self.sequence:
105 case = self.get(monomer)
107 markup += monomer.code1.upper()
108 elif case == 'lower':
109 markup += monomer.code1.lower()
110 return {'markup': "'%s'" % markup, 'io_class': 'SequenceCaseMarkup'}
112 class SequencePdbResiMarkup(base.SequenceMarkup, IntMarkupMixin):
116 for monomer in self.sequence:
118 monomer.pdb_resi = monomer.pdb_residue.id[1]
122 def add_pdb(self, download_pdb=None, xyz_only=False):
124 if download_pdb is None:
125 download_pdb = structure.cached_download_pdb
127 match = structure.pdb_id_parse(self.sequence.name)
128 code, model , chain = match['code'], match['model'], match['chain']
129 pdb_file = download_pdb(code)
130 pdb_structure = structure.get_structure(pdb_file, self.sequence.name)
131 pdb_chain = pdb_structure[0][chain]
133 self.sequence.pdb_chain = pdb_chain
134 for monomer in self.sequence:
136 pdb_residue = pdb_chain[' ', monomer.pdb_resi, ' ']
137 monomer.ca_xyz = pdb_residue['CA'].get_vector()
139 monomer.pdb_residue = pdb_residue
141 # This MUST be the last statement in this module.
144 # vim: set ts=4 sts=4 sw=4 et: