3
bV                 @   s   d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZ ee
jZi ZG dd dZd	d
 Zdd Zdd Zdd ZG dd dZdS )    )with_statementN)defaultdict)fastaparsergenes_parser	reportingqconfigqutils)
get_logger)run_parallelc               @   s   e Zd ZdddZdS )FeatureContainer c             C   s   || _ || _g | _i | _d S )N)kindfpathsregion_listchr_names_dict)selfr   r    r   D/home/psgendb/BIRCHDEV/pkg/quast-5.2.0/quast_libs/genome_analyzer.py__init__   s    zFeatureContainer.__init__N)r   )__name__
__module____qualname__r   r   r   r   r   r      s   r   c               C   s   t S )N)ref_lengths_by_contigsr   r   r   r   get_ref_aligned_lengths   s    r   c             C   s   i }x.|D ]&}|j |kr&|j ||j < q
d||j < q
W t|dkrt|dkr||d j  dkr|j }tjd| |d j |f dd xf|D ]}||_ |||j < qW nHtdd |j D rtjd	|  dd nd|j krtjd
|  dd |S )z
    returns dictionary to translate chromosome name in list of features (genes or operons) to
    chromosome name in reference file.
    N   r   zReference name in file with genomic features of type "%s" (%s) does not match the name in the reference file (%s). QUAST will ignore this issue and count as if they match.z  )indentc             s   s   | ]}|d kV  qd S )Nr   ).0chr_namer   r   r   	<genexpr>8   s    z)chromosomes_names_dict.<locals>.<genexpr>z{Reference names in file with genomic features of type "%s" do not match any chromosome. Check your genomic feature file(s).zSome of the reference names in file with genomic features of type "%s" does not match any chromosome. Check your genomic feature file(s).)seqnamelenpoploggernoticeallvalueswarning)featureregions	chr_namesZregion_2_chr_nameregionr   r   r   r   chromosomes_names_dict"   s(    

*


r+   c       2         s  t j| }t j| }t }	tt}
tjdt j| |  t	j
j||d }tjrX|}n|d }t	j
j|stjd| d dd d4S i }x&|j D ]\}}dg|d  ||< qW tj| }tt|d	d
 dd}g }g }x(|D ] \}\}}|j| |j| qW dgt|  dgt| i }t|}tjrD|rDtjd |rbx|D ]}g ||< qPW t|
}x |D ]}t|jdd j d }t|jdd j d }t|jdd j d }t|jdd j d }|j d j }|j d j }||kr"tjd| d  d S |rF|| jt||||||d x$t||d D ]} d|| | < qVW qvW W d Q R X xB|j D ]6}x|| D ]} d|| | < qW t|| |
|< qW tj r|j!drt	j"| d}!tj#rtj st	j
j||d nd}"t|"d}#x|j D ]\}}|#j$|d  d}$xtd|d D ]n} || |  dksl| || kr|$tj%kr|!d7 }!|#j$t&| |$ d t&| d  d  d}$n|$d7 }$qHW |$tj%kr|!d7 }!|#j$t&||$ d d t&| d  qW W d Q R X |!|	d< d |	t'j(j)d < d |	t'j(j)d < d |	t'j(j*d < d |	t'j(j*d < x|D ]}%|%j+sfqTd}&d}'t	j
j||d |%j,j-  d }(t|(d})|)j$dd5  |)j$d$d% d  dgt|%j+ }*xt|%j+D ]\} d|*| < g }+j.d krd&t&j/d  _.xbt|D ]T\},}d'}-x:|| D ],}.|.j0j0krJq2j1|.j2ks2|.j1j2krlq2n|.j2j2kr
j1|.j1kr
|*|  d(kr|'d8 }'d|*| < |&d7 }&|.j3}/|)j$d)j.j2j1|/f  |%j,d*kr|,  d7  < n |,  d7  < d}-P nLt4j1|.j1t5j2|.j2 tj6krV|*|  dkrLd(|*| < |'d7 }'|+j|. |-r2P q2W |-rP qW |*|  d(krd+jfd,d-t|+d.d
 d/D }/|)j$d0j.j2j1|/f  qW |%j,d*kr|&|	t'j(j*d < |'|	t'j(j*d < nf|	t'j(j)d  d kr*d|	t'j(j)d < d|	t'j(j)d < |	t'j(j)d   |&7  < |	t'j(j)d   |'7  < |)j7  qTW tjdt j| d1   fd2d-|D }0fd3d-|D }1|
|	|0 |1ffS )6Nz  z.coordsz	.filteredzFile with alignment coords (z") not found! Try to restart QUAST.)r   r   r   c             S   s   t | d d S )Nr   )r    )xr   r   r   <lambda>`   s    z%process_single_file.<locals>.<lambda>T)keyreversezAnalysis of genes and/or operons files (provided with -g and -O) requires extensive RAM usage, consider running QUAST without them if memory consumption is critical.|      z?Something went wrong and chromosome names in your coords file (zS) differ from the names in the reference. Try to remove the file and restart QUAST.)r   startendcontigstart_in_contigend_in_contigz	_gaps.txtz	/dev/nullw
 
gaps_count_full_partialZ_genomic_features_z.txtz%s		%s	%s	%s	%s
ID or #StartEndTypeContig=2   z# F   z%s		%d	%d	complete	%s
operon,c                s   g | ]}|j  qS r   )format_gene_info)r   block)r*   r   r   
<listcomp>   s    z'process_single_file.<locals>.<listcomp>c             S   s   | j S )N)r3   )rI   r   r   r   r-      s    )r.   z%s		%d	%d	partial	%s
zAnalysis is finished.c                s   g | ]} | qS r   r   )r   idx)features_in_contigsr   r   rJ      s    c                s   g | ]} | qS r   r   )r   rK   )operons_in_contigsr   r   rJ      s    )NN)r>   r?   r@   rA   rB   )8r   label_from_fpathlabel_from_fpath_for_fnamedictr   intr"   infoindex_to_strospathjoinr   use_all_alignmentsisfileerroritemsr   
read_fastasorted	enumerateappendr    memory_efficientr&   opensplitstripAlignedBlockrangekeyssumspace_efficientendswithremoveanalyze_gapswritemin_gap_sizestrr   FieldsGENESOPERONSr   r   loweridnumberr   r4   r3   rH   minmaxmin_gene_overlapclose)2contigs_fpathindexcoords_dirpathgenome_stats_dirpathreference_chromosomesns_by_chromosomes
containersassembly_labelZcorr_assembly_labelresultsref_lengthsZcoords_base_fpathcoords_fpathZgenome_mappingr   chr_lenZcontig_tuplesZsorted_contig_tuplesZsorted_contigs_namesZcontigs_orderrK   name_Zaligned_blocks_by_contig_nameZgene_searching_enabledZ	coordfilelines1e1s2e2Zcontig_nameir;   Z
gaps_fpathZ	gaps_fileZcur_gap_size	container
total_fullZtotal_partialZfound_fpathZ
found_fileZ
found_listZgene_blocks	contig_idZcur_feature_is_foundZ	cur_blockZcontig_infounsorted_features_in_contigsunsorted_operons_in_contigsr   )rL   rM   r*   r   process_single_fileB   s   






"
 &6 

$
$"r   c       3   
      s  t jj|tjddlm} |jr0t jjdtj	  tj
d t jjsXt j tj| \}t jjd}	t|	d}
g  x&|j D ]\}} jt|g| qW |stjddd	 |rԈ jt|d
 ntjddd	 x D ]}|jsqx(|jD ]}| jtj||j7  _ qW t|jdkrdtjd|j d dd	 |
jd|j d d d  qtjdtt|j d |j d  |
jd|j d tt|j d  t|j|jtj  |_!qW dO\}}x|D ]z}t"j#|}d}xJ D ]B}|jd
kr&t|j}|j$t"j%j&t|j n|t|j7 }qW |r|}|j$t"j%j'| qW i }i }i }i }g }g }g }tj(}t)t|tj*} fddt+|D }t,t-||dd\}|t|t 7 }|t_(stj
d |
j.  d S x.D ]&fddt/tD t0< qW |
jd xhj D ]\\}} t1t0| }!|
jd| d t|  d d t| t|   d  t|! d!  q8W |
jd |
jd"t| d#  |
jd$ttj2 d  |
jd%ttj3 d#  |
jd# |
jd&dP  |
jd&dQ  |
jd2d3 d  xt4||D ]x\}\}"}#}$}%}&t5j6|}'|$||< |#||< |&||< |%||< |jt7|$ |jt7|& |"d4 }(|"t"j%j8d5  })|"t"j%j8d6  }*|"t"j%j9d5  }+|"t"j%j9d6  },t"j#|}|
jd7|'d d8 |j:t"j%j;|j:t"j%j<|(f  |jt=|j:t"j%j; xvt"j%j8|)|*ft"j%j9|+|,fgD ]V\}-}.}/|.d krp|/d krp|
jd9dR  n&|
jd9|.|/f  |j$|-d;|.|/f  qBW |
jd q.W |
j.  tj>rdd<l?m@}0 |r|0jA||d=|| |r|0jA||d-|| tjBrd>d?lCmD}1 dd@lEmF}2 |rb|1jG|||dA dB |1jH|| ||2|dC dB |1jI||dD dE |r|1jG|||dF d- |1jH|| ||2|dG d- |1jI||dH dI |1jI||dJ dKdLdM tj
dN  S )SNr   )search_references_metarawzRunning Genome analyzer...zgenome_info.txtr8   zbNo file with genomic features were provided. Use the --features option if you want to specify it.
z  )r   rF   zPNo file with operons were provided. Use the -O option if you want to specify it.zNo genomic features of type "z" were loaded.zGenomic features of type "z
" loaded: Noner9   z	  Loaded z genomic features of type ""c          	      s"   g | ]\}}|| fqS r   r   )r   ry   rx   )r~   rz   r{   r}   r|   r   r   rJ   D  s   zdo.<locals>.<listcomp>T)filter_resultsz.Genome analyzer failed for all the assemblies.c                s   g | ]}|   qS r   r   )r   r   )refr   r   r   rJ   P  s    zreference chromosomes:
	z (total length: z bp, ztotal length without N's: z bp, maximal covered length: z bp)
ztotal genome size: z

zgap min size: zpartial gene/operon min size: z8%-25s| %-10s| %-12s| %-10s| %-10s| %-10s| %-10s| %-10s|
assemblygenomeduplicationgapsgenespartialoperonsr   fractionratiors   rC   x   r;   r<   r=   z%-25s| %-10s| %-12s| %-10s|   z %-10s| %-10s|-z%s + %s part)
html_saverfeaturesr   )plotter)contigs_aligned_lengthsz/features_cumulative_plotzgenomic featuresz/features_frcurve_plotz/complete_features_histogramz# complete genomic featuresz/operons_cumulative_plotz/operons_frcurve_plotz/complete_operons_histogramz# complete operonsz/genome_fraction_histogramzGenome fraction, %d   )	top_valuezDone.)NN)r   r   r   r   r   r   r   r   )r   r   r   rs   r   r   r   r   )r   r   )JrT   rU   rV   r   aligner_output_dirname
quast_libsr   is_quast_first_runr"   print_timestamp	main_infoisdirmkdirr   get_genome_statsr`   rZ   r^   r   r#   r   r   r   get_genes_from_filer   r    r&   rk   rR   rm   r+   listre   r   r   get	add_fieldrn   REF_OPERONS	REF_GENES_num_nf_errorsrt   max_threadsr]   r
   r   rw   rd   r   ru   rl   rv   zipr   name_from_fpathrf   ro   rp   	get_fieldMAPPEDGENOMEDUPLICATION_RATIOfloathtml_reportquast_libs.html_saverr   save_features_in_contigs
draw_plotsr   r   quast_libs.ca_utils.miscr   genes_operons_plotfrc_plot	histogram)3	ref_fpathaligned_contigs_fpathsoutput_dirpathZfeatures_dictZoperons_fpaths detailed_contigs_reports_dirpathr{   r   genome_sizeZresult_fpathZres_filer'   feature_fpathr   fpathZref_genes_numZref_operons_numrx   reportZgenomic_featuresZfiles_features_in_contigsZ"files_unsorted_features_in_contigsZfiles_operons_in_contigsZ!files_unsorted_operons_in_contigsZgenome_mappedZfull_found_genesZfull_found_operonsZnum_nf_errorsn_jobsparallel_run_argsZresults_genes_operons_tuplesr   r   aligned_lenr   r   rL   r   rM   assembly_namer;   Z
genes_fullZ
genes_partZoperons_fullZoperons_partfieldfullpartr   r   r   r   )r~   rz   r{   r}   r   r   r|   r   do   s   



&&





&
H



,



r   c               @   s   e Zd ZdddZdd ZdS )rc   Nc             C   s(   || _ || _|| _|| _|| _|| _d S )N)r   r3   r4   r5   r6   r7   )r   r   r3   r4   r5   r6   r7   r   r   r   r     s    zAlignedBlock.__init__c             C   s   | j | j }}| j|jk r@|j| j }||k r8||7 }n||8 }|j| jk rz|jt|j| j }||k rr|| }n|| }| jd t| d t| S )N:r   )r6   r7   r3   r4   ru   r5   rm   )r   r*   r3   r4   Zregion_shiftZregion_sizer   r   r   rH     s    

zAlignedBlock.format_gene_info)NNNNNN)r   r   r   r   rH   r   r   r   r   rc     s   
rc   )
__future__r   loggingrT   collectionsr   r   r   r   r   r   r   quast_libs.logr	   quast_libs.qutilsr
   LOGGER_DEFAULT_NAMEr"   r   r   r   r+   r   r   rc   r   r   r   r   <module>   s    
  0 6