ó
£žbc           @   s%  d  d l  m Z d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z e j	 ƒ  d  d l m
 Z
 m Z m Z m Z d  d l m Z d  d l m Z m Z m Z m Z d  d	 l m Z e e j ƒ Z d
 d d „  ƒ  YZ d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   with_statement(   t   divisionN(   t   defaultdict(   t   qconfig(   t   contigs_analyzert   fastaparsert	   reportingt   plotter_data(   t   qutils(   t   correct_seqt   correct_namet   get_uniq_namet   run_parallel(   t
   get_loggert   Assemblyc           B   s   e  Z d  „  Z RS(   c         C   s>   | |  _  | |  _ t j j t j j |  j  ƒ ƒ d |  _ d  S(   Ni    (   t   fpatht   labelt   ost   patht   splitextt   basenamet   name(   t   selfR   R   (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   __init__   s    		(   t   __name__t
   __module__R   (    (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyR      s   c         C   s›  t  j |  j ƒ } t  j |  j ƒ } t j d	 | ƒ g  } | d } t j j | | ƒ } i  }	 t	 ƒ  }
 i  } t
 j |  j ƒ } | | } t j j | ƒ r6t | ƒ o} xe| D]]} | j ƒ  } | d t j j ƒ  k r³ t j | d } | d } t j j | | d | d ƒ } | | k r3g  | | <n  x† | D]~ \ } } | |	 k r_| |	 | <n  | | k r:| | | k r:|
 j | ƒ | | j | ƒ t
 j | | | f g d ƒ q:q:Wt | | ƒ } | j | k r| | k r| | j | ƒ | j | j ƒ qqq³ q³ WWd  QXt j r6t j | ƒ q6n  t	 |	 j ƒ  ƒ } | |
 } t
 j | g  | D] } | |	 | f ^ qbƒ t | |  j ƒ } | | f S(
   Ns     s   processing s   _not_aligned_anywhere.fastai    i   t   _to_s   .fastat   as     processing (   R   t   label_from_fpathR   t   label_from_fpath_for_fnamet   loggert   infoR   R   t   joint   setR   t   read_fasta_one_timet   existst   opent   splitR   t   ref_labels_by_chromosomest   keyst   addt   appendt   write_fastaR   R   R   t   space_efficientt   removeR   (   t   asmt   assemblies_by_reft   corrected_dirpatht   alignments_fpath_templatet   assembly_labelt   corr_assembly_labelt   added_ref_asmt   not_aligned_fnamet   not_aligned_fpatht   contigst   aligned_contig_namest   aligned_contigs_for_each_reft   contigs_seqt   alignments_fpatht   ft   linet   valuest   ref_namet   ref_contigs_namest   ref_contigs_fpatht	   cont_namet   seqt   ref_asmt   all_contigs_namest   not_aligned_contigs_namesR   t   not_aligned_asm(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   parallel_partition_contigs   sR    
	

	##	
-c            sY  t  g  | D] } t j | ƒ g  f ^ q
 ƒ } t t j t |  ƒ ƒ } g  |  D] } | | | | f ^ qM }	 t t |	 | ƒ \ ‰  }
 g  } xÃ | D]» } t j | ƒ ‰ t	 g  ‡  ‡ f d †  t
 t ˆ  ƒ ƒ Dƒ D] } | D] } | ^ qØ qÎ ƒ } g  } x? | D]7 } x. | D]& } | j | k r
| j | ƒ Pq
q
Wqý W| j | | f ƒ q W| |
 f S(   Nc         3   s   |  ] } ˆ  | ˆ Vq d  S(   N(    (   t   .0t   i(   t   assemblies_dictsR>   (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pys	   <genexpr>\   s    (   t   dictR   t   name_from_fpatht   minR   t   max_threadst   lenR   RG   R!   t   rangeR   R)   (   t
   assembliest
   ref_fpathsR/   R0   t   labelst	   ref_fpathR.   t   n_jobsR-   t   parallel_run_argst   not_aligned_assembliest   sublistt   valt   not_sorted_assembliest   sorted_assembliesR   t   assembly(    (   RJ   R>   sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   partition_contigsR   s"    ."Kc         C   s  t  j j | t j ƒ } t j } d t _ t j |  | | d d  ƒ\ } } | t _ g  | D] } t	 | t j
 | ƒ ƒ ^ q[ } g  | D] }	 |	 j ^ q† }
 t j s­ t j rû g  | D] }	 |	 j ^ q´ } g  | D] }	 |	 j ^ qÐ } t j | d | ƒn  | |
 f S(   Ni    R   RS   (   R   R   R    R   t   corrected_dirnamet
   min_contigR   t   correct_contigst   NoneR   R   R   t
   draw_plotst   html_reportR   R   t   save_colors_and_ls(   t   contigs_fpathst   output_dirpathRS   R/   R_   t   corrected_contigs_fpathst   old_contigs_fpathsR   RQ   R-   t   corrected_labelst   corr_fpathst   corr_labels(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   correct_assembliesg   s    		!	+c      	      sà  g  ‰ t  j j ˆ t j ƒ } i  ‰  ‡  ‡ ‡ f d †  } g  |  D] } t  j j | ƒ ^ q@ } g  } x0 | D]( } t j | ƒ \ }	 }
 | j |	 ƒ qk Wg  } t j	 |  ƒ } xÍt
 |  | ƒ D]¼\ } }	 d } t  j j | ƒ } t j | ƒ \ } }
 g  ˆ  |	 <t t ƒ } d  } x t t j | ƒ ƒ D]† \ } \ } } | d 7} t | t j t |	 ƒ d ƒ } t | | ƒ } | | c d 7<| | | |	 |
 | | ƒ \ } } | s'Pq'q'W| rüt j d | d t j | ƒ d ƒ t j | t j | ƒ d ƒ q¼ | r]t j d | d	 ƒ x" ˆ  |	 D] \ } } t j | =q"Wˆ  |	 =ˆ j ƒ  | j | ƒ q¼ t j d
 | d d d ƒq¼ Wx | D] } |  j | ƒ qƒWt ˆ  ƒ d k rÃt j d t j ƒ n t j d ƒ ˆ | ˆ  |  f S(   Nc   
         sé   | } | | 7} | d k r) ˆ d } n+ t  j t j j ˆ | ƒ ƒ } ˆ j | ƒ t  j | ƒ d |  } t j s t	 | | ƒ }	 |	 s d Sn  t j | | | f g d ƒ t  j | ƒ t j | <ˆ  | j | t | ƒ f ƒ | | f S(   Ni   iÿÿÿÿt   _R   (   NN(   R   t   unique_corrected_fpathR   R   R    R)   RL   R   t   no_checkR	   Ra   R   R*   R   R&   RO   (
   t   seq_nameRB   R>   t   ref_fasta_extt   total_referencesRT   t	   seq_fnamet   corr_seq_fpatht   corr_seq_namet   corr_seq(   t   chromosomes_by_refsR/   t   corrected_ref_fpaths(    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   _proceed_seq   s    
	i    i   s     s    ==> t    R   s	   Skipping s`    because it is empty or contains incorrect sequences (header-only or with non-ACGTN characters)!s   Reference file sU    is empty or contains incorrect sequences (header-only or with non-ACGTN characters)!t   exit_with_codes"     All references were combined in s   All references were skipped!(   R   R   R    R   t   combined_ref_nameR   R   t   splitext_for_fasta_fileR)   t   process_labelst   zipR   t   intRa   t	   enumerateR   t
   read_fastaR
   t   MAX_CONTIG_NAMERO   R   R   t	   main_infoRL   R*   t   warningR   R&   t   popt   errorR,   (   RR   R/   t   downloaded_refst   combined_ref_fpathRy   RT   t
   ref_fnamest	   ref_namest	   ref_fnameR>   Rq   t   excluded_ref_fpathsRr   Rm   t   used_seq_namesRt   RI   Rp   RB   t   uniq_seq_nameRu   t   excluded(    (   Rw   R/   Rx   sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   correct_meta_referencesx   sZ    %
(
 !&
c         C   s‚  i  } t  |  d ƒ Ž } | j ƒ  xz | D]r } | d k sB | rF Pn  t j d | ƒ } | r) t | ƒ d k r) | j ƒ  } | d | d f | | d <q) q) WWd  QXg  } xÐ | D]È } t j j | ƒ }	 t	 j
 |	 ƒ \ }
 } d } d } x] | |
 D]Q } | d | k rö | t | | d d ƒ 7} | t | | d d ƒ 7} qö qö W| sWq² n  | | t j k r² | j | ƒ q² q² W| S(   Nt   rs   
s   length: (\d+)i   i    i   (   R$   t   readlinet   ret   findallRO   R%   R   R   R   R   R}   R€   R   t   downloaded_ref_min_aligned_rateR)   (   t   genome_info_fpathRR   Rw   t   refs_lent   report_fileR<   t   lengthst	   corr_refsRT   RŒ   t   refRq   t   aligned_lent   all_lent
   chromosome(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt#   get_downloaded_refs_with_alignmentsÉ   s2    
)#c      
   C   s˜  t  j j |  t j t j ƒ } xs| D]k} t ƒ  } t j | j	 ƒ } t j
 | j	 ƒ } t | | ƒ a } xW | D]O } | j ƒ  j d ƒ \ }	 }
 } | j |	 g  ƒ j t |
 ƒ t | ƒ f ƒ qr WWd  QXxÂ | j ƒ  D]´ \ }	 } t d „  | Dƒ ƒ t d „  | Dƒ ƒ } t j
 | j	 ƒ } t  j j t  j j | j	 ƒ | d |	 d ƒ } | t j | <t j | d |	 ƒ} | j t j j d | ƒ qØ Wq% Wd  S(   Ns   	c         s   s   |  ] \ } } | | Vq d  S(   N(    (   RH   R   t
   contig_cov(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pys	   <genexpr>ò   s    c         s   s   |  ] \ } } | Vq d  S(   N(    (   RH   R   R¡   (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pys	   <genexpr>ó   s    R   s   .fastaR>   s   %.2f(   R   R   R    R   t    detailed_contigs_reports_dirnamet   unique_contigs_fname_patternRK   R   R   R   R   R$   t   stripR%   t
   setdefaultR)   t   floatt   itemst   sumt   dirnamet   assembly_labels_by_fpathR   t   gett	   add_fieldt   Fieldst   AVE_READ_SUPPORT(   t   combined_output_dirpathRQ   t   unique_contigs_fpathR\   t   aligned_contigs_by_refR1   R2   t   in_fR<   R>   t
   contig_lenR¡   R6   t   ref_covR@   t   report(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   calculate_ave_read_supportæ   s&    	5	'(    (    t
   __future__R    R   R   R”   t   collectionsR   t
   quast_libsR   t   check_python_versionR   R   R   R   R   t   quast_libs.qutilsR	   R
   R   R   t   quast_libs.logR   t   LOGGER_META_NAMER   R   RG   R]   Rl   t   FalseR‘   R    R¶   (    (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/metautils.pyt   <module>   s$   
""	3		Q	