ó
uŸbc           @€  s‡  d  d l  m Z d  d l Z d  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 d  d l m Z d  d l m Z d  d l m Z m Z m Z m Z m Z m Z e e j ƒ Z d	 Z d
 Z d e d Z d Z d Z d Z  d d g Z! d Z" d Z# e$ e$ d „ Z% e$ d „ Z& e$ d „ Z' d „  Z( d „  Z) e$ d „ Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d S(   iÿÿÿÿ(   t   with_statementN(   t   joint   dirnamet   realpath(   t   download_unpack_compressed_tar(   t	   reportingt   qconfigt   qutils(   t   busco(   t
   get_logger(   t   download_blast_binariest   run_parallelt   compile_toolt   get_dir_for_downloadt   check_prev_compilation_failedt   get_blast_fpaths   3.2.3s   3.3s?   http://bioinf.uni-greifswald.de/augustus/binaries/old/augustus-s   .tar.gzs@   https://busco-archive.ezlab.org/v3/datasets/bacteria_odb9.tar.gzs=   https://busco-archive.ezlab.org/v3/datasets/fungi_odb9.tar.gzsA   https://busco-archive.ezlab.org/v3/datasets/eukaryota_odb9.tar.gzt   tblastnt   makeblastdbs   config.ini.defaults
   config.inic   	      C€  s)  | r t  } d } n! | r* t } d } n t } d } t d d | g |  d | ƒ} | s^ d  St | | ƒ } | rŸ t j j | ƒ r› t	 j
 | d t ƒn  t St j j | ƒ s%t | | d ƒ } |  j d	 ƒ t | d
 | | | |  ƒ t j j | ƒ s%|  j d | d | d | ƒ d  Sn  | S(   Nt   bacteriat   fungit	   eukaryotaR   s   Busco databasest
   only_cleant   ignore_errorss   .tar.gzs     Downloading BUSCO database...s	    databases   Failed to download s    database from s    and unpack it into (   t   bacteria_db_urlt   fungi_db_urlt   eukaryota_db_urlR   t   NoneR   t   ost   patht   isdirt   shutilt   rmtreet   Truet   existst	   main_infoR   t   warning(	   t   loggert   is_prokaryotet	   is_fungusR   t   urlt   cladet   dirpatht
   db_dirpatht   downloaded_fpath(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   download_db"   s0    		!c         €  s  t  |  | |  | | d | ƒ‰  ˆ  s) d  S| r[ t j j ˆ  ƒ rW t j ˆ  d t ƒn  ˆ  St ˆ  d ƒ } t	 ‡  f d †  | Dƒ ƒ rt
 |  | ƒ rt ˆ  |  d ƒ } | j d ƒ t |  | | ˆ  | ƒ t	 ‡  f d †  | Dƒ ƒ s| j d |  d	 | d
 ˆ  ƒ d  Sn  ˆ  S(   NR   R   s   make.failedc         3€  s*   |  ]  } t  j j t ˆ  | ƒ ƒ Vq d  S(   N(   R   R   R!   R   (   t   .0t   fpath(   t   tool_dirpath(    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pys	   <genexpr>L   s    s   .tar.gzs"     Downloading third-party tools...c         3€  s*   |  ]  } t  j j t ˆ  | ƒ ƒ Vq d  S(   N(   R   R   R!   R   (   R-   R.   (   R/   (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pys	   <genexpr>R   s    s   Failed to download s    from s   and unpack it into (   R   R   R   R   R   R   R   R    R   t   allR   R"   R   R#   (   t   toolt   tool_versiont   required_filesR$   R'   R   t   failed_compilation_flagR+   (    (   R/   sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   download_toolA   s"    !c         C€  s^   t  |  d t d | ƒ} t  |  d t d | ƒ} t  |  d t d t d | ƒ} | o] | o] | S(   NR%   R   R&   (   R,   R    t   False(   R$   R   t   bacteria_dbt   eukaryota_dbt   fungi_db(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   download_all_dbX   s    c         C€  sA   t  j j |  d ƒ } t  j j t  j j | d d ƒ ƒ r= | Sd  S(   Nt   configt   cgps   log_reg_parameters_default.cfg(   R   R   R   t   isfileR   (   t   core_dirpatht   expected_path(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   get_augustus_config_dir_   s    $c         C€  s[   d d g } xH | D]@ } t  j j |  | ƒ } t  j j t  j j | d ƒ ƒ r | Sq Wd  S(   Nt   scriptst   bins   augustus2browser.pl(   R   R   R   R=   R   (   R>   t   expected_optionst   optR?   (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   get_augustus_scripts_dirf   s    !c         C€  s†   d „  } t  j d d t d t d t ƒ} | d  k	 rg t j j t j j | ƒ ƒ } | | ƒ rg | Sn  t d t d g |  t	 d | ƒS(   Nc         S€  se   t  j j |  d d ƒ } t  j j | ƒ sA t  j | t  j ƒ rE t St |  ƒ ra t |  ƒ ra t	 St S(   NRB   t	   etraining(
   R   R   R   R=   t   accesst   X_OKR6   R@   RE   R    (   R)   t   etraining_path(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt*   __check_preinstalled_augustus_completenessp   s    )t   augustust   min_versiont   recommend_versiont   max_allowed_versionRB   R   (
   R   t   get_path_to_programt   augustus_versiont   augustus_max_allowed_versionR   R   R   R   R5   t   augustus_url(   R$   R   RJ   t   preinstalled_augustust   preinstalled_augustus_dirpath(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   download_augustuso   s    		c         C€  sx  t  t t t ƒ ƒ d ƒ } t j r* d n d } i |  d 6| d 6| d 6| d 6t | ƒ d 6t t d	 ƒ ƒ d
 6t t d ƒ ƒ d 6t  | d ƒ d 6t  | d ƒ d 6t | ƒ d 6| d 6} t  | t	 ƒ } t  |  t
 ƒ }	 t | ƒ  }
 t |	 d ƒ w } xm |
 D]e } | j ƒ  j ƒ  } | s#qÿ n  | d } | | k rJ| | | d <n  | j d j  | ƒ d ƒ qÿ WWd  QXWd  QX|	 S(   NR   t
   prokaryotaR   t   out_patht   lineage_patht   domaint   tmp_dirt   threadsR   t   tblastn_pathR   t   makeblastdb_pathRB   t   augustus_pathRI   t   augustus_scripts_patht   hmmsearch_patht   wiÿÿÿÿt    s   
(   R   R   R   t   __file__R   t
   prokaryotet   strR   RE   t   default_config_fnamet   config_fnamet   opent   stript   splitt   write(   t   output_dirpatht   tmp_dirpathR[   t   clade_dirpatht   augustus_dirpatht   busco_dirpathRY   t   valuest   default_config_fpatht   config_fpatht   f_int   f_outt   linet   fst   keyword(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   make_config‚   s4    


*c          G€  s*   y t  j |  Œ  SWn t k
 r% d  SXd  S(   N(   R   t   maint
   SystemExitR   (   t
   busco_args(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   busco_main_handler    s    c   
   
   C€  sÕ  t  |  ƒ } t | d ƒ } | s5 t j j | ƒ r9 d  St j j | ƒ s[ t j | ƒ n  x t j | ƒ D]p \ } } } x] | D]U } | d k r t j j t | | ƒ ƒ sÖ t j	 t | | ƒ t | | ƒ ƒ qÖ q q WPqk Wt | d ƒ } t | d ƒ }	 t j j | ƒ sd  St j j |	 ƒ s5t j |	 ƒ n  x™ t j | ƒ D]ˆ \ } } } xu | D]m } d | k s…d | k s…d | k r[t j j t |	 | ƒ ƒ sÈt j	 t | | ƒ t |	 | ƒ ƒ qÈq[q[WPqEW| S(   NR;   t   speciest   generict   templatet   fly(
   R@   R   R   R   R   R   t   makedirst   walkR   t   copytree(
   Ro   Rl   t   input_basedirt   output_basedirR)   t   dirnamest   filesR   t   species_input_dirt   species_output_dir(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   copy_augustus_configs§   s2    ,$,c         C€  s¡   d d g } xŽ t  j |  ƒ D]} \ } } } x$ | D] } t j t | | ƒ ƒ q2 WxC | D]; } t  j j | ƒ d | k rY t  j t | | ƒ ƒ qY qY WPq Wd  S(   Ns   .logs   .txti   (   R   Rƒ   R   R   R   R   t   splitextt   remove(   t   busco_output_dirt   do_not_remove_extsR)   R‡   Rˆ   R   t   filename(    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   cleanupÄ   s    c         C€  s4  | j  ƒ  | j d ƒ t } t | ƒ } | s8 t } n- t d | t d d ƒ g d | ƒse t } n  | rŠ t d | d t ƒ rŠ t } n  | s¡ | j d ƒ d  St	 j
 j | ƒ sÃ t	 j | ƒ n  t | d ƒ } t	 j
 j | ƒ sô t	 j | ƒ n  t t |  ƒ t j ƒ } t d	 t j | ƒ } t | d
 t j d t j ƒ} | sW| j d ƒ d  St | | | | | ƒ }	 | j d | ƒ | j d | d ƒ |	 t	 j d <t | | ƒ t	 j d <t	 j d sÕ| j d ƒ n  g  |  D] }
 |
 t j |
 ƒ g ^ qÜ} t t | t j ƒ } t d „  | Dƒ ƒ sA| j d | d ƒ d  St } x•t |  ƒ D]‡\ } }
 t j  |
 ƒ } | | rÆt	 j
 j! | | ƒ rÆd" \ } } } t" | | ƒ ‡ } x} | D]u } d | k rãt# | j$ ƒ  d ƒ } q¸d | k rt# | j$ ƒ  d ƒ } q¸d | k r¸t# | j$ ƒ  d ƒ } q¸q¸WWd  QX| d k r–| j% t j& j' d t( | ƒ d | ƒ | j% t j& j) d t( | ƒ d | ƒ n  | | d k r¯t } n  t* j+ | | | ƒ qT| j d |
 d ƒ qTW| r| j, d | d t- d t. d  ƒ n  t j/ s#t0 | ƒ n  | j d! ƒ d  S(#   Ns   Running BUSCO...t   AugustusRB   RK   R$   t	   filenamess"   Failed finding conservative genes.t   tmpi   R%   R&   s#     running BUSCO with augustus from s%   Logs and results will be saved under s   ...t   BUSCO_CONFIG_FILEt   AUGUSTUS_CONFIG_PATHs=   Augustus configs not found, failed to run BUSCO without them.c         s€  s   |  ] } | Vq d  S(   N(    (   R-   R.   (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pys	   <genexpr>ú   s    s>   Failed running BUSCO for all the assemblies. See log files in sE    for information (rerun with --debug to keep all intermediate files).i    s   Complete BUSCOst
   Fragmentedt   Totals   %.2fg      Y@s   Failed running BUSCO for s[   . See the log for detailed information (rerun with --debug to keep all intermediate files).së  BUSCO did not fail explicitly but found nothing for all assemblies! Possible reasons and workarounds:
  1. Provided assemblies are so small that they do not contain even a single partial BUSCO gene. Not likely but may happen -- nothing to worry then.
  2. Incorrect lineage database was used. To run with fungi DB use --fungus, to run with eukaryota DB use --eukaryote, otherwise BUSCO uses bacteria DB.
  3. Problem with BUSCO dependencies, most likely Augustus. Check that the binaries in s<  /bin/ are working properly.
     If something is wrong with Augustus, you may try to install it yourself (https://github.com/Gaius-Augustus/Augustus or `conda install -c bioconda augustus`) and make sure "augustus" binary is in PATH.
     Please install the PROPER VERSION of Augustus, we tested BUSCO with augustus-s!   , it may also work with augustus-s6  , the newer/older versions are not supported.
  4. Some other problem with BUSCO. Check the logs (you may need to rerun QUAST with --debug to see all intermediate files).
     If you cannot solve the problem yourself, post an issue at https://github.com/ablab/quast/issues or write to quast.support@cab.spbu.rus   Done.(   i    i    i    (1   t   print_timestampt   infoR    RU   R6   R   R   R
   t   blast_filenamesR   R   R   R‚   t   mint   lenR   t   max_threadst   maxR,   Rd   R&   Ry   t   environR‹   t   errorR   t   label_from_fpath_for_fnameR   R}   t   anyt	   enumerateR   t   getR=   Rh   t   intRj   t	   add_fieldt   Fieldst   BUSCO_COMPLETEt   floatt
   BUSCO_PARTR   t   copyR#   RP   RQ   t   debugR‘   (   t   contigs_fpathst
   output_dirR$   t   compilation_successRo   RZ   t   n_jobst   busco_threadsRn   Rs   t   contigs_fpathR|   t   summary_fpathst   zero_output_for_allt   it   reportt   total_buscost   part_buscost   complete_buscost   fRv   (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   doÏ   s~    
	$		( #(+	"	(0   t
   __future__R    R   t   os.pathR   R   R   R   t   quast_libs.ra_utils.miscR   t
   quast_libsR   R   R   t   quast_libs.buscoR   t   quast_libs.logR	   t   quast_libs.qutilsR
   R   R   R   R   R   t   LOGGER_DEFAULT_NAMER$   RP   RQ   RR   R   R   R   R›   Rf   Rg   R6   R,   R5   R:   R@   RE   RU   Ry   R}   R‹   R‘   R¼   (    (    (    sB   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/run_busco.pyt   <module>   s:   .							