3
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
 yd dlmZ W n  ek
rt   d dlmZ Y nX 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}| r*|  r*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| s&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 | j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   r   compile_aligner9   s    
r   c             C   s   t |  t | kS )N)ref_labels_by_chromosomes)Zchr1Zchr2r   r   r   is_same_reference@   s    r    c             C   s   | t krt |  S dS )N )r   )Zchromr   r   r   get_ref_by_chromosomeD   s    r"   c             C   s   t jd}|j| 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}x8| D ]0}dd t|d gdd |d D  |D }qW t||rNdnd}xH| D ]@}|jd	jd
d t||d gdd |d D  D d  qZW d S )Nr   c             S   s   g | ]\}}t t||qS r   )maxlen).0vwr   r   r   
<listcomp>P   s    zprint_file.<locals>.<listcomp>
metricNamec             S   s   g | ]}t |qS r   )r
   )r)   r*   r   r   r   r,   P   s    valuesar+   z  c             s   s   | ]\}}d ||f V  qdS )z%-*sNr   )r)   colwidthcellr   r   r   	<genexpr>S   s    zprint_file.<locals>.<genexpr>c             S   s   g | ]}t |qS r   )r
   )r)   r*   r   r   r   r,   T   s    
)r   zipopenwriter   )all_rowsfpathZappend_to_existing_file	colwidthsrowtxt_filer   r   r   
print_fileM   s    
0
r<   c             C   sZ   t | tj}t|stj| tjrVddlm} |j	rVt |d}tj
j|sVtj| |S )Nr   )search_references_metaraw)r   r   aligner_output_dirnamer   osmkdiris_combined_ref
quast_libsr=   Zis_quast_first_runpath)
output_dirZminimap_output_dirr=   r   r   r   create_minimap_output_dirW   s    


rF   c             C   s&   x t | j D ]}|r|j  qW d S )N)varsr.   close)Z	ca_outputhandlerr   r   r   close_handlersd   s    rJ   rtc             C   s   t jj|  rd|kr| d7 } | jds2| jdrd|krB|d7 }ytj| |d}W n tk
rp   t| |dS X d|kr~|S y|jd W n$ tk
r   |j  t| |dS X |j  tj| |d}|S nt| |dS d S )Nrz.gzz.gzipt)moder+   r   )	r@   rD   existsendswithgzipr5   IOErrorreadrH   )frN   hr   r   r   open_gzipsafej   s(    rV   )F)F)F)F)rK   ))
__future__r   rQ   r@   r#   	itertoolsr   os.pathr   r   r   collectionsr   ImportErrorZ%quast_libs.site_packages.ordered_dictrC   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&   r<   rF   rJ   rV   r   r   r   r   <module>   s8   





