How to understand a middle argument for the range function in DNA sequence translation?

Viewed 25

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.

2 Answers

range(0, len(phiseq)-(3+len(phiseq)%3), 3)

The arguments here are: range(start, stop, stepsize)

If the stop argument would just be the length of phiseq you could get a KeyError. Near the end of the for loop you could get a slice of phiseq that results in the codon variable being only one or two bases. If you look up that slice in the dictionary you'd get a KeyError.

len(phiseq)-(3+len(phiseq)%3) ensures that the loop stops before an incomplete slice at the end is met. I think it's a little buggy because the 3+ part ensures you don't look at your final codon.

Let's use an example phiseq of 'ATCTTGTCGTAG'

Then len(phiseq) is the length of the input sequence (12 in this case).

(3+len(phiseq)%3) gives you 3 plus the remainder of the length of the sequence, when divided by 3. This is a bit tricky, but in this example means 3+12%3 where 12%3 is equal to 0 so 3+0 so 3.

The range function is simple, (ie, range(1,10,1)) it gives you a range of values, in this example from 1 to 10 incrementing by 1 each time, so 1, 2, 3,...,9 (it stops at 9 because of python coding reasons).

So knowing these things means that the for loop looks at i to i+3, in a safe way which doesn't cause errors by looking outside the length of the inputted sequence.

Related