B
    b                @   sV  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddl	mZ d	d
 ZeejZeejZed ed< eeZ[G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zdd Zd0d"d#Zd1d%d&Zd'd( Zd)d* Zd+d, Z d-d. Z!e"d/krRe!  dS )2aP  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)ABC)abstractmethod)BiopythonDeprecationWarning)BiopythonWarning)
CodonTable)	IUPACDatac             C   sD   d |  d}d |  d}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.
     ASCII)joinkeysencodevaluesbytes	maketranslower)Zcomplement_mappingr   r    r   &lib/python3.7/site-packages/Bio/Seq.py
_maketrans"   s    r   UTc               @   s0  e Zd ZdZdZdd Zedd Ze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dEd d!ZdFd#d$ZdGd%d&ZdHd'd(ZdId)d*ZdJd+d,ZdKd-d.ZdLd/d0ZdMd2d3ZdNd4d5ZdOd6d7ZdPd8d9ZdQd:d;Z d<d= Z!d>d? Z"d@dA Z#dRdCdDZ$d"S )SSequenceDataAbstractBaseClassa[  Abstract base class for sequence content providers.

    Most users will not need to use this class. It is used internally as a base
    class for sequence content provider classes such as _UndefinedSequenceData
    defined in this module, and _TwoBitSequenceData in Bio.SeqIO.TwoBitIO.
    Instances of these classes can be used instead of a ``bytes`` object as the
    data argument when creating a Seq object, and provide the sequence content
    only when requested via ``__getitem__``. This allows lazy parsers to load
    and parse sequence data from a file only for the requested sequence regions,
    and _UndefinedSequenceData instances to raise an exception when undefined
    sequence data are requested.

    Future implementations of lazy parsers that similarly provide on-demand
    parsing of sequence data should use a subclass of this abstract class and
    implement the abstract methods ``__len__`` and ``__getitem__``:

    * ``__len__`` must return the sequence length;
    * ``__getitem__`` must return

      * a ``bytes`` object for the requested region; or
      * a new instance of the subclass for the requested region; or
      * raise an ``UndefinedSequenceError``.

      Calling ``__getitem__`` for a sequence region of size zero should always
      return an empty ``bytes`` object.
      Calling ``__getitem__`` for the full sequence (as in data[:]) should
      either return a ``bytes`` object with the full sequence, or raise an
      ``UndefinedSequenceError``.

    Subclasses of SequenceDataAbstractBaseClass must call ``super().__init__()``
    as part of their ``__init__`` method.
    r   c             C   s   | dd dkst dS )z5Check if ``__getitem__`` returns a bytes-like object.Nr       )AssertionError)selfr   r   r   __init__`   s    z&SequenceDataAbstractBaseClass.__init__c             C   s   d S )Nr   )r   r   r   r   __len__d   s    z%SequenceDataAbstractBaseClass.__len__c             C   s   d S )Nr   )r   keyr   r   r   __getitem__h   s    z)SequenceDataAbstractBaseClass.__getitem__c             C   s   | d d  S )Nr   )r   r   r   r   	__bytes__l   s    z'SequenceDataAbstractBaseClass.__bytes__c             C   s   t t| S )N)hashr   )r   r   r   r   __hash__o   s    z&SequenceDataAbstractBaseClass.__hash__c             C   s   t | |kS )N)r   )r   otherr   r   r   __eq__r   s    z$SequenceDataAbstractBaseClass.__eq__c             C   s   t | |k S )N)r   )r   r!   r   r   r   __lt__u   s    z$SequenceDataAbstractBaseClass.__lt__c             C   s   t | |kS )N)r   )r   r!   r   r   r   __le__x   s    z$SequenceDataAbstractBaseClass.__le__c             C   s   t | |kS )N)r   )r   r!   r   r   r   __gt__{   s    z$SequenceDataAbstractBaseClass.__gt__c             C   s   t | |kS )N)r   )r   r!   r   r   r   __ge__~   s    z$SequenceDataAbstractBaseClass.__ge__c             C   s   t | | S )N)r   )r   r!   r   r   r   __add__   s    z%SequenceDataAbstractBaseClass.__add__c             C   s   |t |  S )N)r   )r   r!   r   r   r   __radd__   s    z&SequenceDataAbstractBaseClass.__radd__c             C   s   t | | S )N)r   )r   r!   r   r   r   __mul__   s    z%SequenceDataAbstractBaseClass.__mul__c             C   s   t | |S )N)r   __contains__)r   itemr   r   r   r*      s    z*SequenceDataAbstractBaseClass.__contains__utf-8c             C   s   t | |S )zDecode the data as bytes using the codec registered for encoding.

        encoding
          The encoding with which to decode the bytes.
        )r   decode)r   encodingr   r   r   r-      s    z$SequenceDataAbstractBaseClass.decodeNc             C   s   t | |||S )zReturn the number of non-overlapping occurrences of sub in data[start:end].

        Optional arguments start and end are interpreted as in slice notation.
        )r   count)r   substartendr   r   r   r/      s    z#SequenceDataAbstractBaseClass.countc             C   s   t | |||S )a9  Return the lowest index in data where subsection sub is found.

        Return the lowest index in data where subsection sub is found,
        such that sub is contained within data[start,end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        )r   find)r   r0   r1   r2   r   r   r   r3      s    	z"SequenceDataAbstractBaseClass.findc             C   s   t | |||S )a;  Return the highest index in data where subsection sub is found.

        Return the highest index in data where subsection sub is found,
        such that sub is contained within data[start,end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        )r   rfind)r   r0   r1   r2   r   r   r   r4      s    	z#SequenceDataAbstractBaseClass.rfindc             C   s   t | |||S )aW  Return the lowest index in data where subsection sub is found.

        Return the lowest index in data where subsection sub is found,
        such that sub is contained within data[start,end].  Optional
        arguments start and end are interpreted as in slice notation.

        Raises ValueError when the subsection is not found.
        )r   index)r   r0   r1   r2   r   r   r   r5      s    	z#SequenceDataAbstractBaseClass.indexc             C   s   t | |||S )aX  Return the highest index in data where subsection sub is found.

        Return the highest index in data where subsection sub is found,
        such that sub is contained within data[start,end].  Optional
        arguments start and end are interpreted as in slice notation.

        Raise ValueError when the subsection is not found.
        )r   rindex)r   r0   r1   r2   r   r   r   r6      s    	z$SequenceDataAbstractBaseClass.rindexc             C   s   t | |||S )a  Return True if data starts with the specified prefix, False otherwise.

        With optional start, test data beginning at that position.
        With optional end, stop comparing data at that position.
        prefix can also be a tuple of bytes to try.
        )r   
startswith)r   prefixr1   r2   r   r   r   r7      s    z(SequenceDataAbstractBaseClass.startswithc             C   s   t | |||S )a  Return True if data ends with the specified suffix, False otherwise.

        With optional start, test data beginning at that position.
        With optional end, stop comparing data at that position.
        suffix can also be a tuple of bytes to try.
        )r   endswith)r   suffixr1   r2   r   r   r   r9      s    z&SequenceDataAbstractBaseClass.endswithc             C   s   t | ||S )a  Return a list of the sections in the data, using sep as the delimiter.

        sep
          The delimiter according which to split the data.
          None (the default value) means split on ASCII whitespace characters
          (space, tab, return, newline, formfeed, vertical tab).
        maxsplit
          Maximum number of splits to do.
          -1 (the default value) means no limit.
        )r   split)r   sepmaxsplitr   r   r   r<      s    z#SequenceDataAbstractBaseClass.splitc             C   s   t | ||S )a  Return a list of the sections in the data, using sep as the delimiter.

        sep
          The delimiter according which to split the data.
          None (the default value) means split on ASCII whitespace characters
          (space, tab, return, newline, formfeed, vertical tab).
        maxsplit
          Maximum number of splits to do.
          -1 (the default value) means no limit.

        Splitting is done starting at the end of the data and working to the front.
        )r   rsplit)r   r=   r>   r   r   r   r?      s    z$SequenceDataAbstractBaseClass.rsplitc             C   s   t | |S )zStrip leading and trailing characters contained in the argument.

        If the argument is omitted or None, strip leading and trailing ASCII whitespace.
        )r   strip)r   charsr   r   r   r@      s    z#SequenceDataAbstractBaseClass.stripc             C   s   t | |S )zStrip leading characters contained in the argument.

        If the argument is omitted or None, strip leading ASCII whitespace.
        )r   lstrip)r   rA   r   r   r   rB      s    z$SequenceDataAbstractBaseClass.lstripc             C   s   t | |S )zStrip trailing characters contained in the argument.

        If the argument is omitted or None, strip trailing ASCII whitespace.
        )r   rstrip)r   rA   r   r   r   rC     s    z$SequenceDataAbstractBaseClass.rstripc             C   s   t |  S )zGReturn a copy of data with all ASCII characters converted to uppercase.)r   upper)r   r   r   r   rD     s    z#SequenceDataAbstractBaseClass.upperc             C   s   t |  S )zGReturn a copy of data with all ASCII characters converted to lowercase.)r   r   )r   r   r   r   r     s    z#SequenceDataAbstractBaseClass.lowerc             C   s   t | ||S )zDReturn a copy with all occurrences of substring old replaced by new.)r   replace)r   oldnewr   r   r   rE     s    z%SequenceDataAbstractBaseClass.replacer   c             C   s   t | |S )aM  Return a copy with each character mapped by the given translation table.

          table
            Translation table, which must be a bytes object of length 256.

        All characters occurring in the optional argument delete are removed.
        The remaining characters are mapped through the given translation table.
        )r   	translate)r   tabledeleter   r   r   rH     s    	z'SequenceDataAbstractBaseClass.translate)r,   )NN)NN)NN)NN)NN)NN)NN)Nr;   )Nr;   )N)N)N)r   )%__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   r*   r-   r/   r3   r4   r5   r6   r7   r9   r<   r?   r@   rB   rC   rD   r   rE   rH   r   r   r   r   r   <   sB    






	
	




r   c               @   sz  e Zd ZdZdZe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dWd$d%ZdXd&d'Zd(d) ZdYd*d+ZdZd,d-Zd[d.d/Zd\d0d1Zd]d2d3Zd^d4d5Zd_d7d8Zd`d9d:Z dad<d=Z!dbd>d?Z"dcd@dAZ#dddBdCZ$dedDdEZ%dfdIdJZ&dgdKdLZ'dhdMdNZ(didOdPZ)djdQdRZ*dSdT Z+dkdUdVZ,d#S )l_SeqAbstractBaseClasszAbstract base class for the Seq and MutableSeq classes (PRIVATE).

    Most users will not need to use this class. It is used internally as an
    abstract base class for Seq and MutableSeq, as most of their methods are
    identical.
    )_datac             C   s   d S )Nr   )r   r   r   r   r   -  s    z_SeqAbstractBaseClass.__init__c             C   s
   t | jS )N)r   rQ   )r   r   r   r   r   1  s    z_SeqAbstractBaseClass.__bytes__c             C   s   | j }t|tr dt|  dS t|dkrj|dd d}|dd d}| jj d| d	| d
S |d}| jj d| d
S dS )z2Return (truncated) representation of the sequence.zSeq(None, length=)<   N6   r	   z('z...z'))rQ   
isinstance_UndefinedSequenceDatalenr-   	__class__rK   )r   datar1   r2   r   r   r   __repr__4  s    

z_SeqAbstractBaseClass.__repr__c             C   s   | j dS )z,Return the full sequence as a python string.r	   )rQ   r-   )r   r   r   r   __str__D  s    z_SeqAbstractBaseClass.__str__c             C   s>   t |tr| j|jkS t |tr0| j|dkS | j|kS dS )aL  Compare the sequence to another sequence or a string.

        Sequences are equal to each other if their sequence contents is
        identical:

        >>> from Bio.Seq import Seq, MutableSeq
        >>> seq1 = Seq("ACGT")
        >>> seq2 = Seq("ACGT")
        >>> mutable_seq = MutableSeq("ACGT")
        >>> seq1 == seq2
        True
        >>> seq1 == mutable_seq
        True
        >>> seq1 == "ACGT"
        True

        Note that the sequence objects themselves are not identical to each
        other:

        >>> id(seq1) == id(seq2)
        False
        >>> seq1 is seq2
        False

        Sequences can also be compared to strings, ``bytes``, and ``bytearray``
        objects:

        >>> seq1 == "ACGT"
        True
        >>> seq1 == b"ACGT"
        True
        >>> seq1 == bytearray(b"ACGT")
        True
        r	   N)rV   rP   rQ   strr   )r   r!   r   r   r   r"   H  s
    #

z_SeqAbstractBaseClass.__eq__c             C   s>   t |tr| j|jk S t |tr0| j|dk S | j|k S dS )z Implement the less-than operand.r	   N)rV   rP   rQ   r]   r   )r   r!   r   r   r   r#   r  s
    

z_SeqAbstractBaseClass.__lt__c             C   s>   t |tr| j|jkS t |tr0| j|dkS | j|kS dS )z)Implement the less-than or equal operand.r	   N)rV   rP   rQ   r]   r   )r   r!   r   r   r   r$   {  s
    

z_SeqAbstractBaseClass.__le__c             C   s>   t |tr| j|jkS t |tr0| j|dkS | j|kS dS )z#Implement the greater-than operand.r	   N)rV   rP   rQ   r]   r   )r   r!   r   r   r   r%     s
    

z_SeqAbstractBaseClass.__gt__c             C   s>   t |tr| j|jkS t |tr0| j|dkS | j|kS dS )z,Implement the greater-than or equal operand.r	   N)rV   rP   rQ   r]   r   )r   r!   r   r   r   r&     s
    

z_SeqAbstractBaseClass.__ge__c             C   s
   t | jS )z"Return the length of the sequence.)rX   rQ   )r   r   r   r   r     s    z_SeqAbstractBaseClass.__len__c             C   s,   t |trt| j| S | | j| S dS )a  Return a subsequence as a single letter or as a sequence object.

        If the index is an integer, a single letter is returned as a Python
        string:

        >>> seq = Seq('ACTCGACGTCG')
        >>> seq[5]
        'A'

        Otherwise, a new sequence object of the same class is returned:

        >>> seq[5:8]
        Seq('ACG')
        >>> mutable_seq = MutableSeq('ACTCGACGTCG')
        >>> mutable_seq[5:8]
        MutableSeq('ACG')
        N)rV   intchrrQ   rY   )r   r5   r   r   r   r     s    
z!_SeqAbstractBaseClass.__getitem__c             C   s^   t |tr| | j|j S t |tr<| | j|d S ddlm} t ||rVtS t	dS )zAdd a sequence or string to this sequence.

        >>> from Bio.Seq import Seq, MutableSeq
        >>> Seq("MELKI") + "LV"
        Seq('MELKILV')
        >>> MutableSeq("MELKI") + "LV"
        MutableSeq('MELKILV')
        r	   r   )	SeqRecordN)
rV   rP   rY   rQ   r]   r   Bio.SeqRecordr`   NotImplemented	TypeError)r   r!   r`   r   r   r   r'     s    	


z_SeqAbstractBaseClass.__add__c             C   s(   t |tr | |d| j S tdS )a   Add a sequence string on the left.

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

        Adding two sequence objects is handled via the __add__ method.
        r	   N)rV   r]   rY   r   rQ   rc   )r   r!   r   r   r   r(     s    
z_SeqAbstractBaseClass.__radd__c             C   s.   t |tstd| jj d| | j| S )zMultiply sequence by integer.

        >>> from Bio.Seq import Seq, MutableSeq
        >>> Seq('ATG') * 2
        Seq('ATGATG')
        >>> MutableSeq('ATG') * 2
        MutableSeq('ATGATG')
        zcan't multiply z by non-int type)rV   r^   rc   rY   rK   rQ   )r   r!   r   r   r   r)     s    	
z_SeqAbstractBaseClass.__mul__c             C   s.   t |tstd| jj d| | j| S )z|Multiply integer by sequence.

        >>> from Bio.Seq import Seq
        >>> 2 * Seq('ATG')
        Seq('ATGATG')
        zcan't multiply z by non-int type)rV   r^   rc   rY   rK   rQ   )r   r!   r   r   r   __rmul__  s    
z_SeqAbstractBaseClass.__rmul__c             C   s.   t |tstd| jj d| | j| S )av  Multiply the sequence object by other and assign.

        >>> from Bio.Seq import Seq
        >>> seq = Seq('ATG')
        >>> seq *= 2
        >>> seq
        Seq('ATGATG')

        Note that this is different from in-place multiplication. The ``seq``
        variable is reassigned to the multiplication result, but any variable
        pointing to ``seq`` will remain unchanged:

        >>> seq = Seq('ATG')
        >>> seq2 = seq
        >>> id(seq) == id(seq2)
        True
        >>> seq *= 2
        >>> seq
        Seq('ATGATG')
        >>> seq2
        Seq('ATG')
        >>> id(seq) == id(seq2)
        False
        zcan't multiply z by non-int type)rV   r^   rc   rY   rK   rQ   )r   r!   r   r   r   __imul__  s    
z_SeqAbstractBaseClass.__imul__Nc             C   sj   t |tr|j}nHt |tr&t|}n4t |tr<|d}nt |ttfsZtdt	| | j
|||S )a  Return a non-overlapping count, like that of a python string.

        The number of occurrences of substring argument sub in the
        (sub)sequence given by [start:end] is returned as an integer.
        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 the ``count`` method of Seq and MutableSeq
        objects, like that of Python strings, do a non-overlapping search, this
        may not give the answer you expect:

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

        For an overlapping search, use the ``count_overlap`` method:

        >>> print(Seq("AAAA").count_overlap("AA"))
        3
        r	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s')rV   
MutableSeqrQ   Seqr   r]   r   	bytearrayrc   typer/   )r   r0   r1   r2   r   r   r   r/     s    (



z_SeqAbstractBaseClass.countc             C   s   t |tr|j}nHt |tr&t|}n4t |tr<|d}nt |ttfsZtdt	| | j}d}x,|
|||d }|dkr|d7 }qf|S qfW dS )aw  Return an overlapping count.

        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

        For a non-overlapping search, use the ``count`` method:

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

        Where substrings do not overlap, ``count_overlap`` behaves 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	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s'r      N)rV   rf   rQ   rg   r   r]   r   rh   rc   ri   r3   )r   r0   r1   r2   rZ   Zoverlap_countr   r   r   count_overlapC  s"    5




z#_SeqAbstractBaseClass.count_overlapc             C   s2   t |trt|}nt |tr(|d}|| jkS )aK  Return True if item is a subsequence of the sequence, and False otherwise.

        e.g.

        >>> from Bio.Seq import Seq, MutableSeq
        >>> my_dna = Seq("ATATGAAATTTGAAAA")
        >>> "AAA" in my_dna
        True
        >>> Seq("AAA") in my_dna
        True
        >>> MutableSeq("AAA") in my_dna
        True
        r	   )rV   rP   r   r]   r   rQ   )r   r+   r   r   r   r*     s
    



z"_SeqAbstractBaseClass.__contains__c             C   sX   t |trt|}n4t |tr*|d}nt |ttfsHtdt| | j	|||S )ay  Return the lowest index in the sequence where subsequence sub is found.

        With optional arguments start and end, return the lowest index in the
        sequence such that the subsequence sub is contained within the sequence
        region [start:end].

        Arguments:
         - sub - a string or another Seq or MutableSeq object to search 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

        The next typical start codon can then be found by starting the search
        at position 4:

        >>> my_rna.find("AUG", 4)
        15
        r	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s')
rV   rP   r   r]   r   rh   rc   ri   rQ   r3   )r   r0   r1   r2   r   r   r   r3     s    


z_SeqAbstractBaseClass.findc             C   sX   t |trt|}n4t |tr*|d}nt |ttfsHtdt| | j	|||S )a  Return the highest index in the sequence where subsequence sub is found.

        With optional arguments start and end, return the highest index in the
        sequence such that the subsequence sub is contained within the sequence
        region [start:end].

        Arguments:
         - sub - a string or another Seq or MutableSeq object to search 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

        The location of the typical start codon before that can be found by
        ending the search at positon 15:

        >>> my_rna.rfind("AUG", end=15)
        3
        r	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s')
rV   rP   r   r]   r   rh   rc   ri   rQ   r4   )r   r0   r1   r2   r   r   r   r4     s    


z_SeqAbstractBaseClass.rfindc             C   sj   t |tr|j}nHt |tr&t|}n4t |tr<|d}nt |ttfsZtdt	| | j
|||S )a  Return the lowest index in the sequence where subsequence sub is found.

        With optional arguments start and end, return the lowest index in the
        sequence such that the subsequence sub is contained within the sequence
        region [start:end].

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

        Raises a ValueError 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.index("AUG")
        3

        The next typical start codon can then be found by starting the search
        at position 4:

        >>> my_rna.index("AUG", 4)
        15

        This method performs the same search as the ``find`` method.  However,
        if the subsequence is not found, ``find`` returns -1 which ``index``
        raises a ValueError:

        >>> my_rna.index("T")
        Traceback (most recent call last):
                   ...
        ValueError: ...
        >>> my_rna.find("T")
        -1
        r	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s')rV   rf   rQ   rg   r   r]   r   rh   rc   ri   r5   )r   r0   r1   r2   r   r   r   r5     s    &



z_SeqAbstractBaseClass.indexc             C   sj   t |tr|j}nHt |tr&t|}n4t |tr<|d}nt |ttfsZtdt	| | j
|||S )a  Return the highest index in the sequence where subsequence sub is found.

        With optional arguments start and end, return the highest index in the
        sequence such that the subsequence sub is contained within the sequence
        region [start:end].

        Arguments:
         - sub - a string or another Seq or MutableSeq object to search 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.rindex("AUG")
        15

        The location of the typical start codon before that can be found by
        ending the search at positon 15:

        >>> my_rna.rindex("AUG", end=15)
        3

        This method performs the same search as the ``rfind`` method.  However,
        if the subsequence is not found, ``rfind`` returns -1 which ``rindex``
        raises a ValueError:

        >>> my_rna.rindex("T")
        Traceback (most recent call last):
                   ...
        ValueError: ...
        >>> my_rna.rfind("T")
        -1
        r	   zHa Seq, MutableSeq, str, bytes, or bytearray object is required, not '%s')rV   rf   rQ   rg   r   r]   r   rh   rc   ri   r6   )r   r0   r1   r2   r   r   r   r6     s    &



z_SeqAbstractBaseClass.rindexc             C   sV   t |trtdd |D }n(t |tr2t|}nt |trF|d}| j|||S )a  Return True if the sequence starts with the given prefix, False otherwise.

        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 |trt|n|d V  qdS )r	   N)rV   rP   r   r   ).0pr   r   r   	<genexpr>h  s   z3_SeqAbstractBaseClass.startswith.<locals>.<genexpr>r	   )rV   tuplerP   r   r]   r   rQ   r7   )r   r8   r1   r2   r   r   r   r7   R  s    




z _SeqAbstractBaseClass.startswithc             C   sV   t |trtdd |D }n(t |tr2t|}nt |trF|d}| j|||S )a  Return True if the sequence ends with the given suffix, False otherwise.

        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 |trt|n|d V  qdS )r	   N)rV   rP   r   r   )rl   rm   r   r   r   rn     s   z1_SeqAbstractBaseClass.endswith.<locals>.<genexpr>r	   )rV   ro   rP   r   r]   r   rQ   r9   )r   r:   r1   r2   r   r   r   r9   q  s    




z_SeqAbstractBaseClass.endswithr;   c             C   s@   t |trt|}nt |tr(|d}dd | j||D S )a}  Return a list of subsequences when splitting the sequence by separator sep.

        Return a list of the subsequences in the sequence (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.

        For consistency with the ``split`` method of Python strings, any
        whitespace (tabs, spaces, newlines) is a separator if sep is None, the
        default value

        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, which splits the sequence starting from the
        end:

        >>> for pep in my_aa.rsplit("*", 1):
        ...     pep
        Seq('VMAIVMGR*KGAR')
        Seq('L')
        r	   c             S   s   g | ]}t |qS r   )rg   )rl   partr   r   r   
<listcomp>  s    z/_SeqAbstractBaseClass.split.<locals>.<listcomp>)rV   rP   r   r]   r   rQ   r<   )r   r=   r>   r   r   r   r<     s
    %



z_SeqAbstractBaseClass.splitc             C   s@   t |trt|}nt |tr(|d}dd | j||D S )a  Return a list of subsequences by splitting the sequence from the right.

        Return a list of the subsequences in the sequence (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.

        For consistency with the ``rsplit`` method of Python strings, any
        whitespace (tabs, spaces, newlines) is a separator if sep is None, the
        default value

        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.rsplit("*"):
        ...     pep
        Seq('VMAIVMGR')
        Seq('KGAR')
        Seq('L')
        >>> for pep in my_aa.rsplit("*", 1):
        ...     pep
        Seq('VMAIVMGR*KGAR')
        Seq('L')

        See also the split method, which splits the sequence starting from the
        beginning:

        >>> for pep in my_aa.split("*", 1):
        ...     pep
        Seq('VMAIVMGR')
        Seq('KGAR*L')
        r	   c             S   s   g | ]}t |qS r   )rg   )rl   rp   r   r   r   rq     s    z0_SeqAbstractBaseClass.rsplit.<locals>.<listcomp>)rV   rP   r   r]   r   rQ   r?   )r   r=   r>   r   r   r   r?     s
    %



z_SeqAbstractBaseClass.rsplitFc             C   s   t |trt|}nt |tr(|d}y| j|}W n tk
rV   tddY nX |rt | jtsptd|| jdd< | S t | t	rt
|S | |S dS )a\  Return a sequence object with leading and trailing ends stripped.

        With default arguments, leading and trailing whitespace is removed:

        >>> seq = Seq(" ACGT ")
        >>> seq.strip()
        Seq('ACGT')
        >>> seq
        Seq(' ACGT ')

        If ``chars`` is given and not ``None``, remove characters in ``chars``
        instead.  The order of the characters to be removed is not important:

        >>> Seq("ACGTACGT").strip("TGCA")
        Seq('')

        A copy of the sequence is returned if ``inplace`` is ``False`` (the
        default value).  If ``inplace`` is ``True``, the sequence is stripped
        in-place and returned.

        >>> seq = MutableSeq(" ACGT ")
        >>> seq.strip(inplace=False)
        MutableSeq('ACGT')
        >>> seq
        MutableSeq(' ACGT ')
        >>> seq.strip(inplace=True)
        MutableSeq('ACGT')
        >>> seq
        MutableSeq('ACGT')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if ``strip``
        is called on a ``Seq`` object with ``inplace=True``.

        See also the lstrip and rstrip methods.
        r	   zHargument must be None or a string, Seq, MutableSeq, or bytes-like objectNzSequence is immutable)rV   rP   r   r]   r   rQ   r@   rc   rh   
UnknownSeqrg   rY   )r   rA   inplacerZ   r   r   r   r@     s$    $





z_SeqAbstractBaseClass.stripc             C   s   t |trt|}nt |tr(|d}y| j|}W n tk
rV   tddY nX |rt | jtsptd|| jdd< | S t | t	rt
|S | |S dS )a{  Return a sequence object with leading and trailing ends stripped.

        With default arguments, leading whitespace is removed:

        >>> seq = Seq(" ACGT ")
        >>> seq.lstrip()
        Seq('ACGT ')
        >>> seq
        Seq(' ACGT ')

        If ``chars`` is given and not ``None``, remove characters in ``chars``
        from the leading end instead.  The order of the characters to be removed
        is not important:

        >>> Seq("ACGACGTTACG").lstrip("GCA")
        Seq('TTACG')

        A copy of the sequence is returned if ``inplace`` is ``False`` (the
        default value).  If ``inplace`` is ``True``, the sequence is stripped
        in-place and returned.

        >>> seq = MutableSeq(" ACGT ")
        >>> seq.lstrip(inplace=False)
        MutableSeq('ACGT ')
        >>> seq
        MutableSeq(' ACGT ')
        >>> seq.lstrip(inplace=True)
        MutableSeq('ACGT ')
        >>> seq
        MutableSeq('ACGT ')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``lstrip`` is called on a ``Seq`` object with ``inplace=True``.

        See also the strip and rstrip methods.
        r	   zHargument must be None or a string, Seq, MutableSeq, or bytes-like objectNzSequence is immutable)rV   rP   r   r]   r   rQ   rB   rc   rh   rr   rg   rY   )r   rA   rs   rZ   r   r   r   rB     s$    %





z_SeqAbstractBaseClass.lstripc             C   s   t |trt|}nt |tr(|d}y| j|}W n tk
rV   tddY nX |rt | jtsptd|| jdd< | S t | t	rt
|S | |S dS )at  Return a sequence object with trailing ends stripped.

        With default arguments, trailing whitespace is removed:

        >>> seq = Seq(" ACGT ")
        >>> seq.rstrip()
        Seq(' ACGT')
        >>> seq
        Seq(' ACGT ')

        If ``chars`` is given and not ``None``, remove characters in ``chars``
        from the trailing end instead.  The order of the characters to be
        removed is not important:

        >>> Seq("ACGACGTTACG").rstrip("GCA")
        Seq('ACGACGTT')

        A copy of the sequence is returned if ``inplace`` is ``False`` (the
        default value).  If ``inplace`` is ``True``, the sequence is stripped
        in-place and returned.

        >>> seq = MutableSeq(" ACGT ")
        >>> seq.rstrip(inplace=False)
        MutableSeq(' ACGT')
        >>> seq
        MutableSeq(' ACGT ')
        >>> seq.rstrip(inplace=True)
        MutableSeq(' ACGT')
        >>> seq
        MutableSeq(' ACGT')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``rstrip`` is called on a ``Seq`` object with ``inplace=True``.

        See also the strip and lstrip methods.
        r	   zHargument must be None or a string, Seq, MutableSeq, or bytes-like objectNzSequence is immutable)rV   rP   r   r]   r   rQ   rC   rc   rh   rr   rg   rY   )r   rA   rs   rZ   r   r   r   rC   W  s$    %





z_SeqAbstractBaseClass.rstripc             C   sB   | j  }|r4t| j ts"td|| j dd< | S | |S dS )a  Return the sequence in upper case.

        An upper-case copy of the sequence is returned if inplace is False,
        the default value:

        >>> from Bio.Seq import Seq, MutableSeq
        >>> my_seq = Seq("VHLTPeeK*")
        >>> my_seq
        Seq('VHLTPeeK*')
        >>> my_seq.lower()
        Seq('vhltpeek*')
        >>> my_seq.upper()
        Seq('VHLTPEEK*')
        >>> my_seq
        Seq('VHLTPeeK*')

        The sequence is modified in-place and returned if inplace is True:

        >>> my_seq = MutableSeq("VHLTPeeK*")
        >>> my_seq
        MutableSeq('VHLTPeeK*')
        >>> my_seq.lower()
        MutableSeq('vhltpeek*')
        >>> my_seq.upper()
        MutableSeq('VHLTPEEK*')
        >>> my_seq
        MutableSeq('VHLTPeeK*')

        >>> my_seq.lower(inplace=True)
        MutableSeq('vhltpeek*')
        >>> my_seq
        MutableSeq('vhltpeek*')
        >>> my_seq.upper(inplace=True)
        MutableSeq('VHLTPEEK*')
        >>> my_seq
        MutableSeq('VHLTPEEK*')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``upper`` is called on a ``Seq`` object with ``inplace=True``.

        See also the ``lower`` method.
        zSequence is immutableN)rQ   rD   rV   rh   rc   rY   )r   rs   rZ   r   r   r   rD     s    +
z_SeqAbstractBaseClass.upperc             C   sB   | j  }|r4t| j ts"td|| j dd< | S | |S dS )a  Return the sequence in lower case.

        An lower-case copy of the sequence is returned if inplace is False,
        the default value:

        >>> from Bio.Seq import Seq, MutableSeq
        >>> my_seq = Seq("VHLTPeeK*")
        >>> my_seq
        Seq('VHLTPeeK*')
        >>> my_seq.lower()
        Seq('vhltpeek*')
        >>> my_seq.upper()
        Seq('VHLTPEEK*')
        >>> my_seq
        Seq('VHLTPeeK*')

        The sequence is modified in-place and returned if inplace is True:

        >>> my_seq = MutableSeq("VHLTPeeK*")
        >>> my_seq
        MutableSeq('VHLTPeeK*')
        >>> my_seq.lower()
        MutableSeq('vhltpeek*')
        >>> my_seq.upper()
        MutableSeq('VHLTPEEK*')
        >>> my_seq
        MutableSeq('VHLTPeeK*')

        >>> my_seq.lower(inplace=True)
        MutableSeq('vhltpeek*')
        >>> my_seq
        MutableSeq('vhltpeek*')
        >>> my_seq.upper(inplace=True)
        MutableSeq('VHLTPEEK*')
        >>> my_seq
        MutableSeq('VHLTPEEK*')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``lower`` is called on a ``Seq`` object with ``inplace=True``.

        See also the ``upper`` method.
        zSequence is immutableN)rQ   r   rV   rh   rc   rY   )r   rs   rZ   r   r   r   r     s    +
z_SeqAbstractBaseClass.lowerStandard*-c          
   C   s   t |trt|dkrtdyt| }W n> tk
rh   t| }|d dkrZtdt td|d S X | 	t
t| |||||dS )a  Turn a nucleotide sequence into a protein sequence by creating a new sequence 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.

        A ``Seq`` object is returned if ``translate`` is called on a ``Seq``
        object; a ``MutableSeq`` object is returned if ``translate`` is called
        pn a ``MutableSeq`` object.

        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 MutableSeq 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.   r   zYPartial codon, len(sequence) not a multiple of three. This may become an error in future.N)gap)rV   r]   rX   
ValueErrorUndefinedSequenceErrorwarningswarnr   rg   rY   _translate_str)r   rI   stop_symbolto_stopcdsry   rZ   nr   r   r   rH     s    Rz_SeqAbstractBaseClass.translatec             C   sZ   y| j t}W n tk
r$   | S X |rPt| j ts>td|| j dd< | S | |S )a|  Return the complement as an RNA sequence.

        >>> Seq("CGA").complement_rna()
        Seq('GCU')

        Any T in the sequence is treated as a U:

        >>> Seq("CGAUT").complement_rna()
        Seq('GCUAA')

        In contrast, ``complement`` returns a DNA sequence by default:

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

        The sequence is modified in-place and returned if inplace is True:

        >>> my_seq = MutableSeq("CGA")
        >>> my_seq
        MutableSeq('CGA')
        >>> my_seq.complement_rna()
        MutableSeq('GCU')
        >>> my_seq
        MutableSeq('CGA')

        >>> my_seq.complement_rna(inplace=True)
        MutableSeq('GCU')
        >>> my_seq
        MutableSeq('GCU')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``complement_rna`` is called on a ``Seq`` object with ``inplace=True``.
        zSequence is immutableN)rQ   rH   _rna_complement_tabler{   rV   rh   rc   rY   )r   rs   rZ   r   r   r   complement_rnad  s    "z$_SeqAbstractBaseClass.complement_rnac             C   sr   y| j t}W n tk
r$   | S X | j t}|r^t| j tsJtd|| j ddd< | S | |ddd S )a  Return the reverse complement as an RNA sequence.

        >>> Seq("CGA").reverse_complement_rna()
        Seq('UCG')

        Any T in the sequence is treated as a U:

        >>> Seq("CGAUT").reverse_complement_rna()
        Seq('AAUCG')

        In contrast, ``reverse_complement`` returns a DNA sequence by default:

        >>> Seq("CGA").reverse_complement()
        Seq('TCG')

        The sequence is modified in-place and returned if inplace is True:

        >>> my_seq = MutableSeq("CGA")
        >>> my_seq
        MutableSeq('CGA')
        >>> my_seq.reverse_complement_rna()
        MutableSeq('UCG')
        >>> my_seq
        MutableSeq('CGA')

        >>> my_seq.reverse_complement_rna(inplace=True)
        MutableSeq('UCG')
        >>> my_seq
        MutableSeq('UCG')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``reverse_complement_rna`` is called on a ``Seq`` object with
        ``inplace=True``.
        zSequence is immutableNr;   )rQ   rH   r   r{   rV   rh   rc   rY   )r   rs   rZ   r   r   r   reverse_complement_rna  s    #z,_SeqAbstractBaseClass.reverse_complement_rnac             C   sd   y| j dddd}W n tk
r.   | S X |rZt| j tsHtd|| j dd< | S | |S )a|  Transcribe a DNA sequence into RNA and return the RNA sequence as a new Seq object.

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

        The sequence is modified in-place and returned if inplace is True:

        >>> sequence = MutableSeq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")
        >>> sequence
        MutableSeq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')
        >>> sequence.transcribe()
        MutableSeq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')
        >>> sequence
        MutableSeq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')

        >>> sequence.transcribe(inplace=True)
        MutableSeq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')
        >>> sequence
        MutableSeq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``transcribe`` is called on a ``Seq`` object with ``inplace=True``.

        Trying to transcribe an RNA sequence has 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.

        >>> from Bio.Seq import Seq
        >>> my_protein = Seq("MAIVMGRT")
        >>> my_protein.transcribe()
        Seq('MAIVMGRU')
           T   U   t   uzSequence is immutableN)rQ   rE   r{   rV   rh   rc   rY   )r   rs   rZ   r   r   r   
transcribe  s    *z _SeqAbstractBaseClass.transcribec             C   sd   y| j dddd}W n tk
r.   | S X |rZt| j tsHtd|| j dd< | S | |S )ax  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')

        The sequence is modified in-place and returned if inplace is True:

        >>> sequence = MutableSeq("AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG")
        >>> sequence
        MutableSeq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')
        >>> sequence.back_transcribe()
        MutableSeq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')
        >>> sequence
        MutableSeq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG')

        >>> sequence.back_transcribe(inplace=True)
        MutableSeq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')
        >>> sequence
        MutableSeq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``transcribe`` is called on a ``Seq`` object with ``inplace=True``.

        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 U becomes T.

        Trying to back-transcribe a protein sequence will replace any U for
        Selenocysteine with T for Threonine, which is biologically meaningless.

        >>> from Bio.Seq import Seq
        >>> my_protein = Seq("MAIVMGRU")
        >>> my_protein.back_transcribe()
        Seq('MAIVMGRT')
        r   r   r   r   zSequence is immutableN)rQ   rE   r{   rV   rh   rc   rY   )r   rs   rZ   r   r   r   back_transcribe  s    (z%_SeqAbstractBaseClass.back_transcribec             C   s   t |tr"| t| t|S t |tr@| t| |S ddlm} t ||r^tdx6|D ].}t ||r|tdqdt |ttfsdtdqdW | t| dd |D S )a  Return a merge of the sequences in other, spaced by the sequence from self.

        Accepts a Seq object, MutableSeq object, or string (and iterates over
        the letters), or an iterable containing Seq, MutableSeq, 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   )r`   zIterable cannot be a SeqRecordz"Iterable cannot contain SeqRecordszHInput must be an iterable of Seq objects, MutableSeq objects, or stringsc             S   s   g | ]}t |qS r   )r]   )rl   _r   r   r   rq   T  s    z._SeqAbstractBaseClass.join.<locals>.<listcomp>)rV   rP   rY   r]   r
   ra   r`   rc   )r   r!   r`   cr   r   r   r
   0  s    






z_SeqAbstractBaseClass.joinc             C   s   t |trt|}nt |tr(|d}t |tr<t|}nt |trP|d}| j||}|rt | jtsvtd|| jdd< | S | 	|S )aX  Return a copy with all occurrences of subsequence old replaced by new.

        >>> s = Seq("ACGTAACCGGTT")
        >>> t = s.replace("AC", "XYZ")
        >>> s
        Seq('ACGTAACCGGTT')
        >>> t
        Seq('XYZGTAXYZCGGTT')

        For mutable sequences, passing inplace=True will modify the sequence in place:

        >>> m = MutableSeq("ACGTAACCGGTT")
        >>> t = m.replace("AC", "XYZ")
        >>> m
        MutableSeq('ACGTAACCGGTT')
        >>> t
        MutableSeq('XYZGTAXYZCGGTT')

        >>> m = MutableSeq("ACGTAACCGGTT")
        >>> t = m.replace("AC", "XYZ", inplace=True)
        >>> m
        MutableSeq('XYZGTAXYZCGGTT')
        >>> t
        MutableSeq('XYZGTAXYZCGGTT')

        As ``Seq`` objects are immutable, a ``TypeError`` is raised if
        ``replace`` is called on a ``Seq`` object with ``inplace=True``.
        r	   zSequence is immutableN)
rV   rP   r   r]   r   rQ   rE   rh   rc   rY   )r   rF   rG   rs   rZ   r   r   r   rE   V  s    







z_SeqAbstractBaseClass.replace)NN)NN)NN)NN)NN)NN)NN)NN)Nr;   )Nr;   )NF)NF)NF)F)F)rt   ru   FFrv   )F)F)F)F)F)-rK   rL   rM   rN   rO   r   r   r   r[   r\   r"   r#   r$   r%   r&   r   r   r'   r(   r)   rd   re   r/   rk   r*   r3   r4   r5   r6   r7   r9   r<   r?   r@   rB   rC   rD   r   rH   r   r   r   r   r
   rE   r   r   r   r   rP   #  sR   *				
5
I
&
&
3
3


+
+
8
9
9
4
5
k
/
1
7
5&rP   c               @   sN   e Zd ZdZdddZdd Zdd ZdddZdd Zdd Z	dddZ
dS )rg   aB  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).
    Nc             C   sz   |dkr\t |ttfr|| _qvt |ttfr8t|| _qvt |trRt|dd| _qvtdn|dk	rltdt	|| _dS )a%  Create a Seq object.

        Arguments:
         - data - Sequence, required (string)
         - length - Sequence length, used only if data is None (integer)

        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, you can also create a Seq object directly:

        >>> from Bio.Seq import Seq
        >>> my_seq = Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF")
        >>> my_seq
        Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF')
        >>> print(my_seq)
        MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF

        To create a Seq object with for a sequence of known length but
        unknown sequence contents, use None for the data argument and pass
        the sequence length for the length argument. Trying to access the
        sequence contents of a Seq object created in this way will raise
        an UndefinedSequenceError:

        >>> my_undefined_seq = Seq(None, 20)
        >>> my_undefined_seq
        Seq(None, length=20)
        >>> len(my_undefined_seq)
        20
        >>> print(my_undefined_seq)
        Traceback (most recent call last):
        ...
        Bio.Seq.UndefinedSequenceError: Sequence content is undefined
        Nr	   )r.   zDdata should be a string, bytes, bytearray, Seq, or MutableSeq objectz%length should be None if data is None)
rV   r   r   rQ   rh   rP   r]   rc   rz   rW   )r   rZ   lengthr   r   r   r     s    $
zSeq.__init__c             C   s
   t | j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.
        )r   rQ   )r   r   r   r   r      s    zSeq.__hash__c             C   s   t d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')
        zFmyseq.tomutable() is deprecated; please use MutableSeq(myseq) instead.)r|   r}   r   rf   )r   r   r   r   	tomutable  s    
zSeq.tomutableutf-8strictc             C   s   t dt 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'
        zBmyseq.encode has been deprecated; please use bytes(myseq) instead.)r|   r}   r   r]   r   )r   r.   errorsr   r   r   r     s    z
Seq.encodec             C   sp   t | jtr| S d| jks$d| jkrBd| jks8d| jkrBtdnd| jksVd| jkr\t}nt}t| j|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.
        r   r   r   r   zMixed RNA/DNA found)rV   rQ   rW   rz   r   _dna_complement_tablerg   rH   )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.
        Nr;   )r   )r   r   r   r   reverse_complement@  s    7zSeq.reverse_complementrv   c             C   s>   |st dn$t|dks$t|ts2t d|| |dS )aE  Return a copy of the sequence without the gap character(s) (OBSOLETE).

        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')

        This method is OBSOLETE; please use my_dna.replace(gap, "") instead.
        zGap character required.rj   zUnexpected gap character, r   )rz   rX   rV   r]   rE   )r   ry   r   r   r   ungapy  s
    
z	Seq.ungap)N)r   r   )rv   )rK   rL   rM   rN   r   r    r   r   r   r   r   r   r   r   r   rg     s   
4	
N9rg   c               @   s   e Zd ZdZd:ddZdd Zdd	 Ze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dZd<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=d4d5Zd>d6d7Zd8d9 ZdS )?rr   a  Read-only sequence object of known length but unknown contents (DEPRECATED).

    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????')

    Although originally intended for unknown sequences (thus the class name),
    this can be used for homopolymer sequences like AAAAAA, and the biological
    methods will respect this:

    >>> homopolymer = UnknownSeq(6, character="A")
    >>> homopolymer.complement()
    UnknownSeq(6, character='T')
    >>> homopolymer.complement_rna()
    UnknownSeq(6, character='U')
    >>> homopolymer.translate()
    UnknownSeq(2, character='K')
    N?c             C   sZ   t dt |dk	rtdt|| _| jdk r8td|rHt|dkrPt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.
        zGUnknownSeq(length) is deprecated; please use Seq(None, length) instead.Nz,The alphabet argument is no longer supportedr   zLength must not be negative.rj   z4character argument should be a single letter string.)r|   r}   r   rz   r^   _lengthrX   
_character)r   r   Zalphabet	characterr   r   r   r     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 d| j S )z?Return the unknown sequence as full string of the given length.r	   )r   r   r   )r   r   r   r   r     s    zUnknownSeq.__bytes__c             C   s   | j d| j S )Nr	   )r   r   r   )r   r   r   r   rQ     s    zUnknownSeq._datac             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 )z@Return (truncated) representation of the sequence for debugging.zUnknownSeq(z, character=rR   )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   )rV   rr   r   rX   rg   r   )r   r!   r   r   r   r'     s    zUnknownSeq.__add__c             C   s   |t t|  S )zAdd a sequence on the left.)rg   r   )r   r!   r   r   r   r(     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')
        zcan't multiply z by non-int type)r   )rV   r^   rc   rY   rK   rX   r   )r   r!   r   r   r   r)     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')
        zcan't multiply z by non-int type)r   )rV   r^   rc   rY   rK   rX   r   )r   r!   r   r   r   rd   )  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')
        zcan't multiply z by non-int type)r   )rV   r^   rc   rY   rK   rX   r   )r   r!   r   r   r   re   6  s    	
zUnknownSeq.__imul__c             C   s^   t |tr.|| j kr&|| jk r&| jS td|| j\}}}tt|||}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
        zsequence index out of range)r   )	rV   r^   r   r   
IndexErrorindicesrX   rangerr   )r   r5   r1   stopstrider   r   r   r   r   C  s    
zUnknownSeq.__getitem__c             C   s~   t |trt|}nt |ts.tdt| t|t| jkrDdS t||t|	| j
\}}}tt||t| d |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
        z9a Seq, MutableSeq, or string object is required, not '%s'r   rj   )rV   rP   r]   rc   ri   setr   slicerX   r   r   r   )r   r0   r1   r2   r   r   r   r   r   r/   X  s    &


zUnknownSeq.countc             C   sx   t |trt|}nt |ts.tdt| t|t| jkrDdS t||| j	\}}}t
t||t
| 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
        z9a Seq, MutableSeq, or string object is required, not '%s'r   rj   )rV   rP   r]   rc   ri   r   r   r   r   r   rX   r   )r   r0   r1   r2   r   r   r   r   r   rk     s    '


zUnknownSeq.count_overlapc             C   s   t | j}t| j|dS )ax  Return the complement assuming it is DNA.

        In typical usage this will return the same unknown sequence:

        >>> my_nuc = UnknownSeq(8, character='N')
        >>> my_nuc
        UnknownSeq(8, character='N')
        >>> print(my_nuc)
        NNNNNNNN
        >>> my_nuc.complement()
        UnknownSeq(8, character='N')
        >>> print(my_nuc.complement())
        NNNNNNNN

        If your sequence isn't actually unknown, and has a nucleotide letter
        other than N, the appropriate DNA complement base is used:

        >>> UnknownSeq(8, character="A").complement()
        UnknownSeq(8, character='T')
        )r   )r   r   rr   r   )r   sr   r   r   r     s    
zUnknownSeq.complementc             C   s   t | j}t| j|dS )a>  Return the complement assuming it is RNA.

        In typical usage this will return the same unknown sequence. If your
        sequence isn't actually unknown, the appropriate RNA complement base
        is used:

        >>> UnknownSeq(8, character="A").complement_rna()
        UnknownSeq(8, character='U')
        )r   )r   r   rr   r   )r   r   r   r   r   r     s    

zUnknownSeq.complement_rnac             C   s   |   S )a  Return the reverse complement assuming it is DNA.

        In typical usage this will return the same unknown sequence:

        >>> from Bio.Seq import UnknownSeq
        >>> example = UnknownSeq(6, character="N")
        >>> print(example)
        NNNNNN
        >>> print(example.reverse_complement())
        NNNNNN

        If your sequence isn't actually unknown, the appropriate DNA
        complement base is used:

        >>> UnknownSeq(8, character="A").reverse_complement()
        UnknownSeq(8, character='T')
        )r   )r   r   r   r   r     s    zUnknownSeq.reverse_complementc             C   s   |   S )aN  Return the reverse complement assuming it is RNA.

        In typical usage this will return the same unknown sequence. If your
        sequence isn't actually unknown, the appropriate RNA complement base
        is used:

        >>> UnknownSeq(8, character="A").reverse_complement_rna()
        UnknownSeq(8, character='U')
        )r   )r   r   r   r   r     s    
z!UnknownSeq.reverse_complement_rnac             C   s   t | j}t| j|dS )a  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

        In typical usage this will return the same unknown sequence. If your
        sequence isn't actually unknown, but a homopolymer of T, the standard
        DNA to RNA transcription is done, replacing T with U:

        >>> UnknownSeq(9, character="t").transcribe()
        UnknownSeq(9, character='u')
        )r   )r   r   rr   r   )r   r   r   r   r   r   	  s    
zUnknownSeq.transcribec             C   s   t | j}t| j|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

        In typical usage this will return the same unknown sequence. If your
        sequence is actually a U homopolymer, the standard RNA to DNA back
        translation applies, replacing U with T:

        >>> UnknownSeq(9, character="U").back_transcribe()
        UnknownSeq(9, character='T')
        )r   )r   r   rr   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   )rr   r   r   rD   )r   r   r   r   rD   2	  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   )rr   r   r   r   )r   r   r   r   r   D	  s    zUnknownSeq.lowerrt   ru   Frv   c             C   sL   yt | jd |||||d}W n tjk
r8   d}Y nX t| jd |dS )a  Translate an unknown nucleotide sequence into an unknown protein.

        If your sequence makes sense as codons (e.g. a poly-A tail AAAAAA),
        it will be translated accordingly:

        >>> UnknownSeq(7, character='A').translate()
        UnknownSeq(2, character='K')

        Otherwise, it will be translated as X for unknown amino acid:

        >>> UnknownSeq(7).translate()
        UnknownSeq(2, character='X')
        rx   )rI   r   r   r   ry   X)r   )rH   r   r   TranslationErrorrr   r   )r   rI   r   r   r   ry   r   r   r   r   rH   V	  s    
zUnknownSeq.translatec             C   s&   | j |krtdS t| j| j 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   rg   rr   r   )r   ry   r   r   r   r   u	  s    
zUnknownSeq.ungapc             C   s   ddl m} t|ttfrnt|trX| j|jkrX| jt|t| t|d   | jdS t	t| 
t|S t||rtdx6|D ].}t||rtdqt|ttfstdqW t| 
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   )r`   rj   )r   zIterable cannot be a SeqRecordz"Iterable cannot contain SeqRecordsz,Input must be an iterable of Seqs or Stringsc             S   s   g | ]}t |qS r   )r]   )rl   r   r   r   r   rq   	  s    z#UnknownSeq.join.<locals>.<listcomp>)ra   r`   rV   r]   rP   rr   r   rY   rX   rg   r
   rc   r/   )r   r!   r`   r   Z	temp_datar   r   r   r
   	  s"    $



zUnknownSeq.join)Nr   )NN)NN)rt   ru   FFrv   )rv   )rK   rL   rM   rN   r   r   r   propertyrQ   r\   r[   r'   r(   r)   rd   re   r   r/   rk   r   r   r   r   r   r   rD   r   rH   r   r
   r   r   r   r   rr     s4   =

2
3

 rr   c               @   s   e Zd ZdZdd Zedd Zejdd Zdd Zd	d
 Z	dd Z
dd ZdddZdd Zdd Zdd Zdd Zdd Zdd ZdS ) rf   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jr2|jdkrtdtdt | }t |trD|| _	nlt |t
rZt|| _	nVt |trrt|d| _	n>t |tr|j	dd | _	n"t |trtt
|| _	ntddS )zCreate a MutableSeq object.uzNdata should be a string, array of characters, Seq object, or MutableSeq objectzaInitializing a MutableSeq by an array has been deprecated; please use a bytearray object instead.r	   NzMdata should be a string, bytearray object, Seq object, or a MutableSeq object)rV   arraytypecoderz   r|   r}   r   Z	tounicoderh   rQ   r   r]   rf   rg   rc   )r   rZ   r   r   r   r   	  s(    





zMutableSeq.__init__c             C   s    t dt td| jdS )zGet the data.zAccessing MutableSeq.data has been deprecated, as it is now a private attribute. Please use indexing to access the sequence contents of a MutableSeq object.r   r	   )r|   r}   r   r   rQ   r-   )r   r   r   r   rZ   
  s    zMutableSeq.datac             C   s   t dt | | dS )zSet the data.zAccessing MutableSeq.data has been deprecated, as it is now a private attribute. Please use indexing to access the sequence contents of a MutableSeq object.N)r|   r}   r   r   )r   valuer   r   r   rZ   
  s    c             C   s|   t |trt|| j|< n^t |tr2|j| j|< nFt |trLt|| j|< n,t |trh|d| j|< nt	dt
| dS )zSet a subsequence of single letter via value parameter.

        >>> my_seq = MutableSeq('ACTCGACGTCG')
        >>> my_seq[0] = 'T'
        >>> my_seq
        MutableSeq('TCTCGACGTCG')
        r	   zreceived unexpected type %sN)rV   r^   ordrQ   rf   rg   r   r]   r   rc   ri   )r   r5   r   r   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)rQ   )r   r5   r   r   r   __delitem__0
  s    	zMutableSeq.__delitem__c             C   s   | j t|d 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.
        r	   N)rQ   appendr   r   )r   r   r   r   r   r   ;
  s    
zMutableSeq.appendc             C   s   | j |t|d 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.
        r	   N)rQ   insertr   r   )r   ir   r   r   r   r   G
  s    zMutableSeq.insertr;   c             C   s   | j | }| j |= t|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.
        )rQ   r_   )r   r   r   r   r   r   popV
  s    
zMutableSeq.popc             C   s<   t |}y| j| W n tk
r6   tddY nX 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.
        zvalue not found in MutableSeqN)r   rQ   removerz   )r   r+   Z	codepointr   r   r   r   i
  s
    zMutableSeq.removec             C   s   | j   dS )zQModify the mutable sequence to reverse itself.

        No return value.
        N)rQ   reverse)r   r   r   r   r   |
  s    zMutableSeq.reversec             C   sP   t d| jkr&t d| jkr&tdnt d| jkr:t}nt}| 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   zMixed RNA/DNA foundN)r   rQ   rz   r   r   rH   )r   rI   r   r   r   r   
  s    

zMutableSeq.complementc             C   s   |    | j  dS )zaModify the mutable sequence to take on its reverse complement.

        No return value.
        N)r   rQ   r   )r   r   r   r   r   
  s    zMutableSeq.reverse_complementc             C   s`   t |tr| j|j nBt |tr6| jt| n&t |trT| j|d ntd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.
        r	   z$expected a string, Seq or MutableSeqN)	rV   rf   rQ   extendrg   r   r]   r   rc   )r   r!   r   r   r   r   
  s    


zMutableSeq.extendc             C   s   t dt t| 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')
        z;myseq.toseq() is deprecated; please use Seq(myseq) instead.)r|   r}   r   rg   )r   r   r   r   toseq
  s    
zMutableSeq.toseqN)r;   )rK   rL   rM   rN   r   r   rZ   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rf   	  s   
rf   c               @   s   e Zd ZdZdS )r{   zSequence contents is undefined.N)rK   rL   rM   rN   r   r   r   r   r{   
  s   r{   c                   sD   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	  Z
S )rW   a  Stores the length of a sequence with an undefined sequence contents (PRIVATE).

    Objects of this class can be used to create a Seq object to represent
    sequences with a known length, but an unknown sequence contents.
    Calling __len__ returns the sequence length, calling __getitem__ raises a
    ValueError except for requests of zero size, for which it returns an empty
    bytes object.
    )r   c                s$   |dk rt d|| _t   dS )z/Initialize the object with the sequence length.r   zLength must not be negative.N)rz   r   superr   )r   r   )rY   r   r   r   
  s    z_UndefinedSequenceData.__init__c             C   sL   t |tr@|| j\}}}tt|||}|dkr8dS t|S tdd S )Nr   r   zSequence content is undefined)rV   r   r   r   rX   r   rW   r{   )r   r   r1   r2   stepsizer   r   r   r   
  s    
z"_UndefinedSequenceData.__getitem__c             C   s   | j S )N)r   )r   r   r   r   r   
  s    z_UndefinedSequenceData.__len__c             C   s   | j dkrdS tdd S )Nr   r   zSequence content is undefined)r   r{   )r   r   r   r   r   
  s    
z _UndefinedSequenceData.__bytes__c             C   s"   t |trt| j|j S td S )N)rV   rW   r   rc   )r   r!   r   r   r   r'   
  s    
z_UndefinedSequenceData.__add__)rK   rL   rM   rN   rO   r   r   r   r   r'   __classcell__r   r   )rY   r   rW   
  s   
rW   c             C   s@   t | tr|  S t | tr(t|  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   tr   N)rV   rg   r   rf   rE   )Zdnar   r   r   r   
  s
    

r   c             C   s@   t | tr|  S t | tr(t|  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)rV   rg   r   rf   rE   )Zrnar   r   r   r     s
    

r   ru   Fr   c          
      s  yt |}W nP tk
r*   tj| }Y n> ttfk
r\   t|tjrN|}n
tddY nX tj| }|  } g }	|j	 |j
}
|jdk	rt|j }nttj tj  }t| } fdd|
D }|r,|d }|rtdt| d| d |  d	td
t| d| d |  dt |rt| dd  |jkrhtd| dd  d|d dkrtd| dt| dd  |
krtd| dd  d| dd } |d8 }dg}	n|d dkrtdt |dk	r(t|tstdnt|dkr(tdxtd||d  dD ]}| ||d  }y|	 |  W n ttjfk
r   ||j
kr|rtdd|rP |	| nT|t|r|	| n8|dk	r||d kr|	| ntd| ddY nX q>W d|	S )a	  Translate nucleotide string into a protein string (PRIVATE).

    Arguments:
     - sequence - a string
     - 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 - 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.
    zBad table argumentNc                s   g | ]}| kr|qS r   r   )rl   r   )forward_tabler   r   rq     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.rx   zFirst codon 'z' is not a start codonzSequence length z is not a multiple of threerU   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.rj   z Extra in frame stop codon found.zCodon 'z' is invalidr   )r^   rz   r   Zambiguous_generic_by_nameAttributeErrorrc   rV   Zambiguous_generic_by_idrD   r   stop_codonsZnucleotide_alphabetr   r   Zambiguous_dna_lettersZambiguous_rna_lettersrX   r|   r}   r   r]   Zstart_codonsr   r   r   KeyError
issupersetr
   )sequencerI   r   r   r   Zpos_stopry   Ztable_idZcodon_tableZamino_acidsr   Zvalid_lettersr   Zdual_codingr   r   Zcodonr   )r   r   r~      s    A

"

r~   rt   c             C   sP   t | tr| ||||S t | tr8t| ||||S 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 ...
    )ry   N)rV   rg   rH   rf   r~   )r   rI   r   r   r   ry   r   r   r   rH     s
    ^

rH   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'
    Nr;   )r   )r   r   r   r   r   1  s    r   c             C   s   t | tr|  S t | tr(t|  S | d} d| ksBd| kr\d| ksRd| kr\tdnd| ksld| krrt}nt}| |} | 	d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   zMixed RNA/DNA found)
rV   rg   r   rf   r   rz   r   r   rH   r-   )r   r   r   r   r   r   F  s    




r   c             C   sF   t | tr|  S t | tr(t|  S | d} | t} | dS )zReturn the complement sequence of an RNA string.

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

    Any T in the sequence is treated as a U.
    r	   )rV   rg   r   rf   r   rH   r   r-   )r   r   r   r   r   x  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__)ru   FFr   N)rt   ru   FFN)#rN   r   r|   abcr   r   ZBior   r   ZBio.Datar   r   r   Zambiguous_dna_complementr   dictZambiguous_rna_complementr   r   rP   rg   rr   rf   rz   r{   rW   r   r   r~   rH   r   r   r   r   rK   r   r   r   r   <module>   sd   

 h          k      = |0
 *
g2	
