Functions and reproducible mutations
A function packages a named operation. Inputs go in parentheses; return sends a result back.
def gc_percent(dna):
dna = dna.upper()
gc_count = dna.count("G") + dna.count("C")
return 100 * gc_count / len(dna)
print(gc_percent("ACGT"))A small seeded random-number generator
The bundled module includes a linear congruential generator (LCG) using seed 12345, and a function that mutates a requested number of distinct positions. The same seed and input produce the same result, which is useful for a classroom debugging exercise.
from bioinfo_practice import mutate_dna original = "ATGGCTTTTGAATGGTAA" mutant = mutate_dna(original, 3, seed=12345) print(original) print(mutant)
This simple generator is not a high-quality scientific random-number generator, does not represent biological mutation processes, and must not be used for security. The function changes positions without replacement and chooses one of the three alternative bases at each selected position. Test a fixed seed and count changed positions.
Exercises
- Call
mutate_dnatwice with the same sequence, mutation count and seed. What should happen? - Ask for more mutations than the sequence length. Why should the function report an error?
Worked answers
The outputs should match exactly because the operation is deterministic for those inputs. More unique positions than the sequence has cannot be selected; rejecting it is clearer than silently doing a different number of mutations.