allpy
changeset 933:f3358da68bcd
Fixed two bugs in one place: base Alignment/Block
- realign used to cut sequence tails if the new alignment was wider than the
existing one
- any block modification that added new columns used to forget to add the
columns to the alignment
AFAIR, currently the only such modifications are IO, append_*, and realign()
To fix it I've:
- changed realign to use _pad_to_width() same as IO used,
- and changed _pad_to_width() to behave in a blocks-safe manner: it now calls
self._append_columns(), which in case of blocks calls itself recursively to
add columns to parent too.
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 22 Nov 2011 00:13:40 +0300 |
parents | 61f28f17f027 |
children | 763eb1b43534 |
files | allpy/base.py |
diffstat | 1 files changed, 16 insertions(+), 2 deletions(-) [+] |
line diff
1.1 --- a/allpy/base.py Mon Nov 21 19:16:25 2011 +0300 1.2 +++ b/allpy/base.py Tue Nov 22 00:13:40 2011 +0300 1.3 @@ -259,10 +259,14 @@ 1.4 column[sequence] = monomer 1.5 return self 1.6 1.7 + def _append_columns(self, n, columns): 1.8 + """Insert list of `columns` after position `n`.""" 1.9 + self.columns[n+1:n+1] = columns 1.10 + 1.11 def _pad_to_width(self, n): 1.12 """Pad alignment with empty columns on the right to width n.""" 1.13 - for i in range(len(self.columns), n): 1.14 - self.columns.append(Column()) 1.15 + columns = [Column() for _ in range(len(self.columns), n)] 1.16 + self._append_columns(len(self.columns)-1, columns) 1.17 1.18 def append_file(self, file, format='fasta', gaps=default_gaps): 1.19 """Append sequences from file to alignment. Return self. 1.20 @@ -445,6 +449,7 @@ 1.21 monomers = filter(None, row) 1.22 assert len(monomers) == len(filter(None, new_row)) 1.23 self._wipe_row(sequence) 1.24 + self._pad_to_width(len(new_row)) 1.25 non_gap_columns = [column 1.26 for column, monomer in zip(self.columns, new_row) 1.27 if monomer 1.28 @@ -507,6 +512,15 @@ 1.29 """Return hash by identity.""" 1.30 return id(self) 1.31 1.32 + def _append_columns(self, n, columns): 1.33 + """Insert list of `columns` before position `n`.""" 1.34 + target = self.columns[n] 1.35 + for k, column in self.alignment.columns: 1.36 + if column is target: 1.37 + me = k 1.38 + self.alignment._append_columns(me, columns) 1.39 + self.columns[n+1:n+1] = columns 1.40 + 1.41 class Block(Alignment): 1.42 """Block of alignment. 1.43