ó
ÀênQc           @   s’   d  Z  d d l Z d d l Z d d l m Z d d l m Z d e f d „  ƒ  YZ d „  Z	 d „  Z
 d	 e f d
 „  ƒ  YZ d „  Z d „  Z d S(   sY    Represent a read alignment in SAM  textual format

Created on Sep 25, 2010
@author: ian
iÿÿÿÿN(   t   SeqIO(   t   defaultdictt   SAMTextc           B   sÚ   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z RS(   c         C   s   t  | ƒ |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _	 d |  _
 d |  _ g  |  _ d |  _ d  S(   Ni    t   *iÿ   (   t   strt   qnamet   flagt   rnamet   post   mapqt   cigart   mrnmt   mpost   isizet   seqt   qualt   tagst   alen(   t   selft   Qname(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __init__   s    											c         C   sƒ   |  j  t |  j ƒ |  j t |  j ƒ t |  j ƒ |  j |  j t |  j ƒ t |  j	 ƒ |  j
 |  j g } | j |  j ƒ d j | ƒ S(   Ns   	(   R   R   R   R   R   R	   R
   R   R   R   R   R   t   extendR   t   join(   R   t   str_list(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __str__   s    fc         C   sw   t  |  j ƒ |  j t  |  j ƒ t  |  j ƒ t  |  j ƒ t  |  j ƒ |  j |  j t  |  j	 ƒ t  |  j
 ƒ t j @S(   N(   t   hashR   R   R   R   R
   R   R   R   R   R   t   syst   maxint(   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __hash__#   s    c         C   sÅ   y¶ |  j  | j  k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j | j k o´ |  j	 | j	 k SWn t
 SXd  S(   N(   R   R   R   R   R
   R   R   R   R   R   t   False(   R   t   other(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __eq__&   s    ¶c         C   s   |  j  d O_  d  S(   Ni   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_paired_flag,   s    c         C   s   |  j  d O_  d  S(   Ni   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_proper_pair_flag/   s    c         C   s   |  j  d O_  d  S(   Ni   i   i   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_reverse_strand_flag2   s    c         C   s   |  j  d M_  d  S(   Ni   i   i   iïÿÿÿ(   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_forward_strand_flag5   s    c         C   s   |  j  d O_  d  S(   Ni   i   i    (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_mate_reverse_strand_flag8   s    c         C   s   |  j  d M_  d  S(   Ni   i   i    ißÿÿÿ(   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_mate_forward_strand_flag;   s    c         C   s   |  j  d O_  d  S(   Ni   i   i@   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_first_of_pair_flag>   s    c         C   s   |  j  d O_  d  S(   Ni   i   i€   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   set_second_of_pair_flagA   s    c         C   s   |  j  d @S(   Ni   i   i   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   is_reversedD   s    c         C   s   |  j  d @S(   Ni   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt	   is_pairedG   s    c         C   s   |  j  d @S(   Ni   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   is_properly_pairedJ   s    c         C   s   |  j  d @S(   Ni   i   i    (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   is_mate_reversedM   s    c         C   s   |  j  d @S(   Ni   i   i@   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   is_first_of_pairP   s    c         C   s   |  j  d @S(   Ni   i   i€   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   is_second_of_pairS   s    c         C   s   |  j  d @d AS(   Ni   (   R   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt	   is_mappedV   s    c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __len__Y   s    c         C   s"   |  j  ƒ  j d ƒ } t j | ƒ S(   Ns   	(   t   stript   splitR   t
   fromFields(   t   linet   fields(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt
   fromString\   s    c         C   sÓ   t  |  d ƒ } t |  d ƒ | _ |  d | _ t |  d ƒ | _ t |  d ƒ | _ |  d | _ |  d | _ t |  d ƒ | _ t |  d	 ƒ | _	 |  d
 | _
 |  d | _ |  d | _ t | j ƒ | _ | S(   Ni    i   i   i   i   i   i   i   i   i	   i
   i   (   R   t   intR   R   R   R	   R
   R   R   R   R   R   R   t   get_aligned_lengthR   (   R5   t   result(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyR3   a   s    (   t   __name__t
   __module__R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R+   R,   R-   R.   R0   t   staticmethodR6   R3   (    (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyR      s,   																				c         C   s•   t  t ƒ } t j d |  j ƒ  ƒ d  } xB t d t | ƒ d ƒ D]( } | | | d c t | | ƒ 7<qA W| d | d | d | d	 | d
 S(   Ns   ([DIMNSHP=X])iÿÿÿÿi    i   i   t   Mt   It   St   =t   X(   R   R7   t   reR2   R1   t   rangeR/   (   R
   t   lengthst   piecest   i(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   get_cigar_lengthr   s
    &c         C   s   t  t ƒ } t j d |  j ƒ  ƒ d  } xB t d t | ƒ d ƒ D]( } | | | d c t | | ƒ 7<qA W| d | d | d | d	 | d
 | d S(   Ns   ([DIMNSHP=X])iÿÿÿÿi    i   i   R=   R>   R?   R@   RA   t   N(   R   R7   RB   R2   R1   RC   R/   (   R
   RD   RE   RF   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyR8   y   s
    &t   SAMTextFileIteratorc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s™   | d k r t  j |  _ n t | d ƒ |  _ g  |  _ |  j j ƒ  |  _ xJ |  j r” |  j j d ƒ r” |  j j |  j j	 ƒ  ƒ |  j j ƒ  |  _ qK Wd  S(   Nt   -t   rt   @(
   R   t   stdint   inputt   opent   _headert   readlinet	   next_linet
   startswitht   appendR1   (   R   t   filename(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyR      s    	c         C   s   d j  |  j ƒ S(   Ns   
(   R   RP   (   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   headerŒ   s    c         c   sF   x? t  rA |  j r8 t j |  j ƒ V|  j j ƒ  |  _ q t ‚ q Wd  S(   N(   t   TrueRR   R   R6   RN   RQ   t   StopIteration(   R   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   __iter__   s
    		(   R:   R;   R   RV   RY   (    (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyRI   €   s   		c         C   s}   g  } t  |  ƒ } xQ | D]I } | j ƒ  j ƒ  } | d } | d } d | | f } | j | ƒ q W| j ƒ  d j | ƒ S(   Ni    i   s   @SQ	SN:%s	LN:%ss   
(   RO   R1   R2   RT   t   closeR   (   t   fai_filenamet   header_linest   faiR4   R5   t   ref_namet
   ref_lengtht   hdr_line(    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   generate_SAM_header_from_fai™   s    


c         C   so   g  } t  j |  d ƒ } xG t | j ƒ  ƒ D]3 } t | | ƒ } d | | f } | j | ƒ q+ Wd j | ƒ S(   Nt   fastas   @SQ	SN:%s	LN:%ss   
(   R    t   indext   sortedt   keysR/   RT   R   (   t   fasta_filenameR\   t   genome_indexR^   R_   R`   (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   generate_SAM_header_from_fasta¥   s    (   t   __doc__R   RB   t   BioR    t   collectionsR   t   objectR   RG   R8   RI   Ra   Rh   (    (    (    sQ   /woldlab/castor/data00/home/georgi/programs/RNA-Seq-Simulator.v1.0/lib/samText.pyt   <module>	   s   d			