For an exercise, I was tasked to convert a text sequence of DNA (eg "TCGATGAATCG..") into a protein sequence (eg "RDFJKDSJKS...").
I used this code:
#rna to aa
def translate(phiseq):
codontable = {
'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W',
}
phipep = ""
for i in range(0, len(phiseq)-(3+len(phiseq)%3), 3):
codon = phiseq[i:i + 3]
phipep += codontable[codon]
return phipep
pep = translate(phiseq)
where phiseq is the DNA sequence in question, codontable is the dictionary index to translate the codon into protein and the loop reads the length of the DNA sequence and outputs the protein sequence. Pep means peptide.
My question is, why does the middle argument (stop) of the range function mean? Namely:
len(phiseq)-(3+len(phiseq)%3)
In my ape brain it just gives a random number that seems to work well for this purpose but I stole this from somewhere in the internet and cannot explain how it works. The place from where I stole it does not explain it either. What am I exactly achieving by this operation?
Thank you for your help.