How to calculate counts and frequencies for pairs in list of lists?
<p>Bases refers to A,T,G and C</p>

<pre><code>sample = [['CGG','ATT'],['GCGC','TAAA']]

# Note on fragility of data: Each element can only be made up only 2 of the 4 bases.  
# [['CGG' ==&gt; Only C and G,'ATT' ==&gt; Only A and T],['GCGC'==&gt; Only C and G,'TAAA' ==&gt; Only T and A]]
# Elements like "ATGG" are not present in the data as the have more than 3 different types of bases
</code></pre>

<p>Consider the first pair : ['CGG','ATT']</p>

<ol>
<li><p>Calculate frequency of each base in the pairs separately:</p>

<p>CGG => (C = 1/3, G = 2/3)
 ATT => (A = 1/3, T = 2/3)</p></li>
<li><p>Calculate frequency of occurrence of combination of bases in the pairs. Here, the combinations are 'CA' and 'GT' (Notice, order of the base matters. It is not 'CA','AC','GT' and 'TG'. Just only 'CA' and 'GT'). </p>

<p>Pairs => (CA = 1/3, GT = 2/3) </p></li>
<li><p>Calculate float(a) = (freq of Pairs) - ((freq of C in CGG) * (freq of A in ATT))</p>

<p>Eg in CA pairs, float (a) = (freq of CA pairs) - ((freq of C in CGG) * (freq of A in ATT))</p>

<p>Output a = (1/3) - ((1/3) * (1/3)) = 0.222222</p></li>
</ol>

<p>Calculating "a" for any one combination (either CA pair or GT pair)</p>

<p>NOTE: If the pair is AAAC and CCCA, the freq of C would it be 1/4, i.e. it is the frequency of the base over one of the pairs</p>

<ol start="4">
<li><p>Calculate b
 float (b) = (float(a)^2)/ (freq of C in CGG) * (freq G in CGG) * (freq A in ATT) * (freq of T in ATT)</p>

<pre><code>Output b = 1
</code></pre></li>
</ol>

<p>Do this for the entire list</p>

<pre><code>   Final Output a = [0.2222, - 0.125]
                b = [1, 0.3333]
</code></pre>

<p>This code has been adapted from <a href="http://stackoverflow.com/questions/40066439/how-to-calculate-frequency-of-elements-for-pairwise-comparisons-of-lists-in-pyth?noredirect=1#comment67422112_40066439">this answer</a>. Please note that there are subtle differences in the two questions and they are NOT the same, in the approach to the problem.  </p>

<p>However, I am unable to get this code to run. I get the following error:
   for pair, count in i:
TypeError: 'int' object is not iterable</p>

<pre><code>#Count individual bases.

sample4 = [['CGG','ATT'],['GCGC','TAAA']]
base_counter = Counter()
for i in enumerate(sample4):
    for pair, count in i:
        base_counter[pair[0]] += count
        base_counter[pair[1]] += count
        print base_counter

# Get the total for each base.
total_count = sum(base_counter.values())

# Convert counts to frequencies.
base_freq = {}
for base, count in base_counter.items():
    base_freq[base] = count / total_count
# Not sure how to write a code to count the number of pairs (Step 2)
# Let's say the counts have been stored in pair_counts

# Examine a pair from the two unique pairs to calculate float_a.
for i in enumerate(sample4):
    float(a) = (pair_count[pair] / sum(pair_count.values())) - (base_freq[pair[0]] * base_freq[pair[1]])

# Step 7!
for i in enumerate(sample4):
    float_b = float_a / float(base_freq[0][0] * base_freq[0][1] * base_freq[1][0] * base_freq[1][1])
</code></pre>

<p>You are not really using <code>Counter</code> any different than a plain <code>dict</code>. Try something like the following approach:</p>

<pre><code>&gt;&gt;&gt; sample = [['CGG','ATT'],['GCGC','TAAA']]
&gt;&gt;&gt; from collections import Counter
&gt;&gt;&gt; base_counts = [[Counter(base) for base in sub] for sub in sample]
&gt;&gt;&gt; base_counts
[[Counter({'G': 2, 'C': 1}), Counter({'T': 2, 'A': 1})], [Counter({'G': 2, 'C': 2}), Counter({'A': 3, 'T': 1})]]
</code></pre>

<p>Now you can continue with a functional approach using nested comprehensions to transform your data*:</p>

<pre><code>&gt;&gt;&gt; base_freqs = [[{k_v[0]:k_v[1]/len(bases[i]) for i,k_v in enumerate(count.items())} for count in counts] 
...               for counts, bases in zip(base_counts, sample)]
&gt;&gt;&gt; 
&gt;&gt;&gt; base_freqs
[[{'G': 0.6666666666666666, 'C': 0.3333333333333333}, {'A': 0.3333333333333333, 'T': 0.6666666666666666}], [{'G': 0.5, 'C': 0.5}, {'A': 0.75, 'T': 0.25}]]
&gt;&gt;&gt; 
</code></pre>

<p>*Note, some people do not like big, nested comprehensions like that. I think it's fine as long as you are sticking to functional constructs and not mutating data structures inside your comprehensions. I actually find it very expressive. Others disagree vehemently. You can always unfold that code into nested for-loops.</p>

<p>Anyway, you can then work the same thing with the pairs. First:</p>

<pre><code>&gt;&gt;&gt; pairs = [list(zip(*bases)) for bases in sample]
&gt;&gt;&gt; pairs
[[('C', 'A'), ('G', 'T'), ('G', 'T')], [('G', 'T'), ('C', 'A'), ('G', 'A'), ('C', 'A')]]
&gt;&gt;&gt; pair_counts = [Counter(base_pair) for base_pair in pairs]
&gt;&gt;&gt; pair_counts
[Counter({('G', 'T'): 2, ('C', 'A'): 1}), Counter({('C', 'A'): 2, ('G', 'T'): 1, ('G', 'A'): 1})]
&gt;&gt;&gt; 
</code></pre>

<p>Now, here it is easier to not use comprehensions so we don't have to calculate <code>total</code> more than once:</p>

<pre><code>&gt;&gt;&gt; pair_freq = []
&gt;&gt;&gt; for count in pair_counts:
...   total = sum(count.values())
...   pair_freq.append({k:c/total for k,c in count.items()})
... 
&gt;&gt;&gt; pair_freq
[{('C', 'A'): 0.3333333333333333, ('G', 'T'): 0.6666666666666666}, {('G', 'T'): 0.25, ('C', 'A'): 0.5, ('G', 'A'): 0.25}]
&gt;&gt;&gt; 
</code></pre>

python list dictionary count