Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/raw-rev/364232e42888
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 07:47:45 2012
Кодировка:

# HG changeset patch
# User Daniil Alexeyevsky
# Date 1298917061 -10800
# Node ID 364232e428887b5b7ec2f697c3aaa83c434449d3
# Parent 1a8ddc6e2eeee82dece4972d930942c353617f3b
Reimplemented flush(direction) as processor

diff -r 1a8ddc6e2eee -r 364232e42888 allpy/base.py
--- a/allpy/base.py Mon Feb 28 19:24:28 2011 +0300
+++ b/allpy/base.py Mon Feb 28 21:17:41 2011 +0300
@@ -180,6 +180,15 @@
self.sequences.append(sequence)
return self

+ def append_row_with_gaps(self, row, sequence):
+ """Add row from row_as_list representation and sequence. Return self."""
+ self.sequences.append(sequence)
+ self._pad_to_width(len(row))
+ for column, monomer in zip(self.columns, row):
+ if monomer:
+ column[sequence] = monomer
+ return self
+
def _pad_to_width(self, n):
"""Pad alignment with empty columns on the right to width n."""
for i in range(len(self.columns), n):
@@ -285,35 +294,20 @@
# Alignment / Block editing methods
# =================================

- def _flush_row(self, row, whence='left'):
- """Helper for `flush`: flush to one side all monomers in one row."""
- row = filter(None, row)
- padding = [None] * len(self.columns)
- if whence == 'left':
- return row + padding
- if whence == 'right':
- return padding + row
- if whence == 'center':
- pad_len = (len(self.columns) - len(row)) // 2
- # vvv fix padding for case when length is odd: better have more
- pad_len += len(self.columns) - 2 * pad_len
- padding = [None] * pad_len
- return padding + row + padding
- assert True, "whence must be either 'left' or 'right' or 'center'"
-
def flush(self, whence='left'):
"""Remove all gaps from alignment and flush results to one side.

`whence` must be one of 'left', 'right' or 'center'
"""
- for row in self.rows_as_lists():
- sequence = row.sequence
- row = self._flush_row(row, whence)
- for monomer, column in zip(row, self.columns):
- if monomer:
- column[sequence] = monomer
- elif sequence in column:
- del column[sequence]
+ if whence == 'left':
+ from processors import Left as Flush
+ elif whence == 'right':
+ from processors import Right as Flush
+ elif whence == 'center':
+ from processors import Center as Flush
+ else:
+ raise AssertionError, "Whence must be left, right or center"
+ self.realign(Flush())

def remove_gap_columns(self):
"""Remove all empty columns."""
@@ -391,6 +385,17 @@
copy_contents = function.copy_contents
self._replace_contents(new, copy_descriptions, copy_contents)

+ def realign(self, function):
+ """Realign self.
+
+ I.e.: apply function to self to produce a new alignment, then update
+ self to have the same gap patterns as the new alignment.
+
+ This is the same as process(function, False, False)
+ """
+ new = function(self)
+ self._replace_column_contents(new)
+
class Column(dict):
"""Column of alignment.

diff -r 1a8ddc6e2eee -r 364232e42888 allpy/processors.py
--- a/allpy/processors.py Mon Feb 28 19:24:28 2011 +0300
+++ b/allpy/processors.py Mon Feb 28 21:17:41 2011 +0300
@@ -67,4 +67,31 @@
new_block = ExternalCommand.__call__(self, block)
return fix_ordering(block, fix_missing(block, new_block))

+class Flush(object):
+ """Flush all non-gap monomers to one side."""
+ def __call__(self, alignment):
+ result = alignment.types.Alignment()
+ for row in alignment.rows_as_lists():
+ sequence = row.sequence
+ no_gaps = filter(None, row)
+ llen, rlen = self._padding_sizes(len(row) - len(no_gaps))
+ row = [None] * llen + no_gaps + [None] * rlen
+ result.append_row_with_gaps(row, sequence)
+ return result
+
+class Left(Flush):
+ """Flush all non-gap monomers to the left."""
+ def _padding_sizes(self, n_gaps):
+ return 0, n_gaps
+
+class Right(Flush):
+ """Flush all non-gap monomers to the right."""
+ def _padding_sizes(self, n_gaps):
+ return n_gaps, 0
+
+class Center(Flush):
+ """Center all non-gap monomers in alignment."""
+ def _padding_sizes(self, n_gaps):
+ return n_gaps // 2, (n_gaps + 1) // 2
+
# vim: set et ts=4 sts=4 sw=4:
diff -r 1a8ddc6e2eee -r 364232e42888 test/test_base.py
--- a/test/test_base.py Mon Feb 28 19:24:28 2011 +0300
+++ b/test/test_base.py Mon Feb 28 21:17:41 2011 +0300
@@ -1,5 +1,6 @@
import allpy.base as b
import allpy.protein as p
+from allpy import processors
from StringIO import StringIO

def test_new_monomers():
@@ -42,21 +43,21 @@
"-------------RST",
)

- a.flush("left")
+ a.process(processors.Left())
assert_alignment(a,
"ACDEF-----------",
"GHIKLMPQ--------",
"RST-------------",
)

- a.flush("center")
+ a.process(processors.Center())
assert_alignment(a,
"-----ACDEF------",
"----GHIKLMPQ----",
"------RST-------",
)

- a.flush("right")
+ a.process(processors.Right())
assert_alignment(a,
"-----------ACDEF",
"--------GHIKLMPQ",