0

I have a sample that looks like

tt = [AAT, AAA, ATGA]

Calculate frequency of occurrence of each character in the element and square it

eg. AAT => 2 A's and 1 T ===> p = (2/3)^2 + (1/3)^2 Here, p = 0.55555

Then, q = 1 - p Here, q = 0.44444

Altough I am using float, I am getting rounded-off values for q

 Output: 
 [1.0, 0.0, 1.0]

I need the output to be :

 Desired Output:
 [0.44444,0.0,0.625]

My program

## Calculatting 1-p
q = []
for idx, element in enumerate(tt):
   count_dict = {}
   square_dict = {}
   for base in list(element):
       if base in count_dict:
           count_dict[base] += 1
       else:
           count_dict[base] = 1
   for allele in count_dict:
       square_freq = (count_dict[allele] / nn[idx])**2  #nn is the number of characters in the element, here it is a list of [3,3,4]
       square_dict[allele] = square_freq        
   pf = 0.0
   for i in square_dict:
       pf += square_dict[i]   # pf --> pi^2 + pj^2...pn^2
   het = 1-pf                    
   q.append(het)
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
biogeek
  • 197
  • 3
  • 8
  • Are you using Python 2? – juanpa.arrivillaga Oct 10 '16 at 01:24
  • Yes I am using Python 2.7.12 – biogeek Oct 10 '16 at 01:27
  • Well, Python 2 uses integer division for ` / `. So 2/3 wil give you 0, 3/2 will give you 1. You need to convert one of them to floats to get regular division. – juanpa.arrivillaga Oct 10 '16 at 01:29
  • That being said, your code seems overly complex. You don't need to use `for base in list(element)` since strings are already iterables. Also, the only reason you use `idx` is to index into another list, `nn` to get the number of characters in your element, but why not just use `len(element)`? Also, you should use `Counter` from `collections` – juanpa.arrivillaga Oct 10 '16 at 01:34
  • I don't understand where I need to put float against. Do I put float(q)? It does not work. – biogeek Oct 10 '16 at 03:13
  • I figured it out. I need to put float(nn). Problem solved. Thanks for your inputs – biogeek Oct 10 '16 at 03:15

0 Answers0