
:>"^b%  ã               @   s¶   d  Z  d d l m Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 Gd d „  d e ƒ Z d	 d
 „  Z d d „  Z Gd d „  d e ƒ Z Gd 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.
é    )Úprint_functionN)Úgeneric_alphabet)ÚSeqÚ
MutableSeq)Ú	SeqRecordc               @   s\   e  Z d  Z d Z e d d „ Z d d „  Z e j d d k  rL d d	 „  Z	 d
 d „  Z
 d S)ÚSequenceIteratorz²Base class for building SeqRecord iterators.

    You should write a __next__ method to return SeqRecord  objects.  You may
    wish to redefine the __init__ method as well.
    c             C   s   | |  _  | |  _ d S)aö  Create a SequenceIterator object.

        Arguments:
        - handle - input file
        - alphabet - optional, e.g. Bio.Alphabet.generic_protein

        This method MAY be overridden by any subclass, for example if you need
        to process a header or accept additional arguments.

        Note when subclassing:
        - there should be a single non-optional argument, the handle.
        - you do not have to require an alphabet.
        - you can add additional optional arguments.
        N)ÚhandleÚalphabet)Úselfr   r	   © r   ú9/tmp/pip-build-ww9dw3qa/biopython/Bio/SeqIO/Interfaces.pyÚ__init__   s    	zSequenceIterator.__init__c             C   s   t  d ƒ ‚ d S)zÖReturn the next record in the file.

        This method's stub-implementation MUST be overridden by any subclass
        to actually parse the file and return the next entry as a SeqRecord
        object.
        z2The subclass should implement the __next__ method.N)ÚNotImplementedError)r
   r   r   r   Ú__next__/   s    zSequenceIterator.__next__r   é   c             C   s
   |  j  ƒ  S)z8Python 2 style alias for Python 3 style __next__ method.)r   )r
   r   r   r   Únext:   s    zSequenceIterator.nextc             C   s   t  |  j d ƒ 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.
        N)Úiterr   )r
   r   r   r   Ú__iter__>   s    zSequenceIterator.__iter__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚsysÚversion_infor   r   r   r   r   r   r      s   	r   c             C   sx   t  |  t ƒ s t d ƒ ‚ |  j d k r@ t d |  j ƒ ‚ n+ t  |  j t t f ƒ sk t 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   Ú	TypeErrorÚseqÚidr   r   Ústr)Úrecordr   r   r   Ú_get_seq_stringQ   s    r    c             C   s   |  j  d d ƒ j  d d ƒ S)z;Use this to avoid getting newlines in the output (PRIVATE).Ú
ú ú)Úreplace)Útextr   r   r   Ú_clean]   s    r&   c               @   sF   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 S)ÚSequenceWriterzèBase class for building SeqRecord writers.

    Interlaced file formats (e.g. Clustal) should subclass directly.

    Sequential file formats (e.g. Fasta, GenBank) should subclass the
    SequentialSequenceWriter class instead.
    c             C   s   | |  _  d S)zqCreate the writer object.

        Use the method write_file() to actually record your sequence records.
        N)r   )r
   r   r   r   r   r   k   s    zSequenceWriter.__init__c             C   sx   t  | t ƒ s t d ƒ ‚ | j d k r@ t d | j ƒ ‚ n+ t  | j t t f ƒ sk t 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.)r   r   r   r   r   r   r   r   )r
   r   r   r   r   r    r   s    zSequenceWriter._get_seq_stringc             C   s   | j  d d ƒ j  d d ƒ S)z1Use this to avoid getting newlines in the output.r!   r"   r#   )r$   )r
   r%   r   r   r   Úclean|   s    zSequenceWriter.cleanc             C   s   t  d ƒ ‚ d S)zõUse this to write an entire file containing the given records.

        records - A list or iterator returning SeqRecord objects

        Should return the number of records (as an integer).

        This method can only be called once.
        z This object should be subclassedN)r   )r
   Úrecordsr   r   r   Ú
write_file€   s    zSequenceWriter.write_fileN)r   r   r   r   r   r    r(   r*   r   r   r   r   r'   b   s
   
r'   c               @   s^   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z d d „  Z	 d S)ÚSequentialSequenceWritera  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.

    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.
    c             C   s(   | |  _  d |  _ d |  _ d |  _ d S)zInitialize the class.FN)r   Ú_header_writtenÚ_record_writtenÚ_footer_written)r
   r   r   r   r   r   §   s    			z!SequentialSequenceWriter.__init__c             C   sO   |  j  s t d ƒ ‚ |  j s, t d ƒ ‚ |  j sB 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)r,   ÚAssertionErrorr-   r.   )r
   r   r   r   Úwrite_header®   s
    		z%SequentialSequenceWriter.write_headerc             C   sM   |  j  s t d ƒ ‚ |  j s* t d ƒ ‚ |  j s@ 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)r,   r/   r-   r.   )r
   r   r   r   Úwrite_footer¾   s
    		z%SequentialSequenceWriter.write_footerc             C   sD   |  j  s t d ƒ ‚ |  j s+ 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)r,   r/   r.   r-   r   )r
   r   r   r   r   Úwrite_recordÎ   s    		z%SequentialSequenceWriter.write_recordc             C   sf   |  j  s t d ƒ ‚ |  j s+ t d ƒ ‚ d } x% | D] } |  j | ƒ | d 7} q8 Wd |  _ | 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   é   T)r,   r/   r.   r2   r-   )r
   r)   Úcountr   r   r   r   Úwrite_recordsß   s    	z&SequentialSequenceWriter.write_recordsc             C   s'   |  j  ƒ  |  j | ƒ } |  j ƒ  | 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.
        )r0   r5   r1   )r
   r)   r4   r   r   r   r*   õ   s    

z#SequentialSequenceWriter.write_fileN)
r   r   r   r   r   r0   r1   r2   r5   r*   r   r   r   r   r+   ‘   s   r+   )r   Ú
__future__r   r   ZBio.Alphabetr   ZBio.Seqr   r   ZBio.SeqRecordr   Úobjectr   r    r&   r'   r+   r   r   r   r   Ú<module>   s   ;/