
    Xf3                         d Z ddlmc mZ ddlmZ  G d de          Zd Z	d Z
d Zd	 Zed
k    rddlmZ  e             dS dS )zAModule for the support of Motif Alignment and Search Tool (MAST).    N)memec                       e Zd ZdZd Zd ZdS )Recordaq  The class for holding the results from a MAST run.

    A mast.Record holds data about matches between motifs and sequences.
    The motifs held by the Record are objects of the class meme.Motif.

    The mast.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/mast.crp0.de.oops.txt.xml") as f:
    ...     record = motifs.parse(f, 'MAST')
    >>> motif = record[0]
    >>> print(motif.name)
    1
    >>> motif = record['1']
    >>> print(motif.name)
    1
    c                 Z    g | _         d| _        d| _        i | _        d| _        d| _        dS )zInitialize the class. N)	sequencesversiondatabasediagramsalphabetstrand_handling)selfs    /lib/python3.11/site-packages/Bio/motifs/mast.py__init__zRecord.__init__"   s3    !    c                     t          |t                    r| D ]}|j        |k    r|c S dS t                              | |          S )zReturn the motif of index key.N)
isinstancestrnamelist__getitem__)r   keymotifs      r   r   zRecord.__getitem__+   s`    c3 	/ ! !:$$ LLL %! ! ##D#...r   N)__name__
__module____qualname____doc__r   r    r   r   r   r      s<         (" " "/ / / / /r   r   c                     t                      }	 t          j        |           }n"# t          j        $ r t	          d          w xY wt          ||           t          ||           |S )z2Parse a MAST XML format handle as a Record object.zOImproper MAST XML input file. XML root tag should start with <mast version= ...)r   ETparse
ParseError
ValueError__read_metadata__read_sequences)handlerecordxml_trees      r   readr)   5   sz    XXF
8F##= 
 
 
]
 
 	

 FH%%%VX&&&Ms	   % Ac                 H   |                                                     d          | _        |                    d                              d                              d          | _        |                    d                              d          | _        |                    d                              d          | _        t          |                    d	                              d
                    D ]\  }}t          j
        | j                  }t          |dz             |_        |                    d          |_        |                    d          |_        t          |                    d                    |_        |                     |           d S )Nr	   sequence_dbssequence_dbsourcer   r   settingsr   motifsr      idaltlength)getrootgetr	   findr
   r   r   	enumeratefindallr   Motifr   r   r1   alt_idintr3   append)r'   r(   i
motif_treer   s        r   r$   r$   F   sM   %%''++I66FNmmN3388GGKKHUUFOmmJ//33F;;FO%]]:66::;LMMF"8==#:#:#B#B7#K#KLL  :
6?++QZZ
>>$''!~~e,,:>>(3344e r   c                     |                     d                              d          D ]K}|                    d          }| j                            |           t          | |          }|| j        |<   LdS )z+Read sequences from XML ElementTree object.r   sequencer   N)r6   r8   r5   r   r<   __make_diagramr   )r'   r(   sequence_treesequence_namediagram_strs        r   r%   r%   W   s}    !{33;;JGG 5 5%))&11...$V];;)4&&	5 5r   c           
      <   t          |                    d                    }g g g }}}|                    d          D ]}|                    d          D ]}t          |                    d                    }|s|dz
  }	n9|t          |d                             d                    z
  |d         j        z
  }	|                    |	           |                    | t          |                    d                                        |                    |           ˌ|st          |          S | j        dk    rd	 t          ||          D             }
n>| j        d
k    rd t          ||          D             }
nt          d| j         d          |t          |d                             d                    z
  |d         j        z
  dz   }d t          ||
          D             t          |          gz   }d |D             }d	                    |          S )z?Make diagram string found in text file based on motif hit info.r3   seghitposr0   idxcombinec                 d    g | ]-\  }}d |                     d          dk    rdnd |j         d.S )[rcy-+])r5   r   .0hit_ele	hit_motifs      r   
<listcomp>z"__make_diagram.<locals>.<listcomp>r   sY     
 
 
" Lw{{4((C//SK).KKK
 
 
r   
unstrandedc                 *    g | ]\  }}d |j          dS )rM   rR   )r   rS   s      r   rW   z"__make_diagram.<locals>.<listcomp>w   s7     
 
 
&8gy!	!!!
 
 
r   zStrand handling option z not parsablec                 6    g | ]}|D ]}t          |          S r   )r   )rT   pairss      r   rW   z"__make_diagram.<locals>.<listcomp>   s+    OOO4$OOQAOOOOr   c                     g | ]
}|d k    |S )0r   )rT   r\   s     r   rW   z"__make_diagram.<locals>.<listcomp>   s    @@@aqCxxxxxr   rP   )
r;   r5   r8   r3   r<   r   r   zip	Exceptionjoin)r'   rB   sequence_lengthhit_eles
hit_motifsgapsseg_treerU   hit_posgap
motif_strstail_lengthmotifs_with_gapss                r   rA   rA   a   s^   -++H5566O!#R$jH!))%00 	% 	%''.. 	% 	%G'++e,,--G UkHRL$4$4U$;$; < <<z"~?TTKKfSU););%<%<=>>>OOG$$$$	%  $?###**
 
&)(J&?&?
 
 


 
	<	/	/
 
<?*<U<U
 
 



 W&2HWWWXXX#hrl..u55666B9NNQRR  PO3tZ+@+@OOOKS  A@#3@@@88$%%%r   __main__)run_doctest)r   xml.etree.ElementTreeetreeElementTreer    
Bio.motifsr   r   r   r)   r$   r%   rA   r   
Bio._utilsrm   r   r   r   <module>rs      s    H G " " " " " " " " "      %/ %/ %/ %/ %/T %/ %/ %/P  "  "5 5 5$& $& $&N z&&&&&&KMMMMM r   