B
    b	                 @   s4   d Z ddlmZ G d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 )RecordzClass to store the information in a Cluster Buster matrix table.

    The record inherits from a list containing the individual motifs.
    c             C   s   d dd | D S )N
c             s   s   | ]}t |V  qd S )N)str).0motif r   7lib/python3.7/site-packages/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                s   d}t  }g g g g d d}x| D ]}| }|r"|dr|dkrbtjd d}||_|| |dd  }g g g g d |d7 }q"|d	rq"| }t|d
kr" fddt	ddddg|D  q"W tjd d}||_|| |S )zRead 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GATC)Zalphabetcounts   N#   c                s"   g | ]\}} |  t|qS r   )appendfloat)r   Z
nucleotideZnucleotide_count)nucleotide_countsr   r	   
<listcomp>5   s   zread.<locals>.<listcomp>r   r   r   r   )
r   strip
startswithr   ZMotifnamer   splitlenzip)ZhandleZ	motif_nbrrecordZ
motif_nameliner   Zmatrix_columnsr   )r   r	   read   s4    






r(   c             C   sx   g }xd| D ]\}d|j  d}|| x>t|jd |jd |jd |jd D ]}|dj|  qNW q
W d|}|S )	zWReturn the representation of motifs in Cluster Buster position frequency matrix format.r   r   r   r   r   r   z {:0.0f}	{:0.0f}	{:0.0f}	{:0.0f}
r   )r"   r   r%   r   formatr   )r   linesmr'   ZACGT_countstextr   r   r	   writeB   s    

(
r-   N)r   ZBior   listr   r(   r-   r   r   r   r	   <module>   s   
,