ó
N{=Oc           @   s+   d  d l  Z  d   Z d   Z d   Z d S(   i’’’’Nc         C   sś  g  } g  } d } d } xI t  t |   D]5 }	 |  t  | |	 | |	 | |	  k r+ |	 } Pq+ q+ W| d k r d } | | | f S| d k r0x | d k r,t | | | | | |   }
 | |  g 7} | |
 g 7} | d } | |
 } | t |  k r| d k rd } n  | | | f S| | }  q Wn  | d k rķx® | d k rét | |  | | d  }
 | j d |  |
 d  | j d |
  | d } | |
 } | d k  rŠ| d k rĆd } n  | | | f S| | | | d }  q?Wn  | | | f S(   s“  
  This function returns the corresponding segstart and seg length (as in the list)
  parameters:
  pos: the position to be queried
  seglen: the length of the segment
  direction: 1 (forward), -1 (backward)
  exonstart,exonlen: the exon positions
  Return value: (segstart,seglength,status)
  status is used to indicate the possible errors
  0: normal
  1: the position is not in the exon range
  2: the segment exceeds the boundary
  i’’’’i    i   i   (   t   ranget   lent   mint   insert(   t   post   seglent	   directiont	   exonstartt   exonlent   segstartt	   seglengtht	   posexonidt   statust   net   lentoadd(    (    sB   /home/wli/Dropbox/work/NGSTools/rnaseqreadsimulator/src/getSegs.pyt   getSegs   sH    %

	

	c         C   s   t  j | |   } | t |  k rE t |  d } | d d }  n  | d k rn | | |  | | d } n | | |  } | S(   s   
  Convertion from coordinates in a transcript to coordinates in a reference.
  Need to provide exon start position and the cumulate exon length as input.
  i   i’’’’i    (   t   bisectt   bisect_rightR   (   t   tpost   cumlenR   t   selsegR   (    (    sB   /home/wli/Dropbox/work/NGSTools/rnaseqreadsimulator/src/getSegs.pyt   tpos2pos9   s    c         C   sP  | d | d | d f } g  | D] } | | d ^ q# } | d k rR d } n | d k rg d } n  |  j  | d t | d  d t | d  d | d d | d t | d  d t | d  d d t t |   d d j g  | D] } t |  d	 ^ qõ  d d j g  | D] } t |  d	 ^ q# d d
  d S(   s   
  Write one line in .bed file.
  Need to provide information of chromosome, id, direction, segment starts and segment lengths
  i    i’’’’i   t   +t   -s   	s   0	t    t   ,s   
N(   t   writet   strR   t   join(   t   fidt   lineidt
   chromosomeR   t
   startranget   lenranget   bedranget   i(    (    sB   /home/wli/Dropbox/work/NGSTools/rnaseqreadsimulator/src/getSegs.pyt   writeBedlineI   s    !		(   R   R   R   R$   (    (    (    sB   /home/wli/Dropbox/work/NGSTools/rnaseqreadsimulator/src/getSegs.pyt   <module>   s   	4	