o
    Df                     @   sv   d dl mZ d dlmZmZmZ d dlmZ ddlmZ ddl	m
Z
 G dd dZG d	d
 d
eZG dd deZdS )    )PathLike)UnionBinaryIOOptional)xopen   )Sequence)_is_pathc                   @   sZ   e Zd Zedfdeeeef dee	 fddZ
defddZdd	d
Zdd Zdd ZdS )
FileWriterNfile_close_filec                 C   s2   t |r||d| _d| _d S || _t|| _d S )NwbT)r	   _file_close_on_exitbool)selfr   openerr    r   V/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dnaio/writers.py__init__   s
   
zFileWriter.__init__returnc                 C   s   d | jjt| jd| jS )Nz{}({!r})name)format	__class____name__getattrr   r   r   r   r   __repr__   s   zFileWriter.__repr__c                 C   s   | j r
| j  d S d S N)r   r   closer   r   r   r   r      s   zFileWriter.closec                 C   s   | j jrtd| S )NzI/O operation on closed file)r   closed
ValueErrorr   r   r   r   	__enter__   s   zFileWriter.__enter__c                 G   s   |    d S r   )r   )r   argsr   r   r   __exit__$   s   zFileWriter.__exit__)r   N)r   
__module____qualname__r   r   r   strr   r   r   r   r   r   r"   r$   r   r   r   r   r
   
   s    

r
   c                       sj   e Zd ZdZdedfdeeeef de	e
 de	e f fddZdefd	d
Zdde	e fddZ  ZS )FastaWriterz4
    Write FASTA-formatted sequences to a file.
    Nr   line_lengthr   c                    s.   t  j|||d |dkr|| _dS d| _dS )zm
        If line_length is not None, the lines will
        be wrapped after line_length characters.
        r   r   r   N)superr   r)   )r   r   r)   r   r   r   r   r   r   -   s   zFastaWriter.__init__r   c                 C      d t| jd| jS )NzFastaWriter('{}')r   r   r   r   r   r   r   r   r   ;      zFastaWriter.__repr__sequencec                 C   s   |du r|j }|j}n|}| jdurI| jd| d d g }tdt|| jD ]}||||| j  d  q*| jd	|d dS d| d | d }| j|d dS )a  Write an entry to the the FASTA file.

        If only one parameter (name_or_record) is given, it must have
        attributes .name and .sequence, which are then used.
        Otherwise, the first parameter must be the name and the second
        the sequence.

        The effect is that you can write this:
        writer.write("name", "ACCAT")
        or
        writer.write(Sequence("name", "ACCAT"))
        N>
asciir    )
r   r0   r)   r   writeencoderangelenappendjoin)r   Zname_or_recordr0   r   sitextr   r   r   r5   >   s   
zFastaWriter.writer   )r   r%   r&   __doc__r   r   r   r'   r   r   intr   r   r   r5   __classcell__r   r   r,   r   r(   (   s    r(   c                       s   e Zd ZdZdZdedfdeeee	f de
dee
 f fdd	Zd
efddZded
dfddZded
dfddZdededed
dfddZ  ZS )FastqWriterz
    Write sequences with qualities in FASTQ format.

    FASTQ files are formatted like this:
    @read name
    SEQUENCE
    +
    QUALITIS
    r   FNr   two_headersr   c                    s6   t  j|||d || _| jr| j| _d S | j| _d S )Nr*   )r+   r   Z_two_headers_write_two_headers_writer5   )r   r   rB   r   r   r,   r   r   r   h   s   zFastqWriter.__init__r   c                 C   r-   )NzFastqWriter('{}')r   r.   r   r   r   r   r   s   r/   zFastqWriter.__repr__recordc                 C      | j |  dS )z=
        Write a Sequence record to the FASTQ file.

        N)r   r5   Zfastq_bytesr   rE   r   r   r   rD   v      zFastqWriter._writec                 C   rF   )z{
        Write a Sequence record to the FASTQ file, repeating the header
        in the third line after the "+" .
        N)r   r5   Zfastq_bytes_two_headersrG   r   r   r   rC   }   rH   zFastqWriter._write_two_headersr   r0   	qualitiesc                 C   s    | j d|||d d S )Nz@{0:s}
{1:s}
+
{2:s}
r3   )r   r5   r   r6   )r   r   r0   rI   r   r   r   writeseq   s
   
zFastqWriter.writeseq)r   r%   r&   r>   Z	file_moder   r   r   r'   r   r   r   r   r   r   rD   rC   rJ   r@   r   r   r,   r   rA   \   s"    	"rA   N)osr   typingr   r   r   r   r4   r   Z_utilr	   r
   r(   rA   r   r   r   r   <module>   s    4