ó
£žbc           @€  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 m Z Wn! e k
 rw d  d l	 m Z n Xy d  d l
 Z
 Wn! e k
 r« 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 e 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   OrderedDict(   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(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyR   M   s    c         C€  s/   y |  d j  ƒ  d SWn t k
 r* d SXd S(   sk   
        Extracts name from fasta entry line:
        ">chr1  length=100500; coverage=15;" ---> "chr1"
    i   i    t    N(   t   splitt
   IndexError(   t   line(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   __get_entry_nameS   s    c         C€  så   t  ƒ  } d } d } t |  ƒ } x© | D]¡ } | j d ƒ d k rU | j d ƒ } n	 | g } xh | D]` } | sw qe n  | d d k r¯ | r  | | | <d } n  t | ƒ } 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   >N(	   R   R   R,   t   findR4   R7   R   t   stript   close(   R$   t   chr_lengthst   lt   chr_nameR%   t   raw_linet   linesR6   (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   get_chr_lengths_from_fastafile^   s(    		
	

c   	      C€  s©   d } i  } i  } x‡ t  |  ƒ D]y \ } } | j ƒ  d } t | ƒ } | | 7} t d „  t | ƒ Dƒ ƒ | | <| rŽ | t | | ƒ 8} n  | | | <q W| | | f S(   Ni    c         s€  s+   |  ]! \ } } | d  k r | d Vq d S(   t   Ni   N(    (   t   .0t   xt   s(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pys	   <genexpr>„   s    (   t
   read_fastaR4   R   t   sett	   enumerate(	   t   fasta_fpatht   skip_nst   genome_sizet   reference_chromosomest   ns_by_chromosomest   namet   seqR>   t   chr_len(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   get_genome_stats|   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  t |
 ƒ } | 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ÿÿÿÿR8   t   ws   	s   
(   R   R   R9   R4   t   appendR   R:   R7   t   writet   joinR   (   RI   R=   t   total_offsett
   chr_offsetR>   t	   fai_fpatht
   fai_fieldst   in_fR?   R@   R6   t   chr_linet   out_ft   fieldst   fs(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   create_fai_file‹   s<    
	.
		.c         C€  sº   t  j j | ƒ s" t  j | ƒ n  d } xx t |  ƒ D]j } | d d k r‰ | r^ | j ƒ  n  t t  j j | t | ƒ 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)s.fasta" original.fasta
    i    R8   s   .faRR   N(
   R   R   t   isdirt   mkdirR   R   R;   RU   R7   RT   (   R$   t   output_dirpatht   outFileR6   (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   split_fasta®   s    +c         c€  sç   t  } g  } d } t |  ƒ } x• | D] } | j d ƒ } xu | D]m } | sS qA n  | d d k r› | s€ | d j | ƒ f Vn  t } t | ƒ } g  } qA | j | j ƒ  ƒ qA Wq% W| sÂ | rÙ | d j | ƒ f Vn  | j ƒ  d S(   sH   
        Generator that returns FASTA entries in tuples (name, seq)
    R3   s   i    R8   N(	   R   R,   R4   RU   t   FalseR7   RS   R:   R;   (   R$   t   firstRO   RN   R%   R?   R@   R6   (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyRF   Ã   s&    	c         C€  s:   g  } x- t  |  ƒ D] \ } } | j | | f ƒ q W| S(   sB   
        Returns list of FASTA entries (in tuples: name, seq)
    (   RF   RS   (   R$   t   list_seqRN   RO   (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   read_fasta_one_timeâ   s    c         C€  s’   t  |  ƒ } g  } x` | D]X } | j d ƒ } x@ | D]8 } | sG q5 n  | d d k r5 | j | j ƒ  ƒ q5 q5 Wq W| j ƒ  d j | ƒ } | S(   s   
        Returns string
    s   i    R8   R3   (   R,   R4   RS   R:   R;   RU   (   R$   R%   Rg   R?   R@   R6   t	   fasta_str(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   read_fasta_strì   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   fastaRN   RO   t   i(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   print_fasta   s    	RR   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   RT   Rk   R   R;   (   R$   Rl   R   t   outfileRN   RO   Rm   (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/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   CRB   (   t   upper(   t   letter(    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   comp  s    c         €  s8   t  t d d ƒ ƒ ‰  d j ‡  f d †  t |  ƒ Dƒ ƒ S(   Nt
   ATCGNatcgnt
   TAGCNtagcnR3   c         3€  s!   |  ] } ˆ  j  | d  ƒ Vq d S(   R3   N(   t   get(   RC   t
   nucleotide(   t   c(    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pys	   <genexpr>  s    (   t   dictt   zipRU   t   reversed(   RO   (    (   R|   sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   rev_comp  s    (#   t
   __future__R    R   R   R   R   t   collectionsR   t   ImportErrort%   quast_libs.site_packages.ordered_dictR   t   quast_libs.site_packagesR-   R.   t
   quast_libsR   t   quast_libs.logR   t   LOGGER_DEFAULT_NAMER   R,   R   R7   RA   Re   RQ   R_   Rd   RF   Rh   Rj   Rn   Rp   Rw   R€   (    (    (    sD   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/fastaparser.pyt   <module>   s>   	,				#			
		
	