ó
›¢yZc           @€  s  d  d l  m Z d  d l Z d  d l Z d  d l Z y d  d l m Z Wn d  d l m Z n Xd  d l m	 Z	 m
 Z
 m Z d  d l Z d  d l m Z d  d l m Z m Z m Z m Z e
 e j d ƒ Z e
 e j d ƒ Z e
 e j d	 ƒ Z e
 e j d	 d
 ƒ Z d Z d a e
 d d
 d ƒ Z e
 e j d ƒ Z e
 e d ƒ Z e
 e d ƒ Z  e j! e e e j ƒ Z" e j! e e  e j ƒ Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ e, d „ Z- e, d „ Z. d e, d „ Z/ e, d „ Z0 d „  Z1 d S(   iÿÿÿÿ(   t   with_statementN(   t   urlopen(   t   existst   joint   isfile(   t   qconfig(   t   compile_toolt   check_prev_compilation_failedt   get_dir_for_downloadt   relpatht   bwat   sambambat   bedtoolst   bins   0.29.6t   builds   configManta.pys   external_tools/mantas   manta_linux.tar.bz2s   manta_osx.tar.bz2c         C€  s   t  t |  ƒ S(   N(   R   t   bwa_dirpath(   t   fname(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt	   bwa_fpath(   s    c         C€  s,   t  j d k r d n d } t t |  | ƒ S(   Nt   macosxt   _osxt   _linux(   R   t   platform_nameR   t   sambamba_dirpath(   R   t   platform_suffix(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   sambamba_fpath,   s    c         C€  s   t  t |  ƒ S(   N(   R   t   bedtools_bin_dirpath(   R   (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   bedtools_fpath1   s    c          C€  s   t  s
 d  St t  t ƒ }  |  S(   N(   t   manta_dirpatht   NoneR   t   config_manta_relpath(   t   config_manta_fpath(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   get_manta_fpath5   s    c         C€  s   |  j  d ƒ d  S(   Ns[   Manta failed to compile, and QUAST SV module will be able to search trivial deletions only.(   t	   main_info(   t   logger(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   print_manta_warning<   s    c          C€  s&   t  t d ƒ }  t d |  ƒ r" t St S(   Ns   make.failedt   Manta(   R   R   R   t   Truet   False(   t   failed_compilation_flag(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   manta_compilation_failed@   s    c         C€  så   d  } y t | ƒ } | j ƒ  } Wn | j d ƒ n X| rá | j d |  d ƒ t | d d ƒ  } | j | ƒ Wd  QXt | d ƒ rÃ | j d |  d ƒ t | d | ƒ | j d ƒ qá | j d	 |  | f ƒ t	 Sn  d  S(
   Ns!     Failed to establish connection!s     s    successfully downloaded!s	   .downloadt   wbs     Unpacking s   ...s     Dones      Failed downloading %s from %s!(
   R   R   t   readR    t   opent   writeR   t   infot
   unpack_tarR%   (   t   namet   download_patht   downloaded_fpatht   final_dirpathR!   t   contentt   responset   f(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   download_unpack_tar_bzG   s     c         C€  s™   t  j |  |  ƒ d d  l } | j |  d ƒ } | j | ƒ | j ƒ  t | | j d j ƒ } d d l	 m
 } | | | ƒ t  j | ƒ t j |  ƒ t S(   Niÿÿÿÿs   r:bz2i    (   t	   copy_tree(   t   shutilt   movet   tarfileR*   t
   extractallt   closeR   t   membersR.   t   distutils.dir_utilR6   t   rmtreet   ost   removeR$   (   t   fpatht   dst_dirpathR9   t   tart   temp_dirpathR6   (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyR-   [   s    
c         C€  s   t  d t d g d | d |  ƒS(   Nt   BWAR
   t
   only_cleanR!   (   R   R   (   R!   RF   (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   compile_bwai   s    c         C€  s(   t  d t t d d ƒ g d | d |  ƒS(   Nt   BEDtoolsR   R   RF   R!   (   R   t   bedtools_dirpathR   (   R!   RF   (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   compile_bedtoolsm   s    c   
      C€  s+  t  d t d t g |  d | ƒa t s, t St t d ƒ } t ƒ  } | rv t j j	 | ƒ rr t
 j | d t ƒn  t St j r'| d  k r't | ƒ r't j d k r· t } t } n/ t j d k rÕ t } t } n |  j d ƒ t St | ƒ st j | ƒ n  t | d	 ƒ } t | ƒ rg|  j d
 | ƒ t
 j | | ƒ |  j d | d | ƒ t | | ƒ nO t t d ƒ } t d | ƒ r“t |  ƒ t S|  j d ƒ t d | | | |  ƒ t | d d ƒ }	 t j j	 |	 ƒ rðt
 j |	 d t ƒn  t | ƒ s'|  j d t t d ƒ ƒ t |  ƒ t Sn  t S(   Nt   mantaR#   RF   R   t   ignore_errorst   linux_64R   s)   Manta is not available for your platform.s   manta.tar.bz2s   Copying manta from s
   Unpacking s    into s   make.faileds-     Downloading binary distribution of Manta...t   sharet   demoss   Failed to download binary distribution from https://github.com/ablab/quast/external_tools/manta and unpack it into s   build/(    R   t   manta_versionR   R   R%   R   R   R?   t   patht   isdirR7   R>   R$   R   t   no_svR   R   R   t   manta_linux_urlt   manta_ext_linux_fpatht   manta_osx_urlt   manta_ext_osx_fpatht   warningR   t   makedirsR,   t   copyR-   R   R"   R    R5   (
   R!   t	   bed_fpathRF   t   manta_build_dirpathR   t   urlRA   t   manta_downloaded_fpathR&   t   manta_demo_dirpath(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   download_mantaq   sR    "	#		
	
c         C€  s&   t  |  | ƒ r" t |  | ƒ r" t St S(   N(   RG   RJ   R$   R%   (   R!   RF   (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   compile_reads_analyzer_tools¦   s    c         C€  s£  g  } x<t  |  ƒ D].\ } } d | d } d } | rB d } n  t j j | ƒ \ } } d  }	 y4 | d k r‡ t j | d d ƒ}	 n t | ƒ }	 Wn t k
 rµ | j d	 ƒ t	 SX|	 j
 ƒ  }
 |
 j ƒ  j ƒ  d
 } t | ƒ d k  sú | j | ƒ r#| j d | d | d | d ƒ t S| j | d d !ƒ |	 j ƒ  q Wt | ƒ d k rh| j d ƒ t S| d
 | d k rŸ| j d | d
 | d f ƒ t St	 S(   Ns   /%di   t   forwardt   reverses   .gzs   .gzipt   modet   rtsU   Cannot check equivalence of paired reads names, BWA will fail if reads are discordanti    i   s   Improper read names in s    (sT    reads)! Names should end with /1 (for forward reads) or /2 (for reverse reads) but s    was found!iþÿÿÿi   sA   Something bad happened and we failed to check paired reads names!s0   Paired read names do not match! Check %s and %s!(   s   .gzs   .gzip(   t	   enumerateR?   RQ   t   splitextR   t   gzipR*   t   IOErrort   noticeR$   t   readlinet   stript   splitt   lent   endswithRX   R%   t   appendR;   (   t   reads_fpathsR!   t   first_read_namest   idxRA   t   name_endingt
   reads_typet   _t   extt   handlert
   first_linet   full_read_name(    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   paired_reads_names_are_equal¬   s<    	"(2   t
   __future__R    R?   Rh   t   sockett   urllib2R   t   urllib.requestt   os.pathR   R   R   R7   t
   quast_libsR   t   quast_libs.qutilsR   R   R   R	   t   LIBS_LOCATIONR   R   RI   R   RP   R   R   R   t
   QUAST_HOMEt   manta_external_dirpathRU   RW   t   GIT_ROOT_URLRT   RV   R   R   R   R   R"   R'   R5   R-   R%   RG   RJ   R`   Ra   R{   (    (    (    sG   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/ra_utils/misc.pyt   <module>   sH   "								5