
    Ri                     z    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        yy)z4Module for the support of MEME minimal motif format.    )motifsc                    d}t               }t        ||        t        ||        t        ||        	 | D ]  }|j	                  d      s n |S |j                         d   }|dz  }t        |       \  }}}t        || ||      }t        j                  |j                  |      }	|j                  |	_        |	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   MOTIF   )alphabetcounts)Record_read_version_read_alphabet_read_background
startswithsplit_read_motif_statistics	_read_lpmr   Motifr   
backgroundr   lengthnum_occurrencesevaluenameappendlen)
handlemotif_numberrecordliner   r   r   r   r   motifs
             J/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/minimal.pyreadr      s    > LXF&&!66"VV$
 	Dw'	 Mzz|A*@*H'666?CfoofE!,,||** /
e6{l***%     c                       e Zd ZdZd Zd Zy)r	   z4Class for holding the results of a minimal MEME run.c                 X    d| _         d| _        d| _        d| _        i | _        g | _        y)zInitialize record class values. N)versiondatafilecommandr   r   	sequences)selfs    r   __init__zRecord.__init__K   s,    r    c                     t        |t              r| D ]  }|j                  |k(  s|c S  yt        j	                  | |      S )zReturn the motif of index key.N)
isinstancestrr   list__getitem__)r(   keyr   s      r   r.   zRecord.__getitem__T   sA    c3 !::$ L! ##D#..r    N)__name__
__module____qualname____doc__r)   r.    r    r   r	   r	   H   s    >/r    r	   c                    |D ]  }|j                  d      sg }|D ]c  }|j                         }|rN|j                  t        |j	                  d            D cg c]  \  }}|dz  dk(  rt        |       c}}       c n |st        d       n t        d      t        t        | j                  |            | _
        yc c}}w )z-Read background letter frequencies (PRIVATE).zBackground letter frequencies    r   zPUnexpected end of stream: Expected to find line starting background frequencies.zPImproper input file. File should contain a line starting background frequencies.N)r   rstripextend	enumerater   float
ValueErrordictzipr   r   )r   r   r   background_freqsifreqs         r   r   r   a   s     
??:;! {{}$++ ,5TZZ_+E '4 1uz "$K  $ f  '
* ^
 	
 S2BCDF#s   Cc                     |D ]  }|j                  d      s n t        d      |j                         }|j                         }|d   | _        y)zRead MEME version (PRIVATE).zMEME versionzFImproper input file. File should contain a line starting MEME version.r7   N)r   r<   stripr   r$   )r   r   r   lss       r   r
   r
   ~   sU     
??>*
 T
 	
 ::<D	BUFNr    c                    |D ]  }|j                  d      s n t        d      |j                  d      st        d|z        |j                         j                  dd      }|dk(  r
d}|| _        y	|dk(  r
d}|| _        y	t        d      )
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ACGUz1Only parsing of DNA and RNA motifs is implementedN)r   r<   rC   replacer   )r   r   r   als       r   r   r      s     
??:&
 V
 	
 ??<(CdJKK::<b1Dv~ FO 
 FO LMMr    c           	      *   g g g g g}|D ]  }|j                         }t        |      dk7  r n|d   j                  t        t	        |d         |z               |d   j                  t        t	        |d         |z               |d   j                  t        t	        |d         |z               |d   j                  t        t	        |d         |z               |st        |d         |k(  s n t        t        | j                  |            }|S )z)Read letter probability matrix (PRIVATE).   r   r   r7      )r   r   r   roundr;   r=   r>   r   )r   r   r   r   r   r   freqscs           r   r   r      s    "b"F 	

u:?q	uU1X@ABq	uU1X@ABq	uU1X@ABq	uU1X@ABc&)n.	 	S&)*AHr    c                    | D ]  }|j                  d      s n j                  d      dk7  r.t        |j                  d      d   j                         d         nd}|j                  d      dk7  r.t        |j                  d      d   j                         d         nd}|j                  d	      dk7  r.t	        |j                  d	      d   j                         d         nd
}|||fS )z Read motif statistics (PRIVATE).zletter-probability matrix:znsites=r   r      zw=NzE=g        )r   findintr   r;   )r   r   r   r   r   s        r   r   r      s      ??78 59IIi4HB4NDJJy!!$**,Q/0TV  59IIdOr4ISD!!$**,Q/0tF6:iio6KU4::d#A&,,.q12QTF?F**r    c                     | D ]  }d|v s n t        d      |j                         }|j                         }dj                  |dd       }|S )zRead motif name (PRIVATE).zsorted by position p-valuez3Unexpected end of stream: Failed to find motif namer6   r   r7   )r<   rC   r   join)r   r   wordsr   s       r   _read_motif_namerY      s[     P'4/P NOO::<DJJLE88E!AJDKr    __main__)run_doctestN)r3   Bior   r   r-   r	   r   r
   r   r   r   rY   r0   
Bio._utilsr[   r4   r    r   <module>r^      sU    ; 8v/T /2E:,"+4
 z&M r    