B
    ‰°bÍ6  ã               @   s¢   d 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	lmZ G d
d„ deƒZdd„ Zdd„ ZG dd„ dƒZG dd„ deƒZdS )zÄBio.SeqIO support module (not for general use).

Unless you are writing a new parser or writer for Bio.SeqIO, you should not
use this module.  It provides base classes to try and simplify things.
é    N)ÚABC)Úabstractmethod)ÚBiopythonDeprecationWarning)ÚStreamModeError)Ú
MutableSeq)ÚSeq)Ú	SeqRecordc               @   s6   e Zd ZdZddd„Zdd„ Zdd	„ Zed
d„ ƒZdS )ÚSequenceIteratorzµBase class for building SeqRecord iterators.

    You should write a parse method that returns a SeqRecord generator.  You
    may wish to redefine the __init__ method as well.
    NÚtc             C   sä   |dk	rt dƒ‚yt|d| ƒ| _d| _W nz tk
r¤   |dkr`| d¡dkr”td| ƒd‚n4|d	kr†| d¡d
kr”td| ƒd‚nt d| ƒd‚|| _d| _Y nX y|  | j¡| _W n& t	k
rÞ   | jrØ| j 
¡  ‚ Y nX dS )a³  Create a SequenceIterator object.

        Arguments:
        - source - input file stream, or path to input file
        - alphabet - no longer used, should be None

        This method MAY be overridden by any subclass.

        Note when subclassing:
        - there should be a single non-optional argument, the source.
        - you do not have to require an alphabet.
        - you can add additional optional arguments.
        Nz,The alphabet argument is no longer supportedÚrTr
   r   Ú z%%s files must be opened in text mode.Úbó    z'%s files must be opened in binary mode.zUnknown mode '%s'F)Ú
ValueErrorÚopenÚstreamÚshould_close_streamÚ	TypeErrorÚreadr   ÚparseÚrecordsÚ	ExceptionÚclose)ÚselfÚsourceZalphabetÚmodeZfmt© r   ú3lib/python3.7/site-packages/Bio/SeqIO/Interfaces.pyÚ__init__   s2    

zSequenceIterator.__init__c             C   s6   y
t | jƒS  tk
r0   | jr*| j ¡  ‚ Y nX d S )N)Únextr   r   r   r   r   )r   r   r   r   Ú__next__H   s    

zSequenceIterator.__next__c             C   s   | S )a  Iterate over the entries as a SeqRecord objects.

        Example usage for Fasta files::

            with open("example.fasta","r") as myFile:
                myFastaReader = FastaIterator(myFile)
                for record in myFastaReader:
                    print(record.id)
                    print(record.seq)

        This method SHOULD NOT be overridden by any subclass. It should be
        left as is, which will call the subclass implementation of __next__
        to actually parse the file.
        r   )r   r   r   r   Ú__iter__P   s    zSequenceIterator.__iter__c             C   s   dS )z8Start parsing the file, and return a SeqRecord iterator.Nr   )r   Úhandler   r   r   r   a   s    zSequenceIterator.parse)Nr
   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r!   r   r   r   r   r   r   r	      s
   
)r	   c             C   sT   t | tƒstdƒ‚| jdkr,td| j ƒ‚nt | jttfƒsJtd| j ƒ‚t| jƒS )z@Use this to catch errors like the sequence being None (PRIVATE).zExpected a SeqRecord objectNz,SeqRecord (id=%s) has None for its sequence.z*SeqRecord (id=%s) has an invalid sequence.)Ú
isinstancer   r   ÚseqÚidr   r   Ústr)Úrecordr   r   r   Ú_get_seq_stringf   s    

r,   c             C   s   |   dd¡  dd¡S )z;Use this to avoid getting newlines in the output (PRIVATE).Ú
ú ú)Úreplace)Útextr   r   r   Ú_cleanr   s    r2   c               @   sN   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	ddd„Z
dS )ÚSequenceWriterai  Base class for sequence writers. This class should be subclassed.

    It is intended for sequential file formats with an (optional)
    header, repeated records, and an (optional) footer, as well
    as for interlaced file formats such as Clustal.

    The user may call the write_file() method to write a complete
    file containing the sequences.

    Alternatively, users may call the write_header(), followed
    by multiple calls to write_record() and/or write_records(),
    followed finally by write_footer().

    Note that write_header() cannot require any assumptions about
    the number of records.
    Úwc             C   sÌ   |dkrXy|  d¡ W n: tk
r4   tdƒd‚Y q¼ tk
rP   t||ƒ}Y q¼X |}nd|dkr°y|  d¡ W n: tk
rŒ   tdƒd‚Y q¼ tk
r¨   t||ƒ}Y q¼X |}ntd| ƒ‚|| _|| _dS )	zCreate the writer object.r4   r   z!File must be opened in text mode.NÚwbr   z#File must be opened in binary mode.zUnknown mode '%s')Úwriter   r   ÚAttributeErrorr   ÚRuntimeErrorÚ_targetr"   )r   Útargetr   r"   r   r   r   r   ‰   s&    zSequenceWriter.__init__c             C   s   |  dd¡  dd¡S )z1Use this to avoid getting newlines in the output.r-   r.   r/   )r0   )r   r1   r   r   r   Úclean§   s    zSequenceWriter.cleanc             C   s   dS )z)Write the file header to the output file.Nr   )r   r   r   r   Úwrite_header«   s    zSequenceWriter.write_headerc             C   s   dS )z)Write the file footer to the output file.Nr   )r   r   r   r   Úwrite_footer³   s    zSequenceWriter.write_footerc             C   s   t dƒ‚dS )zWWrite a single record to the output file.

        record - a SeqRecord object
        z!This method should be implementedN)ÚNotImplementedError)r   r+   r   r   r   Úwrite_record»   s    zSequenceWriter.write_recordNc             C   sx   d}|dkr.xf|D ]}|   |¡ |d7 }qW nFxD|D ]<}||kr^|dkrRtdƒ‚ntd| ƒ‚|   |¡ |d7 }q4W |S )a  Write records to the output file, and return the number of records.

        records - A list or iterator returning SeqRecord objects
        maxcount - The maximum number of records allowed by the
        file format, or None if there is no maximum.
        r   Né   zMore than one sequence foundz%Number of sequences is larger than %d)r?   r   )r   r   ÚmaxcountÚcountr+   r   r   r   Úwrite_recordsÆ   s    





zSequenceWriter.write_recordsr   c             C   s‚   z |   ¡  |  ||¡}|  ¡  W d| j| jk	r8| j ¡  X ||k r~|dkrTtdƒ‚n*||krntd||f ƒ‚ntd||f ƒ‚|S )z”Write a complete file with the records, and return the number of records.

        records - A list or iterator returning SeqRecord objects
        Nr@   zMust have one sequencez'Number of sequences is %d (expected %d)z0Number of sequences is %d (expected at least %d))r<   rC   r=   r"   r9   r   r   )r   r   ZmincountrA   rB   r   r   r   Ú
write_fileß   s     	
zSequenceWriter.write_file)r4   )N)r   N)r#   r$   r%   r&   r   r;   r<   r=   r?   rC   rD   r   r   r   r   r3   w   s   

r3   c                   sJ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚSequentialSequenceWritera,  Base class for sequential sequence writers (DEPRECATED).

    This class should be subclassed. It is no longer used.
    It was intended for sequential file formats with an (optional)
    header, repeated records, and an (optional) footer. It would
    enforce callign the methods in appropriate order. To update
    code using ``SequentialSequenceWriter``, just subclass
    ``SequenceWriter`` and drop the ``._header_written`` etc
    checks (or reimplement them).

    In this case (as with interlaced file formats), the user may
    simply call the write_file() method and be done.

    However, they may also call the write_header(), followed
    by multiple calls to write_record() and/or write_records()
    followed finally by write_footer().

    Users must call write_header() and write_footer() even when
    the file format concerned doesn't have a header or footer.
    This is to try and make life as easy as possible when
    switching the output format.

    Note that write_header() cannot require any assumptions about
    the number of records.
    r4   c                s0   t ƒ  ||¡ d| _d| _d| _t dt¡ dS )zInitialize the class.FztSequentialSequenceWriter has been deprecated, any class subclassing it will need to subclass SequenceWriter instead.N)Úsuperr   Ú_header_writtenÚ_record_writtenÚ_footer_writtenÚwarningsÚwarnr   )r   r:   r   )Ú	__class__r   r   r     s    z!SequentialSequenceWriter.__init__c             C   s4   | j rtdƒ‚| jrtdƒ‚| jr*tdƒ‚d| _ dS )a4  Write the file header.

        If your file format defines a header, you should implement this method
        in order to write the header before any of the records.

        The default implementation checks the private attribute ._header_written
        to ensure the header is only written once.
        z%You have aleady called write_header()z8You have aleady called write_record() or write_records()z%You have aleady called write_footer()TN)rG   ÚAssertionErrorrH   rI   )r   r   r   r   r<   %  s
    	z%SequentialSequenceWriter.write_headerc             C   s4   | j stdƒ‚| jstdƒ‚| jr*tdƒ‚d| _dS )a0  Write the file footer.

        If your file format defines a footer, you should implement this method
        in order to write the footer after all the records.

        The default implementation checks the private attribute ._footer_written
        to ensure the footer is only written once.
        z"You must call write_header() firstz9You have not called write_record() or write_records() yetz%You have aleady called write_footer()TN)rG   rM   rH   rI   )r   r   r   r   r=   5  s
    	z%SequentialSequenceWriter.write_footerc             C   s.   | j stdƒ‚| jrtdƒ‚d| _tdƒ‚dS )a  Write a single record to the output file.

        record - a SeqRecord object

        Once you have called write_header() you can call write_record()
        and/or write_records() as many times as needed.  Then call
        write_footer() and close().
        z"You must call write_header() firstz&You have already called write_footer()Tz This object should be subclassedN)rG   rM   rI   rH   r>   )r   r+   r   r   r   r?   E  s    	z%SequentialSequenceWriter.write_recordc             C   sJ   | j stdƒ‚| jrtdƒ‚d}x|D ]}|  |¡ |d7 }q&W d| _|S )aT  Write multiple record to the output file.

        records - A list or iterator returning SeqRecord objects

        Once you have called write_header() you can call write_record()
        and/or write_records() as many times as needed.  Then call
        write_footer() and close().

        Returns the number of records written.
        z"You must call write_header() firstz&You have already called write_footer()r   r@   T)rG   rM   rI   r?   rH   )r   r   rB   r+   r   r   r   rC   S  s    

z&SequentialSequenceWriter.write_recordsc             C   s<   z|   ¡  |  |¡}|  ¡  W d| j| jk	r6| j ¡  X |S )zçUse this to write an entire file containing the given records.

        records - A list or iterator returning SeqRecord objects

        This method can only be called once.  Returns the number of records
        written.
        N)r<   rC   r=   r"   r9   r   )r   r   rB   r   r   r   rD   i  s    
z#SequentialSequenceWriter.write_file)r4   )r#   r$   r%   r&   r   r<   r=   r?   rC   rD   Ú__classcell__r   r   )rL   r   rE   þ   s   rE   )r&   rJ   Úabcr   r   ZBior   r   ZBio.Seqr   r   ZBio.SeqRecordr   r	   r,   r2   r3   rE   r   r   r   r   Ú<module>   s   N 