
:>"^                 @   sq   d  Z  d d l m Z d d l m Z m Z d d l Z Gd d   d  Z Gd d   d e  Z	 d	 d
   Z
 d S)zParse XMS motif files.    )motifs)minidomNodeNc               @   sR   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   sI   t    |  _ x6 | j d  D]% } | j t j k r |  j |  q Wd 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   3/tmp/pip-build-ww9dw3qa/biopython/Bio/motifs/xms.py__init__   s    zXMSScanner.__init__c                s   |  j  | j d   } d g  d g  d g  d g  i   xL | j d  D]; }   f d d   t d d d d g |  j |   D qF Wt j d	 d
 d    } | | _ |  j 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  |  ]% \ } }   | j  t |    q Sr   )appendfloat).0Z
nucleotideZnucleotide_count)nucleotide_countsr   r   
<listcomp>    s   	z+XMSScanner.handle_motif.<locals>.<listcomp>ZalphabetZGATC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   s   x | j  d  D] } d } d } xt | j D]i } | j t j k rG q, | j d k rt |  j | g  | k rt d } | j d k r, |  j | g  } q, W| r | Sq Wd S)zCExtract the value of the motif's property named key_name from node.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 | j  d  D] } | j d  d k rX t |  j | g   } q" | j d  d k r t |  j | g   } q" | j d  d k r t |  j | g   } q" | j d  d k r" t |  j | g   } q" W| | | | f S)	z1Get and return the motif's weights of A, C, G, T.        weightsymbolZadenineZcytosineZguanineZthymine)r(   r(   r(   r(   )r	   getAttributer   r   )r   r"   acgtr)   r   r   r   r!   8   s    zXMSScanner.get_acgtc             C   sz   g  } xU | D]M } | j  t j k r8 | j | j  q | j r | j |  j | j   q Wt j	 d d d j
 |   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   F   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 j  d d   |  D  S)N
c             s   s   |  ] } t  |  Vq d  S)N)str)r   r   r   r   r   	<genexpr>Y   s    z!Record.__str__.<locals>.<genexpr>)r4   )r   r   r   r   __str__X   s    zRecord.__str__N)r5   r6   r7   r8   r<   r   r   r   r   r   R   s   r   c             C   s"   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   )handleZxms_docr   r   r   r   read\   s    r?   )r8   ZBior   xml.domr   r   r2   r   listr   r?   r   r   r   r   <module>   s   B
