B
    ‰°b‡  ã               @   s\   d Z ddlmZ dd„ ZG dd„ deƒZdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )z4Module for the support of MEME minimal motif format.é    )Úmotifsc       
      C   sÆ   d}t ƒ }t|| ƒ t|| ƒ t|| ƒ x˜x| D ]}| d¡r0P q0W |S | ¡ d }|d7 }t| ƒ\}}}t| |ƒ}tj	|j
|d}	|j|	_||	_||	_||	_||	_| |	¡ t|ƒ|ks*t‚q*W |S )a  Parse the text output of the MEME program into a meme.Record object.

    Examples
    --------
    >>> from Bio.motifs import minimal
    >>> with open("motifs/meme.out") as f:
    ...     record = minimal.read(f)
    ...
    >>> for motif in record:
    ...     print(motif.name, motif.evalue)
    ...
    1 1.1e-22

    You can access individual motifs in the record by their index or find a motif
    by its name:

    >>> from Bio import motifs
    >>> with open("motifs/minimal_test.meme") as f:
    ...     record = motifs.parse(f, 'minimal')
    ...
    >>> motif = record[0]
    >>> print(motif.name)
    KRP
    >>> motif = record['IFXA']
    >>> print(motif.name)
    IFXA

    This function wont retrieve instances, as there are none in minimal meme format.

    r   ZMOTIFé   )ÚalphabetÚcounts)ÚRecordÚ_read_versionÚ_read_alphabetÚ_read_backgroundÚ
startswithÚsplitÚ_read_motif_statisticsÚ	_read_lpmr   ZMotifr   Ú
backgroundÚlengthÚnum_occurrencesÚevalueÚnameÚappendÚlenÚAssertionError)
ÚhandleZmotif_numberÚrecordÚliner   r   r   r   r   Úmotif© r   ú1lib/python3.7/site-packages/Bio/motifs/minimal.pyÚread   s.    






r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z4Class for holding the results of a minimal MEME run.c             C   s(   d| _ d| _d| _d| _i | _g | _dS )zInitialize record class values.Ú N)ÚversionZdatafileZcommandr   r   Ú	sequences)Úselfr   r   r   Ú__init__K   s    zRecord.__init__c             C   s8   t |tƒr(x(| D ]}|j|kr|S qW nt | |¡S dS )zReturn the motif of index key.N)Ú
isinstanceÚstrr   ÚlistÚ__getitem__)r    Úkeyr   r   r   r   r%   T   s
    



zRecord.__getitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r%   r   r   r   r   r   H   s   	r   c             C   s¤   x |D ]}|  d¡rP qW tdƒ‚yt|ƒ}W n tk
rJ   tdƒ‚Y nX | ¡ }| ¡ }t|d ƒt|d ƒt|d ƒt|d ƒf\}}}}||||dœ| _d	S )
z-Read background letter frequencies (PRIVATE).zBackground letter frequencieszPImproper input file. File should contain a line starting background frequencies.zPUnexpected end of stream: Expected to find line starting background frequencies.r   é   é   é   )ÚAÚCÚGÚTN)r
   Ú
ValueErrorÚnextÚStopIterationÚstripr   Úfloatr   )r   r   r   Úlsr.   r/   r0   r1   r   r   r   r	   a   s    

4r	   c             C   s@   x |D ]}|  d¡rP qW tdƒ‚| ¡ }| ¡ }|d | _dS )zRead MEME version (PRIVATE).zMEME versionzFImproper input file. File should contain a line starting MEME version.é   N)r
   r2   r5   r   r   )r   r   r   r7   r   r   r   r   v   s    

r   c             C   sd   x |D ]}|  d¡rP qW tdƒ‚|  d¡s8td| ƒ‚| ¡  dd¡}|dkrVd}nd}|| _dS )	zRead alphabet (PRIVATE).ZALPHABETzHUnexpected end of stream: Expected to find line starting with 'ALPHABET'z
ALPHABET= z'Line does not start with 'ALPHABET':
%sr   ZACGTZACDEFGHIKLMNPQRSTVWYN)r
   r2   r5   Úreplacer   )r   r   r   Zalr   r   r   r   „   s    


r   c             C   sà   g g g g g}xš| D ]’}|  ¡ }t|ƒdkr,P |d  tt|d ƒ| ƒ¡ |d  tt|d ƒ| ƒ¡ |d  tt|d ƒ| ƒ¡ |d  tt|d ƒ| ƒ¡ qW i }|d |d< |d |d< |d |d< |d |d	< |S )
z)Read letter probability matrix (PRIVATE).é   r   r   r8   r+   r.   r/   r0   r1   )r   r   r   Úroundr6   )r   r   r   r   ZfreqsÚcr   r   r   r   —   s    
"r   c             C   sr   x| D ]}|  d¡rP qW t| d¡d  ¡ d ƒ}t| d¡d  ¡ d ƒ}t| d¡d  ¡ d ƒ}|||fS )z Read motif statistics (PRIVATE).zletter-probability matrix:znsites=r   r   zw=zE=)r
   Úintr   r6   )r   r   r   r   r   r   r   r   r   ª   s    

r   c             C   sF   x| D ]}d|krP qW t dƒ‚| ¡ }| ¡ }d |dd… ¡}|S )zRead motif name (PRIVATE).zsorted by position p-valuez3Unexpected end of stream: Failed to find motif nameú r   r8   )r2   r5   r   Újoin)r   r   Zwordsr   r   r   r   Ú_read_motif_name·   s    
r@   N)r*   ZBior   r   r$   r   r	   r   r   r   r   r@   r   r   r   r   Ú<module>   s   ;