Beginner Python script for calculating GC content in DNA sequence

jstewartmitchel picture jstewartmitchel · Jun 4, 2013 · Viewed 21.2k times · Source

I'm trying to calculate the GC content (in %) of a DNA sequence for a Rosalind question. I have the following code, but it returns 0, or only the number of G's alone or C's alone (no percentage).

x = raw_input("Sequence?:").upper()
total = len(x)
c = x.count("C")
g = x.count("G")

gc_total = g+c

gc_content = gc_total/total

print gc_content

I also tried this, just to get a count of G's and C's, and not the percentage, but it just returns a count of the entire string:

x = raw_input("Sequence?:").upper()
def gc(n):
    count = 0
    for i in n:
        if i == "C" or "G":
            count = count + 1
        else:
            count = count
    return count
gc(x)

EDIT: I fixed the typo in the print statement in the first example of code. That wasn't the problem, I just pasted the wrong snippet of code (there were many attempts...)

Answer

Owen picture Owen · Jun 4, 2013

Your problem is that you are performming integer division, not floating point division.

Try

gc_content = gc_total / float(total)