o
    Rŀgf                     @   sZ   d Z ddlZddlmZ ddlmZ ddlmZ G dd dZG dd	 d	eZ	d
d Z
dS )zParse XMS motif files.    N)minidom)Node)motifsc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
XMSScannerz Class for scanning XMS XML file.c                 C   s2   t  | _|dD ]}|jtjkr| | q	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child r   B/var/www/html/myenv/lib/python3.10/site-packages/Bio/motifs/xms.py__init__   s   
zXMSScanner.__init__c                    st   |  |d}g g g g d |dD ]} fddtg d| |D  qtjd 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                    s"   g | ]\}} |  t|qS r   )appendfloat).0
nucleotidenucleotide_countnucleotide_countsr   r   
<listcomp>    s    z+XMSScanner.handle_motif.<locals>.<listcomp>GATC)alphabetcountsN)	get_textr	   zipget_acgtr   Motifr   r   r   )r   node
motif_namer   r   r   r   r   r      s   
zXMSScanner.handle_motifc                 C   sv   | dD ]3}d}d}|jD ]#}|jtjkrq|jdkr&| |g|kr&d}|jdkr1| |g}q|r8|  S qd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   r   r   r   get_property_value,   s   

zXMSScanner.get_property_valuec                 C   s   d\}}}}| dD ]A}|ddkrt| |g}q|ddkr-t| |g}q|ddkr=t| |g}q|ddkrLt| |g}q||||fS )z1Get and return the motif's weights of A, C, G, T.)        r4   r4   r4   weightsymboladeninecytosineguaninethymine)r	   getAttributer   r$   )r   r(   acgtr5   r   r   r   r&   <   s   zXMSScanner.get_acgtc                 C   sT   g }|D ]}|j tjkr||j q|jr|| |j qt	ddd
|S )zMReturn a string representation of the motif's properties listed on nodelist .z\s+  )r
   r   	TEXT_NODEr   	wholeTexthasChildNodesr$   r-   resubjoin)r   nodelistretlistr(   r   r   r   r$   J   s   zXMSScanner.get_textN)	__name__
__module____qualname____doc__r   r   r3   r&   r$   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZd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                 C   s   d dd | D S )zBReturn a string representation of the motifs in the Record object.
c                 s   s    | ]}t |V  qd S )N)str)r   r   r   r   r   	<genexpr>^   s    z!Record.__str__.<locals>.<genexpr>)rG   )r   r   r   r   __str__\   s   zRecord.__str__N)rJ   rK   rL   rM   rQ   r   r   r   r   r   V   s    r   c                 C   s   t | }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   r   r   r   reada   s   

rU   )rM   rE   xml.domr   r   Bior   r   listr   rU   r   r   r   r   <module>   s   F