
    Rif                     d    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
y)zParse XMS motif files.    N)minidom)Node)motifsc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)
XMSScannerz Class for scanning XMS XML file.c                     t               | _        |j                  d      D ]1  }|j                  t        j
                  k(  s!| j                  |       3 y)zDGenerate motif Record from xms document, an XML-like motif pfm file.motifN)RecordrecordgetElementsByTagNamenodeTyper   ELEMENT_NODEhandle_motif)selfdocchilds      F/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/xms.py__init__zXMSScanner.__init__   sE    h--g6 	)E~~!2!22!!%(	)    c           
         | j                  |j                  d            }g g g g d}|j                  d      D ]M  }t        g d| j                  |            D cg c]"  \  }}||   j	                  t        |            $ c}} O t        j                  d|      }||_        | j                  j	                  |       yc c}}w )zHRead the motif's name and column from the node and add the motif record.name)ACGTcolumnGATC)alphabetcountsN)
get_textr   zipget_acgtappendfloatr   Motifr   r   )r   node
motif_namenucleotide_countsr   
nucleotidenucleotide_countr	   s           r   r   zXMSScanner.handle_motif   s    ]]4#<#<V#DE
"$2BR@//9 	F 58($--*?50J 0 "*-44U;K5LM	 f5FG
5!s   'Cc                 2   |j                  d      D ]  }d}d}|j                  D ]h  }|j                  t        j                  k7  r!|j
                  dk(  r| j                  |g      |k(  rd}|j
                  dk(  sW| j                  |g      }j |s|c S  y)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0 7>>T%6%66==E)dmmUG.D.P%)N==G+ $ug 6I7   	! r   c                    d\  }}}}|j                  d      D ]  }|j                  d      dk(  rt        | j                  |g            }3|j                  d      dk(  rt        | j                  |g            }c|j                  d      dk(  rt        | j                  |g            }|j                  d      dk(  st        | j                  |g            } ||||fS )z1Get and return the motif's weights of A, C, G, T.)        r7   r7   r7   weightsymboladeninecytosineguaninethymine)r   getAttributer$   r    )r   r&   acgtr8   s          r   r"   zXMSScanner.get_acgt<   s    '
1a//9 	3F""8,	9$--12$$X.*<$--12$$X.);$--12$$X.);$--12	3 !Qzr   c                 @   g }|D ]r  }|j                   t        j                  k(  r|j                  |j                         <|j
                  sI|j                  | j                  |j                               t t        j                  dddj                  |            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&   s       r   r    zXMSScanner.get_textJ   sv     	?D}}.t~~.##t}}T__=>		? vvfc2777#344r   N)	__name__
__module____qualname____doc__r   r   r5   r"   r     r   r   r   r      s    *)"$ 	5r   r   c                       e Zd ZdZd Zy)r
   zClass to store the information in a XMS matrix table.

    The record inherits from a list containing the individual motifs.
    c                 2    dj                  d | D              S )zBReturn a string representation of the motifs in the Record object.
c              3   2   K   | ]  }t        |        y w)N)str).0r	   s     r   	<genexpr>z!Record.__str__.<locals>.<genexpr>^   s     6U6s   )rK   )r   s    r   __str__zRecord.__str__\   s    yy6666r   N)rN   rO   rP   rQ   rZ   rR   r   r   r
   r
   V   s    
7r   r
   c                 Z    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   readr_   a   s'     mmF#G ''FMr   )rQ   rI   xml.domr   r   Bior   r   listr
   r_   rR   r   r   <module>rc      s4     	   C5 C5L7T 7	r   