U
    ]<`]                    @   s   d Z ddlZddlZddlZddlmZ ddlmZmZ ddlm	Z
 ddlmZ ddlmZ dd	 ZeeZeeZG d
d dZG dd deZG dd dZdd Zdd Zd%ddZd&ddZdd Zdd Zd d! Zd"d# Zed$k re  dS )'aP  Provide objects to represent biological sequences.

See also the Seq_ wiki and the chapter in our tutorial:
 - `HTML Tutorial`_
 - `PDF Tutorial`_

.. _Seq: http://biopython.org/wiki/Seq
.. _`HTML Tutorial`: http://biopython.org/DIST/docs/tutorial/Tutorial.html
.. _`PDF Tutorial`: http://biopython.org/DIST/docs/tutorial/Tutorial.pdf

    N)BiopythonWarning)ambiguous_dna_complementambiguous_rna_complement)ambiguous_dna_letters)ambiguous_rna_letters)
CodonTablec                 C   s@   d |  }d |  }|| 7 }|| 7 }t||S )a  Make a python string translation table (PRIVATE).

    Arguments:
     - complement_mapping - a dictionary such as ambiguous_dna_complement
       and ambiguous_rna_complement from Data.IUPACData.

    Returns a translation table (a string of length 256) for use with the
    python string's translate method to use in a (reverse) complement.

    Compatible with lower case and upper case sequences.

    For internal use only.
     )joinkeysvalueslowerstr	maketrans)Zcomplement_mappingZbeforeZafter r   &lib/python3.8/site-packages/Bio/Seq.py
_maketrans"   s
    r   c                   @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$ejfd%d&Zd$ejfd'd(Zd)d* Zd$ejfd+d,Zd$ejfd-d.Zd$ejfd/d0Zd$ejfd1d2Zd$ejfd3d4Zd$ejfd5d6Zdad9d:Z dbd;d<Z!dcd=d>Z"ddd?d@Z#dedAdBZ$dCdD Z%dEdF Z&dfdIdJZ'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dgd[d\Z.dhd]d^Z/d_d` Z0d7S )iSeqaB  Read-only sequence object (essentially a string with biological methods).

    Like normal python strings, our basic sequence object is immutable.
    This prevents you from doing my_seq[5] = "A" for example, but does allow
    Seq objects to be used as dictionary keys.

    The Seq object provides a number of string like methods (such as count,
    find, split and strip).

    The Seq object also provides some biological methods, such as complement,
    reverse_complement, transcribe, back_transcribe and translate (which are
    not applicable to protein sequences).
    c                 C   s   t |tstd|| _dS )ak  Create a Seq object.

        Arguments:
         - data - Sequence, required (string)

        You will typically use Bio.SeqIO to read in sequences from files as
        SeqRecord objects, whose sequence will be exposed as a Seq object via
        the seq property.

        However, will often want to create your own Seq objects directly:

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF")
        >>> my_seq
        Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF')
        >>> print(my_seq)
        MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF
        zWThe sequence data given to a Seq object should be a string (not another Seq object etc)N)
isinstancer   	TypeError_dataselfdatar   r   r   __init__J   s
    
zSeq.__init__c                 C   sX   t | dkr>| jj dt| dd  dt| dd  dS | jj d| jd	S dS )
@Return (truncated) representation of the sequence for debugging.<   ('N6   ...')())len	__class____name__r   r   r   r   r   r   __repr__e   s    2zSeq.__repr__c                 C   s   | j S )a  Return the full sequence as a python string, use str(my_seq).

        Note that Biopython 1.44 and earlier would give a truncated
        version of repr(my_seq) for str(my_seq).  If you are writing code
        which need to be backwards compatible with really old Biopython,
        you should continue to use my_seq.tostring() as follows::

            try:
                # The old way, removed in Biopython 1.73
                as_string = seq_obj.tostring()
            except AttributeError:
                # The new way, needs Biopython 1.45 or later.
                # Don't use this on Biopython 1.44 or older as truncates
                as_string = str(seq_obj)

        )r   r&   r   r   r   __str__o   s    zSeq.__str__c                 C   s   t t| S )zHash of the sequence as a string for comparison.

        See Seq object comparison documentation (method ``__eq__`` in
        particular) as this has changed in Biopython 1.65. Older versions
        would hash on object identity.
        )hashr   r&   r   r   r   __hash__   s    zSeq.__hash__c                 C   s   t | t |kS )aU  Compare the sequence to another sequence or a string (README).

        Historically comparing Seq objects has done Python object comparison.
        After considerable discussion (keeping in mind constraints of the
        Python language, hashes and dictionary support), Biopython now uses
        simple string comparison (with a warning about the change).

        If you still need to support releases prior to Biopython 1.65, please
        just do explicit comparisons:

        >>> from Bio.Seq import Seq
        >>> seq1 = Seq("ACGT")
        >>> seq2 = Seq("ACGT")
        >>> id(seq1) == id(seq2)
        False
        >>> str(seq1) == str(seq2)
        True

        The new behaviour is to use string-like equality:

        >>> from Bio.Seq import Seq
        >>> seq1 == seq2
        True
        >>> seq1 == "ACGT"
        True
        r   r   otherr   r   r   __eq__   s    z
Seq.__eq__c                 C   sF   t |tttfr t| t|k S tdt| j dt|j ddS z Implement the less-than operand.z('<' not supported between instances of '' and ''Nr   r   r   
MutableSeqr   typer%   r,   r   r   r   __lt__   s
    z
Seq.__lt__c                 C   sF   t |tttfr t| t|kS tdt| j dt|j ddS z)Implement the less-than or equal operand.z)'<=' not supported between instances of 'r0   r1   Nr2   r,   r   r   r   __le__   s
    z
Seq.__le__c                 C   sF   t |tttfr t| t|kS tdt| j dt|j ddS z#Implement the greater-than operand.z('>' not supported between instances of 'r0   r1   Nr2   r,   r   r   r   __gt__   s
    z
Seq.__gt__c                 C   sF   t |tttfr t| t|kS tdt| j dt|j ddS z,Implement the greater-than or equal operand.z)'>=' not supported between instances of 'r0   r1   Nr2   r,   r   r   r   __ge__   s
    z
Seq.__ge__c                 C   s
   t | jS z3Return the length of the sequence, use len(my_seq).)r#   r   r&   r   r   r   __len__   s    zSeq.__len__c                 C   s&   t |tr| j| S t| j| S dS )zReturn a subsequence of single letter, use my_seq[index].

        >>> my_seq = Seq('ACTCGACGTCG')
        >>> my_seq[5]
        'A'
        N)r   intr   r   r   indexr   r   r   __getitem__   s    

zSeq.__getitem__c                 C   sH   t |tttfr&| t| t| S ddlm} t ||r@tS tdS )zAdd another sequence or string to this sequence.

        >>> from Bio.Seq import Seq
        >>> Seq("MELKI") + "LV"
        Seq('MELKILV')
        r   	SeqRecordN)	r   r   r   r3   r$   Bio.SeqRecordrC   NotImplementedr   )r   r-   rC   r   r   r   __add__   s    
zSeq.__add__c                 C   s.   t |tttfr&| t|t|  S tdS )zAdd a sequence on the left.

        >>> from Bio.Seq import Seq
        >>> "LV" + Seq("MELKI")
        Seq('LVMELKI')

        Adding two Seq (like) objects is handled via the __add__ method.
        N)r   r   r   r3   r$   r   r,   r   r   r   __radd__   s    	zSeq.__radd__c                 C   s0   t |tstd| jj d| t| | S )zwMultiply Seq by integer.

        >>> from Bio.Seq import Seq
        >>> Seq('ATG') * 2
        Seq('ATGATG')
        can't multiply  by non-int typer   r>   r   r$   r%   r   r,   r   r   r   __mul__   s    
zSeq.__mul__c                 C   s0   t |tstd| jj d| t| | S )zwMultiply integer by Seq.

        >>> from Bio.Seq import Seq
        >>> 2 * Seq('ATG')
        Seq('ATGATG')
        rH   rI   rJ   r,   r   r   r   __rmul__	  s    
zSeq.__rmul__c                 C   s0   t |tstd| jj d| t| | S )zMultiply Seq in-place.

        >>> from Bio.Seq import Seq
        >>> seq = Seq('ATG')
        >>> seq *= 2
        >>> seq
        Seq('ATGATG')
        rH   rI   rJ   r,   r   r   r   __imul__  s    	
zSeq.__imul__c                 C   s   t t| S )a  Return the full sequence as a MutableSeq object.

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("MKQHKAMIVALIVICITAVVAAL")
        >>> my_seq
        Seq('MKQHKAMIVALIVICITAVVAAL')
        >>> my_seq.tomutable()
        MutableSeq('MKQHKAMIVALIVICITAVVAAL')
        )r3   r   r&   r   r   r   	tomutable!  s    
zSeq.tomutabler   c                 C   s   t | t |||S )a,  Return a non-overlapping count, like that of a python string.

        This behaves like the python string method of the same name,
        which does a non-overlapping count!

        For an overlapping search use the newer count_overlap() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        e.g.

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("AAAATGA")
        >>> print(my_seq.count("A"))
        5
        >>> print(my_seq.count("ATG"))
        1
        >>> print(my_seq.count(Seq("AT")))
        1
        >>> print(my_seq.count("AT", 2, -1))
        1

        HOWEVER, please note because python strings and Seq objects (and
        MutableSeq objects) do a non-overlapping search, this may not give
        the answer you expect:

        >>> "AAAA".count("AA")
        2
        >>> print(Seq("AAAA").count("AA"))
        2

        An overlapping search, as implemented in .count_overlap(),
        would give the answer as three!
        )r   countr   substartendr   r   r   rO   -  s    +z	Seq.countc                 C   sB   t |}t | }d}||||d }|dkr8|d7 }q|S qdS )a0  Return an overlapping count.

        For a non-overlapping search use the count() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        e.g.

        >>> from Bio.Seq import Seq
        >>> print(Seq("AAAA").count_overlap("AA"))
        3
        >>> print(Seq("ATATATATA").count_overlap("ATA"))
        4
        >>> print(Seq("ATATATATA").count_overlap("ATA", 3, -1))
        1

        Where substrings do not overlap, should behave the same as
        the count() method:

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("AAAATGA")
        >>> print(my_seq.count_overlap("A"))
        5
        >>> my_seq.count_overlap("A") == my_seq.count("A")
        True
        >>> print(my_seq.count_overlap("ATG"))
        1
        >>> my_seq.count_overlap("ATG") == my_seq.count("ATG")
        True
        >>> print(my_seq.count_overlap(Seq("AT")))
        1
        >>> my_seq.count_overlap(Seq("AT")) == my_seq.count(Seq("AT"))
        True
        >>> print(my_seq.count_overlap("AT", 2, -1))
        1
        >>> my_seq.count_overlap("AT", 2, -1) == my_seq.count("AT", 2, -1)
        True

        HOWEVER, do not use this method for such cases because the
        count() method is much for efficient.
        r      Nr   findr   rQ   rR   rS   sub_strZself_strZoverlap_countr   r   r   count_overlapZ  s    2
zSeq.count_overlapc                 C   s   t |t | kS )zImplement the 'in' keyword, like a python string.

        e.g.

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("ATATGAAATTTGAAAA")
        >>> "AAA" in my_dna
        True
        >>> Seq("AAA") in my_dna
        True
        r+   )r   charr   r   r   __contains__  s    zSeq.__contains__c                 C   s   t | t |||S )a  Find method, like that of a python string.

        This behaves like the python string method of the same name.

        Returns an integer, the index of the first occurrence of substring
        argument sub in the (sub)sequence given by [start:end].

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        Returns -1 if the subsequence is NOT found.

        e.g. Locating the first typical start codon, AUG, in an RNA sequence:

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_rna.find("AUG")
        3
        rU   rP   r   r   r   rV     s    zSeq.findc                 C   s   t | t |||S )a  Find from right method, like that of a python string.

        This behaves like the python string method of the same name.

        Returns an integer, the index of the last (right most) occurrence of
        substring argument sub in the (sub)sequence given by [start:end].

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        Returns -1 if the subsequence is NOT found.

        e.g. Locating the last typical start codon, AUG, in an RNA sequence:

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_rna.rfind("AUG")
        15
        )r   rfindrP   r   r   r   r\     s    z	Seq.rfindc                 C   s   t | t |||S )al  Like find() but raise ValueError when the substring is not found.

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_rna.find("T")
        -1
        >>> my_rna.index("T")
        Traceback (most recent call last):
                   ...
        ValueError: substring not found...
        )r   r@   rP   r   r   r   r@     s    z	Seq.indexc                 C   s   t | t |||S )zBLike rfind() but raise ValueError when the substring is not found.)r   rindexrP   r   r   r   r]     s    z
Seq.rindexc                 C   sH   t |tr.tdd |D }t| |||S t| t|||S dS )a+  Return True if the Seq starts with the given prefix, False otherwise.

        This behaves like the python string method of the same name.

        Return True if the sequence starts with the specified prefix
        (a string or another Seq object), False otherwise.
        With optional start, test sequence beginning at that position.
        With optional end, stop comparing sequence at that position.
        prefix can also be a tuple of strings to try.  e.g.

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_rna.startswith("GUC")
        True
        >>> my_rna.startswith("AUG")
        False
        >>> my_rna.startswith("AUG", 3)
        True
        >>> my_rna.startswith(("UCC", "UCA", "UCG"), 1)
        True
        c                 s   s   | ]}t |V  qd S Nr+   .0pr   r   r   	<genexpr>  s     z!Seq.startswith.<locals>.<genexpr>N)r   tupler   
startswith)r   prefixrR   rS   Zprefix_strsr   r   r   rd     s    
zSeq.startswithc                 C   sH   t |tr.tdd |D }t| |||S t| t|||S dS )a   Return True if the Seq ends with the given suffix, False otherwise.

        This behaves like the python string method of the same name.

        Return True if the sequence ends with the specified suffix
        (a string or another Seq object), False otherwise.
        With optional start, test sequence beginning at that position.
        With optional end, stop comparing sequence at that position.
        suffix can also be a tuple of strings to try.  e.g.

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_rna.endswith("UUG")
        True
        >>> my_rna.endswith("AUG")
        False
        >>> my_rna.endswith("AUG", 0, 18)
        True
        >>> my_rna.endswith(("UCC", "UCA", "UUG"))
        True
        c                 s   s   | ]}t |V  qd S r^   r+   r_   r   r   r   rb     s     zSeq.endswith.<locals>.<genexpr>N)r   rc   r   endswith)r   suffixrR   rS   Zsuffix_strsr   r   r   rf     s    
zSeq.endswithNc                 C   s   dd t | t ||D S )ah  Split method, like that of a python string.

        This behaves like the python string method of the same name.

        Return a list of the 'words' in the string (as Seq objects),
        using sep as the delimiter string.  If maxsplit is given, at
        most maxsplit splits are done.  If maxsplit is omitted, all
        splits are made.

        Following the python string method, sep will by default be any
        white space (tabs, spaces, newlines) but this is unlikely to
        apply to biological sequences.

        e.g.

        >>> from Bio.Seq import Seq
        >>> my_rna = Seq("GUCAUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAGUUG")
        >>> my_aa = my_rna.translate()
        >>> my_aa
        Seq('VMAIVMGR*KGAR*L')
        >>> for pep in my_aa.split("*"):
        ...     pep
        Seq('VMAIVMGR')
        Seq('KGAR')
        Seq('L')
        >>> for pep in my_aa.split("*", 1):
        ...     pep
        Seq('VMAIVMGR')
        Seq('KGAR*L')

        See also the rsplit method:

        >>> for pep in my_aa.rsplit("*", 1):
        ...     pep
        Seq('VMAIVMGR*KGAR')
        Seq('L')
        c                 S   s   g | ]}t |qS r   r   r`   partr   r   r   
<listcomp>D  s     zSeq.split.<locals>.<listcomp>)r   splitr   sepmaxsplitr   r   r   rm     s    &z	Seq.splitc                 C   s   dd t | t ||D S )a  Do a right split method, like that of a python string.

        This behaves like the python string method of the same name.

        Return a list of the 'words' in the string (as Seq objects),
        using sep as the delimiter string.  If maxsplit is given, at
        most maxsplit splits are done COUNTING FROM THE RIGHT.
        If maxsplit is omitted, all splits are made.

        Following the python string method, sep will by default be any
        white space (tabs, spaces, newlines) but this is unlikely to
        apply to biological sequences.

        e.g. print(my_seq.rsplit("*",1))

        See also the split method.
        c                 S   s   g | ]}t |qS r   ri   rj   r   r   r   rl   X  s     zSeq.rsplit.<locals>.<listcomp>)r   rsplitrn   r   r   r   rq   F  s    z
Seq.rsplitc                 C   s   t t| t|S )a  Return a new Seq object with leading and trailing ends stripped.

        This behaves like the python string method of the same name.

        Optional argument chars defines which characters to remove.  If
        omitted or None (default) then as for the python string method,
        this defaults to removing any white space.

        e.g. print(my_seq.strip("-"))

        See also the lstrip and rstrip methods.
        )r   r   stripr   charsr   r   r   rr   Z  s    z	Seq.stripc                 C   s   t t| t|S )a  Return a new Seq object with leading (left) end stripped.

        This behaves like the python string method of the same name.

        Optional argument chars defines which characters to remove.  If
        omitted or None (default) then as for the python string method,
        this defaults to removing any white space.

        e.g. print(my_seq.lstrip("-"))

        See also the strip and rstrip methods.
        )r   r   lstriprs   r   r   r   ru   i  s    z
Seq.lstripc                 C   s   t t| t|S )a  Return a new Seq object with trailing (right) end stripped.

        This behaves like the python string method of the same name.

        Optional argument chars defines which characters to remove.  If
        omitted or None (default) then as for the python string method,
        this defaults to removing any white space.

        e.g. Removing a nucleotide sequence's polyadenylation (poly-A tail):

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("CGGTACGCTTATGTCACGTAGAAAAAA")
        >>> my_seq
        Seq('CGGTACGCTTATGTCACGTAGAAAAAA')
        >>> my_seq.rstrip("A")
        Seq('CGGTACGCTTATGTCACGTAG')

        See also the strip and lstrip methods.
        )r   r   rstriprs   r   r   r   rv   x  s    z
Seq.rstripc                 C   s   t t|  S )a  Return an upper case copy of the sequence.

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("VHLTPeeK*")
        >>> my_seq
        Seq('VHLTPeeK*')
        >>> my_seq.lower()
        Seq('vhltpeek*')
        >>> my_seq.upper()
        Seq('VHLTPEEK*')
        )r   r   upperr&   r   r   r   rw     s    z	Seq.upperc                 C   s   t t|  S )a7  Return a lower case copy of the sequence.

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("CGGTACGCTTATGTCACGTAGAAAAAA")
        >>> my_seq
        Seq('CGGTACGCTTATGTCACGTAGAAAAAA')
        >>> my_seq.lower()
        Seq('cggtacgcttatgtcacgtagaaaaaa')

        See also the upper method.
        )r   r   r   r&   r   r   r   r     s    z	Seq.lowerutf-8strictc                 C   s   t | ||S )av  Return an encoded version of the sequence as a bytes object.

        The Seq object aims to match the interface of a Python string.

        This is essentially to save you doing str(my_seq).encode() when
        you need a bytes string, for example for computing a hash:

        >>> from Bio.Seq import Seq
        >>> Seq("ACGT").encode("ascii")
        b'ACGT'
        )r   encode)r   encodingerrorsr   r   r   rz     s    z
Seq.encodec                 C   sb   d| j ksd| j kr2d| j ks(d| j kr2tdnd| j ksFd| j krLt}nt}tt| |S )a5  Return the complement sequence by creating a new Seq object.

        This method is intended for use with DNA sequences:

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("CCCCCGATAG")
        >>> my_dna
        Seq('CCCCCGATAG')
        >>> my_dna.complement()
        Seq('GGGGGCTATC')

        You can of course used mixed case sequences,

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("CCCCCgatA-GD")
        >>> my_dna
        Seq('CCCCCgatA-GD')
        >>> my_dna.complement()
        Seq('GGGGGctaT-CH')

        Note in the above example, ambiguous character D denotes
        G, A or T so its complement is H (for C, T or A).

        Note that if the sequence contains neither T nor U, we
        assume it is DNA and map any A character to T:

        >>> Seq("CGA").complement()
        Seq('GCT')
        >>> Seq("CGAT").complement()
        Seq('GCTA')

        If you actually have RNA, this currently works but we
        may deprecate this later. We recommend using the new
        complement_rna method instead:

        >>> Seq("CGAU").complement()
        Seq('GCUA')
        >>> Seq("CGAU").complement_rna()
        Seq('GCUA')

        If the sequence contains both T and U, an exception is
        raised:

        >>> Seq("CGAUT").complement()
        Traceback (most recent call last):
           ...
        ValueError: Mixed RNA/DNA found

        Trying to complement a protein sequence gives a meaningless
        sequence:

        >>> my_protein = Seq("MAIVMGR")
        >>> my_protein.complement()
        Seq('KTIBKCY')

        Here "M" was interpreted as the IUPAC ambiguity code for
        "A" or "C", with complement "K" for "T" or "G". Likewise
        "A" has complement "T". The letter "I" has no defined
        meaning under the IUPAC convention, and is unchanged.
        UuTtMixed RNA/DNA found)r   
ValueError_rna_complement_table_dna_complement_tabler   r   	translate)r   ttabler   r   r   
complement  s    =
zSeq.complementc                 C   s   |   ddd S )a  Return the reverse complement sequence by creating a new Seq object.

        This method is intended for use with DNA sequences:

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("CCCCCGATAGNR")
        >>> my_dna
        Seq('CCCCCGATAGNR')
        >>> my_dna.reverse_complement()
        Seq('YNCTATCGGGGG')

        Note in the above example, since R = G or A, its complement
        is Y (which denotes C or T).

        You can of course used mixed case sequences,

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("CCCCCgatA-G")
        >>> my_dna
        Seq('CCCCCgatA-G')
        >>> my_dna.reverse_complement()
        Seq('C-TatcGGGGG')

        As discussed for the complement method, if the sequence
        contains neither T nor U, is is assumed to be DNA and
        will map any letter A to T.

        If you are dealing with RNA you should use the new
        reverse_complement_rna method instead

        >>> Seq("CGA").reverse_complement()  # defaults to DNA
        Seq('TCG')
        >>> Seq("CGA").reverse_complement_rna()
        Seq('UCG')

        If the sequence contains both T and U, an exception is raised:

        >>> Seq("CGAUT").reverse_complement()
        Traceback (most recent call last):
           ...
        ValueError: Mixed RNA/DNA found

        Trying to reverse complement a protein sequence will give
        a meaningless sequence:

        >>> from Bio.Seq import Seq
        >>> my_protein = Seq("MAIVMGR")
        >>> my_protein.reverse_complement()
        Seq('YCKBITK')

        Here "M" was interpretted as the IUPAC ambiguity code for
        "A" or "C", with complement "K" for "T" or "G" - and so on.
        Nrh   r   r&   r   r   r   reverse_complement  s    7zSeq.reverse_complementc                 C   sL   d| j ksd| j kr:d| j ks(d| j kr2tdntdtt| tS )aO  Complement of an RNA sequence.

        >>> Seq("CGA").complement()  # defaults to DNA
        Seq('GCT')
        >>> Seq("CGA").complement_rna()
        Seq('GCU')

        If the sequence contains both T and U, an exception is raised:

        >>> Seq("CGAUT").complement_rna()
        Traceback (most recent call last):
           ...
        ValueError: Mixed RNA/DNA found

        If the sequence contains T, an exception is raised:

        >>> Seq("ACGT").complement_rna()
        Traceback (most recent call last):
           ...
        ValueError: DNA found, RNA expected
        r   r   r}   r~   r   zDNA found, RNA expected)r   r   r   r   r   r   r&   r   r   r   complement_rna;  s
    
zSeq.complement_rnac                 C   s   |   ddd S )zReverse complement of an RNA sequence.

        >>> from Bio.Seq import Seq
        >>> Seq("ACG").reverse_complement_rna()
        Seq('CGU')
        Nrh   )r   r&   r   r   r   reverse_complement_rnaX  s    zSeq.reverse_complement_rnac                 C   s   t t| ddddS )a  Return the RNA sequence from a DNA sequence by creating a new Seq object.

        >>> from Bio.Seq import Seq
        >>> coding_dna = Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")
        >>> coding_dna
        Seq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')
        >>> coding_dna.transcribe()
        Seq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')

        Trying to transcribe an RNA sequence should have no effect.
        If you have a nucleotide sequence which might be DNA or RNA
        (or even a mixture), calling the transcribe method will ensure
        any T becomes U.

        Trying to transcribe a protein sequence will replace any
        T for Threonine with U for Selenocysteine, which has no
        biologically plausible rational. Older versions of Biopython
        would throw an exception.

        >>> from Bio.Seq import Seq
        >>> my_protein = Seq("MAIVMGRT")
        >>> my_protein.transcribe()
        Seq('MAIVMGRU')
        r   r}   r   r~   r   r   replacer&   r   r   r   
transcribeb  s    zSeq.transcribec                 C   s   t t| ddddS )a  Return the DNA sequence from an RNA sequence by creating a new Seq object.

        >>> from Bio.Seq import Seq
        >>> messenger_rna = Seq("AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG")
        >>> messenger_rna
        Seq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')
        >>> messenger_rna.back_transcribe()
        Seq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')

        Trying to back-transcribe DNA has no effect, If you have a nucleotide
        sequence which might be DNA or RNA (or even a mixture), calling the
        back-transcribe method will ensure any T becomes U.

        Trying to back-transcribe a protein sequence will replace any U for
        Selenocysteine with T for Threonine, which is biologically meaningless.
        Older versions of Biopython would raise an exception here:

        >>> from Bio.Seq import Seq
        >>> my_protein = Seq("MAIVMGRU")
        >>> my_protein.back_transcribe()
        Seq('MAIVMGRT')
        r}   r   r~   r   r   r&   r   r   r   back_transcribe}  s    zSeq.back_transcribeStandard*F-c              	   C   s   t |trt|dkrtdzt|}W nP tk
rH   tj| }Y n> ttfk
rz   t |tjrl|}n
tddY nX tj	| }t
tt| |||||dS )a  Turn a nucleotide sequence into a protein sequence by creating a new Seq object.

        This method will translate DNA or RNA sequences. It should not
        be used on protein sequences as any result will be biologically
        meaningless.

        Arguments:
         - table - Which codon table to use?  This can be either a name
           (string), an NCBI identifier (integer), or a CodonTable
           object (useful for non-standard genetic codes).  This
           defaults to the "Standard" table.
         - stop_symbol - Single character string, what to use for
           terminators.  This defaults to the asterisk, "*".
         - to_stop - Boolean, defaults to False meaning do a full
           translation continuing on past any stop codons (translated as the
           specified stop_symbol).  If True, translation is terminated at
           the first in frame stop codon (and the stop_symbol is not
           appended to the returned protein sequence).
         - cds - Boolean, indicates this is a complete CDS.  If True,
           this checks the sequence starts with a valid alternative start
           codon (which will be translated as methionine, M), that the
           sequence length is a multiple of three, and that there is a
           single in frame stop codon at the end (this will be excluded
           from the protein sequence, regardless of the to_stop option).
           If these tests fail, an exception is raised.
         - gap - Single character string to denote symbol used for gaps.
           Defaults to the minus sign.

        e.g. Using the standard table:

        >>> coding_dna = Seq("GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")
        >>> coding_dna.translate()
        Seq('VAIVMGR*KGAR*')
        >>> coding_dna.translate(stop_symbol="@")
        Seq('VAIVMGR@KGAR@')
        >>> coding_dna.translate(to_stop=True)
        Seq('VAIVMGR')

        Now using NCBI table 2, where TGA is not a stop codon:

        >>> coding_dna.translate(table=2)
        Seq('VAIVMGRWKGAR*')
        >>> coding_dna.translate(table=2, to_stop=True)
        Seq('VAIVMGRWKGAR')

        In fact, GTG is an alternative start codon under NCBI table 2, meaning
        this sequence could be a complete CDS:

        >>> coding_dna.translate(table=2, cds=True)
        Seq('MAIVMGRWKGAR')

        It isn't a valid CDS under NCBI table 1, due to both the start codon
        and also the in frame stop codons:

        >>> coding_dna.translate(table=1, cds=True)
        Traceback (most recent call last):
            ...
        Bio.Data.CodonTable.TranslationError: First codon 'GTG' is not a start codon

        If the sequence has no in-frame stop codon, then the to_stop argument
        has no effect:

        >>> coding_dna2 = Seq("TTGGCCATTGTAATGGGCCGC")
        >>> coding_dna2.translate()
        Seq('LAIVMGR')
        >>> coding_dna2.translate(to_stop=True)
        Seq('LAIVMGR')

        NOTE - Ambiguous codons like "TAN" or "NNN" could be an amino acid
        or a stop codon.  These are translated as "X".  Any invalid codon
        (e.g. "TA?" or "T-A") will throw a TranslationError.

        NOTE - This does NOT behave like the python string's translate
        method.  For that use str(my_seq).translate(...) instead
           zThe Seq object translate method DOES NOT take a 256 character string mapping table like the python string object's translate method. Use str(my_seq).translate(...) instead.Bad table argumentNgap)r   r   r#   r   r>   r   ambiguous_generic_by_nameAttributeErrorr   ambiguous_generic_by_idr   _translate_str)r   tablestop_symbolto_stopcdsr   Ztable_idcodon_tabler   r   r   r     s     N
zSeq.translatec                 C   sF   |st dn$t|dks$t|ts2t d|tt| |dS )a  Return a copy of the sequence without the gap character(s).

        The gap character now defaults to the minus sign, and can only
        be specified via the method argument. This is no longer possible
        via the sequence's alphabet (as was possible up to Biopython 1.77):

        >>> from Bio.Seq import Seq
        >>> my_dna = Seq("-ATA--TGAAAT-TTGAAAA")
        >>> my_dna
        Seq('-ATA--TGAAAT-TTGAAAA')
        >>> my_dna.ungap("-")
        Seq('ATATGAAATTTGAAAA')
        zGap character required.rT   zUnexpected gap character, r   )r   r#   r   r   r   r   )r   r   r   r   r   ungap  s
    
z	Seq.ungapc                 C   s   t |tttfr(| t| t|S ddlm} t ||rFtd|D ]0}t ||rbtdqJt |tttfsJtdqJ| t| dd |D S )a  Return a merge of the sequences in other, spaced by the sequence from self.

        Accepts either a Seq or string (and iterates over the letters), or an
        iterable containing Seq or string objects. These arguments will be
        concatenated with the calling sequence as the spacer:

        >>> concatenated = Seq('NNNNN').join([Seq("AAA"), Seq("TTT"), Seq("PPP")])
        >>> concatenated
        Seq('AAANNNNNTTTNNNNNPPP')

        Joining the letters of a single sequence:

        >>> Seq('NNNNN').join(Seq("ACGT"))
        Seq('ANNNNNCNNNNNGNNNNNT')
        >>> Seq('NNNNN').join("ACGT")
        Seq('ANNNNNCNNNNNGNNNNNT')
        r   rB   Iterable cannot be a SeqRecord"Iterable cannot contain SeqRecords,Input must be an iterable of Seqs or Stringsc                 S   s   g | ]}t |qS r   r+   r`   _r   r   r   rl   4  s     zSeq.join.<locals>.<listcomp>)	r   r   r   r3   r$   r	   rD   rC   r   )r   r-   rC   cr   r   r   r	     s    



zSeq.join)Nrh   )Nrh   )N)N)N)rx   ry   )r   r   FFr   )r   )1r%   
__module____qualname____doc__r   r'   r(   r*   r.   r5   r7   r9   r;   r=   rA   rF   rG   rK   rL   rM   rN   sysmaxsizerO   rY   r[   rV   r\   r@   r]   rd   rf   rm   rq   rr   ru   rv   rw   r   rz   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   ;   sb   
					-<
(




J9
         
k
r   c                   @   s   e Zd ZdZd7ddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd ZdejfddZdejfddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd8d1d2Zd9d3d4Zd5d6 ZdS ):
UnknownSeqa  Read-only sequence object of known length but unknown contents.

    If you have an unknown sequence, you can represent this with a normal
    Seq object, for example:

    >>> my_seq = Seq("N"*5)
    >>> my_seq
    Seq('NNNNN')
    >>> len(my_seq)
    5
    >>> print(my_seq)
    NNNNN

    However, this is rather wasteful of memory (especially for large
    sequences), which is where this class is most useful:

    >>> unk_five = UnknownSeq(5)
    >>> unk_five
    UnknownSeq(5, character='?')
    >>> len(unk_five)
    5
    >>> print(unk_five)
    ?????

    You can add unknown sequence together. Provided the characters are the
    same, you get another memory saving UnknownSeq:

    >>> unk_four = UnknownSeq(4)
    >>> unk_four
    UnknownSeq(4, character='?')
    >>> unk_four + unk_five
    UnknownSeq(9, character='?')

    If the characters are different, addition gives an ordinary Seq object:

    >>> unk_nnnn = UnknownSeq(4, character="N")
    >>> unk_nnnn
    UnknownSeq(4, character='N')
    >>> unk_nnnn + unk_four
    Seq('NNNN????')

    Combining with a real Seq gives a new Seq object:

    >>> known_seq = Seq("ACGT")
    >>> unk_four + known_seq
    Seq('????ACGT')
    >>> known_seq + unk_four
    Seq('ACGT????')
    N?c                 C   sN   |dk	rt dt|| _| jdk r,t d|r<t|dkrDt d|| _dS )a   Create a new UnknownSeq object.

        Arguments:
         - length - Integer, required.
         - alphabet - no longer used, must be None.
         - character - single letter string, default "?". Typically "N"
           for nucleotides, "X" for proteins, and "?" otherwise.
        Nz,The alphabet argument is no longer supportedr   zLength must not be negative.rT   z4character argument should be a single letter string.)r   r>   _lengthr#   
_character)r   ZlengthZalphabet	characterr   r   r   r   j  s    	

zUnknownSeq.__init__c                 C   s   | j S )z1Return the stated length of the unknown sequence.)r   r&   r   r   r   r=   }  s    zUnknownSeq.__len__c                 C   s   | j | j S )z?Return the unknown sequence as full string of the given length.)r   r   r&   r   r   r   r(     s    zUnknownSeq.__str__c                 C   s   d| j  d| jdS )r   zUnknownSeq(z, character=r"   )r   r   r&   r   r   r   r'     s    zUnknownSeq.__repr__c                 C   s@   t |tr0|j| jkr0tt| t| | jdS tt| | S )a  Add another sequence or string to this sequence.

        Adding two UnknownSeq objects returns another UnknownSeq object
        provided the character is the same.

        >>> from Bio.Seq import UnknownSeq
        >>> UnknownSeq(10, character='X') + UnknownSeq(5, character='X')
        UnknownSeq(15, character='X')

        If the characters differ, an UnknownSeq object cannot be used, so a
        Seq object is returned:

        >>> from Bio.Seq import UnknownSeq
        >>> UnknownSeq(10, character='X') + UnknownSeq(5, character="x")
        Seq('XXXXXXXXXXxxxxx')

        If adding a string to an UnknownSeq, a new Seq is returned:

        >>> from Bio.Seq import UnknownSeq
        >>> UnknownSeq(5, character='X') + "LV"
        Seq('XXXXXLV')
        r   )r   r   r   r#   r   r   r,   r   r   r   rF     s    zUnknownSeq.__add__c                 C   s   |t t|  S )zAdd a sequence on the left.)r   r   r,   r   r   r   rG     s    zUnknownSeq.__radd__c                 C   s6   t |tstd| jj d| jt| | | jdS )zMultiply UnknownSeq by integer.

        >>> from Bio.Seq import UnknownSeq
        >>> UnknownSeq(3) * 2
        UnknownSeq(6, character='?')
        >>> UnknownSeq(3, character="N") * 2
        UnknownSeq(6, character='N')
        rH   rI   r   r   r>   r   r$   r%   r#   r   r,   r   r   r   rK     s    	
zUnknownSeq.__mul__c                 C   s6   t |tstd| jj d| jt| | | jdS )zMultiply integer by UnknownSeq.

        >>> from Bio.Seq import UnknownSeq
        >>> 2 * UnknownSeq(3)
        UnknownSeq(6, character='?')
        >>> 2 * UnknownSeq(3, character="N")
        UnknownSeq(6, character='N')
        rH   rI   r   r   r,   r   r   r   rL     s    	
zUnknownSeq.__rmul__c                 C   s6   t |tstd| jj d| jt| | | jdS )zMultiply UnknownSeq in-place.

        >>> from Bio.Seq import UnknownSeq
        >>> seq = UnknownSeq(3, character="N")
        >>> seq *= 2
        >>> seq
        UnknownSeq(6, character='N')
        rH   rI   r   r   r,   r   r   r   rM     s    	
zUnknownSeq.__imul__c                 C   s   t |trt| | S | j}|j}|dks2|dkr|j}|j}|dkrLd}n$|dk rdtd|| }n||krp|}|dkr~|}n$|dk rtd|| }n||kr|}td|| }n"|dkrtdnt	d| | }t
|| jdS )a  Get a subsequence from the UnknownSeq object.

        >>> unk = UnknownSeq(8, character="N")
        >>> print(unk[:])
        NNNNNNNN
        >>> print(unk[5:3])
        <BLANKLINE>
        >>> print(unk[1:-1])
        NNNNNN
        >>> print(unk[1:-1:2])
        NNN
        NrT   r   zslice step cannot be zeroXr   )r   r>   r   r   steprR   stopmaxr   r#   r   r   )r   r@   Z
old_lengthr   rR   rS   Z
new_lengthr   r   r   rA     s0    

zUnknownSeq.__getitem__r   c                 C   s   t |}| jt| }}t|t| jkr.dS |dkr:d}|dkrHtj}tt||| }tt||| }|dk r|||7 }|dk r||7 }||ks|| |k rdS || | S )a  Return a non-overlapping count, like that of a python string.

        This behaves like the python string (and Seq object) method of the
        same name, which does a non-overlapping count!

        For an overlapping search use the newer count_overlap() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        >>> "NNNN".count("N")
        4
        >>> Seq("NNNN").count("N")
        4
        >>> UnknownSeq(4, character="N").count("N")
        4
        >>> UnknownSeq(4, character="N").count("A")
        0
        >>> UnknownSeq(4, character="N").count("AA")
        0

        HOWEVER, please note because that python strings and Seq objects (and
        MutableSeq objects) do a non-overlapping search, this may not give
        the answer you expect:

        >>> UnknownSeq(4, character="N").count("NN")
        2
        >>> UnknownSeq(4, character="N").count("NNN")
        1
        r   N	r   r   r#   setr   r   r   r   minr   rQ   rR   rS   rX   Zlen_selfZlen_sub_strr   r   r   rO     s"    &zUnknownSeq.countc                 C   s   t |}| jt| }}t|t| jkr.dS |dkr:d}|dkrHtj}tt||| }tt||| }|dk r|||7 }|dk r||7 }||ks|| |k rdS || | d S )aG  Return an overlapping count.

        For a non-overlapping search use the count() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        e.g.

        >>> from Bio.Seq import UnknownSeq
        >>> UnknownSeq(4, character="N").count_overlap("NN")
        3
        >>> UnknownSeq(4, character="N").count_overlap("NNN")
        2

        Where substrings do not overlap, should behave the same as
        the count() method:

        >>> UnknownSeq(4, character="N").count_overlap("N")
        4
        >>> UnknownSeq(4, character="N").count_overlap("N") == UnknownSeq(4, character="N").count("N")
        True
        >>> UnknownSeq(4, character="N").count_overlap("A")
        0
        >>> UnknownSeq(4, character="N").count_overlap("A") == UnknownSeq(4, character="N").count("A")
        True
        >>> UnknownSeq(4, character="N").count_overlap("AA")
        0
        >>> UnknownSeq(4, character="N").count_overlap("AA") == UnknownSeq(4, character="N").count("AA")
        True
        r   NrT   r   r   r   r   r   rY   >  s"    'zUnknownSeq.count_overlapc                 C   s   | S )aJ  Return the complement of an unknown nucleotide equals itself.

        >>> my_nuc = UnknownSeq(8)
        >>> my_nuc
        UnknownSeq(8, character='?')
        >>> print(my_nuc)
        ????????
        >>> my_nuc.complement()
        UnknownSeq(8, character='?')
        >>> print(my_nuc.complement())
        ????????
        r   r&   r   r   r   r   ~  s    zUnknownSeq.complementc                 C   s   |   S )z)Return the complement assuming it is RNA.r   r&   r   r   r   r     s    zUnknownSeq.complement_rnac                 C   s   | S )aN  Return the reverse complement of an unknown sequence.

        The reverse complement of an unknown nucleotide equals itself:

        >>> from Bio.Seq import UnknownSeq
        >>> example = UnknownSeq(6, character="N")
        >>> print(example)
        NNNNNN
        >>> print(example.reverse_complement())
        NNNNNN
        r   r&   r   r   r   r     s    zUnknownSeq.reverse_complementc                 C   s   |   S )z1Return the reverse complement assuming it is RNA.)r   r&   r   r   r   r     s    z!UnknownSeq.reverse_complement_rnac                 C   s    t | j }t| jt|dS )an  Return an unknown RNA sequence from an unknown DNA sequence.

        >>> my_dna = UnknownSeq(10, character="N")
        >>> my_dna
        UnknownSeq(10, character='N')
        >>> print(my_dna)
        NNNNNNNNNN
        >>> my_rna = my_dna.transcribe()
        >>> my_rna
        UnknownSeq(10, character='N')
        >>> print(my_rna)
        NNNNNNNNNN
        r   )r   r   r   r   r   r   r   sr   r   r   r     s    zUnknownSeq.transcribec                 C   s    t | j }t| jt|dS )a  Return an unknown DNA sequence from an unknown RNA sequence.

        >>> my_rna = UnknownSeq(20, character="N")
        >>> my_rna
        UnknownSeq(20, character='N')
        >>> print(my_rna)
        NNNNNNNNNNNNNNNNNNNN
        >>> my_dna = my_rna.back_transcribe()
        >>> my_dna
        UnknownSeq(20, character='N')
        >>> print(my_dna)
        NNNNNNNNNNNNNNNNNNNN
        r   )r   r   r   r   r   r   r   r   r   r   r     s    zUnknownSeq.back_transcribec                 C   s   t | j| j dS )a  Return an upper case copy of the sequence.

        >>> from Bio.Seq import UnknownSeq
        >>> my_seq = UnknownSeq(20, character="n")
        >>> my_seq
        UnknownSeq(20, character='n')
        >>> print(my_seq)
        nnnnnnnnnnnnnnnnnnnn
        >>> my_seq.upper()
        UnknownSeq(20, character='N')
        >>> print(my_seq.upper())
        NNNNNNNNNNNNNNNNNNNN

        See also the lower method.
        r   )r   r   r   rw   r&   r   r   r   rw     s    zUnknownSeq.upperc                 C   s   t | j| j dS )a  Return a lower case copy of the sequence.

        >>> from Bio.Seq import UnknownSeq
        >>> my_seq = UnknownSeq(20, character="X")
        >>> my_seq
        UnknownSeq(20, character='X')
        >>> print(my_seq)
        XXXXXXXXXXXXXXXXXXXX
        >>> my_seq.lower()
        UnknownSeq(20, character='x')
        >>> print(my_seq.lower())
        xxxxxxxxxxxxxxxxxxxx

        See also the upper method.
        r   )r   r   r   r   r&   r   r   r   r     s    zUnknownSeq.lowerr   r   Fr   c                 C   s   t | jd ddS )aP  Translate an unknown nucleotide sequence into an unknown protein.

        e.g.

        >>> my_seq = UnknownSeq(9, character="N")
        >>> print(my_seq)
        NNNNNNNNN
        >>> my_protein = my_seq.translate()
        >>> my_protein
        UnknownSeq(3, character='X')
        >>> print(my_protein)
        XXX

        In comparison, using a normal Seq object:

        >>> my_seq = Seq("NNNNNNNNN")
        >>> print(my_seq)
        NNNNNNNNN
        >>> my_protein = my_seq.translate()
        >>> my_protein
        Seq('XXX')
        >>> print(my_protein)
        XXX

           r   r   )r   r   )r   r   r   r   r   r   r   r   r   r     s    zUnknownSeq.translatec                 C   s0   t | j|}|r$t| j| jdS t dS dS )a  Return a copy of the sequence without the gap character(s).

        The gap character now defaults to the minus sign, and can only
        be specified via the method argument. This is no longer possible
        via the sequence's alphabet (as was possible up to Biopython 1.77):

        >>> from Bio.Seq import UnknownSeq
        >>> my_dna = UnknownSeq(20, character='N')
        >>> my_dna
        UnknownSeq(20, character='N')
        >>> my_dna.ungap()  # using default
        UnknownSeq(20, character='N')
        >>> my_dna.ungap("-")
        UnknownSeq(20, character='N')

        If the UnknownSeq is using the gap character, then an empty Seq is
        returned:

        >>> my_gap = UnknownSeq(20, character="-")
        >>> my_gap
        UnknownSeq(20, character='-')
        >>> my_gap.ungap()  # using default
        Seq('')
        >>> my_gap.ungap("-")
        Seq('')
        r   r   N)r   r   r   r   r   )r   r   r   r   r   r   r     s    zUnknownSeq.ungapc                 C   s   ddl m} t|tttfrpt|trZ| j|jkrZ| jt	|t	| t	|d   | jdS tt| 
t|S t||rtd|D ]0}t||rtdqt|tttfstdqt| 
dd	 |D }|| jt	|kr| jt	|| jdS t|S )
ap  Return a merge of the sequences in other, spaced by the sequence from self.

        Accepts either a Seq or string (and iterates over the letters), or an
        iterable containing Seq or string objects. These arguments will be
        concatenated with the calling sequence as the spacer:

        >>> concatenated = UnknownSeq(5).join([Seq("AAA"), Seq("TTT"), Seq("PPP")])
        >>> concatenated
        Seq('AAA?????TTT?????PPP')

        If all the inputs are also UnknownSeq using the same character, then it
        returns a new UnknownSeq:

        >>> UnknownSeq(5).join([UnknownSeq(3), UnknownSeq(3), UnknownSeq(3)])
        UnknownSeq(19, character='?')

        Examples taking a single sequence and joining the letters:

        >>> UnknownSeq(3).join("ACGT")
        Seq('A???C???G???T')
        >>> UnknownSeq(3).join(UnknownSeq(4))
        UnknownSeq(13, character='?')

        Will only return an UnknownSeq object if all of the objects to be joined are
        also UnknownSeqs with the same character as the spacer, similar to how the
        addition of an UnknownSeq and another UnknownSeq would work.
        r   rB   rT   r   r   r   r   c                 S   s   g | ]}t |qS r   r+   r   r   r   r   rl   V  s     z#UnknownSeq.join.<locals>.<listcomp>)rD   rC   r   r   r   r3   r   r   r$   r#   r	   r   rO   )r   r-   rC   r   Z	temp_datar   r   r   r	   )  s&     



zUnknownSeq.join)Nr   )r   r   FFr   )r   )r%   r   r   r   r   r=   r(   r'   rF   rG   rK   rL   rM   rA   r   r   rO   rY   r   r   r   r   r   r   rw   r   r   r   r	   r   r   r   r   r   7  s:   2
-?@         

"r   c                   @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZdAd)d*Zd+d, Zd-ejfd.d/Zd-ejfd0d1Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z d:d; Z!d<d= Z"d>d? Z#d@S )Br3   a  An editable sequence object.

    Unlike normal python strings and our basic sequence object (the Seq class)
    which are immutable, the MutableSeq lets you edit the sequence in place.
    However, this means you cannot use a MutableSeq object as a dictionary key.

    >>> from Bio.Seq import MutableSeq
    >>> my_seq = MutableSeq("ACTCGTCGTCG")
    >>> my_seq
    MutableSeq('ACTCGTCGTCG')
    >>> my_seq[5]
    'T'
    >>> my_seq[5] = "A"
    >>> my_seq
    MutableSeq('ACTCGACGTCG')
    >>> my_seq[5]
    'A'
    >>> my_seq[5:8] = "NNN"
    >>> my_seq
    MutableSeq('ACTCGNNNTCG')
    >>> len(my_seq)
    11

    Note that the MutableSeq object does not support as many string-like
    or biological methods as the Seq object.
    c                 C   s>   t |trtd|| _n t |tttfr4tdn|| _dS )zInitialize the class.r~   zdThe sequence data given to a MutableSeq object should be a string or an array (not a Seq object etc)N)r   r   arrayr   r   r>   floatr   r   r   r   r   r   y  s    
zMutableSeq.__init__c                 C   sZ   t | dkr>| jj dt| dd  dt| dd  dS | jj dt|  dS dS )r   r   r   Nr   r   r   r    )r#   r$   r%   r   r&   r   r   r   r'     s    2zMutableSeq.__repr__c                 C   s   d | jS )aU  Return the full sequence as a python string.

        Note that Biopython 1.44 and earlier would give a truncated
        version of repr(my_seq) for str(my_seq).  If you are writing code
        which needs to be backwards compatible with old Biopython, you
        should continue to use my_seq.tostring() rather than str(my_seq).
        r   )r	   r   r&   r   r   r   r(     s    	zMutableSeq.__str__c                 C   s&   t |tr| j|jkS t| t|kS )a  Compare the sequence to another sequence or a string.

        Historically comparing DNA to RNA, or Nucleotide to Protein would
        raise an exception. This was later downgraded to a warning, but since
        Biopython 1.78 the alphabet is ignored for comparisons.

        If you need to support older Biopython versions, please just do
        explicit comparisons:

        >>> seq1 = MutableSeq("ACGT")
        >>> seq2 = MutableSeq("ACGT")
        >>> id(seq1) == id(seq2)
        False
        >>> str(seq1) == str(seq2)
        True

        Biopython now does:

        >>> seq1 == seq2
        True
        >>> seq1 == Seq("ACGT")
        True
        >>> seq1 == "ACGT"
        True

        )r   r3   r   r   r,   r   r   r   r.     s    
zMutableSeq.__eq__c                 C   s\   t |tr| j|jk S t |tttfr6t| t|k S tdt| j dt|j ddS r/   	r   r3   r   r   r   r   r   r4   r%   r,   r   r   r   r5     s    
zMutableSeq.__lt__c                 C   s\   t |tr| j|jkS t |tttfr6t| t|kS tdt| j dt|j ddS r6   r   r,   r   r   r   r7     s    
zMutableSeq.__le__c                 C   s\   t |tr| j|jkS t |tttfr6t| t|kS tdt| j dt|j ddS r8   r   r,   r   r   r   r9     s    
zMutableSeq.__gt__c                 C   s\   t |tr| j|jkS t |tttfr6t| t|kS tdt| j dt|j ddS r:   r   r,   r   r   r   r;     s    
zMutableSeq.__ge__c                 C   s
   t | jS r<   )r#   r   r&   r   r   r   r=     s    zMutableSeq.__len__c                 C   s&   t |tr| j| S t| j| S dS )zReturn a subsequence of single letter, use my_seq[index].

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq[5]
        'A'
        N)r   r>   r   r3   r?   r   r   r   rA     s    

zMutableSeq.__getitem__c                 C   sd   t |tr|| j|< nJt |tr.|j| j|< n2t |t| jrJ|| j|< ntdt|| j|< dS )zSet a subsequence of single letter via value parameter.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq[0] = 'T'
        >>> my_seq
        MutableSeq('TCTCGACGTCG')
        r~   N)r   r>   r   r3   r4   r   r   )r   r@   valuer   r   r   __setitem__  s    

zMutableSeq.__setitem__c                 C   s   | j |= dS )zDelete a subsequence of single letter.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> del my_seq[0]
        >>> my_seq
        MutableSeq('CTCGACGTCG')
        Nr   r?   r   r   r   __delitem__  s    	zMutableSeq.__delitem__c                 C   sH   t |tr| | j|j S t |ttfr@| t| t| S tdS )zcAdd another sequence or string to this sequence.

        Returns a new MutableSeq object.
        Nr   r3   r$   r   r   r   r   r,   r   r   r   rF     s
    
zMutableSeq.__add__c                 C   sH   t |tr| |j| j S t |ttfr@| t|t|  S tdS )zAdd a sequence on the left.

        >>> from Bio.Seq import MutableSeq
        >>> "LV" + MutableSeq("MELKI")
        MutableSeq('LVMELKI')
        Nr   r,   r   r   r   rG   $  s
    
zMutableSeq.__radd__c                 C   s.   t |tstd| jj d| | j| S )a  Multiply MutableSeq by integer.

        Note this is not in-place and returns a new object,
        matching native Python list multiplication.

        >>> from Bio.Seq import MutableSeq
        >>> MutableSeq('ATG') * 2
        MutableSeq('ATGATG')
        rH   rI   r   r>   r   r$   r%   r   r,   r   r   r   rK   4  s    

zMutableSeq.__mul__c                 C   s.   t |tstd| jj d| | j| S )a  Multiply integer by MutableSeq.

        Note this is not in-place and returns a new object,
        matching native Python list multiplication.

        >>> from Bio.Seq import MutableSeq
        >>> 2 * MutableSeq('ATG')
        MutableSeq('ATGATG')
        rH   rI   r   r,   r   r   r   rL   B  s    

zMutableSeq.__rmul__c                 C   s.   t |tstd| jj d| | j| S )zMultiply MutableSeq in-place.

        >>> from Bio.Seq import MutableSeq
        >>> seq = MutableSeq('ATG')
        >>> seq *= 2
        >>> seq
        MutableSeq('ATGATG')
        rH   rI   r   r,   r   r   r   rM   P  s    	
zMutableSeq.__imul__c                 C   s   | j | dS )zAdd a subsequence to the mutable sequence object.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq.append('A')
        >>> my_seq
        MutableSeq('ACTCGACGTCGA')

        No return value.
        N)r   append)r   r   r   r   r   r   ]  s    
zMutableSeq.appendc                 C   s   | j || dS )aD  Add a subsequence to the mutable sequence object at a given index.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq.insert(0,'A')
        >>> my_seq
        MutableSeq('AACTCGACGTCG')
        >>> my_seq.insert(8,'G')
        >>> my_seq
        MutableSeq('AACTCGACGGTCG')

        No return value.
        N)r   insertr   ir   r   r   r   r   i  s    zMutableSeq.insertrh   c                 C   s   | j | }| j |= |S )aV  Remove a subsequence of a single letter at given index.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq.pop()
        'G'
        >>> my_seq
        MutableSeq('ACTCGACGTC')
        >>> my_seq.pop()
        'C'
        >>> my_seq
        MutableSeq('ACTCGACGT')

        Returns the last character of the sequence.
        r   r   r   r   r   popx  s    
zMutableSeq.popc                 C   s<   t t| jD ] }| j| |kr| j|=  dS qtddS )a6  Remove a subsequence of a single letter from mutable sequence.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq.remove('C')
        >>> my_seq
        MutableSeq('ATCGACGTCG')
        >>> my_seq.remove('A')
        >>> my_seq
        MutableSeq('TCGACGTCG')

        No return value.
        Nz#MutableSeq.remove(x): x not in listranger#   r   r   r   itemr   r   r   r   remove  s
    zMutableSeq.remover   c                 C   s   zt |}W n tk
r$   |}Y nX t|t s8tdt|dkrpd}| j|| D ]}||krV|d7 }qV|S t | |||S dS )a  Return a non-overlapping count, like that of a python string.

        This behaves like the python string method of the same name,
        which does a non-overlapping count!

        For an overlapping search use the newer count_overlap() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        e.g.

        >>> from Bio.Seq import MutableSeq
        >>> my_mseq = MutableSeq("AAAATGA")
        >>> print(my_mseq.count("A"))
        5
        >>> print(my_mseq.count("ATG"))
        1
        >>> print(my_mseq.count(Seq("AT")))
        1
        >>> print(my_mseq.count("AT", 2, -1))
        1

        HOWEVER, please note because that python strings, Seq objects and
        MutableSeq objects do a non-overlapping search, this may not give
        the answer you expect:

        >>> "AAAA".count("AA")
        2
        >>> print(MutableSeq("AAAA").count("AA"))
        2

        An overlapping search would give the answer as three!
        z$expected a string, Seq or MutableSeqrT   r   N)r   r   r   r   r#   r   rO   )r   rQ   rR   rS   searchrO   r   r   r   r   rO     s    *


zMutableSeq.countc                 C   sB   t |}t | }d}||||d }|dkr8|d7 }q|S qdS )ag  Return an overlapping count.

        For a non-overlapping search use the count() method.

        Returns an integer, the number of occurrences of substring
        argument sub in the (sub)sequence given by [start:end].
        Optional arguments start and end are interpreted as in slice
        notation.

        Arguments:
         - sub - a string or another Seq object to look for
         - start - optional integer, slice start
         - end - optional integer, slice end

        e.g.

        >>> from Bio.Seq import MutableSeq
        >>> print(MutableSeq("AAAA").count_overlap("AA"))
        3
        >>> print(MutableSeq("ATATATATA").count_overlap("ATA"))
        4
        >>> print(MutableSeq("ATATATATA").count_overlap("ATA", 3, -1))
        1

        Where substrings do not overlap, should behave the same as
        the count() method:

        >>> from Bio.Seq import MutableSeq
        >>> my_mseq = MutableSeq("AAAATGA")
        >>> print(my_mseq.count_overlap("A"))
        5
        >>> my_mseq.count_overlap("A") == my_mseq.count("A")
        True
        >>> print(my_mseq.count_overlap("ATG"))
        1
        >>> my_mseq.count_overlap("ATG") == my_mseq.count("ATG")
        True
        >>> print(my_mseq.count_overlap(Seq("AT")))
        1
        >>> my_mseq.count_overlap(Seq("AT")) == my_mseq.count(Seq("AT"))
        True
        >>> print(my_mseq.count_overlap("AT", 2, -1))
        1
        >>> my_mseq.count_overlap("AT", 2, -1) == my_mseq.count("AT", 2, -1)
        True

        HOWEVER, do not use this method for such cases because the
        count() method is much for efficient.
        r   rT   NrU   rW   r   r   r   rY     s    4
zMutableSeq.count_overlapc                 C   s6   t t| jD ]}| j| |kr|  S qtddS )a  Return first occurrence position of a single entry (i.e. letter).

        >>> my_seq = MutableSeq("ACTCGACGTCG")
        >>> my_seq.index("A")
        0
        >>> my_seq.index("T")
        2
        >>> my_seq.index(Seq("T"))
        2

        Note unlike a Biopython Seq object, or Python string, multi-letter
        subsequences are not supported.  Instead this acts like an array or
        a list of the entries. There is therefore no ``.rindex()`` method.
        z"MutableSeq.index(x): x not in listNr   r   r   r   r   r@     s    
zMutableSeq.indexc                 C   s   | j   dS )zQModify the mutable sequence to reverse itself.

        No return value.
        N)r   reverser&   r   r   r   r   .  s    zMutableSeq.reversec                    s|   d| j krd| j krtdnd| j kr.t}nt}|   dd | D   fdd| j D | _ td| j | _ d	S )
a
  Modify the mutable sequence to take on its complement.

        No return value.

        If the sequence contains neither T nor U, DNA is assumed
        and any A will be mapped to T.

        If the sequence contains both T and U, an exception is raised.
        r}   r   r   c                 s   s"   | ]\}}|  |  fV  qd S r^   )r   )r`   xyr   r   r   rb   F  s     z(MutableSeq.complement.<locals>.<genexpr>c                    s   g | ]} | qS r   r   r   Zmixedr   r   rl   G  s     z)MutableSeq.complement.<locals>.<listcomp>r~   N)r   r   r   r   copyupdateitemsr   )r   dr   r   r   r   5  s    


zMutableSeq.complementc                 C   s   |    | j  dS )zaModify the mutable sequence to take on its reverse complement.

        No return value.
        N)r   r   r   r&   r   r   r   r   J  s    zMutableSeq.reverse_complementc                 C   s>   t |tr$|jD ]}| j| qn|D ]}| j| q(dS )a9  Add a sequence to the original mutable sequence object.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq.extend('A')
        >>> my_seq
        MutableSeq('ACTCGACGTCGA')
        >>> my_seq.extend('TTT')
        >>> my_seq
        MutableSeq('ACTCGACGTCGATTT')

        No return value.
        N)r   r3   r   r   )r   r-   r   r   r   r   extendR  s
    

zMutableSeq.extendc                 C   s   t d| jS )a-  Return the full sequence as a new immutable Seq object.

        >>> from Bio.Seq import MutableSeq
        >>> my_mseq = MutableSeq("MKQHKAMIVALIVICITAVVAAL")
        >>> my_mseq
        MutableSeq('MKQHKAMIVALIVICITAVVAAL')
        >>> my_mseq.toseq()
        Seq('MKQHKAMIVALIVICITAVVAAL')
        r   )r   r	   r   r&   r   r   r   toseqf  s    
zMutableSeq.toseqc                 C   s   |   |S )a  Return a merge of the sequences in other, spaced by the sequence from self.

        Accepts all Seq objects and Strings as objects to be concatenated with the spacer

        >>> concatenated = MutableSeq('NNNNN').join([Seq("AAA"), Seq("TTT"), Seq("PPP")])
        >>> concatenated
        Seq('AAANNNNNTTTNNNNNPPP')

        Throws error if other is not an iterable and if objects inside of the iterable
        are not Seq or String objects
        )r   r	   r,   r   r   r   r	   r  s    zMutableSeq.joinN)rh   )$r%   r   r   r   r   r'   r(   r.   r5   r7   r9   r;   r=   rA   r   r   rF   rG   rK   rL   rM   r   r   r   r   r   r   rO   rY   r@   r   r   r   r   r   r	   r   r   r   r   r3   ]  s>   

=>r3   c                 C   s@   t | tr|  S t | tr(|   S | ddddS dS )zTranscribe a DNA sequence into RNA.

    If given a string, returns a new string object.

    Given a Seq or MutableSeq, returns a new Seq object.

    e.g.

    >>> transcribe("ACTGN")
    'ACUGN'
    r   r}   r   r~   N)r   r   r   r3   r   r   )Zdnar   r   r   r     s
    

r   c                 C   s@   t | tr|  S t | tr(|   S | ddddS dS )zReturn the RNA sequence back-transcribed into DNA.

    If given a string, returns a new string object.

    Given a Seq or MutableSeq, returns a new Seq object.

    e.g.

    >>> back_transcribe("ACUGN")
    'ACTGN'
    r}   r   r~   r   N)r   r   r   r3   r   r   )Zrnar   r   r   r     s
    

r   r   Fr   c              
      s  |   } g }|j |j}|jdk	r2t|j  }	ntt  t   }	t| }
 fdd|D }|r|d }|rtdt| d| d |  dt	
d	t| d
| d |  dt |rft| dd   |jkrtd| dd  d|
d dkrtd|
 dt| dd   |krJtd| dd  d| dd } |
d8 }
dg}n|
d dkrt	
dt |dk	rt|tstdnt|dkrtdtd|
|
d  dD ]}| ||d  }z| |  W n ttjfk
r   ||jkr@|r&tdd|r4Y  q|| nT|	t|r\|| n8|dk	r||d kr|| ntd| ddY nX qd|S )aL	  Translate nucleotide string into a protein string (PRIVATE).

    Arguments:
     - sequence - a string
     - table - a CodonTable object (NOT a table name or id number)
     - stop_symbol - a single character string, what to use for terminators.
     - to_stop - boolean, should translation terminate at the first
       in frame stop codon?  If there is no in-frame stop codon
       then translation continues to the end.
     - pos_stop - a single character string for a possible stop codon
       (e.g. TAN or NNN)
     - cds - Boolean, indicates this is a complete CDS.  If True, this
       checks the sequence starts with a valid alternative start
       codon (which will be translated as methionine, M), that the
       sequence length is a multiple of three, and that there is a
       single in frame stop codon at the end (this will be excluded
       from the protein sequence, regardless of the to_stop option).
       If these tests fail, an exception is raised.
     - gap - Single character string to denote symbol used for gaps.
       Defaults to None.

    Returns a string.

    e.g.

    >>> from Bio.Data import CodonTable
    >>> table = CodonTable.ambiguous_dna_by_id[1]
    >>> _translate_str("AAA", table)
    'K'
    >>> _translate_str("TAR", table)
    '*'
    >>> _translate_str("TAN", table)
    'X'
    >>> _translate_str("TAN", table, pos_stop="@")
    '@'
    >>> _translate_str("TA?", table)
    Traceback (most recent call last):
       ...
    Bio.Data.CodonTable.TranslationError: Codon 'TA?' is invalid

    In a change to older versions of Biopython, partial codons are now
    always regarded as an error (previously only checked if cds=True)
    and will trigger a warning (likely to become an exception in a
    future release).

    If **cds=True**, the start and stop codons are checked, and the start
    codon will be translated at methionine. The sequence must be an
    while number of codons.

    >>> _translate_str("ATGCCCTAG", table, cds=True)
    'MP'
    >>> _translate_str("AAACCCTAG", table, cds=True)
    Traceback (most recent call last):
       ...
    Bio.Data.CodonTable.TranslationError: First codon 'AAA' is not a start codon
    >>> _translate_str("ATGCCCTAGCCCTAG", table, cds=True)
    Traceback (most recent call last):
       ...
    Bio.Data.CodonTable.TranslationError: Extra in frame stop codon found.
    Nc                    s   g | ]}| kr|qS r   r   )r`   r   forward_tabler   r   rl     s      z"_translate_str.<locals>.<listcomp>r   z=You cannot use 'to_stop=True' with this table as it contains z; codon(s) which can be both  STOP and an amino acid (e.g. 'z' -> 'z' or STOP).zThis table contains z? codon(s) which code(s) for both STOP and an amino acid (e.g. 'z9' or STOP). Such codons will be translated as amino acid.r   zFirst codon 'z' is not a start codonzSequence length z is not a multiple of threer   zFinal codon 'z' is not a stop codon   MzPartial codon, len(sequence) not a multiple of three. Explicitly trim the sequence or add trailing N before translation. This may become an error in future.z2Gap character should be a single character string.rT   z Extra in frame stop codon found.zCodon 'z' is invalidr   )rw   r   stop_codonsZnucleotide_alphabetr   _ambiguous_dna_letters_ambiguous_rna_lettersr#   r   warningswarnr   r   Zstart_codonsr   ZTranslationErrorr   r   r   r   KeyError
issupersetr	   )sequencer   r   r   r   Zpos_stopr   Zamino_acidsr   Zvalid_lettersnZdual_codingr   r   Zcodonr   r   r   r     s    ?




r   r   c              	   C   s   t | tr| ||||S t | tr8|  ||||S ztjt| }W nP tk
rh   tj	| }Y n4 t
tfk
r   t |tjr|}n
tddY nX t| |||||dS dS )a  Translate a nucleotide sequence into amino acids.

    If given a string, returns a new string object. Given a Seq or
    MutableSeq, returns a Seq object.

    Arguments:
     - table - Which codon table to use?  This can be either a name
       (string), an NCBI identifier (integer), or a CodonTable object
       (useful for non-standard genetic codes).  Defaults to the "Standard"
       table.
     - stop_symbol - Single character string, what to use for any
       terminators, defaults to the asterisk, "*".
     - to_stop - Boolean, defaults to False meaning do a full
       translation continuing on past any stop codons
       (translated as the specified stop_symbol).  If
       True, translation is terminated at the first in
       frame stop codon (and the stop_symbol is not
       appended to the returned protein sequence).
     - cds - Boolean, indicates this is a complete CDS.  If True, this
       checks the sequence starts with a valid alternative start
       codon (which will be translated as methionine, M), that the
       sequence length is a multiple of three, and that there is a
       single in frame stop codon at the end (this will be excluded
       from the protein sequence, regardless of the to_stop option).
       If these tests fail, an exception is raised.
     - gap - Single character string to denote symbol used for gaps.
       Defaults to None.

    A simple string example using the default (standard) genetic code:

    >>> coding_dna = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG"
    >>> translate(coding_dna)
    'VAIVMGR*KGAR*'
    >>> translate(coding_dna, stop_symbol="@")
    'VAIVMGR@KGAR@'
    >>> translate(coding_dna, to_stop=True)
    'VAIVMGR'

    Now using NCBI table 2, where TGA is not a stop codon:

    >>> translate(coding_dna, table=2)
    'VAIVMGRWKGAR*'
    >>> translate(coding_dna, table=2, to_stop=True)
    'VAIVMGRWKGAR'

    In fact this example uses an alternative start codon valid under NCBI
    table 2, GTG, which means this example is a complete valid CDS which
    when translated should really start with methionine (not valine):

    >>> translate(coding_dna, table=2, cds=True)
    'MAIVMGRWKGAR'

    Note that if the sequence has no in-frame stop codon, then the to_stop
    argument has no effect:

    >>> coding_dna2 = "GTGGCCATTGTAATGGGCCGC"
    >>> translate(coding_dna2)
    'VAIVMGR'
    >>> translate(coding_dna2, to_stop=True)
    'VAIVMGR'

    NOTE - Ambiguous codons like "TAN" or "NNN" could be an amino acid
    or a stop codon.  These are translated as "X".  Any invalid codon
    (e.g. "TA?" or "T-A") will throw a TranslationError.

    It will however translate either DNA or RNA.

    NOTE - Since version 1.71 Biopython contains codon tables with 'ambiguous
    stop codons'. These are stop codons with unambiguous sequence but which
    have a context dependent coding as STOP or as amino acid. With these tables
    'to_stop' must be False (otherwise a ValueError is raised). The dual
    coding codons will always be translated as amino acid, except for
    'cds=True', where the last codon will be translated as STOP.

    >>> coding_dna3 = "ATGGCACGGAAGTGA"
    >>> translate(coding_dna3)
    'MARK*'

    >>> translate(coding_dna3, table=27)  # Table 27: TGA -> STOP or W
    'MARKW'

    It will however raise a BiopythonWarning (not shown).

    >>> translate(coding_dna3, table=27, cds=True)
    'MARK'

    >>> translate(coding_dna3, table=27, to_stop=True)
    Traceback (most recent call last):
       ...
    ValueError: You cannot use 'to_stop=True' with this table ...
    r   Nr   )r   r   r   r3   r   r   r   r>   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   D	  s    ^

r   c                 C   s   t | ddd S )a  Return the reverse complement sequence of a nucleotide string.

    If given a string, returns a new string object.
    Given a Seq or a MutableSeq, returns a new Seq object.

    Supports unambiguous and ambiguous nucleotide sequences.

    e.g.

    >>> reverse_complement("ACTG-NH")
    'DN-CAGT'

    If neither T nor U is present, DNA is assumed and A is mapped to T:

    >>> reverse_complement("A")
    'T'
    Nrh   r   r   r   r   r   r   	  s    r   c                 C   sv   t | tr|  S t | tr(|   S d| ks8d| krRd| ksHd| krRtdnd| ksbd| krht}nt}| |S )a  Return the complement sequence of a DNA string.

    If given a string, returns a new string object.

    Given a Seq or a MutableSeq, returns a new Seq object.

    Supports unambiguous and ambiguous nucleotide sequences.

    e.g.

    >>> complement("ACTG-NH")
    'TGAC-ND'

    If neither T nor U is present, DNA is assumed and A is mapped to T:

    >>> complement("A")
    'T'

    However, this may not be supported in future. Please use the
    complement_rna function if you have RNA.
    r}   r~   r   r   r   )	r   r   r   r3   r   r   r   r   r   )r   r   r   r   r   r   	  s    

 
r   c                 C   sd   t | tr|  S t | tr(|   S d| ks8d| krZd| ksHd| krRtdntd| tS )zReturn the complement sequence of an RNA string.

    >>> complement("ACG")  # assumed DNA
    'TGC'
    >>> complement_rna("ACG")
    'UGC'

    If the sequence contains a T, and error is raised.
    r   r   r}   r~   r   zDNA found, expect RNA)r   r   r   r3   r   r   r   r   r   r   r   r   r   	  s    



r   c                  C   s*   t d ddl} | j| jd t d dS )z,Run the Bio.Seq module's doctests (PRIVATE).zRunning doctests...r   N)ZoptionflagsZDone)printdoctestZtestmodZIGNORE_EXCEPTION_DETAIL)r   r   r   r   _test
  s    r   __main__)r   FFr   N)r   r   FFN)r   r   r   r   ZBior   ZBio.Data.IUPACDatar   r   r   r   r   r   ZBio.Datar   r   r   r   r   r   r3   r   r   r   r   r   r   r   r   r%   r   r   r   r   <module>   sf               *    .         
          
q.	
