Ñò
lV.Jc           @   sé   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Td d k Td d k Z e i	 d ƒ Z
 e i	 d ƒ Z d Z d d d „  ƒ  YZ d	 d d
 „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d S(   s=   
This module contains all the classes to deal with BED files
iÿÿÿÿN(   t   *s   \+s   \-s   Error in BED classt   BED2c           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sÀ   
    Class for bed lines with 2 values: start and strand, this will be
    useful for things like TSS information or tags where the only
    important information is the start and strand.
    c         C   s   | |  _  | |  _ d  S(   N(   t   startt   strand(   t   selfR   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __init__8   s    	c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __set__<   s    	c         C   sE   t  |  i ƒ d |  i } y | SWn t i i d |  ƒ d SXd  S(   Nt    s   No coord information for %s
t    (   t   strR   R   t   syst   stderrt   write(   R   t	   outstring(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   getCoord@   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   2   s   		t   BED3c           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sA   
    Class for bed lines with 3 values: chrom, start and end
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   chromR   t   end(   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   P   s    		c         C   s   | |  _  | |  _ | |  _ d  S(   N(   R   R   R   (   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   V   s    		c         C   sV   |  i  d t |  i ƒ d t |  i ƒ } y | SWn t i i d |  ƒ d SXd  S(   NR   s   No coord information for %s
R   (   R   R	   R   R   R
   R   R   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   \   s    +(   R   R   R   R   R   R   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   L   s   		t   BED6c           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sT   
    Class for bed lines with 6 values:  chrom, start, end, name, score, strand
    c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R   R   R   t   namet   scoreR   (   R   R   R   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   n   s    					c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R   R   R   R   R   R   (   R   R   R   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   v   s    					c         C   s}   |  i  d t |  i ƒ d t |  i ƒ d |  i d t |  i ƒ d |  i } y | SWn t i i	 d |  ƒ d SXd  S(   Ns   	s   No coord information for %s
R   (
   R   R	   R   R   R   R   R   R
   R   R   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   ~   s    R(   R   R   R   R   R   R   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   i   s   		t	   BED_GRAPHc           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   sk   
    Class to deal with bed graph lines: chrom, start, end, value
    This emulates the wiggle format

    i    c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R   R   R   t   value(   R   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   ’   s    			c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R   R   R   R   (   R   R   R   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   —   s    			c         C   sV   |  i  d t |  i ƒ d t |  i ƒ } y | SWn t i i d |  ƒ d SXd  S(   NR   s    No BED coord information for %s
R   (   R   R	   R   R   R
   R   R   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   œ   s    +c         C   sg   |  i  d t |  i ƒ d t |  i ƒ d t |  i ƒ } y | SWn t i i d |  ƒ d SXd  S(   NR   s   No BED all information for %s
R   (   R   R	   R   R   R   R
   R   R   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   getAll£   s    <(   R   R   R   R   R   R   R   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   Œ   s
   		t   BEDc           B   s¡   e  Z d  Z d d d d 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 RS(   s@   
    Class to deal with bed files and do common operations 
    t   hg18R   i    c   
      C   s0  h  |  _  x" t i | D] } g  |  i  | <q W| o÷t i | d ƒ o‘t | ƒ } xÑ| D]v} t i d | ƒ p]| i ƒ  } | i ƒ  } t | ƒ d j oF t	 | d t
 | d ƒ t
 | d ƒ ƒ }	 |  i  | d i |	 ƒ n t | ƒ d j oa t | d ƒ | j oF t	 | d t
 | d ƒ t
 | d ƒ ƒ }	 |  i  | d i |	 ƒ qYn t | ƒ d j oa t | d ƒ | j oF t	 | d t
 | d ƒ t
 | d ƒ ƒ }	 |  i  | d i |	 ƒ qÍqÑq[ q[ Wq,t i | d	 ƒ ot | ƒ } x-| D]d} t i d | ƒ pK| i ƒ  } | i ƒ  } t | ƒ d j o t i i d
 ƒ t ‚ n t | ƒ d j on t | d ƒ | j oS t | d t
 | d ƒ t
 | d ƒ t | d ƒ ƒ }	 |  i  | d i |	 ƒ qÞn t | ƒ d j on t | d ƒ | j oS t | d t
 | d ƒ t
 | d ƒ t | d ƒ ƒ }	 |  i  | d i |	 ƒ q_qcqÿqÿWq,t i | d ƒ o^t | ƒ } x›| D]C} t i d | ƒ p*| i ƒ  } | i ƒ  } t | ƒ d j o t i i d ƒ t ‚ n t | ƒ d j o t i i d ƒ t ‚ n t | ƒ d j o¡ t | d ƒ | j o† t i | d ƒ o! t t
 | d ƒ | d ƒ }	 n6 t i | d ƒ o! t t
 | d ƒ | d ƒ }	 n |  i  | d i |	 ƒ qÐqÔq‘q‘Wq,t i | d ƒ o9t | ƒ } x*| D]} t i d | ƒ p| i ƒ  } | i ƒ  } t | ƒ d j o t i i d ƒ t ‚ n t | ƒ d j o t i i d ƒ t ‚ n t | ƒ d j o| t | d ƒ | j oa t | d t
 | d ƒ t
 | d ƒ | d t | d ƒ | d ƒ }	 |  i  | d i |	 ƒ qq qqWq,n d S(   sš   Overload __init__ so that if a threshold is given, only
        grab bed vals that are above threshold -- This won't do
        anything different for bed3 entries because there is no value
        information in these.
              
        Reads in a bed file and builds a dictionary with chromosomes
        as keys and lists of bed elements as values. The values are
	stored as floating numbers.
        R   t   tracki   i    i   i   i   i   R   sE   Can't make bed_graph with only                             3 elementsR   s   Need BED6 to make BE2i   R   s   Need BED6 to make BE6N(   t   bed_valst
   GenomeDatat   species_chromst   ret   matcht   opent   stript   splitt   lenR   t   atoit   appendt   atofR
   R   R   t   bedErrorR   t   plusR   t   minusR   (
   R   t   speciest   filet   bed_typet   val_thresholdt   ct   infilet   linet   slinet   bed(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   ´   s¨    	  * , 	
$ $, 

!!, 

$!c         C   s   |  i  i ƒ  S(   sV   
        Return a list of the keys - duplicating the function of a dictionary
        (   R   t   keys(   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR6   #  s    c         C   s—   g  } xc |  i  | D]T } t i | i ƒ o | i | i ƒ q t i | i ƒ o | i | i ƒ q q Wy | SWn t i	 i
 d |  ƒ d SXd S(   s?   
        Return a list of starts on a given chromosome
        s#   Having trouble returning starts %s
R   N(   R   R+   R"   R   R(   R   R,   R   R
   R   R   (   R   R   t   startst   t(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   getStarts_consider_strands0  s     c         C   sY   g  } x% |  i  | D] } | i | i ƒ q Wy | SWn t i i d |  ƒ d SXd S(   s?   
        Return a list of starts on a given chromosome
        s#   Having trouble returning starts %s
R   N(   R   R(   R   R
   R   R   (   R   R   R7   R8   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt	   getStarts@  s     c         C   sY   g  } x% |  i  | D] } | i | i ƒ q Wy | SWn t i i d |  ƒ d SXd S(   s?   
        Return a list of starts on a given chromosome
        s!   Having trouble returning ends %s
R   N(   R   R(   R   R
   R   R   (   R   R   t   endsR8   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   getEndsN  s     c         C   s/   y t  t SWn t i i d |  ƒ d SXd S(   sN   
        Return a list of all the chromosomes in order (ordered keys)
        s   Can't return chromosome list
R   N(   t   chromsR-   R
   R   R   (   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt	   getChroms[  s
    c         C   s;   d } x. |  i  i ƒ  D] } | t |  i  | ƒ 7} q W| S(   s?   
        Return the number of bed vals in BED instance
        i    (   R   R6   R&   (   R   t   numR1   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt
   getNumValsf  s
     c         C   s\   |  i  i | ƒ o8 t |  i  | ƒ d j o t i i d | ƒ t ‚ n | |  i  | <d  S(   Ni    s   chromsome %s already populated
(   R   t   has_keyR&   R
   R   R   R*   (   R   R   t   bed_list(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   addChromp  s    -
c         C   s   |  i  i ƒ  d S(   s!   
        Delete, delete;
        N(   R   t   clear(   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __del__w  s    c         C   s   |  i  i | ƒ S(   s+   
        Returns  mapping iterator
        (   R   RA   (   R   t   item(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __contains__~  s    c         C   s   |  i  i ƒ  S(   s*   
        Returns mapping iterator
        (   R   t   iterkeys(   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __iter__„  s    c         C   s   t  |  i ƒ S(   s,   
        Returns number of bed_vals
        (   R&   R   (   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __len__Š  s    c         C   s+   |  i  i | ƒ o |  i  | =n t ‚ d S(   sŽ   
        removes a chrom if its name exists in the dictionary
        -- I guess this could possible be useful at some point
        
        N(   R   RA   R*   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __delitem__  s    c         C   s   | |  i  | <d S(   s&   
        Sets a new bed value
        N(   R   (   R   R   t   bedlist(    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __setitem__š  s    c         C   s)   |  i  i | ƒ o |  i  | St ‚ d S(   sY   
        Returns a bed_val indexed by its name or None if no such bed_val exists
        N(   R   RA   R*   (   R   R   (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   __getitem__   s    N(   R   R   R   t   NoneR   R6   R9   R:   R<   R>   R@   RC   RE   RG   RI   RJ   RK   RM   RN   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyR   ¯   s    o						
						
	(    (    (    (    (    (   R   R!   t   ost   shutilt   timeR
   t   matht   stringR   t   compileR+   R,   R*   R   R   R   R   R   (    (    (    s?   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/BED.pyt   <module>   s   <

##