
:>"^p	  ã               @   sH   d  Z  d d l m Z Gd d „  d e ƒ Z d d „  Z d d „  Z d	 S)
z5Parse Cluster Buster position frequency matrix files.é    )Úmotifsc               @   s"   e  Z d  Z d Z d d „  Z d S)ÚRecordzŒClass to store the information in a Cluster Buster matrix table.

    The record inherits from a list containing the individual motifs.
    c             C   s   d j  d d „  |  Dƒ ƒ S)NÚ
c             s   s   |  ] } t  | ƒ Vq d  S)N)Ústr)Ú.0Úmotif© r   ú=/tmp/pip-build-ww9dw3qa/biopython/Bio/motifs/clusterbuster.pyú	<genexpr>   s    z!Record.__str__.<locals>.<genexpr>)Újoin)Úselfr   r   r	   Ú__str__   s    zRecord.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r	   r      s   r   c          	      sn  d } t  ƒ  } d g  d g  d g  d g  i ‰  d } x|  D]þ } | j ƒ  } | r: | j d ƒ rÜ | d k r› t j d d	 d
 ˆ  ƒ } | | _ | j | ƒ | d d … j ƒ  } d g  d g  d g  d g  i ‰  | d 7} q: | j d ƒ rî q: | j ƒ  } t | ƒ d k r: ‡  f d d †  t	 d d d d g | ƒ Dƒ q: Wt j d d	 d
 ˆ  ƒ } | | _ | j | ƒ | S)z°Read motifs in Cluster Buster position frequency matrix format from a file handle.

    Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html
    r   ÚAÚCÚGÚTÚ ú>ZalphabetZGATCÚcountsé   Nú#é   c                s/   g  |  ]% \ } } ˆ  | j  t | ƒ ƒ ‘ q Sr   )ÚappendÚfloat)r   Z
nucleotideZnucleotide_count)Únucleotide_countsr   r	   ú
<listcomp>4   s   	zread.<locals>.<listcomp>)
r   ÚstripÚ
startswithr   ZMotifÚnamer   ÚsplitÚlenÚzip)ÚhandleZ	motif_nbrÚrecordZ
motif_nameÚliner   Zmatrix_columnsr   )r   r	   Úread   s2    		!	r)   c             C   s›   g  } x |  D]w } d j  | j ƒ } | j | ƒ xO t | j d | j d | j d | j d ƒ D] } | j d j  | Œ  ƒ qd Wq Wd j | ƒ } | S)zWReturn the representation of motifs in Cluster Buster position frequency matrix format.z>{0}
r   r   r   r   z${0:0.0f}	{1:0.0f}	{2:0.0f}	{3:0.0f}
r   )Úformatr"   r   r%   r   r   )r   ÚlinesÚmr(   ZACGT_countsÚtextr   r   r	   Úwrite>   s    8r.   N)r   ZBior   Úlistr   r)   r.   r   r   r   r	   Ú<module>   s   
(