I have to find the total number of each nucleotide in the sequence. For example,
ID: 3885297 • Letter: I
Question
I have to find the total number of each nucleotide in the sequence. For example, I have to find how many G bases appear in the sequence ATTGGCG. So this is my code:
def count_ntd(ntd, dna_seq):
"""
Counts the number of times a nucleotide appears in a sequence.
"""
total = 0
for ntd in dna_seq:
if ntd is base:
total += 1
return total
This actually gave me the right answer, but I couldn't pass assertions.
assert_equal(count_ntd('T', 'CTTAATGGTT'), 5)
assert_equal(count_ntd('G', 'GATAGACAGA'), 3)
assert_equal(count_ntd('C', 'TTCTTCAGAG'), 2)
assert_equal(count_ntd('A', 'AGGGGCCTCT'), 1)
Please tell me what I am doing wrong here. I got an error message that the calculations were not made correct. However, it works for A, C, T but not G.
Explanation / Answer
I have made the following changes..To get the ouput i have made it to print .
def count_ntd(ntd, dna_seq):
"""
Counts the number of times a nucleotide appears in a sequence.
"""
total = 0
for key in dna_seq:
if key == ntd:
total += 1
return total
print(count_ntd('T', 'CTTAATGGTT'))
print(count_ntd('G', 'GATAGACAGA'))
print(count_ntd('C', 'TTCTTCAGAG'))
print(count_ntd('A', 'AGGGGCCTCT'))