0

the content of my tasks are "Write functions counting the DNA sequence of codon " ATG "

I did something like that but I do not know what is wrong with that

def seq(dna):
    dna = dna.count("ATG")

print seq("ATGSDSGFAGEFRASFWET")

Please help me

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • So, are you getting an error, or...? – Kevin Jan 13 '16 at 14:46
  • You did not indent the function body. Otherwise, you should state as specificly as posslible what you expect to happen, and what happens instead. – MB-F Jan 13 '16 at 14:46
  • 1
    What is “counting the DNA sequence of codon ‘ATG’” supposed to mean? Count the frequency of the sequence? If so, then you cannot simply use the `count` method, it will give you wrong results because it will count frame-shifted “ATG” (e.g. “CATGTA” would return `1`, even though there’s no in-frame “ATG”). – Konrad Rudolph Jan 13 '16 at 14:47
  • @Kevin I only getting "None" – warszawiaczek Jan 13 '16 at 14:48
  • @KonradRudolph exactly – warszawiaczek Jan 13 '16 at 14:49
  • Use `return dna.count("ATG")` inside your function to return a value – Finwood Jan 13 '16 at 14:49
  • See also http://stackoverflow.com/q/12610184/1288408 – Modus Tollens Jan 13 '16 at 15:02
  • I've written many of these before... you could use a modulo to make sure your sequence is divisible by three and returns codons or amino acids as well in your function. It's more of a sanity check. – Sean Jan 13 '16 at 15:03

2 Answers2

1

1 - Strip your major string in a array of strings with size 3.

2 - Do an iteration in your array of strings and compare with string "ATG" - If the comparison is true, increment in 1 an int auxiliar variable

3 - Display the result of the auxiliar int variable

Victor Viola
  • 575
  • 1
  • 4
  • 14
  • 1
    That's not gonna work: it is possible that `ATG` starts halfway the first substring: like `CAT GAT`. Furthermore it is inefficient. You can use the *Knuth-Moris-Pratt* algorithm to do this in one linear search. – Willem Van Onsem Jan 13 '16 at 14:53
  • 1
    of course it is gonna work. There is no sequence of proteins in DNA with less or more than 3 letters. – Victor Viola Jan 13 '16 at 14:56
  • I mean nucleoside triphosphate instead of proteins – Victor Viola Jan 13 '16 at 15:05
  • yeah I know that. But who says you have *the start of the DNA string*. You do not have to do all this expensive splitting, etc. – Willem Van Onsem Jan 13 '16 at 15:12
  • Who said the opposite? He is clearly a begginner, does not make sense to explain Knuth-Moris-Pratt approach or something similar. Just solving his problem at the moment. In the right moment he will face more complex questions. – Victor Viola Jan 13 '16 at 15:15
0

i'm guessing you forgot to add return?

def seq(dna):
    dna = dna.count("ATG")
    return dna

print seq("ATGSDSGFAGEFRASFWET")
Bear
  • 550
  • 9
  • 25