allpy
changeset 382:df571a5233fb
Changed the way related types are stored for Alignment, Block, Sequence, Monomer
Now each of them has attribute 'type', which is an object with four attributes:
- Alignment
- Block
- Sequence
- Monomer
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 01 Feb 2011 23:08:08 +0300 |
parents | d66d470a9c0a |
children | bed32775625a 02c4cfd12d4c |
files | allpy/base.py allpy/dna.py allpy/protein.py allpy/rna.py |
diffstat | 4 files changed, 32 insertions(+), 41 deletions(-) [+] |
line diff
1.1 --- a/allpy/base.py Tue Feb 01 18:46:23 2011 +0300 1.2 +++ b/allpy/base.py Tue Feb 01 23:08:08 2011 +0300 1.3 @@ -4,6 +4,9 @@ 1.4 import util 1.5 import fasta 1.6 1.7 +# import this very module as means of having all related classes in one place 1.8 +import base 1.9 + 1.10 default_gaps = set((".", "-", "~")) 1.11 """Set of characters to recoginze as gaps when parsing alignment.""" 1.12 1.13 @@ -13,6 +16,9 @@ 1.14 type = None 1.15 """Either of 'dna', 'rna', 'protein'.""" 1.16 1.17 + types = base 1.18 + """Mapping of related types. SHOULD be redefined in subclasses.""" 1.19 + 1.20 by_code1 = {} 1.21 """A mapping from 1-letter code to Monomer subclass.""" 1.22 1.23 @@ -90,14 +96,10 @@ 1.24 * source -- str denoting source of the sequence 1.25 1.26 Any of them may be empty (i.e. hold empty string) 1.27 - 1.28 - Class attributes: 1.29 - 1.30 - * monomer_type -- type of monomers in sequence, must be redefined when 1.31 - subclassing 1.32 """ 1.33 1.34 - monomer_type = Monomer 1.35 + types = base 1.36 + """Mapping of related types. SHOULD be redefined in subclasses.""" 1.37 1.38 name = '' 1.39 description = '' 1.40 @@ -118,7 +120,7 @@ 1.41 @classmethod 1.42 def from_string(cls, string, name='', description='', source=''): 1.43 """Create sequences from string of one-letter codes.""" 1.44 - monomer = cls.monomer_type.from_code1 1.45 + monomer = cls.types.Monomer.from_code1 1.46 monomers = [monomer(letter) for letter in string] 1.47 return cls.from_monomers(monomers, name, description, source) 1.48 1.49 @@ -136,16 +138,8 @@ 1.50 class Alignment(object): 1.51 """Alignment. It is a list of Columns.""" 1.52 1.53 - alignment_type = None 1.54 - """Related Alignment class. SHOULD be redefined when subclassing.""" 1.55 - # Actually given value at EOF, since name Alignment is not yet defined 1.56 - 1.57 - block_type = None 1.58 - """Related Block class. SHOULD be redefined when subclassing.""" 1.59 - # Actually given value at EOF, since name Block is not yet defined 1.60 - 1.61 - sequence_type = Sequence 1.62 - """Type of sequences in alignment. SHOULD be redefined when subclassing.""" 1.63 + types = base 1.64 + """Mapping of related types. SHOULD be redefined in subclasses.""" 1.65 1.66 sequences = None 1.67 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!""" 1.68 @@ -171,7 +165,7 @@ 1.69 def append_row_from_string(self, string, 1.70 name='', description='', source='', gaps=default_gaps): 1.71 """Add row from a string of one-letter codes and gaps. Return self.""" 1.72 - Sequence = self.sequence_type 1.73 + Sequence = self.types.Sequence 1.74 not_gap = lambda (i, char): char not in gaps 1.75 without_gaps = util.remove_each(string, gaps) 1.76 sequence = Sequence.from_string(without_gaps, name, description, source) 1.77 @@ -392,6 +386,9 @@ 1.78 the column. 1.79 """ 1.80 1.81 + types = base 1.82 + """Mapping of related types. SHOULD be redefined in subclasses.""" 1.83 + 1.84 def __hash__(self): 1.85 """Return hash by identity.""" 1.86 return id(self) 1.87 @@ -434,7 +431,4 @@ 1.88 block.columns = columns 1.89 return block 1.90 1.91 -Alignment.alignment_type = Alignment 1.92 -Alignment.block_type = Block 1.93 - 1.94 # vim: set ts=4 sts=4 sw=4 et:
2.1 --- a/allpy/dna.py Tue Feb 01 18:46:23 2011 +0300 2.2 +++ b/allpy/dna.py Tue Feb 01 23:08:08 2011 +0300 2.3 @@ -1,25 +1,24 @@ 2.4 import base 2.5 import data.codes 2.6 2.7 +import dna 2.8 + 2.9 class Monomer(base.Monomer): 2.10 """DNA monomers: nucleotides.""" 2.11 type = 'dna' 2.12 + types = dna 2.13 by_code1 = {} 2.14 by_code3 = {} 2.15 by_name = {} 2.16 Monomer._initialize(data.codes.dna) 2.17 2.18 class Sequence(base.Sequence): 2.19 - monomer_type = Monomer 2.20 + types = dna 2.21 + 2.22 +class Alignment(base.Alignment): 2.23 + types = dna 2.24 2.25 class Block(Alignment, base.Block): 2.26 pass 2.27 2.28 -class Alignment(base.Alignment): 2.29 - sequence_type = Sequence 2.30 - block_type = Block 2.31 - # alignment_type is defined at EOF, since name Alignment undefined yet 2.32 - 2.33 -Alignment.alignment_type = Alignment 2.34 - 2.35 # vim: set ts=4 sts=4 sw=4 et:
3.1 --- a/allpy/protein.py Tue Feb 01 18:46:23 2011 +0300 3.2 +++ b/allpy/protein.py Tue Feb 01 23:08:08 2011 +0300 3.3 @@ -1,9 +1,12 @@ 3.4 import base 3.5 import data.codes 3.6 3.7 +import protein 3.8 + 3.9 class Monomer(base.Monomer): 3.10 """Protein monomers: aminoacids.""" 3.11 type = 'protein' 3.12 + types = protein 3.13 by_code1 = {} 3.14 by_code3 = {} 3.15 by_name = {} 3.16 @@ -14,15 +17,13 @@ 3.17 pass 3.18 3.19 class Sequence(base.Sequence): 3.20 - monomer_type = Monomer 3.21 + types = protein 3.22 3.23 class Block(Alignment, base.Block): 3.24 pass 3.25 3.26 class Alignment(base.Alignment): 3.27 - sequence_type = Sequence 3.28 - block_type = Block 3.29 - # alignment_type is defined at EOF, since name Alignment undefined yet 3.30 + types = protein 3.31 3.32 def muscle_align(self): 3.33 """ Simple align ths alignment using sequences (muscle) 3.34 @@ -52,6 +53,4 @@ 3.35 self.body[sequence].append(old_monomer) 3.36 os.unlink(tmp_file.name) 3.37 3.38 -Alignment.alignment_type = Alignment 3.39 - 3.40 # vim: set ts=4 sts=4 sw=4 et:
4.1 --- a/allpy/rna.py Tue Feb 01 18:46:23 2011 +0300 4.2 +++ b/allpy/rna.py Tue Feb 01 23:08:08 2011 +0300 4.3 @@ -1,25 +1,24 @@ 4.4 import base 4.5 import data.codes 4.6 4.7 +import rna 4.8 + 4.9 class Monomer(base.Monomer): 4.10 """RNA monomers: nucleotides.""" 4.11 type = 'rna' 4.12 + types = rna 4.13 by_code1 = {} 4.14 by_code3 = {} 4.15 by_name = {} 4.16 Monomer._initialize(data.codes.rna) 4.17 4.18 class Sequence(base.Sequence): 4.19 - monomer_type = Monomer 4.20 + types = rna 4.21 4.22 class Block(Alignment, base.Block): 4.23 pass 4.24 4.25 class Alignment(base.Alignment): 4.26 - sequence_type = Sequence 4.27 - block_type = Block 4.28 - # alignment_type is defined at EOF, since name Alignment undefined yet 4.29 - 4.30 -Alignment.alignment_type = Alignment 4.31 + types = rna 4.32 4.33 # vim: set ts=4 sts=4 sw=4 et: