o
    Rŀg                     @   sz   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edkr;ddlmZ e  dS dS )z4Module for the support of MEME minimal motif format.    )motifsc           
      C   s   d}t  }t||  t||  t||  	 | D ]	}|dr  nq|S | d }|d7 }t| \}}}t| |}tj	|j
|d}	|j|	_||	_||	_||	_||	_||	 t||ks^J q)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 won't retrieve instances, as there are none in minimal meme format.

    r   TMOTIF   )alphabetcounts)Record_read_version_read_alphabet_read_background
startswithsplit_read_motif_statistics	_read_lpmr   Motifr   
backgroundlengthnum_occurrencesevaluenameappendlen)
handlemotif_numberrecordliner   r   r   r   r   motif r   F/var/www/html/myenv/lib/python3.10/site-packages/Bio/motifs/minimal.pyread   s0   





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)versiondatafilecommandr   r   	sequences)selfr   r   r   __init__K   s   
zRecord.__init__c                 C   s6   t |tr| D ]}|j|kr|  S qdS t| |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   |D ]	}| dr nqtdzt|}W n ty!   tdw | }| }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   lsr2   r3   r4   r5   r   r   r   r
   a   s$   
4r
   c                 C   s>   |D ]	}| dr nq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   r6   r9   r   r    )r   r   r   r;   r   r   r   r   v   s   
r   c                 C   sb   |D ]	}| dr nqtd| dstd| | dd}|dkr*d}nd}|| _dS )	zRead alphabet (PRIVATE).ALPHABETzHUnexpected end of stream: Expected to find line starting with 'ALPHABET'z
ALPHABET= z'Line does not start with 'ALPHABET':
%sr   ACGTACDEFGHIKLMNPQRSTVWYN)r   r6   r9   replacer   )r   r   r   alr   r   r   r	      s   


r	   c                 C   s   g g g g g}| D ]J}|  }t|dkr n=|d tt|d |  |d tt|d |  |d tt|d |  |d tt|d |  qi }|d |d< |d |d< |d |d< |d |d	< |S )
z)Read letter probability matrix (PRIVATE).   r   r   r<   r/   r2   r3   r4   r5   )r   r   r   roundr:   )r   r   r   r   freqscr   r   r   r      s    r   c                 C   sp   | D ]	}| dr nq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   r:   )r   r   r   r   r   r   r   r   r      s   

r   c                 C   sD   | D ]}d|v r
 nq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   r<   )r6   r9   r   join)r   r   wordsr   r   r   r   _read_motif_name   s   rJ   __main__)run_doctestN)r.   Bior   r   r(   r   r
   r   r	   r   r   rJ   r+   
Bio._utilsrL   r   r   r   r   <module>   s   ;
