allpy
changeset 260:aae821828b03
Moved contents of allpy._monomer to allpy.base
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Tue, 14 Dec 2010 20:51:53 +0300 |
parents | 0e8b5ad66125 |
children | d60628e29b24 |
files | allpy/_monomer.py allpy/base.py |
diffstat | 2 files changed, 116 insertions(+), 128 deletions(-) [+] |
line diff
1.1 --- a/allpy/_monomer.py Tue Dec 14 20:05:13 2010 +0300 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,119 +0,0 @@ 1.4 -#!/usr/bin/python 1.5 - 1.6 -import data.codes as table 1.7 - 1.8 -class MonomerType(object): 1.9 - """Class of monomer types. 1.10 - 1.11 - Each MonomerType object represents a known monomer type, e.g. Valine, 1.12 - and is referenced to by each instance of monomer in a given sequence. 1.13 - 1.14 - - `name`: full name of monomer type 1.15 - - `code1`: one-letter code 1.16 - - `code3`: three-letter code 1.17 - - `is_modified`: either of True or False 1.18 - 1.19 - class atributes: 1.20 - 1.21 - - `by_code1`: a mapping from one-letter code to MonomerType object 1.22 - - `by_code3`: a mapping from three-letter code to MonomerType object 1.23 - - `by_name`: a mapping from monomer name to MonomerType object 1.24 - - `instance_type`: class of Monomer objects to use when creating new 1.25 - objects; this must be redefined in descendent classes 1.26 - 1.27 - All of the class attributes MUST be redefined when subclassing. 1.28 - """ 1.29 - 1.30 - by_code1 = {} 1.31 - by_code3 = {} 1.32 - by_name = {} 1.33 - instance_type = None 1.34 - 1.35 - def __init__(self, name="", code1="", code3="", is_modified=False): 1.36 - self.name = name.capitalize() 1.37 - self.code1 = code1.upper() 1.38 - self.code3 = code3.upper() 1.39 - self.is_modified = bool(is_modified) 1.40 - if not is_modified: 1.41 - self.by_code1[self.code1] = self 1.42 - self.by_code3[code3] = self 1.43 - self.by_name[name] = self 1.44 - # We duplicate distinguished long names into MonomerType itself, 1.45 - # so that we can use MonomerType.from_code3 to create the relevant 1.46 - # type of monomer. 1.47 - MonomerType.by_code3[code3] = self 1.48 - MonomerType.by_name[name] = self 1.49 - 1.50 - @classmethod 1.51 - def _initialize(cls, type_letter, codes=table.codes): 1.52 - """Create all relevant instances of MonomerType. 1.53 - 1.54 - `type_letter` is either of: 1.55 - 1.56 - - 'p' for protein 1.57 - - 'd' for DNA 1.58 - - 'r' for RNA 1.59 - 1.60 - `codes` is a table of monomer codes 1.61 - """ 1.62 - for type, code1, is_modified, code3, name in codes: 1.63 - if type == type_letter: 1.64 - cls(name, code1, code3, is_modified) 1.65 - 1.66 - @classmethod 1.67 - def from_code1(cls, code1): 1.68 - """Return monomer type by one-letter code.""" 1.69 - return cls.by_code1[code1.upper()] 1.70 - 1.71 - @classmethod 1.72 - def from_code3(cls, code3): 1.73 - """Return monomer type by three-letter code.""" 1.74 - return cls.by_code3[code3.upper()] 1.75 - 1.76 - @classmethod 1.77 - def from_name(cls, name): 1.78 - """Return monomer type by name.""" 1.79 - return cls.by_name[name.capitalize()] 1.80 - 1.81 - def instance(self): 1.82 - """Create a new monomer of given type.""" 1.83 - return self.instance_type(self) 1.84 - 1.85 - def __eq__(self, other): 1.86 - if hasattr(other, "type"): 1.87 - return self is other.type 1.88 - return self is other 1.89 - 1.90 -class Monomer(object): 1.91 - """Monomer object. 1.92 - 1.93 - attributes: 1.94 - 1.95 - - `type`: type of monomer (a MonomerType object) 1.96 - 1.97 - class attribute `monomer_type` is MonomerType or either of it's subclasses, 1.98 - it is used when creating new monomers. It MUST be redefined when subclassing Monomer. 1.99 - """ 1.100 - monomer_type = MonomerType 1.101 - 1.102 - def __init__(self, type): 1.103 - self.type = type 1.104 - 1.105 - @classmethod 1.106 - def from_code1(cls, code1): 1.107 - return cls(cls.monomer_type.by_code1[code1.upper()]) 1.108 - 1.109 - @classmethod 1.110 - def from_code3(cls, code3): 1.111 - return cls(cls.monomer_type.by_code3[code3.upper()]) 1.112 - 1.113 - @classmethod 1.114 - def from_name(cls, name): 1.115 - return cls(cls.monomer_type.by_name[name.capitalize()]) 1.116 - 1.117 - def __eq__(self, other): 1.118 - if hasattr(other, "type"): 1.119 - return self.type is other.type 1.120 - return self.type is other 1.121 - 1.122 -# vim: set ts=4 sts=4 sw=4 et:
2.1 --- a/allpy/base.py Tue Dec 14 20:05:13 2010 +0300 2.2 +++ b/allpy/base.py Tue Dec 14 20:51:53 2010 +0300 2.3 @@ -1,5 +1,119 @@ 2.4 -import _monomer 2.5 from fasta import save_fasta 2.6 +import data.codes 2.7 + 2.8 +class MonomerType(object): 2.9 + """Class of monomer types. 2.10 + 2.11 + Each MonomerType object represents a known monomer type, e.g. Valine, 2.12 + and is referenced to by each instance of monomer in a given sequence. 2.13 + 2.14 + - `name`: full name of monomer type 2.15 + - `code1`: one-letter code 2.16 + - `code3`: three-letter code 2.17 + - `is_modified`: either of True or False 2.18 + 2.19 + class atributes: 2.20 + 2.21 + - `by_code1`: a mapping from one-letter code to MonomerType object 2.22 + - `by_code3`: a mapping from three-letter code to MonomerType object 2.23 + - `by_name`: a mapping from monomer name to MonomerType object 2.24 + - `instance_type`: class of Monomer objects to use when creating new 2.25 + objects; this must be redefined in descendent classes 2.26 + 2.27 + All of the class attributes MUST be redefined when subclassing. 2.28 + """ 2.29 + 2.30 + by_code1 = {} 2.31 + by_code3 = {} 2.32 + by_name = {} 2.33 + instance_type = None 2.34 + 2.35 + def __init__(self, name="", code1="", code3="", is_modified=False): 2.36 + self.name = name.capitalize() 2.37 + self.code1 = code1.upper() 2.38 + self.code3 = code3.upper() 2.39 + self.is_modified = bool(is_modified) 2.40 + if not is_modified: 2.41 + self.by_code1[self.code1] = self 2.42 + self.by_code3[code3] = self 2.43 + self.by_name[name] = self 2.44 + # We duplicate distinguished long names into MonomerType itself, 2.45 + # so that we can use MonomerType.from_code3 to create the relevant 2.46 + # type of monomer. 2.47 + MonomerType.by_code3[code3] = self 2.48 + MonomerType.by_name[name] = self 2.49 + 2.50 + @classmethod 2.51 + def _initialize(cls, type_letter, codes=data.codes.codes): 2.52 + """Create all relevant instances of MonomerType. 2.53 + 2.54 + `type_letter` is either of: 2.55 + 2.56 + - 'p' for protein 2.57 + - 'd' for DNA 2.58 + - 'r' for RNA 2.59 + 2.60 + `codes` is a table of monomer codes 2.61 + """ 2.62 + for type, code1, is_modified, code3, name in codes: 2.63 + if type == type_letter: 2.64 + cls(name, code1, code3, is_modified) 2.65 + 2.66 + @classmethod 2.67 + def from_code1(cls, code1): 2.68 + """Return monomer type by one-letter code.""" 2.69 + return cls.by_code1[code1.upper()] 2.70 + 2.71 + @classmethod 2.72 + def from_code3(cls, code3): 2.73 + """Return monomer type by three-letter code.""" 2.74 + return cls.by_code3[code3.upper()] 2.75 + 2.76 + @classmethod 2.77 + def from_name(cls, name): 2.78 + """Return monomer type by name.""" 2.79 + return cls.by_name[name.capitalize()] 2.80 + 2.81 + def instance(self): 2.82 + """Create a new monomer of given type.""" 2.83 + return self.instance_type(self) 2.84 + 2.85 + def __eq__(self, other): 2.86 + if hasattr(other, "type"): 2.87 + return self is other.type 2.88 + return self is other 2.89 + 2.90 +class Monomer(object): 2.91 + """Monomer object. 2.92 + 2.93 + attributes: 2.94 + 2.95 + - `type`: type of monomer (a MonomerType object) 2.96 + 2.97 + class attribute `monomer_type` is MonomerType or either of it's subclasses, 2.98 + it is used when creating new monomers. It MUST be redefined when subclassing Monomer. 2.99 + """ 2.100 + monomer_type = MonomerType 2.101 + 2.102 + def __init__(self, type): 2.103 + self.type = type 2.104 + 2.105 + @classmethod 2.106 + def from_code1(cls, code1): 2.107 + return cls(cls.monomer_type.by_code1[code1.upper()]) 2.108 + 2.109 + @classmethod 2.110 + def from_code3(cls, code3): 2.111 + return cls(cls.monomer_type.by_code3[code3.upper()]) 2.112 + 2.113 + @classmethod 2.114 + def from_name(cls, name): 2.115 + return cls(cls.monomer_type.by_name[name.capitalize()]) 2.116 + 2.117 + def __eq__(self, other): 2.118 + if hasattr(other, "type"): 2.119 + return self.type is other.type 2.120 + return self.type is other 2.121 2.122 class Sequence(list): 2.123 """ Sequence of Monomers 2.124 @@ -12,9 +126,6 @@ 2.125 """ 2.126 pass 2.127 2.128 -MonomerType = _monomer.MonomerType 2.129 -Monomer = _monomer.Monomer 2.130 - 2.131 class Alignment(dict): 2.132 """ Alignment 2.133 2.134 @@ -230,8 +341,4 @@ 2.135 """ """ 2.136 pass 2.137 2.138 - 2.139 - 2.140 - 2.141 - 2.142 - 2.143 +# vim: set ts=4 sts=4 sw=4 et: