KKULHARIA LABComputational Biology & BioinformaticsProf. Mahesh Kulharia
Central University of Himachal Pradesh
Bioinfo Compass

Python basics with sequence strings

Python treats DNA as text. A variable name points to a value; quotes mark a string. Python counts positions from zero.

dna = "ATGGCTTTTGAATGGGGTAAACCTGTTGACTACTAA"
print(len(dna))        # number of bases
print(dna[0])          # first base
print(dna[0:3])        # first codon
print(dna.count("G"))  # number of G bases
print(dna.upper())

Calculate GC percentage

For an unambiguous DNA string, GC% = 100 × (G+C)/sequence length.

dna = "ACGT"
gc_count = dna.count("G") + dna.count("C")
gc_percent = 100 * gc_count / len(dna)
print(gc_percent)  # 50.0

In the downloaded module, gc_percent first checks that the string is not empty and contains only A, C, G and T. A program should reject a sequence it cannot handle, rather than silently report a misleading value.

Exercises

  1. What does dna[3:6] return for "ATGGCT"?
  2. How many nucleotides are in "ATGCC"?
  3. Work out GC% for "GGCCAT".
Worked answers

GCT; 5 nucleotides; 4 of 6 bases are G or C, so GC = 66.67%.