pythonfunctionrosalind

Beginner Python script for calculating GC content in DNA sequence


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...)


Solution

  • Shouldn't:

    print cg_content

    read

    print gc_content?

    As for the other snippet of code, your loop says

    if i == "C" or "G":

    This is evaluating "G" to true every time and thus running the if statement as true.

    Instead, it should read

    if i == "C" or i=="G":

    Also, you don't need that else statement.

    Hope this helps. Let us know how it goes.

    Abdul Sattar