ó
š¢ Ic           @   s…   d  d l  m Z d  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 „  Z
 d S(   iÿÿÿÿ(   t   Seq(   t   IUPAC(   t   sqrtNt   Motifc           B   sk   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 d „ Z RS(   s´  A generic motif class.
    
    A Motif has information about the alphabet used in the motif,
    the length of the motif, the motif name, the number of occurrences,
    and the individual instances of the motif. Each of the instances
    is an object of the Instance class.
    
    Methods:
    add_instance(instance): adds a new instance of the motif.
    get_instance_by_name(name): returns an instance with the specified name.
    c         C   sC   g  |  _  d  |  _ d |  _ d |  _ d |  _ d |  _ g  |  _ d  S(   Ni    t    (   t	   instancest   Nonet   alphabett   lengtht   num_occurrencest   namet	   consensust   pssm(   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/Motif.pyt   __init__   s    						c         C   s^   t  | t ƒ rZ |  j r+ | j |  j ƒ n  |  j rG | j |  j ƒ n  |  j j | ƒ n  d  S(   N(   t
   isinstancet   InstanceR   t   _lengthR
   t
   _motifnameR   t   append(   R   t   instance(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   add_instance    s    		c         C   s+   x$ t  j D] } | j | k r
 | Sq
 Wd  S(   N(   t   setR   t   seqnameR   (   R   R
   t   i(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   get_instance_by_name(   s    c         C   sA   | t  j k s- | t  j k s- | t  j k r9 | |  _ n d Sd  S(   Niÿÿÿÿ(   R   t   unambiguous_dnat   proteint   ambiguous_dnaR   (   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/Motif.pyt	   _alphabet.   s    -c         C   s7   t  | ƒ t k r | |  _ n t | ƒ } | |  _ d  S(   N(   t   typet   intR   (   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/Motif.pyR   4   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/Motif.pyt   _name;   s    c         C   s1   |  j  r$ t j | |  j  ƒ |  _ n	 | |  _ d  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/Motif.pyt
   _consensus>   s    	c         C   s7   t  | ƒ t k r | |  _ n t | ƒ } | |  _ d  S(   N(   R   R   R	   (   R   t   number(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   _numoccurrencesD   s    c         C   s²  |  j  d  k r t d ƒ ‚ n  d } |  j  t j k r? d } n  |  j  t j k rZ d } n  g  } |  j r¥|  j d j r¥xI |  j d j D]7 } g  } x | D] } | j d ƒ q W| j | ƒ qŠ Wxy |  j D]n } xe t	 |  j
 ƒ D]T } | j | } y | j | ƒ } Wn t k
 r$d } n X| | | c d 7<qå WqÏ Wg  | D], }	 g  |	 D] }
 |
 t |  j ƒ ^ qU^ qH} g  | D] }
 t |
 ƒ ^ q} | |  _ n	 d  |  _ d  S(   Ns#   Alphabet for motif has not been setR   t   ACGTt   ACDEFGHIKLMNPQRSTVWYi    g        g      ð?(   R   R   t
   ValueErrorR   R   R   R   t   sequenceR   t   rangeR   t   indext   lent   tupleR   (   R   t   moietiesR   t   positiont   post   mR   t	   my_moietyt   moiety_indext   yt   x(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt	   make_pssmK   s4    		
9g333333ã?c            sâ   |  j  s |  j ƒ  n  d } d } d } |  j t j k rI d } d } n  x‰ |  j  D]~ } | } t | | ƒ } t ‡  f d †  | ƒ } | rÁ g  | D] }	 t |	 d ƒ ^ q“ }
 d j |
 ƒ } n | } | | 7} qS W| |  _	 d  S(	   NR   t   NR$   t   XR%   c            s   |  d ˆ  k S(   Ni    (    (   R3   (   t   minimum_frequency(    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   <lambda>t   s    i   t   /(
   R   R4   R   R   R   t   zipt   filtert   strt   joinR   (   R   R7   R   t   null_characterR,   R-   t   this_positiont   valst   good_valuesR3   t   letterst	   my_letter(    (   R7   s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   make_consensush   s$    		#(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R    R!   R#   R4   RD   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyR      s   										t	   MEMEMotifc           B   sS   e  Z d  Z d „  Z d d d d d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s  A subclass of Motif used in parsing MEME (and MAST) output.
    
    This sublcass defines functions and data specific to MEME motifs. 
    This includes the evalue for a motif and the PSSM of the motif.
    
    Methods:
    add_instance_from_values (name = 'default', pvalue = 1, sequence = 'ATA', start = 0, strand = +): create a new instance of the motif with the specified values.
    add_to_pssm (position): add a new position to the pssm. The position should be a list of nucleotide/amino acid frequencies
    add_to_logodds (position): add a new position to the log odds matrix. The position should be a tuple of log odds values for the nucleotide/amino acid at that position.
    compare_motifs (other_motif): returns the maximum correlation between this motif and other_motif
    c         C   s,   t  j |  ƒ d |  _ g  |  _ g  |  _ d  S(   Ng        (   R   R   t   evalueR   t   logodds(   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/Motif.pyR   ‰   s    		t   defaulti   t   ATAi    t   +c         C   s–   t  ƒ  } | j | ƒ | j | ƒ | j | ƒ | j | ƒ | j | ƒ |  j rf | j |  j ƒ n  |  j r‚ | j	 |  j ƒ n  |  j
 j | ƒ d  S(   N(   R   t	   _sequencet   _pvaluet   _seqnamet   _startt   _strandR   R   R
   R   R   R   (   R   R
   t   pvalueR'   t   startt   strandt   inst(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   add_instance_from_values   s    			c         C   s7   t  | ƒ t k r | |  _ n t | ƒ } | |  _ d  S(   N(   R   t   floatRI   (   R   RI   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   _evalueœ   s    c         C   s   |  j  j | ƒ d  S(   N(   R   R   (   R   t   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/Motif.pyt   add_to_pssm£   s    c         C   s   |  j  j | ƒ d  S(   N(   RJ   R   (   R   RZ   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   add_to_logodds¦   s    c   
      C   sv  t  | t ƒ rX|  j s' t d ƒ ‚ n  | j s? t d ƒ ‚ n  t |  j ƒ } t | j ƒ } d  } | | k r‡ t |  j | j ƒ } nÍ | | k  rõ | | } x´ t d | ƒ D]A } | j | | | !} t |  j | ƒ } | | k r­ | } q­ q­ Wn_ | | } xR t d | ƒ D]A } |  j | | | !}	 t |	 | j ƒ } | | k r| } qqW| St j	 j
 t t ƒ ƒ d  Sd  S(   Ns   This motif does not have a PSSMs$   The other motif does not have a PSSMi    (   R   RH   R   R&   R*   R   t   _corrR(   t   syst   stderrt   writeR<   t   m2(
   R   t   motift   mylent   yourlent   myrt   diffR   t   yourpssmt   rt   mypssm(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   compare_motifs©   s4    		

(	   RE   RF   RG   R   RW   RY   R[   R\   Rj   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyRH   }   s   				R   c           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   sH   A class describing the instances of a motif, and the data thereof. 
    c         C   sC   d  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   NR   i    g      ð?(   R   R'   t   sequence_nameRT   RS   RU   R   t
   motif_name(   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/Motif.pyR   Ì   s    						c         C   s   | |  _  d  S(   N(   R'   (   R   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/Motif.pyRN   Õ   s    c         C   s   | |  _  d  S(   N(   Rk   (   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/Motif.pyRP   Ø   s    c         C   s   | |  _  d  S(   N(   Rl   (   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/Motif.pyR   Û   s    c         C   s   t  | ƒ } | |  _ d  S(   N(   R   RT   (   R   RT   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyRQ   Þ   s    c         C   s   t  | ƒ } | |  _ d  S(   N(   RX   RS   (   R   t   pval(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyRO   â   s    c         C   s   t  | ƒ } | |  _ d  S(   N(   RX   t   score(   R   Ro   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyt   _scoreæ   s    c         C   s   | |  _  d  S(   N(   RU   (   R   RU   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyRR   ê   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/Motif.pyR   í   s    (   RE   RF   RG   R   RN   RP   R   RQ   RO   Rp   RR   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/Motif.pyR   É   s   									c         C   s?  d } d } d } d } d } t  |  d t k ƒ rI t d „  |  ƒ }  n  t  | d t k ƒ rt t d „  | ƒ } n  d „  } t |  ƒ } x_ t |  | ƒ D]N \ }	 }
 | |	 7} | |
 7} | | |	 ƒ 7} | | |
 ƒ 7} | |	 |
 7} q™ W| | | d | } | | | d | | | | d | } | t | ƒ } | S(   Ni    c         S   s   |  | S(   N(    (   t   at   b(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyR8   ù   s    c         S   s   |  | S(   N(    (   Rq   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/Motif.pyR8   û   s    c         S   s   |  |  S(   N(    (   t   t(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyR8   ü   s    g      ð?(   R   t   listt   reduceR*   R:   R   (   R3   R2   t   sxt   syt   sxxt   syyt   sxyt   sqR   Rq   Rr   t   s1t   s2Rh   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/MEME/Motif.pyR]   ñ   s*    	

*(    (    (   t   BioR    t   Bio.AlphabetR   t   mathR   R^   R   RH   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/Motif.pyt   <module>   s   rL(