Ńň
Zs*Jc           @   sî   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 l Z d  d k Z d  d k	 Z	 d  d k
 Z
 d  d k Z d  d k Z d Z d Z e  i d  Z e  i d  Z e i   Z d   Z d	   Z d
   Z d   Z d S(   i˙˙˙˙N(   t   *(   t   OptionParsers	   /bin/greps   /bin/cats   \+s   \-c         C   s6   y t  |   } Wn t j
 o d } n Xd } | S(   Ni    i   (   t   opent   IOError(   t   ft   filet   exists(    (    sC   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/Utility.pyt
   fileExists   s    c         C   sW   d } xJ t  d t |   d  D]/ } |  | i |  | d i j o
 d } q  q  W| S(   s   
        Check if sorted in ascending order.
        input is a list of BED with chrom, start, end and value.
        output: sorted =1 or 0
        i   i    (   t   ranget   lent   start(   t   listt   sortedt   index(    (    sC   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/Utility.pyt	   is_sorted   s     c   	      C   sË   t  |  d  } t  | d  } x | D] } t i d |  pq | i   } | i   } t |  d j oB t | |  | } t |  | | <| i d i	 |  d  qŻ q% q% W| i
   | i
   d S(   s=   
	c is the 0-based column number 
	Return a list of names
	
	t   rt   wt   #i    s   	s   
N(   R   t   ret   matcht   stript   splitR	   t   atoft   strt   writet   joint   close(	   t
   input_filet   ct   rescale_factort   output_filet   infilet   outfilet   linet   slinet	   new_value(    (    sC   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/Utility.pyt   rescale_a_column*   s     &
c   
      C   sü   d } t  |  d  } t  | d  } x˝ | D]ľ } t i d |  p | i   } | i   } t |  d j om | d j o t | |  } n t | |  | }	 t |	  | | <| i d i	 |  d  | d 7} qŕ q+ q+ W| i
   | i
   d S(	   s=   
	c is the 0-based column number 
	Return a list of names
	
	i    R   R   R   s   	s   
i   N(   R   R   R   R   R   R	   R   R   R   R   R   (
   R   R   R   t   line_numberR   R    R!   R"   R   R#   (    (    sC   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/Utility.pyt   normalize_a_column=   s"     
(   R   t   ost   syst   shutilt   matht   stringt   optparseR   t   operatort   bisectt   BEDt   UCSCt
   GenomeDatat   grept   catt   compilet   plust   minust   getcwdt   DirR   R   R$   R&   (    (    (    sC   /woldlab/castor/data00/home/georgi/SICER_v1.01/SICER/lib/Utility.pyt   <module>   s"   0

				