ó
Æ;ìWc           @€  s‘  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d e f d „  ƒ  YZ d „  Z d e
 d „ Z d „  Z d „  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d  d l Z d e f d „  ƒ  YZ d  d l Z d  d l Z i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d  d! 6d" d# 6d$ d% 6d& d' 6d" d( 6d d) 6d" d* 6Z d+ e f d, „  ƒ  YZ d- „  Z e d. k re j d/ ƒ Z g  Z e d0 ƒ Z e e ƒ Z e j e ƒ x e j ƒ  D] Z e GHqÕWx¦ e D]› Z  e  j! ƒ  Ge  j" ƒ  Ge  j# ƒ  j$ ƒ  GHx5 e% j& e  ƒ D]$ Z' d1 e' d2 e' d3 e' d4 f GHq&Wx5 e j& e  ƒ D]$ Z' d1 e' d2 e' d3 e' d4 f GHq^WqëWn  d S(5   iÿÿÿÿ(   t   with_statementNt   Sequencec           B€  s}   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d d „ Z d „  Z RS(   s  Biological sequence class. Sequence data is immutable.

    data: the sequence data as a tuple or string
    alpha: the alphabet from which symbols are taken
    name: the sequence name, if any
    info: can contain additional sequence information apart from the name
    t    c         C€  sÇ  | |  _  | |  _ t | ƒ t k r6 t | ƒ |  _ nN t | ƒ t k rT | |  _ n0 t | ƒ t k rx t | ƒ |  _ n t d ƒ ‚ | d
 k rÀ t	 j
 |  j ƒ } | d
 k rÃt d ƒ ‚ qÃnt | t ƒ rV| } t	 j | ƒ } | d
 k rt d | d ƒ ‚ n  | j |  j ƒ sÃt d d j | j ƒ  ƒ d d j |  j ƒ d ƒ ‚ qÃnm t | t	 j ƒ r·| j |  j ƒ sÃt d d j | j ƒ  ƒ d d j |  j ƒ d ƒ ‚ qÃn t d	 ƒ ‚ d
 S(   s  Create a sequence with sequence data.
        Specifying the alphabet is optional, so is the name and info.
        Example:
        >>> myseq = sequence.Sequence('MVSAKKVPAIAMSFGVSF')
        will create a sequence with name "", and assign one of the predefined alphabets on basis of what symbols were used.
        >>> myseq.getAlphabet().getSymbols()
        will most likely output the standard protein alphabet:
        ('A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y')
        sA   Sequence data is not specified correctly: must be string or tuples)   Could not identify alphabet from sequences"   No predefined alphabet with name "s   "s   Invalid alphabet specified: R   s"    is not compatible with sequence 't   's   Unexpected type for alphaN(   t   namet   infot   typet   strt   tuplet   datat   listt   RuntimeErrort   Nonet   alphabett   getBySeqt
   isinstancet
   basestringt	   getByNamet   isValidStringt   joint
   getSymbolst   Alphabet(   t   selft   sequencet   alphaR   t   seqinfot	   alphaname(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   __init__   s0    
		==c         C€  s   |  j  S(   s   Get the name of the sequence(   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getName9   s    c         C€  s   |  j  S(   sK   Get additional info of the sequence (e.g. from the defline in a FASTA file)(   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getInfo<   s    c         C€  s   |  j  S(   s7   Retrieve the alphabet that is assigned to this sequence(   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getAlphabet?   s    c         C€  s   | |  _  d S(   s   Change the name of the sequenceN(   R   (   R   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   setNameB   s    c         C€  sF   t  | t j ƒ r6 | j |  j ƒ sB t d ƒ ‚ qB n t d ƒ ‚ d S(   sT   Set the alphabet, throws an exception if it is not compatible with the sequence datas   Invalid alphabet specifieds   Unexpected type for alphaN(   R   R   R   R   R	   R   (   R   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   setAlphabetE   s    c         C€  s   |  j  S(   s/   Retrieve the sequence data (a tuple of symbols)(   R	   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getSequenceM   s    c         C€  s   d j  |  j ƒ S(   s<   Retrieve the sequence data as a string (copy of actual data)R   (   R   R	   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt	   getStringP   s    c         C€  s   t  |  j ƒ S(   s2   Get the length of the sequence (number of symbols)(   t   lenR	   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getLenS   s    i   c         C€  sh   | d k rN | |  j  ƒ  | k rN | d k r9 |  j | S|  j | | | !Sn t d |  j ƒ  ƒ ‚ d S(   sÊ   Retrieve a site in the sequence of desired length.
        Note that positions go from 0 to length-1, and that if the requested site
        extends beyond those the method throws an exception.
        i    i   s/   Attempt to access invalid position in sequence N(   R$   R	   R   R   (   R   t   positiont   length(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getSiteV   s
    "c         C€  s   |  j  ƒ  Gd G|  j ƒ  GHd S(   s%    A short description of the sequence t   :N(   R   R$   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   nicec   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R    R!   R"   R$   R'   R)   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR   	   s   (								c         C€  s@   g  } t  |  ƒ } x' | j ƒ  D] } | j | j ƒ  ƒ q W| S(   s‡    Read one or more lines of text from a file--for example an alignment.
    Return as a list of strings.
    filename: name of file
    (   t   opent	   readlinest   extendt   split(   t   filenamet   txtlistt   ft   line(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   readStringsg   s
    c         C€  sÂ  g  } d
 } d
 } g  } t |  ƒ } | j ƒ  } xý | r/| d d k rç | rÁ y> | rj d j | ƒ }	 n t | | | | ƒ }	 | j |	 ƒ WqÁ t k
 r½ }
 t j d | d I|
 IJqÁ Xn  | d d !} | j	 ƒ  d } g  } n9 | j	 ƒ  } x* | D]" } | j
 t | j d ƒ ƒ ƒ qú W| j ƒ  } q3 W| r¨y> | rQd j | ƒ }	 n t | | | | ƒ }	 | j |	 ƒ Wq´t k
 r¤}
 t j d | d I|
 IJq´Xn t d	 ƒ ‚ | j ƒ  | S(   sð    Read one or more sequences from a file in FASTA format.
    filename: name of file to load sequences from
    alpha: alphabet that is used (if left unspecified, an attempt is made to identify the alphabet for each individual sequence)
    i    t   >R   s	   Warning: s    is invalid (ignored): i   iÿÿÿÿt   *s/   No sequences on FASTA format found in this fileN(   R   R-   t   readlineR   R   t   appendR   t   syst   stderrR0   R/   R   t   stript   close(   R1   R   t   string_onlyt   seqlistt   seqnameR   t   seqdatat   fht   thislinet   seqnewt   et	   cleanlineR4   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt	   readFASTAr   sD    	"	 "
c         C€  s   | j  d |  j ƒ  d ƒ |  j ƒ  } |  j ƒ  d d d } xG t | ƒ D]9 } d j | | d | d d !ƒ d } | j  | ƒ qL Wd S(   s:   Write one sequence in FASTA format to an already open fileR6   s   
i   i<   R   N(   t   writeR   R!   R$   t   rangeR   (   R   t
   filehandleR	   t   linest   it	   lineofseq(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   _writeOneFASTAŸ   s    &c         C€  s‘   t  | d ƒ } t |  t ƒ r. t |  | ƒ nK xH |  D]@ } t | t ƒ rZ t | | ƒ q5 t j d | j ƒ  d IJq5 W| j ƒ  | j ƒ  d S(   sE   Write a list (or a single) of sequences to a file in the FASTA formatt   ws   Warning: could not write s    (ignored).N(	   R-   R   R   RN   R:   R;   R   t   flushR=   (   R   R1   RB   t   seq(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt
   writeFASTAª   s    
t   Distribc           B€  sh   e  Z d  Z d d „ Z d „  Z d d „ Z d „  Z d „  Z d d „ Z	 d „  Z
 d	 „  Z d
 „  Z RS(   sX   Class for storing a multinomial probability distribution over the symbols in an alphabetg        c         C€  sN   | |  _  | |  j  j ƒ  |  _ g  t |  j  j ƒ  ƒ D] } | ^ q5 |  _ d  S(   N(   R   R$   t   totRI   t   cnt(   R   R   t   pseudo_countt   _(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR   ¼   s    	c         C€  sC   t  |  j ƒ } t j |  j | ƒ | _ t j |  j | ƒ | _ | S(   N(   RS   R   t   copyt   deepcopyRT   RU   (   R   t   memot   dup(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   __deepcopy__Á   s    c         C€  sg   | d k r |  j j ƒ  } n  xB | D]: } |  j j | ƒ } |  j | c d 7<|  j d 7_ q% Wd S(   s    Count an observation of a symbolg      ð?i   N(   R   R   R   t   getIndexRU   RT   (   R   t   symst   symt   idx(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   countÇ   s    c         C€  s   |  j  j ƒ  s t d ƒ ‚ n  |  j  j ƒ  } g  } xM t t | ƒ ƒ D]9 } | | } | d k rk | } n  | j |  j | ƒ qF W| |  _ |  S(   s;   Complement the counts, throw an error if this is impossiblesG   Attempt to complement a Distrib based on a non-complementable alphabet.N(	   R   t   isComplementableR   t   getComplementsRI   R#   R   R9   RU   (   R   t   comst	   new_countR`   t   cidx(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt
   complementÐ   s    
		c         C€  s8   d |  _  g  t |  j j ƒ  ƒ D] } d ^ q |  _ d S(   s2   Reset the distribution, that is, restart counting.i    g        N(   RT   RI   R   R$   RU   (   R   RW   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   resetß   s    	c         C€  ss   |  j  d k ro | d k rH t g  |  j D] } | |  j  ^ q( ƒ } | S|  j j | ƒ } |  j | |  j  Sn  d S(   s£   Determine the probability distribution from the current counts.
        The order in which probabilities are given follow the order of the symbols in the alphabet.i    N(   RT   R   R   RU   R   R]   (   R   R_   t   yt   freqR`   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getFreqä   s    )c         C€  sl   d j  g  |  j j ƒ  D] } d | ^ q ƒ g } | j d j  g  t j |  ƒ D] } d | ^ qN ƒ ƒ | S(   s_   Retrieve the probabilites for all symbols and return as a pretty table (a list of text strings)R   s   %4s s   %3.2f (   R   R   R   R9   RS   Rk   (   R   t   st   tableRi   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   prettyð   s    26c         C€  s   |  j  j ƒ  S(   sM   Get the symbols in the alphabet in the same order as probabilities are given.(   R   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR   ö   s    c         C€  s   |  j  S(   s8   Get the alphabet over which the distribution is defined.(   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR   ú   s    N(   R*   R+   R,   R   R\   R   Ra   Rg   Rh   Rk   Rn   R   R   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyRS   º   s   						t   Motifc           B€  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s±    Sequence motif class--defining a pattern that can be searched in sequences.
    This class is not intended for direct use. Instead use and develop sub-classes (see below).
    c         C€  s   d |  _  | |  _ d  S(   Ni    (   R#   R   (   R   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR     s    	c         C€  s   |  j  S(   s   Get the length of the motif(   R#   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR$     s    c         C€  s   |  j  S(   s*   Get the alphabet that is used in the motif(   R   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR     s    c         C€  s:   | } t  | ƒ t k r' | j ƒ  } n  |  j ƒ  j | ƒ S(   s4   Check if the sequence can be processed by this motif(   R   R   R"   R   R   (   R   t   seqstrt   mystr(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt
   isAlphabet  s    (   R*   R+   R,   R   R$   R   Rr   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyRo      s
   			t   RegExpc           B€  s    e  Z d  Z d „  Z d „  Z RS(   sG   A motif class that defines the pattern in terms of a regular expressionc         C€  s&   t  j |  | ƒ t j | ƒ |  _ d  S(   N(   Ro   R   t   ret   compilet   pattern(   R   R   t	   re_string(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR     s    c         c€  s¢   | } t  | ƒ t k	 r- t | |  j ƒ } n  | j ƒ  } t j |  | ƒ sd t d | j ƒ  ƒ ‚ n  x7 t j	 |  j
 | ƒ D]  } | j ƒ  | j ƒ  d f Vqz Wd S(   sB  Find matches to the motif in a specified sequence.
        The method is a generator, hence subsequent hits can be retrieved using next().
        The returned result is a tuple (position, match-sequence, score), where score is
        always 1.0 since a regular expression is either true or false (not returned).
        s)   Motif alphabet is not valid for sequence g      ð?N(   R   R   R   R"   Ro   Rr   R   R   Rt   t   finditerRv   t   startt   group(   R   RQ   t   myseqRq   t   m(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   match  s    (   R*   R+   R,   R   R}   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyRs     s   	t   NAt   LOGOTYPEt   12t   FONTSIZEt   TITLEFONTSIZEt   6t   SMALLFONTSIZEs   0.9t	   TOPMARGINt   BOTTOMMARGINt   truet   YAXISt   bitst
   YAXISLABELR   t
   XAXISLABELt   TITLEs   1.0t   ERRORBARFRACTIONt   falset
   SHOWINGBOXs   2.0t   BARBITSt   1t   TICBITSt   SHOWENDSt	   NUMBERINGt   OUTLINEt   PWMc           B€  s˜   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d d d „ Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d d „ Z e j ƒ  d „ Z RS(   sã   This motif subclass defines a pattern in terms of a position weight matrix.
    An alphabet must be provided. A pseudo-count to be added to each count is
    optional.  A uniform background distribution is used by default.
    c         C€  sB   t  j |  | ƒ t | ƒ |  _ |  j j | j ƒ  ƒ d |  _ d  S(   Ni    (   Ro   R   RS   t
   backgroundRa   R   t   nsites(   R   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR   H  s    g        c         C€  sh  d |  _  t | ƒ |  _ g  } x¸ | D]° } t | ƒ t k	 rX t | t j |  ƒ ƒ } n+ t j |  | ƒ sƒ t d | j	 ƒ  ƒ ‚ n  |  j  d k r¤ | j
 ƒ  |  _  n$ |  j  | j
 ƒ  k rÈ t d ƒ ‚ n  | j | ƒ q% Wg  t |  j  ƒ D] } t t j |  ƒ | ƒ ^ qé |  _ xE t |  j  ƒ D]4 } x+ | D]# } |  j | j | j | ƒ ƒ q-Wq W|  j  |  _ d S(   s¦   Set the probabilities in the PWM from an alignment.
        The alignment is a list of equal-length strings (see readStrings), OR
        a list of Sequence.
        iÿÿÿÿs)   Motif alphabet is not valid for sequence s.   Sequences in alignment are not of equal lengthN(   t   colsR#   R˜   R   R   Ro   R   Rr   R   R   R$   R9   RI   RS   t   countsRa   R'   (   R   t   alignedRV   t   seqsRl   RW   t   c(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   setFromAlignmentN  s$    	7%c         C€  s´   d } t  |  j ƒ d } xt | | k  r |  j | } |  j | |  j | <| |  j | <|  j | j ƒ  |  j | j ƒ  | d 7} | d 8} q W| | k r° |  j | j ƒ  n  |  S(   s   Reverse complement the PWMi    i   (   R#   Rš   Rg   (   R   RL   t   jt   temp(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   reverseComplementp  s    
c         C€  s   |  j  S(   s)   Get the number of sites that made the PWM(   R˜   (   R   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt	   getNSites€  s    c         C€  s7   | j  ƒ  t j  |  ƒ k s* t d ƒ ‚ n  | |  _ d S(   s   Set the background distributions   Incompatible alphabetsN(   R   Ro   R   R—   (   R   t   distrib(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   setBackground„  s    c         C€  sD   | d k r, g  |  j D] } | j ƒ  ^ q S|  j | j | ƒ Sd S(   sG   Get the probabilities for all positions in the PWM (a list of Distribs)N(   R   Rš   Rk   (   R   t   colR_   Ri   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyRk   Š  s     c         C€  sQ   g  } xD t  j |  ƒ D]3 } | j d j g  | D] } d | ^ q/ ƒ ƒ q W| S(   sa   Retrieve the probabilites for all positions in the PWM as a pretty table (a list of text strings)R   s   %8.6f (   R–   Rk   R9   R   (   R   Rm   t   rowRi   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyRn   ‘  s    1c      
   C€  s   g  } xr t  j |  ƒ D]a } | j d j g  t t | ƒ ƒ D]2 } d t j | | | | ƒ t j d ƒ ^ q; ƒ ƒ q W| S(   sf   Retrieve the (base-2) log-odds for all positions in the PWM as a pretty table (a list of text strings)R   s   %8.6f i   (   R–   Rk   R9   R   RI   R#   t   matht   log(   R   t   bkgRm   R¦   RL   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   logoddsPretty™  s    _c         C€  s©   d } t  j |  ƒ j ƒ  } x‡ t |  j ƒ D]v } | d } |  j | j | ƒ } xC | d D]7 } |  j | j | ƒ } | | k r\ | } | } q\ q\ W| | 7} q+ W| S(   s¢   
        Get the consensus sequence corresponding to a PWM.
        Consensus sequence is the letter in each column
        with the highest probability.
        R   i    i   (   Ro   R   R   RI   R™   Rš   Rk   (   R   t	   consensusR   t   post   best_lettert   best_pt   lettert   p(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   consensus_sequence£  s    
c         C€  s†   g  } xy t  |  j ƒ D]h } g  } xL t j |  ƒ j ƒ  D]5 } |  j | j | ƒ } | d k r8 | | 7} q8 q8 W| j | ƒ q W| S(   s¢   
        Get the consensus corresponding to a PWM.
        Consensus at each column of motif is a list of
        characters with non-zero probabilities.
        i    (   RI   R™   Ro   R   R   Rš   Rk   R9   (   R   R«   R¬   t   matchesR¯   R°   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR«   ·  s    c         C€  s   d } | j  ƒ  | | |  j !} xv t t | ƒ ƒ D]b } |  j | j | | ƒ } | d k rh d } n  t j | |  j j | | ƒ ƒ } | | 7} q3 W| S(   s[   Score this particular list of symbols using the PFM (background needs to be set separately)g        i    g-Cëâ6?(	   R!   R™   RI   R#   Rš   Rk   R§   R¨   R—   (   R   RQ   Ry   t   sumRA   R¬   t   qt   logodds(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   getScoreÈ  s    	#iöÿÿÿc         c€  sÊ   | } t  | ƒ t k	 r- t | |  j ƒ } n  t j |  | ƒ sX t d | j ƒ  ƒ ‚ n  xk t | j ƒ  |  j	 ƒ D]P } t
 j |  | | ƒ } | | k rr | d j | j | |  j	 ƒ ƒ | f Vqr qr Wd S(   s6  Find matches to the motif in a specified sequence.
        The method is a generator, hence subsequent hits can be retrieved using next().
        The returned result is a tuple (position, match-sequence, score).
        The optional parameter _LOG0 specifies a lower bound on reported logodds scores.
        s)   Motif alphabet is not valid for sequence R   N(   R   R   R   Ro   Rr   R   R   RI   R$   R™   R–   R¶   R   R'   (   R   RQ   t   _LOG0R{   R¬   t   score(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR}   Ô  s     c   "      C€  s  d } d } t  j | | ƒ } t  j | | ƒ } d d }	 d }
 |  j ƒ  d d } t d | ƒ } t |
 |	 ƒ } t | |	 ƒ } t j ƒ  } | | d	 <| | d
 <t |
 ƒ | d <t | ƒ | d <| d | | d <t |  j ƒ  ƒ | d <t | ƒ | d <t | ƒ | d <t |
 ƒ | d <t | d ƒ Û} t	 j
 d ƒ } xÂ| D]º} d } | j | ƒ } xv| rÖ| | j ƒ  k  rœ| | | j ƒ  !} | j | ƒ n  | j d ƒ } | d k r!| j d ƒ xJt |  j ƒ  ƒ D]6} | j d j | d ƒ ƒ g  } d } d } xX |  j ƒ  j ƒ  D]D } |  j | | ƒ } | d k rFqn  | | t j | d ƒ 8} qW| | } xU |  j ƒ  j ƒ  D]A } |  j | | ƒ } | d k r«qn  | j | | | f ƒ qW| j ƒ  x- | D]% \ } } | j d j | | ƒ ƒ q×W| j d ƒ q×W| j d ƒ n”| d k r| j d ƒ x’ |  j ƒ  j ƒ  D]~ } |  j ƒ  j | ƒ } t | d ?d  @ƒ d! } t | d" ?d  @ƒ d! }  t | d  @ƒ d! }! | j d# j | | |  |! ƒ ƒ qMW| j d$ ƒ x’ |  j ƒ  j ƒ  D]~ } |  j ƒ  j | ƒ } t | d ?d  @ƒ d! } t | d" ?d  @ƒ d! }  t | d  @ƒ d! }! | j d% j | | |  |! ƒ ƒ qïW| j d& ƒ n4 | | k r¡| j | | ƒ n t d' | d( ƒ ‚ | j ƒ  } | j | | ƒ } qaW| t | ƒ k  rC| j | | ƒ qCqCWWd) QXd) S(*   s$   Write out a DNA motif to EPS format.s   %d.%m.%Y %H:%Ms   %d.%m.%Y %H:%M:%S %Zg      R@gR¸…ëQ@g      @gš™™™™™é?i   i   t   CREATORt   CREATIONDATEt
   LOGOHEIGHTt	   LOGOWIDTHt    t	   FINEPRINTt   CHARSPERLINEt   BOUNDINGHEIGHTt   BOUNDINGWIDTHt   LOGOLINEHEIGHTt   rs   \{\$([A-Z]+)\}i    i   t   DATAs   
StartLine
s   ({0:d}) startstack
s    {0:f} ({1:s}) numchar
s
   endstack

s   EndLine
t	   COLORDICTs   /fullColourDict <<
i   iÿ   g     ào@i   s    ({0:s})  [{1:f} {2:f} {3:f}]
s   >> def
/mutedColourDict <<
s$    ({0:s})  [{1:.4f} {2:.4f} {3:.4f}]
s   >> def
s   Unknown variable "s   " in EPS templateN(   t   timet   strftimeR$   t   mint   intt   _eps_defaultsRX   R   R-   Rt   Ru   t   searchRy   RH   Rz   RI   t   formatR   R   Rk   R§   R¨   R9   t   sortt	   getColourt   roundt   getMutedColourR   t   endR#   ("   R   t   programt   template_filet   eps_fht	   timestampt
   small_dfmtt	   full_dfmtt
   small_datet	   full_datet   points_per_cmt   heightt   widtht   points_heightt   points_widtht   defaultst   template_fht   m_varR4   t   lastR}   t   prevt   keyR¬   t   stackt   alpha_ict   hR_   Rj   t   stack_ict   symht   rgbRÃ   t   gt   b(    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   writeEPSä  s     


	



N(   R*   R+   R,   R   Rž   R¡   R¢   R¤   R   Rk   Rn   Rª   R±   R«   R¶   R}   RÆ   t	   localtimeRí   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyR–   C  s   	"					
			c         C€  sG   | j  t ƒ } x1 t t |  ƒ ƒ D] } |  | j | ƒ |  | <q" W|  S(   s   Convert aliases to prime symbol and ambiguous to wildcard
       in each of a list of strings.  Changes are made in place.
    (   t
   translatort   FalseRI   R#   t	   translate(   t   stringst   alpht   msRL   (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   convert_ambigs<  s    t   __main__t   DNAt   tmp0s   pos: %d pat: %s %4.2fi    i   i   ((   t
   __future__R    RX   t   stringR:   R   t   objectR   R5   R   Rð   RG   RN   RR   RS   Ro   Rt   Rs   R§   RÆ   RÊ   R–   Rõ   R*   R   R   Rœ   t   alnt   pwmRž   Rn   R¦   Rl   R   R$   R   R   t   regexpR}   R|   (    (    (    sD   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/sequence.pyt   <module>   s\   $^	-		F
ù		%"