
:>"^B  ã               @   s°   d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d „  Z Gd d	 „  d	 e ƒ Z	 d
 d „  Z
 d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d S)z4Module for the support of MEME minimal motif format.é    )Úprint_function)ÚSeq)ÚmotifsNc       
      C   s  d } t  ƒ  } t | |  ƒ t | |  ƒ t | |  ƒ xÚ x" |  D] } | j d ƒ r@ Pq@ W| S| j ƒ  d } | d 7} t | |  ƒ \ } } } t | |  ƒ } t j	 d | j
 d | ƒ }	 | j |	 _ | |	 _ | |	 _ | |	 _ | |	 _ | j |	 ƒ t | ƒ | k s9 t ‚ q9 W| S)a  Parse the text output of the MEME program into a meme.Record object.

    Examples
    --------
    >>> from Bio.motifs import minimal
    >>> with open("motifs/meme.out") as f:
    ...     record = minimal.read(f)
    ...
    >>> for motif in record:
    ...     print(motif.name, motif.evalue)
    ...
    1 1.1e-22

    You can access individual motifs in the record by their index or find a motif
    by its name:

    >>> from Bio import motifs
    >>> with open("motifs/minimal_test.meme") as f:
    ...     record = motifs.parse(f, 'minimal')
    ...
    >>> motif = record[0]
    >>> print(motif.name)
    KRP
    >>> motif = record['IFXA']
    >>> print(motif.name)
    IFXA

    This function wont retrieve instances, as there are none in minimal meme format.

    r   ZMOTIFé   ÚalphabetÚcounts)ÚRecordÚ_read_versionÚ_read_alphabetÚ_read_backgroundÚ
startswithÚsplitÚ_read_motif_statisticsÚ	_read_lpmr   ZMotifr   Ú
backgroundÚlengthÚnum_occurrencesÚevalueÚnameÚappendÚlenÚAssertionError)
ÚhandleZmotif_numberÚrecordÚliner   r   r   r   r   Úmotif© r   ú7/tmp/pip-build-ww9dw3qa/biopython/Bio/motifs/minimal.pyÚread   s.    	
				r   c               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)r   z4Class for holding the results of a minimal MEME run.c             C   s:   d |  _  d |  _ d |  _ d |  _ i  |  _ g  |  _ d S)zInitialize record class values.Ú N)ÚversionZdatafileÚcommandr   r   Ú	sequences)Úselfr   r   r   Ú__init__N   s    					zRecord.__init__c             C   sJ   t  | t ƒ r6 x4 |  D] } | j | k r | Sq Wn t j |  | ƒ Sd S)zReturn the motif of index key.N)Ú
isinstanceÚstrr   ÚlistÚ__getitem__)r#   Úkeyr   r   r   r   r(   W   s
    zRecord.__getitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r(   r   r   r   r   r   K   s   	r   c             C   sá   x* | D] } | j  d ƒ r Pq Wt d ƒ ‚ y t | ƒ } Wn t k
 r] t d ƒ ‚ Yn X| j ƒ  } | j ƒ  } t | d ƒ t | d ƒ t | d ƒ t | d ƒ f \ } } } } d | d	 | d
 | d | i |  _ d S)z-Read background letter frequencies (PRIVATE).zBackground letter frequencieszPImproper input file. File should contain a line starting background frequencies.zPUnexpected end of stream: Expected to find line starting background frequencies.r   é   é   é   ÚAÚCÚGÚTN)r   Ú
ValueErrorÚnextÚStopIterationÚstripr   Úfloatr   )r   r   r   Úlsr1   r2   r3   r4   r   r   r   r   c   s    Fr   c             C   sV   x* | D] } | j  d ƒ r Pq Wt d ƒ ‚ | j ƒ  } | j ƒ  } | d |  _ d S)zRead MEME version (PRIVATE).zMEME versionzFImproper input file. File should contain a line starting MEME version.é   N)r   r5   r8   r   r    )r   r   r   r:   r   r   r   r	   t   s    r	   c             C   sŒ   x* | D] } | j  d ƒ r Pq Wt d ƒ ‚ | j  d ƒ sL t d | ƒ ‚ | j ƒ  j d d ƒ } | d k ry d } n d } | |  _ d S)	zRead alphabet (PRIVATE).ZALPHABETzHUnexpected end of stream: Expected to find line starting with 'ALPHABET'z
ALPHABET= z'Line does not start with 'ALPHABET':
%sr   ZACGTZACDEFGHIKLMNPQRSTVWYN)r   r5   r8   Úreplacer   )r   r   r   Úalr   r   r   r
   €   s    	r
   c             C   s  g  g  g  g  g } xÁ | D]¹ }  |  j  ƒ  } t | ƒ d k r> P| d j t t | d ƒ d ƒ ƒ | d j t t | d ƒ d ƒ ƒ | d j t t | d ƒ d ƒ ƒ | d j t t | d ƒ d ƒ ƒ q Wi  } | d | d <| d | d <| d | d	 <| d | d
 <| S)z)Read letter probability matrix (PRIVATE).é   r   i@B r   r;   r.   r1   r2   r3   r4   )r   r   r   Úintr9   )r   r   r   ZfreqsÚcr   r   r   r   ‘   s    %%%)r   c             C   s—   x | D] }  |  j  d ƒ r Pq Wt |  j d ƒ d j ƒ  d ƒ } t |  j d ƒ d j ƒ  d ƒ } t |  j d ƒ d j ƒ  d ƒ } | | | f S)z Read motif statistics (PRIVATE).zletter-probability matrix:znsites=r   r   zw=zE=)r   r?   r   r9   )r   r   r   r   r   r   r   r   r   ¤   s    ###r   c             C   s_   x' |  D] } d | k r Pq Wt  d ƒ ‚ | j ƒ  } | j ƒ  } d j | d d … ƒ } | S)zRead motif name (PRIVATE).zsorted by position p-valuez3Unexpected end of stream: Failed to find motif nameú r   r;   )r5   r8   r   Újoin)r   r   Úwordsr   r   r   r   Ú_read_motif_name±   s    rD   )r-   Ú
__future__r   ZBior   r   Úmathr   r'   r   r   r	   r
   r   r   rD   r   r   r   r   Ú<module>   s   ;