B
    7]D                 @   sN   d Z ddlmZ ddlmZmZmZ dd ZdddZ	dd
dZ
dddZdS )z(Chunked reading of FASTA and FASTQ files   )paired_fastq_heads)FileFormatErrorFastaFormatErrorUnknownFileFormatc             C   sB   |  dd|}|dkr|d S | dd dkr2dS tddddS )	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      >zFile does not start with ">"N)line)rfindr   )bufendpos r   X/home/fbrian/BIRCH/lib-linux-x86_64/python/lib64/python3.7/site-packages/dnaio/chunks.py_fasta_head   s    r   Nc             C   sB   |  dd|}|}x&t|d d D ]}| dd|}q$W |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     @ c             c   s4  t |}| t|dd }|dkr<|dd dkr<t}nB|dkrj|dd dksd|dd dkrjt}n|dkrvdS tdx|t|krtd| t||d | }||krP |||}||kst|dkrt|d| V  || }|dkst||| |d|< qW |dkr0t|d| V  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 unknownz+FASTA/FASTQ record does not fit into buffer)		bytearrayreadinto
memoryviewr   r   r   lenOverflowErrorAssertionError)fbuffer_sizer   startheadZbufendr   r   r   r   read_chunks'   s0    	(

r%   c             c   s  |dk rt dt|}t|}| t|dd }|t|dd }|dkrd|dd dks||dkr|dd dkrtdd dx"|t|ks|t|krt d| t||d  | }|t||d  | }||kr||krP t||||\}	}
|	|kst|
|ks t|	dks4|
dkrVt|d|	 t|d|
 fV  ||	 }|dkslt||	| |d|< ||
 }|dkst||
| |d|< qW |dks|dkrt|d| t|d| fV  d S )Nr   zBuffer size too smallr   r   zAPaired-end data must be in FASTQ format when using multiple cores)r	   z%FASTQ record does not fit into buffer)
ValueErrorr   r   r   r   r   _paired_fastq_headsr    )r!   f2r"   Zbuf1buf2Zstart1Zstart2Zbufend1Zbufend2Zend1Zend2r   r   r   read_paired_chunks_   s:    0
"r*   )N)r   )r   )__doc__Z_corer   r'   
exceptionsr   r   r   r   r   r%   r*   r   r   r   r   <module>   s   

8