
    Xf                         d Z ddlmc mZ ddlmZ ddlmZ ddlmZ d Z	 G d dej
                  Z
 G d	 d
ej                  Z G d de          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 )z,Module for the support of MEME motif format.    N)Align)motifs)Seqc                 ^   t                      }	 t          j        |           }n"# t          j        $ r t	          d          w xY wt          ||           t          ||           t          |          }t          |	                                          |_
        t          |||           |S )a?  Parse the text output of the MEME program into a meme.Record object.

    Examples
    --------
    >>> from Bio.motifs import meme
    >>> with open("motifs/meme.INO_up800.classic.oops.xml") as f:
    ...     record = meme.read(f)
    >>> for motif in record:
    ...     for sequence in motif.alignment.sequences:
    ...         print(sequence.motif_name, sequence.sequence_name, sequence.sequence_id, sequence.strand, sequence.pvalue)
    GSKGCATGTGAAA INO1 sequence_5 + 1.21e-08
    GSKGCATGTGAAA FAS1 sequence_2 - 1.87e-08
    GSKGCATGTGAAA ACC1 sequence_4 - 6.62e-08
    GSKGCATGTGAAA CHO2 sequence_1 - 1.05e-07
    GSKGCATGTGAAA CHO1 sequence_0 - 1.69e-07
    GSKGCATGTGAAA FAS2 sequence_3 - 5.62e-07
    GSKGCATGTGAAA OPI3 sequence_6 + 1.08e-06
    TTGACWCYTGCYCWG CHO2 sequence_1 + 7.2e-10
    TTGACWCYTGCYCWG OPI3 sequence_6 - 2.56e-08
    TTGACWCYTGCYCWG ACC1 sequence_4 - 1.59e-07
    TTGACWCYTGCYCWG CHO1 sequence_0 + 2.05e-07
    TTGACWCYTGCYCWG FAS1 sequence_2 + 3.85e-07
    TTGACWCYTGCYCWG FAS2 sequence_3 - 5.11e-07
    TTGACWCYTGCYCWG INO1 sequence_5 + 8.01e-07

    zOImproper MEME XML input file. XML root tag should start with <MEME version= ...)RecordETparse
ParseError
ValueError__read_metadata__read_alphabet__get_sequence_id_name_maplistkeys	sequences__read_motifs)handlerecordxml_treesequence_id_name_maps       /lib/python3.11/site-packages/Bio/motifs/meme.pyreadr      s    6 XXF
8F##= 
 
 
]
 
 	

 FH%%%FH%%%5h??0557788F&($8999Ms	   % Ac                       e Zd ZdZddZdS )MotifzA subclass of Motif used in parsing MEME (and MAST) output.

    This subclass defines functions and data specific to MEME motifs.
    This includes the motif name, the evalue for a motif, and its number
    of occurrences.
    Nc                     t           j                            | ||           d| _        d| _        d| _        d| _        d| _        dS )Initialize the class.g        r   N)r   r   __init__evaluenum_occurrencesnameidalt_id)selfalphabet	alignments      r   r   zMotif.__init__B   sD    dHi888 	    )NN__name__
__module____qualname____doc__r    r&   r   r   r   :   s2              r&   r   c                       e Zd ZdZd ZdS )InstancezGA class describing the instances of a MEME motif, and the data thereof.c                     t          j         j        | g|R i | d| _        d| _        d| _        d| _        d| _        d| _        d| _        dS )r    r   g      ?N)	r   r   sequence_namesequence_idstartpvaluestrandlength
motif_name)r#   argskwdss      r   r   zInstance.__init__O   s\    -------
r&   Nr'   r,   r&   r   r.   r.   L   s)        QQ	 	 	 	 	r&   r.   c                       e Zd ZdZd Zd ZdS )r   ay  A class for holding the results of a MEME run.

    A meme.Record is an object that holds the results from running
    MEME. It implements no methods of its own.

    The meme.Record class inherits from list, so you can access individual
    motifs in the record by their index. Alternatively, you can find a motif
    by its name:

    >>> from Bio import motifs
    >>> with open("motifs/meme.INO_up800.classic.oops.xml") as f:
    ...     record = motifs.parse(f, 'MEME')
    >>> motif = record[0]
    >>> print(motif.name)
    GSKGCATGTGAAA
    >>> motif = record['GSKGCATGTGAAA']
    >>> print(motif.name)
    GSKGCATGTGAAA
    c                 L    d| _         d| _        d| _        d| _        g | _        dS )r   r0   N)versiondatafilecommandr$   r   )r#   s    r   r   zRecord.__init__p   s)    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    r   __getitem__)r#   keymotifs      r   rB   zRecord.__getitem__x   s`    c3 	/ ! !:$$ LLL %! ! ##D#...r&   N)r(   r)   r*   r+   r   rB   r,   r&   r   r   r   [   s<         (  / / / / /r&   r   c                    |                                                     d          | _        |                    d                              d          | _        |                    d                              d          j        | _        d S )Nr<   training_setprimary_sequencesmodelcommand_line)getrootgetr<   findr=   textr>   )r   r   s     r   r   r      sm    %%''++I66FNmmN33778KLLFO]]7++00@@EFNNNr&   c                     |                     d                               d                               d          }|                    d          D ]%}| xj        |                    d          z  c_        &d S )NrF   letter_frequenciesalphabet_arrayvalue	letter_id)rL   findallr$   rK   )r   r   alphabet_treerQ   s       r   r   r      s{    n%%**+?@@EEFVWW  &&w// 2 2599[1112 2r&   c                 f    d |                      d                              d          D             S )Nc                 `    i | ]+}|                     d           |                     d          ,S )r!   r    rK   ).0sequence_trees     r   
<dictcomp>z.__get_sequence_id_name_map.<locals>.<dictcomp>   sF        	$!2!26!:!:  r&   rF   sequence)rL   rS   )r   s    r   r   r      s=     %]]>::BB:NN   r&   c                 :   |                     d                              d          D ]p}g }|                     d                              d          D ]@}d |                     d                              d          D             }d                    |          }t          |          }|                    d	          |_        |                    d
          |_        ||j                 |_        t          |                    d                    dz   |_	        t          |                    d                    |_        t          |                    d                    |_        t          |          |_        |                    |           Bt#          j        |          }	t'          | j        |	          }
|                    d          |
_        |                    d	          |
_        |                    d          |
_        t          |                    d                    |
_        t          |                    d                    |
_        t          |                    d                    |
_        |                     |
           rd S )Nr   rD   contributing_sitescontributing_sitec                 8    g | ]}|                     d           S )rR   rW   )rX   
letter_refs     r   
<listcomp>z!__read_motifs.<locals>.<listcomp>   s4        {++  r&   siter`   r0   r    r2   position   r4   r5   r!   altwidthsitese_value)rL   rS   joinr.   rK   r7   r2   r1   intr3   floatr4   __convert_strandr5   lenr6   appendr   	Alignmentr   r$   r!   r    r"   r   r   )r   r   r   
motif_tree	instances	site_treelettersr[   instancer%   rD   s              r   r   r      s3   mmH--55g>>  
	#)=>>FF
 
 	' 	'I "+.."8"8"@"@"N"N  G www''H))H",.."8"8H#,==#?#?H %9(:N%OH" z!:!:;;a?HN#IMM($;$;<<HO.y}}X/F/FGGHO!(mmHOX&&&&OI..	foy11>>$''^^F++
!~~e,,:>>'2233 #JNN7$;$; < <Z^^I6677e; r&   c                 2    | dk    rdS | dk    s| dk    rdS dS )z>Convert strand (+/-) from XML if present.

    Default: +
    minus-plusnone+Nr,   )r5   s    r   rl   rl      s6    
 s6V++s ,+r&   __main__)run_doctest)r+   xml.etree.ElementTreeetreeElementTreer   Bior   r   r   r   r   r.   r   r   r   r   r   r   rl   r(   
Bio._utilsr|   r,   r&   r   <module>r      sv   3 2 " " " " " " " " "                  ' ' 'T    FL   $    sw   $/ $/ $/ $/ $/T $/ $/ $/TF F F2 2 2    B   z&&&&&&KMMMMM r&   