
    Xff                     r    d Z ddlZddlmZ ddlmZ ddlmZ  G d d          Z G d d	e          Z	d
 Z
dS )zParse XMS motif files.    N)minidom)Node)motifsc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )
XMSScannerz Class for scanning XMS XML file.c                     t                      | _        |                    d          D ],}|j        t          j        k    r|                     |           -dS )zDGenerate motif Record from xms document, an XML-like motif pfm file.motifN)RecordrecordgetElementsByTagNamenodeTyper   ELEMENT_NODEhandle_motif)selfdocchilds      .lib/python3.11/site-packages/Bio/motifs/xms.py__init__zXMSScanner.__init__   sY    hh--g66 	) 	)E~!222!!%(((	) 	)    c                 f   |                      |                    d                    }g g g g d|                    d          D ]3}fdt          g d|                     |                    D              4t	          j        d          }||_        | j                            |           dS )zHRead the motif's name and column from the node and add the motif record.name)ACGTcolumnc                 f    g | ]-\  }}|                              t          |                    .S  )appendfloat).0
nucleotidenucleotide_countnucleotide_countss      r   
<listcomp>z+XMSScanner.handle_motif.<locals>.<listcomp>    sJ       0J 0 "*-44U;K5L5LMM  r   GATC)alphabetcountsN)	get_textr   zipget_acgtr   Motifr   r   r   )r   node
motif_namer   r	   r$   s        @r   r   zXMSScanner.handle_motif   s    ]]4#<#<V#D#DEE
"$2BR@@//99 	 	F   47((($--*?*?5 5     f5FGGG
5!!!!!r   c                    |                     d          D ]t}d}d}|j        D ]`}|j        t          j        k    r|j        dk    r|                     |g          |k    rd}|j        dk    r|                     |g          }a|r|c S udS )zCExtract the value of the motif's property named key_name from node.propFNkeyTvalue)r   
childNodesr   r   r   tagNamer)   )r   r-   key_namecur_propertyright_property	cur_valuer   s          r   get_property_valuezXMSScanner.get_property_value,   s     55f== 	! 	!L"NI%0 7 7>T%666=E))dmmUG.D.D.P.P%)N=G++ $ug 6 6I !    !tr   c                 2   d\  }}}}|                     d          D ]}|                    d          dk    r$t          |                     |g                    }?|                    d          dk    r$t          |                     |g                    }||                    d          dk    r$t          |                     |g                    }|                    d          dk    r#t          |                     |g                    }||||fS )z1Get and return the motif's weights of A, C, G, T.)        r;   r;   r;   weightsymboladeninecytosineguaninethymine)r   getAttributer    r)   )r   r-   acgtr<   s          r   r+   zXMSScanner.get_acgt<   s   '
1a//99 	3 	3F""8,,	99$--1122$$X..*<<$--1122$$X..);;$--1122$$X..);;$--1122!Qzr   c                 *   g }|D ]f}|j         t          j        k    r|                    |j                   2|j        r-|                    |                     |j                             gt          j	        ddd
                    |                    S )zMReturn a string representation of the motif's properties listed on nodelist .z\s+  )r   r   	TEXT_NODEr   	wholeTexthasChildNodesr)   r3   resubjoin)r   nodelistretlistr-   s       r   r)   zXMSScanner.get_textJ   s     	? 	?D}..t~....# ?t}}T_==>>>vfc2777#3#3444r   N)	__name__
__module____qualname____doc__r   r   r9   r+   r)   r   r   r   r   r      se        **) ) )" " "$     	5 	5 	5 	5 	5r   r   c                       e Zd ZdZd ZdS )r
   zClass to store the information in a XMS matrix table.

    The record inherits from a list containing the individual motifs.
    c                 @    d                     d | D                       S )zBReturn a string representation of the motifs in the Record object.
c              3   4   K   | ]}t          |          V  d S )N)str)r!   r	   s     r   	<genexpr>z!Record.__str__.<locals>.<genexpr>^   s(      66U666666r   )rO   )r   s    r   __str__zRecord.__str__\   s#    yy66666666r   N)rR   rS   rT   rU   r\   r   r   r   r
   r
   V   s-         
7 7 7 7 7r   r
   c                 V    t          j        |           }t          |          j        }|S )zRead motifs in XMS matrix format from a file handle.

    XMS is an XML format for describing regulatory motifs and PSSMs.
    This format was defined by Thomas Down, and used in the NestedMICA and MotifExplorer programs.
    )r   parser   r   )handlexms_docr   s      r   readra   a   s(     mF##G  'FMr   )rU   rM   xml.domr   r   Bior   r   listr
   ra   r   r   r   <module>re      s      				                  C5 C5 C5 C5 C5 C5 C5 C5L7 7 7 7 7T 7 7 7	 	 	 	 	r   