
jac           @   s  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 Z d  d l	 Z	 d  d l
 Z
 d  d l Z d  d l m Z d  d l m Z d  d l m Z m Z m Z d  d l Z d  d l m Z d Z d Z d Z d	 Z d a d a e Z e  Z! d e d  d
  Z" d d d  Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d d d  Z* d d  Z+ d d d  Z, d d d  Z- d d  Z. d   Z/ d   Z0 d   Z1 d   Z2 d   Z3 d   Z4 d   Z5 e	 j6 j7 d  d   Z8 d!   Z9 d d d"  Z: d d d d#  Z; d$   Z< e d%  Z= d&   Z> e  d'  Z? d(   Z@ d)   ZA d d d*  ZB d+   ZC d,   ZD d-   ZE d.   ZF d/   ZG d0   ZH d1   ZI d2   ZJ d3   ZK d4   ZL e jM dQ d7 d8  ZN dR d9  ZO d d:  ZP d;   ZQ d<   ZR d=   ZS d>   ZT d?   ZU d@   ZV dA   ZW dB   ZX dC   ZY dD   ZZ dE dF  Z[ e  dG  Z\ dH   Z] dI e  dJ  Z^ dK   Z_ dL   Z` dM   Za dN   Zb dO   Zc dP   Zd d S(S   iN(   t   uname(   t   LooseVersion(   t   abspatht
   expandusert   join(   t   SeqIOs   == Error == s   == Warning == s    ERROR s    WARN c         C   s	  d } | rk | j  d | |  f  t | d t | j  d |  | j  d  | j  d | j    ne t j j d | |  f  t j j d |  t j j d	  t j j d
 | j    t j j   t r t	 j
 j t  r t j t  n  t j |  d  S(   Nt   SPAdess   

%s %st
   with_errorsR   
In case you have troubles running %s, you can write to spades.support@cab.spbu.rusC   or report an issue on our GitHub repository github.com/ablab/spadessM   Please provide us with params.txt and %s.log files from the output directory.s	   

%s %s

sS   
In case you have troubles running %s, you can write to spades.support@cab.spbu.ru
sD   or report an issue on our GitHub repository github.com/ablab/spades
sN   Please provide us with params.txt and %s.log files from the output directory.
(   t   infot   log_warningst   Truet   lowert   syst   stderrt   writet   flusht   current_tmp_dirt   ost   patht   isdirt   shutilt   rmtreet   exit(   t   err_strt   logt   prefixt	   exit_codet   binary_name(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   error+   s$    		c         C   sK   | r  | j  d | |  f  n' t j j d | |  f  t j j   d  S(   Ns	   

%s %s

s
   

%s %s


(   R   R   t   stdoutR   R   (   t   warn_strR   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   warningB   s    c          C   s   d   }  |    r d Sd S(   Nc           S   s   d t    d j   k S(   Nt	   microsofti   (   R    R   (    (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   in_wslK   s    s   1. WSL is an unsupported platform
2. If SPAdes crashes, then you might want to compile it from sources
3. If nothing works, run on real Linuxt    (    (   R!   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt	   wsl_checkJ   s    		c         C   s   |  d k r t    Sd  S(   Ni(   R#   (   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_error_hintsV   s    c         C   s6   t  |  } d |  | | f } t | | d | d  S(   NsA   system call for: "%s" finished abnormally, OS return value: %d
%sR   (   R$   R   (   t   cmdR   R   t	   hints_strt   err_msg(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt	   sys_error[   s    c          C   s  d   }  t  j j   d } g  } x t j D] } | d } d | k r` | j d  \ } } n- | j d  r | d  | } } n
 | } } |  |  } | j d | | j d d  f  t |  t |  k o t |  k  n r, t	 Sq, Wt
 d | d	 j |  f  d  S(
   Nc         S   sq   |  j  d  } xR t t t |    D]8 } | | j   r( t t | |  d  | | <Pq( q( Wd j |  S(   Nt   .i   (   t   splitt   reversedt   ranget   lent   isdigitt   strt   intR   (   t   versiont
   componentst   i(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   __next_versionb   s    i    t   -t   +is   Python%s: %ss    and highers=   python version %s is not supported!
Supported versions are %ss   , (   R   R1   R*   t   options_storaget   SUPPORTED_PYTHON_VERSIONSt   endswitht   appendt   replaceR   R
   R   R   (   R4   t   current_versiont   supported_versions_msgt   supported_versionst   majort   min_inct   max_inct   max_exc(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_python_versiona   s     	

#.c           C   s   d d d S(   Ns2   You can obtain SPAdes binaries in one of two ways:s:   
1. Download them from http://cab.spbu.ru/software/spades/s5   
2. Build source code with ./spades_compile.sh script(    (    (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt    get_spades_binaries_info_message|   s    c         C   se   x^ d d d d g D]J } t  j j |  |  } t  j j |  s t d | t   f |  q q Wd  S(   Ns   spades-hammers   spades-ionhammers   spades-cores
   spades-bwas    SPAdes binaries not found: %s
%s(   R   R   R   t   isfileR   RD   (   t
   binary_dirR   t   binaryt   binary_path(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_binaries   s    R"   c         C   s_   t  t |    } t | |  t j j |  sN t d | | f d | n  | t j |  <| S(   Ns   file not found: %s (%s)R   (	   R   R   t   check_path_is_asciiR   R   RE   R   R7   t   dict_of_rel2abs(   t   input_filenamet   messageR   t   filename(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_file_existence   s    c         C   s_   |  t  j k r2 t  j |  } t j d |  } n t j |   } | s[ t d |  |  n  | S(   NRN   s%   incorrect extension of reads file: %s(   R7   t   dict_of_prefixesR   t   get_read_file_typeR   (   RL   R   t   extt	   file_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyRQ      s    c         C   s   t  t |    } t |  |  } | d k r1 d  SyT t j t j | d  |  } t | d   d  k r t d | | f d | n  WnP t	 k
 r } t | j
 d j d |  d t j   j d |  d | n Xd  S(   Nt   bamt   rs   file is empty: %s (%s)R   i    t   FILEs   

(   R   R   RQ   R   t   parset   Opent   nextt   NoneR   t	   Exceptiont   argst   formatt	   tracebackt
   format_exc(   RL   RM   R   RN   RS   t   reads_iteratort   inst(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_file_not_empty   s    !c         C   s_   t  t |    } t | |  t j j |  sN t d | | f d | n  | t j |  <| S(   Ns   directory not found: %s (%s)R   (	   R   R   RJ   R   R   R   R   R7   RK   (   t   input_dirnameRM   R   t   dirname(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_dir_existence   s    c         C   s'   t  |   s# t d |  | f  n  d  S(   Ns+   path contains non-ASCII characters: %s (%s)(   t   is_ascii_stringR   (   R   RM   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyRJ      s    c         C   sH   t  j j |   r" t  j |   n  t  j j |   sD t  j |   n  d  S(   N(   R   R   RE   t   removet   existst   makedirs(   Rd   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   ensure_dir_existence   s    c         C   s3   t  j j |   r" t j |   n  t  j |   d  S(   N(   R   R   R   R   R   Ri   (   Rd   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   recreate_dir   s    c         C   s>   x7 |  D]/ } |  j  |  d k r t d | |  q q Wd  S(   Ni   s$   file %s was specified at least twice(   t   countR   (   t	   filenamesR   RN   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_files_duplication   s    c   	      C   s<  |  t  j k r t  j |  } n t j j |   d } | j   d k r t j j |  t |    d } | | j   t  j k r | | } q t j j |  t | |    d } | | } n  | j   t  j k r t d d j	 t  j  |  | f |  n  | rD| j   t  j
 k rDt d j	 t  j
  d |  | f |  n  | r| j   t  j k r| t  j k rt d d j	 t  j  |  | f |  n  | j d  r| j   t  j k rt d | d j	 t  j  |  | f |  n  | j d	  r8| j   t  j k r8t d
 | d j	 t  j  |  | f |  n  d  S(   Ni   s   .gzsG   file with reads has unsupported format (only %s are supported): %s (%s)s   , s4    formats supported only for iontorrent mode: %s (%s)sY   to run read error correction, reads should be in FASTQ format (%s are supported): %s (%s)t   contigssC   file with %s should be in FASTA format  (%s are supported): %s (%s)t   graphsA   file with %s should be in GFA format  (%s are supported): %s (%s)(   R7   RP   R   R   t   splitextR   R-   t   ALLOWED_READS_EXTENSIONSR   R   t(   IONTORRENT_ONLY_ALLOWED_READS_EXTENSIONSt   BH_ALLOWED_READS_EXTENSIONSt   LONG_READS_TYPESR9   t    CONTIGS_ALLOWED_READS_EXTENSIONSt   GRAPH_ALLOWED_READS_EXTENSIONS(	   RN   RM   t   only_assemblert
   iontorrentt   library_typeR   RR   t   pre_extt   pre_pre_ext(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_reads_file_format   s2    !%##$&$c         C   s   d   } t  j j |   \ } } | r: | |   r |  Snj d t  j k r xX t  j d j t  j  D]: } | j d  } t  j j | |   } | |  rc | Sqc Wn  d  S(   Nc         S   s%   t  j j |   o$ t  j |  t  j  S(   N(   R   R   RE   t   accesst   X_OK(   t   fpath(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   is_exe   s    t   PATHt   "(   R   R   R*   t   environt   pathsept   stripR   RZ   (   t   programR   R   t   fnameR   t   exe_file(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   which   s    	 c          C   s   d }  d } t  j j |   r yX xQ t |   D]C } | j |  r. t | t |  j   d  } | d :} | Sq. WWq t k
 r d  St
 k
 r d  SXn  d  S(   Ns   /proc/meminfos	   MemTotal:i    i   i   (   R   R   RE   t   opent
   startswithR0   R-   R*   t
   ValueErrorRZ   t   IOError(   t   mem_info_filenamet   avail_mem_headert   linet	   avail_mem(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_available_memory  s     
c         C   s?   y |  j  d  Wn# t k
 r% t St k
 r6 t SXt Sd  S(   Nt   ascii(   t   encodet   UnicodeDecodeErrort   Falset   UnicodeEncodeErrorR
   (   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyRf     s    s   3.c         C   s#   | r t  |  d  j   S|  j   S(   Ns   utf-8(   R/   t   rstrip(   R   t
   is_python3(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   process_readline   s    c         C   s!   d |  k r d |  d }  n  |  S(   Nt    R   (    (   R/   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   process_spaces&  s    c   	      C   sJ  d d  l  } d d  l } t |  t  r0 |  } n | j |   } | j | d | j d | j d | } d } xe | j   s t	 | j
 j    } | r | r | j |  q | | d 7} n  | j d  k	 ro Pqo qo WxP | j
 j   D]? } t	 |  } | r | r| j |  q#| | d 7} q q W| j rFt |  | | j  n  | S(   NiR   R   t   cwdR"   s   
(   t   shlext
   subprocesst
   isinstancet   listR*   t   Popent   PIPEt   STDOUTt   pollR   R   t   readlineR   t
   returncodeRZ   t	   readlinesR(   (	   R%   R   R   R   R   t   cmd_listt   proct   outputR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   sys_call,  s0    	'	c         C   s$  d d l  } d d l } t |  t  r0 |  } n | j |   } | rW t | d  } n	 | j } | rx t | d  }	 n	 | j }	 | j | d | d |	 d | }
 | r| s | rx |
 j   sC| s t	 |
 j
 j    } | r | j |  q n  | s-t	 |
 j j    } | r-| j |  q-n  |
 j d k	 r Pq q W| sx< |
 j
 j   D]( } | d k rZ| j t	 |   qZqZWn  | sx< |
 j j   D]( } | d k r| j t	 |   qqWqn
 |
 j   | r| j   n  | r|	 j   n  |
 j r t |  | |
 j  n  d S(   s   
    Runs cmd and redirects stdout to out_filename (if specified), stderr to err_filename (if specified), or to log otherwise
    iNt   wR   R   R   R"   (   R   R   R   R   R*   R   R   R   R   R   R   R   R   R   R   RZ   R   t   waitt   closeR(   (   R%   R   t   out_filenamet   err_filenameR   R   R   R   R   R   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   universal_sys_callO  sN    			! 
	c         C   s^   t  | d   } | j |  j    Wd  QXt j | t j t j Bt j Bt j	 Bt j
 B d  S(   Nt   wb(   R   R   t   readR   t   chmodt   statt   S_IWRITEt   S_IREADt   S_IXUSRt   S_IXGRPt   S_IXOTH(   t   datat   fileR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   save_data_to_file  s    c         C   s  d   } | r t  } t } n t } t } t r t |   K } | j t  | j   } x | j   sv | j   } q[ W| j	   } Wd  QXt |   j	   } | j
 |  }	 | |	  | } n t |   j	   } g  }
 g  } d } x | D] } | j |  rq n  | | k rn| | j |  t |  j   } | j | d  j   } | |
 |  s|
 j | |  qq | | k r | | j |  t |  j   } | j   } | | |  s| j | |  qq q W|
 | f S(   Nc         S   s(   x! |  D] } | j  |  r t Sq Wt S(   N(   R9   R
   R   (   t   list_to_checkt   suffixt   item(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   already_saved  s    s    * R"   (   t   SPADES_PY_WARN_MESSAGEt   SPADES_WARN_MESSAGEt   SPADES_PY_ERROR_MESSAGEt   SPADES_ERROR_MESSAGEt   continue_logfile_offsetR   t   seekR   R   R   t   indexR   t   findR-   R;   R:   (   t   log_filenamet   warningsR   t   spades_py_messaget   spades_messaget   continued_logt   continued_stage_phraset   lines_to_checkt	   all_linest   failed_stage_indext   spades_py_msgst   spades_msgst    IMPORTANT_MESSAGE_SUMMARY_PREFIXR   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_important_messages_from_log  sD    		##c         C   s@   d  } x3 |  j d D]$ } | j j d k r | j } q q W| S(   Nt   handlerst   FileHandler(   RZ   t   __dict__t	   __class__t   __name__t   baseFilename(   R   t   log_filet   h(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_logger_filename  s
    c         C   s  t  |   } | s t Sx |  j d D] } | j   q$ Wt | d t \ } } | s\ | r| rr |  j d  n |  j d  t j j	 t j j
 |  d  } t j | d d } |  j |  |  j d  | r|  j d	  x | D] } |  j |  q Wn  | r7|  j d
  x | D] } |  j |  qWn  |  j d |  |  j |  | rt | d t \ }	 }
 |  j d  |  j d  x" |
 |	 D] } |  j |  qWn  t St S(   NR   R   s?   
======= SPAdes pipeline finished abnormally and WITH WARNINGS!s0   
======= SPAdes pipeline finished WITH WARNINGS!s   warnings.logt   modeR   R"   s   === Pipeline warnings:s-   === Error correction and assembling warnings:s   ======= Warnings saved to s   === ERRORs:(   R   R   R   R   R   R
   R   R   R   R   Rd   t   loggingR   t
   addHandlert   removeHandler(   R   R   R   R   t   spades_py_warnst   spades_warnst   warnings_filenamet   warnings_handlerR   t   spades_py_errorst   spades_errors(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyR	     s@    !c         C   s\   t  j j rX t t  j _ t |   } | rX t |  } | j d d  | j   a qX n  d  S(   Ni    i   (	   R7   R\   t   continue_modeR   R   R   R   t   tellR   (   R   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   continue_from_here  s    c            sj   d       f d   } d  } xE t t j |   d | d t D]" } t j j |  r@ | } Pq@ q@ W| S(   Nc         S   s   |  j    r t |   S|  S(   N(   R.   R0   (   t   text(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   atoi  s    
c            s)   g  t  j d |   D] }   |  ^ q S(   Ns   (\d+)(   t   reR*   (   R   t   c(   R   (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   natural_keys  s    t   keyt   reverse(   RZ   t   sortedt   globR
   R   R   R   (   t   patternR   t
   latest_dirt   dir_to_test(    (   R   sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_latest_dir  s    	(c         C   sS   | s t  j j } n  t j j |  s7 t j |  n  t j d | d |   a	 t	 S(   Nt   dirR   (
   R7   R\   t   tmp_dirR   R   R   Ri   t   tempfilet   mkdtempR   (   R   t   base_dir(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_tmp_dir  s    c         C   sc   x\ t  j j   D]K } |  j d |  r |  t d |  t d |  d !j   r[ | Sq q Wd  S(   Ns   --i   (   R7   t   SHORT_READS_TYPESt   keysR   R-   R.   RZ   (   t   optiont   short_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_short_reads_type  s
    +c         C   s;   x4 t  j D]) } |  j d  r
 |  d | k r
 | Sq
 Wd  S(   Ns   --(   R7   Ru   R   RZ   (   R  t   long_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_long_reads_type  s    c         C   s;   x4 t  j D]) } |  j d  r
 |  d | k r
 | Sq
 Wd  S(   Ns   --(   R7   t   GRAPH_READS_TYPESR   RZ   (   R  t   graph_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_graph_type"  s    c         C   s   |  j  d  o |  d j   S(   Ns   --si   (   R   R.   (   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   is_single_read_type)  s    c         C   s   d } d } t  |   rE t  |   } t t j d |   j    } n6 t |   r` t |   } n t |   r{ t |   } n  | | f S(   Nt   pei   s   \d+(   R  R0   R   t   searcht   groupR  R  (   R  t   lib_typet
   lib_number(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_lib_type_and_number-  s    !c         C   s   |  j  d  r d } n |  j  d  r0 d } n |  j  d  rH d } ni |  j  d  s{ t |   s{ t |   s{ t |   r d } n- |  j  d	  s |  j  d
  r d } n d } | S(   Ns   -12s   interlaced readss   -1s
   left readss   -2s   right readss   -ss   single readss   -ms   -mergeds   merged readst   orientation(   R9   R  R  R  (   R  t	   data_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_data_type<  s    					c         C   si   d  } d |  k r_ d |  |  j d   t j k r_ |  |  j d   } |  |  j d  d }  n  |  | f S(   Nt   :R)   i   (   RZ   R   R7   Rr   (   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_option_prefixO  s
    ,c         C   s,  t  |   \ } } d | | f } t |   } | d k rG |  d } n  | | k r i  | | <| | | d <| t j k r t j | | | d <q | | | d <n  | j d  rt |  \ } } | r d | t j | <n  | | | k r| | | j |  q(| g | | | <n | | | | <d  S(   Ns   %s_%dR  it   numbert   typet   readsR)   (   R  R  R7   R  R9   R  RP   R:   (   R  R   t   dataset_dataR  R  t	   record_idR  R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   add_to_datasetW  s&    
c         C   s?  g  } x2t  |  t  r$ |  j   n |  D]} | s: q( n  t } t } xB | j   D]4 } | j d  rq t } n  | d k rS t } PqS qS W| s q( n  | r | d d k r d | d <d	 | k r | d	 =q n  d	 | k r*| d d k s | d d
 k r
d | d	 <q*| d d k r*d | d	 <q*n  | j |  q( W| S(   NR  s   interlaced readss   merged readss
   left readss   right readsR  s
   paired-endt   singleR  s   hq-mate-pairst   frs
   mate-pairst   rf(   s   interlaced readss   merged readss
   left readss   right reads(   R   t   dictt   valuesR   R  R9   R
   R:   (   R  t   corrected_dataset_datat   reads_libraryt	   has_readst   has_paired_readsR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   correct_datasetq  s2    (	
 c   	      C   s   t  t |   } g  } x |  D] } x | j   D] \ } } | j d  r2 g  } x | D]w } t  t | t |    } | t j | <| t j k r | | k r t j | t j | <t j | =n  | j |  qZ W| | | <q2 q2 W| j |  q W| S(   NR  (	   R   R   t   itemsR9   R   R7   RK   RP   R:   (	   R  Rd   t   abs_paths_dataset_dataR$  R   t   valuet   abs_paths_readst
   reads_filet   abs_path(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   relative2abs_paths  s     i
   i   i   c         C   s   g  t  |  | | |  D] } t | | |  ^ q } t |  t |  } xA | D]9 }	 t j |	 |  | k rQ t d |	 | f |  qQ qQ Wt |  }
 | j d |
  |
 S(   NsE   read lengths differ more than allowable. Length: %f. Avg. length: %f.s   
Reads length: %d
(	   t   get_reads_filest   get_max_reads_lengtht   sumR-   t   matht   fabsR   t   minR   (   R  R   t   ignored_typest
   used_typest   num_checkedt   diff_len_allowableR,  t   max_reads_lenghtst   avg_lent   max_lent   reads_length(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_reads_length  s    1c         C   sU   g  t  |  | | |  D] } t | | |  ^ q } t |  } | j d |  | S(   Ns   
Reads length: %d
(   R/  R0  t   maxR   (   R  R   R5  R6  R7  R,  R9  R<  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_primary_max_reads_length  s
    1c         c   s   x |  D] } | d  k	 r/ | d | k r/ q n  xe | j   D]W \ } } | | k rk | j d |  q< q< | j d  r< x | D] } | Vq Wq< q< Wq Wd  S(   NR  s   Files with %s were ignored.R  (   RZ   R(  R   R9   (   R  R   R5  R6  R$  R   R*  R,  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyR/    s    c         C   s   t  |  |  } d } yM t g  t j t j t j |  d  |  |  D] } t |  ^ qF  } WnP t k
 r } t	 | j
 d j d |   d t j   j d |   d | n X| j d |  t |  f  | S(   Ni    RU   RV   s   

R   s   %s: max reads length: %s(   RQ   R>  t	   itertoolst   isliceR   RW   RX   R-   R[   R   R\   R]   R^   R_   R   R/   (   R,  R   R7  RS   t   max_reads_lengtht   recRa   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyR0    s    J$c      
   C   s  g  } xt  |   D]x\ } } d } d } d | k rH | d | d <n  x| j   D]\ }	 }
 |	 j d  rU x |
 D] } t | d |	 | d | d f |  t | d |	 | d | d f | | | d |  | d t j k rt | d |	 | d | d f |  n  | j |  qw W|	 d k r;t	 |
  } qY|	 d k rYt	 |
  } qYqU qU W| | k r t
 d	 | d | d f |  q q Wt	 |  st
 d
 |  n  t | |  d  S(   Ni    R  i   R  s(   %s, library number: %d, library type: %sR  s
   left readss   right readss   the number of files with left paired reads is not equal to the number of files with right paired reads (library number: %d, library type: %s)!s0   you should specify at least one file with reads!(   t	   enumerateR(  R9   RO   R}   R7   t    READS_TYPES_USED_IN_CONSTRUCTIONRb   R:   R-   R   Rn   (   R  Rx   Ry   R   t	   all_filest   idR$  t   left_numbert   right_numberR   R*  R,  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_dataset_reads  s<    	 c         C   s!   t  r t r t d |   n  d  S(   Nsq   it is recommended to specify single reads with --pe<#>-s, --mp<#>-s, --hqmp<#>-s, or --s<#> option instead of -s!(   t   only_old_style_optionst   old_style_single_readsR   (   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   check_single_reads_in_options  s    c         C   se   t  |  t k	 r | g } n  g  } x: t |   D], \ } } | d | k r1 | j |  q1 q1 W| S(   NR  (   R  R   RD  R:   (   R  t   typest   lib_idsRG  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_lib_ids_by_type   s    c         C   s;   t  |  |  } g  } x | D] } | j |  |  q W| S(   N(   RP  R:   (   R  RN  t   idst   resultRG  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_libs_by_type
  s
    c         C   s7   t  |  |  } x! t | d t D] } |  | =q" W|  S(   NR   (   RP  R   R
   (   R  RN  RQ  RG  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   rm_libs_by_type  s    c         C   s   x |  D] } | r t  Sq Wt S(   N(   R   R
   (   R  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   dataset_is_empty  s    c         C   s`   xY |  D]Q } xH | D]@ } | j  d  r x( | | D] } | j  d  r4 t Sq4 Wq q Wq Wt S(   NR  s   .gz(   R9   R
   R   (   R  R$  R   R,  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_gzipped_reads   s    c         C   s%   x |  D] } d | k r t  Sq Wt S(   Ns   interlaced reads(   R
   R   (   R  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_interlaced_reads*  s    c         C   s,   x% |  D] } | d j  d  r t Sq Wt S(   NR  Ro   (   R9   R
   R   (   R  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_additional_contigs1  s    s       c         C   s   d d d d d g } x t  |   D] \ } } | j | d | d | d f  d	 | k r{ | j d
 | | d	 f  n  xM | D]E } | | k r d } n t | |  } | j d | | | f  q Wq" Wd  S(   Ns
   left readss   right readss   interlaced readss   single readss   merged readss$   Library number: %d, library type: %si   R  R  s   %s  orientation: %ss   not specifieds
   %s  %s: %s(   RD  R   R/   (   R  R   t   indentt   READS_TYPESRG  R$  t
   reads_typeR*  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   pretty_print_reads8  s    #	c         C   s   g  } g  } t  } d } | r0 t j |   } n t |   } x | D] } t | | od t j j d   } | sv qC n  | d d k r | j | j    | s | j |  n t	 } d } qC | | j   7} qC W| j |  | j
   t | |  S(   NR"   s   3.i    t   >(   R
   t   gzipR   R   R   R1   R   R:   R   R   R   t   zip(   RN   t   gzippedt   res_namet   res_seqt   firstt   seqt   file_handlerR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt
   read_fastaI  s*    !	
c         C   s   t  |  d  n } xd | D]\ \ } } | j | d  x< t d t |  d  D]" } | j | | | d !d  qO Wq WWd  QXd  S(   NR   s   
i    i<   (   R   R   R,   R-   (   RN   t   fastat   outfilet   nameRd  R3   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   write_fastad  s
    t   Nc         C   s  g  } t  } xt t |  |   D]\ } \ } } d }	 d }
 d } x-|	 t |  k  ru| j d |	  d k ru| d k r t } n  | j d |	  } | d } x0 | t |  k r | | d k r | d 7} q W| d }	 | | | k rI t } | | k ri| j | j   d d t |
  d d j	 | j   d  | | | !j
 d |  f  |
 d 7}
 n  | } qI qI W| t |  k  r" | j | j   d d t |
  d d j	 | j   d  | | j
 d |  f  q" q" W| | f S(   Ni    i   Rk  it   _R   (   R   RD  Rf  R-   R   R
   R:   R*   R/   R   R;   (   RL   t	   thresholdt   replace_charR`  t	   new_fastat   modifiedRG  Ri  Rd  R3   t   cur_contig_numbert   cur_contig_startt   startt   end(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   break_scaffoldsl  s2    (-	
%
<<!c         C   s1   i d d 6d d 6d d 6d d 6d d 6|  j    S(   Nt   Tt   At   Gt   CRk  (   t   upper(   t   letter(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   comp  s    c         C   s&   d j  t j t |  d  d  d    S(   NR"   i(   R   R@  t   imapR|  (   Rd  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   rev_comp  s    c         C   ss   |  j  d  } t |  d k  sA | d d k rS | d d k rS t d |   d  Sd |  k rk | d d S| d S(	   NRl  i   i    s   >NODEt   NODEs   contig %s has unknown ID formatt   'i   (   R*   R-   R   RZ   (   t   sR"  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   get_contig_id  s    2c         C   s   |  j  d  r |  d S|  S(   NR]  i   (   R   (   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   remove_fasta_pref  s    c         C   s+   y t  |   t SWn t k
 r& t SXd  S(   N(   t   floatR
   R   R   (   R*  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   is_float  s
    
c         C   s+   y t  |   t SWn t k
 r& t SXd  S(   N(   R0   R
   R   R   (   R*  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   is_int  s
    
i'  i'  (e   R   R^  R@  R   R2  R   R   R   R   R   R   R^   t   platformR    t   distutils.versionR   t   os.pathR   R   R   R7   t   commonR   R   R   R   R   RZ   R   R   R
   RK  R   RL  R   R   R#   R$   R(   RC   RD   RI   RO   RQ   Rb   Re   RJ   Rj   Rk   Rn   R}   R   R   Rf   R1   R   R   R   R   R   R   R   R   R	   R   R   R  R  R  R  R  R  R  R  R  R'  R.  RE  R=  R?  R/  R0  RJ  RM  RP  RS  RT  RU  RV  RW  RX  R\  Rf  Rj  Ru  R|  R~  R  R  R  R  (    (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.4-Linux/share/spades/spades_pipeline/support.pyt   <module>
   s   												#				#7	0	%																&		
				
						
		