o
    Df                     @   s   d Z ddlmZ ddlmZmZmZ ddlmZ	 ddl
mZmZmZ dded	ee d
efddZdded	ee d
efddZddeded
ee fddZ	ddededed
eeeef  fddZdS )z(Chunked reading of FASTA and FASTQ files    )	RawIOBase)OptionalIteratorTuple   )paired_fastq_heads)FileFormatErrorFastaFormatErrorUnknownFileFormatNbufendreturnc                 C   s>   |  dd|}|dkr|d S | dd dkrdS tddd)	z
    Search for the end of the last complete FASTA record within buf[:end]

    Return an integer length such that buf[:length] contains the highest
    possible number of complete FASTA records.
    s   
>r   r      >zFile does not start with ">"Nline)rfindr	   )r   r   pos r   U/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dnaio/chunks.py_fasta_head	   s   r   c                 C   s>   |  dd|}|}t|d d D ]	}| dd|}q|d S )z
    Search for the end of the last complete *two* FASTQ records in buf[:end].

    Two FASTQ records are required to ensure that read pairs in interleaved
    paired-end data are not split.
       
r      r   )countranger   )r   r   Z
linebreaksright_r   r   r   _fastq_head   s
   r     @ fbuffer_sizec                 c   s6   t |}| t|dd }|dkr|dd dkrt}n!|dkr6|dd dks3|dd dkr6t}n
|dkr<dS td	 |t|krKtd	| t||d | }||kr]n-|||}||kshJ |dkrut|d| V  || }|dksJ ||| |d|< qA|dkrt|d| V  dS dS )
z
    Read a chunk of complete FASTA or FASTQ records from a file.
    The size of a chunk is at most buffer_size.
    f needs to be a file opened in binary mode.

    The yielded memoryview objects become invalid on the next iteration.
    r   r      @   #r   NzInput file format unknownTz+FASTA/FASTQ record does not fit into buffer)	bytearrayreadinto
memoryviewr   r   r
   lenOverflowError)r   r    r   startheadZbufendr   r   r   r   read_chunks)   s6   	(
r*   f2c                 c   s   |dk r	t dt|}t|}| t|dd }|t|dd }|dkr3|dd dks?|dkrE|dd dkrEtdd d	 |t|ksR|t|krVt d| t||d  | }|t||d  | }||kry||krynWt||||\}	}
|	|ksJ |
|ksJ |	dks|
dkrt|d|	 t|d|
 fV  ||	 }|dksJ ||	| |d|< ||
 }|dksJ ||
| |d|< qF|dks|dkrt|d| t|d| fV  d S d S )	Nr   zBuffer size too smallr   r!   zAPaired-end data must be in FASTQ format when using multiple coresr   Tz%FASTQ record does not fit into buffer)
ValueErrorr#   r$   r%   r   r&   _paired_fastq_heads)r   r+   r    Zbuf1Zbuf2Zstart1Zstart2Zbufend1Zbufend2Zend1Zend2r   r   r   read_paired_chunksa   sB   0"&r.   )N)r   )__doc__ior   typingr   r   r   Z_corer   r-   
exceptionsr   r	   r
   bytesintr   r   r%   r*   r.   r   r   r   r   <module>   s$    ;