ó
J,TXc           @   sº   d  d l  Z  d  d l Z d  d l Z d  d l Z e j j d ƒ d  d l Z d  d l Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z e j ƒ  d „ Z d	 „  Z e d
 k r¶ e ƒ  n  d S(   iÿÿÿÿNs@   /woldlab/castor/home/georgi/programs/meme_4.11.2/meme/lib/pythonc   	      C   s  | j  ƒ  } g  t | ƒ D] } d ^ q } g  t | ƒ D]% } g  t | ƒ D] } d ^ qK ^ q8 } t |  ƒ d k r÷ | j |  d ƒ } d | | <xb t t |  ƒ d ƒ D]G } | } | j |  | d ƒ } | | c d 7<| | | c d 7<q© Wn  | | f S(   Ni    i   (   t
   getFullLent   xranget   lent   getIndex(	   t   st   alpht   alent   it   nuclCntt   jt	   dinuclCntt   yt   x(    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   computeCounts   s    8
c         C   s¹   t  j  ƒ  } t | |  ƒ } d } xh t t | |  ƒ d ƒ D]L } | | |  | 7} | t | ƒ t | ƒ k  r= | |  | c d 8<| Sq= Wt | |  ƒ d } | |  | c d 8<| S(   Ni    i   (   t   randomt   sumR   R   t   float(   R   R
   R   t   zt   denomt	   numeratorR   (    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt
   chooseEdge   s    !c         C   sÛ   i  } x | D] } d | | <q Wx- |  D]% \ } } | | k r( d | | <q( q( WxR t  t | ƒ d ƒ D]: } x1 |  D]) \ } } | | d k ru d | | <qu qu Wqh Wx. | D]& } | | k r­ | | d k r­ t Sq­ Wt S(   Ni    i   (   R   R   t   Falset   True(   t   edgeListt   usedSymIt   lastSymIt   DR   t   at   bR   (    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   connectedToLast(   s    c   
      C   sÚ   t  |  | ƒ \ } } g  t t | ƒ ƒ D] } | | d k r( | ^ q( } | j |  d ƒ } t } xp | sÕ t j | ƒ } g  }	 x< | D]4 } | | k rˆ |	 j | t | | | ƒ f ƒ qˆ qˆ Wt	 |	 | | ƒ } qf W|	 S(   Ni    iÿÿÿÿ(
   R   R   R   R   R   t   copyt   deepcopyt   appendR   R   (
   R   R   R   R
   R   R   R   t   okt   countsR   (    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   eulerian8   s    5	&c         C   s    g  t  | j ƒ  ƒ D] } g  ^ q } t |  ƒ d k rœ | j |  d ƒ } xO t  t |  ƒ d ƒ D]4 } | } | j |  | d ƒ } | | j | ƒ qa Wn  | S(   Ni    i   (   R   R    R   R   R    (   R   R   R   t   outR   R   (    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   computeListH   s    %c   
      C   s`  t  |  ƒ d k r |  St |  | ƒ } t |  | ƒ } x% | D] \ } } | | j | ƒ q; Wx+ t t  | ƒ ƒ D] } t j | | ƒ qo Wx% | D] \ } } | | j | ƒ q‘ W| j |  d ƒ } | j	 | ƒ g } xM t t  |  ƒ d ƒ D]5 } | | j
 d ƒ }	 | j | j	 |	 ƒ ƒ |	 } qî W| j |  d ƒ }	 | j | j	 |	 ƒ ƒ t j | d ƒ S(   Ni   i    iÿÿÿÿt    (   R   R#   R%   t   removeR   R   t   shuffleR    R   t	   getSymbolt   popt   stringt   join(
   R   R   R   t   symIListR   R   t   prevSymIt   LR   t   symI(    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   dinuclShuffleR   s&    
c          C   s  d  }  d  } d } d } d t j d | | f } t t j ƒ d k rd t j | IJt j d ƒ n  d } d } x| t t j ƒ k  rt j | } | d k rã | d 7} y t j | }  Wqtt j | IJt j d ƒ qtXn‘| d k r1| d 7} y t j | } Wqtt j | IJt j d ƒ qtXnC| d k rˆ| d 7} y t j t j | ƒ } Wqtt j | IJt j d ƒ qtXnì | d k rß| d 7} y t j t j | ƒ } Wqtt j | IJt j d ƒ qtXn• | d	 k r-| d 7} y t j | } Wqtt j | IJt j d ƒ qtXnG | d
 k rVt j | IJt j d ƒ n t j d | IJt j d ƒ | d 7} qs W|  d  k r«t j | IJt j d ƒ n  | d  k rÉt j	 | ƒ } n t j
 ƒ  } t j | ƒ t j |  | ƒ }	 x” |	 D]Œ }
 |
 j ƒ  } |
 j ƒ  } xk t | ƒ D]] } t | | ƒ } | d k ret j d | | | f IJq&t j d | | | | f IJq&WqûWd  S(   Ni   s´  USAGE:
    %s [options]

    -f <filename>   file name (required)
    -t <tag>        added to shuffled sequence names
    -s <seed>       random seed; default: %d
    -c <n>          make <n> shuffled copies of each sequence; default: %d
    -a <filename>   alphabet file to use non-DNA alphabets
    -h              print this usage message

    Note that fasta-shuffle-letters also supports dinucleotide shuffling and is faster.
    i    R&   s   -fs   -ts   -ss   -cs   -as   -hs   Unknown command line argument: s   >%s
%ss	   >%s_%d
%s(   t   Nonet   syst   argvR   t   stderrt   exitR+   t   atoit   alphabett   loadFromFilet   dnaR   t   seedt   sequencet	   readFASTAt	   getStringt   getNamet   rangeR1   t   stdout(   t	   file_namet   alphabet_file_nameR;   t   copiest   usaget   tagR   t   argR   t   seqsR   t   seqt   namet   shuffledSeq(    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   mainm   sŠ     
   
   
   
   
     t   __main__(   R   R   R+   R3   t   pathR    R8   R<   R   R   R   R#   R%   R:   R1   RL   t   __name__(    (    (    sV   /woldlab/castor/home/georgi/programs/meme_4.11.2/scripts/fasta-dinucleotide-shuffle.pyt   <module>   s   0					
	X 