ó
êOü_c           @   sð   d  d l  Z  d  d l Z d  d l Z d  d l Z d d d d d d d d	 g Z d
 d g Z d „  Z d d d „  ƒ  YZ d d d „  ƒ  YZ d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿNs   .fas   .fass   .fastas   .seqs   .fsas   .fnas   .ffns   .frns   .fqs   .fastqc         C   sN   |  j  ƒ  j d ƒ r4 t j d ƒ t j |  | ƒ ƒ St j |  | d d ƒSd  S(   Ns   .gzs   .gzips   UTF-8t   encodings   utf-8(   s   .gzs   .gzip(   t   lowert   endswitht   codecst	   getreadert   gzipt   open(   t   ft   mode(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   Open   s    t   Readerc           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s   | |  _  d  |  _ d  S(   N(   t   handlert   Nonet   cash(   t   selfR   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   __init__   s    	c         C   s(   |  j  d  k r$ |  j j ƒ  |  _  n  d  S(   N(   R   R   R   t   readline(   R   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   FillCash   s    c         C   s   d  |  _ d  S(   N(   R   R   (   R   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt	   TrashCash    s    c         C   s   |  j  ƒ  |  j S(   N(   R   R   (   R   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   Top#   s    
c         C   s$   |  j  ƒ  |  j ƒ  } |  j ƒ  | S(   N(   R   R   R   (   R   t   result(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR   '   s    

c         C   sn   g  } xX t  r` |  j ƒ  } | d k s3 | | ƒ r@ d j | ƒ S|  j ƒ  | j | j ƒ  ƒ q	 Wd j | ƒ S(   Nt    (   t   TrueR   t   joinR   t   appendt   strip(   R   R   R   t   next(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt
   ReadUntill-   s    	
c         C   s»   d } g  } x„ |  j  ƒ  r’ |  j ƒ  d k r’ | t |  j ƒ  j ƒ  ƒ | k r’ | j |  j ƒ  j ƒ  ƒ | t |  j ƒ  j ƒ  ƒ 7} |  j ƒ  q W| | k r® t d ƒ ‚ n  d j | ƒ S(   Ni    R   s€   The sequence and quality strings for one of reads have different length in file {FILE}. Please check the correctness of the file(   t   EOFR   t   lenR   R   R   t	   ExceptionR   (   R   t   buf_sizet   cntR   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   ReadUntillFill7   s    Dc         C   s   |  j  ƒ  d k S(   NR   (   R   (   R   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR   D   s    (
   t   __name__t
   __module__R   R   R   R   R   R   R!   R   (    (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR
      s   						
	t	   SeqRecordc           B   s8   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   sˆ   | d  k r? t | ƒ t | ƒ k r? t j j d | d ƒ n  | d  k si t | ƒ t | ƒ k si t ‚ | |  _ | |  _ | |  _ d  S(   Nt   oppa(	   R   R   t   syst   stdoutt   writet   AssertionErrort   idt   seqt   qual(   R   R+   R*   R,   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR   I   s    $*		c         C   s   t  |  j ƒ S(   N(   R   R+   (   R   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   __len__Q   s    c         C   s   |  j  | S(   N(   R+   (   R   t   key(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   __getitem__T   s    c         C   s!   |  j  d  k r |  j  | | !Sd  S(   N(   R,   R   (   R   t   lt   r(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt
   QualSubseqW   s    c         C   sv   | d k s! | t  |  j ƒ k rn t |  j | | !|  j d t | d ƒ d t | ƒ d |  j | | ƒ ƒ S|  Sd  S(   Ni    t   (i   t   -t   )(   R   R+   R$   R*   t   strR2   (   R   R0   R1   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   subseq\   s    !MN(   R"   R#   R   R   R-   R/   R2   R7   (    (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR$   H   s
   			c         C   sB   | d k s t  ‚ | d k r( t |  ƒ S| d k r> t |  ƒ Sd  S(   Nt   fastat   fastq(   R8   R9   (   R)   t   parse_fastat   parse_fastq(   R   t	   file_type(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   parsec   s
    
c         c   sŠ   t  |  ƒ } xw | j ƒ  s… | j ƒ  j ƒ  } t | ƒ d k  sO | d d k r^ t d ƒ ‚ n  | j d „  ƒ } t | | d ƒ Vq Wd  S(   Ni   i    t   >s    {FILE} is not a valid FASTA filec         S   s   |  j  d ƒ S(   NR>   (   t
   startswith(   t   s(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   <lambda>q   s    (   R
   R   R   R   R   R   R   R$   (   R   t   readert   rec_idt   rec_seq(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR:   k   s    "c         c   sß   t  |  ƒ } xÌ | j ƒ  sÚ | j ƒ  j ƒ  } t | ƒ d k  sO | d d k r^ t d ƒ ‚ n  | j d „  ƒ } | j ƒ  } t | ƒ d k  sž | d d k r­ t d ƒ ‚ n  | j t | ƒ ƒ } t | | d | ƒ Vq Wd  S(   Ni   i    t   @s    {FILE} is not a valid FASTQ filec         S   s   |  j  d ƒ S(   Nt   +(   R?   (   R@   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyRA   {   s    RF   (	   R
   R   R   R   R   R   R   R!   R$   (   R   RB   RC   RD   t   tmpt   rec_qual(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR;   u   s    ""c         C   s›   | d k r; | j  d |  j d ƒ | j  |  j d ƒ n\ | d k r— | j  d |  j d ƒ | j  |  j d ƒ | j  d ƒ | j  |  j d ƒ n  d  S(   NR8   R>   s   
R9   RE   RF   s   +
(   R(   R*   R+   R,   (   t   recR   R<   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyR(   ƒ   s    c         C   s=   x6 t  |  | ƒ D]% } | | ƒ r t | | | ƒ q q Wd  S(   N(   R=   R(   (   t   input_handlert   output_handlerR   R<   t   contig(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   FilterContigsŽ   s    c         C   sÃ   x¼ t  |  d ƒ D]« } d } x0 | t | ƒ k  rN | | d k rN | d 7} q Wt | ƒ } x. | | k r‹ | | d d k r‹ | d 8} q^ W| | k r t t | j | | !| j ƒ ƒ q q Wd  S(   NR8   i    t   Ni   (   R=   R   R(   R$   R+   R*   (   RJ   RK   RL   R0   R1   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   RemoveNs”   s    %#c         C   sb   |  j  ƒ  }  t j j |  ƒ \ } } | d k r@ |  d } } n  t j j | ƒ \ } } | | k S(   Ns   .gzs   .gzipR   (   s   .gzs   .gzip(   R   t   ost   patht   splitext(   t   fnamet   extension_listt   basename_plus_innerextt	   outer_extt   basenamet	   inner_ext(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   check_extension    s    c         C   s   t  |  t ƒ S(   N(   RY   t	   fasta_ext(   t	   file_name(    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   is_fasta¬   s    c         C   s   t  |  t ƒ S(   N(   RY   t	   fastq_ext(   R[   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   is_fastq°   s    c         C   s   t  |  d g ƒ S(   Ns   .bam(   RY   (   R[   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   is_bam´   s    c         C   s8   t  |  ƒ r d St |  ƒ r  d St |  ƒ r0 d Sd  Sd  S(   NR9   R8   t   bam(   R^   R\   R_   R   (   R[   (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   get_read_file_type¸   s    (    (    (   R   R   R&   RP   RZ   R]   R	   R
   R$   R=   R:   R;   R(   RM   RO   RY   R\   R^   R_   Ra   (    (    (    sp   /oak/stanford/groups/akundaje/marinovg/programs/SPAdes-3.15.0-Linux/share/spades/spades_pipeline/common/SeqIO.pyt   <module>   s&   	1		
								