allpy
diff allpy/base.py @ 299:21c2358cb5e7
Implemented allpy.base.Alignment row/column model: methods rows(), *_as_lists()
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Thu, 16 Dec 2010 18:51:05 +0300 |
parents | c305a80cdb75 |
children | 5d355141f37d |
line diff
1.1 --- a/allpy/base.py Thu Dec 16 18:35:56 2010 +0300 1.2 +++ b/allpy/base.py Thu Dec 16 18:51:05 2010 +0300 1.3 @@ -199,6 +199,9 @@ 1.4 self.sequences = [] 1.5 self.columns = [] 1.6 1.7 + # Alignment modification methods 1.8 + # ============================== 1.9 + 1.10 def append_sequence(self, sequence): 1.11 """Add sequence to alignment. 1.12 1.13 @@ -218,6 +221,9 @@ 1.14 self.columns[j][seq] = sequence[i] 1.15 self.sequences.append(sequence) 1.16 1.17 + # Alignment IO methods 1.18 + # ==================== 1.19 + 1.20 @classmethod 1.21 def from_fasta(cls, file): 1.22 """Create new alignment from FASTA file.""" 1.23 @@ -237,6 +243,69 @@ 1.24 line = "".join(map(char, row)) 1.25 fasta.save_file(file, line, seq.name, seq.description) 1.26 1.27 + # Data access methods for alignment 1.28 + # ================================= 1.29 + 1.30 + def rows(self): 1.31 + """Return list of rows (temporary objects) in alignment. 1.32 + 1.33 + Each row is a dictionary of { column : monomer }. 1.34 + 1.35 + For gap positions there is no key for the column in row. 1.36 + 1.37 + Each row has attribute `sequence` pointing to the sequence the row is 1.38 + describing. 1.39 + 1.40 + Modifications of row have no effect on the alignment. 1.41 + """ 1.42 + # For now, the function returns a list rather than iterator. 1.43 + # It is yet to see, whether memory performance here becomes critical, 1.44 + # or is random access useful. 1.45 + rows = [] 1.46 + for sequence in self.sequences: 1.47 + row = util.UserDict() 1.48 + row.sequence = sequence 1.49 + for column in self.columns: 1.50 + if sequence in column: 1.51 + row[column] = column[sequence] 1.52 + rows.append(row) 1.53 + return rows 1.54 + 1.55 + def rows_as_lists(self): 1.56 + """Return list of rows (temporary objects) in alignment. 1.57 + 1.58 + Each row here is a list of either monomer or None (for gaps). 1.59 + 1.60 + Each row has attribute `sequence` pointing to the sequence of row. 1.61 + 1.62 + Modifications of row have no effect on the alignment. 1.63 + """ 1.64 + rows = [] 1.65 + for sequence in self.sequences: 1.66 + row = util.UserList() 1.67 + row.sequence = sequence 1.68 + for column in self.columns: 1.69 + row.append(column.get(sequence)) 1.70 + rows.append(row) 1.71 + return rows 1.72 + 1.73 + def columns_as_lists(self): 1.74 + """Return list of columns (temorary objects) in alignment. 1.75 + 1.76 + Each column here is a list of either monomer or None (for gaps). 1.77 + 1.78 + Items of column are sorted in the same way as alignment.sequences. 1.79 + 1.80 + Modifications of column have no effect on the alignment. 1.81 + """ 1.82 + columns = [] 1.83 + for column in self.columns: 1.84 + col = [] 1.85 + for sequence in self.sequences: 1.86 + col.append(column.get(sequence)) 1.87 + columns.append(col) 1.88 + return columns 1.89 + 1.90 ## Unclean code follows 1.91 1.92 def length(self):