U
    _v                     @   sF   d dl Z d dlZd dlZd dlmZmZ d dlmZ G dd dZdS )    N)strftime	localtime)versionc                   @   sJ   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dS )	GFFWritera  
    Simple GFF writer class for serializing gffutils
    records to a file.

    Parameters:
    -----------

    out: string or file-like object
        If a string, parsed as a filename, otherwise, a file-like object to
        write to.

    with_header: bool
        If True, output a header file for the GFF.  The header indicates the
        file was created with gffutils and includes a timestamp and version
        info.

    in_place: bool
        If True and if `out` is a filename, then write the file in place (uses
        named temporary files.)

    TODO: Add make separate GTFWriter class or add support
    for GTF here.
    TFc                 C   s   || _ || _|| _d | _d | _t|tjr`| jrPtj	dd| _t
| jjd| _qtt
| j d| _n| jrntd|| _| jrtdt }dt|f }| jd|  d S )NF)deletewz/Cannot use 'in_place' when writing to a stream.z%Y-%m-%d %H:%M:%Sz,#GFF3 file (created by gffutils (v%s) on %s)%s
)outwith_headerin_place	temp_file
out_stream
isinstancesixZstring_typestempfileZNamedTemporaryFileopenname
ValueErrorr   r   r   write)selfr	   r
   r   Z	timestampheader r   1lib/python3.8/site-packages/gffutils/gffwriter.py__init__#   s&    zGFFWriter.__init__c                 C   s   | j d|  dS )z(
        Output record to file.
        r   N)r   r   )r   recr   r   r   	write_recB   s    zGFFWriter.write_recc                 C   s   |D ]}|  | qdS )z1
        Output several records to file.
        N)r   )r   Zrecsr   r   r   r   
write_recsH   s    zGFFWriter.write_recsc                 C   s   || }|  | i }t|j|dd}|j|ddD ]&}tdd |j|ddD ||j< q6t| dd dd	}|D ]*}|d
 }	||	 }
|  |
 | ||	 qx|j|ddD ]}|jdkr|  | qdS )a  
        NOTE: The goal of this function is to have a canonical ordering when
        outputting a gene and all of its records to a file.  The order is
        intended to be:

        gene
          # mRNAs sorted by length, with longest mRNA first
          mRNA_1
            # Exons of mRNA, sorted by start position (ascending)
            exon_1
              # Children of exon, sorted by start position
              exon_child_1
              exon_child_2
            exon_2
              ...
            # Non-exonic children here
            ...
          mRNA_2
            ...
          # Non-mRNA children here
          ...

        Output records of a gene to a file, given a GFF database
        and a gene_id. Outputs records in canonical order: gene record
        first, then longest mRNA, followed by longest mRNA exons,
        followed by rest, followed by next longest mRNA, and so on.

        Includes the gene record itself in the output.

        TODO: This probably doesn't handle deep GFF hierarchies.
        mRNA)featuretypec                 s   s   | ]}t |V  qd S )N)len).0exonr   r   r   	<genexpr>w   s     z,GFFWriter.write_gene_recs.<locals>.<genexpr>r!   c                 S   s   | d S )N   r   )xr   r   r   <lambda>{       z+GFFWriter.write_gene_recs.<locals>.<lambda>T)keyreverser   r#   )levelN)	r   listchildrensumidsorteditemswrite_mRNA_childrenr   )r   dbZgene_idZgene_recZ	mRNA_lenscr   Zsorted_mRNAsZ	curr_mRNAmRNA_idZmRNA_recZ
gene_childr   r   r   write_gene_recsO   s&     




zGFFWriter.write_gene_recsc                 C   sV   |j |dd}g }|D ]0}|jdkr<| | | || q|| q| | dS )z
        Write out the children records of the mRNA given by the ID
        (not including the mRNA record itself) in a canonical
        order, where exons are sorted by start position and given
        first.
        startZorder_byr!   N)r+   r   r   write_exon_childrenappendr   )r   r1   r3   ZmRNA_childrenZnonexonic_childrenZ	child_recr   r   r   r0      s    

zGFFWriter.write_mRNA_childrenc                 C   s&   |j |dd}|D ]}| | qdS )z|
        Write out the children records of the exon given by
        the ID (not including the exon record itself).
        r5   r6   N)r+   r   )r   r1   Zexon_idZexon_childrenZ
exon_childr   r   r   r7      s    zGFFWriter.write_exon_childrenc                 C   s&   | j   | jr"t| jj| j dS )zF
        Close the stream. Assumes stream has 'close' method.
        N)r   closer   shutilmover   r   r	   )r   r   r   r   r9      s    
zGFFWriter.closeN)TF)__name__
__module____qualname____doc__r   r   r   r4   r0   r7   r9   r   r   r   r   r      s   
9	r   )	r   r   r:   timer   r   Zgffutils.versionr   r   r   r   r   r   <module>   s
   