def __add__(self, other): """Combines to alignments with the same number of rows by adding them. If you have two multiple sequence alignments (MSAs), there are two ways to think about adding them - by row or by column. Using the extend method adds by row. Using the addition operator adds by column. For example, >>> from SAP.Bio.Alphabet import generic_dna >>> from SAP.Bio.Seq import Seq >>> from SAP.Bio.SeqRecord import SeqRecord >>> from SAP.Bio.Align import MultipleSeqAlignment >>> a1 = SeqRecord(Seq("AAAAC", generic_dna), id="Alpha") >>> b1 = SeqRecord(Seq("AAA-C", generic_dna), id="Beta") >>> c1 = SeqRecord(Seq("AAAAG", generic_dna), id="Gamma") >>> a2 = SeqRecord(Seq("GT", generic_dna), id="Alpha") >>> b2 = SeqRecord(Seq("GT", generic_dna), id="Beta") >>> c2 = SeqRecord(Seq("GT", generic_dna), id="Gamma") >>> left = MultipleSeqAlignment([a1, b1, c1], ... annotations={"tool": "demo", "name": "start"}) >>> right = MultipleSeqAlignment([a2, b2, c2], ... annotations={"tool": "demo", "name": "end"}) Now, let's look at these two alignments: >>> print(left) DNAAlphabet() alignment with 3 rows and 5 columns AAAAC Alpha AAA-C Beta AAAAG Gamma >>> print(right) DNAAlphabet() alignment with 3 rows and 2 columns GT Alpha GT Beta GT Gamma And add them: >>> combined = left + right >>> print(combined) DNAAlphabet() alignment with 3 rows and 7 columns AAAACGT Alpha AAA-CGT Beta AAAAGGT Gamma For this to work, both alignments must have the same number of records (here they both have 3 rows): >>> len(left) 3 >>> len(right) 3 >>> len(combined) 3 The individual rows are SeqRecord objects, and these can be added together. Refer to the SeqRecord documentation for details of how the annotation is handled. This example is a special case in that both original alignments shared the same names, meaning when the rows are added they also get the same name. Any common annotations are preserved, but differing annotation is lost. This is the same behaviour used in the SeqRecord annotations and is designed to prevent accidental propagation of inappropriate values: >>> combined.annotations {'tool': 'demo'} """ if not isinstance(other, MultipleSeqAlignment): raise NotImplementedError if len(self) != len(other): raise ValueError("When adding two alignments they must have the same length" " (i.e. same number or rows)") alpha = Alphabet._consensus_alphabet([self._alphabet, other._alphabet]) merged = (left + right for left, right in zip(self, other)) # Take any common annotation: annotations = dict() for k, v in self.annotations.items(): if k in other.annotations and other.annotations[k] == v: annotations[k] = v return MultipleSeqAlignment(merged, alpha, annotations)
def __add__(self, other): """Combines to alignments with the same number of rows by adding them. If you have two multiple sequence alignments (MSAs), there are two ways to think about adding them - by row or by column. Using the extend method adds by row. Using the addition operator adds by column. For example, >>> from SAP.Bio.Alphabet import generic_dna >>> from SAP.Bio.Seq import Seq >>> from SAP.Bio.SeqRecord import SeqRecord >>> from SAP.Bio.Align import MultipleSeqAlignment >>> a1 = SeqRecord(Seq("AAAAC", generic_dna), id="Alpha") >>> b1 = SeqRecord(Seq("AAA-C", generic_dna), id="Beta") >>> c1 = SeqRecord(Seq("AAAAG", generic_dna), id="Gamma") >>> a2 = SeqRecord(Seq("GT", generic_dna), id="Alpha") >>> b2 = SeqRecord(Seq("GT", generic_dna), id="Beta") >>> c2 = SeqRecord(Seq("GT", generic_dna), id="Gamma") >>> left = MultipleSeqAlignment([a1, b1, c1], ... annotations={"tool": "demo", "name": "start"}) >>> right = MultipleSeqAlignment([a2, b2, c2], ... annotations={"tool": "demo", "name": "end"}) Now, let's look at these two alignments: >>> print(left) DNAAlphabet() alignment with 3 rows and 5 columns AAAAC Alpha AAA-C Beta AAAAG Gamma >>> print(right) DNAAlphabet() alignment with 3 rows and 2 columns GT Alpha GT Beta GT Gamma And add them: >>> combined = left + right >>> print(combined) DNAAlphabet() alignment with 3 rows and 7 columns AAAACGT Alpha AAA-CGT Beta AAAAGGT Gamma For this to work, both alignments must have the same number of records (here they both have 3 rows): >>> len(left) 3 >>> len(right) 3 >>> len(combined) 3 The individual rows are SeqRecord objects, and these can be added together. Refer to the SeqRecord documentation for details of how the annotation is handled. This example is a special case in that both original alignments shared the same names, meaning when the rows are added they also get the same name. Any common annotations are preserved, but differing annotation is lost. This is the same behaviour used in the SeqRecord annotations and is designed to prevent accidental propagation of inappropriate values: >>> combined.annotations {'tool': 'demo'} """ if not isinstance(other, MultipleSeqAlignment): raise NotImplementedError if len(self) != len(other): raise ValueError("When adding two alignments they must have the same length" " (i.e. same number or rows)") alpha = Alphabet._consensus_alphabet([self._alphabet, other._alphabet]) merged = (left+right for left, right in zip(self, other)) # Take any common annotation: annotations = dict() for k, v in self.annotations.items(): if k in other.annotations and other.annotations[k] == v: annotations[k] = v return MultipleSeqAlignment(merged, alpha, annotations)
def __init__(self, records, alphabet=None, annotations=None): """Initialize a new MultipleSeqAlignment object. Arguments: - records - A list (or iterator) of SeqRecord objects, whose sequences are all the same length. This may be an be an empty list. - alphabet - The alphabet for the whole alignment, typically a gapped alphabet, which should be a super-set of the individual record alphabets. If omitted, a consensus alphabet is used. - annotations - Information about the whole alignment (dictionary). You would normally load a MSA from a file using Bio.AlignIO, but you can do this from a list of SeqRecord objects too: >>> from SAP.Bio.Alphabet import generic_dna >>> from SAP.Bio.Seq import Seq >>> from SAP.Bio.SeqRecord import SeqRecord >>> a = SeqRecord(Seq("AAAACGT", generic_dna), id="Alpha") >>> b = SeqRecord(Seq("AAA-CGT", generic_dna), id="Beta") >>> c = SeqRecord(Seq("AAAAGGT", generic_dna), id="Gamma") >>> align = MultipleSeqAlignment([a, b, c], annotations={"tool": "demo"}) >>> print(align) DNAAlphabet() alignment with 3 rows and 7 columns AAAACGT Alpha AAA-CGT Beta AAAAGGT Gamma >>> align.annotations {'tool': 'demo'} NOTE - The older Bio.Align.Generic.Alignment class only accepted a single argument, an alphabet. This is still supported via a backwards compatible "hack" so as not to disrupt existing scripts and users, but is deprecated and will be removed in a future release. """ if isinstance(records, Alphabet.Alphabet) or isinstance(records, Alphabet.AlphabetEncoder): if alphabet is None: # TODO - Remove this backwards compatible mode! alphabet = records records = [] import warnings from SAP.Bio import BiopythonDeprecationWarning warnings.warn( "Invalid records argument: While the old " "Bio.Align.Generic.Alignment class only " "accepted a single argument (the alphabet), the " "newer Bio.Align.MultipleSeqAlignment class " "expects a list/iterator of SeqRecord objects " "(which can be an empty list) and an optional " "alphabet argument", BiopythonDeprecationWarning, ) else: raise ValueError("Invalid records argument") if alphabet is not None: if not (isinstance(alphabet, Alphabet.Alphabet) or isinstance(alphabet, Alphabet.AlphabetEncoder)): raise ValueError("Invalid alphabet argument") self._alphabet = alphabet else: # Default while we add sequences, will take a consensus later self._alphabet = Alphabet.single_letter_alphabet self._records = [] if records: self.extend(records) if alphabet is None: # No alphabet was given, take a consensus alphabet self._alphabet = Alphabet._consensus_alphabet( rec.seq.alphabet for rec in self._records if rec.seq is not None ) # Annotations about the whole alignment if annotations is None: annotations = {} elif not isinstance(annotations, dict): raise TypeError("annotations argument should be a dict") self.annotations = annotations
def __init__(self, records, alphabet=None, annotations=None): """Initialize a new MultipleSeqAlignment object. Arguments: - records - A list (or iterator) of SeqRecord objects, whose sequences are all the same length. This may be an be an empty list. - alphabet - The alphabet for the whole alignment, typically a gapped alphabet, which should be a super-set of the individual record alphabets. If omitted, a consensus alphabet is used. - annotations - Information about the whole alignment (dictionary). You would normally load a MSA from a file using Bio.AlignIO, but you can do this from a list of SeqRecord objects too: >>> from SAP.Bio.Alphabet import generic_dna >>> from SAP.Bio.Seq import Seq >>> from SAP.Bio.SeqRecord import SeqRecord >>> a = SeqRecord(Seq("AAAACGT", generic_dna), id="Alpha") >>> b = SeqRecord(Seq("AAA-CGT", generic_dna), id="Beta") >>> c = SeqRecord(Seq("AAAAGGT", generic_dna), id="Gamma") >>> align = MultipleSeqAlignment([a, b, c], annotations={"tool": "demo"}) >>> print(align) DNAAlphabet() alignment with 3 rows and 7 columns AAAACGT Alpha AAA-CGT Beta AAAAGGT Gamma >>> align.annotations {'tool': 'demo'} NOTE - The older Bio.Align.Generic.Alignment class only accepted a single argument, an alphabet. This is still supported via a backwards compatible "hack" so as not to disrupt existing scripts and users, but is deprecated and will be removed in a future release. """ if isinstance(records, Alphabet.Alphabet) \ or isinstance(records, Alphabet.AlphabetEncoder): if alphabet is None: #TODO - Remove this backwards compatible mode! alphabet = records records = [] import warnings from SAP.Bio import BiopythonDeprecationWarning warnings.warn("Invalid records argument: While the old " "Bio.Align.Generic.Alignment class only " "accepted a single argument (the alphabet), the " "newer Bio.Align.MultipleSeqAlignment class " "expects a list/iterator of SeqRecord objects " "(which can be an empty list) and an optional " "alphabet argument", BiopythonDeprecationWarning) else : raise ValueError("Invalid records argument") if alphabet is not None : if not (isinstance(alphabet, Alphabet.Alphabet) or isinstance(alphabet, Alphabet.AlphabetEncoder)): raise ValueError("Invalid alphabet argument") self._alphabet = alphabet else : #Default while we add sequences, will take a consensus later self._alphabet = Alphabet.single_letter_alphabet self._records = [] if records: self.extend(records) if alphabet is None: #No alphabet was given, take a consensus alphabet self._alphabet = Alphabet._consensus_alphabet(rec.seq.alphabet for rec in self._records if rec.seq is not None) # Annotations about the whole alignment if annotations is None: annotations = {} elif not isinstance(annotations, dict): raise TypeError("annotations argument should be a dict") self.annotations = annotations