Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/d60f272dc921/allpy/markups.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 05:55:00 2013
Кодировка:
allpy: d60f272dc921 allpy/markups.py

allpy

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
1 import base
3 by_name = {}
4 """A dictionary of default markup name -> markup class."""
6 def update(*args):
7 """Update `by_name` dictionary.
9 If any arguments are given, add them to markups namespace beforehands.
10 """
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
17 global by_name
18 by_name = {}
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):
27 @classmethod
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):
35 if value:
36 result[key] = int(value)
37 return result
39 def to_record(self):
40 values = self.sorted_values(default='', map=str)
41 return {'markup': ','.join(values), 'io_class': 'IntMarkup'}
43 class SequenceNumberMarkup(base.SequenceMarkup):
45 name = 'number'
47 def refresh(self):
48 for number, monomer in enumerate(self.sequence, 1):
49 monomer.number = number
51 class SequenceIndexMarkup(base.SequenceMarkup):
53 name = 'index'
55 def refresh(self):
56 for index, monomer in enumerate(self.sequence):
57 monomer.index = index
59 class AlignmentNumberMarkup(base.AlignmentMarkup):
61 name = 'number'
63 def refresh(self):
64 for number, column in enumerate(self.alignment.columns, 1):
65 self[column] = number
67 class AlignmentIndexMarkup(base.AlignmentMarkup):
69 name = 'index'
71 def refresh(self):
72 for index, column in enumerate(self.alignment.columns):
73 self[column] = index
75 class SequenceCaseMarkup(base.SequenceMarkup):
77 name = 'case'
79 def refresh(self):
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'
86 @classmethod
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] == "'"
92 markup = markup[1:-1]
93 assert len(markup) == len(result.sequence)
94 for monomer, mark in zip(result.sequence, markup):
95 assert monomer.code1 == mark.upper()
96 if mark.isupper():
97 monomer.case = 'upper'
98 if mark.islower():
99 monomer.case = 'lower'
100 return result
102 def to_record(self):
103 markup = ''
104 for monomer in self.sequence:
105 case = self.get(monomer)
106 if case == 'upper':
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):
113 name = 'pdb_resi'
115 def from_pdb(self):
116 for monomer in self.sequence:
117 try:
118 monomer.pdb_resi = monomer.pdb_residue.id[1]
119 except Exception:
120 pass
122 def add_pdb(self, download_pdb=None, xyz_only=False):
123 import structure
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]
132 if not xyz_only:
133 self.sequence.pdb_chain = pdb_chain
134 for monomer in self.sequence:
135 if monomer in self:
136 pdb_residue = pdb_chain[' ', monomer.pdb_resi, ' ']
137 monomer.ca_xyz = pdb_residue['CA'].get_vector()
138 if not xyz_only:
139 monomer.pdb_residue = pdb_residue
141 # This MUST be the last statement in this module.
142 update()
144 # vim: set ts=4 sts=4 sw=4 et: