Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/rev/41a167bbf150
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 00:38:08 2012
Кодировка:
allpy: 41a167bbf150

allpy

changeset 1101:41a167bbf150

Fixed a serious bug with saving sequence markups when alignment does not have all of the sequence monomers
author Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru>
date Sat, 09 Jun 2012 21:03:13 +0400
parents c4c772e3ce86
children 850e7699addc
files allpy/base.py allpy/fileio/markup.py allpy/markups.py test/test_markups.py
diffstat 4 files changed, 25 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/allpy/base.py	Sat Jun 09 19:15:44 2012 +0400
     1.2 +++ b/allpy/base.py	Sat Jun 09 21:03:13 2012 +0400
     1.3 @@ -605,10 +605,12 @@
     1.4          """
     1.5          return container.add_markup(name, markup_class=cls)
     1.6  
     1.7 -    def to_record(self):
     1.8 +    def to_record(self, keys=None):
     1.9          """Save markup to `record`, for saving to file.
    1.10  
    1.11          For description of `record` see docstring for `from_record` method.
    1.12 +
    1.13 +        If `keys` argument is given, restrict output to the given keys.
    1.14          """
    1.15          return {}
    1.16  
     2.1 --- a/allpy/fileio/markup.py	Sat Jun 09 19:15:44 2012 +0400
     2.2 +++ b/allpy/fileio/markup.py	Sat Jun 09 21:03:13 2012 +0400
     2.3 @@ -51,23 +51,24 @@
     2.4      def write_alignment(self, alignment):
     2.5          """Write alignment to file."""
     2.6          self.write_markups(alignment.markups, 'alignment_markup')
     2.7 -        for sequence in alignment.sequences:
     2.8 +        for row in alignment.rows_as_lists():
     2.9              record = {
    2.10 -                'sequence_name': sequence.name,
    2.11 -                'sequence_description': sequence.description,
    2.12 +                'sequence_name': row.sequence.name,
    2.13 +                'sequence_description': row.sequence.description,
    2.14              }
    2.15 -            self.write_markups(sequence.markups, 'sequence_markup', record)
    2.16 +            self.write_markups(row.sequence.markups, 'sequence_markup',
    2.17 +                filter(None, row), record)
    2.18          record = {'type': 'alignment', 'format': self.format}
    2.19          self.write_record(record)
    2.20          self.write_empty_line()
    2.21          alignment.to_file(self.file, format=self.format, gap=self.gaps)
    2.22  
    2.23 -    def write_markups(self, markups, type, pre_record={}):
    2.24 +    def write_markups(self, markups, type, keys=None, pre_record={}):
    2.25          """Write a dictionary of markups as series of records."""
    2.26          for name, markup in markups.items():
    2.27              if not markup.save:
    2.28                  continue
    2.29 -            record = markup.to_record()
    2.30 +            record = markup.to_record(keys)
    2.31              record.update(pre_record)
    2.32              record['type'] = type
    2.33              record['name'] = name
     3.1 --- a/allpy/markups.py	Sat Jun 09 19:15:44 2012 +0400
     3.2 +++ b/allpy/markups.py	Sat Jun 09 21:03:13 2012 +0400
     3.3 @@ -63,10 +63,10 @@
     3.4                  result[key] = result.parse_item(key, value)
     3.5          return result
     3.6  
     3.7 -    def to_record(self):
     3.8 +    def to_record(self, keys=None):
     3.9          """Write markup to semi-serialized record for 'markup' file."""
    3.10          values = []
    3.11 -        for key in self.sorted_keys():
    3.12 +        for key in keys or self.sorted_keys():
    3.13              if key in self:
    3.14                  values.append(self.repr_item(key, self[key]))
    3.15              else:
     4.1 --- a/test/test_markups.py	Sat Jun 09 19:15:44 2012 +0400
     4.2 +++ b/test/test_markups.py	Sat Jun 09 21:03:13 2012 +0400
     4.3 @@ -10,6 +10,19 @@
     4.4      assert seq[5].index == 5
     4.5      assert markup[seq[5]] == 5
     4.6  
     4.7 +def test_hardio():
     4.8 +    aln = protein.Alignment().append_row_from_string("SEQVENCE", name="A")
     4.9 +    markup = aln.sequences[0].add_markup('index')
    4.10 +    del aln.columns[2]
    4.11 +
    4.12 +    file = StringIO()
    4.13 +    aln.to_file(file, format="markup")
    4.14 +
    4.15 +    file.seek(0)
    4.16 +    aln = protein.Alignment().append_file(file, format="markup")
    4.17 +    assert str(aln.sequences[0]) == "SEVENCE"
    4.18 +    assert aln.sequences[0][3].index == 3
    4.19 +
    4.20  def test_number():
    4.21      seq = protein.Sequence.from_string('SEQVENCE', name='sequence')
    4.22      seq.add_markup('number')