title: Entropy of sequences
body: <p>Can some help me to rectify my code?
  I have 3 sequences </p>
  
  <pre><code> seq1='AATC'
   seq2='GCCT'
   seq3='ATCA'
  </code></pre>
  
  <p>I want to calculate the entropy of three sequences, its formula is:</p>
  
  <pre><code>Entropy (A[:][i])= - ∑  p_ia log( p_ia)
  p_ia = C_ia /∑  C_ia'
  </code></pre>
  
  <p>where p_ia is the probability of letter a in column i ,and C_ia is the number of letter a in the column A[:][i]</p>
  
  <p>example: from the above 3 sequences: first column:a' take the values C , A and C, here C_ia'= 3 , if a='C' C_ia=2 so p_ia=2 /3,</p>
  
  <p>if a='A' C_ia=1 so p_ia=1 /3</p>
  
  <pre><code>Entropy (A[:][0])=-(2/3 log (2/3)+1/3 log(1/3))
  </code></pre>
  
  <p>the same thing for the rest of columns.
  I have tried this:
  this function calculate the  occurence (frequency) of each letter in a given string and l is the length of string it means <code>l=∑  C_ia'.</code></p>
  
  <pre><code>def occ(source):
      occ={}
      l=0
      for e in source:
          l +=1
          if e not in occ:
              occ[e]=0
          occ[e]+=1
      return (l,occ)
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>&gt;&gt;&gt; source='aaazzzedc'
  &gt;&gt;&gt; occ(source)
  (9, {'z': 3, 'c': 1, 'a': 3, 'd': 1, 'e': 1})
  </code></pre>
  
  <p>then, this is the function of entropy:</p>
  
  <pre><code>def entropy(occ,l):
      elist=[]
      for v in occ.values():
          c=v/l
          elist.append(-c *math.log(c, 2))
      return sum(elist)
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>&gt;&gt;&gt; (l,h)=occ(source)
  &gt;&gt;&gt; entropy(h,l)
  2.113283334294875
  </code></pre>
  
  <p>OK.
  this is for simple example, but for me I want to apply it on columns of sequences.</p>
  
  <pre><code>&gt;&gt;&gt; source=[occ(t) for t in zip(seq1,seq2,seq3)]
  &gt;&gt;&gt; source
  [(3, {'A': 2, 'G': 1}), (3, {'C': 1, 'A': 1, 'T': 1}), (3, {'C': 2, 'T': 1}), (3, {'C': 1, 'A': 1, 'T': 1})]
  </code></pre>
  
  <p>how can I calculate the entropy of this sequences?
  this gives by the first column:</p>
  
  <pre><code>(3, {'A': 2, 'G': 1})
  2/3 * log(2/3)+ 1/3 * log (1/3)
  </code></pre>
  
  <p>How can I write the entropy of all columns?</p>