Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/9369dbad919d/allpy/_monomer.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 07:39:50 2013
Кодировка:
allpy: 9369dbad919d allpy/_monomer.py

allpy

view allpy/_monomer.py @ 251:9369dbad919d

Incompatible changes to Monomer interfaces. This branch does not work! - (!!) only changed allpy._monomer, not uses - (!!) removed (temporarily) classes for specific monomer types (DNAMonomer, etc) - refurbished allpy.data.AAcodes to allpy.data.codes with much cleaner interface - refurbished allpy._monomer for simplicity and more friendly interface Now it will (someday) be possible to say: a = Monomer.from_name("alanine") b = protein.Monomer.from_code1("a") c = protein.MonomerType.from_code3("ala") d = dna.Monomer.from_code3("DA") but impossible to say: d = protein.Monomer.from_code3("DA")
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 13 Dec 2010 20:12:11 +0300
parents fc6418e32e3c
children 4033c9b05888
line source
1 #!/usr/bin/python
3 import data.codes as table
5 class MonomerType(object):
6 """Class of monomer types.
8 Each MonomerType object represents a known monomer type, e.g. Valine,
9 and is referenced to by each instance of monomer in a given sequence.
11 - `name`: full name of monomer type
12 - `code1`: one-letter code
13 - `code3`: three-letter code
14 - `is_modified`: either of True or False
16 class atributes:
17 - `by_code1`: a mapping from one-letter code to MonomerType object
18 - `by_code3`: a mapping from three-letter code to MonomerType object
19 - `by_name`: a mapping from monomer name to MonomerType object
20 - `instance_type`: class of Monomer objects to use when creating new
21 objects; this must be redefined in descendent classes
23 All of the class attributes MUST be redefined when subclassing.
24 """
26 by_code1 = {}
27 by_code3 = {}
28 by_name = {}
29 instance_type = None
31 def __init__(self, name="", code1="", code3="", is_modified=False):
32 self.name = name.capitalize()
33 self.code1 = code1.upper()
34 self.code3 = code3.upper()
35 self.is_modified = bool(is_modified)
36 if not is_modified:
37 self.by_code1[self.code1] = self
38 self.by_code3[code3] = self
39 self.by_name[name] = self
40 # We duplicate distinguished long names into MonomerType itself,
41 # so that we can use MonomerType.from_code3 to create the relevant
42 # type of monomer.
43 MonomerType.by_code3[code3] = self
44 MonomerType.by_name[name] = self
46 @classmethod
47 def _initialize(cls, type_letter, codes=table.codes):
48 """Create all relevant instances of MonomerType.
50 `type_letter` is either of:
51 - 'p' for protein
52 - 'd' for DNA
53 - 'r' for RNA
55 `codes` is a table of monomer codes
56 """
57 for type, code1, is_modified, code3, name in codes:
58 if type == type_letter:
59 cls(name, code1, code3, is_modified)
61 @classmethod
62 def from_code1(cls, code1):
63 """Return monomer type by one-letter code."""
64 return cls.by_code1[code1.upper()]
66 @clasmethod
67 def from_code3(cls, code3):
68 """Return monomer type by three-letter code."""
69 return cls.by_code3[code3.upper()]
71 @classmethod
72 def from_name(cls, name):
73 """Return monomer type by name."""
74 return cls.by_name[name.capitalize()]
76 def instance(self):
77 """Create a new monomer of given type."""
78 return self.instance_type(self)
80 def __eq__(self, other):
81 if hasattr(other, "type"):
82 return self is other.type
83 return self is other
85 class Monomer(object):
86 """Monomer object.
88 attributes:
89 - `type`: type of monomer (a MonomerType object)
91 class attribute `monomer_type` is MonomerType or either of it's subclasses,
92 it is used when creating new monomers. It MUST be redefined when subclassing Monomer.
93 """
94 monomer_type = MonomerType
96 def __init__(self, type):
97 self.type = type
99 @classmethod
100 def from_code1(cls, code1):
101 return cls(cls.monomer_type.by_code1[code1.upper()])
103 @classmethod
104 def from_code3(cls, code3):
105 return cls(cls.monomer_type.by_code3[code3.upper()])
107 @classmethod
108 def from_name(cls, name):
109 return cls(cls.monomer_type.by_name[name.capitalize()])
111 def __eq__(self, other):
112 if hasattr(other, "type"):
113 return self.type is other.type
114 return self.type is other
116 # vim: set ts=4 sts=4 sw=4 et: