U
    ñadÈO  ã                   @   sŒ  d dl Z d dlZd dlZzd dlmZ W n  ek
rH   d dlmZ Y nX d dlmZ e 	¡  d dl
mZ eej ejd¡ƒ d dlmZmZmZmZmZmZ d dlmZmZmZ d dlmZmZmZmZmZ d d	lm Z m!Z!m"Z"m#Z# d d
l$m%Z% e%ej&ƒZ'e' (¡  ddd„Z)ddd„Z*dd„ Z+e,dkrˆze+e j-dd… ƒZ.e/e.ƒ W n> e0k
r†   e  1¡ \Z2Z3Z2e' 4e3¡ e'j5dddd Y nX dS )é    N)ÚOrderedDict)Úqconfig)Ú
addsitedirZsite_packages)ÚAssemblyÚcorrect_meta_referencesÚcorrect_assembliesÚ#get_downloaded_refs_with_alignmentsÚpartition_contigsÚcalculate_ave_read_support)Úparse_optionsÚremove_from_quast_py_argsÚprepare_regular_quast_args)Úcontigs_analyzerÚsearch_references_metaÚplotter_dataÚqutilsÚ	run_busco)ÚcleanupÚcheck_dirpathÚ
is_python2Úrun_parallel)Ú
get_loggerFc	              	   C   s’  | d d … } |   dd„ |D ƒ¡ |r8|  d¡ |  |¡ |rP|  d¡ |  |¡ |  d¡ dd„ }	|  |	d d	d„ |D ƒ¡ƒ¡ d
d l}
zd
d l}| |
¡ W n  ttfk
r¾   t|
ƒ Y nX |
jj	dt
jd |s|rîtj t |¡¡nd }|
jj||d |r&t dtj |t
jd ¡ ¡ |
 | ¡}|rT|
j ¡ }tttt||ƒƒƒ}|rŠdd„ t
jD ƒ|d d …< dd„ t
jD ƒ|d d …< ||fS )Nc                 S   s   g | ]
}|j ‘qS © )Úfpath©Ú.0Zasmr   r   úopt/quast-5.2.0/metaquast.pyÚ
<listcomp>(   s     z%_start_quast_main.<locals>.<listcomp>z-Rz-oz--labelsc                 S   s   d| krd|  } | S )Nú z"%s"r   )Úliner   r   r   Úquote4   s    z _start_quast_main.<locals>.quotez, c                 S   s   g | ]
}|j ‘qS r   )Úlabelr   r   r   r   r   9   s     r   é   )Z
indent_valÚdebug)Úis_parallel_runÚref_namez(logging to ú.log)c                 S   s   g | ]}t j| ‘qS r   )r   Úassembly_labels_by_fpath©r   r   r   r   r   r   N   s     c                 S   s   g | ]}t |tj| ƒ‘qS r   )r   r   r'   r(   r   r   r   r   O   s     )ÚextendÚappendÚjoinÚquastÚ	importlibÚreloadÚImportErrorÚAttributeErrorÚloggerÚset_up_console_handlerr   r#   ÚosÚpathÚbasenamer   Úname_from_fpathZset_up_metaquastZinfo_to_fileÚLOGGER_DEFAULT_NAMEÚmainZget_numbers_of_notificationsÚlistÚmapÚsumÚzipZassemblies_fpaths)ÚargsÚ
assembliesÚreference_fpathÚoutput_dirpathÚnum_notifications_tupleÚlabelsÚrun_regular_quastÚis_combined_refr$   r    r,   r-   Zreference_nameÚreturn_codeZcur_num_notificationsr   r   r   Ú_start_quast_main$   sB    




ÿ

rF   c                 C   sü   t  |¡}|s~t d| d ¡ tj ||¡}tj |¡sDt |¡ d }t	j
rtddlm}	m}
 |	 |t	j|¡ |
j}|||fS tj ||¡}d| }t d| d tj |t	j¡ d ¡ t| |||||d	\}}d }t	j
rîdd
lm}
 |
j}|||fS d S )Nz*
No contigs were aligned to the reference z, skipping..r   )Ú
html_saverÚ
json_saverzfor the contigs aligned to z
Starting quast.py ú... (logging to r&   )r>   r?   r@   rA   r$   ©rH   )r   r6   r1   Ú	main_infor3   r4   r+   ÚisdirÚmakedirsr   Úhtml_reportÚquast_libs.html_saverrG   rH   Zsave_empty_reportZ
min_contigÚ	json_textr7   rF   )Úquast_py_argsÚoutput_dirpath_per_refÚ	ref_fpathÚref_assembliesÚtotal_num_notificationsr$   r%   r@   rP   rG   rH   Úrun_namerE   r   r   r   Ú_run_quast_per_refT   sB    



ÿÿÿû
rW   c           :   	      s$
  t tjdttjƒ d d dd | s>tjtjd t d¡ tj	 
t¡g}tt||  ƒ\‰}tjtjtj  }}}tj}tj}tj|d | dd	\}}}tj	 |tj¡}	t t¡ tt_d
dlm}
 zd
d l}| |
¡ W n  ttfk
rü   t|
ƒ Y nX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  t #d@¡ tUdtjTtS|$ƒ ƒ}'ˆdAg7 ‰ˆdBt|'ƒg7 ‰dC‰ ‡ ‡‡fdDd„|$D ƒ}(tVtW|(tjTddE\}})}*tXtYtZt[|*Ž ƒƒ}+tXtYtZt[||+ƒƒƒ}|d k	rd| \|)¡ ˆ CdA¡ tPˆdBt|'ƒƒ‰nRg }|$D ]H\},}-tWˆˆ|,|-|ƒ\}.}/}|.s°qˆ| <|.¡ |d k	rˆ| <|/¡ qˆd}0|%D ]4}1tj	 ]|1j^¡rÚt _|1j^¡j`d
krÚd}0 qqÚdF}t #¡  |0r6t #dG| dH ¡ n˜t #d(| dI tj	 |tjatjbdJ ¡ ¡ t3ˆdBttjTƒg |%tj	 |tja¡|dK\}}|dLkr¤t 'dM¡ n|dkr²d}0|0sÎ|d k	rÎ| <|j=¡ |	röt c¡  t #dN¡ tj	 |tjd¡}2tj	 |2¡	st e|2¡ |	r2|	r2d
dOl9mf}3 |3 g|¡}4nd }4d
dPlmh}5 |
jijj}6|
jijk|
jijl|
jijm|
jijng}7|0	r|dQd„ |D ƒ}8ndRd„ |D ƒtjag }8|5 1|4|2|ˆ|6|7|8¡ |	rö|	rö|3jo||t7j8ddS tjp	rê|3 q||¡}9t #dT|9 ¡ |3 r||¡ t?|	ƒ t #d0¡ t #d4¡ tj@tA|ƒ|d5S )UNzYou are trying to run it from z.
z<Please, put QUAST in a different directory, then try again.
é   )Z	exit_code)Ústreamr"   F)Z	save_jsonr   )Ú	reporting)ÚplotterzReference(s):zContigs:TzxNone of the assembly files contains correct contigs. Please, provide different files or decrease --min-contig threshold.é   z--mgmz--min-identityzjMaximum number of references (--max-ref-number) is set to 0, search in SILVA 16S rRNA database is disabledzTList of references was provided, starting to download reference genomes from NCBI...zNo references are provided, starting to search for reference genomes in SILVA 16S rRNA database and to download them from NCBI...zDownloaded reference(s):)Údownloaded_refszjFailed to download or setup SILVA 16S rRNA database for working without references on metagenome datasets!)Ú	to_stderrÚexit_with_codezPNo references are provided, starting regular QUAST with MetaGeneMark gene finderc                 S   s   g | ]}t |t |¡ƒ‘qS r   )r   r   Zlabel_from_fpathr(   r   r   r   r   Ù   s     zmain.<locals>.<listcomp>)r>   r@   rC   z--sv-bedz--combined-refc                 S   s   g | ]}t j|j ‘qS r   )r   Údict_color_and_lsr!   r   r   r   r   r   è   s     z--colorsú,c                 S   s   g | ]}|d  ‘qS )r   r   ©r   Zstyler   r   r   r   ê   s     z--lsc                 S   s   g | ]}|d  ‘qS )r"   r   rb   r   r   r   r   ì   s     zfor the combined referencezStarting quast.py z...rJ   z--ambiguity-usageÚall)rB   r>   r?   r@   rA   rD   Zgenome_statszgenome_info.txtÚ z1Try to restart MetaQUAST with another references.zjTry to use option --max-ref-number to change maximum number of references (per each assembly) to download.z4Failed aligning the contigs for all the references. zMetaQUAST finished.)ZnumbersZ
check_testzPExcluding downloaded references with low genome fraction from further analysis..zFiltered reference(s):z$for the corrected combined referenceéÿÿÿÿzSAll downloaded references have genome fraction more than 10%. Nothing was excluded.zQAll downloaded references have low genome fraction. Nothing was excluded for now.z:Partitioning contigs into bins aligned to each reference..zalignments_%s.tsv)Ú	is_fungusz--conservativez/Run QUAST on different references in parallel..z--memory-efficientz-t)r   r   r   c                    s    g | ]\}}ˆˆ||ˆ d f‘qS )Tr   )r   rS   rT   ©Znum_notificationsrR   rQ   r   r   r   U  s   ÿ)Zfilter_resultsz$for the contigs not aligned anywherezSkipping quast.py z (everything is aligned!)rI   r&   )r>   r@   rA   )r   r\   z;Error running quast.py for the contigs not aligned anywherezSummarizing results...)rG   )Úcreate_meta_summaryc                 S   s   g | ]}t  |¡‘qS r   ©r   r6   ©r   rS   r   r   r   r   —  s     c                 S   s   g | ]}t  |¡‘qS r   ri   rj   r   r   r   r   ™  s     )Úmetaz(  Icarus (contig browser) is saved to %s)sr   r   Z
QUAST_HOMEÚstrZusageÚsysÚstderrÚexitr3   r4   ÚrealpathÚ__file__r   r1   r@   Z	referencerB   rN   Ztestr   Zset_up_output_dirr+   Zcorrected_dirnameZset_max_threadsÚ
quast_libsrZ   r-   r.   r/   r0   r[   rL   ÚshutilZrmtreeÚmkdirrK   r   Zno_check_metar   ÚerrorZgene_findingZmin_IDYZMETA_MIN_IDYÚreuse_combined_alignmentsZmax_referencesZnoticeZreferences_txtZdownloaded_dirnamer   ZdoZis_quast_first_runrF   Zcombined_output_nameZbedZ
draw_plotsr   r`   rO   rH   Zunique_mappingr*   rP   Úexistsr   Z	finish_upÚtupler   Úremover   r   Zref_labels_by_chromosomesZcalculate_read_supportr
   r   r	   Z detailed_contigs_reports_dirnamer   Zdownload_dbZ
prokaryoterf   r   Zper_ref_dirnameZmemory_efficientÚlenZmax_threadsÚmaxr   rW   r9   r:   r;   r<   r)   Úisfiler   ÚstatÚst_sizeZnot_aligned_namer7   Zprint_timestampZmeta_summary_dirrM   rG   Zinit_meta_reportrh   ZFieldsZmain_metricsZMIS_RELOCATIONZMIS_TRANSLOCATIONZMIS_INVERTIONZMIS_ISTRANSLOCATIONSZsave_colorsZcreate_icarus_htmlZcreate_meta_icarusZcreate_meta_report):r=   Zmetaquast_pathZcontigs_fpathsr@   Z
ref_fpathsrB   rN   Z	test_modeÚ_Zcorrected_dirpathrZ   r-   r[   Zcorrected_ref_fpathsZcombined_ref_fpathZchromosomes_by_refsZ	ref_namesr>   rv   r]   Zdownloaded_dirpathZcombined_output_dirpathZcolors_and_lsrV   Ztotal_num_noticesZtotal_num_warningsZtotal_num_nf_errorsrU   rH   Z
json_textsZambiguity_optsrE   Zgenome_info_dirpathZgenome_info_fpathÚmsgZcorr_ref_fpathsZassemblies_by_referenceZnot_aligned_assembliesZ
db_dirpathZthreads_per_refZparallel_run_argsZref_json_textsZref_notificationsZper_ref_num_notificationsrS   rT   r%   rP   Zno_unaligned_contigsZassemblyZsummary_output_dirpathrG   Zhtml_summary_report_fpathrh   Zmetrics_for_plotsZmisassembly_metricsZfull_ref_namesZicarus_html_fpathr   rg   r   r8   t   s"   ÿÿ
  þ



ÿ






ÿ ÿ





úÿ	






ÿ
úÿ






   þ
ÿÿ

ÿÿ


ÿ

$
ÿý





ÿ
   ÿ

r8   Ú__main__r"   zexception caught!T)r_   r^   )NNNNFFF)F)6rm   r3   rs   Úcollectionsr   r/   Z%quast_libs.site_packages.ordered_dictrr   r   Zcheck_python_versionZsiter   r4   r+   ZLIBS_LOCATIONZquast_libs.metautilsr   r   r   r   r	   r
   Zquast_libs.options_parserr   r   r   r   r   r   r   r   Zquast_libs.qutilsr   r   r   r   Zquast_libs.logr   ZLOGGER_META_NAMEr1   r2   rF   rW   r8   Ú__name__ÚargvrE   ro   Ú	ExceptionÚexc_infor   Ú	exc_valueZ	exceptionru   r   r   r   r   Ú<module>
   sF    
        ÿ
0
   7

