
    Xf                         d Z ddlmZ d Z G d de          Zd Zd Zd Zd	 Z	d
 Z
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                     d}t                      }t          ||            t          ||            t          ||            	 | D ]}|                    d          r n|S |                                d         }|dz  }t          |           \  }}}t          | |          }t          j	        |j
        |          }	|j        |	_        ||	_        ||	_        ||	_        ||	_        |                    |	           t#          |          |k    sJ )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   motifs
             2lib/python3.11/site-packages/Bio/motifs/minimal.pyreadr      s%   > LXXF&&!!!66"""VV$$$+ 	 	Dw''  Mzz||A*@*H*H'6?33fofEEE!, /
e6{{l****%+    c                       e Zd ZdZd Zd ZdS )r	   z4Class for holding the results of a minimal MEME run.c                 Z    d| _         d| _        d| _        d| _        i | _        g | _        dS )zInitialize record class values. N)versiondatafilecommandr   r   	sequences)selfs    r   __init__zRecord.__init__K   s0    r    c                     t          |t                    r| D ]}|j        |k    r|c S dS t                              | |          S )zReturn the motif of index key.N)
isinstancestrr   list__getitem__)r(   keyr   s      r   r.   zRecord.__getitem__T   s`    c3 	/ ! !:$$ LLL %! ! ##D#...r    N)__name__
__module____qualname____doc__r)   r.    r    r   r	   r	   H   s8        >>  / / / / /r    r	   c                    |D ]}|                     d          r nt          d          	 t          |          }n# t          $ r t          d          w xY 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   lsr9   r:   r;   r<   s           r   r   r   a   s     
 
??:;; 	E	 ^
 
 	

F|| 
 
 
^
 
 	

 ::<<D	Br!uuRU||U2a5\\5A<<GJAq!Qaaa88Fs	   = Ac                     |D ]}|                     d          r n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   r=   r@   r   r$   )r   r   r   rB   s       r   r
   r
   v   sq     
 
??>** 	E	 T
 
 	
 ::<<D	BUFNNNr    c                    |D ]}|                     d          r nt          d          |                     d          st          d|z            |                                                    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   r=   r@   replacer   )r   r   r   als       r   r   r      s     
 
??:&& 	E	 V
 
 	
 ??<(( LCdJKKK::<<b11Dv~~#FOOOr    c           	         g g g g g}| D ]$}|                                 }t          |          dk    r n|d                             t          t	          |d                   |z                       |d                             t          t	          |d                   |z                       |d                             t          t	          |d                   |z                       |d                             t          t	          |d                   |z                       &i }|d         |d<   |d         |d<   |d         |d<   |d         |d	<   |S )
z)Read letter probability matrix (PRIVATE).   r   r   rD   r6   r9   r:   r;   r<   )r   r   r   roundrA   )r   r   r   r   freqscs         r   r   r      s?   "b"F C C

u::??Eq	uU1X@AABBBq	uU1X@AABBBq	uU1X@AABBBq	uU1X@AABBBB
AAYAcFAYAcFAYAcFAYAcFHr    c                    | D ]}|                     d          r n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   rA   )r   r   r   r   r   s        r   r   r      s       ??788 	E	$**Y//288::1=>>OD!!!$**,,Q/00F4::d##A&,,..q122F?F**r    c                     | D ]}d|v r n	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   rD   )r=   r@   r   join)r   r   wordsr   s       r   _read_motif_namerV      sq     P P'4//E 0 NOOO::<<DJJLLE88E!A#JDKr    __main__)run_doctestN)r3   Bior   r   r-   r	   r   r
   r   r   r   rV   r0   
Bio._utilsrX   r4   r    r   <module>r[      s    ; :      8 8 8v/ / / / /T / / /29 9 9*    &  &
+ 
+ 
+
 
 
 z&&&&&&KMMMMM r    