B
    ‰°b  ã               @   s0   d Z dZdZG dd„ deƒZdd„ Zdd„ Zd	S )
a,  A class to handle frequency tables or letter count files.

Example files for a DNA alphabet:

A count file (whitespace separated)::

 A  50
 C  37
 G  23
 T  58

The same info as a frequency file::

 A 0.2976
 C 0.2202
 G 0.1369
 T 0.3452

Functions:
  :read_count(f): read a count file from stream f. Then convert to
                  frequencies.
  :read_freq(f): read a frequency data file from stream f. Of course, we then
                 don't have the counts, but it is usually the letter frequencies
                 which are interesting.

Methods:
  (all internal)

Attributes:
  :alphabet: The letters you are using as indices into the table.
  :data: Frequency dictionary.
  :count: Count dictionary. Empty if no counts are provided.

Example of use:
    >>> import io
    >>> from Bio.SubsMat import FreqTable
    >>> f_count = io.StringIO(u"A  50\nC  37\nG  23\nT  58")
    >>> ftab = FreqTable.read_count(f_count)
    >>> for nb in sorted(ftab):
    ...     print("%s %0.4f" %(nb, ftab[nb]))
    ...
    A 0.2976
    C 0.2202
    G 0.1369
    T 0.3452

é   é   c               @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
Ú	FreqTablez>Define class to handle frequency tables or letter count files.c             C   s:   t t| j ¡ ƒƒ}x"| j ¡ D ]\}}|| | |< qW dS )z0Calculate frequency from count values (PRIVATE).N)ÚfloatÚsumÚcountÚvaluesÚitems)ÚselfZtotalÚiÚv© r   ú4lib/python3.7/site-packages/Bio/SubsMat/FreqTable.pyÚ_freq_from_count@   s    zFreqTable._freq_from_countc             C   s"   d}xt | ƒD ]}||7 }qW |S )zOrder the alphabet (PRIVATE).Ú )Úsorted)r	   Úsr
   r   r   r   Ú_alphabet_from_inputF   s    zFreqTable._alphabet_from_inputNc             C   sR   || _ |tkr|| _|  ¡  n"|tkr8i | _|  |¡ ntdƒ‚|sN|  ¡ | _ dS )zInitialize the class.zbad dict_typeN)ÚalphabetÚCOUNTr   r   ÚFREQÚupdateÚ
ValueErrorr   )r	   Zin_dictZ	dict_typer   r   r   r   Ú__init__M   s    
zFreqTable.__init__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   =   s   r   c             C   s8   i }x(| D ] }|  ¡  ¡ \}}t|ƒ||< q
W t|tƒS )z;Read a count file f and load values to the Frequency Table.)ÚstripÚsplitÚintr   r   )Úfr   ÚlineÚkeyÚvaluer   r   r   Ú
read_count\   s
    
r$   c             C   s8   i }x(| D ] }|  ¡  ¡ \}}t|ƒ||< q
W t|tƒS )zDRead a frequency data file f and load values to the Frequency Table.)r   r   r   r   r   )r    Z	freq_dictr!   r"   r#   r   r   r   Ú	read_freqe   s
    
r%   N)r   r   r   Údictr   r$   r%   r   r   r   r   Ú<module>6   s
   	