
    Ri                     8    d Z ddlmZ  G d de      Zd ZddZy)	z5Parse Cluster Buster position frequency matrix files.    )motifsc                       e Zd ZdZd Zy)RecordzClass to store the information in a Cluster Buster matrix table.

    The record inherits from a list containing the individual motifs.
    c                 2    dj                  d | D              S )zBReturn a string representation of the motifs in the Record object.
c              3   2   K   | ]  }t        |        y w)N)str).0motifs     P/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/clusterbuster.py	<genexpr>z!Record.__str__.<locals>.<genexpr>   s     6U6s   )join)selfs    r   __str__zRecord.__str__   s    yy6666    N)__name__
__module____qualname____doc__r    r   r   r   r      s    
7r   r   c           
      x   d}t               }g g g g d}d}d}d}| D ]W  }|j                         }|s|j                  d      rf|dk7  r=t        j                  d|      }||_        ||_        ||_        |j                  |       |dd j                         }g g g g d}d}d}|dz  }|j                  d	      rt        |j                         d
         }|j                  d      rt        |j                         d
         }|j                  d      r|j                         }	t        |	      dk(  st        g d|	      D 
cg c]"  \  }
}||
   j                  t        |            $ c}}
 Z t        j                  d|      }||_        ||_        ||_        |j                  |       |S c c}}
w )zRead motifs in Cluster Buster position frequency matrix format from a file handle.

    Cluster Buster motif format: https://bu.wenglab.org/cluster-buster/help/cis-format.html
    r   )ACGT N>GATC)alphabetcounts   z# GAP   z# WEIGHT#   )r   strip
startswithr   Motifnamegapweightappendfloatsplitlenzip)handle	motif_nbrrecordnucleotide_counts
motif_name	motif_gapmotif_weightliner   matrix_columns
nucleotidenucleotide_counts               r   readr;      s   
 IXF r<JIL "zz|s#>"LL&ARSE!+EJ )EI#/ELMM%(!!"X^^-
*,2BR$H! 	#Q	??7+ %djjl1o 6I__Z0#(a#9L__S)!%~&!+ =@0.=8J(8 **5<<UCS=TU;"H LL&1BCEEJEIEL
MM%Ms   	'F6c                 \   g }| D ]  }|j                  d|j                   d       	 |j                  }|r|j                  d| d       	 	 |j                  }|r|j                  d| d       	 t        |j                  d   |j                  d   |j                  d   |j                  d         D ]E  }|j                   d	j                  d
t        t        |            dz         j                  |        G  dj                  |      }|S # t        $ r Y w xY w# t        $ r Y w xY w)aZ  Return the representation of motifs in Cluster Buster position frequency matrix format.

    By default (`precision=0`) Cluster Buster position frequency matrices will be written
    with integer values.
    If a higher precision value is set, Cluster Buster position frequency matrices will be
    written as floats with `x` decimal places.
    r   r   z
# WEIGHT: z# GAP: r   r   r   r   z {:0.0f}	{:0.0f}	{:0.0f}	{:0.0f}
0ffr   )r+   r(   r*   AttributeErrorr)   r/   r    replacer	   intformatr   )r   	precisionlinesmr*   r)   ACGT_countstexts           r   writerH   P   s8    E q^$	6XXF z&45	0%%C wse2./HHSM188C=!((3-#
 	K LL6>>#c)n-3&+'	!4 775>DK1  		  		s#   DD	DD	D+*D+N)r   )r   Bior   listr   r;   rH   r   r   r   <module>rK      s$    < 7T 76r%r   