
ubc           @  s  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
 Z
 d  d l m Z d  d l m Z m Z m Z m Z m Z y  d  d l m Z d  d l Z Wn& d  d l m Z d  d l j Z n Xd  d l m Z m Z m Z d  d l m Z e e j  Z d	 Z  d
 Z! e j" j e j# d  Z$ d a& d   Z' d   Z( d e) d  Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 d   Z1 d d d d  Z2 d   Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 d e) d   Z: d!   Z; d" e d#  Z< e) d$  Z= d%   Z> e  d&  Z? e  d'  Z@ d(   ZA d)   ZB d*   ZC d+   ZD e d,  ZE d-   ZF d.   ZG d/   ZH d d d d" eI d e d0  ZJ d1   ZK d d2  ZL d  d3 lM mN ZN mO ZO mP ZP m Z mQ ZQ mR ZR eN d4  ZS d d d d d5  ZT d6   ZU d d7  ZV d8   ZW d9   ZX d:   ZY d;   ZZ d<   Z[ d=   Z\ e) e d>  Z] d?   Z^ e) d@  Z_ dA   Z` dB   Za e) e e) d d d dC  Zb d" dD dE  Zc dF   Zd e) e dG  Ze dH   Zf e) dI  Zg dJ   Zh dK   Zi dL   Zj e d e) dM  Zk eI dN  Zl e dO  Zm e) e) dP  Zn d e) dQ  Zo dR   Zp dS   Zq d dT  Zr dU   Zs dV   Zt d S(W   i(   t   with_statement(   t   divisionN(   t   defaultdict(   t   basenamet   isfilet   isdirt   existst   join(   t   urlopen(   t   fastaparsert   qconfigt   plotter_data(   t
   get_loggeri  i*  t   external_toolsc   	      C  s  t  } |  r\ t |   r t t |  t j d   sP t t |  t j d   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} q Wq n  t |   s t j |   n  | rjt j   } t j t j  d } t j	 j |  rD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(   Ns   .logi   t   __i   t   latest(   t   FalseR   R   R   R
   t   LOGGER_DEFAULT_NAMEt   LOGGER_META_NAMEt   Truet   ost   patht   abspatht   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_quast_dirt   it   base_dirpatht   prev_dirpatht   latest_symlink(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   <lambda>_   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   errort   None(   t   seqt   original_fpatht   corr_seqt   pat(    (   R9   s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   correct_seqX   s    L%c         C  sb  g  } t  t  } xt j |   D] \ } } | s[ t j d |  d | d  d d t St |  | k ss | r" t |  } t	 | |  }	 | | c d 7<t
 j s t | |   }
 |
 st SnC t j d  j |  r t j d |  d	 d d d
 d t S| }
 | j |	 |
 f  q" q" W| sEt j d |  d d d t S| r^t j | |  n  t S(   Ns	   Skipping sH    because >sequence_name field is empty for the entry starting with "%s".i   R;   s       i   s   [^ACGTN]s   File sG    contains non-ACGTN characters. Please re-run QUAST without --no-check.t   exit_with_codes    because file is empty.(   R   t   intR	   t
   read_fastaRD   RE   R   t   lent   correct_namet   get_uniq_nameR
   t   no_checkRK   R>   RB   RC   t   appendt   warningt   write_fastaR   (   RH   t
   min_contigt   corrected_fpatht   is_referencet   modified_fasta_entriest   used_seq_namest
   first_lineRG   t	   corr_namet	   uniq_nameRI   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   correct_fastah   s6    	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
   RV   (   t   .0t   l(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>   s    s8   Skipping %s because it doesn't contain contigs >= %d bp.(
   R	   t   get_chr_lengths_from_fastafilet   valuest   sumRD   RT   R
   RV   RF   t   list(   t   contigs_fpatht   labelt   lengths(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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(    (   R_   R`   (   t	   threshold(    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>   s    c         3  s!   |  ] } |   k r | Vq d  S(   N(    (   R_   R`   (   Ri   (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>   s    (   t   getR   R
   t   split_scaffoldst   dict_of_broken_scaffoldsR   t   get_color_and_lst   secondary_line_styleR   RV   t	   add_fieldt   Fieldst   CONTIGS__FOR_THRESHOLDSt   contig_thresholdsRc   RF   t   TOTALLENS__FOR_THRESHOLDS(   Rg   t	   reportingRe   t   reportt	   is_brokent   min_threshold(    (   Ri   s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   add_lengths_to_report   s    	Bc         C  s  t  j d  k r d t  _ n  t t |   t  j  } t j d  g  t |   D] \ } } | | | | f ^ qM } t t	 | |  \ } }	 }
 } } g  } g  } t
 |  r t d  n  xt t |   D] } | | } t j d j | |   x, | | D]  } | j |  | t  j | <q WxB |	 | D]6 \ } } | j |  | t  j | <t | | |  q.WxS |
 | D]G \ } } | j |  | j |  | d t  j | <t | | |  qsWq Wt  j st  j rt j st j |  qn  | | f S(   Ni   s     Pre-processing...i   s   
t   _broken(   R
   t   max_threadsRF   t   minRO   RD   t	   main_infot	   enumeratet   run_parallelt   parallel_correct_contigst   anyt   exitt   rangeR   RS   t   assembly_labels_by_fpathRx   t
   draw_plotst   html_reportR   t   dict_color_and_lst   save_colors_and_ls(   t   contigs_fpathst   corrected_dirpatht   labelsRt   t   n_jobsR&   Re   t   parallel_run_argst
   old_fpathst   corr_fpathst   broken_scaffold_fpathst   logst   is_fatal_errort   corrected_contigs_fpathst   old_contigs_fpathst
   contig_idxRf   t	   old_fpatht
   corr_fpathRg   t   broken_fpath(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   correct_contigs   s:    1!
	c         C  s!   t    r t |   St |   Sd  S(   N(   t
   is_python2t   unicodeR   (   t   value(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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>   RA   t   stripR   (   R   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   slugify   s
    -!c      	   C  s  t  j j |  } t |  \ } } | |  } g  } g  }	 g  }
 g  } t } t t  j j | t |  |   } t | |  } | s d  } nW t
 j r t | t
 j  s d  } t } q | } n$ t | t
 j |  s d  } t } n  | rF|	 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 ==> %sRy   (   R   R   R   t   splitext_for_fasta_fileR   t   unique_corrected_fpathR   R   Rh   RF   R
   t   no_check_metaR^   RV   R   RS   t   index_to_strRO   Rk   t   is_combined_reft   broke_scaffoldsRl   (   t   file_counterRe   R   R   t   contigs_fnamet   fnamet	   fasta_extRf   R   R   R   R   R   R   Rg   t   broken_scaffold_fpath(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR      s@    
%					:$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 } xs t t j |   D]\ \ } \ } } | d d k r n  | d k rn  t | | | t j t j  } | | 7} q W| | d k 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:Ry   i    id   i  s6       %d scaffolds (%s) were broken into %d contigs (%s)sK       WARNING: nothing was broken, skipping '%s broken' from further analysis(   RS   R   RO   R   R   R   R   R   R   R   t   name_from_fpathR}   R	   RN   t   split_by_nsR
   t   Ns_break_thresholdRV   RU   RF   (   R   R   Re   R   R   R   R   R   Rf   R   t   corr_fpath_wo_extt   broken_scaffolds_fpatht   broken_scaffolds_fastat   contigs_countert   scaffold_countert   nameRG   t   total_contigs_for_the_scaf(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR     s:    -
%(%
%i   i    c   
      C  sI  d } d } x | t  |   k  r |  j d |  d k r |  j d |  } | d }	 x0 |	 t  |   k r |  |	 d k r |	 d 7}	 qX W|	 d } |	 | | k r | j | j   d d t | d  |  | | !f  | d 7} |	 } q q Wt  |   | | k rE| j | j   d d t | d  |  | f  | d 7} n  | S(   Ni    R+   ii   t   _(   RO   t   findRS   t   splitR   (
   RG   R   t   splitted_fastaR   RV   t   total_contigst   cur_contig_startt   cumul_contig_lengtht   startt   end(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   /  s,    -
%

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
   RR   R   RO   R   R   R   (   RG   R   t   seq_lenR   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   is_scaffoldI  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(   NRX   s   Reference file sU    is empty or contains incorrect sequences (header-only or with non-ACGTN characters)!RL   i   s     %s ==> %s(   R   R   R   R   R   R   R
   R   R   R^   RV   R   RD   RE   R|   R   (   t	   ref_fpathR   t	   ref_fnameR   R   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   correct_referenceZ  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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   remove_quotesn  s    t   ,s@   Number of labels does not match the number of files with contigsi   t	   to_stderr(   R   R   RO   RD   RE   R   R}   (   R   R   R   R   Rf   R&   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   parse_labelsm  s    		c         C  s.   t  j j t  j j t  j j |     } | S(   N(   R   R   R   t   dirnameR   (   Re   Rf   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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(   NR   (   R   R   R   t   rm_extentions_for_fasta_fileR   R   (   Re   R   R   Rf   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   Rf   t   lowercase_labelst
   dup_labels(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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(   RS   R   R   R}   t   zipR   (   R   R   t   fpatht   duplicated_labelR&   Rf   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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&   Rf   R   t   duplicated_labelsR   t   j(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   t   shutilt   rmtree(   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   RE   R   (   R   t   messageRD   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR     s    c         C  s   t  j j |  t j  } t  j j |  r: t  j |  n  t  j j |  t j d  } t  j j |  rx t  j |  n  t  j j |  t j  } t	 |  r t
 j |  n  d  S(   Ns   .html(   R   R   R   R
   t   plots_fnameR   R   t   report_prefixt   html_aux_dirR   R   R   (   R!   t   plots_fpatht
   html_fpatht   html_report_aux_dir(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   remove_reports  s    c         C  s   t  j d d |  j    |  S(   Ns
   [^\w\._\-]R   (   R>   RA   R   (   R   t   max_name_len(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyRP     s    c         C  s   |  j    |  S(   N(   R   (   R   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   correct_asm_label  s    c         C  s+   |  | k r' |  d t  | |   7}  n  |  S(   NR   (   R   (   R   t
   used_names(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyRQ     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   RP   R   R   R   (   R   t   dirpathR   t
   corr_fnameR&   t   base_corr_fnamet   str_i(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   !  s    !$c         C  s   t  t |   d  S(   Ni    (   R   R   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   2  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   t   splitext(   R   t   basename_plus_innerextt	   outer_extR   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   6  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   R   R   RT   (   R   RD   R   R   R   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   check_is_fasta_fileE  s    $c         C  s    t  j j t  j j |    d S(   Ni    (   R   R   R   R   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   [  s    c         C  s   t  j |  S(   N(   R
   R   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   label_from_fpath_  s    c         C  s   t  t j |   S(   N(   R   R
   R   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   label_from_fpath_for_fnamec  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   <t   as   >>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   t   modeR}   t
   startswithR   R   t   relpatht   print_command_linet
   subprocesst   callR   RO   (   t   argsR  R  R  R;   R  R  RD   t   printed_argsR&   t   argt   return_code(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   call_subprocessg  s     ++'(c          C  s   d \ }  } t  j d k r t d d   } x| | D]t } | j   } t | d  d k rw t | d  d d }  n  t | d  d	 k r4 t | d  d d } q4 q4 WWd  QXn  t |  d
 | d  S(   Ni   t   linux_64s   /proc/meminfot   ri    s	   MemTotal:i   i   s   MemFree:i   (   i   i   (   R
   t   platform_namet   openR   R   RM   R{   (   t	   total_memt   free_memt   memR   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   get_free_memory  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(    (   R_   RP   t   raw_name(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>  s    t   ws   	s   
(	   t   dictt   itemsRF   t   is_non_empty_fileR	   Ra   R  t   writeR   (   R   t   correct_chr_namest   chr_len_fpatht   raw_chr_namest   chr_lengthst   out_ft   chr_namet   chr_len(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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*  RO   R,  R+  R)  R   (   R   R   t
   start_listt	   path_listR&   t   rel_list(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR
    s    c           s   d  d   d         f d   } d d     f d   | r| t j j |   } | |  r| | Sn  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
    i    i   i   c           sy   t  j j |   ru t  j |  t  j  ru  |      } |   k ru |  k rn t j d   f  n  t Sn  t S(   Ns   Version of installed %s differs from its version in the QUAST package (%s). Please make sure that you use an actual version of software.(	   R   R   R   t   accesst   X_OKRD   RT   R   R   (   R   t   version_check(   t   INCOMPATIBLE_VERSIONt   NOT_RECOMMENDED_VERSIONt   check_versiont   max_allowed_versiont   min_versiont   programt   recommend_version(    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   is_exe  s    '	c           s  | s
   St  j |  d g d t  j d t  j } | j   \ } } t j d  } | j t |   } | j	 d  s | j	 d  r  St
 t | j d  d   \ }	 }
 t | j	 d   |	 k rt | j	 d   |
 k r| d  k	 rNt
 t | j d  d   \ } } t | j	 d   | k sGt | j	 d   | k rN Sn  | d  k	 rt
 t | j d  d   \ } } t | j	 d   | k st | j	 d   | k r Sn    S Sd  S(	   Ns	   --versionR  R  s.   (?P<major_version>\d+)\.(?P<minor_version>\d+)t   major_versiont   minor_versiont   .i   (   R  t   Popent   PIPEt   STDOUTt   communicateR>   RB   RC   R   R7   R=   RM   R   RF   (   R   R8  R:  R7  t   pR  R  t   version_patternt   vt   versionR=  t   max_versiont   max_minor_versiont   rec_versiont   rec_minor_version(   t   CORRECT_VERSIONR4  R5  (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR6    s(    ' "6"6"6t   PATHN(   RF   R   R   R   t   environR   t   pathsep(   R9  R   R8  R:  R7  R;  t   exe_fileR   (    (   RK  R4  R5  R6  R7  R8  R9  R:  s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   get_path_to_program  s    !
 c         C  s   d   } y t  j d d g d t  j d t  j } | j   \ } } t j d  } t | j t	 |    d k  ry t
 S| | j t	 |   d  } | |  k SWn t k
 r t
 SXd  S(	   Nc         S  s<   |  j  d  r t |  d  Sd d  l } | j t |    S(   Ns   1.i   i(   R	  t   floatt   matht   floor(   t   verRR  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   __get_java_major_version  s    t   javas   -versionR  R  s   version "(\d+\.\d+)i   i    (   R  R?  R@  RA  RB  R>   RB   RO   t   findallR   R   R-  (   R8  RU  RC  R  R  RD  RF  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   check_java_version  s    	'!c         C  s.   |  o- t  j j |   o- t  j j |   | k S(   N(   R   R   R   t   getsize(   R   t   min_size(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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
   isinstanceRd   R  R   R   R   R   (   t	   in_fnamest	   out_fnamet   outfileR   t   infileR   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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(   RQ  R   R-  R   t	   TypeError(   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   is_float  s    
c         C  s-   y t  |   SWn t k
 r( t |   SXd  S(   N(   RM   R-  RQ  (   t   s(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   parse_str_to_num  s    c         C  s   |  d  k r d St |   Sd  S(   NR   (   RF   R   (   t   val(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   .zipR>  R   (   s   .gzs   .bz2s   .zip(   R   R   R   (   R   t   suffixt   baset   extt   ext2(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   all_required_binaries_exist+  s    c         C  sQ   t  |  rM d |  d d | d } | r< | j |  n | j |  t St S(   Ns   Previous try of s    compilation was unsuccessful! s#   For forced retrying, please remove s    and restart QUAST. (   R   t   noticeRT   R   R   (   R   t   failed_compilation_flagt   just_noticeRD   t   msg(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   check_prev_compilation_failed2  s    c         C  sr   t  |   r4 y t j |   Wq4 t k
 r0 q4 Xn  t |   rn y t j |  d t Wqn t k
 rj qn Xn  d  S(   Nt   ignore_errors(   R   R   R   t   OSErrorR   R   R   R   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   IOErrorRE   Rp  (   R   RD   t   is_requiredRs  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   safe_createK  s    
c           C  s   t  j d d k  S(   Ni    i   (   t   syst   version_info(    (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR   V  s    c         C  sv   yh t  j t |  d  d   t  j t |  d  d   t  j t |  d  d   t  j t |  d  d   Wn n Xd  S(   Ns
   aclocal.m4s   Makefile.ins   config.h.int	   configure(   R   t   utimeR   RF   (   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   fix_configure_timestampsZ  s    c	      	   C  s'  t  | d  }	 |	 | r$ t |  n d d }
 | rg x! | D] } t t  | |   q< Wt |
  t St | |  s#t |  |
 | d | r t S| j d |  d |	 d  t |	 d d	  j	   t |	 d
 d	  j	   | rCt
 j   } t
 j |  t d g | d t |	 d d  d t |	 d
 d  t
 j |  n  y[ t | r[d | g n d g d | g d t |	 d d  d t |	 d
 d  d | } WnC t k
 rd | d } | r| j |  n | j |  t SX| d k s t | |  r#t |  | |
 d | d | t Sn  t S(   Nt   makeR   s   .failedRD   s
   Compiling s    (details are in s   .log and make.err)s   .logR  s   .errs   ./configureR  R  R  s   -Cs   Permission denied accessing s   . Did you forget sudo?i    Rr  (   R   R   Rw  R   Ro  Rt  R   R|   R  Rx  R   R   R   R  Ry  Rp  RT   t   write_failed_compilation_flag(   R   R   t   requirementsRr  RD   t
   only_cleant   flag_suffixt   make_cmdt   configure_argst   make_logs_basepathRq  Rn  t   prev_dirR  Rs  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   compile_toold  sD     
#( 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   RL   R   s(   QUAST does not support spaces in paths.
(   t   is_ascii_stringRD   RE   R   (   R   R   t	   exit_code(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   check_dirpath  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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   is_dir_writable  s
    &c         C  s]   d |  d | d t  j s" d n d } | r? | j |  n | j |  t | |  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
   R   Rp  RT   R{  (   t   toolt   tool_dirpathRq  Rr  RD   Rs  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR    s    c         C  s   t  j |  t  j  S(   N(   R   R1  t   W_OK(   R   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR    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   (   R_   R   (   R  (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>  s    c         3  s*   |  ]  } t  j j t   |   Vq d  S(   N(   R   R   R   R   (   R_   R   (   t   tool_home_dirpath(    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>  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   allR  Rp  RT   RF   R   R   (   R   R  t   required_filesRD   R  t   quast_home_dirpath(    (   R  R  s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   get_dir_for_download  s$    $ 	c      	   C  s  t  j t  j t  j t  j t  j t  j t  j t  j t  j	 g	 } g  | D] } | D] } | rM | ^ qM qC t  _
 t  j
 sx d  Sg  t t  j t  j  D] \ } } | | f ^ q t  _ t  j j t  j  g  t t  j t  j  D] \ } } | | f ^ q t  _ t  j j t  j  d  S(   N(   R
   t   forward_readst   reverse_readst   interlaced_readst   unpaired_readst   mp_forward_readst   mp_reverse_readst   mp_interlaced_readst   pacbio_readst   nanopore_readst   reads_fpathsRF   R   t   paired_readst   extendt
   mate_pairs(   RD   t   reads_librariest   libR   t   read1t   read2(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   check_reads_fpaths  s    /	77c         C  s   t  |  t  } | S(   N(   RP  t   blast_dirpath(   R   t
   blast_path(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   get_blast_fpath  s    c      	   C  s   |  d k r t  | | d  d k r |  t  | | d  d k r d t d t  t |  |  | d   | f Gt j j   n  d  S(   Ni    id   s   % 3.1f%% of %d bytes(   RM   R{   RQ  R|  R  t   flush(   R  t   bt   c(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   show_progress  s    D/c      	   C  sF  d d d g } | p | } g  | D] } t  |  s" | ^ q" } | rR | rR t St d d | |  d | a t sw t Sg  | D] } t  |  s~ | ^ q~ } | r t j j t  r t j	 t d t n  t Sxq t
 |  D]c \ } } t | d |  } |  j   | st St j | t j |  j t j Bt j Bt j B q Wt S(	   Nt   makeblastdbt   blastnt   tblastnt   blastt   BLASTR  Ru  RD   (   R  R   R  R  R   R   R   R   R   R   R}   t   download_blast_binaryt   infot   chmodt   statt   st_modet   S_IXUSRt   S_IXGRPt   S_IXOTH(   RD   t	   filenamesR  t   all_binariest   cmdR  R&   t   blast_fpath(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   download_blast_binaries  s(    %%
5c         C  s   t  j j |  s t j d | t  j j |  f  y! t j   j |  | d t	  WnD t
 k
 r t j   \ } } } t j d | |  | | f  d  SX| r t j | d |  t j d |  q n  | S(   Ns   Downloading %s (file: %s)...s	   .downloads   Failed downloading %s (url: %s), QUAST functionality will be limited! Exception caught: %s
You can try to download the file manually, place it in %s and restart QUASTs   %s successfully downloaded!(   R   R   R   RD   R  R   t   urllibt	   URLopenert   retrieveR  t	   ExceptionR|  t   exc_infoRE   RF   R   t   move(   t   urlR   t	   tool_namet	   move_fileR   t	   exc_value(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   download_file  s    #!c         C  s>   t  j j t  s" t  j t  n  t |  t d d t d t S(   NR  t   platform_specifict   is_executable(   R   R   R   R  R   t   download_external_toolR   (   t   blast_filenameRD   (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR    s    c   	      C  s@  t  | |   } t j j |  r% | St  t |  } | rO t  | t j  } n  t j j  | |   } t  t j t | t j	  |   } t |  s t j
 |  n  t |  r t j d |  d |  t j | |  n t | | |  } | r<| r,t j | t j |  j t j Bt j Bt j B q<t j d  n  | S(   Ns   Copying s    from se   Please try to install the tool manually, add it to your PATH environment variable, and restart QUAST.(   R   R   R   R   t   external_tools_dirpathR
   R  t   GIT_ROOT_URLR
  t
   QUAST_HOMER   R   RD   R  R   t   copyR  R  R  R  R  R  R  RE   (	   R   R   R  R  R  t   downloaded_fpatht   external_dirpatht   external_fpathR  (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR    s&    "4c           s  t  j r+ g  | D] }   |   ^ q } n | p7 t  j } i | d 6} yb d d  l } d d l m } m  y( i d d 6}	 | |	   | j |	  Wn t k
 r n XWnI t k
 r t	   r d d l
 m } m  q d d l m } m  n X| |      f d   | D  } g  }
 | rt | d t  sLt | d t  rt | d  } | rg  t |  D]- } g  | D] } | | r|| | ^ q|^ qo}
 qg  t |  D]# } g  | D] } | | ^ q^ q}
 qg  | D] } | s| r| ^ q}
 n  |
 S(   NR   i(   t   Parallelt   delayedt   multiprocessingt   backendc         3  s!   |  ] }     |   Vq d  S(   N(    (   R_   R  (   t   _fnR  (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pys	   <genexpr>B  s    i    (   R
   t   memory_efficientRz   t   joblibR  R  t   updateRa  t   ImportErrorR   t   joblib2t   joblib3R[  Rd   t   tupleRO   R   (   R  t   fn_argsR   t   filter_resultsR  t   results_tuplest   parallel_argsR  R  t   new_style_parallel_argst   resultst   results_cntR&   t   result_listt   result(    (   R  R  s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR~   +  s6    	"
	%&C9)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/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR  Q  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   readR  t	   hexdigest(   R   t   hash_md5t   ft   buf(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyR  \  s    	c         C  s   | d  k r |  d } n  t |   r t |  r t |  # } | j   j   j   d } Wd  QXt t |    } | | k r t j	 d |  | | f  t
 St St j	 d |  | f  t
 S(   Ns   .md5i    sF   Failure of md5 check for %s! Expected md5 is %s, calculated md5 is %s.sV   Failed to check md5 for %s! Either this file or its md5 file (%s) is missing or empty.(   RF   R  R  t   readlineR   R   R   R  RD   RT   R   R   (   R   t	   md5_fpathR  t   expected_md5t   calculated_md5(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt
   verify_md5g  s    "		c         C  sD   d d  l  } t | j t |   | d   d } |  t d |  S(   Niid   i   i    (   RR  RM   t   ceilRO   t   max(   Rb   t   percentRR  t   percentile_idx(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt
   percentiley  s    'c         C  sc   t  |   d d k r1 |  t  |   d d } n. |  t  |   d |  t  |   d d d } | S(   Ni   i   (   RO   (   Rb   t   median(    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   calc_median  s    .(u   t
   __future__R    R   t   globR  R   R  R   R  R|  R>   t   collectionsR   t   os.pathR   R   R   R   R   t   urllib2R   R  t   urllib.requestt   requestt
   quast_libsR	   R
   R   t   quast_libs.logR   R   RD   t   MAX_CONTIG_NAMEt   MAX_CONTIG_NAME_GLIMMERR   R  R  RF   R  R*   RK   R   R^   Rh   Rx   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   RP   R   RQ   R   R   R   R   R   R   R   R   R  R  R(  t	   posixpathR)  R*  R+  R   R,  R
  RP  RX  R  R`  Rb  Rd  Rf  Rk  Ro  Rt  Rw  R{  R   R  R  R  R  R  R  R  R  R  R  R  R  R  R  R~   R  R  R  R  R  (    (    (    s?   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/qutils.pyt   <module>   s   (	1	#			'			)	"							$	.										.5			
							
+	
				&			