ó
¦žbc           @€  s$  d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l m Z m	 Z	 d  d l
 m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d	 l m Z m Z e e j ƒ Z d
 d d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d S(   iÿÿÿÿ(   t   with_statementN(   t   isfile(   t   qconfigt   qutils(   t   Mapping(   t   minimap_fpatht   parse_cs_tag(   t
   get_logger(   t   SPLIT_ALIGN_THRESHOLD(   t   md5t   is_non_empty_filet   AlignerStatusc           B€  s    e  Z d  Z d Z d Z d Z RS(   i    i   i   i   (   t   __name__t
   __module__t   FAILEDt   OKt   NOT_ALIGNEDt   ERROR(    (    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyR      s   c         C€  sr   t  |  d ƒ } | j d t | ƒ ƒ | j d t | ƒ ƒ | j d t j j ƒ  j d ƒ d ƒ | j ƒ  d  S(   Nt   ws   Assembly md5 checksum: %s
s   Reference md5 checksum: %s
s   Successfully finished on s   %Y/%m/%d %H:%M:%Ss   
(   t   opent   writeR	   t   datetimet   nowt   strftimet   close(   t   fpatht   contigs_fpatht	   ref_fpatht   successful_check_file(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   create_successful_check!   s    !c         C€  s•   t  |  ƒ j ƒ  j d ƒ } t | ƒ d k  r1 t S| d j ƒ  j ƒ  d t t | ƒ ƒ k ra t S| d j ƒ  j ƒ  d t t | ƒ ƒ k r‘ t St S(   Ns   
i   i    iÿÿÿÿi   (	   R   t   readt   splitt   lent   Falset   stript   strR	   t   True(   R   R   R   t   successful_check_content(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   check_successful_check*   s    ,,c   	      C€  s¤   t  j d k  r d n d } t ƒ  d d d | d d d	 d
 d d d d d d t | ƒ | | g } t j | d t |  d ƒ d t | d ƒ d d t j | ƒ ƒ} | S(   Ni_   t   1s   0.9s   -cxt   asm20s   --mask-levels   -Nt   100s	   --score-Nt   0s   -Es   1,0s   -ft   200s   --css   -tt   stdoutR   t   stderrt   at   indents     (   R   t   min_IDYR   R#   R   t   call_subprocessR   t   index_to_str(	   t	   out_fpathR   R   t   log_err_fpatht   indext   max_threadst
   mask_levelt   cmdlinet   return_code(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   run_minimap_agb5   s    -*c         C€  sX  t  j r" t |  | | | | | ƒ St  j d k  r: d } n t  j d k  rR d } n d } t  j rg d n d } t  j r| d n d	 } d
 d d d t t  j ƒ d | d t t  j ƒ d d g }	 t ƒ  d d | g t  j	 sÜ |	 n g  d | d d d d d d d d t | ƒ | | g }
 t
 j |
 d t |  d ƒ d t | d ƒ d  d! t
 j | ƒ ƒ} | S("   NiZ   R(   ic   t   asm10t   asm5R'   s   0.9R)   t   50s   -B5s   -O4,16s   --no-long-joins   -rs   -Ns   -ss   -zR+   s   -cs   -xs   --mask-levels	   --min-occs   -gt   2500s	   --score-Nt   2s   --css   -tR,   R   R-   R.   R/   s     (   R   t   is_agb_modeR:   R0   t   is_combined_refR#   t   local_misassembly_min_lengtht   min_alignmentR   t   large_genomeR   R1   R   R2   (   R3   R   R   R4   R5   R6   t   presetR7   t   num_alignmentst   additional_optionsR8   R9   (    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   run_minimap>   s     			!%4*c         C€  sV   |  d } |  d } t  j s' |  d n d } t  j s@ |  d n d } | | | | f S(   Ns   .coordss   .coords.filtereds
   .unaligneds	   /dev/nulls
   .used_snps(   R   t   space_efficient(   t   fnamet   coords_fpatht   coords_filtered_fpatht   unaligned_fpatht   used_snps_fpath(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   get_aux_out_fpaths\   s
    

c         C€  s7  t  j d ƒ } t |  ƒ } t | d ƒ þ} xô| D]ì} | j d ƒ } t | ƒ d k  rd q7 n  | d | d | d | d | d	 | d
 f \ } } }	 }
 } } t t | |	 | f ƒ \ } }	 } | d 7} | d 7} | d j d ƒ r| d j ƒ  } | d } n d } | d } | j d ƒ d } d } |
 d k rV|	 | } }	 d } n  d } d } t t | d | d f ƒ \ } } | j	 | ƒ } x» | D]³ } t | d  ƒ | d } } | d k sÔ| d k rá| | 7} q›| d k s| d k s| d k r| | 7} | | 7} q›| d k r5| | 7} q›| d k r›| | 7} q›q›W| | d | }	 | | d } d | d | } | d k r7 t
 | ƒ t j k rt d | d | d | d  |	 d! | d" | d# | d$ | d% | d& | ƒ 
} | j | j ƒ  d' ƒ q#t | | | | | | | ƒ q7 q7 WWd  QXWd  QXd  S((   Ns   (\d+[M=XIDNSH])R   s   	i
   i    i   i   i   i   i   i   iÿÿÿÿt   csiþÿÿÿt    t   :t   -i	   t   St   Ht   Mt   =t   Xt   Dt   Is   %.2fg      Y@t   *t   s1t   e1t   s2t   e2t   len1t   len2t   idyt   reft   contigt   cigars   
(   t   ret   compileR   R   R    t   mapt   intt
   startswithR"   t   findallt   floatR   R0   R   R   t
   coords_strt   split_align(   t   raw_coords_fpathRK   t   cigar_patternt   ft   coords_filet   linet   fsRd   t   align_startt	   align_endt   strandt   ref_namet	   ref_startRP   Re   t   strand_directiont	   align_lent   ref_lent   matched_basest   bases_in_mappingt
   operationst   opt   n_basest	   operationt   ref_endRb   t   align(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   parse_minimap_outputd   sZ    B!


	#$
$!c         €  s©  ‡ ‡ ‡ ‡ f d †  ‰  d d ‡  ‡ ‡ f d † } d ‰ t  d | d | d | d | d d d	 d d
 | d | d d ƒ 	‰ d } xt | ƒ D]}	 |	 j d ƒ r° | |	 7} q‹ n  | rÝ | j d ƒ }
 | ˆ | |
 |
 ƒ ‰ n  d } |	 j d ƒ r<t |	 d ƒ }
 ˆ j |	 7_ ˆ j |
 7_ ˆ j |
 7_ ˆ |
 7‰ q‹ t |	 ƒ d }
 |	 j d ƒ rs| ˆ |	 d |
 ƒ‰ q‹ |	 j d ƒ r‹ | ˆ |	 d |
 ƒ‰ q‹ q‹ Wˆ  ƒ  d  S(   Nc           €  s¶   ˆ  j  t j k  s& ˆ  j s& ˆ  j r* d  Sˆ  j ˆ  j d ˆ  _ ˆ  j ˆ  j  d ˆ ˆ  _ d ˆ d t	 ˆ  j ˆ  j  ƒ ˆ  _
 t ˆ  j
 ƒ t j k r² ˆ j ˆ  j ƒ  d ƒ n  d  S(   Ni   s   %.2fg      Y@s   
(   Ra   R   RC   R`   Re   R\   R]   R^   R_   t   maxRb   Rl   R0   R   Rm   (    (   R„   Rr   R}   Rz   (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   _write_align   s    &$i    c         €  s¤   | t  k s | t  k rs ˆ  ƒ  ˆ j ˆ j | 7_ ˆ j ˆ j | ˆ 7_ d \ ˆ _ ˆ _ d ˆ _ d }  n- ˆ j | 7_ ˆ j | 7_ ˆ j | 7_ |  S(   Ni    RQ   (   i    i    (   R   R\   R`   R^   Ra   Re   (   R}   t   prev_opt
   n_refbasest   n_alignbases(   R‡   R„   Rz   (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt
   _try_split¦   s    		R\   R]   R^   R_   R`   Ra   Rc   Rd   Re   RQ   R[   RR   i   t   +RŠ   RS   R‰   (	   R   R   Rj   t   countRi   Re   R`   Ra   R    (   Rr   Ru   Rz   Ry   Rx   Rd   RP   R‹   t   cur_mismatch_stretchR€   R   (    (   R‡   R„   Rr   R}   Rz   sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyRn   œ   s4    	$
c	      	   C€  s0  t  | d ƒ }	 | d }
 |	 j d ƒ t j d  k	 rt j j t j ƒ rt t j j	 | ƒ ƒ \ } } } } t j j
 t j | ƒ } t | ƒ rt |  ƒ r® t j |  ƒ n  t j t j j | t j j |  ƒ ƒ |  ƒ |	 j d ƒ t j d t j | ƒ d ƒ t j Sn  d  t _ t |
 ƒ rvt |  ƒ rvt |
 | | ƒ rv|	 j d ƒ t j d t j | ƒ d ƒ t j Sn  |	 j d	 ƒ t j d t j | ƒ d
 ƒ |  d } t | | | | | | ƒ } | d k rÙt j St | ƒ sìt j St | ƒ sÿt j St |
 | | ƒ |	 j d ƒ t | |  ƒ t j S(   NR   s   .sfs!   Aligning contigs to reference...
s9   	Reusing alignments from the combined_reference stage...
s     s8   Reusing alignments from the combined_reference stage... s   	Using existing alignments...
s   Using existing alignments... s#   	Aligning contigs to the reference
s!   Aligning contigs to the referencet   _tmpi    s   Filtering alignments...
(   R   R   R   t   alignments_for_reuse_dirpatht   Nonet   ost   patht   isdirRO   t   basenamet   joinR   t   removet   symlinkt   relpatht   dirnamet   loggert   infoR   R2   R   R   R&   RH   R   R   R
   R   R   R…   (   t   output_fpatht   out_basenameR   R   t   old_contigs_fpathR5   t   threadst   log_out_fpathR4   t	   log_out_ft   successful_check_fpatht   _t   coords_to_reuse_fnamet   coords_to_reuse_fpatht   tmp_output_fpatht	   exit_code(    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   align_contigsÐ   sB    
$$+
	

(    (!   t
   __future__R    Rf   R’   t   os.pathR   R   t
   quast_libsR   R   t)   quast_libs.ca_utils.analyze_misassembliesR   t   quast_libs.ca_utils.miscR   R   t   quast_libs.logR   t   quast_libs.qconfigR   t   quast_libs.qutilsR	   R
   t   LOGGER_DEFAULT_NAMER›   R   R   R&   R:   RH   RO   R…   Rn   R©   (    (    (    sO   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/ca_utils/align_contigs.pyt   <module>   s(   								8	4