allpy
changeset 368:57894fcb7792
Block.flush_left() -> Alignment.flush("left"); implemented flushing left, right and center. (see #25)
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Wed, 26 Jan 2011 21:17:01 +0300 |
parents | 9093525fa79d |
children | 80efdebb9fd6 |
files | allpy/base.py |
diffstat | 1 files changed, 33 insertions(+), 13 deletions(-) [+] |
line diff
1.1 --- a/allpy/base.py Wed Jan 26 21:03:23 2011 +0300 1.2 +++ b/allpy/base.py Wed Jan 26 21:17:01 2011 +0300 1.3 @@ -272,6 +272,39 @@ 1.4 columns.append(col) 1.5 return columns 1.6 1.7 + # Alignment / Block editing methods 1.8 + # ================================= 1.9 + 1.10 + def _flush_row(self, row, whence='left'): 1.11 + """Helper for `flush`: flush to one side all monomers in one row.""" 1.12 + row = filter(None, row) 1.13 + padding = [None] * len(self.columns) 1.14 + if whence == 'left': 1.15 + return row + padding 1.16 + if whence == 'right': 1.17 + return padding + row 1.18 + if whence == 'center': 1.19 + pad_len = (len(self.columns) - len(row)) // 2 1.20 + # vvv fix padding for case when length is odd: better have more 1.21 + pad_len += len(self.columns) - 2 * pad_len 1.22 + padding = [None] * pad_len 1.23 + return padding + row + padding 1.24 + assert True, "whence must be either 'left' or 'right' or 'center'" 1.25 + 1.26 + def flush(self, whence='left'): 1.27 + """Remove all gaps from alignment and flush results to one side. 1.28 + 1.29 + `whence` must be one of 'left', 'right' or 'center' 1.30 + """ 1.31 + for row in self.rows_as_lists(): 1.32 + sequence = row.sequence 1.33 + row = self._flush_row(row, whence) 1.34 + for monomer, column in zip(row, self.columns): 1.35 + if monomer: 1.36 + column[sequence] = monomer 1.37 + elif sequence in column: 1.38 + del column[sequence] 1.39 + 1.40 class Column(dict): 1.41 """Column of alignment. 1.42 1.43 @@ -323,17 +356,4 @@ 1.44 block.columns = columns 1.45 return block 1.46 1.47 - def flush_left(self): 1.48 - """Move all monomers to the left, gaps to the right within block.""" 1.49 - padding = [None] * len(self.columns) 1.50 - for row in self.rows_as_lists(): 1.51 - sequence = row.sequence 1.52 - row = filter(None, row) + padding 1.53 - for monomer, column in zip(row, self.columns): 1.54 - if monomer: 1.55 - column[sequence] = monomer 1.56 - elif sequence in column: 1.57 - del column[sequence] 1.58 - 1.59 - 1.60 # vim: set ts=4 sts=4 sw=4 et: