ó

W/Ic           @   sø   d  d l  m Z d  d l m Z d  d l Td  d l m Z d  d l m Z d  d l Z d d d „  ƒ  YZ	 d	 e
 f d
 „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d e
 f d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d S(   iÿÿÿÿ(   t   IUPAC(   t   File(   t   *(   t   Seq(   t   MotifNt
   MEMERecordc           B   s    e  Z d  Z d „  Z d „  Z RS(   s²   A class for holding the results of a MEME run.
    
    A MEMERecord is an object that holds the results from running
    MEME. It implements no methods of its own.
        
    c         C   s:   g  |  _  d |  _ d |  _ d |  _ d |  _ g  |  _ d S(   s   __init__ (self)t    N(   t   motifst   versiont   datafilet   commandt   Nonet   alphabett   sequence_names(   t   self(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   __init__   s    					c         C   s+   x$ |  j  D] } | j | k r
 | Sq
 Wd  S(   N(   R   t   name(   R   R   t   m(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   get_motif_by_name   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR      s   		t
   MEMEParserc           B   s    e  Z d  Z d „  Z d „  Z RS(   sõ  A parser for the text output of the MEME program.
    Parses the output into an object of the MEMERecord class.
    
    Methods:
    parse (handle): parses the contents of the file handle passed to it.
    
    Example:
    
    f = open("meme.output.txt")
    parser = MEMEParser()
    meme_record = parser.parse(f)
    for motif in meme_record.motifs:
        for instance in motif.instances:
            print instance.motif_name, instance.sequence_name, instance.strand, instance.pvalue
    
    c         C   s   t  ƒ  |  _ t ƒ  |  _ d S(   s   __init__ (self)N(   t   _MEMEScannert   _scannert   _MEMEConsumert	   _consumer(   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   3   s    c         C   s    |  j  j | |  j ƒ |  j j S(   s   parse (self, handle)(   R   t   feedR   t   data(   R   t   handle(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   parse8   s    (   R   R   R   R   R   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   "   s   	R   c           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sB   Scanner for MEME output. 
    
    Methods:
    feed
        
    c         C   sN   t  | t j ƒ r | } n t j | ƒ } |  j | | ƒ |  j | | ƒ d S(   sµ   
        Feeds in MEME output for scanning. handle should
        implement the readline method. consumer is 
        a Consumer object that can receive the salient events.
        N(   t
   isinstanceR   t
   UndoHandlet   _scan_headert   _scan_motifs(   R   R   t   consumert   uhandle(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   G   s
    	c         C   sF  y t  | | j d d ƒWn t k
 r9 t d ƒ ‚ n Xt | | j d d ƒt  | | j d d ƒt | | j d d ƒt | | j d d ƒt | | j d d ƒt | | j d d ƒt | | j d d	 ƒt | | j d d
 ƒt  | | j d d ƒt  | | j d d ƒt | | j d d ƒt  | | j d d ƒd  S(   Nt   containss   MEME versionsF   Improper input file. File should contain a line starting MEME version.t   starts   TRAINING SETs   ****t   DATAFILEt   ALPHABETs   Sequence names   ----s   ***s   command:s   MOTIF  1(	   t   read_and_call_untilt   noeventt
   ValueErrort   read_and_callt   _versiont	   _datafilet	   _alphabett   _sequence_namet   _commandline(   R   R$   R#   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR!   U   s     c         C   sÆ  x¿t  | | j d d ƒt | | j d d ƒt  | | j d d ƒt  | | j d d ƒt  | | j d d ƒt  | | j d d ƒt | | j d d ƒt | | j d d ƒt  | | j ƒ t | | j d d ƒt | | j d d ƒt  | | j d d ƒt | | j d d ƒt | | j d d	 ƒt  | | j d d	 ƒt  | | j d
 d ƒt  | | j d d ƒt | | j d
 d ƒt  | | j d d ƒt	 | ƒ } | j
 d ƒ r Pq q Wd  S(   NR&   t   MOTIFR%   s   sorted by position p-values   ---s   Sequence names   log-odds matrixs   letter-probability matrixt   Timet   blanki   s   ***s   SUMMARY OF MOTIFS(   R,   t   _add_motif_with_infoR)   R*   t
   motif_namet   add_instancet   add_to_logoddst   add_to_pssmt   read_and_call_whilet   safe_peeklinet
   startswith(   R   R$   R#   t   line(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR"   g   s.    (   R   R   R   R   R!   R"   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   ?   s   		R   c           B   sz   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z RS(   s’   
    Consumer that can receive events from MEME Scanner.
    
    This is the Consumer object that should be passed to the 
    MEME Scanner.
    c         C   s"   d  |  _ g  |  _ t ƒ  |  _ d  S(   N(   R   t   current_motifR   R   R   (   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   Š   s    		c         C   s,   | j  ƒ  } | j ƒ  } | d |  j _ d  S(   Ni   (   t   stript   splitR   R   (   R   R=   t   ls(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR-      s    c         C   s.   | j  ƒ  } | j d d ƒ } | |  j _ d  S(   Ns
   DATAFILE= R   (   R?   t   replaceR   R	   (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR.   ”   s    c         C   sO   | j  ƒ  } | j d d ƒ } | d k r6 t j } n	 t j } | |  j _ d  S(   Ns
   ALPHABET= R   t   ACGT(   R?   RB   R    t   unambiguous_dnat   proteinR   R   (   R   R=   t   al(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR/   ™   s    	c         C   s_   | j  ƒ  } | j ƒ  } |  j j j | d ƒ t | ƒ d k r[ |  j j j | d ƒ n  d  S(   Ni    i   i   (   R?   R@   R   R   t   appendt   len(   R   R=   RA   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR0   ¢   s
    c         C   s.   | j  ƒ  } | j d d ƒ } | |  j _ d  S(   Ns	   command: R   (   R?   RB   R   R
   (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR1   ©   s    c         C   sŠ   | j  ƒ  } | j ƒ  } t j ƒ  } | j | d ƒ | j | d ƒ | j | d ƒ | j |  j j	 ƒ |  j j
 j | ƒ | |  _ d  S(   Ni   i   i   (   R?   R@   R   t	   MEMEMotift   _lengtht   _numoccurrencest   _evalueR/   R   R   R   RG   R>   (   R   R=   RA   t   motif(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR5   ®   s    c         C   sB   | j  ƒ  } | j ƒ  } d j | d d !ƒ } |  j j | ƒ d  S(   Nt    i    i   (   R?   R@   t   joinR>   t   _name(   R   R=   RA   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR6   ¹   s    c         C   sÞ   | j  ƒ  } | j ƒ  } |  j j j d ƒ d k r t j | d |  j j ƒ } |  j j d | d d | d | d d	 | d
 d | d ƒ nM t j | d |  j j ƒ } |  j j d | d d | d | d d	 | d ƒ d  S(   Nt   revcompiÿÿÿÿi   R   i    t   sequenceR&   i   t   pvaluei   t   strandi   i   (	   R?   R@   R   R
   t   findR   R   R>   t   add_instance_from_values(   R   R=   RA   t   seq(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR7   ¿   s    >c         C   sQ   | j  ƒ  } | j ƒ  } t g  | D] } t | ƒ ^ q" ƒ } |  j j | ƒ d  S(   N(   R?   R@   t   tuplet   floatR>   R9   (   R   R=   t   slt   it   thisposition(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR9   É   s    %c         C   sQ   | j  ƒ  } | j ƒ  } t g  | D] } t | ƒ ^ q" ƒ } |  j j | ƒ d  S(   N(   R?   R@   RX   RY   R>   R8   (   R   R=   RZ   R[   R\   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR8   Ï   s    %c         C   s   d  S(   N(    (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR*   Õ   s    (   R   R   R   R   R-   R.   R/   R0   R1   R5   R6   R7   R9   R8   R*   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   ‚   s   										
		t   _MASTConsumerc           B   sŒ   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÌ  
    Consumer that can receive events from _MASTScanner.
    
    A _MASTConsumer parses lines from a mast text output file.
    The motif match diagrams are parsed using line buffering. 
    Each of the buffering functions have a dummy variable that is
    required for testing using the Bio.ParserSupport.TaggingConsumer.
    If this variable isn't there, the TaggingConsumer barfs. In
    the _MASTScanner, None is passed in the place of this variable.
    c         C   s4   t  ƒ  |  _ d |  _ g  |  _ d |  _ d |  _ d  S(   NR   i    (   t
   MASTRecordR   t   _current_seqt   _line_buffert   _buffer_sizet   _buffered_seq_start(   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   å   s
    			c         C   s0   | j  ƒ  } | j ƒ  } |  j j | d ƒ d  S(   Ni   (   R?   R@   R   R-   (   R   R=   RA   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR-   ì   s    c         C   s{   | j  ƒ  } | j ƒ  } |  j j | d ƒ d } | d d k r^ t j } |  j j | ƒ n t j } |  j j | ƒ d  S(   Ni   R   i   s   (nucleotide)(   R?   R@   R   t	   _databaseR    RD   R/   RE   (   R   R=   RA   RF   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRc   ñ   s    		c         C   s„   | j  ƒ  } | j ƒ  } t j ƒ  } | j |  j j ƒ | j | d ƒ | d } | j | ƒ | j	 | d ƒ |  j j
 | ƒ d  S(   Ni   i    i   (   R?   R@   R   RI   R/   R   R   RJ   RP   t
   _consensust
   _add_motif(   R   R=   RA   R   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRe   ý   s    
c   
      C   sg  | j  ƒ  } | j ƒ  } |  j j | d |  j ƒ | d j d ƒ } d } d } xt d t | ƒ ƒ D]ø } | | j d ƒ d k sŸ | | j d ƒ d k rKt j	 ƒ  } | j
 |  j ƒ | j | ƒ t j d ƒ } | j | | ƒ d } | | j d ƒ d k rd | _ n	 d	 | _ |  j j | ƒ }	 |	 j | ƒ | |	 j 7} qg | t | | ƒ 7} qg Wd  S(
   Ni   t   _i    t   [iÿÿÿÿt   <s   \d+t   -t   +(   R?   R@   R   t   _add_diagram_for_sequenceR_   t   rangeRH   RU   R   t   Instancet   _seqnamet   _startt   ret   compilet   findallRT   R   R7   t   lengtht   int(
   R   R=   RA   t   dsR[   R&   t   instt   rt   mnRM   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _add_match_diagram  s(    2	c   
      C   s}  | j  ƒ  } | j ƒ  } |  j j | d ƒ |  j j | d | d ƒ | d j d ƒ } d } d } xt d t | ƒ ƒ D]ù } | | j d ƒ d k s´ | | j d ƒ d k rat j	 ƒ  } | j
 | d ƒ | j | ƒ t j d ƒ } | j | | ƒ d } | | j d ƒ d k r)d | _ n	 d | _ |  j j | ƒ }	 |	 j | ƒ | |	 j 7} q| | t | | ƒ 7} q| Wd  S(   Ni    i   Rf   Rj   iÿÿÿÿRi   s   \d+(   R?   R@   R   t   _add_sequenceRk   Rl   RH   RU   R   Rm   Rn   Ro   Rp   Rq   Rr   RT   R   R7   Rs   Rt   (
   R   R=   RA   Ru   R[   R&   Rv   Rw   Rx   RM   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt    _add_sequence_match_with_diagram   s*    2	c         C   s…  d } x! |  j  D] } | | j ƒ  7} q W| j ƒ  } |  j j | d |  j ƒ | d j d ƒ } d } d } xt d t | ƒ ƒ D]ø } | | j d ƒ d k s½ | | j d ƒ d k rit	 j
 ƒ  } | j |  j ƒ | j | ƒ t j d ƒ }	 |	 j | | ƒ d }
 | | j d	 ƒ d k r1d	 | _ n	 d
 | _ |  j j |
 ƒ } | j | ƒ | | j 7} q… | t | | ƒ 7} q… Wd  S(   NR   i   Rf   i    Rg   iÿÿÿÿRh   s   \d+Ri   Rj   (   R`   R?   R@   R   Rk   R_   Rl   RH   RU   R   Rm   Rn   Ro   Rp   Rq   Rr   RT   R   R7   Rs   Rt   (   R   t   dummyR=   t   lRA   Ru   R[   R&   Rv   Rw   Rx   RM   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _add_diagram_from_buffer9  s,    2	c         C   sD   | j  ƒ  } | |  _ |  j j j | ƒ s@ |  j j j | ƒ n  d  S(   N(   R?   R_   R   t	   sequencest   countRG   (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _set_current_seqS  s    	c         C   s6   | j  ƒ  } | j d ƒ s. |  j j | ƒ n d Sd  S(   Ns   *****iÿÿÿÿ(   R?   R<   R`   RG   (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _add_line_to_bufferY  s    c         C   s–  |  j  j |  j ƒ } t | ƒ d k r’|  j |  j d } |  j d j ƒ  } d } t | ƒ } x6| | k  r˜| | j d ƒ d k r‹g  } | | j d ƒ } | j ƒ  | d j	 d ƒ }	 |	 d k rç | d d d | d }
 n | d |	 d d | d }
 | j
 |
 ƒ xt d t | ƒ ƒ D]} | | j	 d ƒ }	 | | d j	 d ƒ } |	 d k rÉ| d k r™| | |	 d d | | d d  }
 q#| | |	 d d | | d d | d !}
 nZ | d k rú| | d d | | d d  }
 n) | | d d | | d d | d !}
 | j
 |
 ƒ q(W| j ƒ  | d k rj| d | !| | | d } n | | | d } t | ƒ } n  | d 7} qc Wd } t | ƒ t | ƒ k røt j j d |  j d |  j d d	 t | ƒ d
 ƒ g  } n  x— t d t | ƒ ƒ D]} } | | } | j |  j d } | | | | j !} t j | |  j  j ƒ } | j | ƒ | r| j t | | ƒ ƒ qqWn  d S(   s©   Parses the line buffer to get e-values for each instance of a motif.
        This buffer parser is the most likely point of failure for the 
        MASTParser.
        i    i   t   et   .iÿÿÿÿi   s   Failure to parse p-values for s   :  s    to: s   
N(   R   t   get_motif_matches_for_sequenceR_   RH   R`   Ra   R@   R€   t   reverseRU   RG   Rl   t   syst   stderrt   writet   strR&   Rb   Rs   R   R   t	   _sequencet   _pvalueRY   (   R   R|   t   instst   fullSeqt   pvalst   pt   lpvalt   pvst   spet   dotindt   thispvalt   spit
   prevdotindR[   Rv   R&   t   thisSeq(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _parse_buffer`  sZ    
)0%)
 8	
c         C   s   g  |  _  d |  _ d  S(   Ni    (   R`   Ra   (   R   R|   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _blank_buffer  s    	c         C   sÛ  |  j  d k r t |  j ƒ d k r×t |  j ƒ |  _  |  j |  j  d j ƒ  } | d |  j |  j  d <t | d ƒ |  _ q×nVd } xZ t |  j  t |  j ƒ d ƒ D]9 } |  j | |  j  |  j | j ƒ  |  j | |  j  <q§ W|  j t |  j ƒ d j ƒ  } t | d ƒ |  j t |  j |  j  d ƒ k rV|  j |  j  d c | d 7<nk t | d ƒ |  j t |  j |  j  d ƒ } |  j |  j  d c d | 7<|  j |  j  d c | d 7<|  j d |  j  !|  _ d  S(   Ni    i   t   N(   Ra   RH   R`   R@   Rt   Rb   Rl   R?   (   R   R|   t   llR[   t   differ(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _collapse_buffer¡  s     &7 1!/c         C   sm   | j  ƒ  } | j d ƒ d k si | j d ƒ d k r9 n0 | j d ƒ d k rQ n | j d ƒ d k ri n  d  S(   NRg   iÿÿÿÿRh   Rƒ   Rj   (   R?   RU   (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   _add_motif_matchµ  s    *c         C   s   d  S(   N(    (   R   R=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR*   ¾  s    (   R   R   R   R   R-   Rc   Re   Ry   R{   R~   R   R‚   R™   Rš   Rž   RŸ   R*   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR]   Ú   s   
										=				t
   MASTParserc           B   s    e  Z d  Z d „  Z d „  Z RS(   s,  
    Parser for MAST text output. HTML output cannot be parsed, yet. Returns a MASTRecord
    
    A MASTParser takes a file handle for a MAST text output file and 
    returns a MASTRecord, containing the hits between motifs and 
    sequences. The parser does some unusual line buffering to parse out 
    match diagrams. Really complex diagrams often lead to an error message 
    and p-values not being parsed for a given line.
    
    Methods:
    parse (handle): parses the data from the file handle passed to it.
    
    Example:
    
    f = open("mast_file.txt")
    parser = MASTParser()
    mast_record = parser.parse(f)
    for motif in mast_record.motifs:
        for instance in motif.instances:
            print instance.motif_name, instance.sequence_name, instance.strand, instance.pvalue
    c         C   s   t  ƒ  |  _ t ƒ  |  _ d  S(   N(   R]   R   t   _MASTScannerR   (   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   Ù  s    c         C   s    |  j  j | |  j ƒ |  j j S(   N(   R   R   R   R   (   R   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   Ý  s    (   R   R   R   R   R   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR    Ã  s   	R¡   c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s1   
    Scanner for MAST text output. 
        
    c         C   s^   t  | t j ƒ r | } n t j | ƒ } |  j | | ƒ |  j | | ƒ |  j | | ƒ d  S(   N(   R   R   R    R!   t   _scan_matchest   _scan_annotated_matches(   R   R   R#   R$   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   è  s    	c         C   s  y t  | | j d d ƒWn t k
 r9 t d ƒ ‚ n Xt | | j d d ƒt  | | j d d ƒt | | j d d ƒt | | j d d ƒt | | j d d ƒt  | | j d d ƒt | | j d d	 ƒt | | j d d
 ƒt  | | j d d ƒt  | | j d d ƒd  S(   NR%   s   MAST versionsC   Improper input file. Does not begin with a line with 'MAST version'R&   s   DATABASE AND MOTIFSt   DATABASEs   ****s   MOTIF WIDTHR2   s   ----R4   i   s   SECTION II:(   R)   R*   R+   R,   R-   Rc   Re   (   R   R$   R#   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR!   ò  s    c         C   sr   t  | | j d d ƒt | | j d d ƒt | | j d d ƒt  | | j d d ƒt | | j d d ƒd  S(   NR&   s   SEQUENCE NAMEs   ---R4   i   (   R)   R*   R,   (   R   R$   R#   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR¢     s
    c         C   s  t  | | j d d ƒt | | j d d ƒt  | | j d d ƒt | | j d d ƒt  | | j d d ƒt | | j ƒ t | | j d d ƒd } x~| d k r| j rá | j d k rÑ | j d  ƒ n  | j d  ƒ n  t | | j	 ƒ t  | | j d d ƒt  | | j
 d d ƒ| j d  ƒ | j d  ƒ t | | j d d ƒxÇ t | ƒ } | j d ƒ r‚| j d  ƒ d } Pn  t  | | j
 d d ƒt | | j d d ƒ| j d  ƒ t | | j d d ƒr×PqPt | | j d d ƒrP| j d  ƒ | j d  ƒ d } PqPqPWq Wd  S(	   NR&   s   SECTION III:s   ****s   *****R4   i   i    s	     DIAGRAM(   R)   R*   R,   R:   R_   Ra   R™   R   Rš   R   R‚   R~   R;   R<   Rž   t   attempt_read_and_call(   R   R$   R#   t   readMatchesR=   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR£   
  sF    	(   R   R   R   R   R!   R¢   R£   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR¡   ã  s
   	
		R^   c           B   sq   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   s˜  The class for holding the results from a MAST run.
    
    A MASTRecord holds data about matches between motifs and sequences.
    The motifs held by the MASTRecord are objects of the class MEMEMotif.
    
    Methods:
    get_motif_matches_for_sequence(sequence_name): returns all of the
        motif matches within a given sequence. The matches are objects of
        the class MEME.Motif.Instance
    get_motif_matches (motif_name): returns all of the matches for a motif
        in the sequences searched. The matches returned are of class 
        MEME.Motif.Instance
    get_motif_by_name (motif_name): returns a MEMEMotif with the given
        name.
    c         C   sC   g  |  _  d |  _ g  |  _ d |  _ i  |  _ d  |  _ g  |  _ d  S(   NR   (   R   R   t   matchest   databaset   diagramsR   R   R   (   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   A  s    						c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR-   J  s    c         C   sA   | t  j k s- | t  j k s- | t  j k r9 | |  _ n d Sd  S(   Niÿÿÿÿ(   R    RE   t   ambiguous_dnaRD   R   (   R   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR/   M  s    -c         C   s   | |  _  d  S(   N(   R¨   (   R   R¨   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRc   S  s    c         C   sa   g  } xD |  j  D]9 } x0 | j D]% } | j | k r  | j | ƒ q  q  Wq W| j d „  ƒ | S(   Nc         S   s   t  |  j | j ƒ S(   N(   t   cmpR&   (   t   xt   y(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   <lambda>\  s    (   R   t	   instancest   sequence_nameRG   t   sort(   R   RW   R   R   R[   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR…   V  s    c         C   s   |  j  | j ƒ } | j S(   N(   R   R   R¯   (   R   RM   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   get_motif_matches_  s    c         C   s   | |  j  | <d  S(   N(   R©   (   R   t   diagramRW   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRk   c  s    c         C   s   |  j  j | ƒ d  S(   N(   R§   RG   (   R   t   match(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt
   _add_matchf  s    c         C   s   |  j  j | ƒ d  S(   N(   R   RG   (   R   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRz   i  s    c         C   s   |  j  j | ƒ d  S(   N(   R   RG   (   R   RM   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyRe   l  s    c         C   s+   x$ |  j  D] } | j | k r
 | Sq
 Wd  S(   N(   R   R   (   R   R   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR   o  s    (   R   R   R   R   R-   R/   Rc   R…   R²   Rk   Rµ   Rz   Re   R   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyR^   1  s   												(    (    (    (    (    (    (   t   Bio.AlphabetR    t   BioR   t   Bio.ParserSupportR   t   Bio.MEMER   Rp   R   t   AbstractParserR   R   R   R]   R    R¡   R^   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Parser.pyt   <module>   s   
CXé N