ó
›¢yZc           @€  s/  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z y d  d l Z Wn! e k
 rs d  d l m Z n Xe j	 d d k r– d  d l
 Z
 n  d  d l m Z d  d l m Z e e j ƒ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   with_statementN(   t   bz2i    i   (   t   qconfig(   t
   get_loggerc         C€  s  d  } t j j |  ƒ \ } } t j |  t j ƒ sS t j d |  d t d d ƒn  | d k rw t	 j
 |  d d ƒ} n–| d k r§ t j |  d d ƒ} t | ƒ } nf| d k rÁy t j |  d d ƒ} WnC t k
 rt j ƒ  \ } } } t j d t | ƒ d d ƒqX| j ƒ  } t | ƒ d k rJt j d |  d d ƒn  t | ƒ d k rtt j d | d ƒ n  y# | j
 | d ƒ } t | ƒ } Wqt k
 r½t j d d d ƒqXnL y t
 |  ƒ } Wn9 t k
 rt j ƒ  \ } } } t j | d d ƒn X| S(   Ns   Permission denied accessing t	   to_stderrt   exit_with_codei   s   .gzs   .gzipt   modet   rts   .bz2s   .bzip2t   rs   .zips   Can't open zip file: i    s    Reading %s: zip archive is emptys3   Zip archive must contain exactly one file. Using %ss=   Use python 2.6 or newer to work with contigs directly in zip.i   t	   exit_code(   s   .gzs   .gzip(   s   .bz2s   .bzip2(   s   .zip(   t   Nonet   ost   patht   splitextt   accesst   R_OKt   loggert   errort   Truet   gzipt   openR   t   BZ2Filet   _read_compressed_filet   zipfilet   ZipFilet	   Exceptiont   syst   exc_infot   strt   namelistt   lent   warningt   AttributeErrort   IOErrort	   exception(   t   fpatht
   fasta_filet   _t   extt   zfilet   exc_typet	   exc_valuet   names(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   _get_fasta_file_handler   s>     !c         C€  s3   t  j d d k r/ t j t j |  j ƒ  ƒ ƒ S|  S(   Ni    i   (   R   t   version_infot   iot   TextIOWrappert   BytesIOt   read(   t   compressed_file(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyR   G   s    c         C€  sé   t  ƒ  } d } d } t |  ƒ } x­ | D]¥ } | j d ƒ d k rU | j d ƒ } n	 | g } xl | D]d } | sw qe n  | d d k r³ | r  | | | <d } n  | d j ƒ  } qe | t | j ƒ  ƒ 7} qe Wq( W| | | <| j ƒ  | S(   se   
        Takes filename of FASTA-file
        Returns list of lengths of sequences in FASTA-file
    i    s   iÿÿÿÿt   >i   N(   t   dictR
   R+   t   findt   splitt   stripR   t   close(   R#   t   chr_lengthst   lt   chr_nameR$   t   raw_linet   linest   line(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   get_chr_lengths_from_fastafileM   s(    		
	

c         C€  sÞ  d } d } d } d  } |  d } g  } t |  ƒ } x| D]} | j d ƒ d k rk | j d ƒ }	 n	 | g }	 xÏ |	 D]Ç }
 |
 s q{ n  |
 d d k r| rê | j | | | t | j ƒ  ƒ t | ƒ g ƒ | | 7} d } d } n  |
 d j ƒ  } | t |
 ƒ 7} q{ | s|
 } n  | t |
 j ƒ  ƒ 7} | t |
 ƒ 7} q{ Wq> WWd  QX| j | | | t | j ƒ  ƒ t | ƒ g ƒ t | d ƒ K } xA | D]9 } | j d j g  | D] } t	 | ƒ ^ q°ƒ d	 ƒ q—WWd  QXd  S(
   Ni    s   .fais   iÿÿÿÿR2   i   t   ws   	s   
(
   R
   R   R4   R5   t   appendR   R6   t   writet   joinR   (   t   fasta_fpathR9   t   total_offsett
   chr_offsetR:   t	   fai_fpatht
   fai_fieldst   in_fR;   R<   R=   t   chr_linet   out_ft   fieldst   fs(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   create_fai_filek   s<    
	.
		.c         C€  s¾   t  j j | ƒ s" t  j | ƒ n  d } x| t |  ƒ D]n } | d d k r | r^ | j ƒ  n  t t  j j | | d j ƒ  d ƒ d ƒ } n  | r5 | j	 | ƒ q5 q5 W| rº | j ƒ  n  d S(   sð   
        Takes filename of FASTA-file and directory to output
        Creates separate FASTA-files for each sequence in FASTA-file
        Returns nothing
        Oops, similar to: pyfasta split --header "%(seqid).fasta" original.fasta
    i    R2   i   s   .faR?   N(
   R   R   t   isdirt   mkdirR
   R   R7   RB   R6   RA   (   R#   t   output_dirpatht   outFileR=   (    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   split_fastaŽ   s    /c         c€  së   t  } g  } d } t |  ƒ } x™ | D]‘ } | j d ƒ } xy | D]q } | sS qA n  | d d k rŸ | s€ | d j | ƒ f Vn  t } | j ƒ  d } g  } qA | j | j ƒ  ƒ qA Wq% W| sÆ | rÝ | d j | ƒ f Vn  | j ƒ  d S(   sB   
        Returns list of FASTA entries (in tuples: name, seq)
    t    s   i    R2   i   N(   R   R+   R5   RB   t   FalseR6   R@   R7   (   R#   t   firstt   seqt   nameR$   R;   R<   R=   (    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt
   read_fasta£   s&    	c   	      C€  s  t  } g  } d } t |  ƒ } g  } x¡ | D]™ } | j d ƒ } x | D]y } | sY qG n  | d d k r­ | sŽ | j | d j | ƒ f ƒ n  t } | j ƒ  d } g  } qG | j | j ƒ  ƒ qG Wq+ W| sÔ | ró | j | d j | ƒ f ƒ n  | j ƒ  | S(   sB   
        Returns list of FASTA entries (in tuples: name, seq)
    RS   s   i    R2   i   (   R   R+   R5   R@   RB   RT   R6   R7   (	   R#   RU   RV   RW   R$   t   list_seqR;   R<   R=   (    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   read_fasta_one_timeÂ   s*    	
c         C€  sW   xP |  D]H \ } } d | GHx0 t  d t | ƒ d ƒ D] } | | | d !GHq5 Wq Wd  S(   Ns   >%si    i<   (   t   rangeR   (   t   fastaRW   RV   t   i(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   print_fastaã   s    	R?   c         C€  s„   t  |  | ƒ } xd | D]\ \ } } | j d | ƒ x< t d t | ƒ d ƒ D]" } | j | | | d !d ƒ qL Wq W| j ƒ  d  S(   Ns   >%s
i    i<   s   
(   R   RA   R[   R   R7   (   R#   R\   R   t   outfileRW   RV   R]   (    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   write_fastaê   s    $c         C€  s1   i d d 6d d 6d d 6d d 6d d 6|  j  ƒ  S(   Nt   Tt   At   Gt   Ct   N(   t   upper(   t   letter(    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   compô   s    c         €  s8   t  t d d ƒ ƒ ‰  d j ‡  f d †  t |  ƒ Dƒ ƒ S(   Nt
   ATCGNatcgnt
   TAGCNtagcnRS   c         3€  s!   |  ] } ˆ  j  | d  ƒ Vq d S(   RS   N(   t   get(   t   .0t
   nucleotide(   t   c(    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pys	   <genexpr>ú   s    (   R3   t   zipRB   t   reversed(   RV   (    (   Rn   sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   rev_compø   s    (   t
   __future__R    R   R   R   R   R   t   ImportErrort   quast_libs.site_packagesR,   R-   t
   quast_libsR   t   quast_libs.logR   t   LOGGER_DEFAULT_NAMER   R+   R   R>   RM   RR   RX   RZ   R^   R`   Rh   Rq   (    (    (    sE   /home/birch/BIRCH/local/install/quast-4.6.3/quast_libs/fastaparser.pyt   <module>   s0   	,			#			!	
	