Python Frequency Map code help
0
0
Entering edit mode
6.6 years ago
oludhe ▴ 90

Hi everyone,

I need some help troubleshooting some code on Python

I am a beginner at python, have only been learning for about two weeks, and without a computer science/programming backgroun so please take this into account when explaining. My problem may be very simple, but I don't know how to figure out the solution.

I have a code which slides a window down a particular pattern of DNA to find 3-mers and their frequency in the given pattern. This code is called FrequencyMap - and if anybody has taken the bioinformatics specialisation on coursera by UCSD, you may be familiar with this problem.

My code is below:

def FrequencyMap(Text, k):
    # your code here
    freq = {}
    n=len(Text)
    for i in range (n-k+1):
        Pattern = Text [i:i+k]
        if Pattern not in freq:
            freq [Pattern] = 1 # if a pattern found is not already  in the dictionary freq{}, it is assigned a value of 1 and added to the list
        else:
            freq [Pattern] +=1 # however, if the pattern is already in the dictionary, its value should go up by 1 (so if it has been found, it is initially given a pattern of 1, and then this adds another 1 if it is found again
    return freq

When I print the following:

print (FrequencyMap ("CGATATATCCATAG", 3))

I get a double-printed output:

{'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1}
{'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1}

Rather than just the one:

{'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1}

I cannot seem to pinpoint the key to this problem, so if you can, please help me.

Also, if there is any other problem with my code and logic, your help would be most welcome.

Thanks!

Tom

genome gene • 3.6k views
ADD COMMENT
1
Entering edit mode

Thanks for the indent.

Works fine on my computer. I think something is missing somewhere in your example.

My bet is that you print something twice, maybe are you printing "freq" in your function or your "print (FrequencyMap ("CGATATATCCATAG", 3))" is called twice.

Could you please take a screenshot of your screen with your text editor (python code), and your terminal after running your command. Upload the screenshot on https://fr.imgbb.com/ and share the link below please.

ADD REPLY
0
Entering edit mode

Hi, thanks for your help, I think I found out the problem. I was working on an interactive python site which automatically printed my result. When I added code which instructed it to print, it just added it onto the already automatically-printing function!

ADD REPLY
0
Entering edit mode

A python code without indent is a bit hard to read. Just like in python you can add 4 spaces before each code lines to indent your code here. Try to edit your code please, that would be helpful

Works well for me I really need the indent.

ADD REPLY
0
Entering edit mode

Sorry, it seemed as though the formatting was carried over when I copied/pasted, but it seems the text editor did not keep the formatting once I had posted. I didn't realise I had to use the code sample button.

def FrequencyMap(Text, k):
# your code here
freq = {}
n=len(Text)
for i in range (n-k+1):
    Pattern = Text [i:i+k]
    if Pattern not in freq:
        freq [Pattern] = 1
    else:
        freq [Pattern] +=1
return freq


Input: print FrequencyMap ("CGATATATCCATAG", 3)

Output:
{'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1} 
{'CGA': 1, 'GAT': 1, 'ATA': 3, 'TAT': 2, 'ATC': 1, 'TCC': 1, 'CCA': 1, 'CAT': 1, 'TAG': 1}
ADD REPLY

Login before adding your answer.

Traffic: 2711 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6