
yZc           @  sk  d  d l  m Z d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l 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 m Z d  d l m Z m Z m Z d  d l m Z e e j  Z d Z d	 Z d
   Z d   Z e d  Z d   Z d   Z  d   Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, d e d  Z. d   Z/ d e d  Z0 e d  Z1 d    Z2 e d!  Z3 d"   Z4 d#   Z5 d$   Z6 d%   Z7 e d&  Z8 d'   Z9 d(   Z: d)   Z; d d d d e< d e d*  Z= d d+  Z> d  d, l? m@ Z@ mA ZA mB ZB mC ZC mD ZD mE ZE e@ d-  ZF d.   ZG d/   ZH d0   ZI d1   ZJ d2   ZK d3   ZL d4   ZM d5   ZN e e d6  ZO d7   ZP e d8  ZQ d9   ZR e e e d d d:  ZS d d; d<  ZT d=   ZU e e d>  ZV d?   ZW e d@  ZX e dA  ZY dB   ZZ dC   Z[ d S(D   i(   t   with_statement(   t   divisionN(   t   defaultdict(   t   basenamet   isfilet   realpatht   isdir(   t   fastaparsert   qconfigt   plotter_data(   t
   get_loggeri  i*  c   	      C  s  t  } |  r$ t |   r t } q n t j j t j j t j  t j	  }  t |   r | r d } |  } x7 t |   r t
 |  d t
 |  }  | d 7} ql Wq n  t |   s t j |   n  | r2t j   } t j t j  d } t j j |  rt j |  n  t j t |   |  t j |  n  | r| r]t |  st j |  qqt j j |  t j  } t |  st j |  qn  |  | | f S(   Ni   t   __i   t   latest(   t   FalseR   t   Truet   ost   patht   joint   abspathR   t   default_results_root_dirnamet   output_dirnamet   strt   makedirst   getcwdt   chdirt   islinkt   removet   symlinkR   t   default_json_dirname(	   t   output_dirpatht   json_outputpatht   make_latest_symlinkt	   save_jsont   existing_alignmentst   it   base_dirpatht   prev_dirpatht   latest_symlink(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   set_up_output_dir   s>    c           s   |  j    } i
 d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d	 6d d
 6d d 6  d d j t t j   j     } t j |   f d   |  } t j d  j |  r t	 j
 d | d d d d  S| S(   Nt   Nt   Mt   Kt   Rt   Yt   Wt   St   Vt   Bt   Ht   Ds   (%s)t   |c           s     |  j    S(   N(   t   group(   t   m(   t   dic(    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   <lambda>S   s    s   [^ACGTN]s	   Skipping s*    because it contains non-ACGTN characters.t   indents       (   t   upperR   t   mapt   ret   escapet   keyst   subt   compilet   searcht   loggert   warningt   None(   t   seqt   original_fpatht   corr_seqt   pat(    (   R5   s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   correct_seqL   s    L%c         C  s"  g  } t  t  } x t j |   D] \ } } | sS t j d |  d d d t St |  | k sk | r" t |  } t	 | |  }	 | | c d 7<t
 j s t | |   }
 |
 s t Sn | }
 | j |	 |
 f  q" q" W| s t j d |  d d d t St j | |  | rt d   | D  } | t
 j k rg  t
 _ t j j |  \ } } t j j t j j |  d  } t j j |  rt j | d	 t n  t j |  t | t
 j t
 j  } d
 } d } t j j | d |  | } x | D] \ } } t |  } | t
 j k rNt j d | d t t
 j  d  qn  | | 7} | | k r| | k rt
 j j |  | } | d 7} t j j | d |  | } n  t j | | | f g d d qW| d
 k rt
 j j |  n  t t
 j  d
 k rt j d  t Sqn  t S(   Ns	   Skipping s'    because >sequence_name field is empty.R7   s       i   s    because file is empty.c         s  s!   |  ] \ } } t  |  Vq d  S(   N(   t   len(   t   .0t   chr_namet   chr_seq(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>{   s    t	   split_reft   ignore_errorsi    s   part_%ds   Skipping chromosome s$    because its length is greater than s    (Nucmer's constraint).t   modet   asO   Skipping reference because all of its chromosomes exceeded Nucmer's constraint.(    R   t   intR   t
   read_fastaR@   RA   R   RH   t   correct_namet   get_uniq_nameR   t   no_checkRG   t   appendt   write_fastat   sumt   MAX_REFERENCE_FILE_LENGTHt   splitted_refR   R   t   splitextR   t   dirnamet   existst   shutilt   rmtreeR   R   t   mint   max_threadst   MAX_REFERENCE_LENGTHR   (   RD   t   corrected_fpatht
   min_contigt   is_referencet   modified_fasta_entriest   used_seq_namest
   first_lineRC   t	   corr_namet	   uniq_nameRE   t   ref_lent   _t	   fasta_extt   split_ref_dirpatht   max_lent   cur_part_lent   cur_part_numt   cur_part_fpathRJ   RK   t   cur_chr_len(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   correct_fasta]   sj    		! 

 #
c         C  sS   t  j |   j   } t d   | D  sI t j d | t j f  d  St	 |  S(   Nc         s  s$   |  ] } | t  j k r | Vq d  S(   N(   R   Rc   (   RI   t   l(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>   s    s8   Skipping %s because it doesn't contain contigs >= %d bp.(
   R   t   get_chr_lengths_from_fastafilet   valuesRW   R@   RA   R   Rc   RB   t   list(   t   contigs_fpatht   labelt   lengths(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_lengths_from_fasta   s    	c           s  | r| j  |  } t } t j r[ | t j k sO t j |  d t j k r[ t } q[ n  | sg d n t j	 } | j
 | j j g  t j D]4     | k r t   f d   |  D  n d  ^ q  | j
 | j j g  t j D]4     | k rt   f d   |  D  n d  ^ q  n  d  S(   Ni   i    c         3  s!   |  ] } |   k r d  Vq d S(   i   N(    (   RI   Rt   (   t	   threshold(    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>   s    c         3  s!   |  ] } |   k r | Vq d  S(   N(    (   RI   Rt   (   R|   (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>   s    (   t   getR   R   t	   scaffoldst   dict_of_broken_scaffoldsR	   t   get_color_and_lst   secondary_line_styleR   Rc   t	   add_fieldt   Fieldst   CONTIGS__FOR_THRESHOLDSt   contig_thresholdsRW   RB   t   TOTALLENS__FOR_THRESHOLDS(   Rz   t	   reportingRx   t   reportt	   is_brokent   min_threshold(    (   R|   s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   add_lengths_to_report   s    	Bc           s4  t  j d  k r d t  _ n  t t |   t  j  } t   rU d d l m } m  n d d l	 m } m  t
 j d  t  j s | d |      f d   t |   D  } n4 g  t |   D]! \ } } t | |     ^ q } g  }	 g  }
 xt |  D] \ } \ } } } }  | } t
 j d j |   x( | D]  } |
 j |  | t  j | <qAWx> | D]6 \ } } |	 j |  | t  j | <t | | |  qlWxO | D]G \ } } |
 j |  |	 j |  | d t  j | <t | | |  qWqWt  j st  j r*t j s*t j |	  q*n  |	 |
 f S(	   Ni   i(   t   Parallelt   delayeds     Pre-processing...t   n_jobsc         3  s0   |  ]& \ } }  t   | |     Vq d  S(   N(   t   parallel_correct_contigs(   RI   R"   Rx   (   t   corrected_dirpathR   t   labels(    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>   s   s   
t   _broken(   R   R`   RB   R_   RH   t
   is_python2t   joblibR   R   t   joblib3R@   t	   main_infot   memory_efficientt	   enumerateR   R   RU   t   assembly_labels_by_fpathR   t
   draw_plotst   html_reportR	   t   dict_color_and_lst   save_colors_and_ls(   t   contigs_fpathsR   R   R   R   R   t   corrected_infoR"   Rx   t   corrected_contigs_fpathst   old_contigs_fpathst
   contig_idxt
   old_fpathst   corr_fpathst   broken_scaffold_fpathst   logsRy   t	   old_fpatht
   corr_fpathRz   t   broken_fpath(    (   R   R   R   s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   correct_contigs   sB    		1%
	c         C  s!   t    r t |   St |   Sd  S(   N(   R   t   unicodeR   (   t   value(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   convert_to_unicode   s    	
c         C  s   d d l  } | j d t |    j d d  j d  }  t t j d d |   j    }  t t j d	 d |    }  t |   S(
   s   
    Prepare string to use in file names: normalizes string,
    removes non-alpha characters, and converts spaces to hyphens.
    iNt   NFKDt   asciit   ignores   utf-8s   [^\w\s-]t   -s   [-\s]+(	   t   unicodedatat	   normalizeR   t   encodet   decodeR:   R=   t   stripR   (   R   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   slugify   s
    -!c         C  s  t  j j |  } t |  \ } } | |  } g  } g  }	 g  }
 g  } t t  j j | t |  |   } t | |  } | s d  } n0 t	 j
 r | } n t | | t	 j  s d  } n  | r|	 j | | f  |
 j |  | j d t |  d t |  d k d | | f  n  t	 j rt	 j r| rt |  | | | |  \ } } | rt | | d  } | rt	 j
 st | | t	 j  r| j | | f  | t	 j | <qqn  |
 |	 | | f S(   Ns     t   forcei   s	   %s ==> %sR   (   R   R   R   t   splitext_for_fasta_filet   unique_corrected_fpathR   R   R{   RB   R   t   no_check_metaRs   Rc   RU   t   index_to_strRH   R~   t   is_combined_reft   broke_scaffoldsR   (   t   file_counterRx   R   R   t   contigs_fnamet   fnameRl   Ry   R   R   R   R   R   Rz   t   broken_scaffold_fpath(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR      s6    
%				:$c         C  s&  | j  d t |  d t |  d k d  t j j |  } t |  \ } } | |  } t t j j | t	 |  |   }	 t j j | t
 |	   }
 |
 d | } g  } d } d } t } xt t j |   D]\ } \ } } | d d k r n  | d k rn  d } d } d } x | t |  k  r| j d	 |  d
 k r| j d	 |  } | d } x0 | t |  k r| | d	 k r| d 7} qiW| d } | | t j k r t } | | t j k r| j  | j   d d t | d  | | | !f  | d 7} n  | } q q Wt |  | t j k rx| j  | j   d d t | d  | | f  | d 7} n  | | 7} q W| rt j | |  | j  d t |  d t |  d k d | d | | | d f  | | f S| j  d t |  d t |  d k d |  d  | f S(   Ns     R   i   s"     breaking scaffolds into contigs:R   i    id   i  R'   iRk   s6       %d scaffolds (%s) were broken into %d contigs (%s)sK       WARNING: nothing was broken, skipping '%s broken' from further analysis(   RU   R   RH   R   R   R   R   R   R   R   t   name_from_fpathR   R   R   RQ   t   findR   t   Ns_break_thresholdR   Rc   t   splitR   RV   RB   (   R   R   Rx   R   R   R   R   Rl   Ry   R   t   corr_fpath_wo_extt   broken_scaffolds_fpatht   broken_scaffolds_fastat   contigs_countert   scaffold_counterR   t   nameRC   t   cumul_contig_lengtht   total_contigs_for_the_scaft   cur_contig_startt   startt   end(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR   "  sj    -
%(-
%
%
%c         C  s   t  j r t Sd } t |   } x | | k  r |  j d |  d k r |  j d |  } | d } x* | | k r |  | d k r | d 7} qe W| d } | | t  j k r" t Sq" Wt S(   Ni    R'   ii   (   R   RT   R   RH   R   R   R   (   RC   R   t   seq_lenR   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   is_scaffold_  s    	'

c         C  s   t  j j |   } t |  \ } } t t  j j | | |   } t j r t j r t	 |  | t j
 d t s t j d |  d d d q n |  } t j d |  t |  f  | }  |  S(   NRd   s   Reference file sU    is empty or contains incorrect sequences (header-only or with non-ACGTN characters)!t   exit_with_codei   s     %s ==> %s(   R   R   R   R   R   R   R   R   R   Rs   Rc   R   R@   t   errorR   R   (   t	   ref_fpathR   t	   ref_fnameR   Rl   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   correct_referencep  s    c         C  s   d   } | |   j  d  } g  | D] } | j   ^ q% } t |  t |  k ro t j d d d t g  Sx0 t |  D]" \ } } | | j    | | <q| W| Sd  S(   Nc         S  sH   |  rD |  d d k r# |  d }  n  |  d d k r@ |  d  }  n  |  Sd  S(   Ni    t   "i   i(    (   t   line(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   remove_quotes  s    t   ,s@   Number of labels does not match the number of files with contigsi   t	   to_stderr(   R   R   RH   R@   R   R   R   (   R   R   R   R   Ry   R"   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   parse_labels  s    		c         C  s.   t  j j t  j j t  j j |     } | S(   N(   R   R   R   R[   R   (   Rx   Ry   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_label_from_par_dir  s    *c         C  sT   t  j j |   } t t  j j |    } t  j j t  j j |   d | } | S(   NRk   (   R   R   R   t   rm_extentions_for_fasta_fileR   R[   (   Rx   R   R   Ry   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt    get_label_from_par_dir_and_fname  s    &c         C  sW   g  |  D] } | j    ^ q } g  |  D]' } | j | j     d k r& | ^ q& } | S(   Ni   (   t   lowert   count(   R   Ry   t   lowercase_labelst
   dup_labels(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_duplicated  s    4c         C  s   g  } x! |  D] } | j  t |   q Wx_ t |  D]Q } xH t t | |    D]1 \ } \ } } | | k rS t |  | | <qS qS Wq7 W| S(   N(   RU   R   R   R   t   zipR   (   R   R   t   fpatht   duplicated_labelR"   Ry   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_labels_from_par_dirs  s    (c   	      C  s{  | rC x t  |  D]) \ } } | s t |  |  | | <q q Wn | rX t |   } n g  |  D] } t t j j |   ^ q_ } t t |   } xL t  t	 | |    D]5 \ } \ } } | | k r t |  |  | | <q q Wx t |  D] } d } xs t  t	 | |    D]\ \ } \ } } | | k r| d k rJ| | | <n | d t
 |  | | <| d 7} qqWq W| S(   Ni    t    i   (   R   R   R   R   R   R   R   t   setR   R   R   (	   R   R   t   all_labels_from_dirsR"   Ry   R   t   duplicated_labelsR   t   j(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   process_labels  s(    +((c         C  s(   t  j r$ t  j r$ t j |   n  d  S(   N(   R   t   debugR   R]   R^   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   cleanup  s    t    c         C  s9   t  j j |   s5 | j d | |  f d d t n  |  S(   Ns   File not found (%s): %si   R   (   R   R   R   R   R   (   R   t   messageR@   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   assert_file_exists  s    
c         C  sD   t  j d k r | r d Sd |  d d k r3 d n d |  d Sd  S(   Ni   R   s   %d i
   R   (   R   t   assemblies_num(   R"   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR     s    c         C  s   xx d t  j g D]g } x^ t  j t  j g D]J } t j j |  | | d  } x$ t j |  D] } t j	 |  q\ Wq) Wq Wt j j |  t  j
  } t j j |  r t j	 |  n  t j j |  t  j  } t |  r t j |  n  d  S(   NR   s   .*(   R   t   gage_report_prefixt   report_prefixt   transposed_report_prefixR   R   R   t   globt   iglobR   t   plots_fnameR   t   html_aux_dirR   R]   R^   (   R   t   gage_prefixR   t   patternt   ft   plots_fpatht   html_report_aux_dir(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   remove_reports  s    c         C  s<   t  j d d |  j    |  }  t  j d d |  j    |  S(   Ns   [^\w\._\-+|]Rk   s   [\|+=/](   R:   R=   R   (   R   t   max_name_len(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyRR   *  s    c         C  s+   |  | k r' |  d t  | |   7}  n  |  S(   NRk   (   R   (   R   t
   used_names(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyRS   /  s    c         C  s   t  j j |   } t  j j |   } t |  } t  j j t  j j | |   r d } | } xR t  j j t  j j | |   r t |  } t |  d | } | d 7} q` Wn  t  j j | |  S(   Ni   R   (   R   R   R[   R   RR   R   R   R   (   R   t   dirpathR   t
   corr_fnameR"   t   base_corr_fnamet   str_i(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR   5  s    !$c         C  s   t  t |   d  S(   Ni    (   RR   R   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR   F  s    c         C  sr   t  j j |   \ } } | d k r4 |  d } } n  t  j j |  \ } } | d k rh | d } } n  | | f S(   Ns   .zips   .gzs   .gzips   .bz2s   .bzip2R   s   .fas   .fastas   .fass   .seqs   .fnas   .contig(   s   .zips   .gzs   .gzips   .bz2s   .bzip2(   s   .fas   .fastas   .fass   .seqs   .fnas   .contig(   R   R   RZ   (   R   t   basename_plus_innerextt	   outer_extR   Rl   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR   J  s    c         C  s   d |  k s$ d |  k s$ |  d k r( t  St j j |   \ } } t j j |  \ } } | d k rt | | } } n  | d k r t S| d k r | j d |   t  S| d k r | j d |   t  St S(   Ns	   blast.ress   blast.checks	   blast.errR   s   .fas   .fastas   .fass   .seqs   .fnas   .zips   .gzs   .gzips   .bz2s   .bzip2s2   Skipping %s because it is not a supported archive.s5   Skipping %s because it has not a supported extension.(   s   .fas   .fastas   .fass   .seqs   .fna(   s   .zips   .gzs   .gzips   .bz2s   .bzip2R   (   s   .fas   .fastas   .fass   .seqs   .fna(   R   R   R   RZ   R   RA   (   R   R@   R
  R  R   Rl   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   check_is_fasta_fileY  s    $c         C  s    t  j j t  j j |    d S(   Ni    (   R   R   RZ   R   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR   o  s    c         C  s   t  j |  S(   N(   R   R   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   label_from_fpaths  s    c         C  s   t  t j |   S(   N(   R   R   R   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   label_from_fpath_for_fnamew  s    c      
   C  sV  |  } | r# | d | j  g 7} n  | rT | | j d k rA d n d | j  g 7} n  | r | | j d k rr d n d | j  g 7} n  xB t |  D]4 \ }	 }
 |
 j t j    r t |
  | |	 <q q W| j | | d | t j	 |  d | d	 | d
 | d | } | d k rR| j
 d t |  d | rGd t | j   d n d  n  | S(   Nt   <RO   s   >>t   >s   2>>s   2>t   only_if_debugt   stdint   stdoutt   stderrt   envi    R   s   The tool returned non-zero.s    See s    for stderr.R   (   R   RN   R   t
   startswithR   R   t   relpatht   print_command_linet
   subprocesst   callR   RH   (   t   argsR  R  R  R7   R  R  R@   t   printed_argsR"   t   argt   return_code(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   call_subprocess{  s     ++'(c      	   C  s   |  d } | r, t  d   | j   D  n d  } t |  s t j |   } t | d  Y } xO | j   D]A \ } } | r | | n | } | j | d t |  d  ql WWd  QXn  | S(   Ns   .faic         s  s!   |  ] \ } } | | f Vq d  S(   N(    (   RI   RR   t   raw_name(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>  s    t   ws   	s   
(	   t   dictt   itemsRB   t   is_non_empty_fileR   Ru   t   opent   writeR   (   R   t   correct_chr_namest   chr_len_fpatht   raw_chr_namest   chr_lengthst   out_fRJ   t   chr_len(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_chr_len_fpath  s    
(,(   t   curdirt   sept   pardirR   R   t   commonprefixc         C  s   |  s t  d   n  t |  j t  } t |   j t  } t t | | g   } t g t |  | | | } | s t St |   S(   s#   Return a relative version of a paths   No path specified(	   t
   ValueErrorR   R   R/  RH   R1  R0  R.  R   (   R   R   t
   start_listt	   path_listR"   t   rel_list(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR    s    c         C  sV   d   } xF t  j d j t  j  D]+ } t  j j | |   } | |  r# | Sq# Wd S(   sH   
    returns the path to an executable or None if it can't be found
    c         S  s%   t  j j |   o$ t  j |  t  j  S(   N(   R   R   R   t   accesst   X_OK(   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   is_exe  s    t   PATHN(   R   t   environR   t   pathsepR   R   RB   (   t   programR8  R   t   exe_file(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_path_to_program  s    	 c         C  s.   |  o- t  j j |   o- t  j j |   d k S(   Ni
   (   R   R   R\   t   getsize(   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR$    s    c         C  s   t  |  t  s |  g }  n  t | d  ` } xV |  D]N } t j j |  r4 t |  % } x | D] } | j |  qb WWd  QXq4 q4 WWd  QXd  S(   NR!  (   t
   isinstanceRw   R%  R   R   R\   R&  (   t	   in_fnamest	   out_fnamet   outfileR   t   infileR   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt	   cat_files  s    c         C  s<   y t  |   t SWn# t k
 r& t St k
 r7 t SXd  S(   N(   t   floatR   R2  R   t	   TypeError(   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   is_float  s    
c         C  s-   y t  |   SWn t k
 r( t |   SXd  S(   N(   RP   R2  RF  (   t   s(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   parse_str_to_num  s    c         C  s   |  d  k r d St |   Sd  S(   NR   (   RB   R   (   t   val(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt
   val_to_str  s    c         C  se   t  j j |   \ } } | d k rI t  j j |  \ } } | | } n  | | r\ d | n d | S(   Ns   .gzs   .bz2s   .zipt   .R   (   s   .gzs   .bz2s   .zip(   R   R   RZ   (   R   t   suffixt   baset   extt   ext2(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt
   add_suffix  s
    c         C  s.   x' | D] } t  t |  |   s t Sq Wt S(   N(   R   R   R   R   (   t   aligner_dirpatht   required_binariest   required_binary(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   all_required_binaries_exist  s    c         C  sg   t  |  rc d |  d d | d |  d k r5 d n d } | rR | j |  n | j |  t St S(   Ns   Previous try of s    compilation was unsuccessful! s#   For forced retrying, please remove s    and restart QUAST. s   E-MEMsI   Currently, QUAST will use Nucmer which is absolutely fine, albeit slower.R   (   R   t   noticeRA   R   R   (   R   t   failed_compilation_flagt   just_noticeR@   t   msg(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   check_prev_compilation_failed  s    c         C  s8   t  |   r4 y t j |   Wq4 t k
 r0 q4 Xn  d  S(   N(   R   R   R   t   OSError(   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   safe_rm  s
    c         C  s\   y t  |  d  j   Wn> t k
 rW |  d } | rG | j |  qX | j |  n Xd  S(   NR!  s(    cannot be created. Did you forget sudo?(   R%  t   closet   IOErrorR   RW  (   R   R@   t   is_requiredRZ  (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   safe_create  s    
c           C  s   t  j d d k  S(   Ni    i   (   t   syst   version_info(    (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR     s    c         C  s  t  | d  } | t |  d }	 | r[ x! | D] }
 t t  | |
   q0 Wt |	  t St | |  st |  |	 | d | r t S| j d |  d | d  y[ t | r d | g n d g d | g d t	 | d	 d
  d t	 | d d
  d | } WnC t
 k
 rCd | d } | r2| j |  n | j |  t SX| d k s`t | |  rt |  | |	 d | d | t Sn  t S(   Nt   makes   .failedR@   s
   Compiling s    (details are in s   .log and make.err)s   -CR  s   .logR!  R  s   .errs   Permission denied accessing s   . Did you forget sudo?i    RY  (   R   R   R]  R   RV  R[  R   R   R  R%  R_  RW  RA   t   write_failed_compilation_flag(   R   R  t   requirementsRY  R@   t
   only_cleant   flag_suffixt   make_cmdt   make_logs_basepathRX  RU  R  RZ  (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   compile_tool"  s4    
( i   c         C  s\   t  |   s, t j d | d t d | n  d |  k rX t j d | d t d | n  t S(   Ns5   QUAST does not support non-ASCII characters in path.
R   R   R   s(   QUAST does not support spaces in paths.
(   t   is_ascii_stringR@   R   R   (   R   R   t	   exit_code(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   check_dirpathE  s
      c         C  sK   t  |   r t |   r t St  |   rG t t j j |    rG t St S(   N(   R   t   check_write_permissionR   R   R   R[   R   (   R  (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   is_dir_writableM  s
    &c         C  si   d |  d | d t  j s" d n d } | r? | j |  n | j |  t | | d |  d k d  S(   Ns   Failed to compile s    (s   )! Try to compile it manually. sK   You can restart Quast with the --debug flag to see the compilation command.R   R`  s   E-MEM(   R   R   RW  RA   Ra  (   t   toolt   tool_dirpathRX  RY  R@   RZ  (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyRe  U  s    c         C  s   t  j |  t  j  S(   N(   R   R6  t   W_OK(   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyRo  _  s    c           s  t  t j |     t  t j j d  d  } t  | |    t   f d   | D  r\   St  f d   | D  r|  St    s | s | j d   d | d |  n  t |  s | s | j	 d | d | d	  n  d  S   n  t    st j    n    S(
   Nt   ~s   .quastc         3  s*   |  ]  } t  j j t   |   Vq d  S(   N(   R   R   R\   R   (   RI   R   (   Rr  (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>g  s    c         3  s*   |  ]  } t  j j t   |   Vq d  S(   N(   R   R   R\   R   (   RI   R   (   t   tool_home_dirpath(    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>i  s    s   Permission denied accessing s   . s&    will be downloaded to home directory s+   Permission denied accessing home directory s    cannot be downloaded.(   R   R   t   LIBS_LOCATIONR   R   t
   expandusert   allRp  RW  RA   RB   R   R   (   R[   Rq  t   required_filesR@   Rg  t   quast_home_dirpath(    (   Rr  Ru  s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   get_dir_for_downloadc  s$    $ 	c   
        s	  t    r" d d l m } m  n d d l m } m  | d |     f d   | D  } | r | d r t | d  n d } | r g  t |  D]- } g  | D] } | | r | | ^ q ^ q }	 n6 g  t |  D]# } g  | D] } | | ^ q ^ q }	 |	 S(   Ni(   R   R   R   c         3  s!   |  ] }     |   Vq d  S(   N(    (   RI   R  (   t   _fnR   (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pys	   <genexpr>~  s    i    (   R   R   R   R   R   RH   t   range(
   R|  t   fn_argsR   t   filter_resultsR   t   results_tuplest   results_cntR"   t   result_listt   results(    (   R|  R   s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   run_parallely  s    	(&C6c         C  s?   y |  j  d  Wn# t k
 r% t St k
 r6 t SXt Sd  S(   NR   (   R   t   UnicodeDecodeErrorR   t   UnicodeEncodeErrorR   (   R   (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyRl    s    c         C  sa   t  j   } t |  d  : } x0 t rP | j d  } | s@ Pn  | j |  q! WWd  QX| j   S(   Nt   rbi    (   t   hashlibt   md5R%  R   t   readt   updatet	   hexdigest(   R   t   hash_md5R   t   buf(    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyR    s    	(\   t
   __future__R    R   R   R  R]   R  R   Rb  R:   t   collectionsR   t   os.pathR   R   R   R   t
   quast_libsR   R   R	   t   quast_libs.logR
   t   LOGGER_DEFAULT_NAMER@   t   MAX_CONTIG_NAMEt   MAX_CONTIG_NAME_GLIMMERR&   RG   R   Rs   R{   R   R   R   R   R   R   R   R   R   R   R   R   R   RB   R   R   R   R   R  RR   RS   R   R   R   R  R   R  R  R   R  R-  t	   posixpathR.  R/  R0  R   R   R1  R  R>  R$  RE  RH  RJ  RL  RR  RV  R[  R]  Ra  R   Rk  Rn  Rp  Re  Ro  R{  R  Rl  R  (    (    (    s@   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/qutils.pyt   <module>   s   "	0	A			.			#	=							$	.									.				
						#	
		