def __add__(self, other): """Combine two codonalignments with the same number of rows by adding them. The method also allows to combine a CodonAlignment object with a MultipleSeqAlignment object. The following rules apply: * CodonAlignment + CodonAlignment -> CodonAlignment * CodonAlignment + MultipleSeqAlignment -> MultipleSeqAlignment """ if isinstance(other, CodonAlignment): if len(self) != len(other): raise ValueError("When adding two alignments they must have the same length" " (i.e. same number or rows)") if compare_codon_alphabet(self._alphabet, other._alphabet): alpha = self._alphabet # merged = (left + right for left, right in zip(self, other)) merged = (SeqRecord(seq=CodonSeq(str(left.seq) + str(right.seq), alphabet=left.seq.alphabet)) for left, right in zip(self, other)) return CodonAlignment(merged, alphabet=alpha) else: raise TypeError("Only CodonAlignment with the same CodonAlphabet can be " "combined.") elif isinstance(other, MultipleSeqAlignment): if len(self) != len(other): raise ValueError("When adding two alignments they must have the same length" " (i.e. same number or rows)") return self.toMultipleSeqAlignment() + other else: raise TypeError("Only CodonAlignment or MultipleSeqAlignment object can be " "added with a CodonAlignment object. " "{} detected.".format(object(other)))
def __add__(self, other): """Combine two codonalignments with the same number of rows by adding them. The method also allows to combine a CodonAlignment object with a MultipleSeqAlignment object. The following rules apply: * CodonAlignment + CodonAlignment -> CodonAlignment * CodonAlignment + MultipleSeqAlignment -> MultipleSeqAlignment """ if isinstance(other, CodonAlignment): if len(self) != len(other): raise ValueError( "When adding two alignments they must have the same length" " (i.e. same number or rows)") if compare_codon_alphabet(self._alphabet, other._alphabet): alpha = self._alphabet # merged = (left + right for left, right in zip(self, other)) merged = (SeqRecord(seq=CodonSeq(str(left.seq) + str(right.seq), alphabet=left.seq.alphabet)) for left, right in zip(self, other)) return CodonAlignment(merged, alphabet=alpha) else: raise TypeError( "Only CodonAlignment with the same CodonAlphabet can be combined." ) elif isinstance(other, MultipleSeqAlignment): if len(self) != len(other): raise ValueError( "When adding two alignments they must have the same length" " (i.e. same number or rows)") return self.toMultipleSeqAlignment() + other else: raise TypeError( "Only CodonAlignment or MultipleSeqAlignment object can be" f" added with a CodonAlignment object. {object(other)} detected." )