dinucleotide count and frequency
<p>Im trying to find the dinuc count and frequencies from a sequence in a text file, but my code is only outputting single nucleotide counts.</p>

<pre><code>e = "ecoli.txt"

ecnt = {}

with open(e) as seq:
    for line in seq:
        for word in line.split():
            for i in range(len(seqr)):
                dinuc = (seqr[i] + seqr[i:i+2])
                for dinuc in seqr:
                    if dinuc in ecnt:
                        ecnt[dinuc] += 1
                    else:
                        ecnt[dinuc] = 1  

for x,y in ecnt.items():
    print(x, y)
</code></pre>

<p>Sample input: "AAATTTCGTCGTTGCCC"</p>

<p>Sample output:
AA:2
TT:3
TC:2
CG:2
GT:2
GC:1
CC:2</p>

<p>Right now, Im only getting single nucleotides for my output:</p>

<p>C 83550600
A 60342100
T 88192300
G 92834000</p>

<p>For the nucleotides that repeat i.e. "AAA", the count has to return all possible combinations of consecutive 'AA', so the output should be 2 rather than 1. It doesnt matter what order the dinucleotides are listed, I just need all combinations, and for the code to return the correct count for the repeated nucleotides. I was asking my TA and she said that my only problem was getting my 'for' loop to add the dinucleotides to my dictionary, and I think my range may or may not be wrong. The file is a really big one, so the sequence is split up into lines.</p>

<p>Thank you so much in advance!!!</p>

<p>I took a look at your code and found several things that you might want to take a look at. </p>

<p>For testing my solution, since I did not have ecoli.txt, I generated one of my own with random nucleotides with the following function:</p>

<pre><code>import random
def write_random_sequence():
    out_file = open("ecoli.txt", "w")
    num_nts = 500
    nts_per_line = 80
    nts = []
    for i in range(num_nts):
        nt = random.choice(["A", "T", "C", "G"])
        nts.append(nt)
    lines = [nts[i:i+nts_per_line] for i in range(0, len(nts), nts_per_line)]
    for line in lines:
        out_file.write("".join(line) + "\n")
    out_file.close()
write_random_sequence()
</code></pre>

<p>Notice that this file has a single sequence of 500 nucleotides separated into lines of 80 nucleotides each. In order to count dinucleotides where you have the first nucleotide at the end of one line and the second nucleotide at the start of the next line, we need to merge all of these separate lines into a single string, without spaces. Let's do that first:</p>

<pre><code>seq = ""
with open("ecoli.txt", "r") as seq_data:
    for line in seq_data:
        seq += line.strip()
</code></pre>

<p>Try printing out "seq" and notice that it should be one giant string containing all of the nucleotides. Next, we need to find the dinucleotides in the sequence string. We can do this using slicing, which I see you tried. So for each position in the string, we look at both the current nucleotide and the one after it.</p>

<pre><code>for i in range(len(seq)-1):#note the -1
    dinuc = seq[i:i+2]
</code></pre>

<p>We can then do the counting of the nucleotides and storage of them in a dictionary "ecnt" very much like you had. The final code looks like this:</p>

<pre><code>ecnt = {}
seq = ""
with open("ecoli.txt", "r") as seq_data:
    for line in seq_data:
        seq += line.strip()
for i in range(len(seq)-1):
    dinuc = seq[i:i+2]
    if dinuc in ecnt:
        ecnt[dinuc] += 1
    else:
        ecnt[dinuc] = 1
print ecnt
</code></pre>

python dictionary bioinformatics sequences