allpy
diff allpy/base.py @ 372:670ae384aa37
Fixed bugs & refactored base.Alignment.process & it's helpers
- there were a few name errors
- process would previously behave bad if new alignment was shorter
- refactored and simplified code for copying sequence contents
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Mon, 31 Jan 2011 18:44:23 +0300 |
parents | 9847bd92b889 |
children | 166806efc570 |
line diff
1.1 --- a/allpy/base.py Wed Jan 26 22:25:13 2011 +0300 1.2 +++ b/allpy/base.py Mon Jan 31 18:44:23 2011 +0300 1.3 @@ -317,36 +317,33 @@ 1.4 for sequence in column: 1.5 del column[sequence] 1.6 1.7 - def _append_row_from_list(self, new): 1.8 - sequence = new.sequence 1.9 - self.sequences.append(sequence) 1.10 - for i, monomer in enumerate(new): 1.11 - if monomer: 1.12 - self._column_at(i)[sequence] = monomer 1.13 + def _merge(self, dst, new, merge): 1.14 + """Replace contents of dst with those of new. 1.15 + 1.16 + Replace contents of elements using function `merge(dst_el, new_le)`. 1.17 + """ 1.18 + for el, new_el in zip(dst, new): 1.19 + merge(el, new_el) 1.20 + dst[len(dst):] = new[len(dst):] 1.21 + del dst[len(new):] 1.22 1.23 def _replace_sequence_contents(self, new): 1.24 """Replace contents of sequences with those of new alignment.""" 1.25 # XXX: we manually copy sequence contents here 1.26 - # XXX: we do not copy all sequence contents, only overlapping parts 1.27 - # XXX: if sequence shortens, we leave some monomers unmodified (!!!) 1.28 - for sequence, new_sequence in zip(self.sequences, new.sequences): 1.29 - sequence.name = new_sequence.name 1.30 - sequence.description = new_sequence.desription 1.31 - sequence.source = new_sequence.source 1.32 - for monomer, new_monomer in zip(sequence, new_sequence): 1.33 - monomer.__class__ = new_monomer.__class__ 1.34 - for i in range(len(sequence), len(new_sequence)): 1.35 - sequence.append(new_sequence[i]) 1.36 - for i in range(len(self.sequences), len(new.sequences)): 1.37 - self._append_row_from_list(new.rows_as_lists()[i]) 1.38 + # XXX: we only copy, overlapping parts and link to the rest 1.39 + def merge_monomers(dst, new): 1.40 + dst.__class__ = new.__class__ 1.41 + def merge_sequences(dst, new): 1.42 + vars(dst).update(vars(new)) 1.43 + self._merge(dst, new, merge_monomers) 1.44 + self._merge(self.sequences, new.sequences, merge_sequences) 1.45 1.46 def _replace_column_contents(self, new): 1.47 """Replace column contents with those of new alignment. 1.48 1.49 - This requires that self.sequences and new.sequences have same length 1.50 - and same contents. (If sequences have same length but different 1.51 - contents, this effectively copies gap patterns. If sequences have 1.52 - different length, the results are meaningless and dumb). 1.53 + Synonym: copy gap patterns from new to self. 1.54 + 1.55 + self.sequences and new.sequences should have the same contents. 1.56 """ 1.57 self._wipe() 1.58 not_gap = lambda (a,b): a != None 1.59 @@ -367,7 +364,7 @@ 1.60 function(block) must return block with same line order. 1.61 """ 1.62 new = function(self) 1.63 - self.replace_contents(new) 1.64 + self._replace_contents(new) 1.65 1.66 class Column(dict): 1.67 """Column of alignment.