B
    b                 @   sR   d Z ddlmZ ddlmZmZ ddlZG dd dZG dd deZ	d	d
 Z
dS )zParse XMS motif files.    )motifs)minidomNodeNc               @   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   s6   t  | _x(|dD ]}|jtjkr| | qW 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   -lib/python3.7/site-packages/Bio/motifs/xms.py__init__   s    zXMSScanner.__init__c                s|   |  |d}g g g g d x8|dD ]*} fddtdddd	g| |D  q*W 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).0Z
nucleotideZnucleotide_count)nucleotide_countsr   r   
<listcomp>!   s   z+XMSScanner.handle_motif.<locals>.<listcomp>r   r   r   r   ZGATC)ZalphabetZcountsN)	get_textr	   zipget_acgtr   ZMotifr   r   r   )r   nodeZ
motif_namer   r   r   )r   r   r      s    
zXMSScanner.handle_motifc             C   sz   xt| dD ]f}d}d}xP|jD ]F}|jtjkr2q |jdkrP| |g|krPd}|jdkr | |g}q W |r|S qW dS )zCExtract the value of the motif's property named key_name from node.ZpropFNkeyTvalue)r	   
childNodesr
   r   r   ZtagNamer   )r   r!   Zkey_nameZcur_propertyZright_propertyZ	cur_valuer   r   r   r   get_property_value,   s    
zXMSScanner.get_property_valuec             C   s   d\}}}}x| dD ]}|ddkr<t| |g}q|ddkr\t| |g}q|ddkr|t| |g}q|ddkrt| |g}qW ||||fS )z1Get and return the motif's weights of A, C, G, T.)g        g        g        g        weightZsymbolZadenineZcytosineZguanineZthymine)r	   ZgetAttributer   r   )r   r!   acgtr&   r   r   r   r    <   s    zXMSScanner.get_acgtc             C   sX   g }x>|D ]6}|j tjkr(||j q
|jr
|| |j q
W t	ddd
|S )zMReturn a string representation of the motif's properties listed on nodelist .z\s+  )r
   r   Z	TEXT_NODEr   Z	wholeTextZhasChildNodesr   r$   resubjoin)r   ZnodelistZretlistr!   r   r   r   r   J   s    
zXMSScanner.get_textN)	__name__
__module____qualname____doc__r   r   r%   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 )N
c             s   s   | ]}t |V  qd S )N)str)r   r   r   r   r   	<genexpr>]   s    z!Record.__str__.<locals>.<genexpr>)r/   )r   r   r   r   __str__\   s    zRecord.__str__N)r0   r1   r2   r3   r7   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   )ZhandleZxms_docr   r   r   r   read`   s    

r9   )r3   ZBior   Zxml.domr   r   r-   r   listr   r9   r   r   r   r   <module>   s   F
