
^c           @   sv  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	  Z d d d
  Z  d   Z! d   Z" 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   Z- e	 j. j/ d  d  Z0 d   Z1 d d d  Z2 d d d d  Z3 d   Z4 e d  Z5 d    Z6 e d!  Z7 d"   Z8 d#   Z9 d d d$  Z: d%   Z; d&   Z< d'   Z= d(   Z> d)   Z? d*   Z@ d+   ZA d,   ZB d-   ZC e jD dK d0 d1  ZE dL d2  ZF d d3  ZG d4   ZH d5   ZI d6   ZJ d7   ZK d8   ZL d9   ZM d:   ZN d;   ZO d<   ZP d=   ZQ d>   ZR d? d@  ZS e dA  ZT dB   ZU dC e dD  ZV dE   ZW dF   ZX dG   ZY dH   ZZ dI   Z[ dJ   Z\ d S(M   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  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.
i   (   t   infot   log_warningst   Truet   lowert   syst   stderrt   writet   flusht   current_tmp_dirt   ost   patht   isdirt   shutilt   rmtreet   exit(   t   err_strt   logt   prefixt   binary_name(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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.14.0-Linux/share/spades/spades_pipeline/support.pyt   warning@   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.14.0-Linux/share/spades/spades_pipeline/support.pyt   __next_versionI   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.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_python_versionH   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.14.0-Linux/share/spades/spades_pipeline/support.pyt    get_spades_binaries_info_messagec   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   R9   (   t
   binary_dirR   t   binaryt   binary_path(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_binariesi   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.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_file_existencep   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   R@   R   R   R   R   R,   RA   (   t   input_dirnameRC   R   t   dirname(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_dir_existencey   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   RC   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyR@      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(   RG   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   RL   (   RG   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   RD   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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  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)(   R,   t   dict_of_prefixesR   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_EXTENSIONS(	   RD   RC   t   only_assemblert
   iontorrentt   library_typeR   t   extt   pre_extt   pre_pre_ext(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_reads_file_format   s,    !%##$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.14.0-Linux/share/spades/spades_pipeline/support.pyt   is_exe   s    t   PATHt   "(   R   R   R   t   environt   pathsept   stripR   t   None(   t   programRd   Rc   t   fnameR   t   exe_file(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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
   ValueErrorRj   t   IOError(   t   mem_info_filenamet   avail_mem_headert   linet	   avail_mem(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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	   (   Ru   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyRI      s    s   3.c         C   s#   | r t  |  d  j   S|  j   S(   Ns   utf-8(   R$   t   rstrip(   Ru   t
   is_python3(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   process_readline   s    c         C   s!   d |  k r d |  d }  n  |  S(   Nt    Rf   (    (   R$   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   process_spaces   s    c   	      C   sQ  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 rMt d |  | j f |  n  | S(   NiR   R   t   cwdR?   s   
s>   system call for: "%s" finished abnormally, OS return value: %d(   t   shlext
   subprocesst
   isinstancet   listR   t   Popent   PIPEt   STDOUTt   pollR   R   t   readlineR   t
   returncodeRj   t	   readlinesR   (	   t   cmdR   R   R   R   t   cmd_listt   proct   outputRu   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-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 d |  |
 j f |  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: %d(   R   R   R   R   R   Ro   R   R   R   R   R   R   R   R   R   Rj   R   t   waitt   closeR   (   R   R   t   out_filenamet   err_filenameR   R   R   R   R   R   R   Ru   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   universal_sys_call  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(   Ro   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.14.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.14.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_offsetRo   t   seekR   Ri   R   t   indexRp   t   findR"   R0   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_PREFIXRu   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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(   Rj   t   __dict__t	   __class__t   __name__t   baseFilename(   R   t   log_filet   h(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   RG   t   loggingR   t
   addHandlert   removeHandler(   R   R   R   R   t   spades_py_warnst   spades_warnst   warnings_filenamet   warnings_handlerRu   t   spades_py_errorst   spades_errors(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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,   t   argst   continue_modeR{   R   Ro   R   t   tellR   (   R   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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.14.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.14.0-Linux/share/spades/spades_pipeline/support.pyt   natural_keys  s    t   keyt   reverse(   Rj   t   sortedt   globR	   R   R   R   (   t   patternR   t
   latest_dirt   dir_to_test(    (   R   sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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,   R   t   tmp_dirR   R   R   RL   t   tempfilet   mkdtempR   (   R   t   base_dir(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   keysRp   R"   R#   Rj   (   t   optiont   short_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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,   RX   Rp   Rj   (   R   t   long_reads_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   get_long_reads_type  s    c         C   s   |  j  d  o |  d j   S(   Ns   --si   (   Rp   R#   (   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   is_single_read_type  s    c         C   sn   d } d } t  |   rI t  |   } t |  t j d |   j    } n t |   rd t |   } n  | | f S(   Nt   pei   s   \d(   R   R%   R   t   searcht   startR   (   R   t   lib_typet
   lib_number(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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 } nu |  j  d  rH d } n] |  j  d  so t |   so t |   rx 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   t	   data_type(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   (   Rj   R   R,   RU   (   R   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   get_option_prefix  s
    ,c         C   s  t  |   \ } } d | | f } t |   } | d k rG |  d } n  | | k r i  | | <| 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   typet   readsR   (   R   R   R,   R   R.   R   RS   R/   (   R   R   t   dataset_dataR   R   t	   record_idR   R   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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	   R/   (   R   t   corrected_dataset_datat   reads_libraryt	   has_readst   has_paired_readsR   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   correct_dataset,  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,   RA   RS   R/   (	   R   RG   t   abs_paths_dataset_dataR  R   t   valuet   abs_paths_readst
   reads_filet   abs_path(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   relative2abs_pathsI  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.14.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  R  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   get_primary_max_reads_lengthk  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   (   Rj   R
  R   R.   (   R   R   R  R  R  R   R  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyR  t  s    c         C   s   |  t  j k r. t  j |  } t j |  } n t j |   } | sW t d |  |  n  t g  t j t j t j	 |  d  |  |  D] } t
 |  ^ q  } | j d |  t |  f  | S(   Ns%   incorrect extension of reads file: %st   rs   %s: max reads length: %s(   R,   RS   R   t   get_read_file_typeR   R   t	   itertoolst   islicet   parset   OpenR"   R   R$   (   R  R   R  R]   t	   file_typet   rect   max_reads_length(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyR    s    Fc      
   C   sd  g  } x.t  |   D] \ } } d } d } x | j   D] \ }	 }
 |	 j d  r8 xr |
 D]j } t | d |	 | d | d f |  t | d |	 | d | d f | | | d |  | j |  qZ W|	 d k r t |
  } q|	 d k rt |
  } qq8 q8 W| | k r t d | d | d f |  q q Wt |  sSt d	 |  n  t | |  d  S(
   Ni    R   s(   %s, library number: %d, library type: %si   R   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.   RE   R`   R/   R"   R   RQ   (   R   RZ   R[   R   t	   all_filest   idR  t   left_numbert   right_numberR   R  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   check_dataset_reads  s0    	 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.14.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+  R/   (   R   t   typest   lib_idsR-  R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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(   R6  R/   (   R   R4  t   idst   resultR-  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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   (   R6  R   R	   (   R   R4  R7  R-  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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.14.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.14.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.14.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   RR   (   R.   R	   R{   (   R   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_additional_contigs  s    c         C   s)   x" |  D] } | d d k r t  Sq Wt S(   NR   t   nxmate(   R	   R{   (   R   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   dataset_has_nxmate_reads  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.14.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   gzipRo   R   R   R&   Rp   R/   Ri   R{   R   t   zip(   RD   t   gzippedt   res_namet   res_seqt   firstt   seqt   file_handlerRu   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt
   read_fasta  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<   (   Ro   R   R!   R"   (   RD   t   fastat   outfilet   nameRL  R(   (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   write_fasta  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   RS  it   _R   (   R{   R+  RN  R"   R   R	   R/   R   R$   R   R0   (   RB   t	   thresholdt   replace_charRH  t	   new_fastat   modifiedR-  RQ  RL  R(   t   cur_contig_numbert   cur_contig_startR   t   end(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   break_scaffolds&  s2    (-	
%
<<!c         C   s1   i d d 6d d 6d d 6d d 6d d 6|  j    S(   Nt   Tt   At   Gt   CRS  (   t   upper(   t   letter(    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   compC  s    c         C   s&   d j  t j t |  d  d  d    S(   NR?   i(   R   R$  t   imapRc  (   RL  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   rev_compG  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(	   NRT  i   i    s   >NODEt   NODEs   contig %s has unknown ID formatt   'i   (   R   R"   R   Rj   (   t   sR  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   get_contig_idK  s    2c         C   s   |  j  d  r |  d S|  S(   NRE  i   (   Rp   (   Rh  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   remove_fasta_prefU  s    c         C   s+   y t  |   t SWn t k
 r& t SXd  S(   N(   t   floatR	   Rq   R{   (   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.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	   Rq   R{   (   R  (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   is_intc  s
    
i'  i'  (]   R   RF  R$  R   R  R   R   R   R   R   R   t   distutils.versionR    t   os.pathR   R   R   R,   t   commonR   R   R   R   R   Rj   R   R   R	   R1  R{   R2  R   R   R8   R9   R>   RE   RH   R@   RM   RN   RQ   R`   Rn   Rw   RI   R&   Rp   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R	  R  t    READS_TYPES_USED_IN_CONSTRUCTIONR  R!  R  R  R0  R3  R6  R9  R:  R;  R<  R=  R>  R@  RD  RN  RR  R\  Rc  Re  Ri  Rj  Rl  Rm  (    (    (    sk   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.14.0-Linux/share/spades/spades_pipeline/support.pyt   <module>
   s   													#7	0	%																	
				
							
		