a
    bJ                     @   s*  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 zd dlmZ W n eyr   d dlmZ Y n0 d dlmZ d dlmZmZmZ d dlmZ eejZe	ejd	Ze Zi Zi Zd
d Zd"ddZd#ddZ d$ddZ!dd Z"dd Z#dd Z$d%ddZ%dd Z&dd Z'd&d d!Z(dS )'    )with_statementN)repeat)isdirjoinbasename)OrderedDict)qconfig)compile_tool
val_to_strget_path_to_program)
get_loggerminimap2c                 C   s
   t t| S N)r   contig_aligner_dirpath)fname r   B/home/psgendb/BIRCHDEV/pkg/quast-5.2.0/quast_libs/ca_utils/misc.py	bin_fpath#   s    r   Fc                 C   s*   t dtddd}|s&| s&tjddd |S )Nr   z2.19z2.24)min_versionrecommend_versiona  Critical! Tried to use minimap2, but it was unavailable despite QUAST's sincere expectations! E.g., the binary file is present but corrupted.
Possible workarounds:
1. Remove the minimap2 binary and restart QUAST (it will automatically try to recompile it): rm -f <quast_installation_dir>/quast_libs/minimap2/minimap2
2. Go to <quast_installation_dir>/quast_libs/minimap2/ and recompile minimap2 manually
3. Install a proper version of minimap2 and add it to your PATH environment variable   )exit_with_code)r   r   loggererror)
just_checkminimap_fpathr   r   r   r   '   s    r   c                 C   s,   t ddr|r$tdtdgd| |dr(dS dS )NT)r   ZMinimap2r   F)just_noticer   
only_clean)r   r	   r   r   r   r   r   r   compile_minimap2   s
    r   c                 C   s   t | |drdS | d dS )N)r   Tz]Compilation of contig aligner software was unsuccessful! QUAST functionality will be limited.F)r   r   r   r   r   r   compile_aligner9   s    
r    c                 C   s   t |  t | kS r   ref_labels_by_chromosomes)Zchr1Zchr2r   r   r   is_same_reference@   s    r#   c                 C   s   | t v rt |  S dS )N r!   )Zchromr   r   r   get_ref_by_chromosomeD   s    r%   c                 C   s   t d}|| S )Nz%:\d+|\*[acgtn]+|\-[acgtn]+|\+[acgtn]+)recompilefindall)ZcigarZ
cs_patternr   r   r   parse_cs_tagH   s    
r)   c                 C   s   t d}| D ]0}dd t|d gdd |d D  |D }qt||rJdnd}| D ]@}|d	d
d t||d gdd |d D  D d  qTd S )Nr   c                 S   s   g | ]\}}t t||qS r   )maxlen).0vwr   r   r   
<listcomp>P       zprint_file.<locals>.<listcomp>
metricNamec                 S   s   g | ]}t |qS r   r
   r,   r-   r   r   r   r/   P   r0   valuesar.   z  c                 s   s   | ]\}}d ||f V  qdS )z%-*sNr   )r,   colwidthcellr   r   r   	<genexpr>S   r0   zprint_file.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   r2   r3   r   r   r   r/   T   r0   
)r   zipopenwriter   )all_rowsfpathZappend_to_existing_file	colwidthsrowtxt_filer   r   r   
print_fileM   s    . rB   c                 C   sZ   t | tj}t|st| tjrVddlm} |j	rVt |d}tj
|sVt| |S )Nr   )search_references_metaraw)r   r   aligner_output_dirnamer   osmkdiris_combined_ref
quast_libsrC   Zis_quast_first_runpath)
output_dirZminimap_output_dirrC   r   r   r   create_minimap_output_dirW   s    


rL   c                 C   s"   t |  D ]}|r|  qd S r   )varsr4   close)Z	ca_outputhandlerr   r   r   close_handlersd   s    rP   rtc                 C   s   t j| sd|v r| d7 } | ds0| drd|vr@|d7 }ztj| |d}W n typ   t| |d Y S 0 d|v r~|S z|d W n& ty   |  t| |d Y S 0 |  tj| |d}|S nt| |dS d S )Nrz.gzz.gzipt)moder.   r   )	rF   rJ   existsendswithgzipr;   IOErrorreadrN   )frT   hr   r   r   open_gzipsafej   s(    r\   )F)F)F)F)rQ   ))
__future__r   rW   rF   r&   	itertoolsr   os.pathr   r   r   collectionsr   ImportErrorZ%quast_libs.site_packages.ordered_dictrI   r   quast_libs.qutilsr	   r
   r   quast_libs.logr   LOGGER_DEFAULT_NAMEr   LIBS_LOCATIONr   r"   Z!intergenomic_misassemblies_by_asmZcontigs_aligned_lengthsr   r   r   r    r#   r%   r)   rB   rL   rP   r\   r   r   r   r   <module>   s8   





