o
    Rŀgo&                     @   s  d 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 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 eeefZee	e eeef Zee ZG dd deee ZdedefddZdedefddZG dd dZdS )zBio.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.
    )ABC)abstractmethod)PathLike)AnyStr)Generic)IO)Iterator)Optional)Union)StreamModeError)
MutableSeq)Seq)	SeqRecordc                   @   sh   e Zd ZdZ			ddedddedee ddf
d	d
Zdd Zdd Z	e
dee dee fddZdS )SequenceIteratorzBase 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tsourcealphabetmodefmtreturnc                 C   s   |durt dt|trt|d| | _d| _n7|dkr-|ddkr,t| ddn|d	krA|dd
kr@t| ddn	t d| dd|| _d| _z
| | j| _	W dS  t
yj   | jri| j   w )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# files must be opened in text mode.b    z% files must be opened in binary mode.Unknown mode ''F)
ValueError
isinstance_PathLikeTypesopenstreamshould_close_streamreadr   parserecords	Exceptionclose)selfr   r   r   r    r(   H/var/www/html/myenv/lib/python3.10/site-packages/Bio/SeqIO/Interfaces.py__init__)   s>   

zSequenceIterator.__init__c                 C   s.   zt | jW S  ty   | jr| j   w )zReturn the next entry.)nextr$   r%   r!   r    r&   r'   r(   r(   r)   __next__X   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__a   s   zSequenceIterator.__iter__handlec                 C      dS )z8Start parsing the file, and return a SeqRecord iterator.Nr(   )r'   r/   r(   r(   r)   r#   r       zSequenceIterator.parse)Nr   N)__name__
__module____qualname____doc__	_IOSourcestrr	   r*   r-   r.   r   r   r   r   r   r#   r(   r(   r(   r)   r   "   s(    	
/	 r   recordr   c                 C   sZ   t | ts	td| jdu rtd| j dt | jttfs(td| j dt| jS )z@Use this to catch errors like the sequence being None (PRIVATE).zExpected a SeqRecord objectNzSeqRecord (id=z) has None for its sequence.z) has an invalid sequence.)r   r   	TypeErrorseqidr   r   r7   )r8   r(   r(   r)   _get_seq_stringw   s   


r<   textc                 C   s   |  dd ddS )z;Use this to avoid getting newlines in the output (PRIVATE).
 replace)r=   r(   r(   r)   _clean      rC   c                   @   sf   e Zd ZdZddededdfddZd	edef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.
    wtargetr   r   Nc                 C   s   |dkr%t |trt||}nCz	|}|d W n9 ty$   tddw |dkrJt |tr4t||}n|}z|d W n tyI   tddw td| d	|| _|| _dS )
zCreate the writer object.rF   r   z!File must be opened in text mode.Nwbr   z#File must be opened in binary mode.r   r   )	r   r   r   writer9   r   RuntimeError_targetr/   )r'   rG   r   r/   r(   r(   r)   r*      s2   



zSequenceWriter.__init__r=   c                 C   s   | dd ddS )z1Use this to avoid getting newlines in the output.r>   r?   r@   rA   )r'   r=   r(   r(   r)   clean   rD   zSequenceWriter.cleanc                 C   r0   )z)Write the file header to the output file.Nr(   r,   r(   r(   r)   write_header   r1   zSequenceWriter.write_headerc                 C   r0   )z)Write the file footer to the output file.Nr(   r,   r(   r(   r)   write_footer   r1   zSequenceWriter.write_footerc                 C   s   t d)zWWrite a single record to the output file.

        record - a SeqRecord object
        z!This method should be implemented)NotImplementedError)r'   r8   r(   r(   r)   write_record   s   zSequenceWriter.write_recordc                 C   sp   d}|du r|D ]}|  | |d7 }q|S |D ]}||kr,|dkr&tdtd| |  | |d7 }q|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)rP   r   )r'   r$   maxcountcountr8   r(   r(   r)   write_records   s    



zSequenceWriter.write_recordsr   c                 C   s   z|    | ||}|   W | j| jur| j  n| j| jur(| j  w w ||k rI|dkr5td||krAtd||f td||f |S )zWrite a complete file with the records, and return the number of records.

        records - A list or iterator returning SeqRecord objects
        rQ   zMust have one sequencez'Number of sequences is %d (expected %d)z0Number of sequences is %d (expected at least %d))rM   rT   rN   r/   rK   r&   r   )r'   r$   mincountrR   rS   r(   r(   r)   
write_file   s.   	


zSequenceWriter.write_file)rF   )N)r   N)r2   r3   r4   r5   r6   r7   r*   rL   rM   rN   rP   rT   rV   r(   r(   r(   r)   rE      s     
rE   N)r5   abcr   r   osr   typingr   r   r   collections.abcr   r	   r
   Bior   Bio.Seqr   r   Bio.SeqRecordr   r7   bytesr   r6   _TextIOSourcer   r<   rC   rE   r(   r(   r(   r)   <module>   s*   
U