
O_c           @   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 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 d d  Z% d d  Z& d d d  Z' d d d  Z( d d  Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 e	 j1 j2 d  d  Z3 d   Z4 d d d  Z5 d d d d  Z6 d    Z7 e d!  Z8 d"   Z9 e d#  Z: d$   Z; d%   Z< d d d&  Z= d'   Z> d(   Z? d)   Z@ d*   ZA d+   ZB d,   ZC d-   ZD d.   ZE d/   ZF d0   ZG e jH dM d3 d4  ZI dN d5  ZJ d d6  ZK d7   ZL d8   ZM d9   ZN d:   ZO d;   ZP d<   ZQ d=   ZR d>   ZS d?   ZT d@   ZU dA dB  ZV e dC  ZW dD   ZX dE e dF  ZY dG   ZZ dH   Z[ dI   Z\ dJ   Z] dK   Z^ dL   Z_ d S(O   iN(   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.0-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.0-Linux/share/spades/spades_pipeline/support.pyt   warningA   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.0-Linux/share/spades/spades_pipeline/support.pyt   __next_versionJ   s    i    t   -t   +is   Python%s: %ss    and highers=   python version %s is not supported!
Supported versions are %ss   , (   R   R'   R    t   options_storaget   SUPPORTED_PYTHON_VERSIONSt   endswitht   appendt   replaceR    R	   R   R   (   R*   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.0-Linux/share/spades/spades_pipeline/support.pyt   check_python_versionI   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.0-Linux/share/spades/spades_pipeline/support.pyt    get_spades_binaries_info_messaged   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   R:   (   t
   binary_dirR   t   binaryt   binary_path(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   check_binariesj   s    t    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   R;   R   R-   t   dict_of_rel2abs(   t   input_filenamet   messageR   t   filename(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   check_file_existenceq   s    c         C   s_   |  t  j k r2 t  j |  } t j d |  } n t j |   } | s[ t d |  |  n  | S(   NRE   s%   incorrect extension of reads file: %s(   R-   t   dict_of_prefixesR   t   get_read_file_typeR   (   RC   R   t   extt	   file_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyRH   z   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   RH   R   t   parset   Opent   nextt   NoneR   t	   Exceptiont   argst   formatt	   tracebackt
   format_exc(   RC   RD   R   RE   RJ   t   reads_iteratort   inst(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   RA   R   R   R   R   R-   RB   (   t   input_dirnameRD   R   t   dirname(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   RD   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyRA      s    c         C   sH   t  j j |   r" t  j |   n  t  j j |   sD t  j |   n  d  S(   N(   R   R   R;   t   removet   existst   makedirs(   R[   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   R`   (   R[   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   RE   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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)(   R-   RG   R   R   t   splitextR
   R#   t   ALLOWED_READS_EXTENSIONSR   R   t(   IONTORRENT_ONLY_ALLOWED_READS_EXTENSIONSt   BH_ALLOWED_READS_EXTENSIONSt   LONG_READS_TYPESR/   t    CONTIGS_ALLOWED_READS_EXTENSIONSt   GRAPH_ALLOWED_READS_EXTENSIONS(	   RE   RD   t   only_assemblert
   iontorrentt   library_typeR   RI   t   pre_extt   pre_pre_ext(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   R;   t   accesst   X_OK(   t   fpath(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   is_exe   s    t   PATHt   "(   R   R   R    t   environt   pathsept   stripR   RQ   (   t   programRx   Rw   t   fnameR   t   exe_file(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   R;   t   opent
   startswithR&   R#   R    t
   ValueErrorRQ   t   IOError(   t   mem_info_filenamet   avail_mem_headert   linet	   avail_mem(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-Linux/share/spades/spades_pipeline/support.pyR]      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.0-Linux/share/spades/spades_pipeline/support.pyt   process_readline  s    c         C   s!   d |  k r d |  d }  n  |  S(   Nt    Rz   (    (   R%   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   process_spaces  s    c   	      C   sZ  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 rVt d |  | j f | d | j n  | S(	   NiR   R   t   cwdR@   s   
s>   system call for: "%s" finished abnormally, OS return value: %dR   (   t   shlext
   subprocesst
   isinstancet   listR    t   Popent   PIPEt   STDOUTt   pollR   R   t   readlineR   t
   returncodeRQ   t	   readlinesR   (	   t   cmdR   R   R   R   t   cmd_listt   proct   outputR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   sys_call  s0    	'	&c         C   s4  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 r0t d |  |
 j f | d	 |
 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@   s>   system call for: "%s" finished abnormally, OS return value: %dR   (   R   R   R   R   R    R   R   R   R   R   R   R   R   R   R   RQ   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.0-Linux/share/spades/spades_pipeline/support.pyt   universal_sys_call7  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.0-Linux/share/spades/spades_pipeline/support.pyt   save_data_to_filen  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(   R/   R	   R   (   t   list_to_checkt   suffixt   item(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   already_savedv  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#   R1   R0   (   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.0-Linux/share/spades/spades_pipeline/support.pyt   get_important_messages_from_logu  sD    		##c         C   s@   d  } x3 |  j d D]$ } | j j d k r | j } q q W| S(   Nt   handlerst   FileHandler(   RQ   t   __dict__t	   __class__t   __name__t   baseFilename(   R   t   log_filet   h(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   R[   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.0-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   (	   R-   RS   t   continue_modeR   R   R   R   t   tellR   (   R   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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$   R&   (   t   text(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-Linux/share/spades/spades_pipeline/support.pyt   natural_keys  s    t   keyt   reverse(   RQ   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.0-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   (
   R-   RS   t   tmp_dirR   R   R   R`   t   tempfilet   mkdtempR   (   R   t   base_dir(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   (   R-   t   SHORT_READS_TYPESt   keysR   R#   R$   RQ   (   t   optiont   short_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   --(   R-   Rl   R   RQ   (   R   t   long_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   --(   R-   t   GRAPH_READS_TYPESR   RQ   (   R   t   graph_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-Linux/share/spades/spades_pipeline/support.pyt   is_single_read_type  s    c         C   s   d } d } t  |   rI t  |   } t |  t j d |   j    } n6 t |   rd t |   } n t |   r t |   } n  | | f S(   Nt   pei   s   \d(   R   R&   R   t   searcht   startR   R  (   R   t   lib_typet
   lib_number(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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(   R/   R  R   R  (   R   t	   data_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   (   RQ   R   R-   Ri   (   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   get_option_prefix7  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  R-   R   R/   R  RG   R0   (   R   R   t   dataset_dataR  R	  t	   record_idR  R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   add_to_dataset?  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   R/   R	   R0   (   R  t   corrected_dataset_datat   reads_libraryt	   has_readst   has_paired_readsR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   correct_datasetY  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   itemsR/   R   R-   RB   RG   R0   (	   R  R[   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.0-Linux/share/spades/spades_pipeline/support.pyt   relative2abs_pathsv  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.0-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'  R(  t   maxR   (   R  R   R-  R.  R/  R$  R1  R4  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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  (   RQ   R   R   R/   (   R  R   R-  R.  R  R   R"  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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    RL   RM   s   

R   s   %s: max reads length: %s(   RH   R6  t	   itertoolst   isliceR   RN   RO   R#   RR   R   RS   RT   RU   RV   R   R%   (   R$  R   R/  RJ   t   max_reads_lengtht   recRX   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyR(    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   R/   RF   Rt   R-   t    READS_TYPES_USED_IN_CONSTRUCTIONRY   R0   R#   R   Re   (   R  Ro   Rp   R   t	   all_filest   idR  t   left_numbert   right_numberR   R"  R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-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   R<  R0   (   R  t   typest   lib_idsR?  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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(   RH  R0   (   R  RF  t   idst   resultR?  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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   (   RH  R   R	   (   R  RF  RI  R?  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-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(   R/   R	   R   (   R  R  R   R$  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-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  Rf   (   R/   R	   R   (   R  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_additional_contigs  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(   R<  R   R%   (   R  R   t   indentt   READS_TYPESR?  R  t
   reads_typeR"  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   pretty_print_reads   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   R'   R   R0   R}   R   R   t   zip(   RE   t   gzippedt   res_namet   res_seqt   firstt   seqt   file_handlerR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt
   read_fasta1  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#   (   RE   t   fastat   outfilet   nameR\  R)   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   write_fastaL  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   Rc  it   _R   (   R   R<  R^  R#   R   R	   R0   R    R%   R   R1   (   RC   t	   thresholdt   replace_charRX  t	   new_fastat   modifiedR?  Ra  R\  R)   t   cur_contig_numbert   cur_contig_startR  t   end(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   break_scaffoldsT  s2    (-	
%
<<!c         C   s1   i d d 6d d 6d d 6d d 6d d 6|  j    S(   Nt   Tt   At   Gt   CRc  (   t   upper(   t   letter(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   compq  s    c         C   s&   d j  t j t |  d  d  d    S(   NR@   i(   R   R8  t   imapRs  (   R\  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   rev_compu  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(	   NRd  i   i    s   >NODEt   NODEs   contig %s has unknown ID formatt   'i   (   R    R#   R   RQ   (   t   sR  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   get_contig_idy  s    2c         C   s   |  j  d  r |  d S|  S(   NRU  i   (   R   (   Rx  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-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.0-Linux/share/spades/spades_pipeline/support.pyt   is_float  s
    
c         C   s+   y t  |   t SWn t k
 r& t SXd  S(   N(   R&   R	   R   R   (   R"  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   is_int  s
    
i'  i'  (`   R   RV  R8  R   R*  R   R   R   R   R   R   RU   t   distutils.versionR    t   os.pathR   R   R   R-   t   commonR   R   R   R   R   RQ   R   R   R	   RC  R   RD  R   R   R9   R:   R?   RF   RH   RY   R\   RA   Ra   Rb   Re   Rt   R   R   R]   R'   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R
  R  R  R  R  R&  R=  R5  R7  R'  R(  RB  RE  RH  RK  RL  RM  RN  RO  RP  RT  R^  Rb  Rl  Rs  Ru  Ry  Rz  R|  R}  (    (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/support.pyt   <module>
   s   									#				#7	0	%																&		
				
						
		