
    Ri                      :   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z  ez  ez  Zee   Zee   Z G d dee
e	         ZdedefdZdedefdZ G d dee
e	         Zy)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)StringIO)PathLike)AnyStr)Generic)IO)Optional)Union)StreamModeError)
MutableSeq)Seq)	SeqRecordc            	       p    e Zd ZdZeed               Z	 	 ddedddedz  ddfdZ	ed	        Z
d
 Zd Zd Zy)SequenceIteratora  Base class for building SeqRecord iterators.

    You should write a __next__ method that returns the next SeqRecord.  You
    may wish to redefine the __init__ method as well.
    You must also create a class property `modes` specifying the allowable
    file stream modes.
    c                      y)aN  File modes (binary or text) that the parser can handle.

        This property must be "t" (for text mode only), "b" (for binary mode
        only), "tb" (if both text and binary mode are accepted, but text mode
        is preferred), or "bt" (if both text and binary mode are accepted, but
        binary mode is preferred).
        N selfs    L/home/agent/.friday_env/lib/python3.12/site-packages/Bio/SeqIO/Interfaces.pymodeszSequenceIterator.modes,        	    Nsourcealphabetfmtreturnc                 l   |t        d      | j                  }|| _        t        |t              r!|d   }t        |d|z         | _        || _
        y|j                  d      }|dk(  r|dk(  rt        | d      dd}n(|d	k(  r|dk(  rt        | d
      dd}nt        d      d|| _        || _
        y)a  Create a SequenceIterator object.

        Arguments:
        - source - input file stream, or path to input file
        - alphabet - no longer used, should be None
        - fmt - string, mixed case format name for in error messages

        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 supportedr   r bz% files must be opened in binary mode.tr   z# files must be opened in text mode.Failed to read from input data)
ValueErrorr   r   
isinstance_PathLikeTypesopenstreamreadr   RuntimeErrormode)r   r   r   r   r   r+   values          r   __init__zSequenceIterator.__init__8   s    ( KLL

fn-8DvsTz2DK$ 	! KKNE{C<)%DE  #C<)%BC  "#CD$N DK	r   c                      y)z]Return the next SeqRecord.

        This method must be implemented by the subclass.
        Nr   r   s    r   __next__zSequenceIterator.__next__f       r   c                     | 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.
        r   r   s    r   __iter__zSequenceIterator.__iter__m   s	     r   c                     | S )Nr   r   s    r   	__enter__zSequenceIterator.__enter__|   s    r   c                     	 | j                   }| j                   | j                  ur| j                   j                          | ` y# t        $ r Y y w xY w)NF)r(   AttributeErrorr   close)r   exc_type	exc_valueexc_tracebackr(   s        r   __exit__zSequenceIterator.__exit__   sN    	[[F ;;dkk)KKK  		s   A 	AA)NN)__name__
__module____qualname____doc__propertyr   r   	_IOSourcestrr-   r/   r2   r4   r;   r   r   r   r   r   #   s~        	,, , 4Z	,
 
,\  r   r   recordr   c                    t        | t              st        d      | j                  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 objectzSeqRecord (id=z) has None for its sequence.z) has an invalid sequence.)r%   r   	TypeErrorseqidr   r   rB   )rC   s    r   _get_seq_stringrH      su    fi(566zz.3OPQQ

S*$56.3MNOOvzz?r   textc                 F    | j                  dd      j                  dd      S )z;Use this to avoid getting newlines in the output (PRIVATE).
 replace)rI   s    r   _cleanrP      s     <<c"**455r   c                   l    e Zd ZdZeed               ZdeddfdZde	de	fdZ
ed	        Zd
 Zd Zd Zy)SequenceWritera  Base class for sequence writers. This class should be subclassed.

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

    Most subclasses will only need to implement the write_record method.
    Subclasses must implement the write_records method to include a file
    header or footer, for file formats that only allow one record, or for
    file formats that cannot be written sequentially.
    c                      y)aN  File modes (binary or text) that the writer can handle.

        This property must be "t" (for text mode only), "b" (for binary mode
        only), "tb" (if both text and binary mode are accepted, but text mode
        is preferred), or "bt" (if both text and binary mode are accepted, but
        binary mode is preferred).
        Nr   r   s    r   r   zSequenceWriter.modes   r   r   targetr   Nc                 j   t        |t              r| j                  d   }t        |d|z         }ng|}d}d}t	        ||      D ]  \  }}	 |j                  |        n t        d      d|| j                  vr |dk(  rt        d      |d	k(  rt        d
      || _	        || _
        y# t        $ r Y ow xY w)zCreate the writer object.r   wtb)r    r   r#   Nr"   z#File must be opened in binary mode.r!   z!File must be opened in text mode.)r%   r&   r   r'   zipwriterE   r*   r   rT   handle)r   rT   r+   r(   r   valuesr,   s          r   r-   zSequenceWriter.__init__   s    fn-::a=D&#*-FFEF"5&1 OeLL' O ##CD$N4::%3;)*OPPS[)*MNN ! s   	B&&	B21B2rI   c                 F    |j                  dd      j                  dd      S )z1Use this to avoid getting newlines in the output.rK   rL   rM   rN   )r   rI   s     r   cleanzSequenceWriter.clean   s     ||D#&..tS99r   c                 n    t               } | |      }|g}|j                  |       |j                         S )z(Format the record and return the string.)r   
write_filegetvalue)clsrC   rZ   writerrecordss        r   	to_stringzSequenceWriter.to_string   s6     V('"  r   c                      y)zWWrite a single record to the output file.

        record - a SeqRecord object
        Nr   )r   rC   s     r   write_recordzSequenceWriter.write_record   r0   r   c                 D    d}|D ]  }| j                  |       |dz  } |S )zWrite records to the output file, and return the number of records.

        records - A list or iterator returning SeqRecord objects
        r      )rf   )r   rc   countrC   s       r   write_recordszSequenceWriter.write_records   s6    
  	Ff%QJE	 r   c                     	 | j                  |      }| j                  | j                  ur| j                  j                          |S # | j                  | j                  ur| j                  j                          w w xY w)zWrite a complete file with the records, and return the number of records.

        records - A list or iterator returning SeqRecord objects
        )rj   rZ   rT   r7   )r   rc   ri   s      r   r_   zSequenceWriter.write_file   se    
	$&&w/E{{$++-!!# {{$++-!!# .s   A 5A<)r<   r=   r>   r?   r@   r   r   rA   r-   rB   r]   classmethodrd   rf   rj   r_   r   r   r   rR   rR      sl    	   y T 8:# :# : ! !	
r   rR   N)r?   abcr   r   ior   osr   typingr   r   r	   r
   r   Bior   Bio.Seqr   r   Bio.SeqRecordr   rB   bytesr&   rA   _TextIOSource_BytesIOSourcer   rH   rP   rR   r   r   r   <module>rw      s                # C'vJ!C'%/	#5!dsGFO dNI # 6 6 6
\S'&/ \r   